]> SALOME platform Git repositories - modules/gde.git/blob - projects/GDE_App/GDE-war/test/com/edf/gde/dao/UserDaoClient.java
Salome HOME
- Merge from home
[modules/gde.git] / projects / GDE_App / GDE-war / test / com / edf / gde / dao / UserDaoClient.java
1 /*
2  * (C) 2015 EDF
3  */
4 package com.edf.gde.dao;
5
6 import com.edf.gde.transferables.CommandTO;
7 import com.edf.gde.transferables.GroupTO;
8 import com.edf.gde.transferables.UserTO;
9 import com.edf.gde.transferables.responses.CommandResultTO;
10 import restapi.RestContext;
11 import java.io.IOException;
12
13 /**
14  *
15  * @author Kavoos
16  */
17 public class UserDaoClient extends BaseDao {
18
19     public static final int CREATEUSER = 1;
20     public static final int DELETEUSER = 2;
21     public static final int ADDTOGROUP = 3;
22     public static final int REMOVEFROMGROUP = 4;
23     public static final int CREATEGROUP = 5;
24     public static final int DELETEGROUP = 6;
25     public static final int FINDUSER = 7;
26     public static final int FINDGROUP = 8;
27
28     protected DaoResponseHandler daoResponseHandler;
29     
30     public UserDaoClient() {
31         getContext().setBaseResource("http://localhost:8080/GDE-war/UserService");
32         getContext().setUserName("admin");
33         getContext().setPassword("edf123");
34         daoResponseHandler = new DaoResponseHandler();
35     }
36
37     public UserDaoClient(RestContext context) {
38         super(context);
39         daoResponseHandler = new DaoResponseHandler();
40     }
41
42     /**
43      *
44      * @param userName
45      * @param password
46      * @return
47      * @throws IOException
48      */
49     public UserTO createUser(String userName, String password) throws IOException {
50         CommandTO commandTO = createCommand(CREATEUSER);
51         UserTO userTO = new UserTO();
52         userTO.setName(userName);
53         userTO.setPassword(password);
54         commandTO.setData(toJson(userTO));
55         if (postAsJSonData(commandTO, daoResponseHandler)) {
56             CommandResultTO resultTO = daoResponseHandler.getResultTO();
57             if (resultTO.getCode() == CommandResultTO.OK) {
58                 userTO = fromJson(resultTO.getData(), UserTO.class);
59                 return userTO;
60             }
61         }
62         throw new RuntimeException("Unable to create user");
63     }
64
65     /**
66      *
67      * @param userId
68      * @return
69      * @throws IOException
70      */
71     public boolean deleteUser(long userId) throws IOException {
72         CommandTO commandTO = createCommand(DELETEUSER);
73         commandTO.setLong("id", userId);
74         if (postAsJSonData(commandTO, daoResponseHandler)) {
75             if (daoResponseHandler.getResultTO().getCode() == CommandResultTO.OK) {
76                 return true;
77             }
78         }
79         return false;
80     }
81
82     /**
83      *
84      * @param userName
85      * @return null if user not found
86      * @throws IOException
87      */
88     public UserTO findUser(String userName) throws IOException {
89         CommandTO commandTO = createCommand(FINDUSER);
90         commandTO.setString("username", userName);
91         if (postAsJSonData(commandTO, daoResponseHandler)) {
92             CommandResultTO resultTO = daoResponseHandler.getResultTO();
93             if (resultTO.getCode() == CommandResultTO.OK) {
94                 UserTO userTO = fromJson(resultTO.getData(), UserTO.class);
95                 return userTO;
96             }
97         }
98         return null;
99     }
100
101     /**
102      *
103      * @param groupName
104      * @return
105      * @throws IOException
106      */
107     public GroupTO createGroup(String groupName) throws IOException {
108         CommandTO commandTO = createCommand(CREATEGROUP);
109         commandTO.setString("name", groupName);
110         if (postAsJSonData(commandTO, daoResponseHandler)) {
111             CommandResultTO resultTO = daoResponseHandler.getResultTO();
112             if (resultTO.getCode() == CommandResultTO.OK) {
113                 GroupTO groupTO = fromJson(resultTO.getData(), GroupTO.class);
114                 return groupTO;
115             }
116         }
117         return null;
118     }
119
120     /**
121      *
122      * @param groupName
123      * @return
124      * @throws IOException
125      */
126     public GroupTO findGroup(String groupName) throws IOException {
127         CommandTO commandTO = createCommand(FINDGROUP);
128         commandTO.setString("groupname", groupName);
129         if (postAsJSonData(commandTO, daoResponseHandler)) {
130             CommandResultTO resultTO = daoResponseHandler.getResultTO();
131             if (resultTO.getCode() == CommandResultTO.OK) {
132                 GroupTO groupTO = fromJson(resultTO.getData(), GroupTO.class);
133                 return groupTO;
134             }
135         }
136         return null;
137     }
138 /**
139  * 
140  * @param id
141  * @return
142  * @throws IOException 
143  */
144     public boolean deleteGroup(long id) throws IOException {
145         CommandTO commandTO = createCommand(DELETEGROUP);
146         commandTO.setLong("id", id);
147         if (postAsJSonData(commandTO, daoResponseHandler)) {
148             CommandResultTO resultTO = daoResponseHandler.getResultTO();
149             if (resultTO.getCode() == CommandResultTO.OK) {
150                 return true;
151             }
152         }
153         return false;
154     }
155     
156     public boolean addToGroup(long groupId, long userId) throws IOException {
157         CommandTO commandTO = createCommand(ADDTOGROUP);
158         commandTO.setLong("groupId", groupId);
159         commandTO.setLong("userId", userId);
160         if (postAsJSonData(commandTO,daoResponseHandler)) {
161             CommandResultTO resultTO = daoResponseHandler.getResultTO();
162             if (resultTO.getCode() == CommandResultTO.OK) {
163                 return true;
164             }
165         }
166         return false;
167     }
168     
169     public boolean removeFromGroup(long groupId, long userId) throws IOException {
170         CommandTO commandTO = createCommand(REMOVEFROMGROUP);
171         commandTO.setLong("groupId", groupId);
172         commandTO.setLong("userId", userId);
173         if (postAsJSonData(commandTO,daoResponseHandler)) {
174             CommandResultTO resultTO = daoResponseHandler.getResultTO();
175             if (resultTO.getCode() == CommandResultTO.OK) {
176                 return true;
177             }
178         }
179         return false;
180     }
181 }