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