package de.starwit.ljprojectbuilder.api.rest;


import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import de.starwit.ljprojectbuilder.ejb.DomainService;
import de.starwit.ljprojectbuilder.ejb.ProjectService;
import de.starwit.ljprojectbuilder.entity.DataType;
import de.starwit.ljprojectbuilder.entity.DomainEntity;
import de.starwit.ljprojectbuilder.entity.ProjectEntity;
import de.starwit.ljprojectbuilder.response.EntityListResponse;
import de.starwit.ljprojectbuilder.response.EntityResponse;
import de.starwit.ljprojectbuilder.response.ResponseCode;
import de.starwit.ljprojectbuilder.response.ResponseMetadata;
import de.starwit.ljprojectbuilder.validation.EntityValidator;

@Path("/domain")
@Consumes("application/json")
@Produces("application/json")
public class DomainRest extends AbstractRest<DomainEntity> {
	
	@Inject
	protected DomainService service;
	
	@Inject
	protected ProjectService projectService;
	
	@Override
	protected DomainService getService() {
		return service;
	}
	
	//Create
	@Path("/")
	@PUT
	public EntityResponse<DomainEntity> create(DomainEntity entity) {
		System.out.println("*************************** -  create");
		System.out.println(entity);
		if (entity.getProject() == null) {
			ProjectEntity projectEntity = new ProjectEntity();
			projectEntity.setId(entity.getProjectId());
		}
		System.out.println("***** " + entity.getProject().getId());
		return super.createGeneric(entity);
	}

	//Update
	@Path("/")
	@POST
	public EntityResponse<DomainEntity> update(DomainEntity entity) {
		System.out.println("*************************** -  update");
		System.out.println(entity);
		if (entity.getProject() == null) {
			ProjectEntity projectEntity = new ProjectEntity();
			projectEntity.setId(entity.getProjectId());
		}
		return super.updateGeneric(entity);
	}
	
	@Path("/query/domainsbyproject/{projectId}")
	@GET
	public EntityListResponse<DomainEntity> findAllDomainsByProject(@PathParam("projectId") Long projectId) {
		ProjectEntity projectEntity = projectService.findById(projectId);
		if (projectEntity == null) {
			EntityListResponse<DomainEntity> response = new EntityListResponse<DomainEntity>(null);
			ResponseMetadata responseMetadata = new ResponseMetadata(ResponseCode.NOT_FOUND, "responsecode.notfound");
			response.setMetadata(responseMetadata);
			return response;
		} else {
			List<DomainEntity> entities = getService().findAllDomainsByProject(projectId);
			EntityListResponse<DomainEntity> response = new EntityListResponse<DomainEntity>(entities);
			ResponseMetadata responseMetadata = EntityValidator.isNotEmpty(response.getResult());
			response.setMetadata(responseMetadata);
			return response;
		}
	}
	
	@Path("/query/types")
	@GET
	public DataType[] getTypes() {
		return DataType.values();
	}
}
@@ -1370,32 +1370,111 @@
 * -  create%22);%0D%0A
+%09%09System.out.println(%22*************************** %22 + entity.getProjectId());%0D%0A
 %09%09System.out.pri
@@ -1625,24 +1625,62 @@
 jectId());%0D%0A
+%09%09%09entity.setProject(projectEntity);%0D%0A
 %09%09%7D%0D%0A%09%09Syste

some output to chase heisenbug