package kr.ac.kaist.resl.lilliput.rest;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import kr.ac.kaist.resl.lilliput.ac.AccessController;
import kr.ac.kaist.resl.lilliput.ac.AccessibilityPair;
import org.json.JSONArray;
import org.json.JSONObject;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
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.OWLNamedClass;
import edu.stanford.smi.protegex.owl.model.OWLObjectProperty;
import edu.stanford.smi.protegex.owl.model.RDFIndividual;
public class GetMyPlace extends ServerResource {
@SuppressWarnings("rawtypes")
@Get("json")
public JsonRepresentation toJson() {
try
{
String epc = (String)getRequest().getAttributes().get("uid");
//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();
@SuppressWarnings("unused")
OWLNamedClass humanClass = owlModel.getOWLNamedClass("Human");
@SuppressWarnings("unused")
OWLNamedClass indoorSpaceClass = owlModel.getOWLNamedClass("IndoorSpace");
OWLObjectProperty hasOwnership = owlModel.getOWLObjectProperty("hasOwnership");
OWLObjectProperty hasFriend = owlModel.getOWLObjectProperty("hasFriend");
OWLObjectProperty isFollowing = owlModel.getOWLObjectProperty("isFollowing");
RDFIndividual humanInd;
List<AccessibilityPair> acPair = AccessController.getAccessibilityList(owlModel, epc);
JSONArray resultArray = new JSONArray();
JSONObject resultObject;
humanInd = owlModel.getRDFIndividual(epc);
Collection destCol = humanInd.getPropertyValues(hasOwnership);
Iterator destIter = destCol.iterator();
while(destIter.hasNext())
{
RDFIndividual destInd = (RDFIndividual)destIter.next();
String destEPC = destInd.getBrowserText();
if( destEPC.contains("sgln") == true )
{
resultObject = new JSONObject();
resultObject.put("epc", destEPC);
JSONObject returnObj = Information.getInformation(owlModel, destEPC);
if( returnObj != null )
{
resultObject.put("information", returnObj);
}
resultArray.put(resultObject);
}
}
Collection friendCol = humanInd.getPropertyValues(hasFriend);
Iterator friendIter = friendCol.iterator();
while( friendIter.hasNext())
{
RDFIndividual destInd = (RDFIndividual)friendIter.next();
Collection fpCol = destInd.getPropertyValues(hasOwnership);
Iterator fpIter = fpCol.iterator();
while(fpIter.hasNext())
{
RDFIndividual fpInd = (RDFIndividual)fpIter.next();
String fpEPC = fpInd.getBrowserText();
if( !AccessController.isAccessible(acPair, epc, fpEPC))
{
continue;
}
if( fpEPC.contains("sgln") == true )
{
resultObject = new JSONObject();
resultObject.put("epc", fpEPC);
JSONObject returnObj = Information.getInformation(owlModel,fpEPC);
if( returnObj != null )
{
resultObject.put("information", returnObj);
}
resultArray.put(resultObject);
}
}
}
Collection followCol = humanInd.getPropertyValues(isFollowing);
Iterator followIter = followCol.iterator();
while( followIter.hasNext())
{
RDFIndividual destInd = (RDFIndividual)followIter.next();
Collection fpCol = destInd.getPropertyValues(hasOwnership);
Iterator fpIter = fpCol.iterator();
while(fpIter.hasNext())
{
RDFIndividual fpInd = (RDFIndividual)fpIter.next();
String fpEPC = fpInd.getBrowserText();
if( !AccessController.isAccessible(acPair, epc, fpEPC))
{
continue;
}
if( fpEPC.contains("sgln") == true )
{
resultObject = new JSONObject();
resultObject.put("epc", fpEPC);
JSONObject returnObj = Information.getInformation(owlModel, fpEPC);
if( returnObj != null )
{
resultObject.put("information", returnObj);
}
resultArray.put(resultObject);
}
}
}
p.dispose();
JsonRepresentation representation = new JsonRepresentation(resultArray);
return representation;
}catch( Exception e)
{
e.printStackTrace();
return null;
}
}
}