Commit fd78b68b authored by Manuel's avatar Manuel

[BusinessLogic] expanded 'Schema' to 'Table'

parent bc4462e8
...@@ -7,7 +7,7 @@ class LayerAdapter: ...@@ -7,7 +7,7 @@ class LayerAdapter:
attributes from the dataset correspond to each one attributes from the dataset correspond to each one
''' '''
def __init__(self, name: str, use_case: str, properties: List[str], cluster_properties: List[str]): def __init__(self, name: str, use_case: str, table: str, properties: List[str], cluster_properties: List[str]):
''' '''
Creates a new instance of LayerAdapter Creates a new instance of LayerAdapter
...@@ -20,6 +20,7 @@ class LayerAdapter: ...@@ -20,6 +20,7 @@ class LayerAdapter:
self.name = name self.name = name
self.properties = properties self.properties = properties
self.use_case = use_case self.use_case = use_case
self.table = table
for prop in cluster_properties: for prop in cluster_properties:
if prop not in properties: if prop not in properties:
...@@ -86,7 +87,8 @@ class LayerAdapter: ...@@ -86,7 +87,8 @@ class LayerAdapter:
"name": self.name, "name": self.name,
"properties": self.properties, "properties": self.properties,
"cluster_properties": self.cluster_properties, "cluster_properties": self.cluster_properties,
"use_case": self.use_case "use_case": self.use_case,
"table": self.table
} }
@staticmethod @staticmethod
...@@ -101,6 +103,7 @@ class LayerAdapter: ...@@ -101,6 +103,7 @@ class LayerAdapter:
return LayerAdapter( return LayerAdapter(
user_dict["name"], user_dict["name"],
user_dict["use_case"], user_dict["use_case"],
user_dict["table"],
user_dict["properties"], user_dict["properties"],
user_dict["cluster_properties"] user_dict["cluster_properties"]
) )
from typing import Dict
class Schema:
def __init__(self, use_case:str, mappings:Dict[str, str]):
self.use_case = use_case
self.mappings = mappings
def add_mapping(self, internal:str, external:str):
if internal not in self.mappings.keys():
self.mappings[internal] = external
def to_serializable_dict(self) -> Dict:
return {
"use_case": self.use_case,
"mappings": self.mappings
}
@staticmethod
def from_serializable_dict(serializable_dict: Dict):
return Schema(
serializable_dict["use_case"],
serializable_dict["mappings"]
)
\ No newline at end of file
from typing import Dict from typing import Dict
class Table: class Table:
def __init__(self, name): def __init__(self, use_case: str, name: str, mappings: Dict[str, str]):
self.use_case = use_case
self.name = name self.name = name
self.mappings = mappings
def to_serializable_dict(self) -> Dict: def to_serializable_dict(self) -> Dict:
return {"name": self.name} return {
"name": self.name,
"use_case": self.use_case,
"mappings": self.mappings
}
def add_mapping(self, internal:str, external:str):
if internal not in self.mappings.keys():
self.mappings[internal] = external
@staticmethod @staticmethod
def from_serializable_dict(data: Dict): def from_serializable_dict(data: Dict):
return Table(data["name"]) return Table(
\ No newline at end of file data["use_case"],
data["name"],
data["mappings"]
)
# global imports (dont't worry, red is normal)
import network_constants as netconst
from database.MongoRepositoryBase import MongoRepositoryBase
from db.entities.table import Table
import pymongo
import json
from typing import List, Dict
class TableRepository(MongoRepositoryBase):
'''This is a repository for MongoDb.'''
def __init__(self):
super().__init__(netconst.BUSINESS_LOGIC_DB_HOSTNAME,
netconst.BUSINESS_LOGIC_DB_PORT,
'rest-gateway-db')
self._table_collection = 'tables'
def add_one(self, table: Table):
super().insert_entry(self._table_collection, table.to_serializable_dict())
\ No newline at end of file
...@@ -14,7 +14,7 @@ class Repository(MongoRepositoryBase): ...@@ -14,7 +14,7 @@ class Repository(MongoRepositoryBase):
def __init__(self): def __init__(self):
super().__init__(netconst.BUSINESS_LOGIC_DB_HOSTNAME, super().__init__(netconst.BUSINESS_LOGIC_DB_HOSTNAME,
netconst.BUSINESS_LOGIC_DB_PORT, netconst.BUSINESS_LOGIC_DB_PORT,
'rest-gateway-db') 'business-logic-db')
self._adapter_collection = 'layer_adapters' self._adapter_collection = 'layer_adapters'
self._use_case_collection = 'use_cases' self._use_case_collection = 'use_cases'
...@@ -29,8 +29,8 @@ class Repository(MongoRepositoryBase): ...@@ -29,8 +29,8 @@ class Repository(MongoRepositoryBase):
return [LayerAdapter.from_serializable_dict(row) for row in list(result)] return [LayerAdapter.from_serializable_dict(row) for row in list(result)]
def one(self, name : str, use_case: str) -> LayerAdapter: def one(self, name : str, use_case: str, table: str) -> LayerAdapter:
result = list(super().get_entries(self._adapter_collection, selection={"name": name, "use_case": use_case})) result = list(super().get_entries(self._adapter_collection, selection={"name": name, "use_case": use_case, "table": table}))
if len(result) == 1: if len(result) == 1:
return LayerAdapter.from_serializable_dict(result[0]) return LayerAdapter.from_serializable_dict(result[0])
...@@ -44,18 +44,14 @@ class Repository(MongoRepositoryBase): ...@@ -44,18 +44,14 @@ class Repository(MongoRepositoryBase):
def add(self, adapter : LayerAdapter): def add(self, adapter : LayerAdapter):
super().insert_entry(self._adapter_collection, adapter.to_serializable_dict()) super().insert_entry(self._adapter_collection, adapter.to_serializable_dict())
def add_empty(self, name: str, use_case: str):
adapter = LayerAdapter(name, use_case, [], [])
super().insert_entry(self._adapter_collection, adapter.to_serializable_dict())
def update_use_case(self, adapter : LayerAdapter, use_case: str): def update_use_case(self, adapter : LayerAdapter, use_case: str):
collection = self._database[self._adapter_collection] collection = self._database[self._adapter_collection]
collection.update_one({"name":adapter.name, "use_case": use_case}, {"$set": adapter.to_serializable_dict()}) collection.update_one({"name":adapter.name, "use_case": use_case, "table": adapter.table}, {"$set": adapter.to_serializable_dict()})
def update(self, adapter : LayerAdapter): def update(self, adapter : LayerAdapter):
collection = self._database[self._adapter_collection] collection = self._database[self._adapter_collection]
collection.update_one({"name":adapter.name, "use_case": adapter.use_case}, {"$set": adapter.to_serializable_dict()}) collection.update_one({"name":adapter.name, "use_case": adapter.use_case, "table": adapter.table}, {"$set": adapter.to_serializable_dict()})
def delete_all_with_name_and_use_case(self, name: str, use_case: str): def delete(self, adapter : LayerAdapter):
collection = self._database[self._adapter_collection] collection = self._database[self._adapter_collection]
collection.delete_many({"name": name, "use_case": use_case}) collection.delete_many({"name": adapter.name, "use_case": adapter.use_case, "table": adapter.table})
\ No newline at end of file \ No newline at end of file
# global imports (dont't worry, red is normal)
import network_constants as netconst
from database.MongoRepositoryBase import MongoRepositoryBase
from db.entities.schema import Schema
from db.use_case_repository import UseCaseRepository
import pymongo
import json
from typing import List, Dict
class SchemaRepository(MongoRepositoryBase):
'''This is a repository for MongoDb.'''
def __init__(self):
super().__init__(netconst.BUSINESS_LOGIC_DB_HOSTNAME,
netconst.BUSINESS_LOGIC_DB_PORT,
'rest-gateway-db')
self._schema_collection = 'schemas'
def all(self) -> List[Schema]:
result = super().get_entries(self._schema_collection, projection={'_id': False})
return [Schema.from_serializable_dict(row) for row in list(result)]
def add(self, schema: Schema):
super().insert_entry(self._schema_collection, schema.to_serializable_dict())
def get_for_use_case(self, use_case: str) -> Schema:
self.put(use_case)
result = list(super().get_entries(self._schema_collection, projection={'_id': False}, selection={"use_case": use_case}))
if len(result) > 1:
raise ValueError("No more than 1 Schema allowed per use-case!")
if len(result) == 1:
return Schema.from_serializable_dict(result[0])
return None
def put(self, use_case: str):
result = list(super().get_entries(self._schema_collection, projection={'_id': False}, selection={"use_case": use_case}))
if len(result) == 0:
schema = Schema(use_case, mappings={})
self.add(schema)
else:
schema = Schema.from_serializable_dict(result[0])
return schema
def delete_for_use_case(self, use_case: str):
collection = self._database[self._schema_collection]
collection.delete_many({"use_case": use_case})
def delete_all(self):
collection = self._database[self._schema_collection]
collection.delete_many({})
def update(self, schema: Schema):
collection = self._database[self._schema_collection]
collection.update_one({"use_case": schema.use_case}, {"$set": schema.to_serializable_dict()})
# global imports (dont't worry, red is normal)
import network_constants as netconst
from database.MongoRepositoryBase import MongoRepositoryBase
from db.entities.table import Table
from typing import Dict, List
class TableRepository(MongoRepositoryBase):
def __init__(self):
super().__init__(netconst.BUSINESS_LOGIC_DB_HOSTNAME,
netconst.BUSINESS_LOGIC_DB_PORT,
'business-logic-db')
self._collection = 'tables'
def get_all(self) -> List[Table]:
result = super().get_entries(self._collection, projection={'_id': False})
return [Table.from_serializable_dict(row) for row in list(result)]
def get_all_for_use_case(self, use_case:str) -> List[Table]:
result = super().get_entries(self._collection, selection={'use_case': use_case}, projection={'_id': False})
return [Table.from_serializable_dict(row) for row in result]
def get_for_use_case_and_name(self, use_case:str, name:str) -> Table:
result = list(super().get_entries(self._collection, selection={"use_case":use_case, "name":name}, projection={'_id': False}))
if len(result) == 1:
return Table.from_serializable_dict(result[0])
if len(result) == 0:
return None
raise ValueError("No more than 1 Schema allowed per use-case!")
def add(self, table:Table):
super().insert_entry(self._collection, table.to_serializable_dict())
def delete_for_use_case(self, use_case: str):
collection = self._database[self._collection]
collection.delete_many({"use_case": use_case})
def delete_all(self):
collection = self._database[self._collection]
collection.delete_many({})
def update(self, table: Table):
collection = self._database[self._collection]
collection.update_one({"use_case": table.use_case, "name": table.name}, {"$set": table.to_serializable_dict()})
\ No newline at end of file
...@@ -13,7 +13,7 @@ class UseCaseRepository(MongoRepositoryBase): ...@@ -13,7 +13,7 @@ class UseCaseRepository(MongoRepositoryBase):
def __init__(self): def __init__(self):
super().__init__(netconst.BUSINESS_LOGIC_DB_HOSTNAME, super().__init__(netconst.BUSINESS_LOGIC_DB_HOSTNAME,
netconst.BUSINESS_LOGIC_DB_PORT, netconst.BUSINESS_LOGIC_DB_PORT,
'rest-gateway-db') 'business-logic-db')
self._use_case_collection = 'use_cases' self._use_case_collection = 'use_cases'
...@@ -30,6 +30,17 @@ class UseCaseRepository(MongoRepositoryBase): ...@@ -30,6 +30,17 @@ class UseCaseRepository(MongoRepositoryBase):
collection = self._database[self._use_case_collection] collection = self._database[self._use_case_collection]
collection.delete_many({"name": name}) collection.delete_many({"name": name})
def get_by_name(self, name: str):
result = list(super().get_entries(self._use_case_collection, {"name":name}))
if len(result) == 1:
return UseCase.from_serializable_dict(result[0])
if len(result) == 0:
return None
raise ValueError("More than one Use-Case in the DB!")
def put(self, use_case_name: str): def put(self, use_case_name: str):
use_cases = self.all() use_cases = self.all()
...@@ -38,4 +49,3 @@ class UseCaseRepository(MongoRepositoryBase): ...@@ -38,4 +49,3 @@ class UseCaseRepository(MongoRepositoryBase):
if len(existing_use_cases) == 0: if len(existing_use_cases) == 0:
use_case = UseCase(use_case_name) use_case = UseCase(use_case_name)
super().insert_entry(self._use_case_collection, use_case.to_serializable_dict()) super().insert_entry(self._use_case_collection, use_case.to_serializable_dict())
\ No newline at end of file
#global imports
from db.entities.schema import Schema
from db.entities.layer_adapter import LayerAdapter
from db.schema_repository import SchemaRepository
from db.use_case_repository import UseCaseRepository
from services.layer_adapter_service import LayerAdapterService
import json
from flask import Response, request
use_case_repository = UseCaseRepository()
schema_repository = SchemaRepository()
def all():
return [schema.to_serializable_dict() for schema in schema_repository.all()]
def add_complete(use_case: str):
mappings = request.json
reference = schema_repository.get_for_use_case(use_case)
if reference != None and len(reference.mappings) > 0:
return Response(status=400, response="Schema already exists.")
schema_repository.delete_for_use_case(use_case)
schema_dict = {
"use_case": use_case,
"mappings": mappings
}
schema_repository.add(Schema.from_serializable_dict(schema_dict))
return Response(status=200)
def get_for_use_case(use_case:str):
use_case_repository.put(use_case)
schema = schema_repository.get_for_use_case(use_case)
if schema != None:
return Response(status=200, response=json.dumps(schema.to_serializable_dict()))
return Response(status=404, response=f"Schema {use_case} does not exist")
def delete_for_use_case(use_case:str):
schema_repository.delete_for_use_case(use_case)
return Response(status=200)
def add_mapping(use_case:str):
use_case_repository.put(use_case)
data = request.json
if "internal" not in data or "external" not in data:
return Response(status=400, response=f"Field missing! Fields required: (internal, external)")
schema = schema_repository.get_for_use_case(use_case)
if schema == None:
print("schema not there, creating it...")
schema = schema_repository.put(use_case)
schema.add_mapping(data["internal"], data["external"])
schema_repository.update(schema)
return Response(status=200)
\ No newline at end of file
#global imports
from db.table_repository import TableRepository
from db.use_case_repository import UseCaseRepository
from db.entities.table import Table
from flask import Response, request
table_repository = TableRepository()
use_case_repository = UseCaseRepository()
def all():
return [t.to_serializable_dict() for t in table_repository.get_all()]
def all_for_use_case(use_case:str):
return [t.to_serializable_dict() for t in table_repository.get_all_for_use_case(use_case)]
def put_mapping(use_case:str, name:str):
body = request.json
if "internal" not in body or "external" not in body:
return Response(status=400, response=f"Field missing! Fields required: (internal, external)")
table = table_repository.get_for_use_case_and_name(use_case, name)
if table == None:
print("table not there, creating it...")
table = table_repository.add(Table(use_case, name, {}))
table.add_mapping(body["internal"], body["external"])
table_repository.update(table)
return Response(status=200)
def add_complete(use_case: str):
body = request.json
body["use_case"] = use_case
# check if fields are present
if "name" not in body or "mappings" not in body:
return Response(status=400, response="Field missing! Fields required: (name, mappings)")
# check if table exists
table_reference = table_repository.get_for_use_case_and_name(body["use_case"], body["name"])
if table_reference != None:
return Response(status=400, response="Table already exists!")
use_case_repository.put(body["use_case"])
table_new = Table.from_serializable_dict(body)
table_repository.add(table_new)
return Response(status=200)
def delete_all_for_use_case(use_case: str):
table_repository.delete_for_use_case(use_case)
return Response(status=200)
def delete_all():
table_repository.delete_all()
return Response(status=200)
\ No newline at end of file
# global imports
from db.entities.layer_adapter import LayerAdapter from db.entities.layer_adapter import LayerAdapter
from db.use_case_repository import UseCaseRepository from db.use_case_repository import UseCaseRepository
from db.table_repository import TableRepository
from db.repository import Repository from db.repository import Repository
from db.schema_repository import SchemaRepository
from services.layer_adapter_service import LayerAdapterService from services.layer_adapter_service import LayerAdapterService
import json import json
from flask import Response, request from flask import Response, request
use_case_repository = UseCaseRepository() use_case_repository = UseCaseRepository()
schema_repository = SchemaRepository() table_repository = TableRepository()
repository = Repository() repository = Repository()
def all(): def all():
...@@ -20,5 +18,21 @@ def all(): ...@@ -20,5 +18,21 @@ def all():
def delete_all(): def delete_all():
use_case_repository.delete_all() use_case_repository.delete_all()
repository.delete_all() repository.delete_all()
schema_repository.delete_all() table_repository.delete_all()
return Response(status=200)
def add():
body = request.json
if "name" not in body.keys():
return Response(status=400, response="Field missing! Fields required: (name)")
name = body["name"]
# check if use-case exists
reference = use_case_repository.get_by_name(name)
if not reference == None:
return Response(status=400, response="Use-Case already exists!")
use_case_repository.put(name)
return Response(status=200) return Response(status=200)
#global imports #global imports
from db.repository import Repository from db.repository import Repository
from db.schema_repository import SchemaRepository from db.table_repository import TableRepository
from db.use_case_repository import UseCaseRepository from db.use_case_repository import UseCaseRepository
from db.entities.layer_adapter import LayerAdapter from db.entities.layer_adapter import LayerAdapter
from db.entities.schema import Schema from db.entities.table import Table
from typing import List from typing import List
class LayerAdapterService: class LayerAdapterService:
_schema_repository = SchemaRepository() _table_repository = TableRepository()
_layer_repository = Repository() _layer_repository = Repository()
_use_case_repository = UseCaseRepository() _use_case_repository = UseCaseRepository()
...@@ -18,10 +18,11 @@ class LayerAdapterService: ...@@ -18,10 +18,11 @@ class LayerAdapterService:
''' '''
checks if the given layer has correct mappings regarding the schema of the use_case checks if the given layer has correct mappings regarding the schema of the use_case
''' '''
schema = LayerAdapterService._schema_repository.put(layer.use_case) # TODO implement with tables
for p in layer.properties: # schema = LayerAdapterService._schema_repository.put(layer.use_case)
if p not in schema.mappings: # for p in layer.properties:
raise ValueError(f'{p} is not existent in the schema!') # if p not in schema.mappings:
# raise ValueError(f'{p} is not existent in the schema!')
@staticmethod @staticmethod
def add_complete(layer: LayerAdapter): def add_complete(layer: LayerAdapter):
...@@ -39,6 +40,6 @@ class LayerAdapterService: ...@@ -39,6 +40,6 @@ class LayerAdapterService:
def delete_all_use_cases(): def delete_all_use_cases():
# TODO # TODO
LayerAdapterService._layer_repository.delete_all_use_cases() LayerAdapterService._layer_repository.delete_all_use_cases()
LayerAdapterService._schema_repository.delete_all() LayerAdapterService._table_repository.delete_all()
LayerAdapterService._use_case_repository.delete_all() LayerAdapterService._use_case_repository.delete_all()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment