From ea0b9d55ac71ca020ae565c343e92ab17f03a472 Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Fri, 2 Aug 2019 20:36:49 +0200 Subject: [PATCH] =?UTF-8?q?Getter,Setter=20etc=20f=C3=BCr=20SciProject=20a?= =?UTF-8?q?ssociation=20classes,=20SciProjectRepository,=20SciProjectManag?= =?UTF-8?q?er=20(partly=20finished)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../contenttypes/sciproject/Contact.java | 154 +++++++++++++++++- .../contenttypes/sciproject/Membership.java | 62 ++++++- .../contenttypes/sciproject/SciProject.java | 127 ++++++++++++++- .../sciproject/SciProjectMananger.java | 60 +++++++ .../sciproject/SciProjectRepository.java | 94 +++++++++++ 5 files changed, 479 insertions(+), 18 deletions(-) create mode 100644 sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/SciProjectMananger.java create mode 100644 sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/SciProjectRepository.java diff --git a/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/Contact.java b/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/Contact.java index 2f5743b..bc6a9cd 100644 --- a/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/Contact.java +++ b/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/Contact.java @@ -6,9 +6,11 @@ package org.scientificcms.contenttypes.sciproject; import org.hibernate.envers.Audited; +import org.libreccm.core.CcmObjects; import org.librecms.assets.ContactableEntity; import java.io.Serializable; +import java.util.Objects; import javax.persistence.Column; import javax.persistence.Entity; @@ -28,27 +30,163 @@ import static org.scientificcms.contenttypes.sciproject.SciProjectConstants.*; @Entity @Audited @Table(name = "PROJECT_CONTACTS", schema = DB_SCHEMA) -public class Contact implements Serializable{ - +public class Contact implements Serializable { + private static final long serialVersionUID = 1L; - + @Id @Column(name = "CONTACT_ID") @GeneratedValue(strategy = GenerationType.AUTO) private long contactId; - + @Column(name = "CONTACT_TYPE", length = 255, nullable = true) private String contactType; - + @Column(name = "CONTACT_ORDER") private long order; - + @ManyToOne @JoinColumn(name = "PROJECT_ID") private SciProject project; - + @ManyToOne @JoinColumn(name = "CONTACTABLE_ID") private ContactableEntity contactable; - + + public long getContactId() { + return contactId; + } + + public void setContactId(final long contactId) { + this.contactId = contactId; + } + + public String getContactType() { + return contactType; + } + + public void setContactType(final String contactType) { + this.contactType = contactType; + } + + public long getOrder() { + return order; + } + + public void setOrder(final long order) { + this.order = order; + } + + public SciProject getProject() { + return project; + } + + protected void setProject(final SciProject project) { + this.project = project; + } + + public ContactableEntity getContactable() { + return contactable; + } + + protected void setContactable(final ContactableEntity contactable) { + this.contactable = contactable; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 41 * hash + (int) (contactId ^ (contactId >>> 32)); + hash = 41 * hash + Objects.hashCode(contactType); + hash = 41 * hash + (int) (order ^ (order >>> 32)); + hash = 41 * hash + CcmObjects.hashCodeUsingUuid(project); + hash = 41 * hash + CcmObjects.hashCodeUsingUuid(contactable); + return hash; + } + + @Override + public boolean equals(final Object obj) { + + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof Contact)) { + return false; + } + final Contact other = (Contact) obj; + if (!other.canEqual(this)) { + return false; + } + if (contactId != other.getContactId()) { + return false; + } + if (!Objects.equals(contactType, other.getContactType())) { + return false; + } + if (!CcmObjects.equalsUsingUuid(project, other.getProject())) { + return false; + } + if (!CcmObjects.equalsUsingUuid(contactable, other.getContactable())) { + return false; + } + return order == other.getOrder(); + } + + 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()); + } + + final String contactableStr; + if (contactable == null) { + contactableStr = ""; + } else { + 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 + ); + } + } diff --git a/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/Membership.java b/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/Membership.java index 629c2bd..df9c6d6 100644 --- a/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/Membership.java +++ b/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/Membership.java @@ -138,10 +138,7 @@ public class Membership implements Serializable { if (!CcmObjects.equalsUsingUuid(member, other.getMember())) { return false; } - if (status != other.getStatus()) { - return false; - } - return true; + return status == other.getStatus(); } public boolean canEqual(final Object obj) { @@ -149,4 +146,61 @@ public class Membership implements Serializable { 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()); + } + + 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()); + } + + 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 + ); + } + } diff --git a/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/SciProject.java b/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/SciProject.java index a2c5829..9690958 100644 --- a/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/SciProject.java +++ b/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/SciProject.java @@ -12,7 +12,6 @@ import javax.persistence.Entity; import org.hibernate.envers.Audited; import org.libreccm.l10n.LocalizedString; -import org.librecms.assets.Person; import org.librecms.contentsection.ContentItem; import org.librecms.contenttypes.AuthoringKit; import org.librecms.contenttypes.AuthoringStep; @@ -20,13 +19,18 @@ import org.librecms.contenttypes.ContentTypeDescription; import java.io.Serializable; import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import javax.persistence.AssociationOverride; +import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Embedded; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; import javax.persistence.OneToMany; import javax.persistence.Table; @@ -55,6 +59,37 @@ import static org.scientificcms.contenttypes.sciproject.SciProjectConstants.*; + ".description", order = 1) }) +@NamedQueries({ + @NamedQuery( + name = "SciProject.findWhereBeginIsBefore", + query = "SELECT p FROM SciProject p WHERE p.begin < :date" + ), + @NamedQuery( + name = "SciProject.findWhereBeginIs", + query = "SELECT p FROM SciProject p WHERE p.begin = :date" + ), + @NamedQuery( + name = "SciProject.findWhereBeginIsAfter", + query = "SELECT p FROM SciProject p WHERE p.begin > :date" + ), + @NamedQuery( + name = "SciProject.findWhereEndIsBefore", + query = "SELECT p FROM SciProject p WHERE p.end < :date" + ), + @NamedQuery( + name = "SciProject.findWhereEndIs", + query = "SELECT p FROM SciProject p WHERE p.end = :date" + ), + @NamedQuery( + name = "SciProject.findWhereEndIsAfter", + query = "SELECT p FROM SciProject p WHERE p.end > :date" + ), + @NamedQuery( + name = "SciProject.findByMember", + query = "SELECT p FROM SciProject p JOIN Membership m " + + "WHERE m.member = :member" + ) +}) public class SciProject extends ContentItem implements Serializable { private static final long serialVersionUID = 1L; @@ -85,7 +120,7 @@ public class SciProject extends ContentItem implements Serializable { @JoinColumn(name = "OBJECT_ID") }) ) - private LocalizedString description; + private LocalizedString projectDescription; @Embedded @AssociationOverride( @@ -108,11 +143,91 @@ public class SciProject extends ContentItem implements Serializable { }) ) private LocalizedString fundingVolume; - - @OneToMany(mappedBy = "project") + + @OneToMany(mappedBy = "project", cascade = CascadeType.ALL) private List contacts; - - @OneToMany(mappedBy = "project") + + @OneToMany(mappedBy = "project", cascade = CascadeType.ALL) private List members; + public LocalDate getBegin() { + return begin; + } + + public void setBegin(final LocalDate begin) { + this.begin = begin; + } + + public LocalDate getEnd() { + return end; + } + + public void setEnd(final LocalDate end) { + this.end = end; + } + + public LocalizedString getShortDescription() { + return shortDescription; + } + + public void setShortDescription(final LocalizedString shortDescription) { + this.shortDescription = shortDescription; + } + + public LocalizedString getProjectDescription() { + return projectDescription; + } + + public void setProjectDescription(final LocalizedString projectDescription) { + this.projectDescription = projectDescription; + } + + public LocalizedString getFunding() { + return funding; + } + + public void setFunding(final LocalizedString funding) { + this.funding = funding; + } + + public LocalizedString getFundingVolume() { + return fundingVolume; + } + + public void setFundingVolume(final LocalizedString fundingVolume) { + this.fundingVolume = fundingVolume; + } + + public List getContacts() { + return Collections.unmodifiableList(contacts); + } + + protected void addContact(final Contact contact) { + contacts.add(contact); + } + + protected void removeContact(final Contact contact) { + contacts.remove(contact); + } + + protected void setContacts(final List contacts) { + this.contacts = new ArrayList<>(contacts); + } + + public List getMembers() { + return Collections.unmodifiableList(members); + } + + protected void addMember(final Membership membership) { + members.add(membership); + } + + protected void removeMembership(final Membership membership) { + members.remove(membership); + } + + protected void setMembers(final List members) { + this.members = new ArrayList<>(members); + } + } diff --git a/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/SciProjectMananger.java b/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/SciProjectMananger.java new file mode 100644 index 0000000..0cfbed7 --- /dev/null +++ b/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/SciProjectMananger.java @@ -0,0 +1,60 @@ +/* + * 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 org.librecms.assets.ContactableEntity; +import org.librecms.assets.Person; + +import java.io.Serializable; +import java.util.Objects; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.transaction.Transactional; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +public class SciProjectMananger implements Serializable { + + private static final long serialVersionUID = 1L; + + @Inject + private SciProjectRepository sciProjectRepository; + + @Transactional(Transactional.TxType.REQUIRED) + public void addcontact(final ContactableEntity contactable, + final SciProject toProject, + final String withType) { + + Objects.requireNonNull(contactable, + "Can't null as Contact to a SciProject."); + Objects.requireNonNull(toProject, + "Can't add a Contact to a project null."); + + final Contact contact = new Contact(); + contact.setContactable(contactable); + contact.setProject(toProject); + contact.setContactType(withType); + contact.setOrder(toProject.getContacts().size()); + + toProject.addContact(contact); + + sciProjectRepository.save(toProject); + } + + @Transactional(Transactional.TxType.REQUIRED) + public void addMember(final Person person, + final SciProject toProject, + final String withRole, + final MembershipStatus withStatus) { + + throw new UnsupportedOperationException("ToDo"); + } + +} diff --git a/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/SciProjectRepository.java b/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/SciProjectRepository.java new file mode 100644 index 0000000..b5a2fd0 --- /dev/null +++ b/sci-types-project/src/main/java/org/scientificcms/contenttypes/sciproject/SciProjectRepository.java @@ -0,0 +1,94 @@ +/* + * 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 org.librecms.assets.Person; +import org.librecms.contentsection.ContentItemRepository; + +import java.time.LocalDate; +import java.util.List; + +import javax.enterprise.context.RequestScoped; +import javax.transaction.Transactional; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +public class SciProjectRepository extends ContentItemRepository { + + private static final long serialVersionUID = 1L; + + @Transactional(Transactional.TxType.REQUIRED) + public List findWhereBeginIsBefore(final LocalDate date) { + + return getEntityManager() + .createNamedQuery("SciProject.findWhereBeginIsBefore", + SciProject.class) + .setParameter("date", date) + .getResultList(); + + } + + @Transactional(Transactional.TxType.REQUIRED) + public List findWhereBeginIs(final LocalDate date) { + + return getEntityManager() + .createNamedQuery("SciProject.findWhereBeginIs", SciProject.class) + .setParameter("date", date) + .getResultList(); + } + + @Transactional(Transactional.TxType.REQUIRED) + public List findWhereBeginIsAfter(final LocalDate date) { + + return getEntityManager() + .createNamedQuery("SciProject.findWhereBeginIsAfter", + SciProject.class) + .setParameter("date", date) + .getResultList(); + } + + @Transactional(Transactional.TxType.REQUIRED) + public List findWhereEndIsBefore(final LocalDate date) { + + return getEntityManager() + .createNamedQuery("SciProject.findWhereEndIsBefore", + SciProject.class) + .setParameter("date", date) + .getResultList(); + } + + @Transactional(Transactional.TxType.REQUIRED) + public List findWhereEndIs(final LocalDate date) { + return getEntityManager() + .createNamedQuery("SciProject.findWhereEndIs", + SciProject.class) + .setParameter("date", date) + .getResultList(); + } + + @Transactional(Transactional.TxType.REQUIRED) + public List findWhereEndIsAfter(final LocalDate date) { + + return getEntityManager() + .createNamedQuery("SciProject.findWhereEndIsAfter", + SciProject.class) + .setParameter("date", date) + .getResultList(); + } + + @Transactional(Transactional.TxType.REQUIRED) + public List findByMember(final Person member) { + + return getEntityManager() + .createNamedQuery("SciProject.findByMember", SciProject.class) + .setParameter("member", member) + .getResultList(); + } + +}