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 gdetester.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 protected DaoResponseHandler daoResponseHandler;
30 public UserDaoClient() {
31 getContext().setBaseResource("http://localhost:8080/GDE-war/UserService");
32 daoResponseHandler = new DaoResponseHandler();
35 public UserDaoClient(RestContext context) {
37 daoResponseHandler = new DaoResponseHandler();
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);
60 throw new RuntimeException("Unable to create user");
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) {
83 * @return null if user not found
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);
103 * @throws IOException
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);
122 * @throws IOException
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);
140 * @throws IOException
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) {
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) {
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) {