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.domain.EntityType;
25  import eu.scape_project.watch.utils.exception.NotFoundException;
26  
27  import org.apache.log4j.Logger;
28  
29  import thewebsemantic.binding.Jenabean;
30  
31  /**
32   * 
33   * REST API for {@link EntityType} operations.
34   * 
35   * @author Luis Faria <lfaria@keep.pt>
36   * 
37   */
38  public class EntityTypeResource extends JavaHelp {
39  
40    /**
41     * Logger.
42     */
43    private static final Logger LOG = Logger.getLogger(EntityTypeResource.class);
44  
45    /**
46     * Get an existing {@link EntityType}.
47     * 
48     * @param name
49     *          The entity type name.
50     * @return The entity type or throws {@link NotFoundException} if not found.
51     */
52    @GET
53    @Path("/{name}")
54    @ApiOperation(value = "Find Entity Type by name", notes = "")
55    @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Entity Type not found")})
56    public Response getEntityTypeByName(
57      @ApiParam(value = "Name of the Entity Type", required = true) @PathParam("name") final String name) {
58      final EntityType entitytype = EntityTypeDAO.getInstance().findById(name);
59  
60      if (entitytype != null) {
61        return Response.ok().entity(entitytype).build();
62      } else {
63        throw new NotFoundException("Entity Type id not found: " + name);
64      }
65    }
66  
67    /**
68     * List all entity types in the KB.
69     * 
70     * @return The complete list of {@link EntityType}.
71     */
72    @GET
73    @Path("/list")
74    @ApiOperation(value = "List all entity types", notes = "")
75    public Response listEntityType() {
76      final Collection<EntityType> list = Jenabean.instance().reader().load(EntityType.class);
77      return Response.ok().entity(new GenericEntity<Collection<EntityType>>(list) {
78      }).build();
79    }
80  
81    /**
82     * Create a new {@link EntityType}.
83     * 
84     * @param name
85     *          The entity type unique name
86     * @param description
87     *          A descriptive information about the entity type.
88     * @return The created entity type.
89     */
90    @POST
91    @Path("/{name}")
92    @ApiOperation(value = "Create Entity Type", notes = "This can only be done by an admin user (TODO)")
93    public Response createEntityType(
94      @ApiParam(value = "Entity type name (must be unique)", required = true) @PathParam("name") final String name,
95      @ApiParam(value = "Entity type description", required = false) final String description) {
96      LOG.info("creating entity name: " + name);
97      final EntityType entitytype = new EntityType(name, description);
98      entitytype.save();
99      return Response.ok().entity(entitytype).build();
100 
101   }
102 
103   /**
104    * Update an existing {@link EntityType}.
105    * 
106    * @param name
107    *          The existing {@link EntityType} name.
108    * @param entitytype
109    *          The new entity type that should replace the existing one
110    * @return The new entity type merged into the KB
111    */
112   @PUT
113   @Path("/{name}")
114   @ApiOperation(value = "Update Entity Type", notes = "This can only be done by an admin user (TODO)")
115   @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Entity Type not found")})
116   public Response updateEntityType(
117     @ApiParam(value = "Name that need to be deleted", required = true) @PathParam("name") final String name,
118     @ApiParam(value = "Updated Entity Type object", required = true) final EntityType entitytype) {
119     final EntityType original = EntityTypeDAO.getInstance().findById(name);
120     if (original != null) {
121       original.delete();
122       entitytype.save();
123       return Response.ok().entity(entitytype).build();
124     } else {
125       throw new NotFoundException("Entity type not found: " + name);
126     }
127   }
128 
129   /**
130    * Delete an existing entity type.
131    * 
132    * @param name
133    *          The name of the {@link EntityType} to delete.
134    * @return The deleted {@link EntityType} or throws {@link NotFoundException}
135    *         if not found
136    */
137   @DELETE
138   @Path("/{name}")
139   @ApiOperation(value = "Delete Entity Type", notes = "This can only be done by an admin user (TODO)")
140   @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Entity Type not found")})
141   public Response deleteEntityType(
142     @ApiParam(value = "The name of the Entity Type to be deleted", required = true) @PathParam("name") final String name) {
143     LOG.info("deleting entity type name: " + name);
144 
145     final EntityType entitytype = EntityTypeDAO.getInstance().findById(name);
146     if (entitytype != null) {
147       entitytype.delete();
148       return Response.ok().entity(entitytype).build();
149     } else {
150       throw new NotFoundException("Entity type not found: " + name);
151     }
152   }
153 
154 }