View Javadoc

1   package org.maxur.perfmodel.backend.rest.resources;
2   
3   
4   import org.jsondoc.core.annotation.*;
5   import org.jsondoc.core.pojo.ApiVerb;
6   import org.maxur.perfmodel.backend.domain.ConflictException;
7   import org.maxur.perfmodel.backend.domain.Project;
8   import org.maxur.perfmodel.backend.domain.Repository;
9   import org.maxur.perfmodel.backend.rest.dto.ProjectDto;
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  import javax.inject.Inject;
14  import javax.ws.rs.*;
15  import javax.ws.rs.core.MediaType;
16  import javax.ws.rs.core.Response;
17  import java.util.Collection;
18  import java.util.Optional;
19  
20  import static com.google.common.base.Strings.isNullOrEmpty;
21  import static java.lang.String.format;
22  import static org.maxur.perfmodel.backend.rest.WebException.*;
23  import static org.maxur.perfmodel.backend.rest.dto.ProjectDto.dto;
24  import static org.maxur.perfmodel.backend.rest.dto.ProjectDto.dtoList;
25  
26  
27  /**
28   * @author Maxim Yunusov
29   * @version 1.0 01.09.13
30   */
31  @Path("/{a:project}")
32  @Api(name = "Project Resource", description = "Methods for managing Project")
33  @ApiVersion(since = "1.0")
34  public class ProjectResource {
35  
36      private static final String PROJECT_IS_NOT_SAVED = "Project is not saved";
37  
38      private static final Logger LOGGER = LoggerFactory.getLogger(ProjectResource.class);
39  
40      @Inject
41      private Repository<Project> repository;
42  
43      @GET
44      @Produces(MediaType.APPLICATION_JSON)
45      @ApiMethod(
46              path = "/project",
47              verb = ApiVerb.GET,
48              description = "Gets all projects",
49              produces = { MediaType.APPLICATION_JSON },
50              responsestatuscode = "200 - OK"
51      )
52      @ApiErrors(apierrors={
53              @ApiError(code="500", description="Application critical error")
54      })
55      @ApiResponseObject
56      public Collection<ProjectDto> findAll() {
57          return dtoList(repository.findAll());
58      }
59  
60      @GET
61      @Path("/{id}")
62      @Produces(MediaType.APPLICATION_JSON)
63      @ApiMethod(
64              path = "/project/{id}",
65              verb = ApiVerb.GET,
66              description = "Get the project by it's identifier",
67              produces = { MediaType.APPLICATION_JSON },
68              responsestatuscode = "200 - OK"
69      )
70      @ApiErrors(apierrors={
71              @ApiError(code="400", description="Bad request"),
72              @ApiError(code="404", description="Project not found"),
73              @ApiError(code="500", description="Application critical error")
74      })
75      @ApiResponseObject
76      public ProjectDto load(
77              @ApiPathParam(name = "id", description = "The project identifier")
78              @PathParam("id") String id
79      ) {
80          checkIdIsNotNull(id);
81          final Optional<Project> result = repository.get(id);
82          checkResult(id, result);
83          return dto(result.get());
84      }
85  
86      @DELETE
87      @Path("/{id}")
88      @Produces(MediaType.APPLICATION_JSON)
89      @ApiMethod(
90              path = "/project/{id}",
91              verb = ApiVerb.DELETE,
92              description = "Delete the project by it's identifier",
93              produces = { MediaType.APPLICATION_JSON },
94              responsestatuscode = "200 - OK"
95      )
96      @ApiErrors(apierrors={
97              @ApiError(code="400", description="Bad request"),
98              @ApiError(code="404", description="Project not found"),
99              @ApiError(code="500", description="Application critical error")
100     })
101     @ApiResponseObject
102     public ProjectDto delete(
103             @ApiPathParam(name = "id", description = "The project identifier")
104             @PathParam("id") String id
105     ) {
106         checkIdIsNotNull(id);
107         final Optional<Project> result = repository.remove(id);
108         checkResult(id, result);
109         return dto(result.get());
110     }
111 
112     @POST
113     @Consumes(MediaType.APPLICATION_JSON)
114     @Produces(MediaType.APPLICATION_JSON)
115     @ApiMethod(
116             path = "/project",
117             verb = ApiVerb.POST,
118             description = "Create new project version by project's identifier",
119             produces = { MediaType.APPLICATION_JSON },
120             consumes = { MediaType.APPLICATION_JSON },
121             responsestatuscode = "201 - Created"
122     )
123     @ApiErrors(apierrors={
124             @ApiError(code="400", description="Bad request"),
125             @ApiError(code="404", description="Project not found"),
126             @ApiError(code="500", description="Application critical error")
127     })
128     @ApiResponseObject
129     public Response create(
130             @ApiBodyObject final ProjectDto dto
131     ) {
132         try {
133             final Project project = dto.assemble();
134             checkIdIsNotNull(dto.getId());
135             final Optional<Project> result = repository.add(project);
136             return created(result.get());
137         } catch (NumberFormatException e) {
138             LOGGER.error(PROJECT_IS_NOT_SAVED, e);
139             throw badRequestException(PROJECT_IS_NOT_SAVED, e.getMessage());
140         } catch (ConflictException e) {
141             LOGGER.error(format("%s: %s", PROJECT_IS_NOT_SAVED, e.getMessage()));
142             throw conflictException(PROJECT_IS_NOT_SAVED, e.getMessage());
143         }
144     }
145 
146     @PUT
147     @Path("/{id}")
148     @Consumes(MediaType.APPLICATION_JSON)
149     @Produces(MediaType.APPLICATION_JSON)
150     @ApiMethod(
151             path = "/project/{id}",
152             verb = ApiVerb.PUT,
153             description = "Create new project version by project's identifier",
154             produces = { MediaType.APPLICATION_JSON },
155             consumes = { MediaType.APPLICATION_JSON },
156             responsestatuscode = "200 - OK"
157     )
158     @ApiErrors(apierrors={
159             @ApiError(code="400", description="Bad request"),
160             @ApiError(code="404", description="Project not found"),
161             @ApiError(code="500", description="Application critical error")
162     })
163     @ApiResponseObject
164     public ProjectDto update(
165             @ApiPathParam(name = "id", description = "The project identifier")
166             @PathParam("id") String id,
167             @ApiBodyObject final ProjectDto dto
168     ) {
169         try {
170             checkIdIsNotNull(id);
171             final Project project = dto.assemble();
172             final Optional<Project> result = repository.amend(project);
173             checkResult(id, result);
174             return dto(result.get());
175         } catch (NumberFormatException e) {
176             LOGGER.error(PROJECT_IS_NOT_SAVED, e);
177             throw badRequestException(PROJECT_IS_NOT_SAVED, e.getMessage());
178         } catch (ConflictException e) {
179             LOGGER.error(format("%s: %s", PROJECT_IS_NOT_SAVED, e.getMessage()));
180             throw conflictException(PROJECT_IS_NOT_SAVED, e.getMessage());
181         }
182     }
183 
184     private Response created(final Project result) {
185         return Response.status(Response.Status.CREATED).entity(dto(result)).build();
186     }
187 
188     private void checkIdIsNotNull(final String id) {
189         if (isNullOrEmpty(id)) {
190             final String msg = "Bad get request. 'Id' must not be null";
191             LOGGER.error(msg);
192             throw notFoundException(msg);
193         }
194     }
195 
196     private void checkResult(final String id, final Optional<Project> result) {
197         if (!result.isPresent()) {
198             final String msg = format("Project '%s' is not founded", id);
199             LOGGER.error(msg);
200             throw notFoundException(msg);
201         }
202     }
203 
204 }