Java tutorial
package com.webbfontaine.valuewebb.action.pricedb.prd; import com.webbfontaine.valuewebb.action.pricedb.mp.MPCriteriaContainer; import com.webbfontaine.valuewebb.action.rimm.RefSelect; import com.webbfontaine.valuewebb.model.Pd; import com.webbfontaine.valuewebb.model.constants.HumanNames; import com.webbfontaine.valuewebb.model.constants.Statuses; import com.webbfontaine.valuewebb.search.custom.CriteriaContainer; import com.webbfontaine.valuewebb.search.custom.StringConditions; import com.webbfontaine.valuewebb.search.custom.UserCriterion; import com.webbfontaine.valuewebb.search.custom.UserCriterionType; import org.apache.commons.lang3.StringUtils; import org.hibernate.Criteria; import org.hibernate.classic.Session; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Example; import org.hibernate.criterion.Junction; import org.hibernate.criterion.Restrictions; import org.jboss.seam.Component; 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/27/11 */ public class PRDCriteriaContainer extends CriteriaContainer { public Object addUserCriteria() { selectedUserCriteria.add(getDefinedCriterion(definedUserCriterias.get(0).getFullKey())); selectedUserCriteria.add(getDefinedCriterion(definedUserCriterias.get(20).getFullKey())); selectedUserCriteria.add(getDefinedCriterion(definedUserCriterias.get(17).getFullKey())); selectedUserCriteria.add(getDefinedCriterion(definedUserCriterias.get(2).getFullKey())); selectedUserCriteria.add(getDefinedCriterion(definedUserCriterias.get(2).getFullKey())); selectedUserCriteria.add(getDefinedCriterion(definedUserCriterias.get(1).getFullKey())); return null; } @Override protected void defineUserCriterias() { getDefinedUserCriterias().add(getDefinedCriterion("ctyOrig")); getDefinedUserCriterias().add(getDefinedCriterion("hsCodeF")); getDefinedUserCriterias().add(getDefinedCriterion("productDscF")); getDefinedUserCriterias().add(getDefinedCriterion("unitFOBValF")); getDefinedUserCriterias().add(getDefinedCriterion("unitFOBValByUsdF")); getDefinedUserCriterias().add(getDefinedCriterion("ttGen.date")); getDefinedUserCriterias().add(getDefinedCriterion("ttGen.id")); getDefinedUserCriterias().add(getDefinedCriterion("ttGen.fcvrNum")); getDefinedUserCriterias().add(getDefinedCriterion("ttGen.fcvrDat")); getDefinedUserCriterias().add(getDefinedCriterion("unitF")); getDefinedUserCriterias().add(getDefinedCriterion("qtyF")); getDefinedUserCriterias().add(getDefinedCriterion("ttInv.invCur")); getDefinedUserCriterias().add(getDefinedCriterion("used")); getDefinedUserCriterias().add(getDefinedCriterion("qlValLine")); getDefinedUserCriterias().add(getDefinedCriterion("qlClassLine")); getDefinedUserCriterias().add(getDefinedCriterion("ttGen.status")); getDefinedUserCriterias().add(getDefinedCriterion("ttGen.impTradeLevel")); getDefinedUserCriterias().add(getDefinedCriterion("ttGen.impTin")); getDefinedUserCriterias().add(getDefinedCriterion("ttGen.appNam")); getDefinedUserCriterias().add(getDefinedCriterion("ttGen.expTradeLevel")); getDefinedUserCriterias().add(getDefinedCriterion("ttGen.expNam")); getDefinedUserCriterias().add(getDefinedCriterion("ttGen.appTin")); getDefinedUserCriterias().add(getDefinedCriterion("valMethod")); getDefinedUserCriterias().add(getDefinedCriterion("valOpinion")); getDefinedUserCriterias().add(getDefinedCriterion("ttInv.dInvNum")); getDefinedUserCriterias().add(getDefinedCriterion("ttGen.ctySupp")); getDefinedUserCriterias().add(getDefinedCriterion("ttInv.invDat")); getDefinedUserCriterias().add(getDefinedCriterion("ageOfPrice")); getDefinedUserCriterias().add(getDefinedCriterion("unitD")); getDefinedUserCriterias().add(getDefinedCriterion("qtyD")); getDefinedUserCriterias().add(getDefinedCriterion("ttGen.impNam")); } @Override protected UserCriterion getDefinedCriterion(String criteriaKey) { //todo: think about auto composing criteria based on criteriaKey which can have ff values: pd.id, pd.fhsCode, pd.ttGen.idfNum, etc UserCriterion uc = new UserCriterion(); uc.setFullKey(criteriaKey); RefSelect refSelect = (RefSelect) Component.getInstance(RefSelect.class, true); if (StringUtils.equals(criteriaKey, "ctyOrig")) { uc.setType(UserCriterionType.REF); uc.setName("Country of Origin"); uc.setPossibleValues(refSelect.selectCountry()); uc.setCondition(StringConditions.EQ); } if (StringUtils.equals(criteriaKey, "hsCodeF")) { uc.setType(UserCriterionType.STRING); uc.setName("F. HS Code"); uc.setCondition(StringConditions.STARTS_WITH); } if (StringUtils.equals(criteriaKey, "productDscF")) { uc.setType(UserCriterionType.STRING); uc.setName("F. Product Description"); uc.setCondition(StringConditions.CONTAINS); } if (StringUtils.equals(criteriaKey, "unitFOBValF")) { uc.setType(UserCriterionType.BIG_DECIMAL); uc.setName("F. Unit FOB"); } if (StringUtils.equals(criteriaKey, "unitFOBValByUsdF")) { uc.setType(UserCriterionType.BIG_DECIMAL); uc.setName("F. Unit FOB (USD)"); } if (StringUtils.equals(criteriaKey, "ttGen.date")) { uc.setType(UserCriterionType.DATE); uc.setName("TT Date"); } if (StringUtils.equals(criteriaKey, "ttGen.fcvrDat")) { uc.setType(UserCriterionType.DATE); uc.setName("FCVR Date"); } if (StringUtils.equals(criteriaKey, "ttGen.fcvrNum")) { uc.setType(UserCriterionType.STRING); uc.setName("FCVR Number"); } if (StringUtils.equals(criteriaKey, "ttGen.id")) { uc.setType(UserCriterionType.LONG); uc.setName("TT ID"); } if (StringUtils.equals(criteriaKey, "qtyF")) { uc.setType(UserCriterionType.BIG_DECIMAL); uc.setName("F. Quantity"); } if (StringUtils.equals(criteriaKey, "unitF")) { uc.setType(UserCriterionType.REF); uc.setName("F. Unit"); uc.setPossibleValues(refSelect.selectRimmUnit()); } if (StringUtils.equals(criteriaKey, "ttInv.invCur")) { uc.setType(UserCriterionType.REF); uc.setName("Currency"); uc.setPossibleValues(refSelect.selectCurrency()); } if (StringUtils.equals(criteriaKey, "used")) { uc.setType(UserCriterionType.BOOLEAN); uc.setName("Used"); } if (StringUtils.equals(criteriaKey, "qlValLine")) { uc.setType(UserCriterionType.REF); uc.setName("QVL"); uc.setPossibleValues(refSelect.getYesNoCepsUndefined()); } if (StringUtils.equals(criteriaKey, "qlClassLine")) { uc.setType(UserCriterionType.REF); uc.setName("QCL"); uc.setPossibleValues(refSelect.getYesNoCepsUndefined()); } if (StringUtils.equals(criteriaKey, "ttGen.status")) { uc.setType(UserCriterionType.REF); uc.setName("TT Status"); uc.setPossibleValues(new Statuses().getAllTtStatuses()); } if (StringUtils.equals(criteriaKey, "ttGen.impTradeLevel")) { uc.setType(UserCriterionType.REF); uc.setPossibleValues(refSelect.selectTradeLevel()); uc.setName("Importer Trade Level"); } if (StringUtils.equals(criteriaKey, "ttGen.impTin")) { uc.setType(UserCriterionType.STRING); uc.setName("Importer TIN"); uc.setCondition(StringConditions.EQ); } if (StringUtils.equals(criteriaKey, "ttGen.appNam")) { uc.setType(UserCriterionType.STRING); uc.setName("Applicant Name"); } if (StringUtils.equals(criteriaKey, "ttGen.expTradeLevel")) { uc.setType(UserCriterionType.STRING); uc.setName("Exporter Trade Level"); } if (StringUtils.equals(criteriaKey, "ttGen.expNam")) { uc.setType(UserCriterionType.STRING); uc.setName("Exporter name"); uc.setCondition(StringConditions.CONTAINS); } if (StringUtils.equals(criteriaKey, "ttGen.appTin")) { uc.setType(UserCriterionType.STRING); uc.setName("Applicant TIN"); } if (StringUtils.equals(criteriaKey, "valMethod")) { uc.setType(UserCriterionType.REF); uc.setName("Valuation Method"); uc.setPossibleValues(refSelect.getValuationMethod()); } if (StringUtils.equals(criteriaKey, "valOpinion")) { uc.setType(UserCriterionType.REF); uc.setName("Valuation Opinion"); uc.setPossibleValues(refSelect.selectRimmValop()); } if (StringUtils.equals(criteriaKey, "ttInv.dInvNum")) { uc.setType(UserCriterionType.STRING); uc.setName("Invoice Number"); } if (StringUtils.equals(criteriaKey, "ttGen.ctySupp")) { uc.setType(UserCriterionType.STRING); uc.setName("Country of supply"); } if (StringUtils.equals(criteriaKey, "ttInv.invDat")) { uc.setType(UserCriterionType.DATE); uc.setName("Invoice Date"); } if (StringUtils.equals(criteriaKey, "unitD")) { uc.setType(UserCriterionType.REF); uc.setName("Declared Unit"); uc.setPossibleValues(refSelect.selectRimmUnit()); } if (StringUtils.equals(criteriaKey, "qtyD")) { uc.setType(UserCriterionType.BIG_DECIMAL); uc.setName("Declared Quantity"); } if (StringUtils.equals(criteriaKey, "ttGen.impNam")) { uc.setType(UserCriterionType.STRING); uc.setName("Importer name"); } if (StringUtils.equals(criteriaKey, "tInv.invDat")) { uc.setType(UserCriterionType.DATE); uc.setName("Invoice Date"); } if (StringUtils.equals(criteriaKey, "ageOfPrice")) { uc.setType(UserCriterionType.LONG); uc.setName(HumanNames.getName(criteriaKey)); } return uc; } @Override public Criteria transformUserCriteria(Session session) throws Exception { Criteria criteria = session.createCriteria(Pd.class).add(Example.create(new Pd()).excludeZeroes()); criteria.createCriteria("ttGen", "ttGen"); criteria.createCriteria("ttInv", "ttInv"); Junction junction = getMatchAll() ? Restrictions.conjunction() : Restrictions.disjunction(); List<UserCriterion> selectedUserCriteria = getSelectedUserCriteria(); for (UserCriterion userCriterion : selectedUserCriteria) { Criterion transformedCriterion; if ("ageOfPrice".equals(userCriterion.getFullKey())) { transformedCriterion = MPCriteriaContainer.transformAgeOfPriceToPriceDate(userCriterion, getDefinedCriterion("ttInv.invDat")); } else { transformedCriterion = userCriterion.transform(); } if (transformedCriterion != null) { criteria.add(junction.add(transformedCriterion)); } } return criteria; } }