Re: How To Use A Type Safe Enum As A Custom Value In EOF (was Re: (EOM) entity logic)
Re: How To Use A Type Safe Enum As A Custom Value In EOF (was Re: (EOM) entity logic)
- Subject: Re: How To Use A Type Safe Enum As A Custom Value In EOF (was Re: (EOM) entity logic)
- From: John Bruce <email@hidden>
- Date: Tue, 2 Aug 2005 11:15:01 +1000
> In sum, I guess your recommendation would be that I use a CHAR, and do a
> Integer.parseInt() on it?
>
That's just the way I do it because it's easy for me to construct lots
of different ones and db space is not a really an issue for my
program.
I based my code off the commons-lang library which has an enum package
(http://jakarta.apache.org/commons/lang) . It might be worth checking
out to see if the way they do it would work for you.
I've attached a copy of the classes I use. I'm not sure if they will
compile on their own, it depends on commons-lang for at least some
things. Also I haven't looked at them for a while so there may be some
methods in there that aren't used etc. There is also a copy of a D2W
class for displaying and editing enums.
Actually after looking at the code I don't do a parseInt - I just
compare the archiveData() value to the incoming value from the db but
the value is stored as an int in the class.
HTH,
John
//
// BusinessLogicEnum.java
//
// Created by John Bruce on 14/01/05.
// Copyright (c) 2005 __MyCompanyName__. All rights reserved.
//
import java.util.*;
import org.apache.commons.lang.*;
import org.apache.commons.lang.builder.*;
import com.webobjects.foundation.*;
public abstract class BusinessLogicEnum extends Object implements Cloneable, Enumerable {
public static final er.extensions.ERXLogger log = er.extensions.ERXLogger.getERXLogger(BusinessLogicEnum.class, "busineslogic");
public abstract String archiveData();
public abstract NSArray enumObjects();
public abstract String displayString();
public final static BusinessLogicEnum objectWithArchiveData(final NSArray objects, final String value) {
if (StringUtils.isBlank(value)) {
throw new IllegalArgumentException("ArchiveData cannot be blank.");
}
BusinessLogicEnum obj = null;
for (Enumeration e = objects.objectEnumerator(); e.hasMoreElements(); ) {
BusinessLogicEnum o = (BusinessLogicEnum)e.nextElement();
if (o.archiveData().equals(value)) {
obj = o;
break;
}
}
if (obj == null) {
throw new IllegalArgumentException("Enum not found with archiveData: " + value);
}
return obj;
}
public Object clone() {
return this;
}
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
//
// BusinessLogicValuedEnum.java
//
// Created by John Bruce on 13/02/05.
// Copyright (c) 2005 __MyCompanyName__. All rights reserved.
//
import com.webobjects.eoaccess.*;
import com.webobjects.eocontrol.*;
import com.webobjects.foundation.*;
public abstract class BusinessLogicValuedEnum extends BusinessLogicEnum {
public static final er.extensions.ERXLogger log = er.extensions.ERXLogger.getERXLogger(BusinessLogicValuedEnum.class, "businesslogic");
final String name;
final int value;
private static NSMutableDictionary valuesDict = new NSMutableDictionary();
BusinessLogicValuedEnum(final String name, final int value) {
this.name = name;
this.value = value;
addValueToDict(this);
}
public final String getName() {
return name;
}
public final int getValue() {
return value;
}
public final String archiveData() {
return Integer.toString(getValue());
}
//public abstract NSArray values();
public final NSArray enumObjects() {
return NSArray.EmptyArray;
}
public final String displayString() {
return getName();
}
private synchronized static void addValueToDict(final Object object) {
NSMutableArray currentValues = new NSMutableArray();
if (valuesDict.objectForKey(object.getClass().getName()) != null)
currentValues = (NSMutableArray)valuesDict().objectForKey(object.getClass().getName());
currentValues.addObject(object);
valuesDict().setObjectForKey(currentValues, object.getClass().getName());
}
private synchronized static NSArray valuesForClass(final Class clazz) {
return (NSArray)valuesDict().objectForKey(clazz.getName());
}
public static NSArray values(final EOEnterpriseObject object, final String propertyKey) {
try {
return valuesForClass(classNameForEnum(object, propertyKey));
} catch (ClassNotFoundException e) {
log.error("Error getting class for: " + object.entityName() + " with key: " + propertyKey + "/n" + e);
}
return NSArray.EmptyArray;
}
private static Class classNameForEnum(final EOEnterpriseObject object, final String propertyKey) throws ClassNotFoundException {
EOModelGroup mg = EOModelGroup.defaultGroup();
EOEntity entity = mg.entityNamed(object.entityName());
EOAttribute attribute = entity.attributeNamed(propertyKey);
return Class.forName(attribute.className());
}
public Object clone() {
return this;
}
private static synchronized NSMutableDictionary valuesDict() {
return valuesDict;
}
public static Enumerable objectWithArchiveData(final Class clazz, final String value) {
return BusinessLogicEnum.objectWithArchiveData(valuesForClass(clazz), value);
}
}
//
// D2WValuedEnum.java: Class file for WO Component 'TQD2WValuedEnum'
// Project TQD2W
//
// Created by john on 13/02/05
//
import com.tradequotient.businesslogic.model.*;
import com.webobjects.appserver.*;
import com.webobjects.foundation.*;
public class D2WValuedEnum extends D2WStatelessComponent {
public BusinessLogicValuedEnum item;
public BusinessLogicValuedEnum selectedItem;
private NSArray values;
public D2WValuedEnum(WOContext context) {
super(context);
}
public void takeValuesFromRequest(WORequest request, WOContext context) throws NSValidation.ValidationException {
super.takeValuesFromRequest(request, context);
if (isEdit()) {
object().takeValueForKeyPath(selectedItem, propertyKey());
}
}
public void reset() {
super.reset();
item = null;
selectedItem = null;
values = null;
}
public BusinessLogicValuedEnum selectedItem() {
if (selectedItem == null) {
selectedItem = (BusinessLogicValuedEnum)object().valueForKeyPath(propertyKey());
}
return selectedItem;
}
public NSArray values() {
return BusinessLogicValuedEnum.values(object(), propertyKey());
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden