it.uniroma2.art.semanticturkey.extensions.sheet2rdf.services.Sheet2RDFController_old Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stx-sheet2rdf Show documentation
Show all versions of stx-sheet2rdf Show documentation
Sheet2RDF extension for Semantic Turkey. Sheet2RDF is a platform for acquisition and transformation of datasheets into RDF.
package it.uniroma2.art.semanticturkey.extensions.sheet2rdf.services;
import it.uniroma2.art.coda.exception.ConverterException;
import it.uniroma2.art.coda.exception.DependencyException;
import it.uniroma2.art.coda.exception.PRParserException;
import it.uniroma2.art.coda.osgi.bundle.CODAOSGiFactory;
import it.uniroma2.art.coda.provisioning.ComponentProvisioningException;
import it.uniroma2.art.coda.structures.ARTTriple;
import it.uniroma2.art.coda.structures.SuggOntologyCoda;
import it.uniroma2.art.owlart.exceptions.ModelAccessException;
import it.uniroma2.art.owlart.exceptions.ModelUpdateException;
import it.uniroma2.art.owlart.exceptions.QueryEvaluationException;
import it.uniroma2.art.owlart.exceptions.UnsupportedQueryLanguageException;
import it.uniroma2.art.owlart.exceptions.UnsupportedRDFFormatException;
import it.uniroma2.art.owlart.io.RDFNodeSerializer;
import it.uniroma2.art.owlart.models.OWLModel;
import it.uniroma2.art.owlart.query.MalformedQueryException;
import it.uniroma2.art.semanticturkey.exceptions.InvalidProjectNameException;
import it.uniroma2.art.semanticturkey.extensions.sheet2rdf.Sheet2RDFOrchestrator;
import it.uniroma2.art.semanticturkey.project.ProjectManager;
import it.uniroma2.art.semanticturkey.services.STServiceContext;
import it.uniroma2.art.semanticturkey.servlet.ServiceVocabulary.RepliesStatus;
import it.uniroma2.art.semanticturkey.servlet.ServletUtilities;
import it.uniroma2.art.semanticturkey.servlet.XMLResponseREPLY;
import it.uniroma2.art.semanticturkey.utilities.XMLHelp;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.uima.UIMAException;
import org.osgi.framework.BundleContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Element;
@Controller
public class Sheet2RDFController_old{
private static final String SERVICE_NAME = "Sheet2RDFController";
@Autowired
private STServiceContext serviceContext;
@Autowired
private CODAOSGiFactory codaOSGiFactory;
@Autowired
private BundleContext bundleContext;
private Sheet2RDFOrchestrator orchestrator;//initialized once the file is uploaded
public Sheet2RDFController_old(){
System.out.println(SERVICE_NAME+" initialized");
}
/**
* Returns an array list containing the headers of the Excel file's data sheet
* @return
*/
@RequestMapping(value = "/"+SERVICE_NAME+"/getHeaders", method = RequestMethod.GET, produces="application/xml;charset=UTF-8")
@ResponseBody
public String getHeaders(){
XMLResponseREPLY resp = ServletUtilities.getService().createReplyResponse("getHeaders", RepliesStatus.ok);
Element dataElem = resp.getDataElement();
List headers = orchestrator.getHeaders();
for (String h : headers){
XMLHelp.newElement(dataElem, "header", h);
}
return resp.getResponseContent();
}
/**
* Returns a structure containing the data inside the excel datasheet
* @return
*/
@RequestMapping(value = "/"+SERVICE_NAME+"/getTablePreview", method = RequestMethod.GET, produces="application/xml;charset=UTF-8")
@ResponseBody
public String getTablePreview(){
XMLResponseREPLY resp = ServletUtilities.getService().createReplyResponse("getTableReview", RepliesStatus.ok);
Element dataElem = resp.getDataElement();
ArrayList> table = orchestrator.getTablePreview();
Element tableElem = XMLHelp.newElement(dataElem, "table");
for (int r = 0; r < table.size(); r++){
Element rowElem = XMLHelp.newElement(tableElem, "row");
rowElem.setAttribute("idx", r+"");
for (int c = 0; c < table.get(0).size(); c++){
String cellValue = table.get(r).get(c);
if (!cellValue.equals("")){
Element cellElem = XMLHelp.newElement(rowElem, "cell");
cellElem.setAttribute("idx", c+"");//column index in excel spreadsheet
cellElem.setAttribute("value", cellValue);
}
}
}
return resp.getResponseContent();
}
/**
* Returns the PEARL code generated by Excel2RDF
* @return
* @throws IOException
*/
@RequestMapping(value = "/"+SERVICE_NAME+"/getPearl", method = RequestMethod.GET, produces="application/xml;charset=UTF-8")
@ResponseBody
public String getPearl(@RequestParam(value="skosSchema", required=false) String skosSchema) throws IOException{
// System.out.println("Skos schema ricevuto: "+skosSchema);
if (skosSchema != null)
orchestrator.setSkosSchema(skosSchema);
String pearl = orchestrator.getPearl();
XMLResponseREPLY resp = ServletUtilities.getService().createReplyResponse("getPearl", RepliesStatus.ok);
Element dataElem = resp.getDataElement();
dataElem.setTextContent(pearl);
return resp.getResponseContent();
}
/**
* Saves/updates the PEARL code eventually edited by user
* @return
* @throws FileNotFoundException
*/
@RequestMapping(value = "/"+SERVICE_NAME+"/savePearl", method = RequestMethod.POST, produces="application/xml;charset=UTF-8")
@ResponseBody
public String savePearl(@RequestParam("pearlCode") String pearlCode) throws FileNotFoundException{
// System.out.println("Codice pearl ricevuto:\n"+pearlCode);
orchestrator.savePearl(pearlCode);
XMLResponseREPLY response = ServletUtilities.getService().createReplyResponse("savePearl", RepliesStatus.ok);
return response.getResponseContent();
}
/**
* Uploads an excel file into a server directory
* @param name
* @param file
* @return
*/
@RequestMapping(value = "/"+SERVICE_NAME+"/uploadExcel", method = RequestMethod.POST, produces="application/xml;charset=UTF-8")
@ResponseBody
public String uploadExcel(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
try {
//Creating the directory on the server to store file
String projectName = serviceContext.getProject().getName();
String projectFolderPath = ProjectManager.resolveProjectNameToDir(projectName).getAbsolutePath()
+ File.separator + "e2rdf" + File.separator;
//initialize the orchestrator
orchestrator = new Sheet2RDFOrchestrator(projectFolderPath, codaOSGiFactory.getInstance(bundleContext));
//retrieve the ontology model
OWLModel model = serviceContext.getProject().getOWLModel();
orchestrator.setModel(model);
File dir = new File(projectFolderPath);
if (!dir.exists())
dir.mkdirs();
//store the file into the created directory
byte[] bytes = file.getBytes();
File serverFile = new File(dir.getAbsolutePath() + File.separator + name);
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
System.out.println("File " + name + " uploaded succesfully");
//pass the excel file path to the orchestrator
orchestrator.setExcelFile(serverFile);
XMLResponseREPLY response = ServletUtilities.getService().createReplyResponse("uploadFile", RepliesStatus.ok);
return response.getResponseContent();
} catch (IOException e) {
e.printStackTrace();
XMLResponseREPLY response = ServletUtilities.getService().createReplyFAIL("uploadFile",
"An exception has been thrown while the excel was uploading");
return response.getResponseContent();
} catch (InvalidProjectNameException e) {
e.printStackTrace();
}
}
XMLResponseREPLY response = ServletUtilities.getService().createReplyFAIL("uploadFile",
"The file that it was trying to upload is empty");
return response.getResponseContent();
}
/**
* Uploads a pearl file into a server directory and returns the code
* @param name
* @param file
* @return
*/
@RequestMapping(value = "/"+SERVICE_NAME+"/uploadPearl", method = RequestMethod.POST, produces="application/xml;charset=UTF-8")
@ResponseBody
public String uploadPearl(@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
try {
//Creating the directory on the server to store file
String projectName = serviceContext.getProject().getName();
String projectFolderPath = ProjectManager.resolveProjectNameToDir(projectName).getAbsolutePath()
+ File.separator + "e2rdf" + File.separator;
File dir = new File(projectFolderPath);
if (!dir.exists())
dir.mkdirs();
//store the file as "pearl.pr" into the created directory
byte[] bytes = file.getBytes();
File serverFile = new File(dir.getAbsolutePath() + File.separator + "pearl.pr");
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
System.out.println("Pearl file uploaded succesfully");
XMLResponseREPLY response = ServletUtilities.getService().createReplyResponse("uploadPearl", RepliesStatus.ok);
Element dataElem = response.getDataElement();
String pearlCode = orchestrator.pearlFileToString(serverFile);
dataElem.setTextContent(pearlCode);
return response.getResponseContent();
} catch (IOException e) {
e.printStackTrace();
XMLResponseREPLY response = ServletUtilities.getService().createReplyFAIL("uploadPearl",
"An exception has been thrown while the pearl file was uploading");
return response.getResponseContent();
} catch (InvalidProjectNameException e) {
e.printStackTrace();
}
}
XMLResponseREPLY response = ServletUtilities.getService().createReplyFAIL("uploadPearl",
"The file that it was trying to upload is empty");
return response.getResponseContent();
}
/**
* Returns triples generated by Sheet2RDF
* XML Response structure: data -> triple -> subject, predicate, object. This means that inside
* data element will be some triple element containing subject predicate and object element
* @return
* @throws QueryEvaluationException
* @throws MalformedQueryException
* @throws ModelAccessException
* @throws UnsupportedQueryLanguageException
* @throws ConverterException
* @throws ComponentProvisioningException
* @throws PRParserException
* @throws UIMAException
* @throws DependencyException
* @throws Exception
*/
@RequestMapping(value = "/"+SERVICE_NAME+"/getTriples", method = RequestMethod.GET, produces="application/xml;charset=UTF-8")
@ResponseBody
public String getTriples() throws UIMAException, PRParserException, ComponentProvisioningException, ConverterException,
UnsupportedQueryLanguageException, ModelAccessException, MalformedQueryException, QueryEvaluationException, DependencyException {
XMLResponseREPLY resp = ServletUtilities.getService().createReplyResponse("getTriples", RepliesStatus.ok);
Element dataElem = resp.getDataElement();
List listOntoSuggCoda = orchestrator.getTriples();
for (SuggOntologyCoda suggOntCoda : listOntoSuggCoda){
if (suggOntCoda.getAnnotation().getType().getName().startsWith("it.uniroma2")){
List tripleList = suggOntCoda.getARTTripleList();
for (ARTTriple t : tripleList){
Element tripleElem = XMLHelp.newElement(dataElem, "triple");
//to specify which row of excel datasheet generate that triple
tripleElem.setAttribute("row", suggOntCoda.getAnnotation().getBegin()+"");
XMLHelp.newElement(tripleElem, "subject", RDFNodeSerializer.toNT(t.getSubject()));
XMLHelp.newElement(tripleElem, "predicate", RDFNodeSerializer.toNT(t.getPredicate()));
XMLHelp.newElement(tripleElem, "object", RDFNodeSerializer.toNT(t.getObject()));
}
}
}
return resp.getResponseContent();
}
/**
* Simply adds the triples generated to the model
* @return
* @throws UnsupportedRDFFormatException
* @throws ModelUpdateException
* @throws ModelAccessException
* @throws IOException
* @throws FileNotFoundException
*/
@RequestMapping(value="/"+SERVICE_NAME+"/addTriples", method = RequestMethod.POST, produces="application/xml;charset=UTF-8")
@ResponseBody
public String addTriples(@RequestParam("triples") String triples) throws FileNotFoundException, IOException, ModelAccessException, ModelUpdateException, UnsupportedRDFFormatException{
orchestrator.addTriples(triples);
XMLResponseREPLY response = ServletUtilities.getService().createReplyResponse("addTriples", RepliesStatus.ok);
return response.getResponseContent();
}
/**
* Checks if all the placeholder have been replaced
* @return
* @throws IOException
*/
@RequestMapping(value="/"+SERVICE_NAME+"/isPearlReady", method = RequestMethod.GET, produces="application/xml;charset=UTF-8")
@ResponseBody
public String isPearlReady() throws IOException{
boolean resp = orchestrator.isPearlReady();
XMLResponseREPLY response = ServletUtilities.getService().createBooleanResponse("isPearlReady", resp);
return response.getResponseContent();
}
}