Newer
Older
CactusServer / src / main / java / cactusServer / resources / ObjectsRest.java
package cactusServer.resources;

import java.util.HashMap;

import javax.ws.rs.FormParam;
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 javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;

import org.ntlab.radishforandroidstudio.framework.model3D.Position3D;
import org.ntlab.radishforandroidstudio.framework.model3D.Quaternion3D;
import org.ntlab.radishforandroidstudio.framework.physics.AngularVelocity3D;
import org.ntlab.radishforandroidstudio.framework.physics.Velocity3D;

import cactusServer.entities.Instance;
import cactusServer.entities.Object;
import cactusServer.entities.Object.Attribute;
import cactusServer.models.Instances;

@Path("/instances/{instanceId}/objects")
public class ObjectsRest {

	// @Path("/{instanceId}/objects/{objectId}")
	@Path("/{objectId}")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Object getObject(@PathParam("instanceId") String instanceId, @PathParam("objectId") String objectId) {
		Instance instance = Instances.getInstance().getInstance(instanceId);
		if (instance != null) {
			return instance.getObject(objectId);
		}
		return null;
	}

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public HashMap<String, Object> getObjects(@PathParam("instanceId") String instanceId) {
		return Instances.getInstance().getInstance(instanceId).getObjects();
	}

	// @Path("/{instanceId}/objects")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	public HashMap<String, Object> createObject(@PathParam("instanceId") String instanceId,
			@FormParam("position") Position3D position, @FormParam("velocity") Velocity3D velocity,
			@FormParam("angularVelocity") AngularVelocity3D angularVelocity, @FormParam("angle") Quaternion3D angle,
			@FormParam("attribute") Attribute attribute, @FormParam("model") String model) {
		Instance instance = Instances.getInstance().getInstance(instanceId);
		HashMap<String, Object> idMap = instance.createObject(position, velocity, angularVelocity, angle, attribute,
				model);
		HashMap<String, Object> uriMap = new HashMap<>();
		for (String id : idMap.keySet()) {
			String uri = (InstancesRest.INSTANCES_URI + "/" + instanceId + "/objects/") + id;
			uriMap.put(uri, idMap.get(id));
		}
		return uriMap;
	}
	 // @Path("/{instance-id}/objects/{object-id}")
	 @Path("/{object-id}")
	 @PUT
	 @Produces(MediaType.APPLICATION_JSON)
	 public Object updateObject(@FormParam("objectId") String objectId,
	 @FormParam("position") Position3D position,
	 @FormParam("velocity") Velocity3D velocity,
	 @FormParam("angularVelocity") AngularVelocity3D angularVelocity,
	 @FormParam("angle") Quaternion3D angle) {
		 Object object = updateObject(objectId, position, velocity, angularVelocity, angle);
		 if (object != null) {
			 return object;
		 }else {
				throw new WebApplicationException(400);
		 }
	 }
}