From d42c66d95cafc4025dd1f9578e23aeb71d324d59 Mon Sep 17 00:00:00 2001
From: Cornelia Michlits <cornelia.michlits@tuwien.ac.at>
Date: Sun, 5 Dec 2021 19:37:10 +0100
Subject: [PATCH] 104 ontology - add methode get_uri by unit name

---
 fda-units-service/app.py            | 18 +++++++++++++++---
 fda-units-service/list.py           |  6 +++---
 fda-units-service/us-yml/geturi.yml | 22 ++++++++++++++++++++++
 fda-units-service/validate.py       |  8 +++++++-
 4 files changed, 47 insertions(+), 7 deletions(-)
 create mode 100644 fda-units-service/us-yml/geturi.yml

diff --git a/fda-units-service/app.py b/fda-units-service/app.py
index 423481f204..36f94de0c5 100644
--- a/fda-units-service/app.py
+++ b/fda-units-service/app.py
@@ -7,7 +7,7 @@ from flasgger import Swagger
 from flasgger.utils import swag_from
 from flasgger import LazyString, LazyJSONEncoder
 from list import list_units, get_uri
-from validate import validator
+from validate import validator, stringmapper
 
 app = Flask(__name__)
 app.config["SWAGGER"] = {"title": "FDA-Units-Service", "uiversion": 3}
@@ -50,11 +50,23 @@ def suggest():
 
 @app.route('/api/units/validate', methods=["POST"], endpoint='validate')
 @swag_from('validate.yml')
-def suggest():
+def valitate():
     input_json = request.get_json()
     try:
         unit = str(input_json['ustring'])
-        res = validator(unit)
+        res = validator(stringmapper(unit))
+    except Exception as e:
+        print(e)
+        res = {"success": False, "message": "Unknown error"+str(e)+unit}
+    return jsonify(res)
+
+@app.route('/api/units/geturi', methods=["POST"], endpoint='geturi')
+@swag_from('geturi.yml')
+def geturi():
+    input_json = request.get_json()
+    try:
+        name = str(input_json['uname'])
+        res = get_uri(name)
     except Exception as e:
         print(e)
         res = {"success": False, "message": "Unknown error"+str(e)+unit}
diff --git a/fda-units-service/list.py b/fda-units-service/list.py
index 889da99214..cbfaaf6c46 100644
--- a/fda-units-service/list.py
+++ b/fda-units-service/list.py
@@ -36,12 +36,12 @@ def list_units(string,offset=0):
         return None
 
 def get_uri(name):
-    if bool(re.match('^[a-zA-Z0-9]+$',name)):
+    if bool(re.match('^[a-zA-Z0-9\\s]+$',name)):
         uri_query = """
-        SELECT ?uri
+        SELECT ?uri ?o
         WHERE {
             ?uri <http://www.w3.org/2000/01/rdf-schema#label> ?o .
-            FILTER (str(?o)=\""""+name+"""\")
+            FILTER regex(str(?o),\"^"""+name+"""$\","i")
             } LIMIT 1
         """
         qres = g.query(uri_query)
diff --git a/fda-units-service/us-yml/geturi.yml b/fda-units-service/us-yml/geturi.yml
new file mode 100644
index 0000000000..cefbb9c04a
--- /dev/null
+++ b/fda-units-service/us-yml/geturi.yml
@@ -0,0 +1,22 @@
+summary: "Get URI of units"
+description: "This is a simple API for getting the URI of a certain unit in OM."
+consumes:
+- "application/json"
+produces:
+- "application/json"
+parameters:
+- in: "body"
+  name: "body"
+  description: "to-do description"
+  required: true
+  schema:
+    type: "object"
+    properties:
+      uname:
+        type: "string"
+        example : "metre"
+responses:
+  200: 
+    description: "OK"
+  405:
+    description: "Invalid input"
\ No newline at end of file
diff --git a/fda-units-service/validate.py b/fda-units-service/validate.py
index 6c57624703..df9010f5de 100644
--- a/fda-units-service/validate.py
+++ b/fda-units-service/validate.py
@@ -19,4 +19,10 @@ def validator(value):
     if next(g.triples((t_uri,None,om.Unit)), _exhausted) is _exhausted and next(g.triples((t_uri,None,om.PrefixedUnit)),_exhausted) is _exhausted and next(g.triples((t_uri,None,om.UnitDivision)),_exhausted) is _exhausted:
         return {"valid": False}
     else:
-        return {"valid": True}
\ No newline at end of file
+        return {"valid": True}
+
+def stringmapper(thisstring):
+    if ' ' in thisstring: 
+        return thisstring.split(" ",1)[0]+thisstring.split(" ",1)[1].title().replace(" ","")
+    else: 
+        return thisstring
\ No newline at end of file
-- 
GitLab