com.webbfontaine.valuewebb.action.pricedb.freight.FreightList.java Source code

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.action.pricedb.freight.FreightList.java

Source

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

import com.webbfontaine.valuewebb.action.tt.TtGenHome;
import com.webbfontaine.valuewebb.model.Freight;
import com.webbfontaine.valuewebb.model.TtGen;
import com.webbfontaine.valuewebb.model.constants.Constants;
import com.webbfontaine.valuewebb.search.FreightCriteria;
import com.webbfontaine.valuewebb.search.ObjectContainer;
import org.apache.commons.lang3.StringUtils;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityQuery;

import javax.faces.event.ActionEvent;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.Arrays;
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: May 10, 2010
 */

@Name("freightList")
public class FreightList extends EntityQuery<Freight> {

    private List<Freight> resultList = null;
    private Long resultCount = null;

    static List<Freight> emptyResultList = new ArrayList<Freight>();

    @In(create = true)
    ObjectContainer objectContainer;

    @In(create = true)
    FreightCriteria freightCriteria;

    private static final String EJBQL_TEMPLATE = "select freight from Freight freight";

    private static final String[] RESTRICTIONS = {
            "lower(freight.scac) like lower(concat(#{freightList.freight.scac},'%'))",
            "lower(freight.pol) like lower(concat(#{freightList.freight.pol},'%'))",
            "lower(freight.pod) like lower(concat(#{freightList.freight.pod},'%'))",
            "freight.containerSize = #{freightList.freight.containerSize}",
            "freight.cargoType = #{freightList.freight.cargoType}", };

    public FreightList() {
        setEjbql(EJBQL_TEMPLATE);
        setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
        setMaxResults(Constants.MAX_RECORDS_IN_FINDER_RESULT);
    }

    public Freight getFreight() {
        return (Freight) objectContainer.get(Freight.class, true);
    }

    @Override
    protected Query createQuery() {
        prepareQuery();
        Query query = super.createQuery();
        tuneQuery(query);
        return query;
    }

    @Override
    protected Query createCountQuery() {
        prepareQuery();
        Query query = super.createCountQuery();
        tuneQuery(query);
        return query;
    }

    private void tuneQuery(Query query) {
        query.setHint("org.hibernate.readOnly", Boolean.TRUE);
    }

    private void prepareQuery() {
    }

    /**
     * If not marked as factory in PAGE scope, then on ajax request (e.g. on gui layout change/persist)<br/>
     * this method would be invoked, because search result is stored in temprary conversation scope.
     *
     * @return result list
     */
    @Override
    public List<Freight> getResultList() {
        if (StringUtils.isEmpty(freightCriteria.getActiveFor())) { // case when attached finder is loaded in page but is not currently visible
            return emptyResultList;
        }
        return resultList == null ? resultList = super.getResultList() : resultList;
    }

    @Override
    public Long getResultCount() {
        return resultCount == null ? resultCount = super.getResultCount() : resultCount;
    }

    public String reset(ActionEvent e) {
        freightCriteria.setActiveFor(e.getComponent().getId());

        objectContainer.add(Freight.class);
        return "reset";
    }

    public void initCriteriaFromTT(ActionEvent actionEvent) {
        reset(actionEvent);

        TtGen ttGen = ((TtGenHome) Component.getInstance(TtGenHome.class, ScopeType.CONVERSATION, false))
                .getInstance();

        Freight freight = getFreight();
        freight.setScac(ttGen.getTtTrans().getShipLine());
        freight.setPod(ttGen.getTtTrans().getDischargePort());
        freight.setPol(ttGen.getTtTrans().getLoadPort());

        if (ttGen.getTtTrans().checkIfAllContainersHaveSameTypeAndSize()) {
            freight.setCargoType(ttGen.getTtTrans().getContainers().get(0).getCargoType());
            freight.setContainerSize(ttGen.getTtTrans().getContainers().get(0).getSize());
        }

    }

    public FreightCriteria getFreightCriteria() {
        return freightCriteria;
    }

    // used from FreightList page
    public String reset() {
        objectContainer.add(Freight.class);
        return "reset";
    }
}