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 7472159..8b14461 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 43dba1b..d5dfd1e 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/dao/SystemDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/SystemDao.java index 550fb3e..be44854 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 6de5197..f1396f2 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; @@ -11,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; @@ -65,6 +67,32 @@ 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; + } + + @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);