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.OWLDatatypeProperty;
import edu.stanford.smi.protegex.owl.model.OWLModel;
import edu.stanford.smi.protegex.owl.model.RDFIndividual;
public class UtilizeDataType extends ServerResource {
@SuppressWarnings("rawtypes")
@Get("json")
public JsonRepresentation toJson() throws JSONException {
try {
String targetEpc = (String)getRequest().getAttributes().get("ind_name");
String datatypeProperty = (String)getRequest().getAttributes().get("property");
String type = getQuery().getValues("type");
if( type == null )
{
JSONObject returnJson = new JSONObject();
returnJson.put("system_message", "additional parameter 'type' is needed where 'type' is one of 'get', 'put'");
JsonRepresentation representation = new JsonRepresentation(returnJson);
return representation;
}
String value = getQuery().getValues("value");
if( type.equals("put") && value == null )
{
JSONObject returnJson = new JSONObject();
returnJson.put("system_message", "additional parameter 'value' is needed when 'type' value is 'put'");
JsonRepresentation representation = new JsonRepresentation(returnJson);
return representation;
}
RemoteProjectManager rpm = RemoteProjectManager.getInstance();
Project p = rpm.getProject("localhost:5100", "Lilliput", "1234", "IoTSocialGraph", true);
OWLModel owlModel = (OWLModel)p.getKnowledgeBase();
RDFIndividual targetInd = owlModel.getOWLIndividual(targetEpc);
OWLDatatypeProperty prop = owlModel.getOWLDatatypeProperty(datatypeProperty);
if( type.equals("get"))
{
if( targetInd.getPropertyValue(prop) != null )
{
String retVal = targetInd.getPropertyValue(prop).toString();
JSONObject returnJson = new JSONObject();
returnJson.put("datatype_value", retVal);
JsonRepresentation representation = new JsonRepresentation(returnJson);
p.dispose();
return representation;
}
else
{
JSONObject returnJson = new JSONObject();
returnJson.put("system_message", "Null");
JsonRepresentation representation = new JsonRepresentation(returnJson);
p.dispose();
return representation;
}
}
else if( type.equals("put"))
{
//����Ʈ ����
targetInd.setPropertyValue(prop, value);
p.dispose();
JSONObject returnJson = new JSONObject();
returnJson.put("system_message", "Done");
JsonRepresentation representation = new JsonRepresentation(returnJson);
return representation;
}
} catch (MongoException 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;
}
}