From b438ea03c8966d175b1d80e3bf8b1b4f853fa255 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Wed, 19 Jun 2024 08:39:35 +0200 Subject: [PATCH] [Refactor for kbss-cvut/fta-fmea-ui#390] Move findWithDetails to new FaultTreeService to enable transactions defined by transactional annotation - seems to contribute fixing an issue with loading fault tree details --- .../controller/FaultTreeController.java | 4 +- .../service/FaultTreeRepositoryService.java | 50 +----------- .../analysis/service/FaultTreeService.java | 80 +++++++++++++++++++ 3 files changed, 86 insertions(+), 48 deletions(-) create mode 100644 src/main/java/cz/cvut/kbss/analysis/service/FaultTreeService.java diff --git a/src/main/java/cz/cvut/kbss/analysis/controller/FaultTreeController.java b/src/main/java/cz/cvut/kbss/analysis/controller/FaultTreeController.java index 2c01b6e..470976f 100755 --- a/src/main/java/cz/cvut/kbss/analysis/controller/FaultTreeController.java +++ b/src/main/java/cz/cvut/kbss/analysis/controller/FaultTreeController.java @@ -4,6 +4,7 @@ import cz.cvut.kbss.analysis.model.opdata.OperationalDataFilter; import cz.cvut.kbss.analysis.service.FaultTreeEvaluationService; import cz.cvut.kbss.analysis.service.FaultTreeRepositoryService; +import cz.cvut.kbss.analysis.service.FaultTreeService; import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jsonld.JsonLd; @@ -27,6 +28,7 @@ public class FaultTreeController { private final FaultTreeRepositoryService repositoryService; private final FaultTreeEvaluationService faultTreeEvaluationService; private final IdentifierService identifierService; + private final FaultTreeService faultTreeService; @GetMapping public List findAll() { @@ -42,7 +44,7 @@ public List summaries() { public FaultTree find(@PathVariable(name = "faultTreeFragment") String faultTreeFragment) { log.info("> find - {}", faultTreeFragment); URI faultTreeUri = identifierService.composeIdentifier(Vocabulary.s_c_fault_tree, faultTreeFragment); - return repositoryService.findWithDetails(faultTreeUri); + return faultTreeService.findWithDetails(faultTreeUri); } @ResponseStatus(HttpStatus.CREATED) 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 5e9a02b..9207466 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java @@ -43,7 +43,6 @@ public class FaultTreeRepositoryService extends ComplexManagedEntityRepositorySe private final ThreadLocal> visitedBehaviors = new ThreadLocal<>(); private final FaultEventDao faultEventDao; private final OperationalDataFilterService operationalDataFilterService; - private final FailureModeDao failureModeDao; private final OperationalDataService operationalDataService; private final FaultEventTypeService faultEventTypeService; private final FailureRateEstimateDao failureRateEstimateDao; @@ -59,7 +58,6 @@ public FaultTreeRepositoryService(@Qualifier("defaultValidator") Validator valid SecurityUtils securityUtils, FaultEventDao faultEventDao, OperationalDataFilterService operationalDataFilterService, - FailureModeDao failureModeDao, OperationalDataService operationalDataService, FaultEventTypeService faultEventTypeService, FailureRateEstimateDao failureRateEstimateDao) { @@ -71,7 +69,6 @@ public FaultTreeRepositoryService(@Qualifier("defaultValidator") Validator valid this.identifierService = identifierService; this.faultEventDao = faultEventDao; this.operationalDataFilterService = operationalDataFilterService; - this.failureModeDao = failureModeDao; this.operationalDataService = operationalDataService; this.faultEventTypeService = faultEventTypeService; this.failureRateEstimateDao = failureRateEstimateDao; @@ -103,34 +100,9 @@ public void createTree(FaultTree faultTree){ persist(faultTree); } - protected void setRelatedBehaviors(Collection events){ - for(Event event : events){ - if(event.getBehavior() == null) - event.setBehavior(failureModeDao.findByEvent(event.getUri())); - } - } - - public FaultTree findWithDetails(URI id) { - FaultTree ft = findRequired(id); - - Collection events = faultTreeDao.getRelatedEventTypes(ft); - setRelatedBehaviors(events); - - events.stream().map(e -> e.getBehavior()).forEach(b -> { - Item item = b.getItem(); - if(item == null) - return; - - item.setComponents(null); - Optional.ofNullable(item.getSupertypes()).ifPresent( s -> s.forEach(st -> st.setComponents(null))); - }); - - setReferences(ft); - - FaultTree summary = findSummary(ft.getUri()); - ft.setOperationalDataFilter(summary.getOperationalDataFilter()); - - return ft; + @Transactional + public FaultTree findRequired(URI id) { + return super.findRequired(id); } public FaultTree findSummary(URI faultTreeUri){ @@ -152,22 +124,6 @@ public List findAllSummaries(){ return summaries; } - public void setReferences(FaultTree faultTree){ - if(faultTree.getManifestingEvent() == null) - return; - - Stack> stack = new Stack<>(); - stack.add(Pair.of(null,faultTree.getManifestingEvent())); - while(!stack.isEmpty()){ - Pair p = stack.pop(); - FaultEvent fe = p.getSecond(); - faultEventRepositoryService.setExternalReference(p.getFirst(), fe); - if(fe.getChildren() == null) - continue; - fe.getChildren().forEach(c -> stack.push(Pair.of(fe.getUri(), c))); - } - } - @Transactional public FaultTree findWithPropagation(URI faultTreeUri) { log.info("> findWithPropagation - {}", faultTreeUri); diff --git a/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeService.java b/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeService.java new file mode 100644 index 0000000..47ddcad --- /dev/null +++ b/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeService.java @@ -0,0 +1,80 @@ +package cz.cvut.kbss.analysis.service; + +import cz.cvut.kbss.analysis.dao.FailureModeDao; +import cz.cvut.kbss.analysis.dao.FaultTreeDao; +import cz.cvut.kbss.analysis.model.Event; +import cz.cvut.kbss.analysis.model.FaultEvent; +import cz.cvut.kbss.analysis.model.FaultTree; +import cz.cvut.kbss.analysis.model.Item; +import cz.cvut.kbss.analysis.service.util.Pair; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.net.URI; +import java.util.Collection; +import java.util.Optional; +import java.util.Stack; + +@Service +@Slf4j +public class FaultTreeService{ + + private final FaultEventRepositoryService faultEventRepositoryService; + private final FailureModeDao failureModeDao; + private final FaultTreeRepositoryService faultTreeRepositoryService; + private final FaultTreeDao faultTreeDao; + + public FaultTreeService(FaultEventRepositoryService faultEventRepositoryService, FailureModeDao failureModeDao, FaultTreeRepositoryService faultTreeRepositoryService, FaultTreeDao faultTreeDao) { + this.faultEventRepositoryService = faultEventRepositoryService; + this.failureModeDao = failureModeDao; + this.faultTreeRepositoryService = faultTreeRepositoryService; + this.faultTreeDao = faultTreeDao; + } + + public FaultTree findWithDetails(URI id) { + FaultTree ft = faultTreeRepositoryService.findRequired(id); + Collection events = faultTreeDao.getRelatedEventTypes(ft); + setRelatedBehaviors(events); + + events.stream().map(e -> e.getBehavior()).forEach(b -> { + Item item = b.getItem(); + if(item == null) + return; + + item.setComponents(null); + Optional.ofNullable(item.getSupertypes()).ifPresent(s -> s.forEach(st -> st.setComponents(null))); + }); + + setReferences(ft); + + FaultTree summary = faultTreeRepositoryService.findSummary(ft.getUri()); + ft.setOperationalDataFilter(summary.getOperationalDataFilter()); + ft.setSystem(summary.getSystem()); + ft.setSubsystem(summary.getSubsystem()); + + return ft; + } + + protected void setReferences(FaultTree faultTree){ + if(faultTree.getManifestingEvent() == null) + return; + + Stack> stack = new Stack<>(); + stack.add(Pair.of(null,faultTree.getManifestingEvent())); + while(!stack.isEmpty()){ + Pair p = stack.pop(); + FaultEvent fe = p.getSecond(); + faultEventRepositoryService.setExternalReference(p.getFirst(), fe); + if(fe.getChildren() == null) + continue; + fe.getChildren().forEach(c -> stack.push(Pair.of(fe.getUri(), c))); + } + } + + protected void setRelatedBehaviors(Collection events){ + for(Event event : events){ + if(event.getBehavior() == null) + event.setBehavior(failureModeDao.findByEvent(event.getUri())); + } + } +}