This content has been marked as final.
Show 31 replies
-
30. Re: selectItems - Is it possible?
igx89 Nov 21, 2007 12:04 PM (in response to kragoth)Have you tried my ObjectConverter?
Another potential solution (if you don't want to use ObjectConverter) is to use the new itemValue attribute on s:selectItems that'll be in Seam 2.0.1. -
31. Re: selectItems - Is it possible?
kragoth Nov 25, 2007 7:48 PM (in response to kragoth)I have used IGx89's converter and it is working perfectly for me....
I've made a few slight changes but nothing major... other then that I have moved the Store outside the converter.
Oh and I have done some specific stuff for my project to make it easier for me but you can get rid of all that.
So for anyone interested:
ObjectConverter.javapackage gekko.web.converter; import static org.jboss.seam.ScopeType.CONVERSATION; import gekko.util.StringUtils; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.ConverterException; import org.jboss.seam.Seam; import org.jboss.seam.annotations.Install; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; import org.jboss.seam.annotations.faces.Converter; import org.jboss.seam.annotations.intercept.BypassInterceptors; @Name("GenericObjectConverter") @Scope(CONVERSATION) @Install(precedence = Install.APPLICATION) @Converter @BypassInterceptors public class ObjectConverter implements javax.faces.convert.Converter { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException { if (value == null) { return null; } return ObjectConverterStore.instance().get(value); } @Override public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException { if (value == null) { return null; } String key = ObjectConverterStore.instance().contains(value); if (!StringUtils.isBlank(key)) { return key; } if(context.getApplication().getStateManager().isSavingStateInClient(context) && Seam.isEntityClass(value.getClass())) { throw new ConverterException("ObjectConverter is unable to handle entity classes when client-side " + "state saving is enabled. Please use EntityConverter instead, or enable server-side state saving"); } return ObjectConverterStore.instance().put(value); } }
and
ObjectConverterStore.javapackage gekko.web.converter; import static org.jboss.seam.ScopeType.PAGE; import static org.jboss.seam.annotations.Install.APPLICATION; import gekko.domain.AbstractEntityId; import gekko.domain.PersistentEntity; import java.rmi.server.UID; import java.util.HashMap; import java.util.Map; import org.jboss.seam.Component; import org.jboss.seam.annotations.Install; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; import org.jboss.seam.contexts.Contexts; @Name("ObjectConverterStore") @Install(precedence = APPLICATION) @Scope(PAGE) public class ObjectConverterStore { private final Map<String, Object> objects = new HashMap<String, Object>(); // Add an object to the store public String put(Object entity) { String key; if (entity instanceof PersistentEntity) { //If this is a compound key then we need to use something else if (((PersistentEntity)entity).getId() instanceof AbstractEntityId) { key = Integer.toString(((PersistentEntity)entity).getId().hashCode()); } else { key = ((PersistentEntity)entity).getId().toString(); } } else { key = new UID().toString(); } objects.put(key, entity); return key; } // Get an object from the store public Object get(String key) { return objects.get(key); } // Check if store contains the object already. public String contains(Object object) { for (Map.Entry<String, Object> entry : objects.entrySet()) { if (entry.getValue().equals(object)) { return entry.getKey(); } } return null; } public static ObjectConverterStore instance() { if (!Contexts.isPageContextActive()) { throw new IllegalArgumentException("Page scope not active"); } return (ObjectConverterStore) Component .getInstance(ObjectConverterStore.class); } }
If you have a look at ObjectConverterStore where i haveif (entity instanceof PersistentEntity) { //If this is a compound key then we need to use something else if (((PersistentEntity)entity).getId() instanceof AbstractEntityId) { key = Integer.toString(((PersistentEntity)entity).getId().hashCode()); } else { key = ((PersistentEntity)entity).getId().toString(); } } else { key = new UID().toString(); }
You could just use the linekey = new UID().toString();
I've only done it that way to make some of my keys a little more meaningful... and so that I can match objects to ones that are inside my list based on id.
At least this is a working example that people can get some ideas off anyway. :)