Salome HOME
10d5d104969a64202cfeda79764ce040b6f90fa0
[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 gdetester.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         daoResponseHandler = new DaoResponseHandler();
33     }
34
35     public UserDaoClient(RestContext context) {
36         super(context);
37         daoResponseHandler = new DaoResponseHandler();
38     }
39
40     /**
41      *
42      * @param userName
43      * @param password
44      * @return
45      * @throws IOException
46      */
47     public UserTO createUser(String userName, String password) throws IOException {
48         CommandTO commandTO = createCommand(CREATEUSER);
49         UserTO userTO = new UserTO();
50         userTO.setName(userName);
51         userTO.setPassword(password);
52         commandTO.setData(toJson(userTO));
53         if (postAsJSonData(commandTO, daoResponseHandler)) {
54             CommandResultTO resultTO = daoResponseHandler.getResultTO();
55             if (resultTO.getCode() == CommandResultTO.OK) {
56                 userTO = fromJson(resultTO.getData(), UserTO.class);
57                 return userTO;
58             }
59         }
60         throw new RuntimeException("Unable to create user");
61     }
62
63     /**
64      *
65      * @param userId
66      * @return
67      * @throws IOException
68      */
69     public boolean deleteUser(long userId) throws IOException {
70         CommandTO commandTO = createCommand(DELETEUSER);
71         commandTO.setLong("id", userId);
72         if (postAsJSonData(commandTO, daoResponseHandler)) {
73             if (daoResponseHandler.getResultTO().getCode() == CommandResultTO.OK) {
74                 return true;
75             }
76         }
77         return false;
78     }
79
80     /**
81      *
82      * @param userName
83      * @return null if user not found
84      * @throws IOException
85      */
86     public UserTO findUser(String userName) throws IOException {
87         CommandTO commandTO = createCommand(FINDUSER);
88         commandTO.setString("username", userName);
89         if (postAsJSonData(commandTO, daoResponseHandler)) {
90             CommandResultTO resultTO = daoResponseHandler.getResultTO();
91             if (resultTO.getCode() == CommandResultTO.OK) {
92                 UserTO userTO = fromJson(resultTO.getData(), UserTO.class);
93                 return userTO;
94             }
95         }
96         return null;
97     }
98
99     /**
100      *
101      * @param groupName
102      * @return
103      * @throws IOException
104      */
105     public GroupTO createGroup(String groupName) throws IOException {
106         CommandTO commandTO = createCommand(CREATEGROUP);
107         commandTO.setString("name", groupName);
108         if (postAsJSonData(commandTO, daoResponseHandler)) {
109             CommandResultTO resultTO = daoResponseHandler.getResultTO();
110             if (resultTO.getCode() == CommandResultTO.OK) {
111                 GroupTO groupTO = fromJson(resultTO.getData(), GroupTO.class);
112                 return groupTO;
113             }
114         }
115         return null;
116     }
117
118     /**
119      *
120      * @param groupName
121      * @return
122      * @throws IOException
123      */
124     public GroupTO findGroup(String groupName) throws IOException {
125         CommandTO commandTO = createCommand(FINDGROUP);
126         commandTO.setString("groupname", groupName);
127         if (postAsJSonData(commandTO, daoResponseHandler)) {
128             CommandResultTO resultTO = daoResponseHandler.getResultTO();
129             if (resultTO.getCode() == CommandResultTO.OK) {
130                 GroupTO groupTO = fromJson(resultTO.getData(), GroupTO.class);
131                 return groupTO;
132             }
133         }
134         return null;
135     }
136 /**
137  * 
138  * @param id
139  * @return
140  * @throws IOException 
141  */
142     public boolean deleteGroup(long id) throws IOException {
143         CommandTO commandTO = createCommand(DELETEGROUP);
144         commandTO.setLong("id", id);
145         if (postAsJSonData(commandTO, daoResponseHandler)) {
146             CommandResultTO resultTO = daoResponseHandler.getResultTO();
147             if (resultTO.getCode() == CommandResultTO.OK) {
148                 return true;
149             }
150         }
151         return false;
152     }
153     
154     public boolean addToGroup(long groupId, long userId) throws IOException {
155         CommandTO commandTO = createCommand(ADDTOGROUP);
156         commandTO.setLong("groupId", groupId);
157         commandTO.setLong("userId", userId);
158         if (postAsJSonData(commandTO,daoResponseHandler)) {
159             CommandResultTO resultTO = daoResponseHandler.getResultTO();
160             if (resultTO.getCode() == CommandResultTO.OK) {
161                 return true;
162             }
163         }
164         return false;
165     }
166     
167     public boolean removeFromGroup(long groupId, long userId) throws IOException {
168         CommandTO commandTO = createCommand(REMOVEFROMGROUP);
169         commandTO.setLong("groupId", groupId);
170         commandTO.setLong("userId", userId);
171         if (postAsJSonData(commandTO,daoResponseHandler)) {
172             CommandResultTO resultTO = daoResponseHandler.getResultTO();
173             if (resultTO.getCode() == CommandResultTO.OK) {
174                 return true;
175             }
176         }
177         return false;
178     }
179 }