View Javadoc

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   * REST API for {@link PropertyValue} operations.
34   * 
35   * @author Luis Faria <lfaria@keep.pt>
36   * 
37   */
38  public class PropertyValueResource extends JavaHelp {
39  
40    /**
41     * The logger.
42     */
43    // private static final Logger LOG =
44    // LoggerFactory.getLogger(PropertyValueResource.class);
45  
46    /**
47     * Get an existing {@link PropertyValue}.
48     * 
49     * @param entityName
50     *          The name of the {@link Entity} this property value refers to.
51     * @param propertyName
52     *          The name of the {@link Property} this property value refers to.
53     * @return The {@link PropertyValue} or throws {@link NotFoundException} if
54     *         not found.
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     * Ge a list with all existing {@link PropertyValue}, independently of the
74     * {@link Entity} or {@link Property} they refer to.
75     * 
76     * @return The complete list of {@link PropertyValue} in the KB
77     */
78    @GET
79    @Path("/list")
80    @ApiOperation(value = "List all property values", notes = "")
81    public Response listPropertyValue() {
82      // TODO list property values of an entity
83      // TODO list property values of an entity and property
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     * Create a new property value.
91     * 
92     * @param entityName
93     *          The name of the related {@link Entity}
94     * @param propertyName
95     *          The name of the related {@link Property}
96     * @param value
97     *          The value of the {@link Property} for the {@link Entity}
98     * @return The newly created {@link PropertyValue}.
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    * Update an existing {@link PropertyValue}.
130    * 
131    * @param entityName
132    *          The name of the related {@link Entity}.
133    * @param propertyName
134    *          The name of the related {@link Property}.
135    * @param value
136    *          The new value of the {@link Property} for the {@link Entity}
137    * @return The updated {@link PropertyValue}.
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     // TODO support several property values, by versioning
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    * Delete an existing {@link PropertyValue}.
161    * 
162    * @param entityName
163    *          The name of the related {@link Entity}.
164    * @param propertyName
165    *          The name of the related {@link Property}.
166    * @return The deleted {@link PropertyValue} or throws
167    *         {@link NotFoundException} if not found.
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 }