4 package com.edf.gde.dao;
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;
17 public class UserDaoClient extends BaseDao {
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;
28 public UserDaoClient() {
29 getContext().setBaseResource("http://localhost:8080/GDE-war/UserService");
30 getContext().setUserName("admin");
31 getContext().setPassword("edf123");
34 public UserDaoClient(RestContext context) {
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);
58 throw new RuntimeException("Unable to create user");
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) {
81 * @return null if user not found
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);
101 * @throws IOException
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);
120 * @throws IOException
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);
138 * @throws IOException
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) {
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) {
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) {