Java tutorial
package com.webbfontaine.valuewebb.action.pricedb.pdss; import com.webbfontaine.valuewebb.action.rimm.RefSelect; import com.webbfontaine.valuewebb.model.constants.HumanNames; import com.webbfontaine.valuewebb.model.util.Utils; 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.apache.commons.lang3.time.DateUtils; 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.*; /** * Copyrights 2002-2012 Webb Fontaine * This software is the proprietary information of Webb Fontaine. * Its use is subject to License terms. * Developer: nigiyan * Date: 07/03/2012 */ public class PdssCriteriaContainer extends CriteriaContainer { private Map<String, String> fakeToRealPropertyMap; private Class entityClass; public void setEntityClass(Class entityClass) { this.entityClass = entityClass; } public void setFakeToRealPropertyMap(Map<String, String> fakeToRealPropertyMap) { this.fakeToRealPropertyMap = fakeToRealPropertyMap; } @Override protected void defineUserCriterias() { getDefinedUserCriterias().add(getDefinedCriterion("hsCode")); getDefinedUserCriterias().add(getDefinedCriterion("used")); getDefinedUserCriterias().add(getDefinedCriterion("productDsc")); getDefinedUserCriterias().add(getDefinedCriterion("ageOfPrice")); getDefinedUserCriterias().add(getDefinedCriterion("unit")); getDefinedUserCriterias().add(getDefinedCriterion("coo")); } @Override public UserCriterion getDefinedCriterion(String criteriaKey) { UserCriterion uc = new UserCriterion(); uc.setFullKey(criteriaKey); RefSelect refSelect = (RefSelect) Component.getInstance(RefSelect.class, true); if (StringUtils.equals(criteriaKey, "hsCode")) { uc.setType(UserCriterionType.STRING); } if (StringUtils.equals(criteriaKey, "productDsc")) { uc.setType(UserCriterionType.STRING); } if (StringUtils.equals(criteriaKey, "ageOfPrice")) { uc.setType(UserCriterionType.LONG); } if (StringUtils.equals(criteriaKey, "used")) { uc.setType(UserCriterionType.BOOLEAN); } if (StringUtils.equals(criteriaKey, "unit")) { uc.setType(UserCriterionType.REF); uc.setPossibleValues(refSelect.selectRimmUnit()); } if (StringUtils.equals(criteriaKey, "coo")) { uc.setType(UserCriterionType.REF); uc.setPossibleValues(refSelect.selectCountry()); } if (uc.getName() == null) { String humanName = HumanNames.getName(criteriaKey); uc.setName(humanName == null ? Utils.toWords(criteriaKey) : humanName); } return uc; } @Override public Criteria transformUserCriteria(Session session) throws Exception { if (fakeToRealPropertyMap == null || entityClass == null) { throw new RuntimeException("Target bean/properties were not initialized"); } Criteria criteria = session.createCriteria(entityClass) .add(Example.create(entityClass.newInstance()).excludeZeroes()); List<String> aliases = new ArrayList<>(); Junction junction = getMatchAll() ? Restrictions.conjunction() : Restrictions.disjunction(); List<UserCriterion> selectedUserCriteria = getSelectedUserCriteria(); for (UserCriterion fakeCriterion : selectedUserCriteria) { if (beanHasCriterionProperty(fakeCriterion.getFullKey())) { UserCriterion realCriterion = prepareUCForBean(fakeCriterion); String alias = extractAlias(realCriterion.getFullKey()); if (alias != null) { if (!aliases.contains(alias)) { aliases.add(alias); criteria.createCriteria(alias, alias); } } Criterion transformedCriterion; if ("ageOfPrice".equals(fakeCriterion.getFullKey())) { transformedCriterion = transformAgeOfPriceToPriceDate(fakeCriterion, realCriterion); } else { transformedCriterion = realCriterion.transform(); } criteria.add(junction.add(transformedCriterion)); } } return criteria; } private static String extractAlias(String fullKey) { int dotIndex = fullKey.indexOf('.'); String alias = null; if (dotIndex != -1) { alias = fullKey.substring(0, dotIndex); } return alias; } private UserCriterion prepareUCForBean(UserCriterion userCriterion) { UserCriterion realUC = new UserCriterion(); realUC.setValue(userCriterion.getValue()); realUC.setType(userCriterion.getType()); realUC.setCondition(userCriterion.getCondition()); realUC.setDiff(userCriterion.getDiff()); realUC.setName(userCriterion.getName()); realUC.setFullKey(fakeToRealPropertyMap.get(userCriterion.getFullKey())); return realUC; } private boolean beanHasCriterionProperty(String fakeName) { return fakeToRealPropertyMap.containsKey(fakeName); } public static Criterion transformAgeOfPriceToPriceDate(UserCriterion ageOfPriceCriterion, UserCriterion criterionToModify) throws Exception { Date today = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH); long userInputedDaysAmount = (Long) ageOfPriceCriterion.getValue(); Date todayMinusUserInputedDaysAmount = DateUtils.addDays(today, -(int) userInputedDaysAmount); criterionToModify.setValue(todayMinusUserInputedDaysAmount); String ageOfPriceCriterionCondition = ageOfPriceCriterion.getCondition(); switch (ageOfPriceCriterionCondition) { case StringConditions.GE: criterionToModify.setCondition(StringConditions.LE); break; case StringConditions.LE: criterionToModify.setCondition(StringConditions.GE); break; case StringConditions.GT: criterionToModify.setCondition(StringConditions.LT); break; case StringConditions.LT: criterionToModify.setCondition(StringConditions.GT); break; default: criterionToModify.setCondition(ageOfPriceCriterionCondition); break; } return criterionToModify.transform(); } }