Skip to content
Open
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 @@ -16,6 +16,7 @@ import life.qbic.business.persons.create.CreatePerson
import life.qbic.business.persons.create.CreatePersonDataSource
import life.qbic.business.persons.list.ListPersonsDataSource
import life.qbic.business.persons.search.SearchPersonDataSource
import life.qbic.business.persons.update.UpdatePersonDataSource
import life.qbic.business.products.archive.ArchiveProduct
import life.qbic.business.products.archive.ArchiveProductDataSource
import life.qbic.business.products.create.CreateProduct
Expand Down Expand Up @@ -155,6 +156,7 @@ class DependencyManager {
private CreateProjectSpaceDataSource createProjectSpaceDataSource
private ListProjectSpacesDataSource listProjectSpacesDataSource
private ListProjectsDataSource listProjectsDataSource
private UpdatePersonDataSource updatePersonDataSource

/**
* Public constructor.
Expand Down Expand Up @@ -199,6 +201,7 @@ class DependencyManager {
createPersonDataSource = personDbConnector
searchPersonDataSource = personDbConnector
listPersonsDataSource = personDbConnector
updatePersonDataSource = personDbConnector

AffiliationDbConnector affiliationDbConnector = new AffiliationDbConnector(sessionProvider)
createAffiliationDataSource = affiliationDbConnector
Expand Down Expand Up @@ -341,7 +344,7 @@ class DependencyManager {
UpdateAffiliationView updateAffiliationView = createUpdateAffiliationView()
SearchAffiliationView searchAffiliationView = new SearchAffiliationView(searchAffiliationViewModel, updateAffiliationView, archiveAffiliationController)
ArchiveAffiliationOutput archiveAffiliationOutput = new ArchiveAffiliationPresenter(viewModel, searchAffiliationViewModel, searchAffiliationView, affiliationResourcesService)
ArchiveAffiliation archiveAffiliation = new ArchiveAffiliation(archiveAffiliationOutput, updateAffiliationDataSource)
ArchiveAffiliation archiveAffiliation = new ArchiveAffiliation(archiveAffiliationOutput, updateAffiliationDataSource, updatePersonDataSource)
archiveAffiliationController.setUseCaseInput(archiveAffiliation)
return searchAffiliationView
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import life.qbic.business.persons.PersonExistsException
import life.qbic.business.persons.create.CreatePersonDataSource
import life.qbic.business.persons.list.ListPersonsDataSource
import life.qbic.business.persons.search.SearchPersonDataSource
import life.qbic.business.persons.update.UpdatePersonDataSource
import life.qbic.portal.offermanager.dataresources.database.SessionProvider
import org.hibernate.HibernateException
import org.hibernate.Session
Expand All @@ -22,110 +23,124 @@ import org.hibernate.query.Query
*
*/
@Log4j2
class PersonDbConnector implements CreatePersonDataSource, SearchPersonDataSource, ListPersonsDataSource {
class PersonDbConnector implements CreatePersonDataSource, SearchPersonDataSource, ListPersonsDataSource, UpdatePersonDataSource {

private final SessionProvider sessionProvider
private final SessionProvider sessionProvider

/**
* Uses a Hibernate session to perform the transactions with the persistence layer
* @param sessionProvider
* @since 1.3.0
*/
PersonDbConnector(SessionProvider sessionProvider) {
this.sessionProvider = sessionProvider
}
/**
* Uses a Hibernate session to perform the transactions with the persistence layer
* @param sessionProvider
* @since 1.3.0
*/
PersonDbConnector(SessionProvider sessionProvider) {
this.sessionProvider = sessionProvider
}

@Override
List<Person> listPersons() {
List<Person> persons = new ArrayList<>()
try (Session session = sessionProvider.getCurrentSession()) {
session.beginTransaction()
Query<Person> query = session.createQuery("SELECT p FROM Person p WHERE p.isActive = TRUE ")
// Print entities
persons.addAll(query.list() as List<Person>)
}
return persons
}

@Override
List<Person> listPersons() {
List<Person> persons = new ArrayList<>()
try (Session session = sessionProvider.getCurrentSession()) {
session.beginTransaction()
Query<Person> query = session.createQuery("SELECT p FROM Person p WHERE p.isActive = TRUE ")
// Print entities
persons.addAll(query.list() as List<Person>)
@Override
Person addPerson(Person person) throws DatabaseQueryException, PersonExistsException {
try (Session session = sessionProvider.openSession()) {
if (isPersonInSession(session, person)) {
throw new PersonExistsException("The person already exists.")
}
session.clear()
session.beginTransaction()
session.save(person) //sets the id of the person
session.getTransaction().commit()
return person
} catch (HibernateException e) {
log.error(e.message, e)
throw new DatabaseQueryException("An unexpected exception occurred during new affiliation creation")
}
}
return persons
}

@Override
Person addPerson(Person person) throws DatabaseQueryException, PersonExistsException {
try (Session session = sessionProvider.openSession()) {
if(isPersonInSession(session, person)) {
throw new PersonExistsException("The person already exists.")
}
session.clear()
session.beginTransaction()
session.save(person) //sets the id of the person
session.getTransaction().commit()
return person
} catch (HibernateException e) {
log.error(e.message, e)
throw new DatabaseQueryException("An unexpected exception occurred during new affiliation creation")
private static boolean isPersonInSession(Session session, Person person) {
session.beginTransaction()
Query<List<Person>> query =
session.createQuery("SELECT p FROM Person p LEFT JOIN FETCH p.affiliations WHERE p.firstName=:firstName AND p.lastName=:lastName AND p.email=:email")
query.setParameter("firstName", person.getFirstName())
query.setParameter("lastName", person.getLastName())
query.setParameter("email", person.getEmail())
boolean isInSession = !query.list().isEmpty()
session.getTransaction().commit()
return isInSession
}
}

private static boolean isPersonInSession(Session session, Person person) {
session.beginTransaction()
Query<List<Person>> query =
session.createQuery("SELECT p FROM Person p LEFT JOIN FETCH p.affiliations WHERE p.firstName=:firstName AND p.lastName=:lastName AND p.email=:email")
query.setParameter("firstName", person.getFirstName())
query.setParameter("lastName", person.getLastName())
query.setParameter("email", person.getEmail())
boolean isInSession = !query.list().isEmpty()
session.getTransaction().commit()
return isInSession
}
@Override
Person updatePerson(Person outdatedPersonData, Person updatedPersonData) throws DatabaseQueryException {

@Override
Person updatePerson(Person outdatedPersonData, Person updatedPersonData) throws DatabaseQueryException {
// the user id nees to be preserved
updatedPersonData.setUserId(outdatedPersonData.getUserId())
try (Session session = sessionProvider.openSession()) {
if (!isPersonInSession(session, outdatedPersonData)) {
throw new DatabaseQueryException("Person was not found in the database and can't be updated.")
}
session.beginTransaction()
session.clear()
outdatedPersonData.setIsActive(false)
session.update(outdatedPersonData)
session.save(updatedPersonData)
session.getTransaction().commit()
return updatedPersonData
} catch (HibernateException e) {
log.error(e.message, e)
throw new DatabaseQueryException("Unable to update person entry.")
}
}

// the user id nees to be preserved
updatedPersonData.setUserId(outdatedPersonData.getUserId())
try (Session session = sessionProvider.openSession()) {
if (!isPersonInSession(session, outdatedPersonData)) {
throw new DatabaseQueryException("Person was not found in the database and can't be updated.")
}
session.beginTransaction()
session.clear()
outdatedPersonData.setIsActive(false)
session.update(outdatedPersonData)
session.save(updatedPersonData)
session.getTransaction().commit()
return updatedPersonData
} catch (HibernateException e) {
log.error(e.message, e)
throw new DatabaseQueryException("Unable to update person entry.")
@Override
Person updatePersonAffiliations(Person person) throws DatabaseQueryException {
try (Session session = sessionProvider.getCurrentSession()) {
session.beginTransaction()
Person mergedPerson = session.<Person> merge(person) //have to un-generify groovy here
session.getTransaction().commit()
return mergedPerson
} catch (HibernateException e) {
log.error(e.message, e)
throw new DatabaseQueryException("Unable to update person affiliations.")
}
}
}

@Override
Person updatePersonAffiliations(Person person) throws DatabaseQueryException {
try (Session session = sessionProvider.getCurrentSession()) {
session.beginTransaction()
Person mergedPerson = session.<Person>merge(person) //have to un-generify groovy here
session.getTransaction().commit()
return mergedPerson
} catch (HibernateException e) {
log.error(e.message, e)
throw new DatabaseQueryException("Unable to update person affiliations.")
@Override
List<Person> findPerson(String firstName, String lastName) throws DatabaseQueryException {
List<Person> matches = new ArrayList<>()
try (Session session = sessionProvider.getCurrentSession()) {
session.beginTransaction()
Query<List<Person>> query =
session.createQuery("SELECT p FROM Person p LEFT JOIN FETCH p.affiliations WHERE p.firstName=:firstName AND p.lastName=:lastName")
query.setParameter("firstName", firstName)
query.setParameter("lastName", lastName)
matches.addAll(query.list() as List<Person>)
session.getTransaction().commit()
} catch (HibernateException e) {
log.error(e.message, e)
throw new DatabaseQueryException("An unexpected exception occurred during the search.")
}
return matches
}
}

@Override
List<Person> findPerson(String firstName, String lastName) throws DatabaseQueryException {
List<Person> matches = new ArrayList<>()
try (Session session = sessionProvider.getCurrentSession()) {
session.beginTransaction()
Query<List<Person>> query =
session.createQuery("SELECT p FROM Person p LEFT JOIN FETCH p.affiliations WHERE p.firstName=:firstName AND p.lastName=:lastName")
query.setParameter("firstName", firstName)
query.setParameter("lastName", lastName)
matches.addAll(query.list() as List<Person>)
session.getTransaction().commit()
} catch (HibernateException e) {
log.error(e.message, e)
throw new DatabaseQueryException("An unexpected exception occurred during the search.")
@Override
void removeAffiliationFromAllPersons(int affiliationId) {
try (Session session = sessionProvider.getCurrentSession()) {
session.beginTransaction()
session.createSQLQuery("DELETE FROM person_affiliation WHERE affiliation_id=:affiliationId")
.setParameter("affiliationId", affiliationId)
.executeUpdate()
session.getTransaction().commit()
} catch (HibernateException e) {
log.error(e.message, e)
throw new DatabaseQueryException("Could remove affiliation relation from person.")
}
}
return matches
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ import life.qbic.business.persons.affiliation.Affiliation

import javax.persistence.*

/**
* <b><class short description - 1 Line!></b>
*
* <p><More detailed description - When to use, what it solves, etc.></p>
*
* @since <version tag>
*/
@ToString
@Entity
@Table(name = "person")
Expand Down Expand Up @@ -42,17 +35,20 @@ class Person {
boolean isActive = true

@ManyToMany(cascade = [CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH])
@JoinTable(name = "person_affiliation", joinColumns = [ @JoinColumn(name = "person_id") ],
inverseJoinColumns = [ @JoinColumn(name = "affiliation_id")])
@JoinTable(name = "person_affiliation", joinColumns = [@JoinColumn(name = "person_id")],
inverseJoinColumns = [@JoinColumn(name = "affiliation_id")])
List<Affiliation> affiliations = []

Person() {}

@PostLoad
protected void onPostLoad() {
this.getAffiliations()
if (affiliations.isEmpty()) {
throw new IllegalStateException("Person $this was loaded without affiliations. Illegal State: A person must have at least one affiliation.")
// We need to set the affiliations to an empty list explicitly if the proxy is empty
// otherwise an persistence bag remains there forever, which will lead
// to LazyInitialisationExceptions
if (this.affiliations.isEmpty()) {
affiliations = []
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import life.qbic.business.logging.Logging
import life.qbic.business.persons.affiliation.Affiliation
import life.qbic.business.persons.affiliation.AffiliationExistsException
import life.qbic.business.persons.affiliation.update.UpdateAffiliationDataSource
import life.qbic.business.persons.update.UpdatePersonDataSource

/**
* This class implements the Archive Affiliations use case.
Expand All @@ -22,13 +23,17 @@ class ArchiveAffiliation implements ArchiveAffiliationInput {

private final Logging log = Logger.getLogger(this.class)

private final UpdatePersonDataSource updatePersonDataSource

/**
* Creates a use case interactor for archiving an affiliation in the provided customer database
* @param dataSource the gateway to the database
* @param output an output to publish the results to
*/
ArchiveAffiliation(ArchiveAffiliationOutput output, UpdateAffiliationDataSource dataSource) {
ArchiveAffiliation(ArchiveAffiliationOutput output, UpdateAffiliationDataSource dataSource,
UpdatePersonDataSource updatePersonDataSource) {
this.dataSource = dataSource
this.updatePersonDataSource = updatePersonDataSource
this.output = output
}

Expand All @@ -38,6 +43,9 @@ class ArchiveAffiliation implements ArchiveAffiliationInput {
affiliation.archive()
try {
dataSource.updateAffiliation(affiliation)
// Since the affiliation was marked as archived, we need to remove
// all existing relations from persons with this affiliation
updatePersonDataSource.removeAffiliationFromAllPersons(affiliation.getId())
output.affiliationArchived(affiliation)

log.info("Successfully archived affiliation " + affiliation.getOrganization())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package life.qbic.business.persons.update;

/**
* Interface for updating person entities.
*
* @since 1.5.0
*/
public interface UpdatePersonDataSource {

/**
* Removes an affiliation relation from all persons.
*
* @param affiliationId the affiliations unique id
* @since 1.5.0
*/
void removeAffiliationFromAllPersons(int affiliationId);

}