Skip to content

Commit

Permalink
Merge pull request #87 from kbss-cvut/74-user-context
Browse files Browse the repository at this point in the history
Persist User entity in separate Person context
  • Loading branch information
blcham authored Dec 27, 2024
2 parents c70324f + 7df7ff8 commit 6b882a8
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/main/java/cz/cvut/kbss/study/persistence/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

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.study.exception.PersistenceException;
import cz.cvut.kbss.study.model.Institution;
import cz.cvut.kbss.study.model.User;
import cz.cvut.kbss.study.model.Vocabulary;
import cz.cvut.kbss.study.util.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;

Expand All @@ -24,6 +28,26 @@ public UserDao(EntityManager em) {
super(User.class, em);
}

private Descriptor getDescriptor(URI ctx){
Descriptor descriptor = new EntityDescriptor(ctx);
EntityType<User> et = em.getMetamodel().entity(User.class);
descriptor.addAttributeContext(et.getAttribute("institution"), null);
return descriptor;
}

@Override
public void persist(User entity) {
Objects.requireNonNull(entity);
try {
entity.generateUri();
Descriptor descriptor = getDescriptor(new URI(Vocabulary.s_c_Person));
em.persist(entity, descriptor);
} catch (RuntimeException | URISyntaxException e) {
LOG.error("Error when persisting entity.", e);
throw new PersistenceException(e);
}
}

public User findByUsername(String username) {
Objects.requireNonNull(username);
try {
Expand Down

0 comments on commit 6b882a8

Please sign in to comment.