package eu.betaas.taas.contextmanager.linkeddata.semantics; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import org.apache.log4j.Logger; import com.google.gson.Gson; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.Property; import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.rdf.model.ResourceFactory; import com.hp.hpl.jena.sparql.vocabulary.FOAF; import com.hp.hpl.jena.vocabulary.DCTerms; import eu.betaas.taas.contextmanager.linkeddata.clients.TaaSCMClient; import eu.betaas.taas.contextmanager.linkeddata.clients.Thing; public class ThingsManager { private Logger logger= Logger.getLogger("betaas.taas"); private TaaSCMClient cmClient; public ThingsManager () { cmClient = TaaSCMClient.instance(); } public String retrieveThingListAsDCAT() { Thing result; ByteArrayOutputStream stream = new ByteArrayOutputStream(); String DCAT_NS = "http://www.w3.org/ns/dcat#"; Model instanceModel = ModelFactory.createDefaultModel(); instanceModel.setNsPrefix("dc", DCTerms.NS); instanceModel.setNsPrefix("dcat", DCAT_NS); instanceModel.setNsPrefix("foaf", FOAF.NS); // Create the Catalog object Resource catalog = ResourceFactory.createResource(DCAT_NS + "Catalog"); Resource betaas = instanceModel.createResource(FOAF.Project); betaas.addProperty(FOAF.homepage, instanceModel.createResource("http://www.betaas.eu/")); betaas.addProperty(FOAF.name, "BETaaS"); Resource myCatalog = instanceModel.createResource("http://www.betaas.eu/instanceName", catalog); myCatalog.addProperty(DCTerms.title, "BETaaS Catalog"); myCatalog.addProperty(DCTerms.description, "Catalog automatically created by a BETaaS instance about its Thing Services."); myCatalog.addProperty(DCTerms.issued, Calendar.getInstance().getTime().toString()); myCatalog.addProperty(DCTerms.modified, Calendar.getInstance().getTime().toString()); myCatalog.addProperty(DCTerms.language, instanceModel.createResource("http://id.loc.gov/vocabulary/iso639-1/en")); myCatalog.addProperty(DCTerms.license, instanceModel.createResource("http://creativecommons.org/licenses/by/3.0/")); myCatalog.addProperty(DCTerms.publisher, betaas); myCatalog.addProperty(FOAF.homepage, instanceModel.createResource("http://localhost:8080/InstanceManager")); for (String thingService : cmClient.getThingServices()) { result = cmClient.getFullMeasurement(thingService); // Create Dataset Resource dataset = ResourceFactory.createResource(DCAT_NS + "Dataset"); Property dcatDataset = ResourceFactory.createProperty(DCAT_NS, "dataset"); Resource myDataset = instanceModel.createResource("http://www.betaas.eu/instanceName/" + thingService, dataset); myDataset.addProperty(DCTerms.title, thingService); myDataset.addProperty(DCTerms.description, result.getMeasurement() + " sensor attached to the BETaaS instance"); myDataset.addProperty(DCTerms.issued, Calendar.getInstance().getTime().toString()); myDataset.addProperty(DCTerms.modified, Calendar.getInstance().getTime().toString()); myDataset.addProperty(DCTerms.language, instanceModel.createResource("http://id.loc.gov/vocabulary/iso639-1/en")); myDataset.addProperty(DCTerms.accrualPeriodicity, instanceModel.createResource("http://purl.org/linked-data/sdmx/2009/code#freq-D")); myDataset.addProperty(DCTerms.publisher, betaas); myCatalog.addProperty(dcatDataset, myDataset); // Create CSV Distribution for Dataset Property dcatDistribution = ResourceFactory.createProperty(DCAT_NS, "distribution"); Property dcatMediaType = ResourceFactory.createProperty(DCAT_NS, "mediaType"); Property dcatDownload = ResourceFactory.createProperty(DCAT_NS, "downloadURL"); Property dcatSize = ResourceFactory.createProperty(DCAT_NS, "byteSize"); Resource distributionDs = ResourceFactory.createResource(DCAT_NS + "Distribution"); Resource myDatasetDistribution = instanceModel.createResource("http://www.betaas.eu/instanceName/" + thingService + "/Distribution1", distributionDs); myDatasetDistribution.addProperty(DCTerms.title, "ThingServiceName Dataset in CSV"); myDatasetDistribution.addProperty(DCTerms.description, "Data generated by the Thing Service " + thingService + " during the last 50 records, provided in CSV format."); myDatasetDistribution.addProperty(DCTerms.issued, Calendar.getInstance().getTime().toString()); myDatasetDistribution.addProperty(DCTerms.modified, Calendar.getInstance().getTime().toString()); myDatasetDistribution.addProperty(DCTerms.license, instanceModel.createResource("http://creativecommons.org/licenses/by/3.0/")); myDatasetDistribution.addProperty(dcatMediaType, "text/csv"); myDatasetDistribution.addProperty(dcatDownload, instanceModel.createResource("http://localhost:8080/dataset/" + thingService)); myDatasetDistribution.addProperty(dcatSize, "1024"); myDataset.addProperty(dcatDistribution, myDatasetDistribution); } // Write the results instanceModel.write(stream, "RDF/XML-ABBREV"); return stream.toString(); } public String retrieveThingListAsJSON() { List<Thing> things = new ArrayList<Thing>(); TaaSCMClient cmClient = TaaSCMClient.instance(); for (String thingService : cmClient.getThingServices()) { things.add(cmClient.getFullMeasurement(thingService)); } return new Gson().toJson(things); } public void updateNewDcatModel(String idThingService) { // TODO at the moment we can create a new one } public void updateRemoveDcatModel(String idThingService) { // TODO at the moment we can create a new one } }