From 5b465b646b4ccbf0d968d3c1355de08ebd44db28 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Mon, 19 Aug 2024 14:30:47 +0200 Subject: [PATCH 1/2] [Fixes kbss-cvut/fta-fmea-ui#557] Throw exception on system creation if system with the same name exists --- .../analysis/controller/SystemController.java | 3 +-- .../cvut/kbss/analysis/dao/NamedEntityDao.java | 18 ++++++++++++++++++ .../service/SystemRepositoryService.java | 13 +++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/analysis/controller/SystemController.java b/src/main/java/cz/cvut/kbss/analysis/controller/SystemController.java index 7472159c..8b144619 100755 --- a/src/main/java/cz/cvut/kbss/analysis/controller/SystemController.java +++ b/src/main/java/cz/cvut/kbss/analysis/controller/SystemController.java @@ -60,8 +60,7 @@ public Set getFailureModes(@PathVariable(name = "systemFragment") S @PostMapping(consumes = {MediaType.APPLICATION_JSON_VALUE, JsonLd.MEDIA_TYPE}) public System create(@RequestBody System system) { log.info("> create - {}", system); - repositoryService.persist(system); - return system; + return repositoryService.create(system); } @PutMapping(consumes = {MediaType.APPLICATION_JSON_VALUE, JsonLd.MEDIA_TYPE}) diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/NamedEntityDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/NamedEntityDao.java index 43dba1b4..d5dfd1e1 100644 --- a/src/main/java/cz/cvut/kbss/analysis/dao/NamedEntityDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/NamedEntityDao.java @@ -39,4 +39,22 @@ public List findAllSummaries(){ } } + public List findUriByName(String name){ + try { + return em.createNativeQuery(""" + SELECT ?x WHERE { + ?x a ?type ; ?hasName ?val . + FILTER(str(?val) = ?name) + } + """, + URI.class) + .setParameter("type", typeUri) + .setParameter("hasName", URI.create(Vocabulary.s_p_name)) + .setParameter("name", name) + .getResultList(); + } catch (RuntimeException e) { + throw new PersistenceException(e); + } + } + } diff --git a/src/main/java/cz/cvut/kbss/analysis/service/SystemRepositoryService.java b/src/main/java/cz/cvut/kbss/analysis/service/SystemRepositoryService.java index 6de51975..355ca715 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/SystemRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/SystemRepositoryService.java @@ -4,6 +4,7 @@ import cz.cvut.kbss.analysis.dao.GenericDao; import cz.cvut.kbss.analysis.dao.SystemDao; import cz.cvut.kbss.analysis.dao.UserDao; +import cz.cvut.kbss.analysis.exception.LogicViolationException; import cz.cvut.kbss.analysis.model.Component; import cz.cvut.kbss.analysis.model.FailureMode; import cz.cvut.kbss.analysis.model.Item; @@ -65,6 +66,18 @@ public System findRequired(URI id) { return system; } + @Transactional + public System create(System system){ + List existingSystems = systemDao.findUriByName(system.getName()); + if(!existingSystems.isEmpty()) + throw new LogicViolationException(( + "Cannot create system with name \"%s\", " + + "the name is already assigned by other system.") + .formatted(system.getName())); + this.persist(system); + return system; + } + @Transactional public System rename(System systemRename) { log.info("> rename - {}", systemRename); From 71dad8824880cf30d53c85d73c69a3246dec34e1 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Mon, 19 Aug 2024 14:31:36 +0200 Subject: [PATCH 2/2] [Fixes kbss-cvut/fta-fmea-ui#567] Throw exception on system delete if system has fault trees --- .../cz/cvut/kbss/analysis/dao/SystemDao.java | 25 +++++++++++++++++++ .../service/SystemRepositoryService.java | 15 +++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/SystemDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/SystemDao.java index 550fb3e7..be448544 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/SystemDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/SystemDao.java @@ -1,6 +1,7 @@ package cz.cvut.kbss.analysis.dao; import cz.cvut.kbss.analysis.config.conf.PersistenceConf; +import cz.cvut.kbss.analysis.exception.PersistenceException; import cz.cvut.kbss.analysis.model.Component; import cz.cvut.kbss.analysis.model.System; import cz.cvut.kbss.analysis.service.IdentifierService; @@ -48,4 +49,28 @@ public List findComponents(URI systemURI){ .setParameter("hasComponentPartProp", HAS_COMPONENT_PART) .getResultList(); } + + public List findSystemsFaultTrees(URI uri){ + try { + return em.createNativeQuery(""" + PREFIX fta: + + SELECT DISTINCT ?ftUri WHERE { + ?uri a ?type. + ?_subsystemUri fta:is-part-of* ?uri. + ?behavior fta:has-component ?_subsystemUri. + ?rootEventType fta:is-manifestation-of ?behavior . + ?rootEvent fta:is-derived-from ?rootEventType. + ?ftUri fta:is-manifested-by ?rootEvent . + ?ftUri a fta:fault-tree. + } + """, + URI.class) + .setParameter("uri", uri) + .setParameter("type", typeUri) + .getResultList(); + } catch (RuntimeException e) { + throw new PersistenceException(e); + } + } } diff --git a/src/main/java/cz/cvut/kbss/analysis/service/SystemRepositoryService.java b/src/main/java/cz/cvut/kbss/analysis/service/SystemRepositoryService.java index 355ca715..f1396f28 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/SystemRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/SystemRepositoryService.java @@ -12,6 +12,7 @@ import cz.cvut.kbss.analysis.model.opdata.OperationalDataFilter; import cz.cvut.kbss.analysis.service.security.SecurityUtils; import cz.cvut.kbss.analysis.util.Vocabulary; +import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -78,6 +79,20 @@ public System create(System system){ return system; } + @Override + public void remove(@NonNull URI instanceUri) { + System system = findAllSummary(instanceUri); + List faultTrees = systemDao.findSystemsFaultTrees(instanceUri); + if(!faultTrees.isEmpty()) { + + throw new LogicViolationException(( + "Cannot delete system \"%s\" (<%s>), " + + "the system has fault %d trees.") + .formatted(system.getName(), instanceUri, faultTrees.size())); + } + super.remove(instanceUri); + } + @Transactional public System rename(System systemRename) { log.info("> rename - {}", systemRename);