From a046d06ba960a8dd57d1a05948e8e7db331bf371 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 8 Feb 2018 12:23:04 +0100
Subject: [PATCH] rest api for sbml functions

---
 .../model/map/kinetics/SbmlFunction.java      |  4 ++
 rest-api/jsbml.log                            |  1 +
 .../models/functions/FunctionsController.java | 45 ++++++++++++++
 .../models/functions/FunctionsRestImpl.java   | 60 +++++++++++++++++++
 .../resources/applicationContext-rest.xml     |  1 +
 5 files changed, 111 insertions(+)
 create mode 100644 rest-api/jsbml.log
 create mode 100644 rest-api/src/main/java/lcsb/mapviewer/api/projects/models/functions/FunctionsController.java
 create mode 100644 rest-api/src/main/java/lcsb/mapviewer/api/projects/models/functions/FunctionsRestImpl.java

diff --git a/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlFunction.java b/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlFunction.java
index c3e23b4573..1d94313a65 100644
--- a/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlFunction.java
+++ b/model/src/main/java/lcsb/mapviewer/model/map/kinetics/SbmlFunction.java
@@ -146,4 +146,8 @@ public class SbmlFunction implements Serializable, SbmlArgument {
     this.model = model;
   }
 
+  public int getId() {
+    return id;
+  }
+
 }
diff --git a/rest-api/jsbml.log b/rest-api/jsbml.log
new file mode 100644
index 0000000000..66a05c7b2e
--- /dev/null
+++ b/rest-api/jsbml.log
@@ -0,0 +1 @@
+2018-02-08 12:13:41,962 INFO o.r.Reflections [main] Reflections took 57 ms to scan 1 urls, producing 34 keys and 433 values 
diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/functions/FunctionsController.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/functions/FunctionsController.java
new file mode 100644
index 0000000000..5dc88ab8c5
--- /dev/null
+++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/functions/FunctionsController.java
@@ -0,0 +1,45 @@
+package lcsb.mapviewer.api.projects.models.functions;
+
+import java.util.List;
+import java.util.Map;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.CookieValue;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+import lcsb.mapviewer.api.BaseController;
+import lcsb.mapviewer.api.QueryException;
+import lcsb.mapviewer.common.Configuration;
+import lcsb.mapviewer.services.SecurityException;
+
+@RestController
+public class FunctionsController extends BaseController {
+  @Autowired
+  private FunctionsRestImpl functionController;
+
+  @RequestMapping(value = "/projects/{projectId}/models/{modelId}/functions/{functionId}", method = {
+      RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
+  public Map<String, Object> getFunction(//
+      @PathVariable(value = "projectId") String projectId, //
+      @PathVariable(value = "modelId") String modelId, //
+      @PathVariable(value = "functionId") String functionId, //
+      @CookieValue(value = Configuration.AUTH_TOKEN) String token //
+  ) throws QueryException, SecurityException {
+    return functionController.getFunction(projectId, modelId, token, functionId);
+  }
+
+  @RequestMapping(value = "/projects/{projectId}/models/{modelId}/functions/", method = {
+      RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
+  public List<Map<String, Object>> getFunctions(//
+      @PathVariable(value = "projectId") String projectId, //
+      @PathVariable(value = "modelId") String modelId, //
+      @CookieValue(value = Configuration.AUTH_TOKEN) String token //
+  ) throws QueryException, SecurityException {
+    return functionController.getFunctions(projectId, modelId, token);
+  }
+
+}
\ No newline at end of file
diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/functions/FunctionsRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/functions/FunctionsRestImpl.java
new file mode 100644
index 0000000000..94dd0f42d3
--- /dev/null
+++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/functions/FunctionsRestImpl.java
@@ -0,0 +1,60 @@
+package lcsb.mapviewer.api.projects.models.functions;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+import org.springframework.transaction.annotation.Transactional;
+
+import lcsb.mapviewer.api.BaseRestImpl;
+import lcsb.mapviewer.api.ObjectNotFoundException;
+import lcsb.mapviewer.api.QueryException;
+import lcsb.mapviewer.model.map.kinetics.SbmlFunction;
+import lcsb.mapviewer.model.map.model.Model;
+import lcsb.mapviewer.services.SecurityException;
+
+@Transactional(value = "txManager")
+public class FunctionsRestImpl extends BaseRestImpl {
+
+  /**
+   * Default class logger.
+   */
+  private Logger logger = Logger.getLogger(FunctionsRestImpl.class);
+
+  public Map<String, Object> getFunction(String projectId, String modelId, String token, String functionId)
+      throws SecurityException, QueryException {
+    List<Model> models = getModels(projectId, modelId, token);
+    int id = Integer.valueOf(functionId);
+    for (Model model : models) {
+      for (SbmlFunction function : model.getFunctions()) {
+        if (function.getId() == id) {
+          return functionToMap(function);
+        }
+      }
+    }
+    throw new ObjectNotFoundException("Function with given id doesn't exist");
+  }
+
+  private Map<String, Object> functionToMap(SbmlFunction function) {
+    Map<String, Object> result = new HashMap<>();
+    result.put("id", function.getId());
+    result.put("functionId", function.getFunctionId());
+    result.put("name", function.getName());
+    result.put("definition", function.getDefinition());
+    result.put("arguments", function.getArguments());
+    return result;
+  }
+
+  public List<Map<String, Object>> getFunctions(String projectId, String modelId, String token) throws SecurityException {
+    List<Map<String, Object>> result = new ArrayList<>();
+    List<Model> models = getModels(projectId, modelId, token);
+    for (Model model : models) {
+      for (SbmlFunction function : model.getFunctions()) {
+        result.add(functionToMap(function));
+      }
+    }
+    return result;
+  }
+}
diff --git a/rest-api/src/main/resources/applicationContext-rest.xml b/rest-api/src/main/resources/applicationContext-rest.xml
index 6fc1903b43..460411f87d 100644
--- a/rest-api/src/main/resources/applicationContext-rest.xml
+++ b/rest-api/src/main/resources/applicationContext-rest.xml
@@ -21,6 +21,7 @@
 	<bean id="BioEntitiesRestImpl" class="lcsb.mapviewer.api.projects.models.bioEntities.BioEntitiesRestImpl"/>
 	<bean id="ChemicalRestImpl" class="lcsb.mapviewer.api.projects.chemicals.ChemicalRestImpl"/>
 	<bean id="ElementsRestImpl" class="lcsb.mapviewer.api.projects.models.bioEntities.elements.ElementsRestImpl"/>
+	<bean id="FunctionsRestImpl" class="lcsb.mapviewer.api.projects.models.functions.FunctionsRestImpl"/>
 	<bean id="DrugRestImpl" class="lcsb.mapviewer.api.projects.drugs.DrugRestImpl"/>
 	<bean id="MiRnaRestImpl" class="lcsb.mapviewer.api.projects.mirnas.MiRnaRestImpl"/>
 	<bean id="OverlayRestImpl" class="lcsb.mapviewer.api.projects.overlays.OverlayRestImpl"/>
-- 
GitLab