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

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.action.pricedb.mp.MPFillUtils.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.constants.Messages;
import com.webbfontaine.valuewebb.model.rimm.SopVm;
import com.webbfontaine.valuewebb.model.util.Utils;
import com.webbfontaine.valuewebb.validation.ErrorHandling;
import org.apache.commons.lang3.StringUtils;
import org.jboss.seam.Component;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

import java.math.BigDecimal;
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: 02/03/2012
 */

@Name("mpUtils")
@BypassInterceptors
public class MPFillUtils {

    private static final String FOBINT = "FOBINT";
    private static final String FOBOTH = "FOBOTH";

    private static final Log LOGGER = Logging.getLog(MPFillUtils.class);

    public Object processSPChange() {
        MarketPriceHome marketPriceHome = (MarketPriceHome) Component.getInstance(MarketPriceHome.class, false);
        MarketPrice mp = marketPriceHome.getInstance();
        if (mp != null) {
            setValuesForSourceOfPrice(mp);

            List<SopVm> vMs = RefSelect.getInstance().getSopVMs(mp.getSourceOfPrice().getCod());
            if (vMs.size() == 1) {
                MarketPriceHome.getErrorHandling().clearErrorByUIFieldId("vmRuleCode");
                mp.setVmRuleCode(vMs.get(0).getVm());
                resetInstanceData();
            }
        }

        return null;
    }

    public Object resetInstanceData() {
        MarketPriceHome marketPriceHome = (MarketPriceHome) Component.getInstance(MarketPriceHome.class, false);
        MarketPrice mp = marketPriceHome.getInstance();

        if (mp != null) {
            clearErrors();

            setValuesForVM(mp);

            MPCalculations.computeAll(mp);
        }

        return "";
    }

    private static void clearErrors() {
        ErrorHandling errorHandling = MarketPriceHome.getErrorHandling();
        errorHandling.removeErrorByMessage(Messages.EXC_RATE_INVALID);
        errorHandling.removeErrorByFieldId("hsCode");
        errorHandling.removeErrorByFieldId("fob");
        errorHandling.removeErrorByFieldId("fobUSD");
        errorHandling.removeErrorByFieldId("retailerPrice");
        errorHandling.removeErrorByFieldId("retailerRate");
        errorHandling.removeErrorByFieldId("retailerPriceUsd");
        errorHandling.removeErrorByFieldId("retailerRate");
    }

    private static void setValuesForVM(MarketPrice mp) {
        String vmCode = StringUtils.trimToEmpty(mp.getVmRuleCode());

        mp.setRetailerPriceCur(null);
        switch (vmCode) {
        case "4":
            mp.setFob(null);
            mp.setFobUSD(null);
            mp.setRetailerPriceCur(Utils.getNationalCurrencyName());
            mp.setRetailPriceCurExcRate(BigDecimal.ONE);
            break;
        case "6":
        case "V":
            mp.setRetailerPrice(null);
            mp.setRetailerRate(null);

            clearCalculableFields(mp);
            break;
        case "X":
            mp.setRetailerPriceUsd(null);
            mp.setRetailerRate(null);
            mp.setFobUSD(null);

            clearCalculableFields(mp);
            break;
        default:
            LOGGER.debug("VM is unknown or not set {}", vmCode);
        }
    }

    private static void setValuesForSourceOfPrice(MarketPrice mp) {
        mp.setSourceRating(mp.getSourceOfPrice().getSourceRating());
        mp.setPriceRating(mp.getSourceOfPrice().getPriceRating());

        if (mp.getSourceOfPrice().getCod().equals(FOBINT)) {
            mp.setComments("Price Source: Researcher to Enter Link");
        } else {
            if (mp.getSourceOfPrice().getCod().equals(FOBOTH)) {
                mp.setComments("Data can not be used singularly as basis for valuation");
            } else {
                mp.setComments(null);
            }
        }
    }

    private static void clearCalculableFields(MarketPrice mp) {

        mp.setDutiesAndTaxes(null);
        mp.setWholesalerPrice(null);
        mp.setLandedCost(null);
        mp.setDutyPaid(null);
        mp.setCif(null);
        mp.setCfr(null);
        mp.setFob(null);
        mp.setFobGcnet(null);
        mp.setFactor(null);
        mp.setRetailerPriceUsd(null);
    }

}