1 package org.kuali.student.lum.program.server;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.apache.log4j.Logger;
9 import org.kuali.student.r1.common.assembly.data.Data;
10 import org.kuali.student.r2.common.dto.RichTextInfo;
11 import org.kuali.student.r2.common.dto.StatusInfo;
12 import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
13 import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
14 import org.kuali.student.r2.core.search.dto.SearchResultInfo;
15 import org.kuali.student.r2.common.util.ContextUtils;
16 import org.kuali.student.r1.core.statement.dto.ReqComponentInfo;
17 import org.kuali.student.r1.core.statement.dto.StatementTreeViewInfo;
18 import org.kuali.student.r1.core.statement.service.StatementService;
19 import org.kuali.student.common.ui.client.service.DataSaveResult;
20 import org.kuali.student.common.ui.server.gwt.DataGwtServlet;
21 import org.kuali.student.core.statement.ui.client.widgets.rules.ReqComponentInfoUi;
22 import org.kuali.student.core.statement.ui.client.widgets.rules.RulesUtil;
23 import org.kuali.student.lum.common.server.StatementUtil;
24 import org.kuali.student.r2.lum.clu.service.CluService;
25 import org.kuali.student.lum.program.client.ProgramConstants;
26 import org.kuali.student.lum.program.client.requirements.ProgramRequirementsDataModel;
27 import org.kuali.student.lum.program.client.requirements.ProgramRequirementsSummaryView;
28 import org.kuali.student.lum.program.client.rpc.MajorDisciplineRpcService;
29 import org.kuali.student.r2.core.proposal.dto.ProposalInfo;
30 import org.kuali.student.r2.core.proposal.service.ProposalService;
31 import org.kuali.student.r2.lum.program.dto.ProgramRequirementInfo;
32 import org.kuali.student.r2.lum.program.service.ProgramService;
33
34 public class MajorDisciplineRpcServlet extends DataGwtServlet implements MajorDisciplineRpcService {
35
36 public static final String PREVIOUS_VERSION_INFO = "proposal";
37
38 final Logger LOG = Logger.getLogger(MajorDisciplineRpcServlet.class);
39
40 private static final long serialVersionUID = 1L;
41
42 private ProposalService proposalService;
43 private ProgramService programService;
44 private StatementService statementService;
45 protected StateChangeService stateChangeService;
46 private CluService cluService;
47
48
49
50
51
52
53
54
55 public DataSaveResult updateState(Data data, String state) throws Exception {
56 try {
57
58 String programId = data.get(ProgramConstants.ID);
59
60
61
62 Data previousVersionInfo = data.query(PREVIOUS_VERSION_INFO);
63 String endEntryTerm = null;
64 String endEnrollTerm = null;
65 String endInstAdmitTerm = null;
66 if (previousVersionInfo != null) {
67 endEntryTerm = previousVersionInfo.get(ProgramConstants.PREV_END_PROGRAM_ENTRY_TERM);
68 endEnrollTerm = previousVersionInfo.get(ProgramConstants.PREV_END_PROGRAM_ENROLL_TERM);
69 endInstAdmitTerm = previousVersionInfo.get(ProgramConstants.PREV_END_INST_ADMIN_TERM);
70 stateChangeService.changeState(endEntryTerm, endEnrollTerm, endInstAdmitTerm, programId, state);
71 }
72 else {
73
74 stateChangeService.changeState(programId, state);
75 }
76
77
78 DataSaveResult result = new DataSaveResult();
79 result.setValue(data);
80 return result;
81 } catch (DataValidationErrorException e) {
82 LOG.error("Error Updating Major Dicipline State", e);
83 DataSaveResult result = new DataSaveResult();
84 result.setValidationResults(e.getValidationResults());
85 return result;
86 } catch (Exception e) {
87 LOG.error("Error Updating Major Dicipline State", e);
88 throw e;
89 }
90
91 }
92
93 public List<ProgramRequirementInfo> getProgramRequirements(List<String> programRequirementIds) throws Exception {
94 try
95 {
96 List<ProgramRequirementInfo> programReqInfos = new ArrayList<ProgramRequirementInfo>();
97
98 for (String programReqId : programRequirementIds) {
99 ProgramRequirementInfo rule = null;
100 rule = programService.getProgramRequirement(programReqId, ContextUtils.getContextInfo());
101 setProgReqNL(rule);
102 programReqInfos.add(rule);
103 }
104
105 return programReqInfos;
106 } catch (Exception ex) {
107
108 ex.printStackTrace();
109 throw new RuntimeException(ex);
110 }
111 }
112
113 public Map<Integer, ProgramRequirementInfo> storeProgramRequirements(
114 Map<Integer, ProgramRequirementsDataModel.requirementState> states,
115 Map<Integer, ProgramRequirementInfo> progReqs) throws Exception {
116 Map<Integer, ProgramRequirementInfo> storedRules = new HashMap<Integer, ProgramRequirementInfo>();
117 try
118 {
119 for (Integer key : progReqs.keySet()) {
120 ProgramRequirementInfo rule = progReqs.get(key);
121 switch (states.get(key)) {
122 case STORED:
123
124 storedRules.put(key, null);
125 break;
126 case ADDED:
127 storedRules.put(key, createProgramRequirement(rule));
128 break;
129 case EDITED:
130 storedRules.put(key, updateProgramRequirement(rule));
131 break;
132 case DELETED:
133 storedRules.put(key, null);
134 deleteProgramRequirement(rule.getId());
135 break;
136 default:
137 break;
138 }
139 }
140 return storedRules;
141 } catch (Exception ex) {
142
143 ex.printStackTrace();
144 throw new RuntimeException(ex);
145 }
146 }
147
148 public ProgramRequirementInfo createProgramRequirement(ProgramRequirementInfo programRequirementInfo)
149 throws Exception {
150 try
151 {
152
153 if (programRequirementInfo.getId().indexOf(ProgramRequirementsSummaryView.NEW_PROG_REQ_ID) >= 0) {
154 programRequirementInfo.setId(null);
155 }
156
157
158 StatementUtil.stripStatementIds(programRequirementInfo.getStatement());
159
160
161
162 StatementUtil.updateStatementTreeViewInfoState(programRequirementInfo.getStateKey(),
163 programRequirementInfo.getStatement());
164
165
166 ProgramRequirementInfo rule = programService.createProgramRequirement(programRequirementInfo.getId(),
167 programRequirementInfo, ContextUtils.getContextInfo());
168
169
170 setProgReqNL(rule);
171
172 return rule;
173 } catch (Exception ex) {
174
175 ex.printStackTrace();
176 throw new RuntimeException(ex);
177 }
178 }
179
180 public StatusInfo deleteProgramRequirement(String programRequirementId) throws Exception {
181 try
182 {
183 return programService.deleteProgramRequirement(programRequirementId, ContextUtils.getContextInfo());
184 } catch (Exception ex) {
185
186 ex.printStackTrace();
187 throw new RuntimeException(ex);
188 }
189 }
190
191 public ProgramRequirementInfo updateProgramRequirement(ProgramRequirementInfo programRequirementInfo)
192 throws Exception {
193 try
194 {
195
196 StatementUtil.stripStatementIds(programRequirementInfo.getStatement());
197
198
199
200 StatementUtil.updateStatementTreeViewInfoState(programRequirementInfo.getStateKey(),
201 programRequirementInfo.getStatement());
202
203
204 if (programRequirementInfo.getDescr() == null) {
205 programRequirementInfo.setDescr(new RichTextInfo());
206 }
207
208 ProgramRequirementInfo rule = programService.updateProgramRequirement(null, null, programRequirementInfo,
209 ContextUtils.getContextInfo());
210 setProgReqNL(rule);
211 return rule;
212 } catch (Exception ex) {
213
214 ex.printStackTrace();
215 throw new RuntimeException(ex);
216 }
217 }
218
219 private void setProgReqNL(ProgramRequirementInfo programRequirementInfo) throws Exception {
220 setReqCompNL(programRequirementInfo.getStatement());
221 }
222
223 private void setReqCompNL(StatementTreeViewInfo tree) throws Exception {
224 List<StatementTreeViewInfo> statements = tree.getStatements();
225 List<ReqComponentInfo> reqComponentInfos = tree.getReqComponents();
226
227 if ((statements != null) && (statements.size() > 0)) {
228
229 for (StatementTreeViewInfo statement : statements) {
230 setReqCompNL(statement);
231 }
232 } else if ((reqComponentInfos != null) && (reqComponentInfos.size() > 0)) {
233
234 for (int i = 0; i < reqComponentInfos.size(); i++) {
235 ReqComponentInfoUi reqUi = null;
236 reqUi = RulesUtil.clone(reqComponentInfos.get(i));
237 reqUi.setNaturalLanguageTranslation(statementService.translateReqComponentToNL(reqUi, "KUALI.RULE",
238 "en"));
239 reqUi.setPreviewNaturalLanguageTranslation(statementService.translateReqComponentToNL(reqUi,
240 "KUALI.RULE.PREVIEW", "en"));
241 reqComponentInfos.set(i, reqUi);
242 }
243 }
244 }
245
246 @Override
247 public Boolean isLatestVersion(String versionIndId, Long versionSequenceNumber) throws Exception {
248 try {
249
250
251 SearchRequestInfo request = new SearchRequestInfo("lu.search.isVersionable");
252 request.addParam("lu.queryParam.versionIndId", versionIndId);
253 request.addParam("lu.queryParam.sequenceNumber", versionSequenceNumber.toString());
254 List<String> states = new ArrayList<String>();
255 states.add("Approved");
256 states.add("Active");
257 states.add("Draft");
258 states.add("Superseded");
259 request.addParam("lu.queryParam.luOptionalState", states);
260 SearchResultInfo result = cluService.search(request, ContextUtils.getContextInfo());
261
262 String resultString = result.getRows().get(0).getCells().get(0).getValue();
263 return "0".equals(resultString);
264 } catch (Exception ex) {
265
266 ex.printStackTrace();
267 throw new RuntimeException(ex);
268 }
269 }
270
271 public void setProgramService(ProgramService programService) {
272 this.programService = programService;
273 }
274
275 public void setStatementService(StatementService statementService) {
276 this.statementService = statementService;
277 }
278
279 public void setStateChangeService(StateChangeService stateChangeService) {
280 this.stateChangeService = stateChangeService;
281 }
282
283
284
285
286
287
288
289
290
291
292
293
294 @Override
295 public Boolean isProposal(String referenceTypeKey, String referenceId) {
296 try {
297
298
299
300
301
302 List<ProposalInfo> proposals = null;
303 proposals = proposalService.getProposalsByReference(referenceTypeKey, referenceId, ContextUtils.getContextInfo());
304
305
306 if (proposals != null && proposals.size() >= 1) {
307 return new Boolean(true);
308 }
309
310
311 return new Boolean(false);
312 } catch (Exception ex) {
313
314 ex.printStackTrace();
315 throw new RuntimeException(ex);
316 }
317
318 }
319
320
321
322
323
324
325
326 public ProposalService getProposalService() {
327 return proposalService;
328 }
329
330 public void setProposalService(ProposalService proposalService) {
331 this.proposalService = proposalService;
332
333 }
334
335 public void setCluService(CluService cluService) {
336 this.cluService = cluService;
337 }
338
339 }