return gson.fromJson(json, classOf);
}
+ protected String toJson(Object object) {
+ Gson gson = new Gson();
+ return toJson(object);
+ }
+
protected void send(Object object, HttpServletResponse out) {
try {
Gson gson = new Gson();
gson.toJson(object, out.getWriter());
} catch (IOException ex) {
- Logger.getLogger(BaseService.class.getName()).log(Level.SEVERE, null, ex);
+ throw new RuntimeException(ex);
}
}
import com.edf.gde.entities.Group;
import com.edf.gde.entities.User;
import com.edf.gde.transferables.CommandTO;
+import com.edf.gde.transferables.GroupTO;
import com.edf.gde.transferables.LongListTO;
import com.edf.gde.transferables.UserTO;
import com.edf.gde.transferables.responses.CommandResultTO;
@Override
public void processRequest(HttpServletRequest request, HttpServletResponse response) {
CommandTO commandTO = getCommand(request);
- ObjectCreationResponseTO creationResponseTO = new ObjectCreationResponseTO();
+ CommandResultTO resultTO = new CommandResultTO();
try {
switch (commandTO.getMethod()) {
case CREATEUSER: {
- long userId = createUser(fromJson(commandTO.getData(), UserTO.class));
- creationResponseTO.setId(userId);
+ UserTO userTO = createUser(fromJson(commandTO.getData(), UserTO.class));
+ resultTO.setData(toJson(userTO));
}
case DELETEUSER: {
long userId = commandTO.getLong("id");
commandResultTO.setCode(CommandResultTO.ERROR);
}
}
-
case CREATEGROUP: {
String groupName = commandTO.getParameter("name");
- long groupId = createGroup(groupName);
- creationResponseTO.setId(groupId);
+ GroupTO groupTO = createGroup(groupName);
+ resultTO.setData(toJson(groupTO));
}
case ADDTOGROUP: {
long groupId = commandTO.getLong("id");
}
} catch (RuntimeException ex) {
// Return error on any error...
- creationResponseTO.setCode(CommandResultTO.ERROR);
+ resultTO.setCode(CommandResultTO.ERROR);
} finally {
- send(creationResponseTO, response);
+ send(resultTO, response);
}
}
- protected long createGroup(String groupName) {
+ protected GroupTO createGroup(String groupName) {
Group group = userDAO.createGroup(groupName);
- return group.getId();
+ return group.toGroupTO();
}
- protected long createUser(UserTO userTO) {
+ protected UserTO createUser(UserTO userTO) {
User user = userDAO.createUser(userTO.getName(), userTO.getPassword());
- return user.getId();
+ return user.toUserTO();
}
protected boolean deleteUser(long userId) {