|  1 |     | 
   package org.kuali.student.enrollment.class2.acal.keyvalue;  | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
     | 
  |  18 |     | 
     | 
  |  19 |     | 
   import org.apache.commons.lang.StringUtils;  | 
  |  20 |     | 
   import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;  | 
  |  21 |     | 
   import org.kuali.rice.core.api.util.ConcreteKeyValue;  | 
  |  22 |     | 
   import org.kuali.rice.core.api.util.KeyValue;  | 
  |  23 |     | 
   import org.kuali.rice.krad.keyvalues.KeyValuesBase;  | 
  |  24 |     | 
   import org.kuali.student.enrollment.acal.dto.AcademicCalendarInfo;  | 
  |  25 |     | 
   import org.kuali.student.enrollment.acal.dto.TermInfo;  | 
  |  26 |     | 
   import org.kuali.student.enrollment.acal.service.AcademicCalendarService;  | 
  |  27 |     | 
   import org.kuali.student.r2.common.dto.ContextInfo;  | 
  |  28 |     | 
   import org.kuali.student.r2.common.exceptions.*;  | 
  |  29 |     | 
   import org.kuali.student.r2.common.util.constants.AtpServiceConstants;  | 
  |  30 |     | 
     | 
  |  31 |     | 
   import javax.xml.namespace.QName;  | 
  |  32 |     | 
   import java.io.Serializable;  | 
  |  33 |     | 
   import java.util.ArrayList;  | 
  |  34 |     | 
   import java.util.Calendar;  | 
  |  35 |     | 
   import java.util.Date;  | 
  |  36 |     | 
   import java.util.List;  | 
  |  37 |     | 
     | 
  |  38 |     | 
     | 
  |  39 |    0 |    public class TermKeyValues extends KeyValuesBase implements Serializable { | 
  |  40 |     | 
     | 
  |  41 |     | 
       private static final long serialVersionUID = 1L;  | 
  |  42 |     | 
     | 
  |  43 |     | 
       private transient AcademicCalendarService acalService;  | 
  |  44 |     | 
     | 
  |  45 |     | 
     | 
  |  46 |     | 
       @Override  | 
  |  47 |     | 
         | 
  |  48 |     | 
     | 
  |  49 |     | 
     | 
  |  50 |     | 
     | 
  |  51 |     | 
     | 
  |  52 |     | 
     | 
  |  53 |     | 
     | 
  |  54 |     | 
     | 
  |  55 |     | 
       public List<KeyValue> getKeyValues() { | 
  |  56 |     | 
     | 
  |  57 |    0 |            List<AcademicCalendarInfo> acals = new ArrayList<AcademicCalendarInfo>();  | 
  |  58 |    0 |            ContextInfo context = new ContextInfo();  | 
  |  59 |     | 
     | 
  |  60 |     | 
     | 
  |  61 |    0 |            List<TermInfo> terms = new ArrayList<TermInfo>();  | 
  |  62 |     | 
           try { | 
  |  63 |    0 |                Calendar nowCal = Calendar.getInstance();  | 
  |  64 |    0 |                nowCal.setTime(new Date());  | 
  |  65 |    0 |                int year = nowCal.get(Calendar.YEAR);  | 
  |  66 |    0 |                acals.addAll(getAcalService().getAcademicCalendarsByStartYear(year - 1, context));  | 
  |  67 |    0 |                acals.addAll(getAcalService().getAcademicCalendarsByStartYear(year, context));  | 
  |  68 |    0 |                acals.addAll(getAcalService().getAcademicCalendarsByStartYear(year+1, context));  | 
  |  69 |    0 |                for (AcademicCalendarInfo acal : acals) { | 
  |  70 |    0 |                    if (StringUtils.equals(acal.getStateKey(), AtpServiceConstants.ATP_OFFICIAL_STATE_KEY)){ | 
  |  71 |    0 |                        terms.addAll(getAcalService().getTermsForAcademicCalendar(acal.getId(), context));  | 
  |  72 |     | 
                   }  | 
  |  73 |     | 
               }  | 
  |  74 |     | 
     | 
  |  75 |     | 
                 | 
  |  76 |    0 |                for (TermInfo term : terms) { | 
  |  77 |    0 |                    if ("testTermId1".equals(term.getId())) { | 
  |  78 |    0 |                        terms.remove(term);  | 
  |  79 |    0 |                        terms.add(0, term);  | 
  |  80 |    0 |                        break;  | 
  |  81 |     | 
                   }  | 
  |  82 |     | 
               }  | 
  |  83 |    0 |            } catch (DoesNotExistException e) { | 
  |  84 |    0 |                throw new RuntimeException("No Terms found for current AcademicCalendar(s)! There should be some in the database.", e); | 
  |  85 |    0 |            } catch (InvalidParameterException e) { | 
  |  86 |    0 |                throw new RuntimeException(e);  | 
  |  87 |    0 |            } catch (MissingParameterException e) { | 
  |  88 |    0 |                throw new RuntimeException(e);  | 
  |  89 |    0 |            } catch (OperationFailedException e) { | 
  |  90 |    0 |                throw new RuntimeException(e);  | 
  |  91 |    0 |            } catch (PermissionDeniedException e) { | 
  |  92 |    0 |                throw new RuntimeException(e);  | 
  |  93 |    0 |            }  | 
  |  94 |     | 
     | 
  |  95 |    0 |            List<KeyValue> keyValues = new ArrayList<KeyValue>();  | 
  |  96 |     | 
     | 
  |  97 |    0 |            for(TermInfo term : terms) { | 
  |  98 |    0 |                keyValues.add(new ConcreteKeyValue(term.getId(), term.getName()));  | 
  |  99 |     | 
           }  | 
  |  100 |     | 
     | 
  |  101 |    0 |            return keyValues;  | 
  |  102 |     | 
       }  | 
  |  103 |     | 
     | 
  |  104 |     | 
       protected AcademicCalendarService getAcalService() { | 
  |  105 |    0 |            if(acalService == null) { | 
  |  106 |    0 |                acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/acal", "AcademicCalendarService")); | 
  |  107 |     | 
           }  | 
  |  108 |    0 |            return this.acalService;  | 
  |  109 |     | 
       }  | 
  |  110 |     | 
   }  |