-
1. Re: Seam 3 & s:entityConverter
msahin35 Nov 1, 2010 6:46 PM (in response to mfillion)You can use without s:conertEntity like this.
<h:selectOneMenu id="combooo" value="#{widgetStoreHome.instance.activeWigdet}" > <f:selectItems value="#{widgetHome.widgetList}" var="s" itemLabel="#{s.id}" itemValue="#{s}"> </f:selectItems> </h:selectOneMenu> <h:commandButton action="#{widgetStoreHome.test}"></h:commandButton>
import javax.annotation.Named; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; /** * * @author msa */ @Named @SessionScoped @ManagedBean public class WidgetStoreHome extends HomeBean<WidgetStore>{ public void test(){ System.out.println(getInstance().getActiveWigdet()); } }
But if you have custom converter and this does not enough for requirements you can do this like this. In my other post. wait
-
2. Re: Seam 3 & s:entityConverter
msahin35 Nov 1, 2010 7:14 PM (in response to mfillion)for example I have two entity class
public class Widget extends BaseObject public class Widget2 extends BaseObject
be carefull both this classes have to equals method
@Entity public class Widget extends BaseEntity { private String partNumber; private String name; private String description; private Long id; @Id @GeneratedValue public Long getId() { return id; } public void setId(Long id) { this.id = id; } // demonstrates a column name override @Column(name = "partno") public String getPartNumber() { return partNumber; } public void setPartNumber(String partNumber) { this.partNumber = partNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } /** Default value included to remove warning. Remove or modify at will. */ private static final long serialVersionUID = 1L; *@Override public boolean equals(Object obj) { if (obj == null) return false; if (obj instanceof Widget) { Widget other = (Widget) obj; if (other.getId() == this.id ) return true; } return false; }* public String toString(){ return "I am a widget and my id is "+getId()+" and My name is "+getName(); } public int hashCode() { return getId().intValue(); } }
import java.io.Serializable; /** * * @author msa */ public class BaseEntity implements Serializable{ private Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Override public boolean equals(Object obj) { if (obj == null) return false; if (obj instanceof BaseEntity) { BaseEntity other = (BaseEntity) obj; if (other.getId() == this.id ) return true; } return false; } }
Both my classes extends baseObject so if I write a base Object converter. So I can use it for 2 classes.
import com.common.helper.util.WeldHelperUtil; import com.common.login.UserDatabase; import java.io.Serializable; import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; import javax.ejb.Stateful; import javax.enterprise.context.Dependent; import javax.enterprise.inject.spi.BeanManager; import javax.faces.bean.RequestScoped; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.FacesConverter; import javax.inject.Inject; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.jboss.weld.extensions.beanManager.BeanManagerAccessor; /** * * @author msa */ @RequestScoped @FacesConverter("baseObjectConverter") @Stateful public class BaseEntityConverter implements Converter,Serializable { HashMap<String,BaseEntity> map=new HashMap<String,BaseEntity>(); @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { Object obj=null; try { String values[] = value.split(":"); Class className = Class.forName(values[0]); BeanManager beanManager = BeanManagerAccessor.getManager(); WebEManager webEManager= WeldHelperUtil.getContextualInstance(beanManager, WebEManager.class); System.out.print("webEManager: "+webEManager); long id = Long.valueOf(values[1]); System.out.println("idddddddddd: "+id); try { obj= webEManager.find(className, id); } catch (Exception e) { System.out.println("Exception"+e.getMessage()); } } catch (ClassNotFoundException ex) { Logger.getLogger(BaseEntityConverter.class.getName()).log(Level.SEVERE, null, ex); } return obj; } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { String str = null; if(value!=null){ str=String.valueOf(""+value.getClass().getCanonicalName() + ":" + ((BaseEntity)(value)).getId()); } return str; } }
You can use like this
<h:selectOneMenu id="combooo" converter="baseObjectConverter" value="#{widgetStoreHome.instance.activeWigdet}" > <f:selectItems value="#{widgetHome.widgetList}" var="s" itemLabel="#{s.id}" itemValue="#{s}"> </f:selectItems> </h:selectOneMenu>
You can inject an entty manager like this but. I could not success this in a converter. Like this methods below. İf some one knows please tell how.
@Inject @Dependent @UserDatabase private EntityManager entityManagerDao; @Inject protected WebEManager entityManagerDao3; @UserDatabase @PersistenceContext EntityManager entityManagerDao2;
But I had the entity manager like this.
WebEManager is my entity manager.
BeanManager beanManager = BeanManagerAccessor.getManager(); WebEManager webEManager= WeldHelperUtil.getContextualInstance(beanManager, WebEManager.class); System.out.print("webEManager: "+webEManager);
import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.BeanManager; /** * * @author msa */ public class WeldHelperUtil { @SuppressWarnings("unchecked") public static <T> T getContextualInstance(final BeanManager manager, final Class<T> type) { T result = null; Bean<T> bean = (Bean<T>) manager.resolve(manager.getBeans(type)); if (bean != null) { CreationalContext<T> context = manager.createCreationalContext(bean); if (context != null) { result = (T) manager.getReference(bean, type, context); } } return result; } }
When page is rendering getAsString method works.
I am writing objects like this.package.x.y.Widget:2
package.x.y.Widget:4
package.x.y.Widget:5
classname:objectIdwhen I clicked submit button.
Get as object method works.
I am parsing string.Class className = Class.forName(values[0]); long id = Long.valueOf(values[1]);
Loading classs
Class className = Class.forName(values[0]);
Querying object
obj= webEManager.find(className, id);
I hope it helps. İf I am making something wrong I want to learn the right method. I did not use any try catch block apologize for that.
-
3. Re: Seam 3 & s:entityConverter
mfillion Nov 9, 2010 2:17 PM (in response to mfillion)Thanks for this example, it helps me a lot !