From 06953a94a66a39133a6ca94f694ae88f8b6fe07b Mon Sep 17 00:00:00 2001 From: Daniil Palagin Date: Wed, 25 Dec 2024 14:03:31 +0100 Subject: [PATCH] [#82] Persist ActionHistory entity in separate Person context --- .../persistence/dao/ActionHistoryDao.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main/java/cz/cvut/kbss/study/persistence/dao/ActionHistoryDao.java b/src/main/java/cz/cvut/kbss/study/persistence/dao/ActionHistoryDao.java index 928126bb..8371bd44 100644 --- a/src/main/java/cz/cvut/kbss/study/persistence/dao/ActionHistoryDao.java +++ b/src/main/java/cz/cvut/kbss/study/persistence/dao/ActionHistoryDao.java @@ -2,17 +2,23 @@ import cz.cvut.kbss.jopa.exceptions.NoResultException; import cz.cvut.kbss.jopa.model.EntityManager; +import cz.cvut.kbss.jopa.model.descriptors.Descriptor; +import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor; +import cz.cvut.kbss.jopa.model.metamodel.EntityType; import cz.cvut.kbss.jopa.model.query.TypedQuery; +import cz.cvut.kbss.study.exception.PersistenceException; import cz.cvut.kbss.study.model.ActionHistory; import cz.cvut.kbss.study.model.User; import cz.cvut.kbss.study.model.Vocabulary; import cz.cvut.kbss.study.util.Constants; +import cz.cvut.kbss.study.util.IdentificationUtils; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Repository; import java.net.URI; +import java.net.URISyntaxException; import java.util.Objects; @Repository @@ -22,6 +28,26 @@ public ActionHistoryDao(EntityManager em) { super(ActionHistory.class, em); } + private Descriptor getDescriptor(URI ctx){ + Descriptor descriptor = new EntityDescriptor(ctx); + EntityType et = em.getMetamodel().entity(ActionHistory.class); + descriptor.addAttributeContext(et.getAttribute("author"), null); + return descriptor; + } + + @Override + public void persist(ActionHistory entity) { + Objects.requireNonNull(entity); + try { + entity.setKey(IdentificationUtils.generateKey()); + Descriptor descriptor = getDescriptor(new URI(Vocabulary.s_c_action_history)); + em.persist(entity, descriptor); + } catch (RuntimeException | URISyntaxException e) { + LOG.error("Error when persisting entity.", e); + throw new PersistenceException(e); + } + } + public ActionHistory findByKey(String key) { Objects.requireNonNull(key); try {