From 328447842e6598fe8eb78b84edf6574d2c997b9c Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Tue, 2 Apr 2024 08:59:18 +0200 Subject: [PATCH 1/5] [UPD] Extend IdentifierService adding methods to create random IRIs --- .../analysis/service/IdentifierService.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/main/java/cz/cvut/kbss/analysis/service/IdentifierService.java b/src/main/java/cz/cvut/kbss/analysis/service/IdentifierService.java index 84a6e345..cfe0f797 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/IdentifierService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/IdentifierService.java @@ -3,14 +3,60 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import java.math.BigInteger; import java.net.URI; +import java.security.SecureRandom; +import java.util.Random; @Service @Slf4j public class IdentifierService { + private final int RANDOM_BOUND = 10000; + + private final Random RANDOM = new Random(); + + private final Random SECURE_RANDOM = new SecureRandom(); + public URI composeIdentifier(String prefix, String fragment) { return URI.create(prefix + "/" + fragment); } + /** + * Generates a pseudo-unique OWL key using current system time and a random generator. + * + * @return OWL key + */ + public String generateKey() { + String key = Long.toString(System.nanoTime()); + return key.concat(Integer.toString(RANDOM.nextInt(RANDOM_BOUND))); + } + + /** + * Generates a number for uri using a random generator. + * + * @return String number + */ + public String generateRandomURINumber() { + return Integer.toString(RANDOM.nextInt(RANDOM_BOUND)); + } + + /** + * Generates a token for setting password using a secure random generator. + * + * @return String number + */ + public String generateRandomToken() { + int length = 20; + return String.format("%"+length+"s", new BigInteger(length*5/*base 32,2^5*/, SECURE_RANDOM) + .toString(32)).replace('\u0020', '0'); + } + + public URI generateNewInstanceUri(String typeUri) { + return generateNewUri(typeUri + "/instance"); + } + + public URI generateNewUri(String prefix) { + return URI.create(prefix + generateKey()); + } } From 852235679f0082103d15298da3284464a2092df4 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Tue, 2 Apr 2024 09:20:30 +0200 Subject: [PATCH 2/5] [UPD] Autowire IdentifierService to BaseDao and its subclasses --- .../java/cz/cvut/kbss/analysis/dao/BaseDao.java | 6 ++++-- .../cz/cvut/kbss/analysis/dao/BehaviorDao.java | 5 +++-- .../cz/cvut/kbss/analysis/dao/ComponentDao.java | 10 +++------- .../cz/cvut/kbss/analysis/dao/FailureModeDao.java | 5 +++-- .../kbss/analysis/dao/FailureModesRowDao.java | 7 +++++-- .../kbss/analysis/dao/FailureModesTableDao.java | 5 +++-- .../cz/cvut/kbss/analysis/dao/FaultEventDao.java | 6 ++++-- .../kbss/analysis/dao/FaultEventScenarioDao.java | 15 +++++++++++++-- .../cz/cvut/kbss/analysis/dao/FaultTreeDao.java | 5 +++-- .../cz/cvut/kbss/analysis/dao/FunctionDao.java | 5 +++-- .../cz/cvut/kbss/analysis/dao/MitigationDao.java | 5 +++-- .../cz/cvut/kbss/analysis/dao/NamedEntityDao.java | 7 +++++-- .../java/cz/cvut/kbss/analysis/dao/SystemDao.java | 5 +++-- .../java/cz/cvut/kbss/analysis/dao/UserDao.java | 5 +++-- 14 files changed, 58 insertions(+), 33 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 6af38cb3..8733b077 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java @@ -3,7 +3,7 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.exception.PersistenceException; import cz.cvut.kbss.analysis.model.util.EntityToOwlClassMapper; -import cz.cvut.kbss.analysis.model.util.HasIdentifier; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.jopa.model.EntityManager; import java.net.URI; @@ -21,12 +21,14 @@ public abstract class BaseDao implements GenericDao protected final EntityManager em; protected final PersistenceConf config; + protected final IdentifierService identifierService; - protected BaseDao(Class type, EntityManager em, PersistenceConf config) { + protected BaseDao(Class type, EntityManager em, PersistenceConf config, IdentifierService identifierService) { this.type = type; this.typeUri = URI.create(EntityToOwlClassMapper.getOwlClassForEntity(type)); this.em = em; this.config = config; + this.identifierService = identifierService; } @Override diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/BehaviorDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/BehaviorDao.java index 1b70971d..8a6a1084 100644 --- a/src/main/java/cz/cvut/kbss/analysis/dao/BehaviorDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/BehaviorDao.java @@ -4,6 +4,7 @@ import cz.cvut.kbss.analysis.exception.ResourceException; import cz.cvut.kbss.analysis.model.Behavior; import cz.cvut.kbss.analysis.resources.ResourceUtils; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jopa.model.EntityManager; @@ -15,8 +16,8 @@ public abstract class BehaviorDao extends NamedEntityDao{ public static URI P_IS_IMPAIRING = URI.create(Vocabulary.s_p_is_impairing); - public BehaviorDao(Class type, EntityManager em, PersistenceConf config) { - super(type, em, config); + public BehaviorDao(Class type, EntityManager em, PersistenceConf config, IdentifierService identifierService) { + super(type, em, config, identifierService); } public Set getTransitiveRequiredBehaviors(URI behaviorURI){ diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/ComponentDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/ComponentDao.java index c677b14c..5c6e4132 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/ComponentDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/ComponentDao.java @@ -1,22 +1,18 @@ 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.FailureMode; -import cz.cvut.kbss.analysis.util.Vocabulary; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.jopa.model.EntityManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; -import java.net.URI; - @Repository public class ComponentDao extends NamedEntityDao { @Autowired - public ComponentDao(EntityManager em, PersistenceConf config) { - super(Component.class, em, config); + public ComponentDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) { + super(Component.class, em, config, identifierService); } } diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/FailureModeDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/FailureModeDao.java index 90a1a9d4..c55b4264 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FailureModeDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FailureModeDao.java @@ -2,6 +2,7 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.model.FailureMode; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.jopa.model.EntityManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @@ -10,8 +11,8 @@ public class FailureModeDao extends BehaviorDao { @Autowired - public FailureModeDao(EntityManager em, PersistenceConf config) { - super(FailureMode.class, em, config); + public FailureModeDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) { + super(FailureMode.class, em, config, identifierService); } } diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/FailureModesRowDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/FailureModesRowDao.java index f822c18c..0d52be76 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FailureModesRowDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FailureModesRowDao.java @@ -2,12 +2,15 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.model.FailureModesRow; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.jopa.model.EntityManager; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository public class FailureModesRowDao extends BaseDao { - protected FailureModesRowDao(EntityManager em, PersistenceConf config) { - super(FailureModesRow.class, em, config); + @Autowired + protected FailureModesRowDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) { + super(FailureModesRow.class, em, config, identifierService); } } diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/FailureModesTableDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/FailureModesTableDao.java index 0f98a96a..143cc275 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FailureModesTableDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FailureModesTableDao.java @@ -2,12 +2,13 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.model.FailureModesTable; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.jopa.model.EntityManager; import org.springframework.stereotype.Repository; @Repository public class FailureModesTableDao extends NamedEntityDao { - protected FailureModesTableDao(EntityManager em, PersistenceConf config) { - super(FailureModesTable.class, em, config); + protected FailureModesTableDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) { + super(FailureModesTable.class, em, config, identifierService); } } 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 f19b9648..a2d4445e 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventDao.java @@ -4,6 +4,7 @@ import cz.cvut.kbss.analysis.exception.PersistenceException; import cz.cvut.kbss.analysis.model.FaultEvent; import cz.cvut.kbss.analysis.model.diagram.Rectangle; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jopa.model.EntityManager; import org.springframework.stereotype.Repository; @@ -12,8 +13,9 @@ @Repository public class FaultEventDao extends NamedEntityDao { - protected FaultEventDao(EntityManager em, PersistenceConf config) { - super(FaultEvent.class, em, config); + @Autowired + protected FaultEventDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) { + super(FaultEvent.class, em, config, identifierService); } public boolean isChild(URI faultEventIri) { diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventScenarioDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventScenarioDao.java index 9262b8a4..4e090ba0 100644 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventScenarioDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventScenarioDao.java @@ -2,12 +2,23 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.model.FaultEventScenario; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.jopa.model.EntityManager; +import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; +import java.net.URI; + @Repository public class FaultEventScenarioDao extends BaseDao { - protected FaultEventScenarioDao(EntityManager em, PersistenceConf config) { - super(FaultEventScenario.class, em, config); + @Autowired + protected FaultEventScenarioDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) { + super(FaultEventScenario.class, em, config, identifierService); + } + + @Override + public EntityDescriptor getEntityDescriptor(URI uri) { + return super.getEntityDescriptor(uri); } } diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java index ab0e1309..8fcfe659 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java @@ -2,6 +2,7 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.model.FaultTree; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jopa.model.EntityManager; import org.springframework.beans.factory.annotation.Autowired; @@ -13,8 +14,8 @@ public class FaultTreeDao extends NamedEntityDao { @Autowired - protected FaultTreeDao(EntityManager em, PersistenceConf config) { - super(FaultTree.class, em, config); + protected FaultTreeDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) { + super(FaultTree.class, em, config, identifierService); } public boolean isRootEvent(URI faultEventIri) { diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/FunctionDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/FunctionDao.java index 22f327a4..878923c7 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FunctionDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FunctionDao.java @@ -4,6 +4,7 @@ import cz.cvut.kbss.analysis.model.Behavior; import cz.cvut.kbss.analysis.model.Component; import cz.cvut.kbss.analysis.model.Function; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jopa.model.EntityManager; import org.springframework.beans.factory.annotation.Autowired; @@ -17,8 +18,8 @@ public class FunctionDao extends BehaviorDao { public static URI P_HAS_FUNCTION = URI.create(Vocabulary.s_p_has_function); @Autowired - protected FunctionDao(EntityManager em, PersistenceConf config) { - super(Function.class, em, config); + protected FunctionDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) { + super(Function.class, em, config, identifierService); } public Component getComponent(URI functionUri) { diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/MitigationDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/MitigationDao.java index 566bc970..f46b0e3d 100644 --- a/src/main/java/cz/cvut/kbss/analysis/dao/MitigationDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/MitigationDao.java @@ -2,6 +2,7 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.model.Mitigation; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.jopa.model.EntityManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @@ -10,7 +11,7 @@ public class MitigationDao extends BehaviorDao { @Autowired - protected MitigationDao(EntityManager em, PersistenceConf config) { - super(Mitigation.class, em, config); + protected MitigationDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) { + super(Mitigation.class, em, config, identifierService); } } 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 2adbc4e6..06aae2f5 100644 --- a/src/main/java/cz/cvut/kbss/analysis/dao/NamedEntityDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/NamedEntityDao.java @@ -4,8 +4,10 @@ import cz.cvut.kbss.analysis.exception.PersistenceException; import cz.cvut.kbss.analysis.model.NamedEntity; import cz.cvut.kbss.analysis.model.Summary; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jopa.model.EntityManager; +import org.springframework.beans.factory.annotation.Autowired; import java.net.URI; import java.util.List; @@ -14,8 +16,9 @@ public class NamedEntityDao extends BaseDao { public static URI P_HAS_NAME = URI.create(Vocabulary.s_p_name); public static URI P_HAS_DESCRIPTION = URI.create(Vocabulary.s_p_description); - protected NamedEntityDao(Class type, EntityManager em, PersistenceConf config) { - super(type, em, config); + @Autowired + protected NamedEntityDao(Class type, EntityManager em, PersistenceConf config, IdentifierService identifierService) { + super(type, em, config, identifierService); } public List findAllSummaries(){ 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 493d8014..94e2782f 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/SystemDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/SystemDao.java @@ -2,6 +2,7 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.model.System; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.jopa.model.EntityManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @@ -10,7 +11,7 @@ public class SystemDao extends NamedEntityDao { @Autowired - protected SystemDao(EntityManager em, PersistenceConf config) { - super(System.class, em, config); + protected SystemDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) { + super(System.class, em, config, identifierService); } } diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/UserDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/UserDao.java index 57db184d..c28224d1 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/UserDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/UserDao.java @@ -3,6 +3,7 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.exception.PersistenceException; import cz.cvut.kbss.analysis.model.User; +import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jopa.exceptions.NoResultException; import cz.cvut.kbss.jopa.model.EntityManager; @@ -17,8 +18,8 @@ public class UserDao extends BaseDao { @Autowired - public UserDao(EntityManager em, PersistenceConf config) { - super(User.class, em, config); + public UserDao(EntityManager em, PersistenceConf config, IdentifierService identifierService) { + super(User.class, em, config, identifierService); } /** From fda4d4c3530728a768312e9312a748324f2f5663 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Tue, 2 Apr 2024 09:28:06 +0200 Subject: [PATCH 3/5] [FIX #71] Update BaseDao to call EntityManager api with entity descriptor returned by getEntityDescriptor() which can be overridden by subclassing DAOs. --- .../cz/cvut/kbss/analysis/dao/BaseDao.java | 61 +++++++++++++++---- .../kbss/analysis/model/AbstractEntity.java | 3 + 2 files changed, 53 insertions(+), 11 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 8733b077..13319072 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java @@ -2,19 +2,24 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.exception.PersistenceException; +import cz.cvut.kbss.analysis.model.AbstractEntity; import cz.cvut.kbss.analysis.model.util.EntityToOwlClassMapper; import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.jopa.model.EntityManager; +import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor; import java.net.URI; -import java.util.*; +import java.util.Collection; +import java.util.List; +import java.util.Objects; +import java.util.Optional; /** * Base implementation of the generic DAO API. * * @param the entity class this DAO manages */ -public abstract class BaseDao implements GenericDao { +public abstract class BaseDao implements GenericDao { protected final Class type; protected final URI typeUri; @@ -31,6 +36,26 @@ protected BaseDao(Class type, EntityManager em, PersistenceConf config, Ident this.identifierService = identifierService; } + public EntityDescriptor getEntityDescriptor(T entity){ + return new EntityDescriptor(); + } + + public EntityDescriptor getEntityDescriptor(URI uri){ + return new EntityDescriptor(); + } + + public URI getContext(T entity){ + if(entity.getContext() == null) + entity.setContext(getContext(entity.getUri())); + return entity.getContext(); + } + + public URI getContext(URI uri){ + return em.createNativeQuery("SELECT DISTINCT ?context {GRAPH ?context {?uri a ?type}}", URI.class) + .setParameter("uri", uri) + .getSingleResult(); + } + @Override public List findAll() { try { @@ -58,7 +83,8 @@ public List findAll(URI contenxt) { public Optional find(URI id) { Objects.requireNonNull(id); try { - return Optional.ofNullable(em.find(type, id)); + EntityDescriptor entityDescriptor = getEntityDescriptor(id); + return Optional.ofNullable(em.find(type, id, entityDescriptor)); } catch (RuntimeException e) { throw new PersistenceException(e); } @@ -68,7 +94,8 @@ public Optional find(URI id) { public Optional getReference(URI id) { Objects.requireNonNull(id); try { - return Optional.ofNullable(em.getReference(type, id)); + EntityDescriptor entityDescriptor = getEntityDescriptor(id); + return Optional.ofNullable(em.getReference(type, id, entityDescriptor)); } catch (RuntimeException e) { throw new PersistenceException(e); } @@ -78,7 +105,7 @@ public Optional getReference(URI id) { public void persist(T entity) { Objects.requireNonNull(entity); try { - em.persist(entity); + _persist(entity); } catch (RuntimeException e) { throw new PersistenceException(e); } @@ -88,17 +115,23 @@ public void persist(T entity) { public void persist(Collection entities) { Objects.requireNonNull(entities); try { - entities.forEach(em::persist); + entities.forEach(this::_persist); } catch (RuntimeException e) { throw new PersistenceException(e); } } + protected void _persist(T entity){ + EntityDescriptor entityDescriptor = getEntityDescriptor(entity); + em.persist(entity, entityDescriptor); + } + @Override public T update(T entity) { Objects.requireNonNull(entity); try { - return em.merge(entity); + EntityDescriptor entityDescriptor = getEntityDescriptor(entity); + return em.merge(entity, entityDescriptor); } catch (RuntimeException e) { throw new PersistenceException(e); } @@ -108,7 +141,8 @@ public T update(T entity) { public void remove(T entity) { Objects.requireNonNull(entity); try { - em.remove(em.merge(entity)); + EntityDescriptor entityDescriptor = getEntityDescriptor(entity); + em.remove(em.merge(entity,entityDescriptor)); } catch (RuntimeException e) { throw new PersistenceException(e); } @@ -128,7 +162,7 @@ public void remove(URI id) { public boolean exists(URI id) { Objects.requireNonNull(id); try { - return em.createNativeQuery("ASK { ?x a ?type . }", Boolean.class) + return em.createNativeQuery("ASK { ?x a ?type . }", Boolean.class) // TODO - consider existence in graph context .setParameter("x", id) .setParameter("type", typeUri) .getSingleResult(); @@ -137,14 +171,19 @@ public boolean exists(URI id) { } } + /** + * Checks if the input id exists in current persistence context. + * @param id Entity identifier + * @return + */ @Override public boolean existsInContext(URI id) { Objects.requireNonNull(id); - return em.find(type, id) != null; + return em.find(type, id) != null;// TODO - consider existence in graph context, alternatively consider em.contains } @Override - public boolean existsWithPredicate(String predicate, String value) { + public boolean existsWithPredicate(String predicate, String value) {// TODO - consider existence in graph context Objects.requireNonNull(value); return em .createNativeQuery("ASK WHERE { ?x a ?type ; ?predicate ?value . }", Boolean.class) diff --git a/src/main/java/cz/cvut/kbss/analysis/model/AbstractEntity.java b/src/main/java/cz/cvut/kbss/analysis/model/AbstractEntity.java index 45e54d7d..e76ab5f3 100755 --- a/src/main/java/cz/cvut/kbss/analysis/model/AbstractEntity.java +++ b/src/main/java/cz/cvut/kbss/analysis/model/AbstractEntity.java @@ -13,6 +13,9 @@ @Data public abstract class AbstractEntity implements HasIdentifier, Serializable { + @Transient + private URI context; + @Id(generated = true) private URI uri; From 2dcc3dd7f216d85999fc5c725d0880560dbc6d97 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Tue, 2 Apr 2024 09:33:06 +0200 Subject: [PATCH 4/5] [FIX #71] Override getEntityDescriptor() in FaultTreeDao and FaultEventDao to perform EntityManager CRUD api in the fault tree's graph context. --- .../cvut/kbss/analysis/dao/FaultEventDao.java | 33 +++++++++++++++++-- .../cvut/kbss/analysis/dao/FaultTreeDao.java | 14 ++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) 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 a2d4445e..da227947 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventDao.java @@ -7,6 +7,9 @@ import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jopa.model.EntityManager; +import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor; +import cz.cvut.kbss.jopa.model.metamodel.EntityType; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import java.net.URI; @@ -27,12 +30,38 @@ public boolean isChild(URI faultEventIri) { .getSingleResult(); } + @Override + public EntityDescriptor getEntityDescriptor(FaultEvent entity) { + URI graph = getContext(entity); + return getEntityDescriptorInContext(graph); + } + + @Override + public EntityDescriptor getEntityDescriptor(URI uri) { + URI graph = getContext(uri); + return getEntityDescriptorInContext(graph); + } + + protected EntityDescriptor getEntityDescriptorInContext(URI graph){ + EntityDescriptor entityDescriptor = new EntityDescriptor(graph); + EntityType fe = em.getMetamodel().entity(FaultEvent.class); + entityDescriptor.addAttributeContext(fe.getAttribute("supertypes"), null); + // TODO - consider not persisting failure mode in graph context + return entityDescriptor; + } + + public EntityDescriptor getRectangleDescriptor(URI uri){ + URI graph = getContext(uri); + EntityDescriptor entityDescriptor = new EntityDescriptor(graph); + return entityDescriptor; + } + public Rectangle update(Rectangle rect){ try{ - return em.merge(rect); + EntityDescriptor entityDescriptor = getRectangleDescriptor(rect.getUri()); + return em.merge(rect, entityDescriptor); }catch (RuntimeException e){ throw new PersistenceException(e); } } - } diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java index 8fcfe659..6bdbb6a8 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FaultTreeDao.java @@ -5,6 +5,7 @@ import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jopa.model.EntityManager; +import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @@ -27,4 +28,17 @@ public boolean isRootEvent(URI faultEventIri) { .getSingleResult(); } + @Override + public EntityDescriptor getEntityDescriptor(FaultTree entity) { + if(entity.getUri() == null) + entity.setUri(identifierService.generateNewInstanceUri(typeUri.toString())); + EntityDescriptor entityDescriptor = new EntityDescriptor(entity.getUri()); + return entityDescriptor; + } + + @Override + public EntityDescriptor getEntityDescriptor(URI uri) { + EntityDescriptor entityDescriptor = new EntityDescriptor(uri); + return entityDescriptor; + } } From ddd836550588be32daab067ea7bff227c82b6d27 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Thu, 4 Apr 2024 15:21:32 +0200 Subject: [PATCH 5/5] [FIX #kbss-cvut/fta-fmea-ui#219] Fix for review comments. - remove TODOs - rename BaseDao.existsInContext to BaseDao.existsInPersistenceContext - remove unused FaultEvent.failureMode, - set default graph for attribute FaultEvent.behavior in FaultEvent EntityDescriptor in FaultEventDao --- .../java/cz/cvut/kbss/analysis/dao/BaseDao.java | 8 ++++---- .../cz/cvut/kbss/analysis/dao/FaultEventDao.java | 3 ++- .../cz/cvut/kbss/analysis/dao/GenericDao.java | 2 +- .../cz/cvut/kbss/analysis/model/FaultEvent.java | 3 --- .../analysis/service/BaseRepositoryService.java | 9 +++------ .../service/FaultTreeRepositoryService.java | 16 ++++++++-------- 6 files changed, 18 insertions(+), 23 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 13319072..a02f5625 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java @@ -162,7 +162,7 @@ public void remove(URI id) { public boolean exists(URI id) { Objects.requireNonNull(id); try { - return em.createNativeQuery("ASK { ?x a ?type . }", Boolean.class) // TODO - consider existence in graph context + return em.createNativeQuery("ASK { ?x a ?type . }", Boolean.class) .setParameter("x", id) .setParameter("type", typeUri) .getSingleResult(); @@ -177,13 +177,13 @@ public boolean exists(URI id) { * @return */ @Override - public boolean existsInContext(URI id) { + public boolean existsInPersistenceContext(URI id) { Objects.requireNonNull(id); - return em.find(type, id) != null;// TODO - consider existence in graph context, alternatively consider em.contains + return em.find(type, id) != null; } @Override - public boolean existsWithPredicate(String predicate, String value) {// TODO - consider existence in graph context + public boolean existsWithPredicate(String predicate, String value) { Objects.requireNonNull(value); return em .createNativeQuery("ASK WHERE { ?x a ?type ; ?predicate ?value . }", Boolean.class) 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 da227947..933535e5 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/FaultEventDao.java @@ -46,7 +46,8 @@ protected EntityDescriptor getEntityDescriptorInContext(URI graph){ EntityDescriptor entityDescriptor = new EntityDescriptor(graph); EntityType fe = em.getMetamodel().entity(FaultEvent.class); entityDescriptor.addAttributeContext(fe.getAttribute("supertypes"), null); - // TODO - consider not persisting failure mode in graph context + entityDescriptor.addAttributeContext(fe.getAttribute("behavior"), null); + return entityDescriptor; } diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/GenericDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/GenericDao.java index 805151a7..4f0de31f 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/GenericDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/GenericDao.java @@ -103,7 +103,7 @@ public interface GenericDao { * @param id Entity identifier * @return {@literal true} if entity exists, {@literal false} otherwise */ - boolean existsInContext(URI id); + boolean existsInPersistenceContext(URI id); /** * Checks whether an entity with the specified predicate and its value exists (and has the type managed by this diff --git a/src/main/java/cz/cvut/kbss/analysis/model/FaultEvent.java b/src/main/java/cz/cvut/kbss/analysis/model/FaultEvent.java index 6b9fce2f..d03cce48 100755 --- a/src/main/java/cz/cvut/kbss/analysis/model/FaultEvent.java +++ b/src/main/java/cz/cvut/kbss/analysis/model/FaultEvent.java @@ -49,9 +49,6 @@ public static FaultEvent create(){ @OWLObjectProperty(iri = Vocabulary.s_p_has_child, cascade = CascadeType.ALL, fetch = FetchType.EAGER) private Set children = new HashSet<>(); - @OWLObjectProperty(iri = Vocabulary.s_p_has_failure_mode, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}) - private FailureMode failureMode; - @OWLDataProperty(iri = Vocabulary.s_p_sequence_probability) private Double sequenceProbability; diff --git a/src/main/java/cz/cvut/kbss/analysis/service/BaseRepositoryService.java b/src/main/java/cz/cvut/kbss/analysis/service/BaseRepositoryService.java index c69ed123..8ce7c89b 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/BaseRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/BaseRepositoryService.java @@ -4,7 +4,6 @@ import java.util.List; import java.util.Objects; import java.util.Optional; -import java.util.Set; import java.util.stream.Collectors; import com.fasterxml.classmate.ResolvedType; @@ -17,9 +16,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.BindingResult; import org.springframework.validation.DataBinder; -import org.springframework.validation.Errors; import org.springframework.validation.Validator; -import org.springframework.validation.beanvalidation.SpringValidatorAdapter; /** @@ -243,13 +240,13 @@ public boolean exists(URI id) { } /** - * Checks whether an instance with the specified identifier exists in the current context and or the repository. + * Checks whether an instance with the specified identifier exists in the current persistence context and or the repository. * * @param id ID to check * @return {@code true} if the instance exists, {@code false} otherwise */ - public boolean existsInContext(URI id) { - return getPrimaryDao().existsInContext(id); + public boolean existsInPersistenceContext(URI id) { + return getPrimaryDao().existsInPersistenceContext(id); } /** 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 c33d260b..3f96e3a0 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java @@ -231,11 +231,11 @@ private FaultEvent transferBehaviorToFaultEvent(Behavior behavior, FaultEvent pa URI faultEventUri1 = createUri(behavior, parentEvent, "e"); URI faultEventUri2 = createUri(behavior, parentEvent, "f"); - if (faultEventRepositoryService.existsInContext(faultEventUri)) { + if (faultEventRepositoryService.existsInPersistenceContext(faultEventUri)) { return faultEventRepositoryService.findRequired(faultEventUri); - } else if(faultEventRepositoryService.existsInContext(faultEventUri1)){ + } else if(faultEventRepositoryService.existsInPersistenceContext(faultEventUri1)){ return faultEventRepositoryService.findRequired(faultEventUri1); - } else if(faultEventRepositoryService.existsInContext(faultEventUri2)){ + } else if(faultEventRepositoryService.existsInPersistenceContext(faultEventUri2)){ return faultEventRepositoryService.findRequired(faultEventUri2); } else { FaultEvent faultEvent = FaultEvent.create(); @@ -276,9 +276,9 @@ private FaultEvent processImpairingBehavior(Behavior impairingBehavior, FaultEve URI faultEventUri = createUri(impairingBehavior, impairedBehaviorEvent, ""); URI faultEventUriTypeEvent = createUri(impairingBehavior, impairedBehaviorEvent, "e"); - if(faultEventRepositoryService.existsInContext(faultEventUri)) { + if(faultEventRepositoryService.existsInPersistenceContext(faultEventUri)) { faultEvent = faultEventRepositoryService.findRequired(faultEventUri); - }else if(faultEventRepositoryService.existsInContext(faultEventUriTypeEvent)){ + }else if(faultEventRepositoryService.existsInPersistenceContext(faultEventUriTypeEvent)){ faultEvent = faultEventRepositoryService.findRequired(faultEventUriTypeEvent); }else { faultEvent = FaultEvent.create(); @@ -302,7 +302,7 @@ private FaultEvent processImpairingBehavior(Behavior impairingBehavior, FaultEve FaultEvent faultEventChild = FaultEvent.create(); faultEventChild.setBehavior(behaviorChild); faultEventUri = createUri(behaviorChild, faultEvent, "e"); - if (faultEventRepositoryService.existsInContext(faultEventUri)) { + if (faultEventRepositoryService.existsInPersistenceContext(faultEventUri)) { faultEventChild = faultEventRepositoryService.findRequired(faultEventUri); } else { faultEventChild.setUri(faultEventUri); @@ -327,7 +327,7 @@ private void processChildBehaviors(Behavior behavior,FaultEvent parentFaultEvent FaultEvent faultEvent; if(behavior instanceof Function){ URI faultEventUri = createUri(behavior, null, "f"); - if (faultEventRepositoryService.existsInContext(faultEventUri)) { + if (faultEventRepositoryService.existsInPersistenceContext(faultEventUri)) { faultEvent = faultEventRepositoryService.findRequired(faultEventUri); } else { faultEvent = FaultEvent.create(); @@ -345,7 +345,7 @@ private void processChildBehaviors(Behavior behavior,FaultEvent parentFaultEvent continue; FaultEvent fEvent = FaultEvent.create(); faultEventUri = createUri(behaviorChild, faultEvent, ""); - if (faultEventRepositoryService.existsInContext(faultEventUri)) { + if (faultEventRepositoryService.existsInPersistenceContext(faultEventUri)) { fEvent = faultEventRepositoryService.findRequired(faultEventUri); } else { fEvent.setBehavior(behaviorChild);