1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.lum.lu.ui.tools.server.gwt; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.kuali.student.common.assembly.data.AssemblyException; |
22 | |
import org.kuali.student.common.assembly.data.Data; |
23 | |
import org.kuali.student.common.exceptions.DataValidationErrorException; |
24 | |
import org.kuali.student.common.search.dto.SearchRequest; |
25 | |
import org.kuali.student.common.search.dto.SearchResult; |
26 | |
import org.kuali.student.common.search.dto.SearchResultCell; |
27 | |
import org.kuali.student.common.search.dto.SearchResultRow; |
28 | |
import org.kuali.student.common.ui.client.service.DataSaveResult; |
29 | |
import org.kuali.student.common.ui.client.service.exceptions.OperationFailedException; |
30 | |
import org.kuali.student.common.ui.server.gwt.DataGwtServlet; |
31 | |
import org.kuali.student.common.versionmanagement.dto.VersionDisplayInfo; |
32 | |
import org.kuali.student.lum.common.client.widgets.CluInformation; |
33 | |
import org.kuali.student.lum.common.client.widgets.CluSetInformation; |
34 | |
import org.kuali.student.lum.common.client.widgets.CluSetManagementRpcService; |
35 | |
import org.kuali.student.lum.lrc.dto.ResultComponentInfo; |
36 | |
import org.kuali.student.lum.lrc.service.LrcService; |
37 | |
import org.kuali.student.lum.lu.dto.*; |
38 | |
import org.kuali.student.lum.lu.service.LuService; |
39 | |
import org.kuali.student.lum.lu.service.LuServiceConstants; |
40 | |
|
41 | |
import org.apache.log4j.Logger; |
42 | |
|
43 | |
import edu.emory.mathcs.backport.java.util.Collections; |
44 | |
|
45 | 0 | public class CluSetManagementRpcGwtServlet extends DataGwtServlet implements |
46 | |
CluSetManagementRpcService { |
47 | |
|
48 | |
private static final long serialVersionUID = 1L; |
49 | 0 | final static Logger LOG = Logger.getLogger(CluSetManagementRpcGwtServlet.class); |
50 | |
private LuService luService; |
51 | |
private LrcService lrcService; |
52 | |
|
53 | |
public LuService getLuService() { |
54 | 0 | return luService; |
55 | |
} |
56 | |
|
57 | |
public void setLuService(LuService luService) { |
58 | 0 | this.luService = luService; |
59 | 0 | } |
60 | |
|
61 | |
public LrcService getLrcService() { |
62 | 0 | return lrcService; |
63 | |
} |
64 | |
|
65 | |
public void setLrcService(LrcService lrcService) { |
66 | 0 | this.lrcService = lrcService; |
67 | 0 | } |
68 | |
|
69 | |
@Override |
70 | |
public Data getData(String id) throws OperationFailedException { |
71 | |
try{ |
72 | 0 | return getDataService().getData(id); |
73 | 0 | } catch (Exception e) { |
74 | 0 | LOG.error("Could not get Data ", e); |
75 | 0 | throw new OperationFailedException("Failed to get data"); |
76 | |
} |
77 | |
} |
78 | |
|
79 | |
@Override |
80 | |
public DataSaveResult saveData(Data data) throws OperationFailedException { |
81 | |
try{ |
82 | 0 | return getDataService().saveData(data); |
83 | 0 | } catch (Exception e) { |
84 | 0 | LOG.error("Could not save data ", e); |
85 | 0 | throw new OperationFailedException("Failed to save data"); |
86 | |
} |
87 | |
} |
88 | |
|
89 | |
private CluSetInfo getCluSetInfo(String cluSetId) throws OperationFailedException { |
90 | 0 | List<String> cluIds = null; |
91 | 0 | CluSetInfo cluSetInfo = null; |
92 | |
try { |
93 | |
|
94 | |
|
95 | |
|
96 | 0 | cluSetInfo = luService.getCluSetInfo(cluSetId); |
97 | 0 | cluSetInfo.setCluIds(null); |
98 | 0 | cluIds = luService.getCluIdsFromCluSet(cluSetId); |
99 | 0 | cluSetInfo.setCluIds(cluIds); |
100 | 0 | upWrap(cluSetInfo); |
101 | 0 | } catch (Exception e) { |
102 | 0 | throw new OperationFailedException("Failed to retrieve cluset info for " + cluSetId, e); |
103 | 0 | } |
104 | 0 | return cluSetInfo; |
105 | |
} |
106 | |
|
107 | |
private List<CluSetInfo> getCluSetInfos(List<String> cluSetIds) throws OperationFailedException { |
108 | 0 | List<CluSetInfo> clusetInfos = null; |
109 | 0 | if (cluSetIds != null) { |
110 | 0 | for (String cluSetId : cluSetIds) { |
111 | 0 | clusetInfos = (clusetInfos == null)? new ArrayList<CluSetInfo>() : clusetInfos; |
112 | 0 | clusetInfos.add(getCluSetInfo(cluSetId)); |
113 | |
} |
114 | |
} |
115 | 0 | return clusetInfos; |
116 | |
} |
117 | |
|
118 | |
private void upWrap(CluSetInfo cluSetInfo) throws AssemblyException { |
119 | 0 | List<String> cluSetIds = (cluSetInfo == null)? null : cluSetInfo.getCluSetIds(); |
120 | 0 | List<String> unWrappedCluSetIds = null; |
121 | 0 | List<CluSetInfo> wrappedCluSets = null; |
122 | 0 | List<CluSetInfo> subCluSets = null; |
123 | |
|
124 | |
try { |
125 | 0 | if (cluSetIds != null && !cluSetIds.isEmpty()) { |
126 | 0 | subCluSets = luService.getCluSetInfoByIdList(cluSetIds); |
127 | |
} |
128 | 0 | } catch (Exception e) { |
129 | 0 | LOG.error(e.getMessage(), e); |
130 | 0 | throw new AssemblyException("Failed to retrieve the sub clusets of cluset " + |
131 | |
cluSetInfo.getId()); |
132 | 0 | } |
133 | |
|
134 | 0 | if (subCluSets != null) { |
135 | 0 | for (CluSetInfo subCluSet : subCluSets) { |
136 | 0 | if (subCluSet.getIsReusable()) { |
137 | 0 | unWrappedCluSetIds = (unWrappedCluSetIds == null)? |
138 | |
new ArrayList<String>() : unWrappedCluSetIds; |
139 | 0 | unWrappedCluSetIds.add(subCluSet.getId()); |
140 | |
} else { |
141 | 0 | wrappedCluSets = (wrappedCluSets == null)? |
142 | |
new ArrayList<CluSetInfo>() : wrappedCluSets; |
143 | 0 | wrappedCluSets.add(subCluSet); |
144 | |
} |
145 | |
} |
146 | |
} |
147 | 0 | cluSetInfo.setCluSetIds(unWrappedCluSetIds); |
148 | 0 | if (wrappedCluSets != null) { |
149 | 0 | for (CluSetInfo wrappedCluSet : wrappedCluSets) { |
150 | 0 | MembershipQueryInfo mqInfo = wrappedCluSet.getMembershipQuery(); |
151 | 0 | if (wrappedCluSet.getCluIds() != null && !wrappedCluSet.getCluIds().isEmpty()) { |
152 | 0 | cluSetInfo.setCluIds(wrappedCluSet.getCluIds()); |
153 | |
} |
154 | 0 | if (mqInfo != null && mqInfo.getSearchTypeKey() != null && !mqInfo.getSearchTypeKey().isEmpty()) { |
155 | 0 | cluSetInfo.setMembershipQuery(mqInfo); |
156 | |
} |
157 | 0 | } |
158 | |
} |
159 | 0 | } |
160 | |
|
161 | |
private List<CluInformation> getCluInformations(List<String> cluIds) throws OperationFailedException { |
162 | 0 | List<CluInformation> result = new ArrayList<CluInformation>(); |
163 | 0 | if (cluIds != null) { |
164 | 0 | for (String cluId : cluIds) { |
165 | |
try { |
166 | 0 | VersionDisplayInfo versionInfo = luService.getCurrentVersion(LuServiceConstants.CLU_NAMESPACE_URI, cluId); |
167 | 0 | CluInfo cluInfo = luService.getClu(versionInfo.getId()); |
168 | 0 | if (cluInfo != null) { |
169 | |
|
170 | |
|
171 | 0 | String credits = ""; |
172 | 0 | List<CluResultInfo> cluResultInfos = luService.getCluResultByClu(versionInfo.getId()); |
173 | 0 | if (cluResultInfos != null) { |
174 | 0 | for (CluResultInfo cluResultInfo : cluResultInfos) { |
175 | 0 | String cluType = cluResultInfo.getType(); |
176 | |
|
177 | |
|
178 | 0 | if ((cluType == null) || (!cluType.equals("kuali.resultType.creditCourseResult"))) { |
179 | 0 | continue; |
180 | |
} |
181 | |
|
182 | |
|
183 | 0 | ResultComponentInfo resultComponentInfo = null; |
184 | 0 | List<String> resultValues = null; |
185 | 0 | String creditType = ""; |
186 | 0 | if (cluResultInfo.getResultOptions() != null) { |
187 | 0 | for (ResultOptionInfo resultOption : cluResultInfo.getResultOptions()) { |
188 | 0 | if (resultOption.getResultComponentId() != null) { |
189 | 0 | resultComponentInfo = lrcService.getResultComponent(resultOption.getResultComponentId()); |
190 | 0 | resultValues = resultComponentInfo.getResultValues(); |
191 | 0 | creditType = resultComponentInfo.getType(); |
192 | 0 | break; |
193 | |
} |
194 | |
} |
195 | |
} |
196 | 0 | if (resultValues == null) { |
197 | 0 | continue; |
198 | |
} |
199 | |
|
200 | 0 | if (!credits.isEmpty()) { |
201 | 0 | credits = credits + "; "; |
202 | |
} |
203 | |
|
204 | 0 | if (creditType.equals("kuali.resultComponentType.credit.degree.fixed")) { |
205 | 0 | credits = credits + resultValues.get(0); |
206 | 0 | } else if (creditType.equals("kuali.resultComponentType.credit.degree.multiple")) { |
207 | 0 | boolean firstValue = true; |
208 | 0 | for (String resultValue : resultValues) { |
209 | 0 | credits = credits + (firstValue ? "" :", ") + resultValue; |
210 | 0 | firstValue = false; |
211 | |
} |
212 | 0 | } else if (creditType.equals("kuali.resultComponentType.credit.degree.range")) { |
213 | 0 | credits = credits + resultComponentInfo.getAttributes().get("minCreditValue") + " - " + resultComponentInfo.getAttributes().get("maxCreditValue"); |
214 | |
} |
215 | 0 | } |
216 | |
} |
217 | |
|
218 | 0 | CluInformation cluInformation = new CluInformation(); |
219 | 0 | if (cluInfo.getOfficialIdentifier() != null) { |
220 | 0 | cluInformation.setCode(cluInfo.getOfficialIdentifier().getCode()); |
221 | 0 | cluInformation.setTitle(cluInfo.getOfficialIdentifier().getShortName()); |
222 | 0 | cluInformation.setCredits(credits); |
223 | |
} |
224 | |
|
225 | 0 | cluInformation.setType(cluInfo.getType()); |
226 | |
|
227 | 0 | if ("kuali.lu.type.Variation".equals(cluInfo.getType())){ |
228 | 0 | List<String> clus = luService.getCluIdsByRelation(cluInfo.getId(), "kuali.lu.lu.relation.type.hasVariationProgram"); |
229 | 0 | if (clus == null || clus.size() == 0){ |
230 | 0 | throw new RuntimeException("Statement Dependency clu found, but no parent Program exists"); |
231 | 0 | } else if(clus.size()>1){ |
232 | 0 | throw new RuntimeException("Statement Dependency clu can only have one parent Program relation"); |
233 | |
} |
234 | 0 | cluInformation.setParentCluId(clus.get(0)); |
235 | |
} |
236 | |
|
237 | 0 | cluInformation.setVerIndependentId(cluInfo.getId()); |
238 | 0 | result.add(cluInformation); |
239 | |
} |
240 | 0 | } catch (Exception e) { |
241 | 0 | throw new OperationFailedException("Failed to get info for cluId " + cluId, e); |
242 | 0 | } |
243 | |
} |
244 | |
} |
245 | 0 | return result; |
246 | |
} |
247 | |
|
248 | |
@Override |
249 | |
public CluSetInformation getCluSetInformation(String cluSetId) throws OperationFailedException { |
250 | 0 | CluSetInformation result = new CluSetInformation(); |
251 | 0 | CluSetInfo cluSetInfo = getCluSetInfo(cluSetId); |
252 | 0 | List<String> allCluIds = cluSetInfo.getCluIds(); |
253 | 0 | List<String> cluSetIds = cluSetInfo.getCluSetIds(); |
254 | 0 | final MembershipQueryInfo membershipQueryInfo = cluSetInfo.getMembershipQuery(); |
255 | 0 | result.setId(cluSetId); |
256 | 0 | if (allCluIds != null) { |
257 | 0 | List<CluInformation> clus = getCluInformations(allCluIds); |
258 | 0 | result.setClus(clus); |
259 | |
} |
260 | 0 | if (cluSetIds != null) { |
261 | 0 | List<CluSetInfo> cluSetInfos = getCluSetInfos(cluSetIds); |
262 | 0 | result.setCluSets(cluSetInfos); |
263 | |
} |
264 | 0 | if (membershipQueryInfo != null) { |
265 | 0 | SearchRequest searchRequest = new SearchRequest(); |
266 | 0 | searchRequest.setSearchKey(membershipQueryInfo.getSearchTypeKey()); |
267 | 0 | searchRequest.setParams(membershipQueryInfo.getQueryParamValueList()); |
268 | 0 | SearchResult searchResult = null; |
269 | |
try { |
270 | 0 | searchResult = luService.search(searchRequest); |
271 | 0 | } catch (Exception e) { |
272 | 0 | throw new OperationFailedException("Failed to search for clus in clu range", e); |
273 | 0 | } |
274 | 0 | List<CluInformation> clusInRange = new ArrayList<CluInformation>(); |
275 | 0 | List<SearchResultRow> rows = searchResult.getRows(); |
276 | 0 | for(SearchResultRow row : rows) { |
277 | 0 | List<SearchResultCell> cells = row.getCells(); |
278 | 0 | CluInformation cluInformation = new CluInformation(); |
279 | 0 | for(SearchResultCell cell : cells) { |
280 | 0 | if(cell.getKey().equals("lu.resultColumn.cluId")) { |
281 | 0 | cluInformation.setVerIndependentId(cell.getValue()); |
282 | |
} |
283 | 0 | if (cell.getKey().equals("lu.resultColumn.luOptionalCode")) { |
284 | 0 | cluInformation.setCode(cell.getValue()); |
285 | |
} |
286 | 0 | if (cell.getKey().equals("lu.resultColumn.luOptionalShortName")) { |
287 | 0 | cluInformation.setTitle(cell.getValue()); |
288 | |
} |
289 | |
} |
290 | 0 | clusInRange.add(cluInformation); |
291 | 0 | } |
292 | 0 | result.setMembershipQueryInfo(membershipQueryInfo); |
293 | 0 | result.setClusInRange(clusInRange); |
294 | |
} |
295 | 0 | if(result.getClus()!=null) |
296 | 0 | Collections.sort(result.getClus()); |
297 | 0 | return result; |
298 | |
} |
299 | |
|
300 | |
} |