com.webbfontaine.valuewebb.search.PortFinder.java Source code

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.search.PortFinder.java

Source

package com.webbfontaine.valuewebb.search;

import com.webbfontaine.valuewebb.action.tt.TtGenHome;
import com.webbfontaine.valuewebb.model.constants.Constants;
import com.webbfontaine.valuewebb.model.util.Utils;
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 javax.persistence.EntityManager;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Copyrights 2002-2010 Webb Fontaine
 * This software is the proprietary information of Webb Fontaine.
 * Its use is subject to License terms.
 * User: nigiyan
 * Date: Oct 28, 2010
 */

@Name("portFinder")
//todo: can be improved later: rename to RimmFinder. pass entity name...
public class PortFinder implements Serializable, Constants {

    private static final long serialVersionUID = 1L;

    @In
    EntityManager entityManager;

    @In(create = true)
    RimmCriteria rimmCriteria;

    List resultList;
    static List emptyResultList = new ArrayList();

    public String select(String str) {
        String sql = "select rimmPort from RimmPort rimmPort where "
                + "lower(rimmPort.cod) like lower(concat(:codParam, '%')) and ";
        if (FR.equals(Utils.getLanguage())) {
            sql += "lower(rimmPort.dscFr) like lower(concat(:dscParam, '%')) ";
        } else {
            sql += "lower(rimmPort.dsc) like lower(concat(:dscParam, '%')) ";
        }
        if (str.equals("")) {
            sql = sql.concat("order by rimmPort.cod, rimmPort.dsc");
        } else {
            sql = sql.concat(
                    "and substr(lower(rimmPort.cod), 0, 2) = lower(:str) order by rimmPort.cod, rimmPort.dsc");
        }

        Query query = entityManager.createQuery(sql).setParameter("codParam", rimmCriteria.getCod())
                .setParameter("dscParam", rimmCriteria.getDsc());
        if (!str.equals("")) {
            query.setParameter("str", str);
        }
        resultList = Utils.setDirectRead(query).setMaxResults(Constants.MAX_RECORDS_IN_ATTACHED_FINDER)
                .getResultList();
        return null;
    }

    public List getResultList() {

        if (StringUtils.isEmpty(rimmCriteria.getActiveFor())) {
            return emptyResultList;
        }

        if (resultList != null) {
            return resultList;
        }

        String ctySupp = "";
        TtGenHome ttGenHome;
        if ((ttGenHome = (TtGenHome) Component.getInstance(TtGenHome.class, ScopeType.CONVERSATION,
                false)) != null) {
            String placeOfReceipt = ttGenHome.getInstance().getTtTrans().getPlaceOfReceipt();
            if (((rimmCriteria.getActiveFor().equals("findLoadPort") && StringUtils.isEmpty(placeOfReceipt))
                    || rimmCriteria.getActiveFor().equals("findPlaceOfReceipt"))) {
                ctySupp = ttGenHome.getInstance().getCtySupp();
            }
        }
        ctySupp = ctySupp == null ? "" : ctySupp;
        select(ctySupp);

        return resultList;
    }

    public void setResultList(List resultList) {
    }

    public RimmCriteria getRimmCriteria() {
        return rimmCriteria;
    }

    public void setRimmCriteria(RimmCriteria rimmCriteria) {
    }
}