package kr.ac.kaist.resl.lilliput.rest;
import java.util.Collection;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import com.mongodb.MongoException;
import edu.stanford.smi.protege.model.Project;
import edu.stanford.smi.protege.server.RemoteProjectManager;
import edu.stanford.smi.protegex.owl.model.OWLModel;
import edu.stanford.smi.protegex.owl.model.OWLObjectProperty;
import edu.stanford.smi.protegex.owl.model.RDFIndividual;
public class UtilizeRelationship extends ServerResource {
@SuppressWarnings("rawtypes")
@Get("json")
public JsonRepresentation toJson() throws JSONException {
try {
JSONObject returnJson = new JSONObject();
String sourceEpc = (String)getRequest().getAttributes().get("subject");
String targetEpc = (String)getRequest().getAttributes().get("object");
String socialRelationship = (String)getRequest().getAttributes().get("property");
String type = getQuery().getValues("type");
if( type == null )
{
returnJson.put("system_message", "additional parameter 'type' is needed where 'type' is one of 'connect', 'delete', 'toggle'");
JsonRepresentation representation = new JsonRepresentation(returnJson);
return representation;
}
//Save Space to Ontology if it is not saved
RemoteProjectManager rpm = RemoteProjectManager.getInstance();
Project p = rpm.getProject("localhost:5100", "Lilliput", "1234", "IoTSocialGraph", true);
OWLModel owlModel = (OWLModel)p.getKnowledgeBase();
RDFIndividual sourceInd = owlModel.getOWLIndividual(sourceEpc);
RDFIndividual destInd = owlModel.getOWLIndividual(targetEpc);
OWLObjectProperty prop = owlModel.getOWLObjectProperty(socialRelationship);
String sourceStr = sourceInd.getBrowserText();
String destStr = destInd.getBrowserText();
String propStr = prop.getBrowserText();
Collection propCol = sourceInd.getPropertyValues(prop);
Iterator propIter = propCol.iterator();
boolean isRelated=false;
while( propIter.hasNext() )
{
RDFIndividual tempInd = (RDFIndividual)propIter.next();
if(destInd.getBrowserText().equals(tempInd.getBrowserText()))
{
isRelated = true;
break;
}
System.out.println(tempInd.getBrowserText());
}
if( type.equals("connect"))
{
if( isRelated )
{
returnJson.put("system_message", "Already connected");
}
else
{
sourceInd.addPropertyValue(prop, destInd);
returnJson.put("system_message", "done");
}
}
else if( type.equals("delete"))
{
if( isRelated )
{
sourceInd.removePropertyValue(prop, destInd);
returnJson.put("system_message", "done");
}
else
{
returnJson.put("system_message", "Already disconnected");
}
}
else if( type.equals("toggle"))
{
if ( isRelated == false )
{
sourceInd.addPropertyValue(prop, destInd);
returnJson.put("system_message", "done");
}
else
{
sourceInd.removePropertyValue(prop, destInd);
returnJson.put("system_message", "done");
}
}
p.dispose();
JsonRepresentation representation = new JsonRepresentation(returnJson);
return representation;
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject returnJson = new JSONObject();
returnJson.put("system_message", "error occured");
JsonRepresentation representation = new JsonRepresentation(returnJson);
return representation;
}
}