Skip to content

Commit

Permalink
added mapping facades
Browse files Browse the repository at this point in the history
  • Loading branch information
ensaremirerol committed Dec 13, 2024
1 parent 1848f97 commit b36359f
Show file tree
Hide file tree
Showing 10 changed files with 474 additions and 1 deletion.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from uuid import uuid4

from kink import inject

from server.facades import (
BaseFacade,
FacadeResponse,
)
from server.models.mapping import MappingGraph
from server.models.source import SourceType
from server.service_protocols.mapping_service_protocol import (
MappingServiceProtocol,
)
from server.services.core.workspace_metadata_service import (
WorkspaceMetadataServiceProtocol,
)
from server.services.local.local_source_service import (
SourceServiceProtocol,
)
from server.services.local.local_workspace_service import (
WorkspaceServiceProtocol,
)


@inject
class CreateMappingInWorkspaceFacade(BaseFacade):
def __init__(
self,
workspace_metadata_service: WorkspaceMetadataServiceProtocol,
workspace_service: WorkspaceServiceProtocol,
mapping_service: MappingServiceProtocol,
source_service: SourceServiceProtocol,
):
super().__init__()
self.workspace_metadata_service: WorkspaceMetadataServiceProtocol = workspace_metadata_service
self.workspace_service: WorkspaceServiceProtocol = (
workspace_service
)
self.mapping_service: MappingServiceProtocol = (
mapping_service
)
self.source_service: SourceServiceProtocol = (
source_service
)

@BaseFacade.error_wrapper
def execute(
self,
workspace_id: str,
name: str,
description: str,
source_content: bytes,
source_type: SourceType,
extra: dict,
) -> FacadeResponse:
self.logger.info(
f"Creating mapping in workspace {workspace_id} with name {name}"
)
self.logger.info(
f"Retrieving workspace {workspace_id}"
)
workspace_metadata = self.workspace_metadata_service.get_workspace_metadata(
workspace_id,
)
workspace = self.workspace_service.get_workspace(
workspace_metadata.location,
)
self.logger.info("Creating source")
source = self.source_service.create_source(
type=source_type,
content=source_content,
extra=extra,
)
self.logger.info("Creating mapping")
mapping_graph_uuid = uuid4().hex
mapping_graph = MappingGraph(
uuid=mapping_graph_uuid,
name=name,
description=description,
source_id=source,
nodes=[],
edges=[],
)
self.mapping_service.create_mapping(mapping_graph)
self.logger.info("Mapping created")
workspace.copy_with(
mappings=workspace.mappings
+ [mapping_graph_uuid],
)
self.workspace_service.update_workspace(
workspace,
)
self.logger.info("Workspace updated")
return FacadeResponse(
status=202,
message="Mapping created",
data=mapping_graph,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
from kink import inject

from server.exceptions import ErrCodes
from server.facades import (
BaseFacade,
FacadeResponse,
ServerException,
)
from server.service_protocols.mapping_service_protocol import (
MappingServiceProtocol,
)
from server.services.core.workspace_metadata_service import (
WorkspaceMetadataServiceProtocol,
)
from server.services.local.local_source_service import (
SourceServiceProtocol,
)
from server.services.local.local_workspace_service import (
WorkspaceServiceProtocol,
)


@inject
class DeleteMappingFromWorkspaceFacade(BaseFacade):
def __init__(
self,
workspace_metadata_service: WorkspaceMetadataServiceProtocol,
workspace_service: WorkspaceServiceProtocol,
mapping_service: MappingServiceProtocol,
source_service: SourceServiceProtocol,
):
super().__init__()
self.workspace_metadata_service: WorkspaceMetadataServiceProtocol = workspace_metadata_service
self.workspace_service: WorkspaceServiceProtocol = (
workspace_service
)
self.mapping_service: MappingServiceProtocol = (
mapping_service
)
self.source_service: SourceServiceProtocol = (
source_service
)

@BaseFacade.error_wrapper
def execute(
self,
workspace_id: str,
mapping_id: str,
) -> FacadeResponse:
self.logger.info(
f"Dropping mapping {mapping_id} from workspace {workspace_id}"
)
self.logger.info(
f"Retrieving workspace {workspace_id}"
)
workspace_metadata = self.workspace_metadata_service.get_workspace_metadata(
workspace_id,
)
workspace = self.workspace_service.get_workspace(
workspace_metadata.location,
)

if mapping_id not in workspace.mappings:
self.logger.error(
f"Mapping {mapping_id} not found in workspace {workspace_id}"
)
raise ServerException(
f"Mapping {mapping_id} not found in workspace {workspace_id}",
code=ErrCodes.MAPPING_NOT_FOUND,
)

self.logger.info(f"Retrieving mapping {mapping_id}")

mapping = self.mapping_service.get_mapping(
mapping_id,
)

self.logger.info(
f"Deleting source {mapping.source_id}"
)

self.source_service.delete_source(
mapping.source_id,
)

self.logger.info(f"Deleting mapping {mapping_id}")

self.mapping_service.delete_mapping(
mapping_id,
)

self.logger.info(
f"Removing mapping {mapping_id} from workspace {workspace_id}"
)

new_model = workspace.copy_with(
mappings=[
m
for m in workspace.mappings
if m != mapping_id
],
)

self.workspace_service.update_workspace(
new_model,
)

return FacadeResponse(
status=200,
message=f"Mapping {mapping_id} dropped from workspace {workspace_id}",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from kink import inject

from server.facades import (
BaseFacade,
FacadeResponse,
)
from server.service_protocols.mapping_service_protocol import (
MappingServiceProtocol,
)
from server.services.core.workspace_metadata_service import (
WorkspaceMetadataServiceProtocol,
)
from server.services.local.local_source_service import (
SourceServiceProtocol,
)
from server.services.local.local_workspace_service import (
WorkspaceServiceProtocol,
)


@inject
class GetMappingsInWorkspaceFacade(BaseFacade):
def __init__(
self,
workspace_metadata_service: WorkspaceMetadataServiceProtocol,
workspace_service: WorkspaceServiceProtocol,
mapping_service: MappingServiceProtocol,
source_service: SourceServiceProtocol,
):
super().__init__()
self.workspace_metadata_service: WorkspaceMetadataServiceProtocol = workspace_metadata_service
self.workspace_service: WorkspaceServiceProtocol = (
workspace_service
)
self.mapping_service: MappingServiceProtocol = (
mapping_service
)
self.source_service: SourceServiceProtocol = (
source_service
)

@BaseFacade.error_wrapper
def execute(
self,
workspace_id: str,
) -> FacadeResponse:
self.logger.info(
f"Retrieving mappings in workspace {workspace_id}"
)
self.logger.info(
f"Retrieving workspace {workspace_id}"
)
workspace_metadata = self.workspace_metadata_service.get_workspace_metadata(
workspace_id,
)
workspace = self.workspace_service.get_workspace(
workspace_metadata.location,
)
self.logger.info("Retrieving mappings")

mappings = [
self.mapping_service.get_mapping(mapping_id)
for mapping_id in workspace.mappings
]

return FacadeResponse(
status=200,
message=f"Retrieved mappings in workspace {workspace_id}",
data=mappings,
)
38 changes: 38 additions & 0 deletions server/facades/workspace/mapping/update_mapping_facade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from kink import inject

from server.facades import (
BaseFacade,
FacadeResponse,
)
from server.models.mapping import MappingGraph
from server.service_protocols.mapping_service_protocol import (
MappingServiceProtocol,
)


@inject
class UpdateMappingFacade(BaseFacade):
def __init__(
self,
mapping_service: MappingServiceProtocol,
):
super().__init__()
self.mapping_service: MappingServiceProtocol = (
mapping_service
)

@BaseFacade.error_wrapper
def execute(
self,
mapping_id: str,
mapping_graph: MappingGraph,
) -> FacadeResponse:
self.logger.info(f"Updating mapping {mapping_id}")
self.mapping_service.update_mapping(
mapping_id,
mapping_graph,
)
return FacadeResponse(
status=200,
message=f"Mapping {mapping_id} updated",
)
12 changes: 12 additions & 0 deletions server/models/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,15 @@ class MappingGraph:
Attributes:
uuid (str): The UUID of the graph
name (str): The name of the graph
description (str): The description of the graph
nodes (list[MappingNode]): The nodes in the graph
edges (list[MappingEdge]): The edges in the graph
"""

uuid: str
name: str
description: str
source_id: str
nodes: list[
MappingNode | MappingLiteral | MappingURIRef
Expand All @@ -207,6 +211,8 @@ class MappingGraph:
def to_dict(self):
return {
"uuid": self.uuid,
"name": self.name,
"description": self.description,
"source_id": self.source_id,
"nodes": [
node.to_dict() for node in self.nodes
Expand All @@ -220,6 +226,10 @@ def to_dict(self):
def from_dict(cls, data):
if "uuid" not in data:
raise ValueError("uuid is required")
if "name" not in data:
raise ValueError("name is required")
if "description" not in data:
raise ValueError("description is required")
if "source_id" not in data:
raise ValueError("source_id is required")
if "nodes" not in data:
Expand All @@ -228,6 +238,8 @@ def from_dict(cls, data):
raise ValueError("edges is required")
return cls(
uuid=data["uuid"],
name=data["name"],
description=data["description"],
source_id=data["source_id"],
nodes=[
MappingNode.from_dict(node)
Expand Down
9 changes: 9 additions & 0 deletions server/routers/workspaces/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
HttpUrl,
)

from server.models.source import SourceType
from server.services.core.sqlite_db_service.tables.workspace_metadata import (
WorkspaceType,
)
Expand Down Expand Up @@ -31,3 +32,11 @@ class CreateOntologyInput(BaseModel):
description: str
base_uri: HttpUrl
content: Base64UrlStr


class CreateMappingInput(BaseModel):
name: str
description: str
content: Base64UrlStr
source_type: SourceType
extra: dict = {}
Loading

0 comments on commit b36359f

Please sign in to comment.