server/src/main/java/org/iri_research/renkan/rest/ProjectsResource.java
changeset 51 3247fccfbd3f
parent 47 267d67791e05
child 71 9af0874ce43f
equal deleted inserted replaced
50:7b517a54b708 51:3247fccfbd3f
     1 package org.iri_research.renkan.rest;
     1 package org.iri_research.renkan.rest;
     2 
     2 
     3 import java.util.ArrayList;
     3 import java.util.ArrayList;
       
     4 import java.util.Date;
     4 import java.util.List;
     5 import java.util.List;
     5 import java.util.UUID;
     6 import java.util.UUID;
     6 
     7 
     7 import javax.ws.rs.Consumes;
     8 import javax.ws.rs.Consumes;
     8 import javax.ws.rs.DELETE;
     9 import javax.ws.rs.DELETE;
    28 
    29 
    29 import com.mongodb.BasicDBObject;
    30 import com.mongodb.BasicDBObject;
    30 import com.mongodb.DBCollection;
    31 import com.mongodb.DBCollection;
    31 import com.mongodb.DBCursor;
    32 import com.mongodb.DBCursor;
    32 import com.mongodb.DBObject;
    33 import com.mongodb.DBObject;
    33 import com.mongodb.WriteResult;
       
    34 import com.mongodb.util.JSON;
    34 import com.mongodb.util.JSON;
    35 import com.sun.jersey.spi.resource.Singleton;
    35 import com.sun.jersey.spi.resource.Singleton;
    36 
    36 
    37 
    37 
    38 @Singleton
    38 @Singleton
    53 	}
    53 	}
    54 
    54 
    55 	@GET
    55 	@GET
    56 	@Path("{id : [a-zA-Z\\-0-9]+}")
    56 	@Path("{id : [a-zA-Z\\-0-9]+}")
    57 	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    57 	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    58 	public Project getProjectSd(@PathParam("id") String projectId) {
    58 	public Project getProject(@PathParam("id") String projectId) {
    59 		
    59 		
    60 		this.logger.debug("GetProject: " + projectId);
    60 		this.logger.debug("GetProject: " + projectId);
    61 				
    61 				
    62 		Project project = this.projectRepository.findOne(projectId);
    62 		Project project = this.projectRepository.findOne(projectId);
    63 		
    63 		
    87 	 * @param projectContent
    87 	 * @param projectContent
    88 	 */
    88 	 */
    89 	@PUT
    89 	@PUT
    90 	@Path("{id : [a-zA-Z\\-0-9]+}")
    90 	@Path("{id : [a-zA-Z\\-0-9]+}")
    91 	@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
    91 	@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
    92 	public Response putProject(@PathParam("id") String projectId, String projectContent) {
    92 	public Response putProject(@PathParam("id") String projectId, Project project) {
       
    93 
       
    94 		if(!projectId.equals(project.getId())) {
       
    95 			throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("Id parameter and id in JSON do not match").build());
       
    96 		}
       
    97 		if(project.getCreated() == null) {
       
    98 			project.setCreated(new Date());
       
    99 		}
       
   100 		this.projectRepository.save(project);
       
   101 		return Response.noContent().build();
    93 		
   102 		
    94 		return this.createOrUpdateProject(projectId, projectContent);		
       
    95 	}
   103 	}
    96 
   104 
       
   105 	
    97 	@POST
   106 	@POST
    98 	@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
   107 	@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
    99 	public Response postProject(String projectContent) {
   108 	public Response postProject(Project project) {
       
   109 		if(project.getId() != null) {
       
   110 			throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("Id in JSON must be null").build());
       
   111 		}
   100 		
   112 		
   101 		return this.createOrUpdateProject(null, projectContent);
   113 		project.setId(UUID.randomUUID().toString());
       
   114 		if(project.getCreated() == null) {
       
   115 			project.setCreated(new Date());
       
   116 		}
       
   117 		project = this.projectRepository.save(project);
       
   118 		return Response.created(this.uriInfo.getAbsolutePathBuilder().segment(project.getId()).build()).entity(project).build();		
       
   119 	}
   102 		
   120 		
   103 	}
       
   104 	
       
   105 	//TODO: uses spring data
       
   106 	@GET
   121 	@GET
   107 	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
   122 	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
   108 	public String getProjectList() {
   123 	public String getProjectList() {
   109 		
   124 		
   110 		DBCollection projectCollection = this.getCollection();
   125 		DBCollection projectCollection = this.getCollection();
   111 		BasicDBObject keys = new BasicDBObject();
   126 		BasicDBObject keys = new BasicDBObject();
   112 		keys.put("description", 1);
   127 		keys.put("description", 1);
   113 		keys.put("title", 1);
   128 		keys.put("title", 1);
   114 		keys.put("uri", 1);
   129 		keys.put("uri", 1);
       
   130 		keys.put("created", 1);
   115 		DBCursor cursor = projectCollection.find(new BasicDBObject(), keys);
   131 		DBCursor cursor = projectCollection.find(new BasicDBObject(), keys);
   116 		
   132 		
   117 		List<DBObject> res = new ArrayList<DBObject>();
   133 		List<DBObject> res = new ArrayList<DBObject>();
   118 				
   134 				
   119 		try {
   135 		try {
   146 			cursor.close();
   162 			cursor.close();
   147 		}
   163 		}
   148 		
   164 		
   149 		return JSON.serialize(res);
   165 		return JSON.serialize(res);
   150 	}
   166 	}
   151 	
       
   152 	//TODO: rewrite for recursive object and use spring data
       
   153 	private Response createOrUpdateProject(String projectId, String projectContent) {
       
   154 		
   167 		
   155 		if(null == projectContent || projectContent.isEmpty()) {
       
   156 			throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("No content").build());
       
   157 		}
       
   158 		
       
   159 		DBCollection projectCollection = this.getCollection(); 
       
   160 		
       
   161 		DBObject obj = (DBObject)JSON.parse(projectContent);
       
   162 		if(obj == null) {
       
   163 			throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("Parse error").build());
       
   164 		}
       
   165 		
       
   166 		String newProjectId = projectId;
       
   167 		
       
   168 		if(newProjectId == null) {
       
   169 			newProjectId = UUID.randomUUID().toString();
       
   170 		}
       
   171 		
       
   172 		obj.put("_id", newProjectId);
       
   173 		
       
   174 		WriteResult res = projectCollection.update(new BasicDBObject("_id", newProjectId), obj, true, false);
       
   175 		
       
   176 		if(res.getLastError().ok()) {
       
   177 			if(projectId == null) {
       
   178 				return Response.created(this.uriInfo.getAbsolutePathBuilder().segment(newProjectId).build()).build();
       
   179 			}
       
   180 			else {
       
   181 				return Response.noContent().build();
       
   182 			}
       
   183 		}
       
   184 		else {
       
   185 			throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(res.getError()).build());
       
   186 		}
       
   187 		
       
   188 	}
       
   189 	
       
   190 }
   168 }