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.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.kuali.student.r2.lum.statement.config.context.util.NLCluSet;
23  import org.kuali.student.r1.core.statement.dto.ReqComponentInfo;
24  import org.kuali.student.r1.lum.statement.typekey.ReqComponentFieldTypes;
25  import org.kuali.student.r2.common.dto.ContextInfo;
26  import org.kuali.student.r2.common.exceptions.OperationFailedException;
27  import org.kuali.student.r2.core.versionmanagement.dto.VersionDisplayInfo;
28  import org.kuali.student.r2.lum.clu.dto.CluInfo;
29  import org.kuali.student.r2.lum.clu.dto.CluSetInfo;
30  import org.kuali.student.r2.lum.clu.dto.CluSetTreeViewInfo;
31  import org.kuali.student.r2.lum.clu.service.CluService;
32  import org.kuali.student.r2.lum.util.constants.CluServiceConstants;
33  
34  
35  /**
36   * This class creates the template context for course list types.
37   */
38  public class CluContextImpl extends BasicContextImpl {
39      /**
40       * Learning unit service.
41       */
42  	private CluService cluService;
43  
44  	/**
45  	 * <code>clu</code> token (key) references a Clu object used in templates.
46  	 * e.g. 'Student must have completed all of 
47  	 * $clu.getOfficialIdentifier().getShortName()'
48  	 */
49  	public final static String CLU_TOKEN = "clu";
50  	public final static String COURSE_CLU_TOKEN = "courseClu";
51  	public final static String PROGRAM_CLU_TOKEN = "programClu";
52  	public final static String TEST_CLU_TOKEN = "testClu";
53  
54  	/**
55  	 * <code>cluSet</code> token (key) references a Clu set object
56  	 * used in templates.
57  	 * e.g. 'Student must have completed all of $cluSet.getCluSetAsCode()'
58  	 */
59  	public final static String CLU_SET_TOKEN = "cluSet";
60  	public final static String COURSE_CLU_SET_TOKEN = "courseCluSet";
61  	public final static String PROGRAM_CLU_SET_TOKEN = "programCluSet";
62  	public final static String TEST_CLU_SET_TOKEN = "testCluSet";
63  
64  	/**
65  	 * Sets the LU service.
66  	 *
67  	 * @param cluService CLU service
68  	 */
69      public void setCluService(CluService cluService) {
70  		this.cluService = cluService;
71  	}
72  
73  	/**
74       * Gets a CLU.
75       *
76       * @param cluId CLU id
77       * @return CLU
78       * @throws OperationFailedException If retrieving CLU fails
79       */
80      public CluInfo getCluInfo(String cluId, ContextInfo contextInfo) throws OperationFailedException {
81  		if (cluId == null) {
82  			return null;
83  		}
84  		try {
85  			VersionDisplayInfo versionInfo = cluService.getCurrentVersion(CluServiceConstants.CLU_NAMESPACE_URI, cluId, null);
86  			CluInfo clu = this.cluService.getClu(versionInfo.getId(), contextInfo);
87  			return clu;
88  		} catch(Exception e) {
89  			throw new OperationFailedException(e.getMessage(), e);
90  		}
91      }
92  
93      private CluInfo getClu(ReqComponentInfo reqComponent, String key, ContextInfo contextInfo) throws OperationFailedException {
94          Map<String, String> map = getReqComponentFieldMap(reqComponent);
95          if(map.containsKey(key)) {
96  	    	String cluId = map.get(key);
97  	    	return getCluInfo(cluId, contextInfo);
98          }
99          return null;
100     }
101     
102     /**
103      * Gets a CLU set.
104      *
105      * @param cluSetId CLU set id
106      * @return CLU set
107      * @throws OperationFailedException If retrieving CLU set fails
108      */
109     public CluSetInfo getCluSetInfo(String cluSetId, ContextInfo contextInfo) throws OperationFailedException {
110 		if (cluSetId == null) {
111 			return null;
112 		}
113 		try {
114 	    	CluSetInfo cluSet = this.cluService.getCluSet(cluSetId, contextInfo);
115 	    	return cluSet;
116 		} catch(Exception e) {
117 			throw new OperationFailedException(e.getMessage(), e);
118 		}
119     }
120 
121     /**
122      * Gets the CLU set.
123      *
124      * @param cluSetId CLU set id
125      * @return CLU set
126      * @throws OperationFailedException If building a custom CLU set fails
127      */
128     public NLCluSet getCluSet(String cluSetId, ContextInfo contextInfo) throws OperationFailedException {
129 		if (cluSetId == null) {
130 			return null;
131 		}
132     	CluSetInfo cluSet = getCluSetInfo(cluSetId, contextInfo);
133 		try {
134 	    	List<CluInfo> list = new ArrayList<CluInfo>();
135 	    	CluSetTreeViewInfo tree = cluService.getCluSetTreeView(cluSetId, contextInfo);
136 	    	findClusInCluSet(tree, list);
137 	    	return new NLCluSet(cluSet.getId(), list, cluSet);
138 		} catch(Exception e) {
139 			throw new OperationFailedException(e.getMessage(), e);
140 		}
141     }
142 
143     private static void findClusInCluSet(CluSetTreeViewInfo tree, List<CluInfo> cluList) {
144     	if (tree.getCluSets() != null) {
145 			for (CluSetTreeViewInfo cluSet : tree.getCluSets()) {
146 				findClusInCluSet(cluSet, cluList);
147 			}
148 		} else {
149 			for (CluInfo clu : tree.getClus()) {
150 				if (!containsClu(cluList, clu)) {
151 					cluList.add(clu);
152 				}
153 			}
154 		}
155 	}
156 
157 	private static boolean containsClu(List<CluInfo> cluList, CluInfo clu) {
158 		for (CluInfo clu2 : cluList) {
159 			if (clu2.getId().equals(clu.getId())) {
160 				return true;
161 			}
162 		}
163 		return false;
164 	}
165 
166 	/**
167      * Gets a new CLU set from comma separated list of CLU ids.
168      *
169      * @param cluIds Comma separated list of CLU ids
170      * @return A new CLU set
171      * @throws OperationFailedException If building a custom CLU set fails
172      */
173     /*public NLCluSet getClusAsCluSet(String cluIds) throws OperationFailedException {
174     	String[] cluIdArray = cluIds.split("\\s*,\\s*");
175     	List<CluInfo> list = new ArrayList<CluInfo>();
176     	for(String cluId : cluIdArray) {
177     		CluInfo clu = getCluInfo(cluId);
178     		list.add(clu);
179     	}
180     	return new NLCluSet(null, list);
181     }*/
182 
183     /**
184      * Gets a custom CLU set from a requirement component.
185      *
186      * @param reqComponent Requirement component
187      * @return custom CLU set
188      * @throws OperationFailedException If building a custom CLU set fails
189      */
190     public NLCluSet getCluSet(ReqComponentInfo reqComponent, String key, ContextInfo contextInfo) throws OperationFailedException {
191         Map<String, String> map = getReqComponentFieldMap(reqComponent);
192     	NLCluSet cluSet = null;
193     	if(map.containsKey(key)) {
194         	String cluSetId = map.get(key);
195             cluSet = getCluSet(cluSetId, contextInfo);
196         }
197     	return cluSet;
198     }
199 
200     /**
201      * Creates the context map (template data) for the requirement component.
202      * 
203      *
204      * @param reqComponent Requirement component
205      * @throws OperationFailedException Creating context map fails
206      */
207     public Map<String, Object> createContextMap(ReqComponentInfo reqComponent, ContextInfo contextInfo) throws OperationFailedException {
208         Map<String, Object> contextMap = super.createContextMap(reqComponent, contextInfo);
209 
210         CluInfo clu = getClu(reqComponent, ReqComponentFieldTypes.CLU_KEY.getId(), contextInfo);
211         if(clu != null) {
212 	        contextMap.put(CLU_TOKEN, clu);
213         }
214         CluInfo courseClu = getClu(reqComponent, ReqComponentFieldTypes.COURSE_CLU_KEY.getId(), contextInfo);
215         if(courseClu != null) {
216 	        contextMap.put(COURSE_CLU_TOKEN, courseClu);
217         }
218         CluInfo programClu = getClu(reqComponent, ReqComponentFieldTypes.PROGRAM_CLU_KEY.getId(), contextInfo);
219         if(programClu != null) {
220 	        contextMap.put(PROGRAM_CLU_TOKEN, programClu);
221         }
222         CluInfo testClu = getClu(reqComponent, ReqComponentFieldTypes.TEST_CLU_KEY.getId(), contextInfo);
223         if(testClu != null) {
224 	        contextMap.put(TEST_CLU_TOKEN, testClu);
225         }
226 
227         NLCluSet cluSet = getCluSet(reqComponent, ReqComponentFieldTypes.CLUSET_KEY.getId(), contextInfo);
228         if(cluSet != null) {
229         	contextMap.put(CLU_SET_TOKEN, cluSet);
230         }
231         NLCluSet courseCluSet = getCluSet(reqComponent, ReqComponentFieldTypes.COURSE_CLUSET_KEY.getId(), contextInfo);
232         if(courseCluSet != null) {
233         	contextMap.put(COURSE_CLU_SET_TOKEN, courseCluSet);
234         }
235         NLCluSet programCluSet = getCluSet(reqComponent, ReqComponentFieldTypes.PROGRAM_CLUSET_KEY.getId(), contextInfo);
236         if(programCluSet != null) {
237         	contextMap.put(PROGRAM_CLU_SET_TOKEN, programCluSet);
238         }
239         NLCluSet testCluSet = getCluSet(reqComponent, ReqComponentFieldTypes.TEST_CLUSET_KEY.getId(), contextInfo);
240         if(testCluSet != null) {
241         	contextMap.put(TEST_CLU_SET_TOKEN, testCluSet);
242         }
243 
244         return contextMap;
245     }
246 }