001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krms.framework.engine;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021
022import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
023import org.kuali.rice.krms.api.engine.TermResolver;
024
025/**
026 * An implementation of {@link Context}
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029public final class BasicContext implements Context {
030        
031        private final List<Agenda> agendas;
032        private final List<TermResolver<?>> termResolvers;
033
034    /**
035     * Create a BasicContext with the given parameters
036     * @param agendas List<{@link}Agenda}> to set the agendas to
037     * @param termResolvers List<{@link TermResolver}<?>> to set the termResolvers to
038     */
039        public BasicContext(List<Agenda> agendas, List<TermResolver<?>> termResolvers) {
040                this.agendas = agendas;
041                this.termResolvers = termResolvers;
042        }
043        
044        @Override
045        public void execute(ExecutionEnvironment environment) {
046                if (termResolvers != null) for (TermResolver<?> termResolver : termResolvers) {
047                        environment.addTermResolver(termResolver);
048                }
049                List<Agenda> matchingAgendas = findMatchingAgendas(environment);
050                for (Agenda matchingAgenda : matchingAgendas) {
051                        matchingAgenda.execute(environment);
052                }
053        }
054
055    /**
056     * Return {@link Agenda}s that appliesTo the given {@link ExecutionEnvironment}
057     * @param environment {@link ExecutionEnvironment} to apply to
058     * @return List<{@link Agenda}> that match
059     */
060        private List<Agenda> findMatchingAgendas(ExecutionEnvironment environment) {
061                List<Agenda> matchingAgendas = new ArrayList<Agenda>();
062                for (Agenda agenda : agendas) {
063                        if (agenda.appliesTo(environment)) {
064                                matchingAgendas.add(agenda);
065                        }
066                }
067                return matchingAgendas;
068        }
069
070    @Override
071        public List<TermResolver<?>> getTermResolvers() {
072                return Collections.unmodifiableList(termResolvers);
073        }
074
075}