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 ToggleRelationship 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");
//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 ( isRelated == false )
{//������ �ȵǾ� �ִ�. �̾���
sourceInd.addPropertyValue(prop, destInd);
p.dispose();
returnJson.put("message", sourceStr + " and " + destStr + " is now " + propStr);
}
else
{
sourceInd.removePropertyValue(prop, destInd);
p.dispose();
returnJson.put("message", sourceStr + " and " + destStr + " is now remove " + propStr);
}
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("message", "error occured");
JsonRepresentation representation = new JsonRepresentation(returnJson);
return representation;
}
}