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.r2.lum.statement.config.context;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.List   ;
21  import org.kuali.student.r1.lum.lrc.dto.ResultComponentTypeInfo;
22  
23  import org.kuali.student.r1.core.statement.dto.ReqComponentInfo;
24  import org.kuali.student.r2.lum.lrc.dto.ResultValuesGroupInfo;
25  import org.kuali.student.r1.lum.lrc.service.LrcService;
26  import org.kuali.student.r1.lum.statement.typekey.ReqComponentFieldTypes;
27  import org.kuali.student.r2.common.exceptions.OperationFailedException;
28  import org.kuali.student.r2.common.dto.ContextInfo;
29  
30  /**
31   * This class creates the template context for grade condition type.
32   */
33  public class LrcContextImpl extends BasicContextImpl {
34  	
35  	private LrcService lrcService;
36  	
37  	/** Total credits template token */ 
38  	public final static String GRADE_TOKEN = "grade";
39      public final static String GRADE_TYPE_TOKEN = "gradeType";	
40  
41  	public void setLrcService(LrcService lrcService) {
42  		this.lrcService = lrcService;
43  	}
44  
45  	private ResultValuesGroupInfo getResultComponentByResultComponentId(String resultComponentId, ContextInfo contextInfo) throws OperationFailedException {
46  		if (resultComponentId == null) {
47  			return null;
48  		}
49  		try {
50  
51  
52  			return  lrcService.getResultValuesGroup(resultComponentId, contextInfo);
53  		} catch (Exception e) {
54  			throw new OperationFailedException(e.getMessage(), e);
55  		}
56  	}
57  	
58  	private String getResultValue(ResultValuesGroupInfo resultComponent, String resultValue) throws OperationFailedException {
59  		if(resultComponent == null || resultValue == null) {
60  			return null;
61  		}
62  		for(String rv : resultComponent.getResultValueKeys()) {
63  			if(rv.equals(resultValue)) {
64  				return rv;
65  			}
66  		}
67  		throw new OperationFailedException("Result value not found: "+resultValue);
68  	}
69  	
70  	private ResultValuesGroupInfo getResultComponentByResultValueId(String resultValueId, ContextInfo contextInfo) throws OperationFailedException {
71  		if(resultValueId == null) {
72  			return null;
73  		}
74  		
75  		try {
76  
77  			List<ResultComponentTypeInfo> typeList = lrcService.getResultComponentTypes(contextInfo);
78  			for(ResultComponentTypeInfo type : typeList) {
79  				List<String> resultComponentIdList = lrcService.getResultValuesGroupIdsByType(type.getId(), contextInfo);
80  				for(String resultComponentId : resultComponentIdList) {
81  				    ResultValuesGroupInfo resultComponent = lrcService.getResultValuesGroup(resultComponentId, contextInfo);
82  					if(resultComponent.getResultValueKeys().contains(resultValueId)) {
83  						return resultComponent;
84  					}
85  				}
86  			}
87  		} catch (Exception e) {
88  			throw new OperationFailedException(e.getMessage(), e);
89  		}
90  		throw new OperationFailedException("Result value id not found: "+resultValueId);
91  	}
92  	
93      /**
94       * Creates the context map (template data) for the requirement component.
95       * 
96       *
97       * @param reqComponent Requirement component
98       * @throws OperationFailedException Creating context map fails
99       */
100     public Map<String, Object> createContextMap(ReqComponentInfo reqComponent, ContextInfo contextInfo) throws OperationFailedException {
101         Map<String, Object> contextMap = new HashMap<String, Object>();
102 
103 //        String gradeTypeId = getReqComponentFieldValue(reqComponent, ReqComponentFieldTypes.GRADE_TYPE_KEY.getId());
104 //        ResultComponentInfo gradeTypeResultComponent = getResultComponentByResultComponentId(gradeTypeId);
105 //        if(gradeTypeResultComponent != null) {
106 //	        contextMap.put(GRADE_TYPE_TOKEN, gradeTypeResultComponent);
107 //        }
108 //	
109 //        String gradeId = getReqComponentFieldValue(reqComponent, ReqComponentFieldTypes.GRADE_KEY.getId());
110 //        String grade = getResultValue(gradeTypeResultComponent, gradeId);
111 //        if(grade != null) {
112 //        	contextMap.put(GRADE_TOKEN, grade);
113 //        }
114 
115     	
116         ResultValuesGroupInfo gradeTypeResultComponent = null;
117         String gradeId = getReqComponentFieldValue(reqComponent, ReqComponentFieldTypes.GRADE_KEY.getId());
118         if (gradeId == null) {
119 			String gradeTypeId = getReqComponentFieldValue(reqComponent, ReqComponentFieldTypes.GRADE_TYPE_KEY.getId());
120 			gradeTypeResultComponent = getResultComponentByResultComponentId(gradeTypeId, contextInfo);
121 			if (gradeTypeResultComponent != null) {
122 				contextMap.put(GRADE_TYPE_TOKEN, gradeTypeResultComponent);
123 			}
124         } else {
125         	gradeTypeResultComponent = getResultComponentByResultValueId(gradeId, contextInfo);
126         }
127 		if (gradeTypeResultComponent != null) {
128 			contextMap.put(GRADE_TYPE_TOKEN, gradeTypeResultComponent);
129 		}
130 
131         String grade = getResultValue(gradeTypeResultComponent, gradeId);
132         if(grade != null) {
133         	contextMap.put(GRADE_TOKEN, grade);
134         }
135 
136         return contextMap;
137     }
138 }