com.webbfontaine.valuewebb.irms.impl.risk.repository.RiskRule.java Source code

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.irms.impl.risk.repository.RiskRule.java

Source

/*
 * Copyrights 2002-2013 Webb Fontaine
 * Developer: Sargis Harutyunyan
 * Date: 12 avr. 2013
 * This software is the proprietary information of Webb Fontaine.
 * Its use is subject to License terms.
 */
package com.webbfontaine.valuewebb.irms.impl.risk.repository;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.webbfontaine.twm.urmcore.utils.Preconditions;
import com.webbfontaine.valuewebb.irms.core.AbstractIrmsRule;
import com.webbfontaine.valuewebb.irms.impl.bean.TTSourceBean;
import com.webbfontaine.valuewebb.irms.impl.function.TtRuleFunctions;
import com.webbfontaine.valuewebb.irms.impl.risk.data.RiskColor;
import com.webbfontaine.valuewebb.irms.impl.risk.data.RiskResult;
import groovy.lang.Closure;
import org.apache.commons.lang3.Validate;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.webbfontaine.valuewebb.irms.impl.risk.repository.RiskAction.*;

public abstract class RiskRule extends AbstractIrmsRule<TTSourceBean, List<RiskResult>> {

    private TtRuleFunctions ttRuleFunctions;

    private final List<RiskResult> riskResult$$ = Lists.newArrayListWithExpectedSize(32);

    @Override
    public List<RiskResult> applyRule() {
        Validate.notNull(getSource());
        Validate.notNull(getParameters());

        execute();

        return Collections.unmodifiableList(riskResult$$);
    }

    protected void applyRisk(Map<String, Object> parameters, Closure<Object> callable) {
        Preconditions.checkNotNull(parameters);
        Preconditions.checkNotNull(callable);

        RiskAction riskAction = new RiskAction(this);
        riskAction.applyRisk(parameters, getRuleContext(), callable);
    }

    protected void applyRisk(RiskColor riskColor, Number percent, Closure<Object> callable) {
        applyRisk(riskColor, percent, 0, callable);
    }

    protected void applyRisk(RiskColor riskColor, Number percent, Integer item, Closure<Object> callable) {
        Preconditions.checkNotNull(riskColor);
        Preconditions.checkNotNull(percent);
        Preconditions.checkNotNull(item);

        ImmutableMap<String, Object> parameters = ImmutableMap.<String, Object>of(COLOR_PARAM_NAME, riskColor,
                PERCENT_PARAM_NAME, percent, ITEM_PARAM_NAME, item);

        applyRisk(parameters, callable);
    }

    void setRiskResult(RiskResult riskResult) {
        riskResult$$.add(riskResult);
    }

    public boolean isDocumentAttached(String docType) {
        return ttRuleFunctions.isDocumentAttached(getSource(), docType);
    }

    public String getOperation() {
        return ttRuleFunctions.getOperation(getRuleContext());
    }

    public void setTtRuleFunctions(TtRuleFunctions ttRuleFunctions) {
        this.ttRuleFunctions = ttRuleFunctions;
    }

    protected abstract void execute();
}