View Javadoc

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   * Created with IntelliJ IDEA.
18   * User: gtaylor
19   * Date: 9/25/12
20   * Time: 3:10 PM
21   * General Schedule of Classes utility file. Methods should be static
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          //Build a predicate to search for published Socs
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              //Try the search
37              socs = courseOfferingSetService.searchForSocs(qBuilder.build(), context);
38              for(SocInfo soc: socs){
39                  //Add all published Soc termIds to termIds List
40                  termIds.add(soc.getTermId());
41              }
42              //Use AtpService to get Term name by Id
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       * This method will return the first ATP that contains the current time. If that isn't possible it will return
54       * the closest to the current time.
55       * @param atpInfos
56       * @return
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       * checks if the atp is not null, and that the start and end dates are not null
86       * @param atp
87       * @return
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       * If the currTime is between the atp start and end date return true else false
100      * @param currTime
101      * @param atp
102      * @return
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      * returns the atp closest to the currTime.
117      * @param currTime
118      * @param pointA
119      * @param pointB
120      * @return
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 }