Importers/Exporters for relation entities of SciProject

master
Jens Pelzetter 2022-11-08 19:43:10 +01:00
parent 261704fa47
commit e9262e956a
10 changed files with 417 additions and 131 deletions

View File

@ -5,11 +5,10 @@
*/
package org.scientificcms.contenttypes.sciproject;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.hibernate.envers.Audited;
import org.libreccm.core.CcmObjects;
import org.libreccm.imexport.Exportable;
import org.librecms.assets.ContactableEntity;
import java.io.Serializable;
@ -33,11 +32,7 @@ import static org.scientificcms.contenttypes.sciproject.SciProjectConstants.*;
@Entity(name = "ProjectContact")
@Audited
@Table(name = "PROJECT_CONTACTS", schema = DB_SCHEMA)
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "uuid"
)
public class Contact implements Serializable {
public class Contact implements Exportable, Serializable {
private static final long serialVersionUID = 1L;
@ -46,6 +41,10 @@ public class Contact implements Serializable {
@GeneratedValue(strategy = GenerationType.AUTO)
private long contactId;
@Id
@Column(name = "uuid", unique = true, nullable = false)
private String uuid;
@Column(name = "CONTACT_TYPE", length = 255, nullable = true)
private String contactType;
@ -66,10 +65,18 @@ public class Contact implements Serializable {
return contactId;
}
public void setContactId(final long contactId) {
protected void setContactId(final long contactId) {
this.contactId = contactId;
}
public String getUuid() {
return uuid;
}
protected void setUuid(final String uuid) {
this.uuid = uuid;
}
public String getContactType() {
return contactType;
}
@ -106,6 +113,7 @@ public class Contact implements Serializable {
public int hashCode() {
int hash = 7;
hash = 41 * hash + (int) (contactId ^ (contactId >>> 32));
hash = 41 * hash + Objects.hashCode(uuid);
hash = 41 * hash + Objects.hashCode(contactType);
hash = 41 * hash + (int) (order ^ (order >>> 32));
hash = 41 * hash + CcmObjects.hashCodeUsingUuid(project);
@ -115,7 +123,6 @@ public class Contact implements Serializable {
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
@ -132,6 +139,9 @@ public class Contact implements Serializable {
if (contactId != other.getContactId()) {
return false;
}
if (!Objects.equals(uuid, other.getUuid())) {
return false;
}
if (!Objects.equals(contactType, other.getContactType())) {
return false;
}
@ -145,56 +155,60 @@ public class Contact implements Serializable {
}
public boolean canEqual(final Object obj) {
return obj instanceof Contact;
}
@Override
public final String toString() {
return toString("");
}
public String toString(final String data) {
final String projectStr;
if (project == null) {
projectStr = "";
} else {
projectStr = String.format("objectId = %d,"
+ "uuid = \"%s\", "
+ "name = \"%s\"",
project.getObjectId(),
project.getUuid(),
project.getDisplayName());
projectStr = String.format(
"objectId = %d,"
+ "uuid = \"%s\", "
+ "name = \"%s\"",
project.getObjectId(),
project.getUuid(),
project.getDisplayName()
);
}
final String contactableStr;
if (contactable == null) {
contactableStr = "";
} else {
contactableStr = String.format("objectId = %d,"
+ "uuid = \"%s\", "
+ "name = \"%s\"",
contactable.getObjectId(),
contactable.getUuid(),
contactable.getDisplayName());
contactableStr = String.format(
"objectId = %d,"
+ "uuid = \"%s\", "
+ "name = \"%s\"",
contactable.getObjectId(),
contactable.getUuid(),
contactable.getDisplayName()
);
}
return String.format("%s{ "
+ "contactId = %d, "
+ "contactType = \"%s\", "
+ "order = %d, "
+ "project = { %s } "
+ "contactableUuid = %s%s"
+ " }",
super.toString(),
contactId,
contactType,
order,
projectStr,
contactableStr,
data
return String.format(
"%s{ "
+ "contactId = %d, "
+ "uuid = %s, "
+ "contactType = \"%s\", "
+ "order = %d, "
+ "project = { %s } "
+ "contactableUuid = %s%s"
+ " }",
super.toString(),
contactId,
uuid,
contactType,
order,
projectStr,
contactableStr,
data
);
}

View File

@ -0,0 +1,66 @@
package org.scientificcms.contenttypes.sciproject;
import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Processes;
import org.librecms.assets.Organization;
import org.librecms.assets.Person;
import java.util.Objects;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
/**
* Importer/Exporter for {@link Contact} entities.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@Processes(Contact.class)
public class ContactImExporter
extends AbstractEntityImExporter<Contact> {
@Inject
private ContactRepository contactRepo;
@PostConstruct
@Override
protected void init() {
addRequiredEntities(
Set.of(
SciProject.class,
Organization.class,
Person.class
)
);
}
@Override
public Class<Contact> getEntityClass() {
return Contact.class;
}
@Override
@Transactional(Transactional.TxType.REQUIRED)
protected void saveImportedEntity(final Contact entity) {
contactRepo.save(entity);
}
@Override
protected Contact reloadEntity(final Contact entity) {
return contactRepo
.findById(entity.getContactId())
.orElseThrow(
() -> new IllegalArgumentException(
String.format(
"Contact entity %s was not found in the database.",
Objects.toString(entity)
)
)
);
}
}

View File

@ -2,6 +2,8 @@ package org.scientificcms.contenttypes.sciproject;
import org.libreccm.auditing.AbstractAuditedEntityRepository;
import java.util.UUID;
import javax.enterprise.context.RequestScoped;
/**
@ -38,5 +40,10 @@ public class ContactRepository
public boolean isNew(final Contact contact) {
return contact.getContactId() == 0;
}
@Override
public void initNewEntity(final Contact contact) {
contact.setUuid(UUID.randomUUID().toString());
}
}

View File

@ -1,8 +1,3 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.scientificcms.contenttypes.sciproject;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
@ -10,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.hibernate.envers.Audited;
import org.libreccm.core.CcmObjects;
import org.libreccm.imexport.Exportable;
import org.librecms.assets.Person;
import java.io.Serializable;
@ -39,7 +35,7 @@ import static org.scientificcms.contenttypes.sciproject.SciProjectConstants.*;
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "uuid"
)
public class Membership implements Serializable {
public class Membership implements Exportable, Serializable {
private static final long serialVersionUID = 1L;
@ -48,6 +44,9 @@ public class Membership implements Serializable {
@GeneratedValue(strategy = GenerationType.AUTO)
private long membershipId;
@Column(name = "uuid", unique = true, nullable = false)
private String uuid;
@Column(name = "MEMBER_ROLE", length = 255, nullable = true)
private String role;
@ -73,6 +72,15 @@ public class Membership implements Serializable {
this.membershipId = membershipId;
}
@Override
public String getUuid() {
return uuid;
}
protected void setUuid(final String uuid) {
this.uuid = uuid;
}
public String getRole() {
return role;
}
@ -110,6 +118,7 @@ public class Membership implements Serializable {
int hash = 7;
hash = 37 * hash
+ (int) (membershipId ^ (membershipId >>> 32));
hash = 37 * hash + Objects.hashCode(uuid);
hash = 37 * hash + Objects.hashCode(role);
hash = 37 * hash + Objects.hashCode(status);
hash = 37 * hash + CcmObjects.hashCodeUsingUuid(project);
@ -119,7 +128,6 @@ public class Membership implements Serializable {
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
@ -137,6 +145,9 @@ public class Membership implements Serializable {
if (membershipId != other.getMembershipId()) {
return false;
}
if (!Objects.equals(uuid, other.getUuid())) {
return false;
}
if (!Objects.equals(role, other.getRole())) {
return false;
}
@ -151,64 +162,68 @@ public class Membership implements Serializable {
}
public boolean canEqual(final Object obj) {
return obj instanceof Membership;
}
@Override
public final String toString() {
return toString("");
}
public String toString(final String data) {
final String projectStr;
if (project == null) {
projectStr = "";
} else {
projectStr = String.format("objectId = %d,"
+ "uuid = \"%s\", "
+ "name = \"%s\"",
project.getObjectId(),
project.getUuid(),
project.getDisplayName());
projectStr = String.format(
"objectId = %d,"
+ "uuid = \"%s\", "
+ "name = \"%s\"",
project.getObjectId(),
project.getUuid(),
project.getDisplayName()
);
}
final String memberStr;
if (member == null) {
memberStr = "";
} else {
memberStr = String.format("objectId = %d, "
+ "uuid = \"%s\", "
+ "name = \"%s\", "
+ "surname = \"%s\", "
+ "givenName = \"%s\", "
+ "prefix = \"%s\", "
+ "suffix = \"%s\"",
member.getObjectId(),
member.getUuid(),
member.getDisplayName(),
member.getPersonName().getSurname(),
member.getPersonName().getGivenName(),
member.getPersonName().getPrefix(),
member.getPersonName().getSuffix());
memberStr = String.format(
"objectId = %d, "
+ "uuid = \"%s\", "
+ "name = \"%s\", "
+ "surname = \"%s\", "
+ "givenName = \"%s\", "
+ "prefix = \"%s\", "
+ "suffix = \"%s\"",
member.getObjectId(),
member.getUuid(),
member.getDisplayName(),
member.getPersonName().getSurname(),
member.getPersonName().getGivenName(),
member.getPersonName().getPrefix(),
member.getPersonName().getSuffix()
);
}
return String.format("%s{ "
+ "membershipId = %d, "
+ "role = \"%s\", "
+ "status = \"%s\","
+ "project = { %s }, "
+ "member = { %s }%s"
+ " }",
super.toString(),
membershipId,
role,
Objects.toString(status),
projectStr,
memberStr,
data
return String.format(
"%s{ "
+ "membershipId = %d, "
+ "uuid = %s, "
+ "role = \"%s\", "
+ "status = \"%s\","
+ "project = { %s }, "
+ "member = { %s }%s"
+ " }",
super.toString(),
membershipId,
uuid,
role,
Objects.toString(status),
projectStr,
memberStr,
data
);
}

View File

@ -0,0 +1,60 @@
package org.scientificcms.contenttypes.sciproject;
import org.libreccm.imexport.AbstractEntityImExporter;
import org.librecms.assets.Person;
import java.util.Objects;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.transaction.Transactional;
/**
* Importer/Exporter for {@link Membership} entities.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class MembershipImExporter
extends AbstractEntityImExporter<Membership> {
@Inject
private MembershipRepository membershipRepo;
@PostConstruct
@Override
protected void init() {
addRequiredEntities(
Set.of(
SciProject.class,
Person.class
)
);
}
@Override
public Class<Membership> getEntityClass() {
return Membership.class;
}
@Override
@Transactional(Transactional.TxType.REQUIRED)
protected void saveImportedEntity(final Membership entity) {
membershipRepo.save(entity);
}
@Override
protected Membership reloadEntity(final Membership entity) {
return membershipRepo
.findById(entity.getMembershipId())
.orElseThrow(
() -> new IllegalArgumentException(
String.format(
"Membership entity %s was not found in the database.",
Objects.toString(entity)
)
)
);
}
}

View File

@ -1,8 +1,4 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.scientificcms.contenttypes.sciproject;
/**

View File

@ -1,15 +1,9 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.scientificcms.contenttypes.sciproject;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.hibernate.envers.Audited;
import org.libreccm.core.CcmObjects;
import org.libreccm.imexport.Exportable;
import org.librecms.assets.Organization;
import java.io.Serializable;
@ -33,11 +27,7 @@ import static org.scientificcms.contenttypes.sciproject.SciProjectConstants.*;
@Entity
@Audited
@Table(name = "SPONSORING", schema = DB_SCHEMA)
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "uuid"
)
public class Sponsoring implements Serializable {
public class Sponsoring implements Exportable, Serializable {
private static final long serialVersionUID = 1L;
@ -46,6 +36,9 @@ public class Sponsoring implements Serializable {
@GeneratedValue(strategy = GenerationType.AUTO)
private long sponsoringId;
@Column(name = "uuid", unique = true, nullable = false)
private String uuid;
@Column(name = "FUNDING_CODE", length = 512, nullable = true)
private String fundingCode;
@ -70,6 +63,15 @@ public class Sponsoring implements Serializable {
this.sponsoringId = sponsoringId;
}
@Override
public String getUuid() {
return uuid;
}
protected void setUuid(final String uuid) {
this.uuid = uuid;
}
public String getFundingCode() {
return fundingCode;
}
@ -104,10 +106,10 @@ public class Sponsoring implements Serializable {
@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash
+ (int) (sponsoringId ^ (sponsoringId >>> 32));
hash = 53 * hash + Objects.hashCode(uuid);
hash = 53 * hash + Objects.hashCode(fundingCode);
hash = 53 * hash + (int) (order ^ (order >>> 32));
hash = 53 * hash + CcmObjects.hashCodeUsingUuid(project);
@ -117,7 +119,6 @@ public class Sponsoring implements Serializable {
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
@ -134,6 +135,9 @@ public class Sponsoring implements Serializable {
if (sponsoringId != other.getSponsoringId()) {
return false;
}
if (!Objects.equals(uuid, other.getUuid())) {
return false;
}
if (!Objects.equals(fundingCode, other.getFundingCode())) {
return false;
}
@ -145,16 +149,13 @@ public class Sponsoring implements Serializable {
}
return order == other.getOrder();
}
public boolean canEqual(final Object obj) {
return obj instanceof Sponsoring;
}
@Override
public final String toString() {
return toString("");
}
@ -164,40 +165,48 @@ public class Sponsoring implements Serializable {
if (project == null) {
projectStr = "";
} else {
projectStr = String.format("objectId = %d,"
+ "uuid = \"%s\", "
+ "name = \"%s\"",
project.getObjectId(),
project.getUuid(),
project.getDisplayName());
projectStr = String.format(
"objectId = %d,"
+ "uuid = \"%s\", "
+ "name = \"%s\"",
project.getObjectId(),
project.getUuid(),
project.getDisplayName()
);
}
final String sponsorStr;
if (sponsor == null) {
sponsorStr = "";
} else {
sponsorStr = String.format("objectId = %d, "
+ "uuid = \"%s\", "
+ "name = \"%s\"",
sponsor.getObjectId(),
sponsor.getUuid(),
sponsor.getDisplayName());
sponsorStr = String.format(
"objectId = %d, "
+ "uuid = \"%s\", "
+ "name = \"%s\"",
sponsor.getObjectId(),
sponsor.getUuid(),
sponsor.getDisplayName()
);
}
return String.format("%s{ "
+ "sponsoringId = %d, "
+ "fundingCode = \"%s\", "
+ "order = %d, "
+ "project = { %s }, "
+ "sponsor = { %s }%s"
+ " }",
super.toString(),
sponsoringId,
fundingCode,
order,
projectStr,
sponsorStr,
data);
return String.format(
"%s{ "
+ "sponsoringId = %d, "
+ "uuid = %s, "
+ "fundingCode = \"%s\", "
+ "order = %d, "
+ "project = { %s }, "
+ "sponsor = { %s }%s"
+ " }",
super.toString(),
sponsoringId,
uuid,
fundingCode,
order,
projectStr,
sponsorStr,
data
);
}
}

View File

@ -0,0 +1,61 @@
package org.scientificcms.contenttypes.sciproject;
import org.libreccm.imexport.AbstractEntityImExporter;
import org.librecms.assets.Organization;
import org.librecms.assets.Person;
import java.util.Objects;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.transaction.Transactional;
/**
* Importer/Exporter for {@link Membership} entities.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class SponsoringImExporter
extends AbstractEntityImExporter<Sponsoring> {
@Inject
private SponsoringRepository sponsoringRepo;
@PostConstruct
@Override
protected void init() {
addRequiredEntities(
Set.of(
SciProject.class,
Organization.class
)
);
}
@Override
public Class<Sponsoring> getEntityClass() {
return Sponsoring.class;
}
@Override
@Transactional(Transactional.TxType.REQUIRED)
protected void saveImportedEntity(final Sponsoring entity) {
sponsoringRepo.save(entity);
}
@Override
protected Sponsoring reloadEntity(final Sponsoring entity) {
return sponsoringRepo
.findById(entity.getSponsoringId())
.orElseThrow(
() -> new IllegalArgumentException(
String.format(
"Membership entity %s was not found in the database.",
Objects.toString(entity)
)
)
);
}
}

View File

@ -0,0 +1,29 @@
alter table SCI_TYPES_PROJECT.PROJECT_CONTACTS
add column uuid varchar(255);
update SCI_TYPES_PROJECT.PROJECT_CONTACTS set uuid = random_uuid();
alter table SCI_TYPES_PROJECT.PROJECT_CONTACTS
alter column uuid set not null;
alter table SCI_TYPES_PROJECT.PROJECT_CONTACTS
add constraint UK_9uf8mpe319no9o4pgv3jtdm9d unique (uuid);
alter table SCI_TYPES_PROJECT.PROJECT_CONTACTS_AUD
add column uuid varchar(255);
alter table SCI_TYPES_PROJECT.PROJECT_MEMBERSHIPS
add column uuid varchar(255);
update SCI_TYPES_PROJECT.PROJECT_MEMBERSHIPS set uuid = random_uuid();
alter table SCI_TYPES_PROJECT.PROJECT_MEMBERSHIPS
alter column uuid set not null;
alter table SCI_TYPES_PROJECT.PROJECT_MEMBERSHIPS
add constraint UK_3ba40n70bpp8wlhhulmrr6e5h unique (uuid);
alter table SCI_TYPES_PROJECT.PROJECT_MEMBERSHIPS_AUD
add column uuid varchar(255);
alter table SCI_TYPES_PROJECT.SPONSORING
add column uuid varchar(255);
update SCI_TYPES_PROJECT.SPONSORING set uuid = random_uuid();
alter table SCI_TYPES_PROJECT.SPONSORING
alter column uuid set not null;
alter table SCI_TYPES_PROJECT.SPONSORING
add constraint UK_b6rte13jrs2se6xad7eenr6mr unique(uuid);
alter table SCI_TYPES_PROJECT.SPONSORING_AUD
add column uuid varchar(255);

View File

@ -0,0 +1,29 @@
alter table SCI_TYPES_PROJECT.PROJECT_CONTACTS
add column uuid varchar(255);
update SCI_TYPES_PROJECT.PROJECT_CONTACTS set uuid = gen_random_uuid();
alter table SCI_TYPES_PROJECT.PROJECT_CONTACTS
alter column uuid set not null;
alter table SCI_TYPES_PROJECT.PROJECT_CONTACTS
add constraint UK_9uf8mpe319no9o4pgv3jtdm9d unique (uuid);
alter table SCI_TYPES_PROJECT.PROJECT_CONTACTS_AUD
add column uuid varchar(255);
alter table SCI_TYPES_PROJECT.PROJECT_MEMBERSHIPS
add column uuid varchar(255);
update SCI_TYPES_PROJECT.PROJECT_MEMBERSHIPS set uuid = gen_random_uuid();
alter table SCI_TYPES_PROJECT.PROJECT_MEMBERSHIPS
alter column uuid set not null;
alter table SCI_TYPES_PROJECT.PROJECT_MEMBERSHIPS
add constraint UK_3ba40n70bpp8wlhhulmrr6e5h unique (uuid);
alter table SCI_TYPES_PROJECT.PROJECT_MEMBERSHIPS_AUD
add column uuid varchar(255);
alter table SCI_TYPES_PROJECT.SPONSORING
add column uuid varchar(255);
update SCI_TYPES_PROJECT.SPONSORING set uuid = gen_random_uuid();
alter table SCI_TYPES_PROJECT.SPONSORING
alter column uuid set not null;
alter table SCI_TYPES_PROJECT.SPONSORING
add constraint UK_b6rte13jrs2se6xad7eenr6mr unique(uuid);
alter table SCI_TYPES_PROJECT.SPONSORING_AUD
add column uuid varchar(255);