Преглед изворни кода

- Adding CORS support for the REST calls
- Adding exceptions when queries are not returning results.

Lucas Albertins пре 2 година
родитељ
комит
990aac65f9

+ 5 - 2
src/main/java/ua/be/wee/controller/rest/NodeController.java

@@ -3,6 +3,7 @@ package ua.be.wee.controller.rest;
 import java.util.List;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.CrossOrigin;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RestController;
@@ -16,13 +17,15 @@ public class NodeController {
 	@Autowired
 	private NodeRespository nodesRepo;
 	
+	@CrossOrigin
 	@GetMapping("/nodes/{pmIRI}")
-	List<Node> all(@PathVariable String pmIRI) {
+	List<Node> all(@PathVariable String pmIRI) throws Exception {
 		return nodesRepo.getNodes(pmIRI);
 	}
 
+	@CrossOrigin
 	@GetMapping("/nodes/{pmIRI}/{nodeIRI}")
-	Node getNode(@PathVariable String pmIRI, @PathVariable String nodeIRI) {
+	Node getNode(@PathVariable String pmIRI, @PathVariable String nodeIRI) throws Exception {
 		List<Node> nodes = nodesRepo.getNodes(pmIRI);
 		for (Node node : nodes) {
 			if (node.getIri().equals(nodeIRI)) {

+ 6 - 3
src/main/java/ua/be/wee/controller/rest/PMController.java

@@ -3,6 +3,7 @@ package ua.be.wee.controller.rest;
 import java.util.List;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.CrossOrigin;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PutMapping;
@@ -23,8 +24,9 @@ public class PMController {
 	@Autowired
 	private NodeRespository nodesRepo;
 
+	@CrossOrigin
 	@GetMapping("/pms")
-	List<PM> all() {
+	List<PM> all() throws Exception {
 		List<PM> allPMs = pmRepo.getAllPMs();
 		for (PM pm : allPMs) {
 			pm.setNodes(nodesRepo.getNodes(pm.getIri()));
@@ -32,14 +34,15 @@ public class PMController {
 		return allPMs;
 	}
 	
-
+	@CrossOrigin
 	@GetMapping("/pmss/{id}")
-	PM getPM(@PathVariable String id) {
+	PM getPM(@PathVariable String id) throws Exception {
 		PM pm = pmRepo.getPM(id);
 		pm.setNodes(nodesRepo.getNodes(pm.getIri()));
 		return pm;
 	}
 	
+	@CrossOrigin
 	@PutMapping("/endpoint")
 	boolean setEndpoint(@RequestBody String url) {
 		FusekiWrapper.getInstance().setServiceURI(url);

+ 5 - 2
src/main/java/ua/be/wee/controller/rest/PTController.java

@@ -3,6 +3,7 @@ package ua.be.wee.controller.rest;
 import java.util.List;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.CrossOrigin;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RestController;
@@ -17,23 +18,25 @@ public class PTController {
 	@Autowired
 	private PTRepository ptRepository;
 	
+	@CrossOrigin
 	@GetMapping("/traces")
 	List<StartTraceEvent> allPTs() {
 		return ptRepository.getStartTraceEvents();
 	}
 	
-	
+	@CrossOrigin
 	@GetMapping("/traces/events/{traceiri}")
 	List<Event> getEvents(@PathVariable String traceiri) throws Exception {
 		return ptRepository.getEvents(traceiri);
 	}
 	
-	
+	@CrossOrigin
 	@GetMapping("/traces/active/{pmiri}")
 	List<StartTraceEvent> getActiveTraces(@PathVariable String pmiri) throws Exception {
 		return ptRepository.getActiveTraces(pmiri);
 	}
 	
+	@CrossOrigin
 	@GetMapping("/traces/finished/{pmiri}")
 	List<StartTraceEvent> getConcludedTraces(@PathVariable String pmiri) throws Exception {
 		return ptRepository.getConcludedTraces(pmiri);

+ 1 - 1
src/main/java/ua/be/wee/model/EnactmentController.java

@@ -34,7 +34,7 @@ public class EnactmentController {
 		return pmRepo.getAllPMs();
 	}
 
-	public PM getPM(String pmid) {
+	public PM getPM(String pmid) throws Exception {
 		PM pm = pmRepo.getPM(pmid);
 		pm.setNodes(nodeRepo.getNodes(pm.getIri()));
 		return pm;

+ 56 - 38
src/main/java/ua/be/wee/model/repository/NodeRespository.java

@@ -28,7 +28,7 @@ public class NodeRespository {
 	@Autowired
 	private PMRepository pmRepo;
 
-	public List<Node> getNodes(String pmIri) {
+	public List<Node> getNodes(String pmIri) throws Exception {
 		List<Node> nodes = new ArrayList<Node>();
 
 		PM pm = pmRepo.getPM(pmIri);
@@ -237,7 +237,7 @@ public class NodeRespository {
 		}
 	}
 
-	private void updateArtifactOfActivities(PM pm) {
+	private void updateArtifactOfActivities(PM pm) throws Exception {
 		for (Node node : pm.getNodes()) {
 			if (node instanceof Activity) {
 				Activity act = (Activity) node;
@@ -247,13 +247,18 @@ public class NodeRespository {
 							+ "  ?inport owl:sameAs <" + inport.getIri() + "> .\n" + "  ?inport pm:dataFrom ?art .\n"
 							+ "}";
 					ResultSet results = FusekiWrapper.getInstance().execQuery(query);
-					QuerySolution soln = results.nextSolution();
-
-					RDFNode art = soln.get("?art");
-					if (art != null && !art.toString().isEmpty()) {
-						Artifact artifact = (Artifact) pm.getNode(art.toString());
-						act.addInputArtifact(artifact);
+					if (results.hasNext()) {
+						QuerySolution soln = results.nextSolution();
+
+						RDFNode art = soln.get("?art");
+						if (art != null && !art.toString().isEmpty()) {
+							Artifact artifact = (Artifact) pm.getNode(art.toString());
+							act.addInputArtifact(artifact);
+						}
+					} else {
+						throw new Exception("Query did not return any data: \n" + query );
 					}
+					
 				}
 
 				for (DataOutputPort outport : act.getDataOutPorts()) {
@@ -262,19 +267,23 @@ public class NodeRespository {
 							+ "WHERE { \n" + "  ?outport owl:sameAs <" + outport.getIri() + "> .\n"
 							+ "  ?outport pm:dataTo ?art .\n" + "}";
 					ResultSet results = FusekiWrapper.getInstance().execQuery(query);
-					QuerySolution soln = results.nextSolution();
-
-					RDFNode art = soln.get("?art");
-					if (art != null && !art.toString().isEmpty()) {
-						Artifact artifact = (Artifact) pm.getNode(art.toString());
-						act.addOutputArtifact(artifact);
+					if (results.hasNext()) {
+						QuerySolution soln = results.nextSolution();
+	
+						RDFNode art = soln.get("?art");
+						if (art != null && !art.toString().isEmpty()) {
+							Artifact artifact = (Artifact) pm.getNode(art.toString());
+							act.addOutputArtifact(artifact);
+						}
+					} else {
+						throw new Exception("Query did not return any data: \n" + query );
 					}
 				}
 			}
 		}
 	}
 
-	private Node createNode(RDFNode iri, RDFNode type) {
+	private Node createNode(RDFNode iri, RDFNode type) throws Exception {
 		Node node = null;
 		switch (type.toString()) {
 		case Node.ACTIVITY_IRI:
@@ -299,7 +308,7 @@ public class NodeRespository {
 		return node;
 	}
 
-	private Node createArtifact(String iri) {
+	private Node createArtifact(String iri) throws Exception {
 		Artifact art = new Artifact();
 		String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"
 				+ "PREFIX pm: <http://ua.be/sdo2l/vocabulary/formalisms/pm#>\n"
@@ -307,17 +316,21 @@ public class NodeRespository {
 				+ "	?art owl:sameAs <" + iri + "> .\n" + " ?art	pm:hasName ?name .\n"
 				+ " ?art  	pm:hasType ?type .\n" + " ?type base:hasGUID ?typename .\n" + "}";
 		ResultSet results = FusekiWrapper.getInstance().execQuery(query);
-		QuerySolution soln = results.nextSolution();
-		RDFNode name = soln.get("?name");
-		RDFNode type = soln.get("?typename");
-		art.setIri(iri);
-		art.setName(name.toString());
-		art.setType(type.toString());
-
-		return art;
+		if (results.hasNext()) {
+			QuerySolution soln = results.nextSolution();
+			RDFNode name = soln.get("?name");
+			RDFNode type = soln.get("?typename");
+			art.setIri(iri);
+			art.setName(name.toString());
+			art.setType(type.toString());
+			return art;
+		} else {
+			throw new Exception("Query did not return any data: \n" + query );
+		}
+		
 	}
 
-	private Node createActivity(String iri) {
+	private Node createActivity(String iri) throws Exception {
 		Activity act = new Activity();
 		String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"
 				+ "PREFIX ftg: <http://ua.be/sdo2l/vocabulary/formalisms/ftg#>"
@@ -326,19 +339,24 @@ public class NodeRespository {
 				+ "	?act owl:sameAs <" + iri + ">;\n" + "   		pm:isTransformation ?t ;  \n"
 				+ "   		pm:hasName ?name .  \n" + " ?t base:hasGUID ?type .  \n" + "}";
 		ResultSet results = FusekiWrapper.getInstance().execQuery(query);
-		QuerySolution soln = results.nextSolution();
-		RDFNode name = soln.get("?name");
-		RDFNode type = soln.get("?type");
-		act.setIri(iri);
-		act.setName(name.toString());
-		act.setTransformationName(type.toString());
-
-		act.setCtrlInPorts(getCtrlInPorts(act));
-		act.setCtrlOutPorts(getCtrlOutPorts(act));
-		act.setDatalInPorts(getDataInPorts(act));
-		act.setDataOutPorts(getDataOutPorts(act));
-
-		return act;
+		if (results.hasNext()) {
+			QuerySolution soln = results.nextSolution();
+			RDFNode name = soln.get("?name");
+			RDFNode type = soln.get("?type");
+			act.setIri(iri);
+			act.setName(name.toString());
+			act.setTransformationName(type.toString());
+
+			act.setCtrlInPorts(getCtrlInPorts(act));
+			act.setCtrlOutPorts(getCtrlOutPorts(act));
+			act.setDatalInPorts(getDataInPorts(act));
+			act.setDataOutPorts(getDataOutPorts(act));
+
+			return act;
+
+		} else {
+			throw new Exception("Query did not return any data: \n" + query );
+		}
 	}
 
 	private List<DataOutputPort> getDataOutPorts(Activity act) {