1 package org.kuali.student.myplan.plan.service;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.apache.commons.lang.StringUtils;
11 import org.kuali.student.ap.framework.config.KsapFrameworkServiceLocator;
12 import org.kuali.student.ap.framework.context.TermHelper;
13 import org.kuali.student.ap.framework.context.YearTerm;
14 import org.kuali.student.common.collection.KSCollectionUtils;
15 import org.kuali.student.enrollment.academicrecord.dto.StudentCourseRecordInfo;
16 import org.kuali.student.myplan.plan.dataobject.AcademicRecordDataObject;
17 import org.kuali.student.myplan.plan.dataobject.PlannedCourseDataObject;
18 import org.kuali.student.myplan.plan.dataobject.PlannedTerm;
19 import org.kuali.student.myplan.plan.dataobject.TermNoteDataObject;
20 import org.kuali.student.r2.core.acal.infc.Term;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24
25
26
27
28 public class PlannedTermsHelperBase {
29
30 private static final Logger LOG = LoggerFactory.getLogger(PlannedTermsHelperBase.class);
31
32 public static List<PlannedTerm> populatePlannedTerms(List<PlannedCourseDataObject> plannedCoursesList,
33 List<PlannedCourseDataObject> backupCoursesList, List<StudentCourseRecordInfo> studentCourseRecordInfos,
34 List<PlannedCourseDataObject> cartCoursesList, List<TermNoteDataObject> termNoteList, String focusAtpId, boolean fullPlanView) {
35
36 TermHelper th = KsapFrameworkServiceLocator.getTermHelper();
37 Map<String, PlannedTerm> termsList = new HashMap<String, PlannedTerm>();
38 List<Term> planningTerms = th.getPlanningTerms();
39 for (Term t : planningTerms) {
40 PlannedTerm term = new PlannedTerm();
41 term.setAtpId(t.getId());
42 term.setQtrYear(t.getName());
43 termsList.put(t.getId(), term);
44 }
45
46 YearTerm focusQuarterYear;
47 if (StringUtils.isEmpty(focusAtpId)) {
48 try{
49 focusQuarterYear = th.getYearTerm(th.getCurrentTerm());
50 }catch(Exception e){
51 LOG.warn("Could not find current term, using next term",e);
52 try{
53 focusQuarterYear = th.getYearTerm(KSCollectionUtils.getRequiredZeroElement(th.getPlanningTerms()));
54 }catch(Exception e2){
55 try{
56 LOG.warn("Could not find future planned term, using last term", e2);
57 StudentCourseRecordInfo studentInfo = studentCourseRecordInfos.get(studentCourseRecordInfos.size() - 1);
58 String termId = studentInfo.getTermId();
59 focusQuarterYear = th.getYearTerm(termId);
60 }catch(Exception e3){
61 LOG.error("Could not find last term, using first term");
62 focusQuarterYear = th.getYearTerm(planningTerms.get(0));
63 }
64 }
65 }
66 } else {
67 focusQuarterYear = th.getYearTerm(th.getFirstTermOfAcademicYear(th.getYearTerm(focusAtpId)));
68 }
69
70 for (PlannedCourseDataObject plan : plannedCoursesList) {
71 String atp = plan.getPlanItemDataObject().getAtp();
72 PlannedTerm pt = termsList.get(atp);
73 if (pt == null)
74 LOG.warn("Course is not planned for a planning term {} {}", plan.getCourseDetails(), atp);
75 else
76 pt.getPlannedList().add(plan);
77 }
78
79 if (backupCoursesList != null)
80 for (PlannedCourseDataObject bl : backupCoursesList) {
81 String atp = bl.getPlanItemDataObject().getAtp();
82 PlannedTerm pt = termsList.get(atp);
83 if (pt == null)
84 LOG.warn("Backup course is not planned for a planning term {} {}", bl.getCourseDetails(), atp);
85 else
86 pt.getBackupList().add(bl);
87 }
88
89 if (cartCoursesList != null) {
90 for (PlannedCourseDataObject bl : cartCoursesList) {
91 String atp = bl.getPlanItemDataObject().getAtp();
92 PlannedTerm pt = termsList.get(atp);
93 if (pt == null)
94 LOG.warn("Cart course is not planned for a planning term {} {}", bl.getCourseDetails(), atp);
95 else
96 pt.getCartList().add(bl);
97 }
98 }
99
100 if (studentCourseRecordInfos.size() > 0) {
101 for (StudentCourseRecordInfo studentInfo : studentCourseRecordInfos) {
102 String termId = studentInfo.getTermId();
103 PlannedTerm pt = termsList.get(termId);
104 if (pt == null) {
105 Term t = KsapFrameworkServiceLocator.getTermHelper().getTerm(termId);
106 PlannedTerm term = new PlannedTerm();
107 term.setAtpId(t.getId());
108 term.setQtrYear(t.getName());
109 termsList.put(termId, term);
110 pt = term;
111 }
112
113 AcademicRecordDataObject academicRecordDataObject = new AcademicRecordDataObject();
114 academicRecordDataObject.setAtpId(termId);
115 academicRecordDataObject.setPersonId(studentInfo.getPersonId());
116 academicRecordDataObject.setCourseCode(studentInfo.getCourseCode());
117 academicRecordDataObject.setCourseId(studentInfo.getId());
118 academicRecordDataObject.setCourseTitle(studentInfo.getCourseTitle());
119 academicRecordDataObject.setCredit(studentInfo.getCreditsEarned());
120 if (!"X".equalsIgnoreCase(studentInfo.getCalculatedGradeValue())) {
121 academicRecordDataObject.setGrade(studentInfo.getCalculatedGradeValue());
122 } else if ("X".equalsIgnoreCase(studentInfo.getCalculatedGradeValue())
123 && KsapFrameworkServiceLocator.getTermHelper().isCompleted(termId)) {
124 academicRecordDataObject.setGrade(studentInfo.getCalculatedGradeValue());
125 }
126 academicRecordDataObject.setRepeated(studentInfo.getIsRepeated()!=null ? studentInfo.getIsRepeated():false);
127 pt.getAcademicRecord().add(academicRecordDataObject);
128 }
129 }
130
131 List<PlannedTerm> perfectPlannedTerms = new ArrayList<PlannedTerm>(termsList.values());
132
133 if (termNoteList != null) {
134 int count = perfectPlannedTerms.size();
135 for (TermNoteDataObject bl : termNoteList) {
136 String atp = bl.getAtpId();
137
138 boolean added = false;
139 for (int i = 0; i < count; i++) {
140 if (atp.equalsIgnoreCase(perfectPlannedTerms.get(i).getAtpId())) {
141 perfectPlannedTerms.get(i).getTermNoteList().add(bl);
142 added = true;
143 }
144 }
145 if (!added) {
146 PlannedTerm plannedTerm = new PlannedTerm();
147 plannedTerm.setAtpId(atp);
148 StringBuilder str = new StringBuilder();
149 YearTerm yearTerm = KsapFrameworkServiceLocator
150 .getTermHelper().getYearTerm(atp);
151 str = str.append(yearTerm.getTermName());
152 String QtrYear = str.substring(0, 1).toUpperCase()
153 .concat(str.substring(1, str.length()));
154 plannedTerm.setQtrYear(QtrYear);
155 plannedTerm.getTermNoteList().add(bl);
156 perfectPlannedTerms.add(plannedTerm);
157 count++;
158 }
159 }
160 }
161
162
163
164
165 Collections.sort(perfectPlannedTerms, new Comparator<PlannedTerm>() {
166 @Override
167 public int compare(PlannedTerm plannedTerm1, PlannedTerm plannedTerm2) {
168 return KsapFrameworkServiceLocator.getTermHelper().getYearTerm(plannedTerm1.getAtpId())
169 .compareTo(KsapFrameworkServiceLocator.getTermHelper().getYearTerm(plannedTerm2.getAtpId()));
170 }
171 });
172
173
174
175 for (int i = 0; i < perfectPlannedTerms.size(); i++) {
176 PlannedTerm pt = perfectPlannedTerms.get(i);
177 if (pt.getAtpId().isEmpty())
178 continue;
179 YearTerm qy = th.getYearTerm(pt.getAtpId());
180 if (qy.equals(focusQuarterYear)) {
181 pt.setIndex(i);
182 break;
183 }
184 }
185
186
187
188
189
190 for (PlannedTerm pl : perfectPlannedTerms) {
191 if (th.isPlanning(pl.getAtpId()))
192 pl.setOpenForPlanning(true);
193 if (th.isCompleted(pl.getAtpId()))
194 pl.setCompletedTerm(true);
195 Term t = th.getCurrentTerm();
196 if(t!=null){
197 if (t.getId().equals(pl.getAtpId()))
198 pl.setCurrentTermForView(true);
199 }
200 }
201
202 populateHelpIconFlags(perfectPlannedTerms);
203 return perfectPlannedTerms;
204 }
205
206
207
208
209
210
211
212 private static void populateHelpIconFlags(List<PlannedTerm> plannedTerms) {
213
214 int index = plannedTerms.size() - 1;
215 while (index >= 0) {
216 for (int i = 4; i > 0; i--) {
217 if (plannedTerms.get(index).isCurrentTermForView() || i == 1) {
218 plannedTerms.get(index).setDisplayBackupHelp(true);
219 plannedTerms.get(index).setDisplayPlannedHelp(true);
220 plannedTerms.get(index).setDisplayCartHelp(true);
221 index = index - i;
222 break;
223 }
224 index--;
225 if (index < 0)
226 break;
227 }
228 }
229 index = 0;
230 while (index < plannedTerms.size()) {
231 for (int i = 1; i < 4; i++) {
232 if (index < plannedTerms.size() && plannedTerms.get(index).isCompletedTerm() && i == 1) {
233 plannedTerms.get(index).setDisplayCompletedHelp(true);
234 index = index + (5 - i);
235 break;
236 }
237 index++;
238
239 }
240 }
241 index = plannedTerms.size() - 1;
242 while (index >= 0) {
243 for (int i = 4; i > 0; i--) {
244 if (index < 0)
245 break;
246 if (plannedTerms.get(index).isCurrentTermForView()
247 || !plannedTerms.get(index).isCompletedTerm()
248 && (plannedTerms.get(index).getAcademicRecord().size() > 0 || !plannedTerms.get(index)
249 .isOpenForPlanning())) {
250 plannedTerms.get(index).setDisplayRegisteredHelp(true);
251 index = index - i;
252 break;
253 }
254 index--;
255 }
256 }
257
258 }
259
260 }