com.webbfontaine.valuewebb.action.pricedb.mp.MPCriteriaContainer.java Source code

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.action.pricedb.mp.MPCriteriaContainer.java

Source

package com.webbfontaine.valuewebb.action.pricedb.mp;

import com.webbfontaine.valuewebb.action.rimm.RefSelect;
import com.webbfontaine.valuewebb.model.MarketPrice;
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.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 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 MPCriteriaContainer extends CriteriaContainer {

    public Object addUserCriteria() {
        selectedUserCriteria.add(getDefinedCriterion(definedUserCriterias.get(2).getFullKey()));
        selectedUserCriteria.add(getDefinedCriterion(definedUserCriterias.get(6).getFullKey()));
        selectedUserCriteria.add(getDefinedCriterion(definedUserCriterias.get(6).getFullKey()));
        selectedUserCriteria.add(getDefinedCriterion(definedUserCriterias.get(5).getFullKey()));
        selectedUserCriteria.add(getDefinedCriterion(definedUserCriterias.get(11).getFullKey()));
        selectedUserCriteria.add(getDefinedCriterion(definedUserCriterias.get(1).getFullKey()));

        return null;
    }

    @Override
    protected void defineUserCriterias() {
        getDefinedUserCriterias().add(getDefinedCriterion("id"));
        getDefinedUserCriterias().add(getDefinedCriterion("sourceOfPrice.cod"));
        getDefinedUserCriterias().add(getDefinedCriterion("ctyOrigin"));
        getDefinedUserCriterias().add(getDefinedCriterion("ctySupply"));
        getDefinedUserCriterias().add(getDefinedCriterion("priceDate"));
        getDefinedUserCriterias().add(getDefinedCriterion("hsCode"));
        getDefinedUserCriterias().add(getDefinedCriterion("dpd"));
        getDefinedUserCriterias().add(getDefinedCriterion("unit"));
        getDefinedUserCriterias().add(getDefinedCriterion("retailerPrice"));
        getDefinedUserCriterias().add(getDefinedCriterion("retailerRate"));
        getDefinedUserCriterias().add(getDefinedCriterion("vmRuleCode"));
        getDefinedUserCriterias().add(getDefinedCriterion("tradeLevel"));
        getDefinedUserCriterias().add(getDefinedCriterion("sourceRating"));
        getDefinedUserCriterias().add(getDefinedCriterion("priceRating"));
        getDefinedUserCriterias().add(getDefinedCriterion("retailerPriceCur"));
        getDefinedUserCriterias().add(getDefinedCriterion("retailerPriceUsd"));
        getDefinedUserCriterias().add(getDefinedCriterion("used"));

        getDefinedUserCriterias().add(getDefinedCriterion("ageOfPrice"));
    }

    @Override
    protected UserCriterion getDefinedCriterion(String criteriaKey) {
        UserCriterion uc = new UserCriterion();

        uc.setFullKey(criteriaKey);

        RefSelect refSelect = (RefSelect) Component.getInstance(RefSelect.class, true);

        if (StringUtils.equals(criteriaKey, "id")) {
            uc.setName("ID");
            uc.setType(UserCriterionType.LONG);
        }

        if (StringUtils.equals(criteriaKey, "sourceOfPrice.cod")) {
            uc.setName("Source of Price");
            uc.setType(UserCriterionType.REF);
            uc.setPossibleValues(refSelect.selectSourceOfPrice());
            uc.setCondition(StringConditions.EQ);
        }

        if (StringUtils.equals(criteriaKey, "ctyOrigin")) {
            uc.setName("Country of Origin");
            uc.setType(UserCriterionType.REF);
            uc.setPossibleValues(refSelect.selectCountry());
            uc.setCondition(StringConditions.EQ);
        }

        if (StringUtils.equals(criteriaKey, "ctySupply")) {
            uc.setName("Country of supply");
            uc.setType(UserCriterionType.REF);
            uc.setPossibleValues(refSelect.selectCountry());
        }

        if (StringUtils.equals(criteriaKey, "priceDate")) {
            uc.setName("Price Date");
            uc.setType(UserCriterionType.DATE);
        }

        if (StringUtils.equals(criteriaKey, "hsCode")) {
            uc.setName("HS Code");
            uc.setType(UserCriterionType.STRING);
            uc.setCondition(StringConditions.STARTS_WITH);
        }

        if (StringUtils.equals(criteriaKey, "dpd")) {
            uc.setName("Declared Product Description");
            uc.setType(UserCriterionType.STRING);
            uc.setCondition(StringConditions.CONTAINS);
        }

        if (StringUtils.equals(criteriaKey, "unit")) {
            uc.setType(UserCriterionType.REF);
            uc.setPossibleValues(refSelect.selectRimmUnit());
        }

        if (StringUtils.equals(criteriaKey, "retailerPrice")) {
            uc.setType(UserCriterionType.BIG_DECIMAL);
        }

        if (StringUtils.equals(criteriaKey, "retailerRate")) {
            uc.setType(UserCriterionType.BIG_DECIMAL);
        }

        if (StringUtils.equals(criteriaKey, "vmRuleCode")) {
            uc.setType(UserCriterionType.REF);
            uc.setName("VM");
            uc.setPossibleValues(refSelect.getValuationMethodForMP());
        }

        if (StringUtils.equals(criteriaKey, "tradeLevel")) {
            uc.setType(UserCriterionType.REF);
            uc.setPossibleValues(refSelect.selectTradeLevel());
            uc.setCondition(StringConditions.EQ);
        }

        if (StringUtils.equals(criteriaKey, "sourceRating")) {
            uc.setType(UserCriterionType.REF);
            uc.setPossibleValues(refSelect.selectSourceRating());
        }

        if (StringUtils.equals(criteriaKey, "priceRating")) {
            uc.setType(UserCriterionType.REF);
            uc.setPossibleValues(refSelect.selectPriceRating());
        }

        if (StringUtils.equals(criteriaKey, "retailerPriceCur")) {
            uc.setType(UserCriterionType.REF);
            uc.setName("Retailer Price Currency");
            uc.setPossibleValues(refSelect.selectCurrency());
        }

        if (StringUtils.equals(criteriaKey, "retailerPriceUsd")) {
            uc.setName("Retailer Price USD");
            uc.setType(UserCriterionType.BIG_DECIMAL);
        }

        if (StringUtils.equals(criteriaKey, "fob")) {
            uc.setName("FOB");
            uc.setType(UserCriterionType.BIG_DECIMAL);
        }

        if (StringUtils.equals(criteriaKey, "fobUsd")) {
            uc.setType(UserCriterionType.BIG_DECIMAL);
            uc.setName("FOB USD");
        }

        if (StringUtils.equals(criteriaKey, "ageOfPrice")) {
            uc.setType(UserCriterionType.LONG);
            uc.setName("Age of Price");
        }

        if (StringUtils.equals(criteriaKey, "used")) {
            uc.setType(UserCriterionType.BOOLEAN);
        }

        if (uc.getName() == null) {
            uc.setName(Utils.toWords(criteriaKey));
        }

        return uc;
    }

    @Override
    public Criteria transformUserCriteria(Session session) throws Exception {

        Criteria criteria = session.createCriteria(MarketPrice.class)
                .add(Example.create(new MarketPrice()).excludeZeroes());
        criteria.createCriteria("sourceOfPrice", "sourceOfPrice");

        Junction junction = getMatchAll() ? Restrictions.conjunction() : Restrictions.disjunction();

        List<UserCriterion> selectedUserCriteria = getSelectedUserCriteria();
        for (UserCriterion userCriterion : selectedUserCriteria) {

            Criterion transformedCriterion;
            if ("ageOfPrice".equals(userCriterion.getFullKey())) {
                transformedCriterion = transformAgeOfPriceToPriceDate(userCriterion,
                        getDefinedCriterion("priceDate"));
            } else {
                transformedCriterion = userCriterion.transform();
            }

            if (transformedCriterion != null) {
                criteria.add(junction.add(transformedCriterion));
            }
        }

        return criteria;
    }

    public static Criterion transformAgeOfPriceToPriceDate(UserCriterion ageOfPriceCriterion,
            UserCriterion criterionToModify) throws Exception {

        if (ageOfPriceCriterion.getValue() == null) {
            return null;
        }

        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();
    }
}