Java tutorial
/* * Copyright 2010, 2011 Renaud Brub * * This file is part of PIGE. * * PIGE is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * PIGE is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with PIGE. If not, see <http://www.gnu.org/licenses/>. */ package ca.qc.cegepoutaouais.tge.pige.server; import ca.qc.cegepoutaouais.tge.pige.client.PIGE; import ca.qc.cegepoutaouais.tge.pige.dao.pojos.Role; import ca.qc.cegepoutaouais.tge.pige.dao.pojos.User; import com.extjs.gxt.ui.client.data.FilterConfig; import java.util.ArrayDeque; import java.util.Deque; import java.util.List; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.apache.log4j.Logger; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Restrictions; import org.hibernate.criterion.SimpleExpression; /** * * @author Renaud Brub */ public class PigeHibernateUtil { private static final Logger logger = LogHelper.getLogger(PigeHibernateUtil.class); private static final SessionFactory sessionFactory; static { /* * Chargement du session factory. */ try { Context initialCntx = new InitialContext(); sessionFactory = (SessionFactory) initialCntx.lookup(PIGE.HIBERNATE_JNDI_SESSION_FACTORY_NAME); logger.info("Chargement du sessionFactory par JNDI russi avec le nom: " + PIGE.HIBERNATE_JNDI_SESSION_FACTORY_NAME + "."); } catch (NamingException e) { throw new RuntimeException(e); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static Session getCurrentSession() { return sessionFactory.getCurrentSession(); } public static Session openSession() { return sessionFactory.openSession(); } public static Criterion buildFilterCriterion(List<FilterConfig> filterConfigList) { if (filterConfigList == null || filterConfigList.size() <= 0) { return null; } FilterConfig matchModeConfig = null; for (FilterConfig fc : filterConfigList) { String field = fc.getField(); if (field != null && field.equals(PIGE.MATCH_MODE)) { matchModeConfig = fc; filterConfigList.remove(fc); break; } } MatchMode matchMode = MatchMode.ANYWHERE; //if (matchModeConfig != null && (Boolean) matchModeConfig.getValue()) { // matchMode = MatchMode.EXACT; //} Criterion filterCriteria = null; Deque<FilterConfig> queue = new ArrayDeque<FilterConfig>(filterConfigList); FilterConfig filterConfig = null; while (queue.peekFirst() != null) { filterConfig = queue.pollFirst(); if (filterConfig.getField() == null || filterConfig.getValue() == null) { continue; } // Test pour chercher par nom de rle. if (filterConfig.getField().equals(User.ROLE_REF)) { ManagementServiceImpl rpcService = new ManagementServiceImpl(); List<Role> roles = rpcService.getAllRole(); String roleName = (String) filterConfig.getValue(); Boolean roleFound = false; for (Role role : roles) { if (role.getName().equalsIgnoreCase(roleName)) { filterConfig.setValue(role); roleFound = true; break; } } if (!roleFound) { continue; } } // if (filterCriteria == null) { filterCriteria = createLikeRestriction(filterConfig, matchMode); //Restrictions.like( //filterConfig.getField(), //(String) filterConfig.getValue(), matchMode); if (queue.peekFirst() != null) { filterConfig = queue.pollFirst(); filterCriteria = Restrictions.or(filterCriteria, createLikeRestriction(filterConfig, matchMode)); //Restrictions.like(filterConfig.getField(), //(String) filterConfig.getValue(), matchMode)); } } else { filterCriteria = Restrictions.or(filterCriteria, createLikeRestriction(filterConfig, matchMode)); //Restrictions.like(filterConfig.getField(), //(String) filterConfig.getValue(), matchMode)); } } return filterCriteria; } private static SimpleExpression createLikeRestriction(FilterConfig config, MatchMode matchMode) { if (config.getField().equals(User.ROLE_REF)) { return Restrictions.like(config.getField(), (Role) config.getValue()); } else { return Restrictions.like(config.getField(), (String) config.getValue(), matchMode); } } }