package kr.ac.kaist.resl.lilliput.rest;
import java.io.IOException;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import kr.ac.kaist.resl.lilliput.core.DeviceInformationAdapter;
import kr.ac.kaist.resl.lilliput.core.SNSPusher;
import kr.ac.kaist.resl.lilliput.util.EPCISHelper;
import net.sf.json.xml.XMLSerializer;
import org.fosstrak.epcis.model.Poll;
import org.fosstrak.epcis.model.QueryResults;
import org.fosstrak.epcis.queryclient.QueryControlClient;
import org.fosstrak.epcis.soap.ImplementationExceptionResponse;
import org.fosstrak.epcis.soap.NoSuchNameExceptionResponse;
import org.fosstrak.epcis.soap.QueryParameterExceptionResponse;
import org.fosstrak.epcis.soap.QueryTooComplexExceptionResponse;
import org.fosstrak.epcis.soap.QueryTooLargeExceptionResponse;
import org.fosstrak.epcis.soap.SecurityExceptionResponse;
import org.fosstrak.epcis.soap.ValidationExceptionResponse;
import org.fosstrak.epcis.utils.QueryResultsParser;
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;
public class STISBroker extends ServerResource {
@Get("json")
public JsonRepresentation toJson() throws JSONException {
try {
String epc = (String)getRequest().getAttributes().get("epc");
//String url = getQuery().getValues("url");
String url = "http://143.248.106.137/epcis-repository-0.4.2/query";
//Query to EPCIS repository
QueryControlClient client = new QueryControlClient(url);
//Poll
EPCISHelper eHelper = new EPCISHelper();
Poll poll = null;
Poll poll2 = null;
if( epc.contains("gid") || epc.contains("sgtin"))
{
poll = eHelper.createRecentObjectPoll(epc);
poll2 = eHelper.createRecentSensingObjectPoll(epc);
QueryResults results = client.poll(poll);
QueryResults results2 = client.poll(poll2);
//Retrieve Document
String result = QueryResultsParser.queryResultsToXml(results);
String result2 = QueryResultsParser.queryResultsToXml(results2);
XMLSerializer serializer = new XMLSerializer();
net.sf.json.JSONObject resultJSON = (net.sf.json.JSONObject)serializer.read(result);
net.sf.json.JSONObject resultJSON2 = (net.sf.json.JSONObject)serializer.read(result2);
JSONObject returnObject = new JSONObject(resultJSON.toString());
JSONObject returnObject2 = new JSONObject(resultJSON2.toString());
//Reflecting it to the IoT-Social Graph
DeviceInformationAdapter da = new DeviceInformationAdapter();
da.objectInformationAdapter(returnObject, epc);
da.sensingInformationAdapter(returnObject2, epc);
//Reflecting it to the Facebook
SNSPusher sPusher = new SNSPusher();
sPusher.pushToFacebook("urn:epc:id:sgtin:0000001.000001.1", "hello");
JSONObject totObj = new JSONObject();
totObj.put("ObjectEvent", returnObject);
totObj.put("SensingEvent", returnObject2);
JsonRepresentation representation = new JsonRepresentation(totObj);
return representation;
}
else if( epc.contains("sgln"))
{
poll = eHelper.createRecentPoll(epc);
QueryResults results = client.poll(poll);
String result = QueryResultsParser.queryResultsToXml(results);
XMLSerializer serializer = new XMLSerializer();
net.sf.json.JSONObject resultJSON = (net.sf.json.JSONObject)serializer.read(result);
JSONObject returnObject = new JSONObject(resultJSON.toString());
DeviceInformationAdapter da = new DeviceInformationAdapter();
da.locationInformationAdapter(returnObject, epc);
JsonRepresentation representation = new JsonRepresentation(returnObject);
return representation;
}
//
// serializer �� json-lib�ȿ� �ְ� nu/zom/serializer ���� ezmorph�� ������� ���� org.apache.common ���� ���� commons beanutil��
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DatatypeConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ImplementationExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (QueryTooComplexExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (QueryTooLargeExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ValidationExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchNameExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (QueryParameterExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject err = new JSONObject();
err.put("message", "error occured");
JsonRepresentation representation = new JsonRepresentation(err);
return representation;
}
}