View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.framework.engine;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
23  import org.kuali.rice.krms.api.engine.TermResolver;
24  
25  /**
26   * An implementation of {@link Context}
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   */
29  public final class BasicContext implements Context {
30  	
31  	private final List<Agenda> agendas;
32  	private final List<TermResolver<?>> termResolvers;
33  
34      /**
35       * Create a BasicContext with the given parameters
36       * @param agendas List<{@link}Agenda}> to set the agendas to
37       * @param termResolvers List<{@link TermResolver}<?>> to set the termResolvers to
38       */
39  	public BasicContext(List<Agenda> agendas, List<TermResolver<?>> termResolvers) {
40  		this.agendas = agendas;
41  		this.termResolvers = termResolvers;
42  	}
43  	
44  	@Override
45  	public void execute(ExecutionEnvironment environment) {
46  		if (termResolvers != null) for (TermResolver<?> termResolver : termResolvers) {
47  			environment.addTermResolver(termResolver);
48  		}
49  		List<Agenda> matchingAgendas = findMatchingAgendas(environment);
50  		for (Agenda matchingAgenda : matchingAgendas) {
51  			matchingAgenda.execute(environment);
52  		}
53  	}
54  
55      /**
56       * Return {@link Agenda}s that appliesTo the given {@link ExecutionEnvironment}
57       * @param environment {@link ExecutionEnvironment} to apply to
58       * @return List<{@link Agenda}> that match
59       */
60  	private List<Agenda> findMatchingAgendas(ExecutionEnvironment environment) {
61  		List<Agenda> matchingAgendas = new ArrayList<Agenda>();
62  		for (Agenda agenda : agendas) {
63  			if (agenda.appliesTo(environment)) {
64  				matchingAgendas.add(agenda);
65  			}
66  		}
67  		return matchingAgendas;
68  	}
69  
70      @Override
71  	public List<TermResolver<?>> getTermResolvers() {
72  		return Collections.unmodifiableList(termResolvers);
73  	}
74  
75  }