View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.enrollment.class2.courseoffering.krms.naturallanguage.context;
17  
18  import org.kuali.rice.core.api.exception.RiceIllegalStateException;
19  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20  import org.kuali.student.r2.common.dto.ContextInfo;
21  import org.kuali.student.r2.common.util.ContextUtils;
22  import org.kuali.student.r2.core.constants.PopulationServiceConstants;
23  import org.kuali.student.r2.core.krms.naturallanguage.TermParameterTypes;
24  import org.kuali.student.r2.core.population.dto.PopulationInfo;
25  import org.kuali.student.r2.core.population.service.PopulationService;
26  
27  import javax.xml.namespace.QName;
28  import java.util.Map;
29  
30  
31  /**
32   * This class creates the template context for Class Standing.
33   */
34  public class PopulationContextImpl extends BasicContextImpl {
35  
36      private PopulationService populationService;
37  
38  	public final static String POPULATION_TOKEN = "population";
39  
40  
41      public void setPopulationService(PopulationService populationService) {
42          this.populationService = populationService;
43      }
44      private PopulationService getPopulationService() {
45          if(populationService == null) {
46              populationService = (PopulationService) GlobalResourceLoader.getService(new QName(PopulationServiceConstants.NAMESPACE, "PopulationService"));
47          }
48          return this.populationService;
49      }
50  	private PopulationInfo getClassStanding(String populationId, ContextInfo context)  {
51  		if (populationId == null) {
52  			return null;
53  		}
54  		try {
55  
56  			return  this.getPopulationService().getPopulation(populationId, context);
57  		} catch (Exception e) {
58                      throw new RiceIllegalStateException (e);
59  		}
60  	}
61      /**
62       * Creates the context map (template data) for the requirement component.
63       *
64       * @param parameters
65       * @throws org.kuali.student.r2.common.exceptions.OperationFailedException Creating context map fails
66       */
67      @Override
68      public Map<String, Object> createContextMap(Map<String, Object> parameters) {
69          ContextInfo contextInfo = ContextUtils.getContextInfo();
70          Map<String, Object> contextMap = super.createContextMap(parameters);
71  
72          String populationId = (String) parameters.get(TermParameterTypes.POPULATION_KEY.getId());
73  
74          if (populationId != null) {
75              PopulationInfo populationInfo = this.getClassStanding(populationId, contextInfo);
76              contextMap.put(POPULATION_TOKEN, populationInfo);
77          }
78  
79          return contextMap;
80      }
81  }