1
2
3
4
5
6
7
8
9
10
11
12
13
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
37
38 public class CluContextImpl extends BasicContextImpl {
39
40
41
42 private CluService cluService;
43
44
45
46
47
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
56
57
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
66
67
68
69 public void setCluService(CluService cluService) {
70 this.cluService = cluService;
71 }
72
73
74
75
76
77
78
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
104
105
106
107
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
123
124
125
126
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
202
203
204
205
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 }