package uk.ac.ox.it.ords.api.project.resources;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import org.apache.shiro.SecurityUtils;

import uk.ac.ox.it.ords.api.project.model.UserRole;
import uk.ac.ox.it.ords.api.project.services.ProjectRoleService;
import uk.ac.ox.it.ords.api.project.services.ProjectService;

public class ProjectRole {
	
	@Path("/project/{id}/role")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Response getRoles(
			@PathParam("id") final int projectId
			){
		
		uk.ac.ox.it.ords.api.project.model.Project project = ProjectService.Factory.getInstance().getProject(projectId);
		
		if (project == null){
			throw new NotFoundException();
		}
		
		if (project.isPrivateProject()){
			if (!SecurityUtils.getSubject().isPermitted("project:view:" + projectId)){
				throw new ForbiddenException();
			}
		}
		
		try {
			return Response.ok(ProjectRoleService.Factory.getInstance().getUserRolesForProject(projectId)).build();
		} catch (Exception e) {
			return Response.serverError().build();
		}
	}

	@Path("/project/{id}/role")
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response addRole(
				UserRole role,
				@PathParam("id") final int projectId,
				@Context UriInfo uriInfo
			){
		
		if (!SecurityUtils.getSubject().isPermitted("project:modify:"+projectId)){
			throw new ForbiddenException();
		}
		
		if (ProjectService.Factory.getInstance().getProject(projectId) == null) {
			throw new NotFoundException();
		}
		
		try {
			role = ProjectRoleService.Factory.getInstance().addUserRoleToProject(projectId, role);
			
	        UriBuilder builder = uriInfo.getAbsolutePathBuilder();
	        builder.path(Integer.toString(role.getId()));
	        return Response.created(builder.build()).build();
		} catch (Exception e) {
			throw new BadRequestException();
		}
	}
	
	@Path("/project/{projectId}/role/{roleId}")
	@DELETE
	public Response deleteRoleFromProject(
				@PathParam("projectId") final int projectId,
				@PathParam("roleId") final int roleId
			){
		
		if (ProjectService.Factory.getInstance().getProject(projectId) == null) {
			throw new NotFoundException();
		}
		if (!SecurityUtils.getSubject().isPermitted("project:modify:"+projectId)){
			throw new ForbiddenException();
		}
		try {
			UserRole userRole = ProjectRoleService.Factory.getInstance().getUserRole(roleId);
			if (userRole == null){
				throw new NotFoundException();				
			}
		} catch (Exception e1) {
			throw new NotFoundException();
		}
		try {
			ProjectRoleService.Factory.getInstance().removeUserFromRoleInProject(projectId, roleId);
			return Response.ok().build();
		} catch (Exception e) {
			e.printStackTrace();
			throw new BadRequestException();
		}
		
		
		
	}

}

@@ -518,16 +518,57 @@
 UriInfo;
+%0Aimport javax.ws.rs.core.Response.Status;
 %0A%0Aimport
@@ -1138,24 +1138,107 @@
 n();%0A%09%09%7D%0A%09%09%0A
+%09%09if (project.isDeleted())%7B%0A%09%09%09return Response.status(Status.GONE).build();%0A%09%09%7D%0A%09%09%0A
 %09%09if (projec
@@ -1812,24 +1812,142 @@
 fo%0A%09%09%09)%7B%0A%09%09%0A
+%09%09uk.ac.ox.it.ords.api.project.model.Project project = ProjectService.Factory.getInstance().getProject(projectId);%0A%09%09%0A
 %09%09if (!Secur
@@ -2062,64 +2062,16 @@
 %09if 
-(ProjectService.Factory.getInstance().getProject
 (project
 Id) 
@@ -2058,35 +2058,32 @@
 %09%09%0A%09%09if (project
-Id)
  == null) %7B%0A%09%09%09t
@@ -2111,32 +2111,115 @@
 ption();%0A%09%09%7D%0A%09%09%0A
+%09%09if (project.isDeleted())%7B%0A%09%09%09return Response.status(Status.GONE).build();%0A%09%09%7D%0A%09%09%0A
 %09%09try %7B%0A%09%09%09role 
@@ -2749,20 +2749,69 @@
 )%7B%0A%09%09%0A%09%09
-if (
+uk.ac.ox.it.ords.api.project.model.Project project = 
 ProjectS
@@ -2860,16 +2860,32 @@
 ojectId)
+;%0A%0A%09%09if (project
  == null
@@ -2926,16 +2926,102 @@
 ();%0A%09%09%7D%0A
+%09%09%0A%09%09if (project.isDeleted())%7B%0A%09%09%09return Response.status(Status.GONE).build();%0A%09%09%7D%0A%09%09%0A
 %09%09if (!S
@@ -3120,32 +3120,35 @@
 xception();%0A%09%09%7D%0A
+%09%09%0A
 %09%09try %7B%0A%09%09%09UserR
@@ -3522,32 +3522,8 @@
 ) %7B%0A
-%09%09%09e.printStackTrace();%0A
 %09%09%09t

Handle more cases, e.g. GONE for deleted databases