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