From 9e72a3b58bcdac2d72c69c30b99725d53baaab9b Mon Sep 17 00:00:00 2001 From: jensp Date: Thu, 5 Apr 2012 08:01:36 +0000 Subject: [PATCH] Assoziation Publication <-> GenericPerson optimiert, Ticket 1115) git-svn-id: https://svn.libreccm.org/ccm/trunk@1567 8810af33-2d31-482b-a856-94f89814c4df --- .../src/com/arsdigita/cms/ContentItem.java | 17 +- .../GenericOrganizationalUnit.java | 4 - ...ricOrganizationalUnitPersonCollection.java | 23 +- .../arsdigita/content-types/Publication.pdl | 22 +- .../contenttypes/AuthorshipCollection.java | 23 +- .../cms/contenttypes/Publication.java | 28 ++- .../cms/contenttypes/PublicationBundle.java | 203 ++++++++++++++++++ 7 files changed, 278 insertions(+), 42 deletions(-) create mode 100644 ccm-sci-publications/src/com/arsdigita/cms/contenttypes/PublicationBundle.java diff --git a/ccm-cms/src/com/arsdigita/cms/ContentItem.java b/ccm-cms/src/com/arsdigita/cms/ContentItem.java index ae539b574..eeeaa98f9 100755 --- a/ccm-cms/src/com/arsdigita/cms/ContentItem.java +++ b/ccm-cms/src/com/arsdigita/cms/ContentItem.java @@ -1947,16 +1947,31 @@ public class ContentItem extends VersionedACSObject implements CustomCopy { } } - final AssociationCopierLoader assocCopierLoader = + /*final AssociationCopierLoader assocCopierLoader = AssociationCopierLoader.getInstance(); final AssociationCopier assocCopier = assocCopierLoader. getAssociationCopierFor(property, source); if (assocCopier != null) { return assocCopier.copyProperty(source, property, copier); + }*/ + + if (handlesReverseProperties()) { + return copyReverseProperty(source, this, property, copier); } return false; } + + public boolean handlesReverseProperties() { + return false; + } + + public boolean copyReverseProperty(final CustomCopy source, + final ContentItem liveItem, + final Property property, + final ItemCopier copier) { + return false; + } /** * Copy services from the source item. This method is the analogue of the {@link #copyProperty} diff --git a/ccm-cms/src/com/arsdigita/cms/contenttypes/GenericOrganizationalUnit.java b/ccm-cms/src/com/arsdigita/cms/contenttypes/GenericOrganizationalUnit.java index 500ff9f13..df0a2db28 100644 --- a/ccm-cms/src/com/arsdigita/cms/contenttypes/GenericOrganizationalUnit.java +++ b/ccm-cms/src/com/arsdigita/cms/contenttypes/GenericOrganizationalUnit.java @@ -20,14 +20,10 @@ package com.arsdigita.cms.contenttypes; import com.arsdigita.cms.ContentPage; -import com.arsdigita.cms.contenttypes.ui.GenericOrganizationalUnitSubordinateOrgaUnitAddForm; -import com.arsdigita.cms.contenttypes.ui.GenericOrganizationalUnitSubordinateOrgaUnitsTable; -import com.arsdigita.cms.ui.authoring.AuthoringKitWizard; import com.arsdigita.domain.DataObjectNotFoundException; import com.arsdigita.persistence.DataCollection; import com.arsdigita.persistence.DataObject; import com.arsdigita.persistence.OID; -import com.arsdigita.util.Assert; import java.math.BigDecimal; import org.apache.log4j.Logger; diff --git a/ccm-cms/src/com/arsdigita/cms/contenttypes/GenericOrganizationalUnitPersonCollection.java b/ccm-cms/src/com/arsdigita/cms/contenttypes/GenericOrganizationalUnitPersonCollection.java index 454789c6c..700faa18e 100644 --- a/ccm-cms/src/com/arsdigita/cms/contenttypes/GenericOrganizationalUnitPersonCollection.java +++ b/ccm-cms/src/com/arsdigita/cms/contenttypes/GenericOrganizationalUnitPersonCollection.java @@ -70,54 +70,55 @@ public class GenericOrganizationalUnitPersonCollection extends DomainCollection public GenericPerson getPerson() { /*return (GenericPerson) DomainObjectFactory.newInstance(m_dataCollection. - getDataObject());*/ - final ContentBundle bundle = (ContentBundle) DomainObjectFactory.newInstance(m_dataCollection.getDataObject()); + getDataObject());*/ + final ContentBundle bundle = (ContentBundle) DomainObjectFactory. + newInstance(m_dataCollection.getDataObject()); return (GenericPerson) bundle.getPrimaryInstance(); } public OID getOID() { return m_dataCollection.getDataObject().getOID(); } - + public String getSurname() { /*return (String) m_dataCollection.getDataObject().get( - GenericPerson.SURNAME);*/ + GenericPerson.SURNAME);*/ return getPerson().getSurname(); } public String getGivenName() { /*return (String) m_dataCollection.getDataObject().get( - GenericPerson.GIVENNAME);*/ + GenericPerson.GIVENNAME);*/ return getPerson().getGivenName(); } public String getTitlePre() { /*return (String) m_dataCollection.getDataObject().get( - GenericPerson.TITLEPRE);*/ + GenericPerson.TITLEPRE);*/ return getPerson().getTitlePre(); } public String getTitlePost() { /*return (String) m_dataCollection.getDataObject().get( - GenericPerson.TITLEPOST);*/ + GenericPerson.TITLEPOST);*/ return getPerson().getTitlePost(); } public Date getBirthdate() { /*return (Date) m_dataCollection.getDataObject().get( - GenericPerson.BIRTHDATE);*/ + GenericPerson.BIRTHDATE);*/ return getPerson().getBirthdate(); } public String getGender() { /*return (String) m_dataCollection.getDataObject().get( - GenericPerson.GENDER);*/ + GenericPerson.GENDER);*/ return getPerson().getGender(); } - + public GenericPerson getAlias() { /*return (DataObject) m_dataCollection.getDataObject().get( - GenericPerson.ALIAS);*/ + GenericPerson.ALIAS);*/ return getPerson().getAlias(); } } diff --git a/ccm-sci-publications/pdl/com/arsdigita/content-types/Publication.pdl b/ccm-sci-publications/pdl/com/arsdigita/content-types/Publication.pdl index 57ff55925..53c13049c 100644 --- a/ccm-sci-publications/pdl/com/arsdigita/content-types/Publication.pdl +++ b/ccm-sci-publications/pdl/com/arsdigita/content-types/Publication.pdl @@ -20,6 +20,7 @@ model com.arsdigita.cms.contenttypes; import com.arsdigita.cms.ContentPage; +import com.arsdigita.cms.ContentBundle; //PDL definition for the basic attributes of publication. object type Publication extends ContentPage { @@ -44,18 +45,23 @@ object type Publication extends ContentPage { reference key (ct_publications.publication_id); } +object type PublicationBundle extends ContentBundle { + + reference key (ct_publication_bundles.bundle_id); +} + //Assoication for the authors association { - Publication[0..n] publication = join cms_persons.person_id - to ct_publications_authorship.person_id, - join ct_publications_authorship.publication_id - to ct_publications.publication_id; + PublicationBundle[0..n] publication = join cms_person_bundles.bundle_id + to ct_publications_authorship.person_id, + join ct_publications_authorship.publication_id + to ct_publication_bundles.bundle_id; - GenericPerson[0..n] authors = join ct_publications.publication_id - to ct_publications_authorship.publication_id, - join ct_publications_authorship.person_id - to cms_persons.person_id; + GenericPersonBundle[0..n] authors = join ct_publication_bundles.bundle_id + to ct_publications_authorship.publication_id, + join ct_publications_authorship.person_id + to cms_person_bundles.bundle_id; // Additional attributes Boolean[0..1] editor = ct_publications_authorship.editor BIT; diff --git a/ccm-sci-publications/src/com/arsdigita/cms/contenttypes/AuthorshipCollection.java b/ccm-sci-publications/src/com/arsdigita/cms/contenttypes/AuthorshipCollection.java index fc3107194..6a213bd42 100644 --- a/ccm-sci-publications/src/com/arsdigita/cms/contenttypes/AuthorshipCollection.java +++ b/ccm-sci-publications/src/com/arsdigita/cms/contenttypes/AuthorshipCollection.java @@ -18,7 +18,9 @@ */ package com.arsdigita.cms.contenttypes; +import com.arsdigita.cms.ContentBundle; import com.arsdigita.domain.DomainCollection; +import com.arsdigita.domain.DomainObjectFactory; import com.arsdigita.persistence.DataCollection; import com.arsdigita.persistence.DataObject; import org.apache.log4j.Logger; @@ -172,17 +174,22 @@ public class AuthorshipCollection extends DomainCollection { this.rewind(); } + public GenericPerson getAuthor() { + //return new GenericPerson(m_dataCollection.getDataObject()); + final ContentBundle bundle = (ContentBundle) DomainObjectFactory. + newInstance(m_dataCollection.getDataObject()); + return (GenericPerson) bundle.getPrimaryInstance(); + } + public String getSurname() { - return (String) m_dataCollection.getDataObject().get( - GenericPerson.SURNAME); + //return (String) m_dataCollection.getDataObject().get( + // GenericPerson.SURNAME); + return getAuthor().getSurname(); } public String getGivenName() { - return (String) m_dataCollection.getDataObject().get( - GenericPerson.GIVENNAME); - } - - public GenericPerson getAuthor() { - return new GenericPerson(m_dataCollection.getDataObject()); + //return (String) m_dataCollection.getDataObject().get( + // GenericPerson.GIVENNAME); + return getAuthor().getGivenName(); } } diff --git a/ccm-sci-publications/src/com/arsdigita/cms/contenttypes/Publication.java b/ccm-sci-publications/src/com/arsdigita/cms/contenttypes/Publication.java index 8e7a415cc..abea57dd7 100644 --- a/ccm-sci-publications/src/com/arsdigita/cms/contenttypes/Publication.java +++ b/ccm-sci-publications/src/com/arsdigita/cms/contenttypes/Publication.java @@ -69,7 +69,7 @@ public class Publication extends ContentPage { public final static String ABSTRACT = "abstract"; public final static String MISC = "misc"; public final static String AUTHORS = "authors"; - private final static String AUTHORS_STR = "authorsStr"; //authorsStr is an interal field only for use in data queries, and is updated automatically + protected final static String AUTHORS_STR = "authorsStr"; //authorsStr is an interal field only for use in data queries, and is updated automatically public final static String EDITOR = "editor"; public final static String AUTHOR_ORDER = "authorOrder"; public final static String SERIES = "series"; @@ -108,6 +108,10 @@ public class Publication extends ContentPage { public static PublicationsConfig getConfig() { return config; } + + public PublicationBundle getPublicationBundle() { + return (PublicationBundle) getContentBundle(); + } /** * Gets the year of publications. @@ -179,7 +183,8 @@ public class Publication extends ContentPage { * @return Collection of the authors of the publication. */ public AuthorshipCollection getAuthors() { - return new AuthorshipCollection((DataCollection) get(AUTHORS)); + //return new AuthorshipCollection((DataCollection) get(AUTHORS)); + return getPublicationBundle().getAuthors(); } /** @@ -190,14 +195,16 @@ public class Publication extends ContentPage { * @param editor Is the author an editor? */ public void addAuthor(final GenericPerson author, final Boolean editor) { - Assert.exists(author, GenericPerson.class); + //Assert.exists(author, GenericPerson.class); - DataObject link = add(AUTHORS, author); + //DataObject link = add(AUTHORS, author); - link.set(EDITOR, editor); - link.set(AUTHOR_ORDER, Integer.valueOf((int) getAuthors().size())); + //link.set(EDITOR, editor); + //link.set(AUTHOR_ORDER, Integer.valueOf((int) getAuthors().size())); - updateAuthorsStr(); + //updateAuthorsStr(); + + getPublicationBundle().addAuthor(author, editor); } /** @@ -206,10 +213,11 @@ public class Publication extends ContentPage { * @param author The author to remove. */ public void removeAuthor(final GenericPerson author) { - Assert.exists(author, GenericPerson.class); - remove(AUTHORS, author); + //Assert.exists(author, GenericPerson.class); + //remove(AUTHORS, author); - updateAuthorsStr(); + //updateAuthorsStr(); + getPublicationBundle().removeAuthor(author); } public void swapWithPreviousAuthor(final GenericPerson author) { diff --git a/ccm-sci-publications/src/com/arsdigita/cms/contenttypes/PublicationBundle.java b/ccm-sci-publications/src/com/arsdigita/cms/contenttypes/PublicationBundle.java new file mode 100644 index 000000000..1e9138431 --- /dev/null +++ b/ccm-sci-publications/src/com/arsdigita/cms/contenttypes/PublicationBundle.java @@ -0,0 +1,203 @@ +package com.arsdigita.cms.contenttypes; + +import com.arsdigita.cms.AssociationCopier; +import com.arsdigita.cms.ContentBundle; +import com.arsdigita.cms.ContentItem; +import com.arsdigita.cms.CustomCopy; +import com.arsdigita.cms.ItemCollection; +import com.arsdigita.cms.ItemCopier; +import com.arsdigita.domain.DataObjectNotFoundException; +import com.arsdigita.domain.DomainObjectFactory; +import com.arsdigita.persistence.DataCollection; +import com.arsdigita.persistence.DataObject; +import com.arsdigita.persistence.OID; +import com.arsdigita.persistence.metadata.Property; +import com.arsdigita.util.Assert; +import java.math.BigDecimal; + +/** + * + * @author Jens Pelzetter + * @version $Id$ + */ +public class PublicationBundle extends ContentBundle { + + public static final String BASE_DATA_OBJECT_TYPE = + "com.arsdigita.cms.contentttypes.PublicationBundle"; + public static final String AUTHORS = "authors"; + public final static String AUTHOR_ORDER = "authorOrder"; + public final static String EDITOR = "editor"; + + public PublicationBundle(final ContentItem primary) { + super(BASE_DATA_OBJECT_TYPE); + + Assert.exists(primary, ContentItem.class); + + setDefaultLanguage(primary.getLanguage()); + setContentType(primary.getContentType()); + addInstance(primary); + + setName(primary.getName()); + } + + public PublicationBundle(final OID oid) throws DataObjectNotFoundException { + super(oid); + } + + public PublicationBundle(final BigDecimal id) + throws DataObjectNotFoundException { + super(new OID(BASE_DATA_OBJECT_TYPE, id)); + } + + public PublicationBundle(final DataObject dobj) { + super(dobj); + } + + public PublicationBundle(final String type) { + super(type); + } + + @Override + public boolean copyProperty(final CustomCopy source, + final Property property, + final ItemCopier copier) { + final String attribute = property.getName(); + if (copier.getCopyType() == ItemCopier.VERSION_COPY) { + final Publication pubBundle = (Publication) source; + + if (AUTHORS.equals(attribute)) { + final DataCollection authors = (DataCollection) pubBundle.get( + AUTHORS); + + while (authors.next()) { + createAuthorAssoc(authors); + } + + return true; + } else { + return super.copyProperty(source, property, copier); + } + + } else { + return super.copyProperty(source, property, copier); + } + } + + @Override + public boolean handlesReverseProperties() { + return true; + } + + @Override + public boolean copyReverseProperty(final CustomCopy source, + final ContentItem liveItem, + final Property property, + final ItemCopier copier) { + final String attribute = property.getName(); + if (copier.getCopyType() == ItemCopier.VERSION_COPY) { + if (("publication".equals(attribute)) + && (source instanceof GenericPersonBundle)) { + final GenericPersonBundle authorBundle = + (GenericPersonBundle) source; + final DataCollection publications = + (DataCollection) authorBundle.get( + "publication"); + + while (publications.next()) { + createAuthorPublicationAssoc(publications, + (GenericPersonBundle) liveItem); + } + + return true; + } else { + return super.copyReverseProperty(source, liveItem, property, + copier); + } + } else { + return super.copyReverseProperty(source, liveItem, property, copier); + } + } + + private void createAuthorAssoc(final DataCollection authors) { + final GenericPersonBundle draftAuthor = + (GenericPersonBundle) DomainObjectFactory. + newInstance(authors.getDataObject()); + final GenericPersonBundle liveAuthor = + (GenericPersonBundle) draftAuthor. + getLiveVersion(); + + if (liveAuthor != null) { + final DataObject link = add(AUTHORS, liveAuthor); + + link.set(EDITOR, authors.get(AuthorshipCollection.LINKEDITOR)); + link.set(AUTHOR_ORDER, authors.get(AuthorshipCollection.LINKORDER)); + + link.save(); + } + } + + private void createAuthorPublicationAssoc(final DataCollection publications, + final GenericPersonBundle author) { + final PublicationBundle draftPublication = + (PublicationBundle) DomainObjectFactory. + newInstance(publications.getDataObject()); + final PublicationBundle livePublication = + (PublicationBundle) draftPublication. + getLiveVersion(); + + if (livePublication != null) { + final DataObject link = author.add("publication", livePublication); + + link.set(EDITOR, publications.get(AuthorshipCollection.LINKEDITOR)); + link.set(AUTHOR_ORDER, publications.get(AuthorshipCollection.LINKORDER)); + + link.save(); + } + } + + public AuthorshipCollection getAuthors() { + return new AuthorshipCollection((DataCollection) get(AUTHORS)); + } + + public void addAuthor(final GenericPerson author, final Boolean editor) { + Assert.exists(author, GenericPerson.class); + + final DataObject link = add(AUTHORS, author); + + link.set(EDITOR, editor); + link.set(AUTHOR_ORDER, Integer.valueOf((int) getAuthors().size())); + + updateAuthorsStr(); + } + + public void removeAuthor(final GenericPerson author) { + Assert.exists(author, GenericPerson.class); + + remove(AUTHORS, author); + + updateAuthorsStr(); + } + + protected void updateAuthorsStr() { + final AuthorshipCollection authors = getAuthors(); + StringBuilder builder = new StringBuilder(); + while (authors.next()) { + if (builder.length() > 0) { + builder.append("; "); + } + builder.append(authors.getSurname()); + builder.append(", "); + builder.append(authors.getGivenName()); + } + + final String authorsStr = builder.toString(); + + final ItemCollection instances = getInstances(); + + Publication publication; + while (instances.next()) { + publication = (Publication) instances.getDomainObject(); + publication.set(Publication.AUTHORS_STR, authorsStr); + } + } +}