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