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.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   * REST API for {@link Entity} operations.
35   * 
36   * @author Luis Faria <lfaria@keep.pt>
37   * 
38   */
39  public class EntityResource extends JavaHelp {
40  
41    /**
42     * Logger.
43     */
44    private static final Logger LOG = Logger.getLogger(EntityResource.class);
45  
46    /**
47     * Get a {@link Entity} by its unique name. REST interface for
48     * {@link EntityDAO#findById(String)}.
49     * 
50     * @param name
51     *          The name of the Entity
52     * @return The Entity object with that name or throws
53     *         {@link NotFoundException} if none found.
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     * Get the list of Entities of a defined {@link EntityType}.
73     * 
74     * @param type
75     *          The name of the {@link EntityType}
76     * @param start
77     *          The index of the first item to retrieve
78     * @param max
79     *          The maximum number of items to retrieve
80     * @return The list of entities filtered by the above constraints
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     * Create a new {@link Entity}.
96     * 
97     * @param name
98     *          The name of the entity
99     * @param type
100    *          the name of the {@link EntityType} this entity belongs to
101    * @return The created Entity or throws {@link NotFoundException} if the type
102    *         is not found.
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    * Update an existing entity.
126    * 
127    * @param name
128    *          The name of the existing entity
129    * @param entity
130    *          The new entity that will replace the existing one.
131    * @return The entity merged into the KB.
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    * Delete an existing entity.
152    * 
153    * @param name
154    *          the name of the entity
155    * @return The deleted entity or throws {@link NotFoundException} if the
156    *         entity to delete is not found
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 }