[jboss-user] [JBoss Seam] - Re: selectItems - Is it possible?
Kragoth
do-not-reply at jboss.com
Sun Nov 25 19:48:22 EST 2007
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.java
| package 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.java
| package 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 have
| 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();
| }
|
You could just use the line
| key = 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. :)
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4107624#4107624
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4107624
More information about the jboss-user
mailing list