2 changed files with 113 additions and 0 deletions
@ -0,0 +1,55 @@
@@ -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; |
||||
} |
||||
} |
||||
@ -0,0 +1,58 @@
@@ -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<? extends Enum> enumClass; |
||||
|
||||
private final boolean fallbackToNull; |
||||
|
||||
/** |
||||
* Create a new EnumEditor, which can create an Enum by retrieving |
||||
* the map of enum keys. |
||||
* |
||||
* <p>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 <E extends Enum<E>> EnumEditor(Class<E> 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; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue