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 GetMyThings extends ServerResource {
@Get("json")
public JsonRepresentation toJson() {
try
{
String epc = (String)getRequest().getAttributes().get("uid");
if( epc.contains("sgln") != true )
{
return null;
}
//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");
RDFIndividual spaceInd;
List<AccessibilityPair> acPair = AccessController.getAccessibilityList(owlModel, epc);
JSONArray resultArray = new JSONArray();
JSONObject resultObject;
spaceInd = owlModel.getRDFIndividual(epc);
OWLObjectProperty contain = owlModel.getOWLObjectProperty("contain");
@SuppressWarnings("rawtypes")
Collection col = spaceInd.getPropertyValues(contain);
@SuppressWarnings("rawtypes")
Iterator iter = col.iterator();
while( iter.hasNext())
{
RDFIndividual containInd = (RDFIndividual)iter.next();
String destEPC = containInd.getBrowserText();
if( !AccessController.isAccessible(acPair, epc, destEPC))
{
continue;
}
if( destEPC.contains("gid") == true )
{
resultObject = new JSONObject();
resultObject.put("epc", destEPC);
resultArray.put(resultObject);
}
}
p.dispose();
JsonRepresentation representation = new JsonRepresentation(resultArray);
return representation;
}catch( Exception e)
{
e.printStackTrace();
return null;
}
}
}