com.webbfontaine.valuewebb.model.util.TTUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.model.util.TTUtils.java

Source

package com.webbfontaine.valuewebb.model.util;

import com.webbfontaine.valuewebb.action.tt.TtGenHome;
import com.webbfontaine.valuewebb.authentication.UserProperties;
import com.webbfontaine.valuewebb.bpm.BpmUtil;
import com.webbfontaine.valuewebb.model.Pd;
import com.webbfontaine.valuewebb.model.TtDoc;
import com.webbfontaine.valuewebb.model.TtGen;
import com.webbfontaine.valuewebb.model.TtInv;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.CacheMode;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;
import org.jboss.seam.security.Identity;

import javax.faces.model.SelectItem;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.webbfontaine.valuewebb.model.constants.Constants.*;

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

@Name("ttUtils")
@Scope(ScopeType.APPLICATION)
@BypassInterceptors
public class TTUtils {

    private TtGen ttGen;

    public TTUtils() {
    }

    public TTUtils(TtGen ttGen) {
        this.ttGen = ttGen;
    }

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

    /**
     * @return null if entity not found, true if TT is frozen, false otherwize
     */
    public static Boolean isTTFreezed(long id) {
        Query query = Utils.getEntityManager().createQuery("SELECT frozen FROM TtGen tt WHERE tt.id = :id");
        query.setParameter("id", id);
        query.setHint("org.hibernate.cacheMode", CacheMode.IGNORE);
        return (Boolean) query.getSingleResult();
    }

    public static boolean canBeFreezed(TtGen ttGen) {
        String currentStatus = ttGen.getStatus();
        return Identity.instance().hasRole(OP_SUPERVISOR_BU) && !ttGen.getFrozen()
                && !TT_STORED.equals(currentStatus) && !GW_PASSED.equals(currentStatus)
                && !GW_LOCKED.equals(currentStatus) && !TT_REJECTED.equals(currentStatus)
                && !TT_SENT.equals(currentStatus);
    }

    public static String getTTStatus(Object id) {
        Query query = Utils.getEntityManager().createQuery("SELECT status FROM TtGen tt WHERE tt.id = :id");
        Utils.setDirectRead(query).setParameter("id", id);
        return (String) query.getSingleResult();
    }

    public void addPd() {
        Pd pd = new Pd();
        addPd(pd);
    }

    public void addPd(Pd pd) {
        pd.setTtGen(ttGen);
        TtInv firstTtInv = ttGen.getFirstTtInvs();
        pd.setTtInv(firstTtInv);

        firstTtInv.getPds().add(pd);
        pd.setItemNumber(firstTtInv.getPds().size());
    }

    public void addAttDoc() {
        TtDoc ttDoc = new TtDoc(ttGen);
        ttGen.getTtDocs().add(ttDoc);
    }

    public void addAttDoc(TtDoc ttDoc) {
        ttDoc.setTtGen(ttGen);
        ttGen.getTtDocs().add(ttDoc);
    }

    public boolean itemEditable(Pd pd) {
        if (pd == null) {
            return false;
        }
        TtGenHome ttGenHome = TtGenHome.getComponentInstance(false);
        String ttStatus = pd.getTtGen().getStatus();
        BpmUtil bpmUtil = new BpmUtil();

        return ttGenHome.isReadOnlyMode() && TT_ACCEPTED.equals(ttStatus) && bpmUtil.loadTask(pd, false)
                && bpmUtil.canEditDocument(pd)
                && !bpmUtil.filterAvailablePdOperations(pd, PD_REVERT_TO_TRANSPOSED_BY_TT).isEmpty()
                && isHsCodeMatches(pd.getHsCodeD());
    }

    public boolean isHsCodeMatches(String hsCode) {
        boolean isMatch = false;
        UserProperties userProperties = UserProperties.getInstance();
        String hsCodeRange = userProperties.getHSRestriction();

        if (!StringUtils.isEmpty(hsCodeRange)) {
            int characterIndex = hsCodeRange.indexOf("..");
            if (hsCode != null && characterIndex != -1) {
                int subHsCode = Integer.parseInt(hsCode.substring(0, 2));
                isMatch = subHsCode >= Integer
                        .parseInt(hsCodeRange.substring(hsCodeRange.indexOf('[') + 1, hsCodeRange.indexOf('.')))
                        && subHsCode <= Integer.parseInt(
                                hsCodeRange.substring(hsCodeRange.indexOf('.') + 2, hsCodeRange.indexOf(']')));
            }
        }

        return isMatch;
    }

    public static List<SelectItem> operationsForLocale() {
        String language = Utils.getLanguage();
        if (!ttOperationsForLocale.containsKey(language)) {
            List<SelectItem> ttOperations = new ArrayList<>();
            ttOperations.add(new SelectItem(STORE, Utils.translate(STORE)));
            ttOperations.add(new SelectItem(SAVE, Utils.translate(SAVE)));
            ttOperations.add(new SelectItem(PEM, Utils.translate(PEM)));
            ttOperations.add(new SelectItem(AFD, Utils.translate(AFD)));
            ttOperations.add(new SelectItem(REJECT, Utils.translate(REJECT)));
            ttOperations.add(new SelectItem(ACCEPT, Utils.translate(ACCEPT)));
            ttOperations.add(new SelectItem(QUERY, Utils.translate(QUERY)));
            ttOperations.add(new SelectItem(CONTINUE, Utils.translate(CONTINUE)));
            ttOperations.add(new SelectItem(GENERATE_FCVR, Utils.translate(GENERATE_FCVR)));
            ttOperations.add(new SelectItem(GENERATE_DRAFT_FCVR, Utils.translate(GENERATE_DRAFT_FCVR)));
            ttOperations.add(new SelectItem(COMPLETE_VC, Utils.translate(COMPLETE_VC)));
            ttOperations.add(new SelectItem(COMPLETE_TRANSACTION, Utils.translate(COMPLETE_TRANSACTION)));
            ttOperations.add(new SelectItem(SEND_FCVR, Utils.translate(SEND_FCVR)));
            ttOperations.add(new SelectItem(GW_LOCK, Utils.translate(GW_LOCK)));
            ttOperations.add(new SelectItem(GW_PASS, Utils.translate(GW_PASS)));
            ttOperationsForLocale.put(language, ttOperations);
        }
        return ttOperationsForLocale.get(language);
    }

    public static boolean canEditStored(TtGen ttGen) {

        if (!"Stored".equals(ttGen.getStatus())) {
            return false;
        }

        Identity identity = Identity.instance();

        // created by GUCE:
        if (identity.hasRole("VC_Approve") && "Submitted".equals(ttGen.getGuceStatus())) {
            return true;
        }

        // created non by GUCE:
        if (ttGen.getGuceStatus() == null && (identity.hasRole("Agent") || identity.hasRole("VC_Data_Entry"))) {
            return true;
        }

        return false;
    }

    private static Map<String, List<SelectItem>> ttOperationsForLocale = new HashMap<>();
}