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 protected DaoResponseHandler daoResponseHandler;
30 public UserDaoClient() {
31 getContext().setBaseResource("http://localhost:8080/GDE-war/UserService");
32 getContext().setUserName("admin");
33 getContext().setPassword("edf123");
34 daoResponseHandler = new DaoResponseHandler();
37 public UserDaoClient(RestContext context) {
39 daoResponseHandler = new DaoResponseHandler();
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);
62 throw new RuntimeException("Unable to create user");
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) {
85 * @return null if user not found
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);
105 * @throws IOException
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);
124 * @throws IOException
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);
142 * @throws IOException
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) {
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) {
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) {