From 17fcb0df3fe64daf4806eb1aed5fb5a08d789d3d Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Thu, 16 May 2024 14:42:22 +0200 Subject: [PATCH] Fix creating fault tree when root event has supertype. - update BaseDao.getContext to not fail when entity is in multiple contexts. - add FaultEventDao.findEvent - fix FaultTreeRepositoryService.createTree to replace root supertypes for managed entities. --- .../cz/cvut/kbss/analysis/dao/BaseDao.java | 7 ++++-- .../cvut/kbss/analysis/dao/FaultEventDao.java | 16 ++++++++++---- .../service/FaultTreeRepositoryService.java | 22 +++++++++++++------ 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java index 75921105..d5628b79 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java @@ -59,9 +59,12 @@ public URI getContext(T entity){ } public URI getContext(URI uri){ - return em.createNativeQuery("SELECT DISTINCT ?context {GRAPH ?context {?uri a ?type}}", URI.class) + List contexts = em.createNativeQuery("SELECT DISTINCT ?context {GRAPH ?context {?uri a ?type}}", URI.class) .setParameter("uri", uri) - .getSingleResult(); + .getResultList(); + if(contexts.isEmpty()) + return null; + return contexts.get(0); } @Override diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventDao.java index c4a4c2c0..707161ae 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventDao.java @@ -2,10 +2,7 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.exception.PersistenceException; -import cz.cvut.kbss.analysis.model.FaultEvent; -import cz.cvut.kbss.analysis.model.FaultEventReference; -import cz.cvut.kbss.analysis.model.FaultEventType; -import cz.cvut.kbss.analysis.model.FaultEventTypeSummary; +import cz.cvut.kbss.analysis.model.*; import cz.cvut.kbss.analysis.model.diagram.Rectangle; import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.util.Vocabulary; @@ -17,6 +14,8 @@ import java.net.URI; import java.util.List; +import java.util.Objects; +import java.util.Optional; @Repository public class FaultEventDao extends NamedEntityDao { @@ -75,6 +74,15 @@ public Rectangle update(Rectangle rect){ } } + public Optional findEvent(URI id){ + Objects.requireNonNull(id); + try { + EntityDescriptor entityDescriptor = getEntityDescriptor(id); + return Optional.ofNullable(em.find(Event.class, id, entityDescriptor)); + } catch (RuntimeException e) { + throw new PersistenceException(e); + } + } public List getFaultEventRootWithSupertype(URI supertype){ try{ diff --git a/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java b/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java index 40531b41..e3f47ca5 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java @@ -1,9 +1,6 @@ package cz.cvut.kbss.analysis.service; -import cz.cvut.kbss.analysis.dao.FaultEventScenarioDao; -import cz.cvut.kbss.analysis.dao.FaultTreeDao; -import cz.cvut.kbss.analysis.dao.GenericDao; -import cz.cvut.kbss.analysis.dao.UserDao; +import cz.cvut.kbss.analysis.dao.*; import cz.cvut.kbss.analysis.exception.EntityNotFoundException; import cz.cvut.kbss.analysis.model.*; import cz.cvut.kbss.analysis.model.diagram.Rectangle; @@ -39,6 +36,7 @@ public class FaultTreeRepositoryService extends ComplexManagedEntityRepositorySe private final IdentifierService identifierService; private final ThreadLocal> visitedBehaviors = new ThreadLocal<>(); + private final FaultEventDao faultEventDao; @Autowired public FaultTreeRepositoryService(@Qualifier("defaultValidator") Validator validator, @@ -48,14 +46,15 @@ public FaultTreeRepositoryService(@Qualifier("defaultValidator") Validator valid FunctionRepositoryService functionRepositoryService, IdentifierService identifierService, UserDao userDao, - SecurityUtils securityUtils - ) { + SecurityUtils securityUtils, + FaultEventDao faultEventDao) { super(validator, userDao, securityUtils); this.faultTreeDao = faultTreeDao; this.faultEventScenarioDao = faultEventScenarioDao; this.faultEventRepositoryService = faultEventRepositoryService; this.functionRepositoryService = functionRepositoryService; this.identifierService = identifierService; + this.faultEventDao = faultEventDao; } @Override @@ -66,7 +65,16 @@ protected GenericDao getPrimaryDao() { @Transactional public void createTree(FaultTree faultTree){ if(faultTree.getManifestingEvent() != null){ - faultTree.getManifestingEvent().setRectangle(new Rectangle()); + FaultEvent faultEvent = faultTree.getManifestingEvent(); + faultEvent.setRectangle(new Rectangle()); + if(faultEvent.getSupertypes() != null) { + Set managedSupertypes = new HashSet<>(); + for(Event event : faultEvent.getSupertypes()){ + faultEventDao.findEvent(event.getUri()) + .ifPresent(managedSupertypes::add); + } + faultEvent.setSupertypes(managedSupertypes); + } } persist(faultTree); }