Skip to content

Commit

Permalink
fixes #1 - new supportsLink API in Factory.
Browse files Browse the repository at this point in the history
In addition, replacing usage of deprecated DomainObjectContainer methods with their equivalents (introduced in 1.12.0)
  • Loading branch information
danhaywood committed Jul 8, 2016
1 parent 1b23144 commit 82265c1
Showing 1 changed file with 47 additions and 15 deletions.
Expand Up @@ -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<S, P, L extends PolymorphicAssociationLink<S, P, L>> implements Comparable<L> {
Expand All @@ -47,6 +52,10 @@ protected InstantiateEvent(final Class<L> 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;
}
Expand Down Expand Up @@ -125,6 +134,21 @@ public Factory(
}
}

/**
* Whether or not this link type is supported.
*
* <p>
* Allows users of this module to be able to turn off/on contributions.
* </p>
*/
public boolean supportsLink(final PR polymorphicReference) {
final E event = instantiateEvent(eventSource, null, polymorphicReference);
eventBusService.post(event);

final Class<? extends L> subtype = event.getSubtype();
return subtype != null;
}

/**
* Instantiates the appropriate link entity (using the subtype specified by subscriber) and persists.
*
Expand All @@ -140,26 +164,26 @@ public L createLink(final S subject, final PR polymorphicReference) {

final Class<? extends L> 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;
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -242,20 +272,22 @@ 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

//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

}

0 comments on commit 82265c1

Please sign in to comment.