1 package org.kuali.student.enrollment.class2.scheduleofclasses.util;
2
3 import org.kuali.rice.core.api.criteria.Predicate;
4 import org.kuali.rice.core.api.criteria.QueryByCriteria;
5 import org.kuali.student.enrollment.courseofferingset.dto.SocInfo;
6 import org.kuali.student.enrollment.courseofferingset.service.CourseOfferingSetService;
7 import org.kuali.student.r2.common.dto.ContextInfo;
8 import org.kuali.student.r2.core.atp.dto.AtpInfo;
9 import org.kuali.student.r2.core.atp.service.AtpService;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
15
16
17
18
19
20
21
22
23 public class ScheduleOfClassesUtil {
24
25 public static List<AtpInfo> getValidSocTerms(CourseOfferingSetService courseOfferingSetService, AtpService atpService, ContextInfo context){
26 List<SocInfo> socs;
27 List<String> termIds = new ArrayList<String>();
28 List<AtpInfo> atps = new ArrayList<AtpInfo>();
29
30
31 QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
32 qBuilder.setPredicates();
33 Predicate pred = equal("socState", "kuali.soc.state.published");
34 qBuilder.setPredicates(pred);
35 try {
36
37 socs = courseOfferingSetService.searchForSocs(qBuilder.build(), context);
38 for(SocInfo soc: socs){
39
40 termIds.add(soc.getTermId());
41 }
42
43 atps = atpService.getAtpsByIds(termIds, context);
44
45 } catch (Exception e) {
46 throw new RuntimeException("Error getting Valid SOC Terms", e);
47 }
48
49 return atps;
50 }
51
52
53
54
55
56
57
58 public static AtpInfo getCurrentAtp(List<AtpInfo> atpInfos){
59
60 Long currTime = System.currentTimeMillis();
61 AtpInfo closest = null;
62
63 for(AtpInfo atp : atpInfos){
64 if(isValid(atp)){
65 if(isBetween(currTime, atp)) {
66 return atp;
67 }else{
68 if(closest == null){
69 closest = atp;
70 } else{
71 closest = getClosest(currTime,closest,atp);
72 }
73 }
74
75 }
76 }
77
78
79
80 return closest;
81
82 }
83
84
85
86
87
88
89 private static boolean isValid(AtpInfo atp){
90 if(atp != null && atp.getStartDate() != null && atp.getEndDate() != null)
91 return true;
92 else
93 return false;
94
95 }
96
97
98
99
100
101
102
103
104 private static boolean isBetween(Long currTime, AtpInfo atp){
105 if(atp.getStartDate() != null && atp.getStartDate() != null){
106 Long startDate = atp.getStartDate().getTime();
107 Long endDate = atp.getEndDate().getTime();
108 if(currTime >= startDate && currTime <= endDate){
109 return true;
110 }
111 }
112 return false;
113 }
114
115
116
117
118
119
120
121
122 private static AtpInfo getClosest(Long currTime, AtpInfo pointA, AtpInfo pointB){
123 Long distA = Math.abs(currTime - getAverageTime(pointA));
124 Long distB = Math.abs(currTime - getAverageTime(pointB));
125
126 if(distA <= distB)
127 return pointA;
128 else
129 return pointB;
130 }
131
132 private static Long getAverageTime(AtpInfo atp) {
133 if(isValid(atp)){
134 return Long.valueOf((atp.getStartDate().getTime() + atp.getEndDate().getTime())/2);
135 } else{
136 return Long.MIN_VALUE;
137 }
138
139 }
140
141 }