-
1. Re: How to get value from expression?
mohtisham Jul 16, 2010 2:36 AM (in response to mohtisham)Well, 158 views on this but no one has been able to give the answer for this post that's amazing...
-
2. Re: How to get value from expression?
nbelaevski Jul 16, 2010 4:12 AM (in response to mohtisham)1 of 1 people found this helpfulHi,
You are already using correct code to obtain expression value, but looking at getExpressionString() Javadoc:
This method does not provide sufficient information to re-create an expression. Two different expressions can have exactly the same expression string but different function mappings. Serialization should be used to save and restore the state of an
Expression
.Is that the thing you are actually doing? Code doesn't provide enough information.
-
3. Re: How to get value from expression?
mohtisham Jul 16, 2010 6:42 AM (in response to nbelaevski)Well, that might be the problem now i'll try to it in that way but i don't know why the code doesn't work as it was working in online example sippets.
-
4. Re: How to get value from expression?
nbelaevski Jul 16, 2010 6:55 PM (in response to mohtisham)...but i don't know why the code doesn't work as it was working in online example sippets.
Can you please post a link to the online example code?
-
5. Re: How to get value from expression?
mohtisham Jul 19, 2010 2:34 AM (in response to nbelaevski)Well, i have taken it from the Richfaces sample code the location was as:
Richfaces\samples\richfaces-demo\src\main\java\org\richfaces\demo\modifiableModel\BaseModifiableHibernateDataModel.java
In the file as the path is mentioned above you can find the following method.
private String getPropertyName(FacesContext facesContext, Expression expression) {
try {
return (String) ((ValueExpression) expression).getValue(facesContext.getELContext());
} catch (ELException e) {
throw new FacesException(e.getMessage(), e);
}
}
-
6. Re: How to get value from expression?
nbelaevski Jul 19, 2010 4:29 AM (in response to mohtisham)I see, it evaluates expression provided by the component, but doesn't re-create it from scratch.
-
7. Re: How to get value from expression?
mohtisham Jul 19, 2010 5:27 AM (in response to nbelaevski)Do let me know if this example works for you in any way
-
8. Re: How to get value from expression?
nbelaevski Jul 19, 2010 8:26 AM (in response to mohtisham)Well, I've written this class and it worked Also it works on livedemo, so have you added all required page code?
-
9. Re: How to get value from expression?
mohtisham Jul 20, 2010 2:23 AM (in response to nbelaevski)Well, i don' t think there was anything special to page code. I have given the required things; what can create probem can you mention it?
-
10. Re: How to get value from expression?
nbelaevski Jul 20, 2010 4:25 AM (in response to mohtisham)No problems, but on the livedemo view to which this code is related, there are column names configured to be passed to model via EL-expressions. If they are removed, demo won't work.
-
11. Re: How to get value from expression?
mohtisham Jul 20, 2010 4:29 AM (in response to nbelaevski)Well, i have provide the name but it didn't work at all don't know why.
-
12. Re: How to get value from expression?
ilya_shaikovsky Jul 21, 2010 7:31 AM (in response to mohtisham)so, your expression can't be evaluated in context.. And it's difficult to tell why just looking to the method which calls evaluation(as Nick told it's correct). Please provide more code details.
-
13. Re: How to get value from expression?
mohtisham Jul 22, 2010 7:30 AM (in response to ilya_shaikovsky)Well, i have provided my code now which is totally in changed form as it was in the example. I hope i have provided all the related code if anything left do let me know.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j">
<ui:composition template="layout.xhtml">
<ui:define name="title">Users List</ui:define>
<ui:define name="content"><h:form>
<rich:dataTable id="users" value="#{usersListBean}" var="user"
width="300px" rows="4" reRender="datascroller">
<f:facet name="header">
<h:outputText value="Users List" />
</f:facet>
<rich:column sortBy="#{userName}">
<f:facet name="header">
<h:outputText value="User Name" />
</f:facet>
<h:outputText value="#{user.userName}" />
</rich:column>
<rich:column sortBy="#{userName}">
<f:facet name="header">
<h:outputText value="First Name" />
</f:facet>
<h:outputText value="#{user.firstName}" />
</rich:column>
<rich:column sortBy="#{lastName}">
<f:facet name="header">
<h:outputText value="Last Name" />
</f:facet>
<h:outputText value="#{user.lastName}" />
</rich:column>
<rich:column sortBy="#{email}">
<f:facet name="header">
<h:outputText value="Email" />
</f:facet>
<h:outputText value="#{user.email}" />
</rich:column>
<rich:column sortBy="#{email}">
<f:facet name="header">
<h:outputText value="Home Address" />
</f:facet>
<h:outputText value="#{user.address}" />
</rich:column>
<f:facet name="footer">
<rich:datascroller id="datascroller" for="users"
selectedStyle="font-weight:bold" />
</f:facet>
</rich:dataTable>
</h:form></ui:define>
</ui:composition>
</html>package backingBeans;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
import manager.UserManager;
import org.ajax4jsf.model.DataVisitor;
import org.ajax4jsf.model.ExtendedDataModel;
import org.ajax4jsf.model.Range;
import org.ajax4jsf.model.SequenceRange;
import org.richfaces.model.FilterField;
import org.richfaces.model.Modifiable;
import org.richfaces.model.SortField2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("usersListBean")
@Scope("request")
public class UsersListBean extends ExtendedDataModel implements Modifiable {
private Integer currentPk;
private Map<Integer, UsersDTO> wrappedData = new HashMap<Integer, UsersDTO>();
private SequenceRange cachedRange;
private List<UsersDTO> cachedItems;
private List<SortField2> sortFields;
private UserManager userManager;
@Autowired
public UsersListBean(UserManager userManager) {
this.userManager = userManager;
}
@Override
public Object getRowKey() {
return currentPk;
}
@Override
public void setRowKey(Object key) {
if (key != null) {
this.currentPk = (Integer) key;
}
}
private static boolean areEqualRanges(SequenceRange range1,
SequenceRange range2) {
if (range1 == null || range2 == null) {
return range1 == null && range2 == null;
} else {
return range1.getFirstRow() == range2.getFirstRow()
&& range1.getRows() == range2.getRows();
}
}
@Override
public void walk(FacesContext facesContext, DataVisitor visitor,
Range range, Object argument) throws IOException {
SequenceRange sequenceRange = (SequenceRange) range;
if (this.cachedItems == null
|| !areEqualRanges(this.cachedRange, sequenceRange)) {
Integer start = 0;
Integer limit = 0;
if (sequenceRange != null) {
start = sequenceRange.getFirstRow();
limit = sequenceRange.getRows();
}
this.cachedRange = sequenceRange;
this.cachedItems = userManager.userPagingList(start, limit,
sortFields);
}
for (UsersDTO item : this.cachedItems) {
visitor.process(facesContext, item.getUserID(), argument);
}
}
@Override
public int getRowCount() {
return userManager.usersRowCount();
}
@Override
public Object getRowData() {
if (currentPk == null) {
return null;
} else {
UsersDTO ret = wrappedData.get(currentPk);
if (ret == null) {
ret = getUserByPk(currentPk);
wrappedData.put(currentPk, ret);
return ret;
} else {
return ret;
}
}
}
public UsersDTO getUserByPk(Integer pk) {
List<UsersDTO> list = userManager.userPagingList(null, null, null);
for (UsersDTO item : list) {
if (item.getUserID().equals(pk)) {
return item;
}
}
throw new RuntimeException("User Item pk=" + pk.toString()
+ " not found");
}
@Override
public int getRowIndex() {
return -1;
}
@Override
public Object getWrappedData() {
return null;
}
@Override
public boolean isRowAvailable() {
return (this.currentPk != null);
}
@Override
public void setRowIndex(int arg0) {
}
@Override
public void setWrappedData(Object arg0) {
}
@Override
public void modify(List<FilterField> filterFields,
List<SortField2> sortFields) {
this.sortFields = sortFields;
this.cachedItems = null;
this.cachedRange = null;
}
public UserManager getUserManager() {
return userManager;
}
}
package backingBeans;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;@Component("usersDTO")
@Scope("request")
public class UsersDTO {
public UsersDTO(){
}
private Integer userID;
private String userName, firstName, lastName, email, address, city, state,
country;public Integer getUserID() {
return userID;
}public void setUserID(Integer userID) {
this.userID = userID;
}public String getUserName() {
return userName;
}public void setUserName(String userName) {
this.userName = userName;
}public String getFirstName() {
return firstName;
}public void setFirstName(String firstName) {
this.firstName = firstName;
}public String getLastName() {
return lastName;
}public void setLastName(String lastName) {
this.lastName = lastName;
}public String getEmail() {
return email;
}public void setEmail(String email) {
this.email = email;
}public String getAddress() {
return address;
}public void setAddress(String address) {
this.address = address;
}public String getCity() {
return city;
}public void setCity(String city) {
this.city = city;
}public String getState() {
return state;
}public void setState(String state) {
this.state = state;
}public String getCountry() {
return country;
}public void setCountry(String country) {
this.country = country;
}
}package manager;
import java.util.List;
import org.richfaces.model.SortField2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;import DAO.Address;
import DAO.City;
import DAO.Contact;
import DAO.Country;
import DAO.State;
import DAO.User;
import backingBeans.UsersDTO;
import backingBeans.Page.Order;@Component("UserManager")
@Scope("singleton")
@Transactional
public class UserManager {private UserDAO userDAO;
public UserManager() {
}
@Autowired
public UserManager(UserDAO userDAO) {
this.userDAO = userDAO;
}public Integer userLogin(String userName, String password) {
// it fetch the users
public List<User> getUsers() {
return userDAO.getUsers();
}// it fetch the users
public List<User> getUsers(Integer start, Integer limit, String orderBy,
Order order) {
return userDAO.getUsers(start, limit, orderBy, order);
}// used for getting the row count of the users
public Integer usersRowCount() {
return userDAO.usersRowCount();
}// used for listing the users
public List<UsersDTO> userPagingList(Integer start, Integer limit,
List<SortField2> sortFields) {
return userDAO.userPagingList(start, limit, sortFields);
}
//getting the country list
public List<Country> getCountryList() {
return userDAO.getCountry();
}
//getting the state list
public List<State> getStateList(Integer countryID) {
return userDAO.getState(countryID);
}
//getting the city list
public List<City> getCityList(Integer lastStateID) {
return userDAO.getCity(lastStateID);
}
}package manager;
import java.util.List;
import javax.el.Expression;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinFragment;
import org.hibernate.transform.Transformers;
import org.richfaces.model.Ordering;
import org.richfaces.model.SortField2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;import DAO.Address;
import DAO.City;
import DAO.Contact;
import DAO.Country;
import DAO.State;
import DAO.User;
import backingBeans.UsersDTO;
import backingBeans.Page.Order;@Component("UserDAO")
@Scope("singleton")
public class UserDAO extends HibernateDaoSupport {public UserDAO() {
}
@Autowired
public UserDAO(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}// it fetch the users
public List<User> getUsers() {Session session = null;
List<User> users = null;try {
session = getHibernateTemplate().getSessionFactory()
.getCurrentSession();
Criteria criteria = session.createCriteria(User.class, "user");
criteria.createCriteria("user.userAddresses", "userAddress",
JoinFragment.INNER_JOIN);criteria.setFetchMode("userAddress", FetchMode.JOIN);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
// criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("userName")));
users = criteria.list();} catch (Exception e) {
e.printStackTrace();
}
return users;
}// paging the users
public List<User> getUsers(Integer start, Integer limit, String orderBy,
Order order) {Session session = null;
List<User> users = null;try {
session = getHibernateTemplate().getSessionFactory()
.getCurrentSession();
Criteria criteria = session.createCriteria(User.class, "user");
criteria.createCriteria("user.userAddresses", "userAddress",
JoinFragment.LEFT_OUTER_JOIN);criteria.setFetchMode("userAddress", FetchMode.JOIN);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);criteria.setFirstResult(start);
criteria.setMaxResults(limit);if (orderBy != null && orderBy.length() > 0) {
if (order.equals(Order.ASC)) {
// criteria.addOrder( Property.forName(orderBy).asc());
criteria.addOrder(org.hibernate.criterion.Order
.asc(orderBy));
} else {
// criteria.addOrder( Property.forName(orderBy).desc());
criteria.addOrder(org.hibernate.criterion.Order
.desc(orderBy));
}
}// criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("userName")));
users = criteria.list();} catch (Exception e) {
e.printStackTrace();
}
return users;
}// creating criteria for listing users
public Criteria createCriteria() {Session session = null;
try {
session = getHibernateTemplate().getSessionFactory()
.getCurrentSession();return session.createCriteria(User.class).createAlias("contact",
"con").createAlias("userAddresses", "ad").add(
Restrictions.eq("ad.type", 0))
.setProjection(
Projections.projectionList().add(
Projections.property("userID"), "userID")
.add(Projections.property("userName"),
"userName").add(
Projections.property("firstName"),
"firstName").add(
Projections.property("lastName"),
"lastName").add(
Projections.property("con.email"),
"email").add(
Projections.property("ad.address"),
"address").add(
Projections.property("ad.city"),
"city").add(
Projections.property("ad.state"),
"state").add(
Projections.property("ad.country"),
"country")).setResultTransformer(
Criteria.DISTINCT_ROOT_ENTITY)
.setResultTransformer(
Transformers.aliasToBean(UsersDTO.class));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}// applying the criteria on the users list
public List<UsersDTO> userPagingList(Integer start, Integer limit,
List<SortField2> sortFields) {Criteria criteria = createCriteria();
appendSorts(criteria, sortFields);
if (start != null && limit != null) {
criteria.setFirstResult(start);
if (limit > 0) {
criteria.setMaxResults(limit);
}
}return criteria.list();
}
// sorting the users list
private void appendSorts(Criteria criteria, List<SortField2> sortFields) {
if (sortFields != null) {
for (SortField2 sortField : sortFields) {Expression expression = sortField.getExpression();
String expressionString = expression.getExpressionString();
String field;
if (expression.isLiteralText()) {
field = expressionString;
} else {
field = expressionString.replaceAll("[#|$]\\{", "")
.replaceAll("\\}", "");
}Ordering ordering = sortField.getOrdering();
if (Ordering.ASCENDING.equals(ordering)) {
criteria.addOrder(org.hibernate.criterion.Order.asc(field));
} else {
criteria
.addOrder(org.hibernate.criterion.Order.desc(field));
}
}
}
}// getting the row count of users
public Integer usersRowCount() {Session session = null;
try {
session = getHibernateTemplate().getSessionFactory()
.getCurrentSession();
Criteria criteria = session.createCriteria(User.class);
criteria.setProjection(Projections.rowCount());
return ((Number) criteria.list().get(0)).intValue();} catch (Exception e) {
e.printStackTrace();
}
return 0;
}public List<Country> getCountry() {
Session session = null;
try {
session = getHibernateTemplate().getSessionFactory()
.getCurrentSession();
Criteria criteria = session.createCriteria(Country.class);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
return criteria.list();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}public List<State> getState(Integer countryID) {
Session session = null;
try {
session = getHibernateTemplate().getSessionFactory()
.getCurrentSession();
Criteria criteria = session.createCriteria(State.class);
criteria.add(Restrictions.eq("country.id", countryID));
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
return criteria.list();} catch (Exception e) {
e.printStackTrace();
}
return null;
}public List<City> getCity(Integer stateID) {
Session session = null;
try {
session = getHibernateTemplate().getSessionFactory()
.getCurrentSession();
Criteria criteria = session
.createCriteria(City.class);
criteria.add(Restrictions.eq("state.stateID", stateID));
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
return criteria.list();
} catch (Exception e) {
e.printStackTrace();
}return null;
}}
-
14. Re: How to get value from expression?
nbelaevski Jul 22, 2010 7:53 AM (in response to mohtisham)You don't have userName variable somewhere in scope, so try rich:column sortBy="userName"