From 82265c161f1945f21f89beb0329f5e97a8910a8c Mon Sep 17 00:00:00 2001 From: Dan Haywood Date: Fri, 8 Jul 2016 19:50:22 +0100 Subject: [PATCH] fixes #1 - new supportsLink API in Factory. In addition, replacing usage of deprecated DomainObjectContainer methods with their equivalents (introduced in 1.12.0) --- .../poly/dom/PolymorphicAssociationLink.java | 62 ++++++++++++++----- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/dom/src/main/java/org/isisaddons/module/poly/dom/PolymorphicAssociationLink.java b/dom/src/main/java/org/isisaddons/module/poly/dom/PolymorphicAssociationLink.java index dd083a3..ba6d832 100644 --- a/dom/src/main/java/org/isisaddons/module/poly/dom/PolymorphicAssociationLink.java +++ b/dom/src/main/java/org/isisaddons/module/poly/dom/PolymorphicAssociationLink.java @@ -20,14 +20,19 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; + import javax.inject.Inject; + import org.apache.isis.applib.DomainObjectContainer; import org.apache.isis.applib.NonRecoverableException; import org.apache.isis.applib.annotation.Programmatic; import org.apache.isis.applib.services.bookmark.Bookmark; -import org.apache.isis.applib.services.bookmark.BookmarkService; +import org.apache.isis.applib.services.bookmark.BookmarkService2; import org.apache.isis.applib.services.eventbus.EventBusService; import org.apache.isis.applib.services.i18n.TranslatableString; +import org.apache.isis.applib.services.repository.RepositoryService; +import org.apache.isis.applib.services.title.TitleService; +import org.apache.isis.applib.services.xactn.TransactionService; import org.apache.isis.applib.util.ObjectContracts; public abstract class PolymorphicAssociationLink> implements Comparable { @@ -47,6 +52,10 @@ protected InstantiateEvent(final Class linkType, final Object source, final S this.polymorphicReference = polymorphicReference; } + /** + * Will be null if checking if the link is {@link PolymorphicAssociationLink.Factory#supportsLink(Object) supported}, + * but is populated when the link is actually {@link PolymorphicAssociationLink.Factory#createLink(Object, Object) created}, + */ public S getSubject() { return subject; } @@ -125,6 +134,21 @@ public Factory( } } + /** + * Whether or not this link type is supported. + * + *

+ * Allows users of this module to be able to turn off/on contributions. + *

+ */ + public boolean supportsLink(final PR polymorphicReference) { + final E event = instantiateEvent(eventSource, null, polymorphicReference); + eventBusService.post(event); + + final Class subtype = event.getSubtype(); + return subtype != null; + } + /** * Instantiates the appropriate link entity (using the subtype specified by subscriber) and persists. * @@ -140,26 +164,26 @@ public L createLink(final S subject, final PR polymorphicReference) { final Class subtype = event.getSubtype(); if(subtype == null) { - throw new NonRecoverableException("Cannot create link to " + container.titleOf(polymorphicReference) + ", no subtype provided"); + throw new NonRecoverableException("Cannot create link to " + titleService.titleOf(polymorphicReference) + ", no subtype provided"); } if(persistStrategy == PersistStrategy.AUTOMATIC) { - if(!container.isPersistent(polymorphicReference)) { + if(!repositoryService.isPersistent(polymorphicReference)) { // in case there are persists pending - container.flush(); - if(!container.isPersistent(polymorphicReference)) { - throw new NonRecoverableException("Link's polymorphic reference " + container.titleOf(polymorphicReference) + " is not persistent"); + transactionService.flushTransaction(); + if(!repositoryService.isPersistent(polymorphicReference)) { + throw new NonRecoverableException("Link's polymorphic reference " + titleService.titleOf(polymorphicReference) + " is not persistent"); } } } - final L link = container.newTransientInstance(subtype); + final L link = repositoryService.instantiate(subtype); link.setPolymorphicReference(polymorphicReference); link.setSubject(subject); if(persistStrategy == PersistStrategy.AUTOMATIC) { - container.persist(link); + repositoryService.persist(link); } return link; @@ -173,6 +197,12 @@ private E instantiateEvent(final Object eventSource, final S subject, final PR p } } + @Inject + TitleService titleService; + @Inject + TransactionService transactionService; + @Inject + RepositoryService repositoryService; @Inject DomainObjectContainer container; @Inject @@ -190,8 +220,8 @@ protected PolymorphicAssociationLink(final String titlePattern) { public TranslatableString title() { return TranslatableString.tr( titlePattern, - "polymorphicReference", container.titleOf(getPolymorphicReference()), - "subject", container.titleOf(getSubject())); + "polymorphicReference", titleService.titleOf(getPolymorphicReference()), + "subject", titleService.titleOf(getSubject())); } //endregion @@ -223,7 +253,7 @@ public TranslatableString title() { @Programmatic public P getPolymorphicReference() { final Bookmark bookmark = new Bookmark(getPolymorphicObjectType(), getPolymorphicIdentifier()); - return (P) bookmarkService.lookup(bookmark); + return (P) bookmarkService.lookup(bookmark, BookmarkService2.FieldResetPolicy.DONT_RESET); } /** @@ -242,7 +272,7 @@ public void setPolymorphicReference(final P polymorphicReference) { @Override public int compareTo(final PolymorphicAssociationLink other) { - return ObjectContracts.compare(this, other, "subject,polymorphicObjectType,polymorphicIdentifier"); + return ObjectContracts.compare(this, other, "subject","polymorphicObjectType","polymorphicIdentifier"); } //endregion @@ -250,12 +280,14 @@ public int compareTo(final PolymorphicAssociationLink other) { //region > injected services @javax.inject.Inject - private DomainObjectContainer container; + protected RepositoryService repositoryService; @javax.inject.Inject - private BookmarkService bookmarkService; + protected TitleService titleService; - //endregion + @javax.inject.Inject + protected BookmarkService2 bookmarkService; + //endregion }