From ed03eee2df54e3ff7e5245141b30551d1055486f Mon Sep 17 00:00:00 2001 From: Ben Alex Date: Sat, 25 Mar 2006 01:02:56 +0000 Subject: [PATCH] Refactored. NB: This code is unsupported and illustrates non-anemic domain objects that do not leverage transparent persistence capabilities (I recommend you use transparent persistence capabilities where possible - I hope to check in different packages with new patterns in due course). --- .../service/ImmutableManagerEditor.java | 55 ++++++++++++++++++ .../acegisecurity/domain/util/EnumEditor.java | 58 +++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 domain/src/main/java/org/acegisecurity/domain/service/ImmutableManagerEditor.java create mode 100644 domain/src/main/java/org/acegisecurity/domain/util/EnumEditor.java diff --git a/domain/src/main/java/org/acegisecurity/domain/service/ImmutableManagerEditor.java b/domain/src/main/java/org/acegisecurity/domain/service/ImmutableManagerEditor.java new file mode 100644 index 0000000000..15aa6c5377 --- /dev/null +++ b/domain/src/main/java/org/acegisecurity/domain/service/ImmutableManagerEditor.java @@ -0,0 +1,55 @@ +package org.acegisecurity.domain.service; + +import java.beans.PropertyEditorSupport; + +import org.acegisecurity.domain.PersistableEntity; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; + +/** + * Converts between a PersistableEntity's internal ID (expressed as a String + * and the corresponding PersistableEntity sourced from an ImmutableManager). + * + * @author Ben Alex + * @version $Id$ + */ +public class ImmutableManagerEditor extends PropertyEditorSupport { + protected final transient Log log = LogFactory.getLog(getClass()); + private ImmutableManager immutableManager; + + private final boolean fallbackToNull; + + public ImmutableManagerEditor(ImmutableManager immutableManager, boolean fallbackToNull) { + Assert.notNull(immutableManager, "ImmutableManager required"); + this.immutableManager = immutableManager; + this.fallbackToNull = fallbackToNull; + } + + public void setAsText(String text) throws IllegalArgumentException { + if (this.fallbackToNull && !StringUtils.hasText(text)) { + // treat empty String as null value + setValue(null); + } + else { + Long id = new Long(text); + PersistableEntity value = immutableManager.readId(id); + + if (log.isDebugEnabled()) { + log.debug("Property Editor converted '" + text + "' to object: " + value); + } + setValue(value); + } + } + + public String getAsText() { + String result = null; + if (getValue() != null) { + result = ((PersistableEntity) getValue()).getInternalId().toString(); + } + if (log.isDebugEnabled()) + log.debug("Property Editor returning: " + result); + return result; + } +} diff --git a/domain/src/main/java/org/acegisecurity/domain/util/EnumEditor.java b/domain/src/main/java/org/acegisecurity/domain/util/EnumEditor.java new file mode 100644 index 0000000000..e5e0f2f5d8 --- /dev/null +++ b/domain/src/main/java/org/acegisecurity/domain/util/EnumEditor.java @@ -0,0 +1,58 @@ +package org.acegisecurity.domain.util; + +import java.beans.PropertyEditorSupport; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.util.StringUtils; + +public class EnumEditor extends PropertyEditorSupport { + protected final transient Log log = LogFactory.getLog(getClass()); + + private final Class enumClass; + + private final boolean fallbackToNull; + + /** + * Create a new EnumEditor, which can create an Enum by retrieving + * the map of enum keys. + * + *

The fallbackToNull indicates whether null should be returned if + * a null String is provided to the property editor or if the provided + * String could not be used to locate an Enum. If set to true, null + * will be returned. If set to false, IllegalArgumentException will be thrown. + */ + public > EnumEditor(Class enumClass, boolean fallbackToNull) { + this.enumClass = enumClass; + this.fallbackToNull = fallbackToNull; + } + + /** + * Parse the Enum from the given text. + */ + @SuppressWarnings("unchecked") + public void setAsText(String text) throws IllegalArgumentException { + if (this.fallbackToNull && !StringUtils.hasText(text)) { + // treat empty String as null value + setValue(null); + } + else { + Enum value = Enum.valueOf(this.enumClass, text); + + if (log.isDebugEnabled()) { + log.debug("Property Editor converted '" + text + "' to object: " + value); + } + setValue(value); + } + } + + public String getAsText() { + String result = null; + if (getValue() != null) { + result = ((Enum) getValue()).name(); + } + if (log.isDebugEnabled()) + log.debug("Property Editor returning: " + result); + return result; + } +}