1
2
3
4 package eu.scape_project.watch.rest.resource;
5
6 import java.util.Collection;
7
8 import javax.ws.rs.DELETE;
9 import javax.ws.rs.GET;
10 import javax.ws.rs.POST;
11 import javax.ws.rs.PUT;
12 import javax.ws.rs.Path;
13 import javax.ws.rs.PathParam;
14 import javax.ws.rs.QueryParam;
15 import javax.ws.rs.core.GenericEntity;
16 import javax.ws.rs.core.Response;
17
18 import com.wordnik.swagger.core.ApiError;
19 import com.wordnik.swagger.core.ApiErrors;
20 import com.wordnik.swagger.core.ApiOperation;
21 import com.wordnik.swagger.core.ApiParam;
22 import com.wordnik.swagger.core.JavaHelp;
23
24 import eu.scape_project.watch.dao.EntityDAO;
25 import eu.scape_project.watch.dao.EntityTypeDAO;
26 import eu.scape_project.watch.domain.Entity;
27 import eu.scape_project.watch.domain.EntityType;
28 import eu.scape_project.watch.utils.exception.NotFoundException;
29
30 import org.apache.log4j.Logger;
31
32
33
34
35
36
37
38
39 public class EntityResource extends JavaHelp {
40
41
42
43
44 private static final Logger LOG = Logger.getLogger(EntityResource.class);
45
46
47
48
49
50
51
52
53
54
55 @GET
56 @Path("/{name}")
57 @ApiOperation(value = "Find Entity by name", notes = "")
58 @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Entity not found")})
59 public Response getEntityByName(
60 @ApiParam(value = "Name of the Entity", required = true) @PathParam("name") final String name) {
61
62 final Entity entity = EntityDAO.getInstance().findById(name);
63
64 if (entity != null) {
65 return Response.ok().entity(entity).build();
66 } else {
67 throw new NotFoundException("Entity not found: " + name);
68 }
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82 @GET
83 @Path("/list")
84 @ApiOperation(value = "List entities", notes = "")
85 public Response listEntityOfType(
86 @ApiParam(value = "Entity type", required = true) @QueryParam("type") final String type,
87 @ApiParam(value = "Index of first item to retrieve", required = true) @QueryParam("start") final int start,
88 @ApiParam(value = "Maximum number of items to retrieve", required = true) @QueryParam("max") final int max) {
89 final Collection<Entity> list = EntityDAO.getInstance().listWithType(type, start, max);
90 return Response.ok().entity(new GenericEntity<Collection<Entity>>(list) {
91 }).build();
92 }
93
94
95
96
97
98
99
100
101
102
103
104 @POST
105 @Path("/{name}")
106 @ApiOperation(value = "Create Entity", notes = "This can only be done by a logged user (TODO)")
107 @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Entity type not found")})
108 public Response createEntity(
109 @ApiParam(value = "Entity name (must be unique)", required = true) @PathParam("name") final String name,
110 @ApiParam(value = "Entity Type (must exist)", required = true) final String type) {
111
112 final EntityType entitytype = EntityTypeDAO.getInstance().findById(type);
113
114 if (entitytype != null) {
115 final Entity entity = new Entity(entitytype, name);
116 entity.save();
117 return Response.ok().entity(entity).build();
118 } else {
119 throw new NotFoundException("Entity type not found: " + type);
120 }
121
122 }
123
124
125
126
127
128
129
130
131
132
133 @PUT
134 @Path("/{name}")
135 @ApiOperation(value = "Update Entity", notes = "This can only be done by a logged user (TODO)")
136 @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Entity not found")})
137 public Response updateEntity(
138 @ApiParam(value = "Name that need to be deleted", required = true) @PathParam("name") final String name,
139 @ApiParam(value = "Updated Entity object", required = true) final Entity entity) {
140 final Entity original = EntityDAO.getInstance().findById(name);
141 if (original != null) {
142 original.delete();
143 entity.save();
144 return Response.ok().entity(entity).build();
145 } else {
146 throw new NotFoundException("Entity not found: " + name);
147 }
148 }
149
150
151
152
153
154
155
156
157
158
159 @DELETE
160 @Path("/{name}")
161 @ApiOperation(value = "Delete Entity", notes = "This can only be done by a logged user (TODO)")
162 @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Entity not found")})
163 public Response deleteEntity(
164 @ApiParam(value = "The name of the Entity to be deleted", required = true) @PathParam("name") final String name) {
165 LOG.info("deleting entity name: " + name);
166 final Entity entity = EntityDAO.getInstance().findById(name);
167 if (entity != null) {
168 entity.delete();
169 return Response.ok().entity(entity).build();
170 } else {
171 throw new NotFoundException("Entity type not found: " + name);
172 }
173 }
174
175 }