Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if system can be created or deleted #149

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public Set<FailureMode> 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})
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/cz/cvut/kbss/analysis/dao/NamedEntityDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,22 @@ public List<T> findAllSummaries(){
}
}

public List<URI> 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);
}
}

}
25 changes: 25 additions & 0 deletions src/main/java/cz/cvut/kbss/analysis/dao/SystemDao.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -48,4 +49,28 @@ public List<Component> findComponents(URI systemURI){
.setParameter("hasComponentPartProp", HAS_COMPONENT_PART)
.getResultList();
}

public List<URI> findSystemsFaultTrees(URI uri){
try {
return em.createNativeQuery("""
PREFIX fta: <http://onto.fel.cvut.cz/ontologies/fta-fmea-application/>

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
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;
import cz.cvut.kbss.analysis.model.System;
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;
Expand Down Expand Up @@ -65,6 +67,32 @@ public System findRequired(URI id) {
return system;
}

@Transactional
public System create(System system){
List<URI> 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<URI> 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);
Expand Down
Loading