Java tutorial
package com.webbfontaine.valuewebb.action.pricedb.mp; import com.webbfontaine.valuewebb.action.PdHome; import com.webbfontaine.valuewebb.cache.PersistenceUtil; import com.webbfontaine.valuewebb.model.MarketPrice; import com.webbfontaine.valuewebb.model.Pd; import com.webbfontaine.valuewebb.model.constants.Constants; import com.webbfontaine.valuewebb.props.ApplicationProperties; import org.apache.commons.lang3.StringUtils; import org.hibernate.Criteria; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import org.jboss.seam.Component; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Name; import javax.persistence.EntityManager; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; /** * Copyrights 2002-2011 Webb Fontaine * This software is the proprietary information of Webb Fontaine. * Its use is subject to License terms. * User: nigiyan * Date: 6/9/11 */ @Name("prdConsult") public class PRDConsult { private List<Pd> pdDB = new ArrayList<Pd>(Constants.MAX_RECORDS_IN_ATTACHED_FINDER); private List<MarketPrice> mpDB = new ArrayList<MarketPrice>(Constants.MAX_RECORDS_IN_ATTACHED_FINDER); @In(create = true) EntityManager entityManager; private Pd pd = new Pd(); /** * triggered when user clicks on link to consult the price DB. * * @return null */ public Object searchByCurrentInstance() { PdHome pdHome = (PdHome) Component.getInstance(PdHome.class, false); Pd pd = pdHome.getInstance(); //copy values from currently opened pd to local just to have something to see in custom/local criteria this.pd.setHsCodeF(pd.getHsCodeF()); this.pd.setValMethod(pd.getValMethod()); this.pd.setProductDscF(pd.getProductDscF()); search(pd); return null; } /** * triggered when user changes custom criteria in 'price DB consult' form. * * @return null */ public Object searchByLocalCriteria() { search(pd); return null; } private void search(Pd pd) { clearSearchData(); if (!StringUtils.isEmpty(pd.getHsCodeF())) { // construct criterias and fill lists fillPDs(pd); MarketPrice mp = new MarketPrice(); mp.setHsCode(pd.getHsCodeF()); mp.setVmRuleCode(pd.getValMethod()); mp.setDpd(pd.getProductDscF()); fillMPs(mp); } } private void fillPDs(Pd pd) { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MONTH, -3); Date dateOfTodayMinus3Months = calendar.getTime(); Criteria criteria = PersistenceUtil.getSession(entityManager).createCriteria(Pd.class) .setMaxResults(Constants.MAX_RECORDS_IN_FINDER_RESULT).addOrder(Order.desc("id")); if (pd.getHsCodeF() != null) { criteria.add(Restrictions.like("hsCodeF", pd.getHsCodeF(), MatchMode.START)); } if (pd.getProductDscF() != null) { criteria.add(Restrictions.ilike("productDscF", pd.getProductDscF(), MatchMode.ANYWHERE)); } if (!StringUtils.isEmpty(pd.getValMethod())) { criteria.add(Restrictions.ilike("valMethod", pd.getValMethod(), MatchMode.ANYWHERE)); } if (ApplicationProperties.getAppCountry().equals(Constants.CI)) { // according to ELL-1186, hardcoded restriction added to qlClassLine, qlValLine criteria.add(Restrictions.and( Restrictions.or(Restrictions.eq("qlValLine", "1"), Restrictions.isNull("qlValLine")), Restrictions.or(Restrictions.eq("qlClassLine", "1"), Restrictions.isNull("qlClassLine")))); } Criteria ttCrit = criteria.createCriteria("ttGen"); ttCrit.add(Restrictions.ge("date", dateOfTodayMinus3Months)); pdDB.addAll(criteria.list()); } private void fillMPs(MarketPrice mp) { Criteria criteria = PersistenceUtil.getSession(entityManager).createCriteria(MarketPrice.class); criteria.createCriteria("sourceOfPrice", "sourceOfPrice"); criteria.setMaxResults(Constants.MAX_RECORDS_IN_FINDER_RESULT).addOrder(Order.desc("priceDate")); if (mp.getHsCode() != null) { criteria.add(Restrictions.like("hsCode", mp.getHsCode(), MatchMode.START)); } if (mp.getDpd() != null) { criteria.add(Restrictions.ilike("dpd", mp.getDpd(), MatchMode.ANYWHERE)); } if (!StringUtils.isEmpty(mp.getVmRuleCode())) { criteria.add(Restrictions.eq("vmRuleCode", mp.getVmRuleCode())); } mpDB.addAll(criteria.list()); } public void clearSearchData() { pdDB.clear(); mpDB.clear(); } public List<Pd> getPdDB() { return pdDB; } public List<MarketPrice> getMpDB() { return mpDB; } public Pd getPd() { return pd; } public void setPd(Pd pd) { this.pd = pd; } }