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.core.GenericEntity;
15 import javax.ws.rs.core.Response;
16
17 import com.wordnik.swagger.core.ApiError;
18 import com.wordnik.swagger.core.ApiErrors;
19 import com.wordnik.swagger.core.ApiOperation;
20 import com.wordnik.swagger.core.ApiParam;
21 import com.wordnik.swagger.core.JavaHelp;
22
23 import eu.scape_project.watch.dao.EntityTypeDAO;
24 import eu.scape_project.watch.dao.PropertyDAO;
25 import eu.scape_project.watch.domain.EntityType;
26 import eu.scape_project.watch.domain.Property;
27 import eu.scape_project.watch.utils.exception.NotFoundException;
28
29 import org.apache.log4j.Logger;
30
31 import thewebsemantic.binding.Jenabean;
32
33
34
35
36
37
38
39
40 public class PropertyResource extends JavaHelp {
41
42
43
44
45 private static final Logger LOG = Logger.getLogger(PropertyResource.class);
46
47
48
49
50
51
52
53
54
55
56
57 @GET
58 @Path("/{type}/{name}")
59 @ApiOperation(value = "Find Property by type and name", notes = "")
60 @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Property or Entity Type not found")})
61 public Response getPropertyByName(
62 @ApiParam(value = "Name of the Entity Type", required = true) @PathParam("type") final String type,
63 @ApiParam(value = "Name of the Property", required = true) @PathParam("name") final String name) {
64 final Property property = PropertyDAO.getInstance().findByEntityTypeAndName(type, name);
65
66 if (property != null) {
67 return Response.ok().entity(property).build();
68 } else {
69 throw new NotFoundException("Property id not found: " + name);
70 }
71 }
72
73
74
75
76
77
78 @GET
79 @Path("/list")
80 @ApiOperation(value = "List all properties", notes = "")
81 public Response listProperty() {
82 final Collection<Property> list = Jenabean.instance().reader().load(Property.class);
83 return Response.ok().entity(new GenericEntity<Collection<Property>>(list) {
84 }).build();
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98 @GET
99 @Path("/list/{type}/{start}/{max}")
100 @ApiOperation(value = "List properties of a type", notes = "")
101 public Response listEntityOfType(
102 @ApiParam(value = "Entity type", required = true) @PathParam("type") final String type,
103 @ApiParam(value = "Index of first item to retrieve", required = true, defaultValue = "0") @PathParam("start") final int start,
104 @ApiParam(value = "Maximum number of items to retrieve", required = true, defaultValue = "100") @PathParam("max") final int max) {
105 final Collection<Property> list = PropertyDAO.getInstance().listWithType(type, start, max);
106 return Response.ok().entity(new GenericEntity<Collection<Property>>(list) {
107 }).build();
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122 @POST
123 @Path("/{type}/{name}")
124 @ApiOperation(value = "Create Entity Type", notes = "This can only be done by an admin user (TODO)")
125 @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Entity type does not exist")})
126 public Response createProperty(
127 @ApiParam(value = "Entity type name (must exist)", required = true) @PathParam("type") final String type,
128 @ApiParam(value = "Property name (must be unique)", required = true) @PathParam("name") final String name,
129 @ApiParam(value = "Property description", required = false) final String description) {
130
131 LOG.debug("Create property name=" + name + " description=" + description + " in type=" + type);
132 final EntityType entityType = EntityTypeDAO.getInstance().findById(type);
133
134 if (entityType != null) {
135 final Property property = new Property(entityType, name, description);
136 property.save();
137 return Response.ok().entity(property).build();
138 } else {
139 throw new NotFoundException("Entity type not found: " + type);
140 }
141
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155 @PUT
156 @Path("/{type}/{name}")
157 @ApiOperation(value = "Update Property of Type", notes = "This can only be done by an admin user (TODO)")
158 @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Property not found")})
159 public Response updateProperty(
160 @ApiParam(value = "Entity type that owns Property", required = true) @PathParam("type") final String type,
161 @ApiParam(value = "Name that needs to be deleted", required = true) @PathParam("name") final String name,
162 @ApiParam(value = "Updated property object", required = true) final Property property) {
163 final Property original = PropertyDAO.getInstance().findByEntityTypeAndName(type, name);
164 if (original != null) {
165 original.delete();
166 property.save();
167 return Response.ok().entity(property).build();
168 } else {
169 throw new NotFoundException("Property not found: " + name);
170 }
171 }
172
173
174
175
176
177
178
179
180
181
182
183 @DELETE
184 @Path("/{type}/{name}")
185 @ApiOperation(value = "Delete property", notes = "This can only be done by an admin user (TODO)")
186 @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Property or Entity Type not found")})
187 public Response deleteProperty(
188 @ApiParam(value = "Entity type that owns property", required = true) @PathParam("type") final String type,
189 @ApiParam(value = "The name of the property to be deleted", required = true) @PathParam("name") final String name) {
190 final Property property = PropertyDAO.getInstance().findByEntityTypeAndName(type, name);
191
192 if (property != null) {
193 property.delete();
194 return Response.ok().entity(property).build();
195 } else {
196 throw new NotFoundException("Property not found: " + name);
197 }
198
199 }
200
201 }