package com.abstratt.kirra.rest.resources;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response.Status;

import com.abstratt.kirra.Entity;
import com.abstratt.kirra.Instance;
import com.abstratt.kirra.InstanceManagement;
import com.abstratt.kirra.Relationship;
import com.abstratt.kirra.TypeRef;
import com.abstratt.kirra.rest.common.CommonHelper;
import com.abstratt.kirra.rest.common.KirraContext;
import com.abstratt.kirra.rest.common.Paths;

@Path(Paths.RELATED_INSTANCE_PATH)
public class RelatedInstanceResource extends InstanceResource {
    @GET
    @Produces("application/json")
    public String getInstance(@PathParam("entityName") String entityName, @PathParam("objectId") String objectId, @PathParam("relationshipName") String relationshipName, @PathParam("relatedObjectId") String relatedObjectId) {
        TypeRef entityRef = new TypeRef(entityName, TypeRef.TypeKind.Entity);
        InstanceManagement instanceManagement = KirraContext.getInstanceManagement();
        
        Entity entity = KirraContext.getSchemaManagement().getEntity(entityRef);
        ResourceHelper.ensure(entity != null, null, Status.NOT_FOUND);
        
        Relationship relationship = entity.getRelationship(relationshipName);
        ResourceHelper.ensure(relationship != null, null, Status.NOT_FOUND);

        Instance instance = instanceManagement.getInstance(relationship.getTypeRef().getEntityNamespace(), relationship.getTypeRef().getTypeName(),
                relatedObjectId, true);
        
        ResourceHelper.ensure(instance != null, "Instance not found", Status.NOT_FOUND);
        return CommonHelper.buildGson(ResourceHelper.resolve(Paths.ENTITIES, entityName, Paths.INSTANCES, objectId, Paths.RELATIONSHIPS, relationshipName)).create().toJson(instance);
    }
    @DELETE
    public void detachInstance(@PathParam("entityName") String entityName, @PathParam("objectId") String objectId, @PathParam("relationshipName") String relationshipName, @PathParam("relatedObjectId") String relatedObjectId) {
        TypeRef entityRef = new TypeRef(entityName, TypeRef.TypeKind.Entity);
        InstanceManagement instanceManagement = KirraContext.getInstanceManagement();
        
        Entity entity = KirraContext.getSchemaManagement().getEntity(entityRef);
        ResourceHelper.ensure(entity != null, null, Status.NOT_FOUND);
        
        Relationship relationship = entity.getRelationship(relationshipName);
        ResourceHelper.ensure(relationship != null, null, Status.NOT_FOUND);
        instanceManagement.unlinkInstances(relationship, objectId, relatedObjectId);
    }    
}

@@ -52,45 +52,147 @@
 java
-x.ws.rs.DELETE;%0Aimport javax.ws.rs.GE
+.util.List;%0A%0Aimport javax.ws.rs.Consumes;%0Aimport javax.ws.rs.DELETE;%0Aimport javax.ws.rs.GET;%0Aimport javax.ws.rs.POST;%0Aimport javax.ws.rs.PU
 T;%0Ai
@@ -472,16 +472,60 @@
 onship;%0A
+import com.abstratt.kirra.SchemaManagement;%0A
 import c
@@ -2857,10 +2857,1444 @@
   %7D    %0A
+    @Produces(%22application/json%22)%0A    @Consumes(%22application/json%22)%0A    @PUT%0A    public String attachInstance(@PathParam(%22entityName%22) String entityName, @PathParam(%22objectId%22) String objectId, @PathParam(%22relationshipName%22) String relationshipName, @PathParam(%22relatedObjectId%22) String relatedObjectId) %7B%0A        TypeRef entityRef = new TypeRef(entityName, TypeRef.TypeKind.Entity);%0A        SchemaManagement schemaManagement = KirraContext.getSchemaManagement();%0A        Entity entity = schemaManagement.getEntity(entityRef);%0A        ResourceHelper.ensure(entity != null, null, Status.NOT_FOUND);%0A        %0A        Relationship relationship = entity.getRelationship(relationshipName);%0A        ResourceHelper.ensure(relationship != null, null, Status.NOT_FOUND);%0A        %0A        InstanceManagement instanceManagement = KirraContext.getInstanceManagement();%0A        %0A        instanceManagement.linkInstances(relationship, objectId, relatedObjectId);%0A        %0A        Instance instance = instanceManagement.getInstance(relationship.getTypeRef().getEntityNamespace(), relationship.getTypeRef().getTypeName(),%0A                relatedObjectId, true);%0A        %0A        ResourceHelper.ensure(instance != null, %22Instance not found%22, Status.NOT_FOUND);%0A        return CommonHelper.buildGson(ResourceHelper.resolve(Paths.ENTITIES, entityName, Paths.INSTANCES, objectId, Paths.RELATIONSHIPS, relationshipName)).create().toJson(instance);%0A    %7D%0A%0A
 %7D%0A

support for linking two instances