Salome HOME
Attribute DAO
[modules/gde.git] / projects / GDE_App / GDE-war / src / java / com / edf / gde / services / AttributesService.java
1 /*
2  * (C) 2015 EDF
3  */
4 package com.edf.gde.services;
5
6 import com.edf.gde.ejb.MetadataEJB;
7 import com.edf.gde.ejb.PermissionsManagerEJB;
8 import com.edf.gde.ejb.UserEJB;
9 import com.edf.gde.tools.Credentials;
10 import com.edf.gde.transferables.AttributeGroupTO;
11 import com.edf.gde.transferables.AttributeTO;
12 import com.edf.gde.transferables.CommandTO;
13 import com.edf.gde.transferables.responses.CommandResultTO;
14 import java.util.logging.Logger;
15 import javax.ejb.EJB;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18
19 /**
20  *
21  * @author kavoos
22  */
23 public class AttributesService extends BaseService {
24
25     public static final String ServiceName = "AttributesService";
26     public static final int CREATEATTRIBUTE = 1;
27     public static final int DELETEATTRIBUTE = 2;
28     public static final int READATTRIBUTE = 3;
29     public static final int CREATEATTRIBUTEGROUP = 4;
30     public static final int DELETEATTRIBUTEGROUP = 5;
31     public static final int UPDATEATTRIBUTEGROUP = 6;
32     public static final int READATTRIBUTEGROUP = 7;
33
34     @EJB
35     private UserEJB userEjb;
36     @EJB
37     private PermissionsManagerEJB pm;
38     @EJB
39     private MetadataEJB mjb;
40
41     @Override
42     public void processRequest(HttpServletRequest request, HttpServletResponse response) {
43         response.setContentType("text/html;charset=UTF-8");
44         Logger logger = Logger.getLogger(ServiceName);
45         CommandTO commandTO = getCommand(request);
46         CommandResultTO resultTO = new CommandResultTO();
47         Credentials credentials = getCredentials(request);
48         //userEjb.checkPassword(credentials.getLogin(), credentials.getPassword());
49         //pm.checkPermission(credentials.getLogin(), ServiceName, commandTO.getMethod());
50         try {
51             switch (commandTO.getMethod()) {
52                 case CREATEATTRIBUTE: {
53                     AttributeTO ato = fromJson(commandTO.getData(), AttributeTO.class);
54                     AttributeTO nato = mjb.createAttribute(ato);
55                     resultTO.setData(toJson(nato));
56                 }
57                 break;
58                 case DELETEATTRIBUTE: {
59                     long attributeId = commandTO.getLong("attributeId");
60                     mjb.deleteAttribute(attributeId);
61                 }
62                 break;
63                 case READATTRIBUTE: {
64                     long attributeId = commandTO.getLong("attributeId");
65                     AttributeTO ato = mjb.readAttribute(attributeId);
66                     resultTO.setData(toJson(ato));
67                 }
68                 break;
69                 case CREATEATTRIBUTEGROUP: {
70                     AttributeGroupTO agto = fromJson(commandTO.getData(), AttributeGroupTO.class);
71                     AttributeGroupTO nagto = mjb.createAttributeGroup(agto);
72                     resultTO.setData(toJson(nagto));
73                 }
74                 break;
75                 case DELETEATTRIBUTEGROUP: {
76                     long attributeGroupId = commandTO.getLong("attributeGroupId");
77                     mjb.deleteAttributeGroup(attributeGroupId);
78                 }
79                 break;
80                 case UPDATEATTRIBUTEGROUP: {
81                     AttributeGroupTO agto = fromJson(commandTO.getData(), AttributeGroupTO.class);
82                     AttributeGroupTO nagto = mjb.updateAttributeGroup(agto);
83                     resultTO.setData(toJson(nagto));
84                 }
85                 break;
86                 case READATTRIBUTEGROUP: {
87                     long id = commandTO.getLong("attributeGroupId");
88                     AttributeGroupTO agto = mjb.readAttributeGroup(id);
89                     resultTO.setData(toJson(agto));
90                 }
91                 break;
92
93             }
94         } catch (RuntimeException ex) {
95             // Return error on any error...
96             resultTO.setCode(CommandResultTO.ERROR);
97             ex.printStackTrace();
98         } finally {
99             send(resultTO, response);
100         }
101     }
102
103 }