Roberto Sánchez
2014-01-08 02c2bc80c76440e849a73e9e7b58f1051f3cc47d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package net.curisit.securis.services;
import java.util.Date;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import net.curisit.integrity.commons.Utils;
import net.curisit.securis.db.LicenseType;
import net.curisit.securis.utils.TokenHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.inject.persist.Transactional;
/**
 * LicenseType resource, this service will provide methods to create, modify and delete license types
 * 
 * @author roberto <roberto.sanchez@curisit.net>
 */
@Path("/licensetype")
public class LicenseTypeResource {
   @Inject
   TokenHelper tokenHelper;
   @Inject
   Provider<EntityManager> emProvider;
   private static final Logger log = LoggerFactory.getLogger(LicenseTypeResource.class);
   public LicenseTypeResource() {
   }
   /**
    * 
    * @return the server version in format majorVersion.minorVersion
    */
   @GET
   @Path("/")
   @Produces(
       { MediaType.APPLICATION_JSON })
   public Response index() {
       log.info("Getting license types list ");
       EntityManager em = emProvider.get();
       TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class);
       List<LicenseType> list = q.getResultList();
       return Response.ok(list).build();
   }
   /**
    * 
    * @return the server version in format majorVersion.minorVersion
    */
   @GET
   @Path("/{ltid}")
   @Produces(
       { MediaType.APPLICATION_JSON })
   public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
       log.info("Getting license type data for id: {}: ", ltid);
       if (ltid == null || ltid.equals("")) {
           log.error("LicenseType ID is mandatory");
           return Response.status(Status.NOT_FOUND).build();
       }
       EntityManager em = emProvider.get();
       LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid));
       if (lt == null) {
           log.error("LicenseType with id {} not found in DB", ltid);
           return Response.status(Status.NOT_FOUND).build();
       }
       return Response.ok(lt).build();
   }
   @POST
   @Path("/")
   @Consumes(MediaType.APPLICATION_JSON)
   @Produces(
       { MediaType.APPLICATION_JSON })
   @Transactional
   public Response create(LicenseType app, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
       log.info("Creating new license type");
       EntityManager em = emProvider.get();
       app.setCreationTimestamp(new Date());
       em.persist(app);
       return Response.ok(app).build();
   }
   @PUT
   @POST
   @Path("/{ltid}")
   @Transactional
   @Consumes(MediaType.APPLICATION_JSON)
   @Produces(
       { MediaType.APPLICATION_JSON })
   public Response modify(LicenseType app, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
       log.info("Modifying license type with id: {}", ltid);
       EntityManager em = emProvider.get();
       LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid));
       if (currentlt == null) {
           log.error("LicenseType with id {} not found in DB", ltid);
           return Response.status(Status.NOT_FOUND).build();
       }
       currentlt.setName(app.getName());
       currentlt.setDescription(app.getDescription());
       em.persist(currentlt);
       return Response.ok(currentlt).build();
   }
   @DELETE
   @Path("/{ltid}")
   @Transactional
   @Produces(
       { MediaType.APPLICATION_JSON })
   public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) {
       log.info("Deleting app with id: {}", ltid);
       EntityManager em = emProvider.get();
       LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid));
       if (app == null) {
           log.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid);
           return Response.status(Status.NOT_FOUND).build();
       }
       em.remove(app);
       return Response.ok(Utils.createMap("success", true, "id", ltid)).build();
   }
}