Salome HOME
- Code cleanup
authormordicus <mordicus@HP-8560w>
Thu, 6 Aug 2015 06:43:20 +0000 (08:43 +0200)
committermordicus <mordicus@HP-8560w>
Thu, 6 Aug 2015 06:43:20 +0000 (08:43 +0200)
- Services allways returns CommandResultTO now.

projects/GDE_App/GDE-war/src/java/com/edf/gde/services/BaseService.java
projects/GDE_App/GDE-war/src/java/com/edf/gde/services/UserService.java

index 4a7bd61e78a430c76b246d944763fc01eb358aec..0e375ba07942707da1b4f69c3e469952023bfb70 100644 (file)
@@ -33,12 +33,17 @@ public abstract class BaseService extends HttpServlet {
         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);
         }
     }
 
index 867f4d83bb05e64172db7d039cd243ef1969f30f..ef78b80d90001a0a41f7c98bc67a351af68686cc 100644 (file)
@@ -4,6 +4,7 @@ import com.edf.gde.ejb.UserDAO;
 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;
@@ -31,12 +32,12 @@ public class UserService extends BaseService {
     @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");
@@ -45,11 +46,10 @@ public class UserService extends BaseService {
                         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");
@@ -64,20 +64,20 @@ public class UserService extends BaseService {
             }
         } 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) {