com.webbfontaine.valuewebb.model.validators.tt.FreightValueValidator.java Source code

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.model.validators.tt.FreightValueValidator.java

Source

package com.webbfontaine.valuewebb.model.validators.tt;

import com.webbfontaine.valuewebb.action.Calculations;
import com.webbfontaine.valuewebb.cache.PersistenceUtil;
import com.webbfontaine.valuewebb.model.*;
import com.webbfontaine.valuewebb.model.constants.Constants;
import com.webbfontaine.valuewebb.model.constants.Messages;
import com.webbfontaine.valuewebb.validation.ErrorHandling;
import com.webbfontaine.valuewebb.validation.FacesMessageText;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Example;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

import javax.faces.application.FacesMessage;
import javax.persistence.EntityManager;
import java.math.BigDecimal;
import java.util.ArrayList;
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.
 * Developer: nigiyan
 * Date: 09/12/2011
 */

public class FreightValueValidator implements Constants {
    private TtGen ttGen;
    private EntityManager entityManager;

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

    public FreightValueValidator(TtGen ttGen, EntityManager entityManager) {
        this.ttGen = ttGen;
        this.entityManager = entityManager;
    }

    /**
     * Checks Freigh value against Freight Database considering containers, shipping line, port of load. port of dis.
     * This is done only when TT has containers with all equal size and type and if there is no LCL container.
     */
    public void checkFreightValue() {

        ErrorHandling errorHandling = ErrorHandling.getInstance();

        TtTrans ttTrans = ttGen.getTtTrans();

        //check if error was already added
        List<TtLog> logs = ttGen.getLogs();
        for (TtLog log : logs) {
            if (!StringUtils.isEmpty(log.getMsg()) && log.getMsg().startsWith(Messages.FREIGHT_IN_RANGE)) {
                return;
            }
        }

        if (!ttTrans.checkIfAllContainersHaveSameTypeAndSize()) {
            return;
        }

        Freight freightSearchCriteria = constructFreightSearchCriteria(ttTrans);
        Freight freight = findFreight(freightSearchCriteria);

        if (freight != null) {
            BigDecimal fFreightInUsdForOneContainer = new Calculations().computeFreightForOneContainer(ttGen);

            if (freight.getMaxRate() != null && !(fFreightInUsdForOneContainer.compareTo(freight.getMaxRate()) <= 0
                    && fFreightInUsdForOneContainer.compareTo(freight.getMinRate()) >= 0)) {
                ArrayList<FacesMessageText> msgs = new ArrayList<FacesMessageText>(1);
                msgs.add(new FacesMessageText(null,
                        Messages.FREIGHT_IN_RANGE + ": " + freight.getMinRate() + '-' + freight.getMaxRate(),
                        false));
                errorHandling.addErrorWithLinks("freightF", msgs, errorHandling.getErrorList(), false, true);
            }
        }
    }

    private Freight constructFreightSearchCriteria(TtTrans ttTrans) {
        Container container = ttTrans.getContainers().get(0);

        Freight freight = new Freight();
        freight.setCargoType(container.getCargoType());
        freight.setContainerSize(container.getSize());
        freight.setPod(ttTrans.getDischargePort());
        freight.setPol(ttTrans.getLoadPort());
        freight.setScac(ttTrans.getShipLine());
        return freight;
    }

    private Freight findFreight(Freight criteria) {
        Freight result = null;
        try {
            result = (Freight) PersistenceUtil.getSession(entityManager).createCriteria(Freight.class)
                    .add(Example.create(criteria).excludeZeroes()).uniqueResult();
        } catch (Exception e) {
            LOGGER.error("", e);
            ErrorHandling.addFacesMessage(document_id, "System error on quering Freight DB",
                    FacesMessage.SEVERITY_ERROR);
        }

        if (result == null) {
            LOGGER.debug("Could not find freight record for TT {}", ttGen.getId());
        }

        return result;
    }
}