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.EntityDAO;
24 import eu.scape_project.watch.dao.PropertyDAO;
25 import eu.scape_project.watch.dao.PropertyValueDAO;
26 import eu.scape_project.watch.domain.Entity;
27 import eu.scape_project.watch.domain.Property;
28 import eu.scape_project.watch.domain.PropertyValue;
29 import eu.scape_project.watch.utils.exception.NotFoundException;
30 import thewebsemantic.binding.Jenabean;
31
32
33
34
35
36
37
38 public class PropertyValueResource extends JavaHelp {
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 @GET
57 @Path("/{entity}/{property}")
58 @ApiOperation(value = "Find property value by entity and property", notes = "")
59 @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Property value not found")})
60 public Response getPropertyValueByName(
61 @ApiParam(value = "Name of the entity type", required = true) @PathParam("entity") final String entityName,
62 @ApiParam(value = "Name of the property", required = true) @PathParam("property") final String propertyName) {
63 final PropertyValue propertyValue = PropertyValueDAO.getInstance().findByEntityAndName(entityName, propertyName);
64
65 if (propertyValue != null) {
66 return Response.ok().entity(propertyValue).build();
67 } else {
68 throw new NotFoundException("Property value not found, entity=" + entityName + ", property=" + propertyName);
69 }
70 }
71
72
73
74
75
76
77
78 @GET
79 @Path("/list")
80 @ApiOperation(value = "List all property values", notes = "")
81 public Response listPropertyValue() {
82
83
84 final Collection<PropertyValue> list = Jenabean.instance().reader().load(PropertyValue.class);
85 return Response.ok().entity(new GenericEntity<Collection<PropertyValue>>(list) {
86 }).build();
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100 @POST
101 @Path("/{entity}/{property}")
102 @ApiOperation(value = "Create property value", notes = "This can only be done by a logged user (TODO)")
103 @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Entity or property does not exist")})
104 public Response createPropertyValue(
105 @ApiParam(value = "Entity name (must exist)", required = true) @PathParam("entity") final String entityName,
106 @ApiParam(value = "Property name (must exist)", required = true) @PathParam("property") final String propertyName,
107 @ApiParam(value = "Property value", required = false) final String value) {
108
109 final Entity entity = EntityDAO.getInstance().findById(entityName);
110
111 if (entity != null) {
112 final String typeName = entity.getEntityType().getName();
113 final Property property = PropertyDAO.getInstance().findByEntityTypeAndName(typeName, propertyName);
114
115 if (property != null) {
116 final PropertyValue propertyValue = new PropertyValue(entity, property, value);
117 propertyValue.save();
118 return Response.ok().entity(propertyValue).build();
119 } else {
120 throw new NotFoundException("Property not found type=" + typeName + ", name=" + propertyName);
121 }
122 } else {
123 throw new NotFoundException("Entity not found: " + entityName);
124 }
125
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139 @PUT
140 @Path("/{entity}/{property}")
141 @ApiOperation(value = "Update property value", notes = "This can only be done by a logged user (TODO)")
142 @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Entity or property not found")})
143 public Response updatePropertyValue(
144 @ApiParam(value = "Entity related to the property value", required = true) @PathParam("entity") final String entityName,
145 @ApiParam(value = "Property related to the property value", required = true) @PathParam("property") final String propertyName,
146 @ApiParam(value = "Updated value", required = true) final String value) {
147
148 final PropertyValue propertyValue = PropertyValueDAO.getInstance().findByEntityAndName(entityName, propertyName);
149
150 if (propertyValue != null) {
151 propertyValue.setValue(value);
152 propertyValue.save();
153 return Response.ok().entity(propertyName).build();
154 } else {
155 throw new NotFoundException("Property value not found entity=" + entityName + ", property=" + propertyName);
156 }
157 }
158
159
160
161
162
163
164
165
166
167
168
169 @DELETE
170 @Path("/{entity}/{property}")
171 @ApiOperation(value = "Delete property value", notes = "This can only be done by an admin user (TODO)")
172 @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Property value not found")})
173 public Response deletePropertyValue(
174 @ApiParam(value = "Entity related with the property value", required = true) @PathParam("entity") final String entityName,
175 @ApiParam(value = "Property related with the property value", required = true) @PathParam("property") final String propertyName) {
176 final PropertyValue propertyValue = PropertyValueDAO.getInstance().findByEntityAndName(entityName, propertyName);
177
178 if (propertyValue != null) {
179 propertyValue.delete();
180 return Response.ok().entity(propertyValue).build();
181 } else {
182 throw new NotFoundException("Property value not found entity=" + entityName + ", property=" + propertyName);
183 }
184 }
185 }