package ox.softeng.metadatacatalogue.restapi.services;

import java.util.Properties;
import java.util.UUID;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

import ox.softeng.metadatacatalogue.api.ApiContext;
import ox.softeng.metadatacatalogue.domain.core.User;

public class BasicCatalogueService {

	
	@Context HttpServletRequest request;
	@Context SecurityContext securityContext;

	
	
	
	protected UUID getUserId()
	{
		if(securityContext.getUserPrincipal() != null)
		{
			return ((User) securityContext.getUserPrincipal()).getId();
		}
		else return null;
	}

	protected EntityManager getNewEntityManager()
	{
		EntityManagerFactory entityManagerFactory = (EntityManagerFactory) request.getServletContext().getAttribute("entityManagerFactory");
		if(entityManagerFactory==null){
			System.err.println("No entity manager factory!");
			throw new javax.ws.rs.ServerErrorException("Error on getting Entity Manager Factory",Response.status(Response.Status.INTERNAL_SERVER_ERROR).build());
		}
		Properties props = (Properties) request.getServletContext().getAttribute("entityManagerProps");
		if(props==null){
			System.err.println("No entity manager properties!");
			throw new javax.ws.rs.ServerErrorException("Error on getting Entity Manager Properties",Response.status(Response.Status.INTERNAL_SERVER_ERROR).build());
		}
		return entityManagerFactory.createEntityManager(props);
	}
	
	protected ApiContext getApiContext()
	{
		if(securityContext.getUserPrincipal() != null)
		{
			return (ApiContext) securityContext;
		}
		else return null;
	}

}

@@ -1626,121 +1626,78 @@
 %7B%0A%09%09
-if(securityContext.getUserPrincipal() != null)%0A%09%09%7B%0A%09%09%09return (ApiContext) securityContext;%0A%09%09%7D%0A%09%09else return null
+%0A%09%09return (ApiContext) request.getSession().getAttribute(%22apiContext%22)
 ;%0A%09%7D

Work around strange SecurityInjectee issue.