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

Java tutorial

Introduction

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

Source

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

import com.webbfontaine.valuewebb.action.tt.dataimporter.TTDataImporter;
import com.webbfontaine.valuewebb.model.TtGen;
import com.webbfontaine.valuewebb.model.constants.Messages;
import com.webbfontaine.valuewebb.model.util.Utils;
import com.webbfontaine.valuewebb.utils.DateUtils;
import com.webbfontaine.valuewebb.validation.ErrorHandling;
import com.webbfontaine.valuewebb.validation.FacesMessageText;
import org.apache.commons.lang3.StringUtils;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyrights 2002-2014 Webb Fontaine
 * This software is the proprietary information of Webb Fontaine.
 * Its use is subject to License terms.
 * Developer: nigiyan
 * Date: 28/04/2014
 */

/**
 * Validates idf date, importer tin against idf number. Service data is loaded if wasn't done before manually.
 */
public class TTServiceDataValidator {

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

    private TtGen ttGen;

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

    public void validate() {
        String currentKey = ttGen.getIdfNum();

        if (StringUtils.isEmpty(currentKey) || ErrorHandling.getInstance().hasScopedErrors("idfNum")) {
            return;
        }

        LOGGER.trace("Started for {0}", currentKey);

        TtGen serviceTt = null;

        try {
            serviceTt = new TTDataImporter().resolveServiceAndLoad(ttGen);
        } catch (Exception e) {
            LOGGER.warn("Unable to validate DAI/FDI number, warning message will be shown");
            addError(Utils.translate("IDF Num.") + ": " + Utils.translate(Messages.IMPOSSIBLE_TO_PROCESS));
        }

        if (serviceTt == null) {
            ErrorHandling.addFacesMessageError("idfNum", Utils.translate(Messages.IDF_NUMBER_INEXISTENT));
        } else {
            String serviceTIN = StringUtils.trimToEmpty(serviceTt.getImpTin());
            String currentTIN = StringUtils.trimToEmpty(ttGen.getImpTin());

            if (!currentTIN.equals(serviceTIN)) {
                ErrorHandling.addFacesMessageError("impTin",
                        Utils.translate(Messages.ALLOWED_VALUE) + ": " + serviceTIN);
            }

            String serviceDate = serviceTt.getIdfDate() == null ? ""
                    : DateUtils.dateToString(serviceTt.getIdfDate(), "dd/MM/yyyy");
            String currentDate = ttGen.getIdfDate() == null ? ""
                    : DateUtils.dateToString(ttGen.getIdfDate(), "dd/MM/yyyy");

            if (!serviceDate.equals(currentDate)) {
                ErrorHandling.addFacesMessageError("idfDate",
                        Utils.translate(Messages.ALLOWED_VALUE) + ": " + serviceDate);
            }

            LOGGER.debug("TT key: {0}, date: {1}, TIN: {2}. Service data key: {3}, date: {4}, TIN: {5}", currentKey,
                    currentDate, currentTIN, serviceTt.getIdfNum(), serviceDate, serviceTIN);
        }
    }

    private static void addError(String msg) {
        ErrorHandling errorHandling = ErrorHandling.getInstance();
        List<FacesMessageText> msgs = new ArrayList<>();
        msgs.add(new FacesMessageText(null, msg, false));
        errorHandling.addErrorWithLinks("idfNum", msgs, errorHandling.getErrorList(), false, false);
    }
}