package kr.ac.kaist.resl.lilliput.rest; import java.net.UnknownHostException; import java.util.Collection; import java.util.Iterator; import java.util.Map; import kr.ac.kaist.resl.lilliput.util.Util; import org.bson.types.ObjectId; import org.json.JSONArray; 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.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBObject; import com.mongodb.Mongo; 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.OWLDatatypeProperty; 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 Information extends ServerResource { @Get("json") public JsonRepresentation toJson() { try { String epc = (String)getRequest().getAttributes().get("uid"); RemoteProjectManager rpm = RemoteProjectManager.getInstance(); Project p = rpm.getProject("localhost:5100", "Lilliput", "1234", "IoTSocialGraph", true); OWLModel owlModel = (OWLModel)p.getKnowledgeBase(); JSONObject returnJson = getInformation(owlModel, epc); JsonRepresentation representation = new JsonRepresentation(returnJson); // Returns the XML representation of this document. return representation; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static JSONArray getServices(OWLModel owlModel, String epc ) { try { JSONArray returnArray = new JSONArray(); JSONObject indObj = null; OWLNamedClass humanClass = owlModel.getOWLNamedClass("Human"); OWLNamedClass objectClass = owlModel.getOWLNamedClass("Object"); OWLNamedClass spaceClass = owlModel.getOWLNamedClass("IndoorSpace"); OWLNamedClass epcService = owlModel.getOWLNamedClass("EPCISService"); OWLNamedClass RESTService = owlModel.getOWLNamedClass("RESTService"); OWLNamedClass WebService = owlModel.getOWLNamedClass("WebService"); OWLNamedClass wsParam = owlModel.getOWLNamedClass("WebServiceParameter"); OWLNamedClass wsReturn = owlModel.getOWLNamedClass("WebServiceReturnValue"); OWLDatatypeProperty epcisURL = owlModel.getOWLDatatypeProperty("epcisURL"); OWLDatatypeProperty restURL = owlModel.getOWLDatatypeProperty("RESTURL"); OWLDatatypeProperty wsMethodName = owlModel.getOWLDatatypeProperty("wsMethodName"); OWLDatatypeProperty wsSoapAction = owlModel.getOWLDatatypeProperty("wsSoapAction"); OWLDatatypeProperty wsNameSpace = owlModel.getOWLDatatypeProperty("wsNameSpace"); OWLDatatypeProperty wsURL = owlModel.getOWLDatatypeProperty("wsURL"); OWLDatatypeProperty wsArgumentName = owlModel.getOWLDatatypeProperty("wsArgumentName"); OWLDatatypeProperty wsArgumentType = owlModel.getOWLDatatypeProperty("wsArgumentType"); OWLDatatypeProperty wsReturnType = owlModel.getOWLDatatypeProperty("wsReturnValueType"); OWLDatatypeProperty wsReturnName = owlModel.getOWLDatatypeProperty("wsReturnValueName"); OWLObjectProperty service = owlModel.getOWLObjectProperty("hasService"); OWLObjectProperty hasParameter= owlModel.getOWLObjectProperty("hasParameter"); OWLObjectProperty hasReturnValue = owlModel.getOWLObjectProperty("hasReturnValue"); RDFIndividual something = owlModel.getOWLIndividual(epc); //���� �� Collection serCol = something.getPropertyValues(service); Iterator iterCol = serCol.iterator(); while( iterCol.hasNext()) { RDFIndividual serviceInd = (RDFIndividual)iterCol.next(); String sName = serviceInd.getBrowserText(); if( sName.contains("EPCIS")) { indObj = new JSONObject(); //���� EPCService�� �ν��Ͻ����� ������ String epcUrlStr = (String)serviceInd.getPropertyValue(epcisURL); indObj.put("type", "epcis"); indObj.put("url", epcUrlStr); }else if( sName.contains("REST")) { indObj = new JSONObject(); String restURLStr = (String)serviceInd.getPropertyValue(restURL); indObj.put("type", "rest"); indObj.put("url", restURLStr); }else if( sName.contains("Web")) { indObj = new JSONObject(); String wsMethodNameStr = (String)serviceInd.getPropertyValue(wsMethodName); String wsSoapActionStr = (String)serviceInd.getPropertyValue(wsSoapAction); String wsNameSpaceStr = (String)serviceInd.getPropertyValue(wsNameSpace); String wsURLStr = (String)serviceInd.getPropertyValue(wsURL); indObj.put("type", "ws"); indObj.put("method_name", wsMethodNameStr); indObj.put("soap_action", wsSoapActionStr); indObj.put("name_space", wsNameSpaceStr); indObj.put("url", wsURLStr); JSONArray paramArr = new JSONArray(); Collection paramCol = serviceInd.getPropertyValues(hasParameter); Iterator paramIter = paramCol.iterator(); while( paramIter.hasNext() ) { //�� �Ķ���Ϳ� ���� ó���� �־�� �Ѵ� JSONObject param = new JSONObject(); RDFIndividual paramInd = (RDFIndividual)paramIter.next(); String arguStr = (String)paramInd.getPropertyValue(wsArgumentName); String arguType = (String)paramInd.getPropertyValue(wsArgumentType); param.put("name", arguStr); param.put("type", arguType); paramArr.put(param); } indObj.put("parameters", paramArr); JSONArray returnArr = new JSONArray(); Collection returnCol = serviceInd.getPropertyValues(hasReturnValue); Iterator returnIter = returnCol.iterator(); while( returnIter.hasNext() ) { //�� �Ķ���Ϳ� ���� ó���� �־�� �Ѵ� JSONObject retObj = new JSONObject(); RDFIndividual retInd = (RDFIndividual)returnIter.next(); String retStr = (String)retInd.getPropertyValue(wsReturnName); String retType = (String)retInd.getPropertyValue(wsReturnType); retObj.put("name", retStr); retObj.put("type", retType); returnArr.put(retObj); } indObj.put("returns", returnArr); } returnArray.put(indObj); } return returnArray; }catch( JSONException e ) { } return null; } public static JSONObject getInformation(OWLModel owlModel, String epc ) { try { JSONObject returnJson = new JSONObject(); //Save Space to Ontology if it is not saved RDFIndividual humanInd; RDFIndividual objectInd; RDFIndividual spaceInd; String[] epcSplit = epc.split(":"); OWLObjectProperty prop ; RDFIndividual informInd = null; String dbCollection = null; String objectID = null; if(epcSplit[3].equals("gid")) { humanInd = owlModel.getOWLIndividual(epc); prop = owlModel.getOWLObjectProperty("hasHumanInformation"); informInd = (RDFIndividual)humanInd.getPropertyValue(prop); dbCollection = "HumanInformation"; } else if(epcSplit[3].equals("sgln")) { spaceInd = owlModel.getOWLIndividual(epc); prop = owlModel.getOWLObjectProperty("hasSpaceInformation"); informInd = (RDFIndividual)spaceInd.getPropertyValue(prop); dbCollection = "SpaceInformation"; } else if(epcSplit[3].equals("sgtin")) { objectInd = owlModel.getOWLIndividual(epc); prop = owlModel.getOWLObjectProperty("hasObjectInformation"); informInd = (RDFIndividual)objectInd.getPropertyValue(prop); dbCollection = "ObjectInformation"; } objectID = Util.individualToBrowserText(informInd.getBrowserText()); Mongo m = new Mongo("143.248.106.26", 27017); System.out.println("connect db"); DB db = m.getDB("Lilliput"); System.out.println("getting db"); DBCollection collection = db.getCollection(dbCollection); System.out.println("getting collection"); DBObject query = new BasicDBObject(); query.put("_id",new ObjectId(objectID)); DBObject queriedObject = collection.findOne(query); if( queriedObject == null ) { return null; } else { @SuppressWarnings("rawtypes") Map queriedMap = queriedObject.toMap(); returnJson = new JSONObject(queriedMap); if(epcSplit[3].equals("gid") ) { String fid = returnJson.getString("fid"); String pictureURL = "http://graph.facebook.com/" + fid + "/picture"; returnJson.put("picture", pictureURL); } } return returnJson; }catch( MongoException e) { } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static JSONObject getLocation(OWLModel owlModel, String epc) { try { JSONObject returnJson = new JSONObject(); //Save Space to Ontology if it is not saved RDFIndividual humanInd; RDFIndividual objectInd; RDFIndividual spaceInd; String[] epcSplit = epc.split(":"); OWLObjectProperty prop ; RDFIndividual informInd = null; String dbCollection = null; String objectID = null; OWLDatatypeProperty px = owlModel.getOWLDatatypeProperty("px"); OWLDatatypeProperty py = owlModel.getOWLDatatypeProperty("py"); Object pxValue; Object pyValue; if(epcSplit[3].equals("gid")) { humanInd = owlModel.getOWLIndividual(epc); pxValue = humanInd.getPropertyValue(px); pyValue = humanInd.getPropertyValue(py); returnJson.put("px", pxValue); returnJson.put("py", pyValue); } else if(epcSplit[3].equals("sgtin")) { objectInd = owlModel.getOWLIndividual(epc); pxValue = objectInd.getPropertyValue(px); pyValue = objectInd.getPropertyValue(py); returnJson.put("px", pxValue); returnJson.put("py", pyValue); } return returnJson; }catch( MongoException e) { } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }