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.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   * REST API for {@link Property} operations.
36   * 
37   * @author Luis Faria <lfaria@keep.pt>
38   * 
39   */
40  public class PropertyResource extends JavaHelp {
41  
42    /**
43     * The logger.
44     */
45    private static final Logger LOG = Logger.getLogger(PropertyResource.class);
46  
47    /**
48     * Get a Property.
49     * 
50     * @param type
51     *          The name of the {@link EntityType} this property belongs to
52     * @param name
53     *          The name of the property
54     * @return The {@link Property} or throws {@link NotFoundException} if not
55     *         found
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     * List all Properties independently of the {@link EntityType}.
75     * 
76     * @return The complete list of Properties within the KB
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     * List all properties of a {@link EntityType}.
89     * 
90     * @param type
91     *          The name of the {@link EntityType}
92     * @param start
93     *          The index of the first item to retrieve
94     * @param max
95     *          The maximum number of items to retrieve
96     * @return A list of {@link Property} filtered by the constraints above.
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    * Create a new {@link Property}.
112    * 
113    * @param type
114    *          The name of the {@link EntityType} that this property belongs to.
115    * @param name
116    *          The name of the property, unique within the {@link EntityType}
117    * @param description
118    *          Descriptive information about the property
119    * @return The newly created Property or throws {@link NotFoundException} if
120    *         the {@link EntityType} is not found.
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     // TODO support data type
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    * Update an existing {@link Property}.
146    * 
147    * @param type
148    *          The name of the {@link EntityType} the property belongs to
149    * @param name
150    *          The name of the property
151    * @param property
152    *          The new Property that should replace the existing one
153    * @return The new Property merged into the KB
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    * Delete an existing {@link Property}.
175    * 
176    * @param type
177    *          The name of the {@link EntityType} the property belongs to
178    * @param name
179    *          The name of the property
180    * @return The deleted {@link Property} or throws {@link NotFoundException} if
181    *         not found.
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 }