package com.altamiracorp.lumify.web.routes.workspace;

import com.altamiracorp.lumify.core.model.workspace.Workspace;
import com.altamiracorp.lumify.core.model.workspace.WorkspaceRepository;
import com.altamiracorp.lumify.core.user.User;
import com.altamiracorp.lumify.core.util.LumifyLogger;
import com.altamiracorp.lumify.core.util.LumifyLoggerFactory;
import com.altamiracorp.lumify.web.BaseRequestHandler;
import com.altamiracorp.miniweb.HandlerChain;
import com.google.inject.Inject;
import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WorkspaceById extends BaseRequestHandler {
    private static final LumifyLogger LOGGER = LumifyLoggerFactory.getLogger(WorkspaceById.class);
    private final WorkspaceRepository workspaceRepository;

    @Inject
    public WorkspaceById(final WorkspaceRepository workspaceRepo) {
        workspaceRepository = workspaceRepo;
    }

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, HandlerChain chain) throws Exception {
        final String workspaceId = getAttributeString(request, "workspaceId");
        final User authUser = getUser(request);

        LOGGER.info("Attempting to retrieve workspace: %s", workspaceId);
        final Workspace workspace = workspaceRepository.findById(workspaceId, authUser);

        if (workspace == null) {
            LOGGER.warn("Could not find workspace: %s", workspaceId);
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        } else {
            LOGGER.debug("Successfully found workspace");
            request.getSession().setAttribute("activeWorkspace", workspaceId);
            final JSONObject resultJSON = workspace.toJson(true);
            respondWithJson(response, resultJSON);
        }

        chain.next(request, response);
    }
}

@@ -48,16 +48,91 @@
 space;%0A%0A
+import com.altamiracorp.lumify.core.exception.LumifyAccessDeniedException;%0A
 import c
@@ -1288,17 +1288,34 @@
 quest);%0A
-%0A
+        try %7B%0A    
         
@@ -1384,24 +1384,28 @@
 d);%0A        
+    
 final Worksp
@@ -1482,16 +1482,20 @@
         
+    
 if (work
@@ -1519,24 +1519,28 @@
             
+ 
+   
 LOGGER.warn(
@@ -1593,24 +1593,28 @@
             
+    
 response.sen
@@ -1663,16 +1663,20 @@
         
+ 
+   
 %7D else %7B
@@ -1676,16 +1676,20 @@
  else %7B%0A
+    
         
@@ -1750,16 +1750,20 @@
         
+  
+  
 request.
@@ -1829,24 +1829,28 @@
             
+    
 final JSONOb
@@ -1899,24 +1899,28 @@
             
+  
+  
 respondWithJ
@@ -1950,24 +1950,28 @@
 N);%0A        
+    
 %7D%0A%0A        c
@@ -1969,16 +1969,20 @@
         
+  
+  
 chain.ne
@@ -2000,20 +2000,224 @@
  response);%0A
+        %7D catch (LumifyAccessDeniedException ex) %7B%0A            LOGGER.warn(%22access denied for workspace: %25s%22, workspaceId, ex);%0A            response.sendError(HttpServletResponse.SC_FORBIDDEN);%0A        %7D%0A
     %7D%0A%7D%0A

return access denied