/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.carlomicieli.rest.resources;
import static com.github.carlomicieli.rest.representations.ScalesArrayRepresentation.createArray;
import java.net.URI;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.dao.DataAccessException;
import com.github.carlomicieli.entities.Scale;
import com.github.carlomicieli.rest.representations.ScaleRepresentation;
import com.github.carlomicieli.services.ScaleService;
import com.sun.jersey.api.spring.Autowire;
/**
*
* @author Carlo Micieli
*
*/
@Autowire
@Scope("request")
@Path("/scales")
public class ScalesResource {
private static final Logger log = LoggerFactory.getLogger("rest");
private @Inject ScaleService scaleService;
private CacheControl getCacheControl() {
CacheControl cache = new CacheControl();
cache.setMaxAge(10);
cache.setPrivate(true);
return cache;
}
/**
* Return the list of all brands.
* <p>
* <code>GET /brands</code>
* </p>
*
* @return the list of brands.
*/
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getScales() {
log.info("GET /scales");
try {
final ScaleRepresentation[] scales =
createArray(scaleService.getAllScales());
return Response.ok(scales)
.cacheControl(getCacheControl())
.build();
}
catch( DataAccessException ex ) {
log.error(ex.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
}
}
@GET
@Path("/{scaleId}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getScale(@PathParam("scaleId") long scaleId) {
log.info("GET /scales/{}", scaleId);
try {
final ScaleRepresentation scaleRep =
new ScaleRepresentation(scaleService.getScale(scaleId));
return Response.ok(scaleRep)
.tag(scaleRep.tag())
.cacheControl(getCacheControl())
.build();
}
catch( DataAccessException ex ) {
log.error(ex.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
}
}
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(MultivaluedMap<String, String> formValues) {
return post(new ScaleRepresentation(formValues));
}
@POST
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response post(ScaleRepresentation scaleRep) {
log.info("POST /scales");
final Scale scale = scaleRep.getInner();
try {
long id = scaleService.create(scale);
// the response contains the ETag for the resource just created.
return Response.created(URI.create(Long.toString(id)))
.tag(scaleRep.tag())
.build();
}
catch( RuntimeException ex ) {
log.error(ex.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
}
}
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response put(MultivaluedMap<String, String> formValues) {
return put(new ScaleRepresentation(formValues));
}
@PUT
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response put(ScaleRepresentation scaleRep) {
log.info("PUT /scales");
final Scale scale = scaleRep.getInner();
try {
scaleService.save(scale);
// the response contains the ETag for the resource just created.
return Response.noContent()
.tag(scaleRep.tag())
.build();
}
catch( RuntimeException ex ) {
log.error(ex.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
}
}
@DELETE
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public void delete(ScaleRepresentation scaleRep) {
delete(scaleRep.getId());
}
@DELETE
@Path("/{scaleId}")
public void delete(@PathParam("scaleId") long scaleId) {
log.info("DELETE /scales");
try {
scaleService.remove(new Scale(scaleId));
}
catch( RuntimeException ex ) {
log.error(ex.getMessage());
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
}
}