Commit 7e36e1f2 authored by Manuel's avatar Manuel

businessLogic: refactored repositories

parent 53aba5c1
from typing import Dict
class Table:
def __init__(self, name):
self.name = name
def to_serializable_dict(self) -> Dict:
return {"name": self.name}
@staticmethod
def from_serializable_dict(data: Dict):
return Table(data["name"])
\ 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.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
......@@ -3,7 +3,6 @@ import network_constants as netconst
from database.MongoRepositoryBase import MongoRepositoryBase
from db.entities.layer_adapter import LayerAdapter
from db.entities.use_case import UseCase
from db.entities.schema import Schema
import pymongo
import json
......@@ -19,72 +18,35 @@ class Repository(MongoRepositoryBase):
self._adapter_collection = 'layer_adapters'
self._use_case_collection = 'use_cases'
self._schema_collection = 'schemas'
def add_schema(self, schema: Schema):
self.put_use_case(schema.use_case)
super().insert_entry(self._schema_collection, schema.to_serializable_dict())
def all(self) -> List[Dict]:
result = super().get_entries(self._adapter_collection, projection={'_id': False})
def all_schemas(self) -> List[Schema]:
result = super().get_entries(self._schema_collection, projection={'_id': False})
return [Schema.from_serializable_dict(row) for row in list(result)]
return [LayerAdapter.from_serializable_dict(row) for row in list(result)]
def schema_for_use_case(self, use_case: str) -> List[Dict]:
result = super().get_entries(self._schema_collection, projection={'_id': False}, selection={"use_case": use_case})
def all_for_use_case(self, use_case: str) -> List[Dict]:
result = super().get_entries(self._adapter_collection, projection={'_id': False}, selection={"use_case": use_case})
result = list(result)
if len(result) > 1:
raise ValueError("No more than 1 Schema allowed per use-case!")
return [LayerAdapter.from_serializable_dict(row) for row in list(result)]
def one(self, name : str, use_case: str) -> LayerAdapter:
result = list(super().get_entries(self._adapter_collection, selection={"name": name, "use_case": use_case}))
if len(result) == 1:
return Schema.from_serializable_dict(result[0])
return LayerAdapter.from_serializable_dict(result[0])
return None
def delete_schema_with_use_case(self, use_case: str):
collection = self._database[self._schema_collection]
collection.delete_one({"use_case": use_case})
def delete_all_schemas(self):
collection = self._database[self._schema_collection]
collection.delete_many({})
def update_schema(self, schema: Schema):
collection = self._database[self._schema_collection]
collection.update_one({"use_case": schema.use_case}, {"$set": schema.to_serializable_dict()})
def delete_all_use_cases_with_name(self, name: str):
collection = self._database[self._use_case_collection]
collection.delete_many({"name": name})
def delete_all_layers(self):
def delete_all(self):
collection = self._database[self._adapter_collection]
collection.delete_many({})
def delete_all_use_cases(self):
collection = self._database[self._use_case_collection]
collection.delete_many({})
def all_use_cases(self) -> List[UseCase]:
dicts = list(super().get_entries(self._use_case_collection, projection={'_id': False}))
return [UseCase.from_serializable_dict(d) for d in dicts]
def put_use_case(self, use_case_name: str):
use_cases = self.all_use_cases()
existing_use_cases = list(filter(lambda use_case: use_case.name == use_case_name, use_cases))
if len(existing_use_cases) == 0:
use_case = UseCase(use_case_name)
super().insert_entry(self._use_case_collection, use_case.to_serializable_dict())
def add(self, adapter : LayerAdapter):
self.put_use_case(adapter.use_case)
super().insert_entry(self._adapter_collection, adapter.to_serializable_dict())
def one_by_name_and_usecase(self, name : str, use_case: str) -> LayerAdapter:
return list(super().get_entries(self._adapter_collection, selection={"name": name, "use_case": use_case}))
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):
collection = self._database[self._adapter_collection]
......@@ -97,13 +59,3 @@ class Repository(MongoRepositoryBase):
def delete_all_with_name_and_use_case(self, name: str, use_case: str):
collection = self._database[self._adapter_collection]
collection.delete_many({"name": name, "use_case": use_case})
\ No newline at end of file
def all(self) -> List[Dict]:
result = super().get_entries(self._adapter_collection, projection={'_id': False})
return list(result)
def all_for_use_case(self, use_case: str) -> List[Dict]:
result = super().get_entries(self._adapter_collection, projection={'_id': False}, selection={"use_case": use_case})
return list(result)
\ 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
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):
self.put_use_case(schema.use_case)
super().insert_entry(self._schema_collection, schema.to_serializable_dict())
def get_for_use_case(self, use_case: str) -> Schema:
result = super().get_entries(self._schema_collection, projection={'_id': False}, selection={"use_case": use_case})
result = list(result)
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):
schema = self.get_for_use_case(use_case)
if schema == None:
schema = Schema(use_case, mappings={})
self.add(schema)
return schema
def delete_for_use_case(self, use_case: str):
collection = self._database[self._schema_collection]
collection.delete_one({"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.use_case import UseCase
import pymongo
import json
from typing import List, Dict
class UseCaseRepository(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._use_case_collection = 'use_cases'
def all(self) -> List[UseCase]:
dicts = list(super().get_entries(self._use_case_collection, projection={'_id': False}))
return [UseCase.from_serializable_dict(d) for d in dicts]
def delete_all(self):
collection = self._database[self._use_case_collection]
collection.delete_many({})
def delete_all_with_name(self, name: str):
collection = self._database[self._use_case_collection]
collection.delete_many({"name": name})
def put(self, use_case_name: str):
use_cases = self.all_use_cases()
existing_use_cases = list(filter(lambda use_case: use_case.name == use_case_name, use_cases))
if len(existing_use_cases) == 0:
use_case = UseCase(use_case_name)
super().insert_entry(self._use_case_collection, use_case.to_serializable_dict())
#global imports
from db.entities.layer_adapter import LayerAdapter
from db.schema_repository import SchemaRepository
from services.layer_adapter_service import LayerAdapterService
import json
from flask import Response, request
schema_repository = SchemaRepository()
def all():
return [schema.to_serializable_dict() for schema in schema_repository.all()]
def get_for_use_case(use_case:str):
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 = schema_repository.get_for_use_case(use_case)
if schema == None:
return Response(status=404, response=f"Schema {use_case} does not exist")
schema_repository.delete_for_use_case(use_case)
return Response(status=200)
def add_mapping(use_case:str):
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
from db.entities.layer_adapter import LayerAdapter
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()
def all():
return [adapter.to_serializable_dict() for adapter in use_case_repository.all()]
def delete_all():
LayerAdapterService.delete_all()
return Response(status=200)
\ No newline at end of file
#global imports
from db.repository import Repository
from db.schema_repository import SchemaRepository
from db.use_case_repository import UseCaseRepository
from db.entities.layer_adapter import LayerAdapter
from db.entities.schema import Schema
from typing import List
class LayerAdapterService:
_repository = Repository()
@staticmethod
def all_schemas() -> List[Schema]:
return LayerAdapterService._repository.all_schemas()
@staticmethod
def schema_for_use_case(use_case: str):
LayerAdapterService._repository.put_use_case(use_case)
return LayerAdapterService._repository.schema_for_use_case(use_case)
@staticmethod
def put_schema(use_case: str):
schema = LayerAdapterService.schema_for_use_case(use_case)
if schema == None:
schema = Schema(use_case, mappings={})
LayerAdapterService._repository.add_schema(schema)
return schema
_schema_repository = SchemaRepository()
_layer_repository = Repository()
_use_case_repository = UseCaseRepository()
@staticmethod
def check_layer(layer: LayerAdapter):
'''
checks if the given layer has correct mappings regarding the schema of the use_case
'''
schema = LayerAdapterService.put_schema(layer.use_case)
schema = LayerAdapterService._schema_repository.put(layer.use_case)
for p in layer.properties:
if p not in schema.mappings:
raise ValueError(f'{p} is not existent in the schema!')
@staticmethod
def delete_schema(use_case:str):
LayerAdapterService._repository.delete_schema_with_use_case(use_case)
@staticmethod
def update_schema(schema: Schema):
LayerAdapterService._repository.update_schema(schema)
@staticmethod
def all_use_cases() -> List[str]:
return LayerAdapterService._repository.all_use_cases()
@staticmethod
def delete_all_use_cases():
LayerAdapterService._repository.delete_all_use_cases()
LayerAdapterService._repository.delete_all_schemas()
LayerAdapterService._repository.delete_all_layers()
@staticmethod
def delete_use_case(name:str):
LayerAdapterService._repository.delete_all_use_cases_with_name(name)
@staticmethod
def all() -> List[LayerAdapter]:
'''
Return all currently defined layers
'''
return LayerAdapterService._repository.all()
@staticmethod
def all_for_use_case(use_case: str) -> List[LayerAdapter]:
'''
Return all currently defined layers that belong to the
given use-case
'''
return LayerAdapterService._repository.all_for_use_case(use_case)
@staticmethod
def update(layer: LayerAdapter):
'''
Overwrite the stored instance with the given one which is
identified by the layer name
@params:
layer - Required : layer object holding the current data
'''
LayerAdapterService._repository.update(layer)
@staticmethod
def update_use_case(layer: LayerAdapter, use_case:str):
'''
Overwrite the stored instance with the given one which is
identified by the layer name
@params:
layer - Required : layer object holding the current data
'''
LayerAdapterService._repository.update_use_case(layer, use_case)
@staticmethod
def add(name: str, use_case: str):
'''
Add a new layer to the DB. Attribute mapping and cluster
attributes will be empty per default
@params:
name - Required : Unique name for a layer.
use_case - Required : String-identifier for the use-case
'''
adapter_new = LayerAdapter(name, use_case, [], [])
LayerAdapterService._repository.add(adapter_new)
@staticmethod
def add_complete(layer: LayerAdapter):
'''
Add a new layer to the DB. Attribute mappings and cluster
attributes of the given layer are used
Add a new layer to the DB. Attribute mappings and cluster attributes of the given layer
are used. Before inserting, the layer gets checked for consistency with the schema.
@params:
layer - Required : layer object holding correct data
'''
LayerAdapterService._repository.add(layer)
@staticmethod
def delete(layer: LayerAdapter):
'''
delete all layers with the given name.
@params:
layer - Required : layer object to remove from the DB
'''
use_case = layer.use_case
LayerAdapterService._repository.delete_all_with_name_and_use_case(layer.name, use_case)
LayerAdapterService.check_layer(layer)
LayerAdapterService._layer_repository.add(layer)
@staticmethod
def delete_all_layers():
LayerAdapterService._repository.delete_all_layers()
@staticmethod
def one(name: str, use_case: str) -> LayerAdapter:
'''
Retrieve a single layer from the DB. Returns None if no layer
was found under this name.
@params:
name - Required : Unique name for a layer
use_case - Required : String-identifier for the use-case
'''
result = LayerAdapterService._repository.one_by_name_and_usecase(name, use_case)
def delete_all_use_cases():
# TODO
LayerAdapterService._layer_repository.delete_all_use_cases()
LayerAdapterService._schema_repository.delete_all()
LayerAdapterService._use_case_repository.delete_all()
\ No newline at end of file
if len(result) == 1:
return LayerAdapter.from_serializable_dict(result[0])
else:
return None
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