ar.com.allium.rules.core.service.ExcecuteRuleService.java Source code

Java tutorial

Introduction

Here is the source code for ar.com.allium.rules.core.service.ExcecuteRuleService.java

Source

package ar.com.allium.rules.core.service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ar.com.allium.rules.core.annotation.ExecuteRule;
import ar.com.allium.rules.core.exception.AlliumRuleException;
import ar.com.allium.rules.core.model.RuleResponse;
import ar.com.allium.rules.core.model.RuleState;
import ar.com.allium.rules.core.model.AlliumRule;
import ar.com.allium.rules.core.model.AlliumRuleEvaluateParameters;

/**
 * Copyright 2014 Joel del Valle <joelmarcosdelvalle@gmail.com>
 * 
 * This file is part of allium-rules, project of allium-projects.
 * 
 * allium-rules is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * allium-rules is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * allium-rules. If not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 * @author joel.delvalle
 * 
 *         AlliumRules execution service manager
 * 
 */
@Component
@Aspect
public final class ExcecuteRuleService {

    private static Logger log = Logger.getLogger(ExcecuteRuleService.class);

    @Autowired
    private AlliumCoreRuleManager alliumCoreRuleManager;

    @Before(value = "( @within(ar.com.allium.rules.core.annotation.ExecuteRule) || "
            + "@annotation(ar.com.allium.rules.core.annotation.ExecuteRule) ) && @annotation(executeRule)")
    public void excecuteRule(JoinPoint joinPoint, ExecuteRule executeRule) throws AlliumRuleException {
        log.debug("start executeRule");

        if (executeRule.executeSetOfRulesFirst()) {
            log.debug("executing set of rules");

            List<RuleResponse> ruleResponseList = this.executeSetOfRules(joinPoint, executeRule);
            ruleResponseList.addAll(this.executeRules(joinPoint, executeRule));

            if (this.haveRuleError(ruleResponseList)) {
                throw new AlliumRuleException("error to process rules", ruleResponseList);
            }

        } else {
            log.debug("executing rules");

            List<RuleResponse> ruleResponseList = this.executeRules(joinPoint, executeRule);
            ruleResponseList.addAll(this.executeSetOfRules(joinPoint, executeRule));

            if (this.haveRuleError(ruleResponseList)) {
                throw new AlliumRuleException("error to process rules", ruleResponseList);
            }

        }

    }

    private boolean haveRuleError(List<RuleResponse> ruleResponseList) {
        for (RuleResponse ruleResponse : ruleResponseList) {
            if (RuleState.RULE_ERROR.equals(ruleResponse.getRuleState())) {
                return true;
            }
        }
        return false;
    }

    private List<RuleResponse> executeSetOfRules(JoinPoint joinPoint, ExecuteRule excecuteRule) {
        String[] ruleSetList = excecuteRule.ruleSetName();

        List<RuleResponse> ruleResponseList = new ArrayList<RuleResponse>();

        if (ruleSetList != null && ruleSetList.length > 0) {

            for (String ruleSet : ruleSetList) {
                log.debug("excecute rule set: " + ruleSet);

                List<AlliumRule> rulesToExecuteList = this.getAlliumCoreRuleManager().getRuleList(ruleSet);

                if (rulesToExecuteList != null && rulesToExecuteList.size() > 0) {

                    for (AlliumRule alliumRule : rulesToExecuteList) {

                        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
                        Method method = signature.getMethod();

                        log.debug("execute rule: " + alliumRule.getRuleName() + " from rule set: " + ruleSet);
                        RuleResponse result = alliumRule
                                .execute(new AlliumRuleEvaluateParameters(method, joinPoint.getArgs()));

                        log.debug("execute rule: " + alliumRule.getRuleName() + " from rule set: " + ruleSet
                                + " -- result: " + result.getRuleState());
                        if (RuleState.RULE_ERROR.equals(result.getRuleState())) {
                            ruleResponseList.add(result);

                            if (excecuteRule.onErrorStopProcess()) {
                                throw new AlliumRuleException("error to process set allium rule: " + ruleSet,
                                        ruleResponseList);
                            }

                        }

                    }

                }

            }

        }

        return ruleResponseList;
    }

    private List<RuleResponse> executeRules(JoinPoint joinPoint, ExecuteRule excecuteRule) {

        List<RuleResponse> ruleResponseList = new ArrayList<RuleResponse>();

        Class<? extends AlliumRule>[] ruleList = excecuteRule.ruleClass();
        for (Class<? extends AlliumRule> clazz : ruleList) {
            AlliumRule alliumRule = this.getAlliumCoreRuleManager().getRule(clazz);

            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();

            log.debug("execute rule: " + alliumRule.getRuleName());
            RuleResponse result = alliumRule.execute(new AlliumRuleEvaluateParameters(method, joinPoint.getArgs()));

            log.debug("execute rule: " + alliumRule.getRuleName() + " -- result: " + result.getRuleState());
            if (RuleState.RULE_ERROR.equals(result.getRuleState())) {
                ruleResponseList.add(result);

                if (excecuteRule.onErrorStopProcess()) {
                    throw new AlliumRuleException(
                            "error to process allium rule: " + alliumRule.getClass().getSimpleName(),
                            ruleResponseList);
                }

            }
        }

        return ruleResponseList;
    }

    private AlliumCoreRuleManager getAlliumCoreRuleManager() {
        return alliumCoreRuleManager;
    }
}