Coverage Report - org.kuali.student.enrollment.class2.acal.service.impl.AcademicCalendarViewHelperServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
AcademicCalendarViewHelperServiceImpl
0%
0/655
0%
0/318
4.796
 
 1  
 /**
 2  
  * Copyright 2012 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  *
 15  
  */
 16  
 package org.kuali.student.enrollment.class2.acal.service.impl;
 17  
 
 18  
 import org.apache.commons.lang.BooleanUtils;
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.apache.commons.lang.math.NumberUtils;
 21  
 import org.kuali.rice.core.api.criteria.Predicate;
 22  
 import org.kuali.rice.core.api.criteria.QueryByCriteria;
 23  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 24  
 import org.kuali.rice.core.api.util.ConcreteKeyValue;
 25  
 import org.kuali.rice.core.api.util.KeyValue;
 26  
 import org.kuali.rice.krad.uif.UifConstants;
 27  
 import org.kuali.rice.krad.uif.container.CollectionGroup;
 28  
 import org.kuali.rice.krad.uif.control.SelectControl;
 29  
 import org.kuali.rice.krad.uif.field.InputField;
 30  
 import org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl;
 31  
 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
 32  
 import org.kuali.rice.krad.uif.view.View;
 33  
 import org.kuali.rice.krad.util.GlobalVariables;
 34  
 import org.kuali.student.enrollment.acal.constants.AcademicCalendarServiceConstants;
 35  
 import org.kuali.student.enrollment.acal.dto.*;
 36  
 import org.kuali.student.enrollment.acal.service.AcademicCalendarService;
 37  
 import org.kuali.student.enrollment.class2.acal.dto.*;
 38  
 import org.kuali.student.enrollment.class2.acal.form.AcademicCalendarForm;
 39  
 import org.kuali.student.enrollment.class2.acal.form.HolidayCalendarForm;
 40  
 import org.kuali.student.enrollment.class2.acal.service.AcademicCalendarViewHelperService;
 41  
 import org.kuali.student.enrollment.class2.acal.util.CalendarConstants;
 42  
 import org.kuali.student.enrollment.class2.acal.util.CommonUtils;
 43  
 import org.kuali.student.r2.common.dto.ContextInfo;
 44  
 import org.kuali.student.r2.common.dto.LocaleInfo;
 45  
 import org.kuali.student.r2.common.dto.RichTextInfo;
 46  
 import org.kuali.student.r2.common.exceptions.*;
 47  
 import org.kuali.student.r2.common.util.constants.AtpServiceConstants;
 48  
 import org.kuali.student.r2.common.util.constants.TypeServiceConstants;
 49  
 import org.kuali.student.r2.core.state.dto.StateInfo;
 50  
 import org.kuali.student.r2.core.type.dto.TypeInfo;
 51  
 import org.kuali.student.r2.core.type.service.TypeService;
 52  
 
 53  
 import javax.xml.namespace.QName;
 54  
 import java.text.SimpleDateFormat;
 55  
 import java.util.*;
 56  
 
 57  
 import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
 58  
 
 59  
 
 60  
 /**
 61  
  * This class implement ViewHelperServiceImpl for  all AcademicCalendar views
 62  
  *
 63  
  * @author Kuali Student Team
 64  
  */
 65  0
 public class AcademicCalendarViewHelperServiceImpl extends ViewHelperServiceImpl implements AcademicCalendarViewHelperService {
 66  
 
 67  
     private AcademicCalendarService acalService;
 68  
     private ContextInfo contextInfo;
 69  
     private TypeService typeService;
 70  
 
 71  
     private List<TypeInfo> holidayTypes;
 72  0
     private Map<String, List<TypeInfo>> typesByGroupTypeMap = new HashMap<String, List<TypeInfo>>();
 73  
 
 74  
     public void saveHolidayCalendar(HolidayCalendarForm hcForm) throws Exception{
 75  
 
 76  
         //Save holiday calendar
 77  0
         HolidayCalendarInfo hcInfo = hcForm.getHolidayCalendarInfo();
 78  0
         if (StringUtils.isBlank(hcInfo.getStateKey())){
 79  0
             hcInfo.setStateKey(AtpServiceConstants.ATP_DRAFT_STATE_KEY);
 80  
         }
 81  0
         hcInfo.setTypeKey(AcademicCalendarServiceConstants.HOLIDAY_CALENDAR_TYPE_KEY);
 82  0
         hcInfo.setDescr(CommonUtils.buildDesc("no description"));
 83  
 
 84  0
         if (StringUtils.isBlank(hcInfo.getId())){
 85  0
             HolidayCalendarInfo createdHCal = getAcalService().createHolidayCalendar(AcademicCalendarServiceConstants.HOLIDAY_CALENDAR_TYPE_KEY, hcInfo, getContextInfo());
 86  0
             createdHCal = getAcalService().getHolidayCalendar(createdHCal.getId(),getContextInfo());
 87  0
             hcForm.setHolidayCalendarInfo(createdHCal);
 88  0
         }else{
 89  0
             HolidayCalendarInfo updatedHCal = getAcalService().updateHolidayCalendar(hcInfo.getId(), hcInfo, getContextInfo());
 90  0
             updatedHCal = getAcalService().getHolidayCalendar(updatedHCal.getId(),getContextInfo());
 91  0
             hcForm.setHolidayCalendarInfo(updatedHCal);
 92  
         }
 93  
 
 94  
         //Save holidays
 95  0
         List<HolidayWrapper> holidays = hcForm.getHolidays();
 96  
         // save list of new holiday IDs here:
 97  0
         List<String> newHolidayIdList = new ArrayList<String>(holidays.size());
 98  
 
 99  
         HolidayInfo holidayInfo, storedHolidayInfo;
 100  0
         for (HolidayWrapper holidayWrapper : holidays){
 101  
 
 102  0
             holidayInfo = holidayWrapper.getHolidayInfo();
 103  0
             holidayWrapper.setTypeName(getHolidayTypeName(holidayWrapper.getTypeKey()));
 104  0
             holidayInfo.setStateKey(AtpServiceConstants.MILESTONE_DRAFT_STATE_KEY);
 105  0
             holidayInfo.setDescr(CommonUtils.buildDesc("no description"));
 106  0
             holidayInfo.setIsAllDay(holidayWrapper.isAllDay());
 107  0
             holidayInfo.setIsInstructionalDay(holidayWrapper.isInstructional());
 108  0
             holidayInfo.setStartDate(holidayWrapper.getStartDate());
 109  0
             holidayInfo.setName(holidayWrapper.getTypeName());
 110  0
             holidayInfo.setStartDate(getStartDateWithUpdatedTime(holidayWrapper,true));
 111  0
             setHolidayEndDate(holidayWrapper);
 112  
 
 113  0
             if (StringUtils.isBlank(holidayInfo.getId())){
 114  0
                 storedHolidayInfo = getAcalService().createHoliday(hcForm.getHolidayCalendarInfo().getId(), holidayWrapper.getTypeKey(), holidayInfo, getContextInfo());
 115  
             }else{
 116  0
                 storedHolidayInfo = getAcalService().updateHoliday(holidayInfo.getId(),holidayInfo, getContextInfo());
 117  
             }
 118  0
             holidayWrapper.setHolidayInfo(storedHolidayInfo);
 119  0
             newHolidayIdList.add(storedHolidayInfo.getId());
 120  
         }
 121  
 
 122  0
         if ( ! StringUtils.isBlank(hcInfo.getId())) { // calendar already exists
 123  
             // remove all old holidays that are not contained in the list of new holidays
 124  0
             List<HolidayInfo> oldHolidayList =
 125  
                     getAcalService().getHolidaysForHolidayCalendar(hcInfo.getId(), getContextInfo());
 126  0
             for (HolidayInfo oldHoliday : oldHolidayList) {
 127  0
                 if ( ! newHolidayIdList.contains(oldHoliday.getId())) {
 128  0
                     getAcalService().deleteHoliday(oldHoliday.getId(), getContextInfo());
 129  
                 }
 130  
             }
 131  
         }
 132  0
     }
 133  
 
 134  
     public HolidayCalendarInfo getHolidayCalendar(String hcId) throws Exception{
 135  0
         HolidayCalendarInfo retrievedHc = getAcalService().getHolidayCalendar(hcId, getContextInfo());
 136  0
         return retrievedHc;
 137  
     }
 138  
 
 139  
     public HolidayCalendarInfo getNewestHolidayCalendar() throws Exception {
 140  0
         int currentYear = Calendar.getInstance().get(Calendar.YEAR);
 141  0
         List<HolidayCalendarInfo> holidayCalendarInfoList =
 142  
                 getAcalService().getHolidayCalendarsByStartYear(currentYear, getContextInfo());
 143  0
         if ((null == holidayCalendarInfoList) || holidayCalendarInfoList.isEmpty()) {
 144  0
             holidayCalendarInfoList =
 145  
                     getAcalService().getHolidayCalendarsByStartYear((currentYear - 1), getContextInfo());
 146  
         }
 147  
 
 148  0
         if ((null == holidayCalendarInfoList) || (holidayCalendarInfoList.size() == 0)) {
 149  0
             return null;
 150  
         }
 151  
         else {
 152  
             //TODO - if > 1 result, find calendar with latest end date?
 153  0
             return holidayCalendarInfoList.get(holidayCalendarInfoList.size() - 1);
 154  
         }
 155  
     }
 156  
 
 157  
     public List<HolidayWrapper> getHolidayWrappersForHolidayCalendar(String holidayCalendarId) throws Exception {
 158  0
         List<HolidayInfo> holidayInfos =
 159  
                 getAcalService().getHolidaysForHolidayCalendar(holidayCalendarId, getContextInfo());
 160  0
         return assembleHolidays(holidayInfos);
 161  
     }
 162  
 
 163  
     private List<HolidayWrapper> assembleHolidays (List<HolidayInfo> holidayInfos) throws Exception{
 164  0
         List<HolidayWrapper> holidays = new ArrayList<HolidayWrapper>();
 165  0
         if (holidayInfos != null && !holidayInfos.isEmpty()){
 166  0
             for (HolidayInfo holidayInfo : holidayInfos) {
 167  0
                 HolidayWrapper holiday = assembleHoliday(holidayInfo);
 168  0
                 holidays.add(holiday);
 169  0
             }
 170  
         }
 171  
 
 172  0
         return holidays;
 173  
     }
 174  
 
 175  
     private HolidayWrapper assembleHoliday(HolidayInfo holidayInfo) throws Exception{
 176  0
         HolidayWrapper holiday = new HolidayWrapper(holidayInfo);
 177  
 //        holiday.setHolidayInfo(holidayInfo);
 178  0
         holiday.setTypeName(getHolidayTypeName(holidayInfo.getTypeKey()));
 179  
 //        CommonUtils.assembleTimeSet(holiday, holidayInfo.getStartDate(), holidayInfo.getEndDate());
 180  
 
 181  0
         return holiday;
 182  
     }
 183  
 
 184  
     public String getHolidayTypeName(String holidayTypeKey) throws Exception {
 185  0
         TypeInfo typeInfo = getAcalService().getHolidayType(holidayTypeKey, getContextInfo());
 186  0
         return typeInfo.getName();
 187  
     }
 188  
 
 189  
     public void deleteHoliday(int selectedIndex,HolidayCalendarForm hcForm) throws Exception{
 190  0
         hcForm.getHolidays().remove(selectedIndex);
 191  0
     }
 192  
 
 193  
     public String getHolidayCalendarState(String holidayCalendarStateKey) throws Exception{
 194  0
         StateInfo hcState = getAcalService().getHolidayCalendarState(holidayCalendarStateKey, getContextInfo());
 195  0
         return hcState.getName();
 196  
     }
 197  
 
 198  
     public void deleteHolidayCalendar(String holidayCalendarId) throws Exception{
 199  
         //delete hc
 200  0
         getAcalService().deleteHolidayCalendar(holidayCalendarId, getContextInfo());
 201  0
     }
 202  
 
 203  
     public void populateHolidayTypes(InputField field, HolidayCalendarForm hcForm){
 204  
 
 205  0
         boolean isAddLine = BooleanUtils.toBoolean((Boolean)field.getContext().get(UifConstants.ContextVariableNames.IS_ADD_LINE));
 206  0
         if (!isAddLine) {
 207  0
             return;
 208  
         }
 209  
 
 210  0
         List<KeyValue> keyValues = new ArrayList<KeyValue>();
 211  0
         List<String> alreadyAddedTypes = new ArrayList<String>();
 212  
 
 213  0
         for (HolidayWrapper holidayWrapper : hcForm.getHolidays()) {
 214  0
              alreadyAddedTypes.add(holidayWrapper.getTypeKey());
 215  
         }
 216  
 
 217  
         //Hard code "Select holiday type"
 218  0
         ConcreteKeyValue topKeyValue = new ConcreteKeyValue();
 219  0
         topKeyValue.setKey("");
 220  0
         topKeyValue.setValue("Select holiday type");
 221  0
         keyValues.add(topKeyValue);
 222  
 
 223  
         try {
 224  0
             List<TypeInfo> types = getHolidayTypes();
 225  0
             for (TypeInfo type : types) {
 226  0
                 if (!alreadyAddedTypes.contains(type.getKey())){
 227  0
                     ConcreteKeyValue keyValue = new ConcreteKeyValue();
 228  0
                     keyValue.setKey(type.getKey());
 229  0
                     keyValue.setValue(type.getName());
 230  0
                     keyValues.add(keyValue);
 231  0
                 }
 232  
             }
 233  0
         } catch (Exception e) {
 234  0
             throw new RuntimeException(e);
 235  0
         }
 236  
 
 237  0
         ((SelectControl) field.getControl()).setOptions(keyValues);
 238  0
     }
 239  
 
 240  
     public AcademicCalendarInfo createAcademicCalendar(AcademicCalendarForm acalForm) throws Exception{
 241  0
         AcademicCalendarInfo acalInfo = acalForm.getAcademicCalendarInfo();
 242  0
         acalInfo.setStateKey(AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_DRAFT_STATE_KEY);
 243  0
         acalInfo.setTypeKey(AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_TYPE_KEY);
 244  0
         RichTextInfo rti = new RichTextInfo();
 245  0
         rti.setPlain(acalInfo.getName());
 246  0
         acalInfo.setDescr(rti);
 247  0
         AcademicCalendarInfo newAcal = getAcalService().createAcademicCalendar(AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_TYPE_KEY, acalInfo, getContextInfo());
 248  0
         return newAcal;
 249  
     }
 250  
 
 251  
     public void deleteAcademicCalendar(String academicCalendarId) throws Exception {
 252  0
         getAcalService().deleteAcademicCalendar(academicCalendarId, getContextInfo());
 253  0
     }
 254  
 
 255  
     public AcademicCalendarInfo getLatestAcademicCalendar() throws Exception {
 256  0
         int currentYear = Calendar.getInstance().get(Calendar.YEAR);
 257  0
         List<AcademicCalendarInfo> academicCalendarInfoList =
 258  
                 getAcalService().getAcademicCalendarsByStartYear(currentYear, getContextInfo());
 259  0
         if ((null == academicCalendarInfoList) || academicCalendarInfoList.isEmpty()) {
 260  0
             academicCalendarInfoList =
 261  
                     getAcalService().getAcademicCalendarsByStartYear((currentYear - 1), getContextInfo());
 262  
         }
 263  
 
 264  0
         if ((null == academicCalendarInfoList) || (academicCalendarInfoList.size() == 0)) {
 265  0
             return null;
 266  
         }
 267  
         else {
 268  
             //TODO - if > 1 result, find calendar with latest end date?
 269  0
             return academicCalendarInfoList.get(academicCalendarInfoList.size() - 1);
 270  
         }
 271  
     }
 272  
 
 273  
     public void copyToCreateAcademicCalendar(AcademicCalendarForm form) throws Exception {
 274  
 
 275  0
            AcademicCalendarInfo orgAcalInfo = form.getOrgAcalInfo();
 276  
 
 277  0
            if (orgAcalInfo == null || StringUtils.isBlank(orgAcalInfo.getId())){
 278  0
                throw new RuntimeException("ACal Info doesn't exists to copy.");
 279  
            }
 280  
 
 281  
            // 1. copy over events
 282  0
            List<AcalEventInfo> orgEventInfoList= getAcalService().getAcalEventsForAcademicCalendar(orgAcalInfo.getId(), getContextInfo());
 283  0
            List<AcalEventWrapper> newEventList = new ArrayList<AcalEventWrapper>();
 284  0
            for (AcalEventInfo orgEventInfo : orgEventInfoList){
 285  0
                AcalEventWrapper newEvent= new AcalEventWrapper(orgEventInfo,true);
 286  
                try {
 287  0
                    TypeInfo type = getTypeService().getType(orgEventInfo.getTypeKey(), getContextInfo());
 288  0
                    newEvent.setEventTypeName(type.getName());
 289  0
                }catch (Exception e){
 290  
                    //TODO
 291  0
                }
 292  0
                newEventList.add(newEvent);
 293  0
            }
 294  0
            form.setEvents(newEventList);
 295  
 
 296  
           // 2. copy over terms
 297  0
           List<AcademicTermWrapper> newTermList = populateTermWrappers(orgAcalInfo.getId(), true);
 298  0
           form.setTermWrapperList(newTermList);
 299  
 
 300  0
     }
 301  
 
 302  
     public List<AcalEventWrapper> populateEventWrappers(AcademicCalendarForm acalForm) throws Exception {
 303  
 
 304  0
         AcademicCalendarInfo acalInfo = acalForm.getAcademicCalendarInfo();
 305  0
         List<AcalEventInfo> eventInfos = getAcalService().getAcalEventsForAcademicCalendar(acalInfo.getId(), getContextInfo());
 306  0
         List<AcalEventWrapper> events = new ArrayList<AcalEventWrapper>();
 307  
 
 308  0
         for (AcalEventInfo eventInfo: eventInfos) {
 309  0
             AcalEventWrapper event  = new AcalEventWrapper(eventInfo,false);
 310  0
             TypeInfo type = getTypeService().getType(event.getEventTypeKey(), getContextInfo());
 311  0
             event.setEventTypeName(type.getName());
 312  0
             events.add(event);
 313  0
         }
 314  0
         return events;
 315  
     }
 316  
 
 317  
     public List<HolidayCalendarWrapper> loadHolidayCalendars(AcademicCalendarInfo acalInfo) throws Exception {
 318  0
         List<HolidayCalendarWrapper> holidayCalendarWrapperList = new ArrayList<HolidayCalendarWrapper>();
 319  0
         List<String> hcIds = acalInfo.getHolidayCalendarIds();
 320  0
         if (hcIds != null && !hcIds.isEmpty()){
 321  0
             for (String hcId : hcIds){
 322  0
                 HolidayCalendarWrapper holidayCalendarWrapper =  getHolidayCalendarWrapper (hcId);
 323  0
                 holidayCalendarWrapperList.add(holidayCalendarWrapper);
 324  0
             }
 325  
         }
 326  0
         return holidayCalendarWrapperList;        
 327  
     }
 328  
     
 329  
     private HolidayCalendarWrapper getHolidayCalendarWrapper(String hcId){
 330  0
         ContextInfo context = getContextInfo();
 331  
 
 332  0
         HolidayCalendarWrapper holidayCalendarWrapper = new HolidayCalendarWrapper();
 333  0
         List<HolidayWrapper> holidays = new ArrayList<HolidayWrapper>();
 334  
         try{
 335  
             //need to retrieve HolidayCalendarInfo and all Holidays to form the HolidayCalendarWrapper.
 336  0
             HolidayCalendarInfo holidayCalendarInfo = getAcalService().getHolidayCalendar(hcId, context);
 337  0
             holidayCalendarWrapper.setHolidayCalendarInfo(holidayCalendarInfo);
 338  0
             holidayCalendarWrapper.setId(holidayCalendarInfo.getId());
 339  0
             holidayCalendarWrapper.setAdminOrgName(getAdminOrgNameById(holidayCalendarInfo.getAdminOrgId()));
 340  0
             StateInfo hcState = getAcalService().getHolidayCalendarState(holidayCalendarInfo.getStateKey(), context);
 341  0
             holidayCalendarWrapper.setStateName(hcState.getName());
 342  
 
 343  0
                 List<HolidayInfo> holidayInfoList = getAcalService().getHolidaysForHolidayCalendar(holidayCalendarInfo.getId(), context);
 344  0
                 for(HolidayInfo holidayInfo : holidayInfoList){
 345  0
                     HolidayWrapper holiday = new HolidayWrapper(holidayInfo);
 346  0
                     TypeInfo typeInfo = getAcalService().getHolidayType(holidayInfo.getTypeKey(), context);
 347  0
                     holiday.setTypeName(typeInfo.getName());
 348  0
                     holidays.add(holiday);
 349  0
                 }
 350  0
                 holidayCalendarWrapper.setHolidays(holidays);
 351  0
         }catch (Exception e){
 352  0
             throw new RuntimeException(e);
 353  0
         }
 354  
 
 355  0
         return holidayCalendarWrapper;
 356  
 
 357  
     }
 358  
 
 359  
 
 360  
     public AcalEventWrapper createEvent(String acalId, AcalEventWrapper event) throws Exception{
 361  0
         AcalEventInfo eventInfo = assembleEventInfoFromWrapper(event);
 362  0
         AcalEventInfo createdEventInfo = getAcalService().createAcalEvent(acalId, eventInfo.getTypeKey(), eventInfo, getContextInfo());
 363  0
         event.setAcalEventInfo(createdEventInfo);
 364  0
         return event;
 365  
     }
 366  
 
 367  
     public AcalEventWrapper updateEvent(String eventId, AcalEventWrapper event) throws Exception {
 368  0
         AcalEventInfo eventInfo = assembleEventInfoFromWrapper(event);
 369  0
         getAcalService().updateAcalEvent(eventId, eventInfo, getContextInfo());
 370  0
         AcalEventInfo updatedEventInfo = getAcalService().getAcalEvent(eventId, getContextInfo());
 371  0
         event.setAcalEventInfo(updatedEventInfo);
 372  0
         return event;
 373  
     }
 374  
 
 375  
     public void deleteEvent(String eventId) throws Exception {
 376  0
         getAcalService().deleteAcalEvent(eventId, getContextInfo());
 377  0
     }
 378  
 
 379  
     private AcalEventInfo assembleEventInfoFromWrapper(AcalEventWrapper eventWrapper) throws Exception{
 380  0
         AcalEventInfo eventInfo = eventWrapper.getAcalEventInfo();
 381  
 
 382  0
         RichTextInfo rti = new RichTextInfo();
 383  0
         rti.setPlain(eventWrapper.getEventTypeKey());
 384  0
         eventInfo.setDescr(rti);
 385  0
         eventInfo.setStateKey(AtpServiceConstants.MILESTONE_DRAFT_STATE_KEY);
 386  0
         eventInfo.setTypeKey(eventWrapper.getEventTypeKey());
 387  0
         eventInfo.setStartDate(eventWrapper.getStartDate());
 388  0
         eventInfo.setIsAllDay(eventWrapper.isAllDay());
 389  0
         eventInfo.setStartDate(getStartDateWithUpdatedTime(eventWrapper,true));
 390  0
         setEventEndDate(eventWrapper);
 391  0
         return eventInfo;
 392  
     }
 393  
 
 394  
     protected boolean performAddLineValidation(View view, CollectionGroup collectionGroup, Object model, Object addLine) {
 395  
 
 396  0
         boolean isValid = super.performAddLineValidation(view, collectionGroup, model, addLine);
 397  
 
 398  0
         if (model instanceof AcademicCalendarForm){
 399  0
             AcademicCalendarForm form = (AcademicCalendarForm)model;
 400  
 
 401  0
             if (addLine instanceof HolidayCalendarWrapper){
 402  0
                 for(HolidayCalendarWrapper holidayCalendarWrapper : form.getHolidayCalendarList()){
 403  0
                     if (StringUtils.equals(holidayCalendarWrapper.getId(),((HolidayCalendarWrapper) addLine).getId())){
 404  0
                         GlobalVariables.getMessageMap().putError("newCollectionLines['holidayCalendarList'].id",CalendarConstants.ERROR_DUPLICATE_HCAL,holidayCalendarWrapper.getHolidayCalendarInfo().getName());
 405  0
                         return false;
 406  
                     }
 407  
                 }
 408  0
             } else if (addLine instanceof KeyDateWrapper) {
 409  0
                 KeyDateWrapper keydate = (KeyDateWrapper)addLine;
 410  0
                 if(StringUtils.isEmpty(keydate.getKeyDateType())) {
 411  0
                     GlobalVariables.getMessageMap().putErrorForSectionId( "acal-term-keydates", CalendarConstants.MSG_ERROR_KEY_DATE_TYPE_REQUIRED);
 412  0
                     return false;
 413  
                 }
 414  
             }
 415  
         }
 416  
 
 417  0
         return isValid;
 418  
     }
 419  
 
 420  
     public void populateKeyDateTypes(InputField field, AcademicCalendarForm acalForm) {
 421  
 
 422  0
         boolean isAddLine = BooleanUtils.toBoolean((Boolean)field.getContext().get(UifConstants.ContextVariableNames.IS_ADD_LINE));
 423  0
         if (!isAddLine) {
 424  0
             return;
 425  
         }
 426  
 
 427  0
         List<KeyValue> keyValues = new ArrayList<KeyValue>();
 428  0
         keyValues.add(new ConcreteKeyValue("", "Select Keydate Type"));
 429  
 
 430  0
         CollectionGroup collectionGroup = (CollectionGroup)field.getContext().get(UifConstants.ContextVariableNames.PARENT);
 431  0
         KeyDatesGroupWrapper groupWrapper = ObjectPropertyUtils.getPropertyValue(acalForm,collectionGroup.getBindingInfo().getBindByNamePrefix());
 432  
 
 433  0
         if (StringUtils.isNotBlank(groupWrapper.getKeyDateGroupType())){
 434  
             try {
 435  0
                 List<TypeInfo> types = getTypesForGroupType(groupWrapper.getKeyDateGroupType());
 436  0
                 for (TypeInfo type : types) {
 437  0
                     if (!groupWrapper.isKeyDateExists(type.getKey())){
 438  0
                         keyValues.add(new ConcreteKeyValue(type.getKey(), type.getName()));
 439  
                     }
 440  
                 }
 441  0
             } catch (Exception e) {
 442  0
                 throw new RuntimeException(e);
 443  0
             }
 444  
         }
 445  
 
 446  0
         ((SelectControl) field.getControl()).setOptions(keyValues);
 447  
 
 448  0
     }
 449  
 
 450  
     public void populateKeyDateGroupTypes(InputField field, AcademicCalendarForm acalForm) {
 451  
 
 452  0
         boolean isAddLine = BooleanUtils.toBoolean((Boolean)field.getContext().get(UifConstants.ContextVariableNames.IS_ADD_LINE));
 453  0
         if (!isAddLine) {
 454  0
             return;
 455  
         }
 456  
 
 457  0
         List<KeyValue> keyValues = new ArrayList<KeyValue>();
 458  0
         keyValues.add(new ConcreteKeyValue("", "Select Keydate Group Type"));
 459  
 
 460  0
         CollectionGroup collectionGroup = (CollectionGroup)field.getContext().get(UifConstants.ContextVariableNames.COLLECTION_GROUP);
 461  0
         AcademicTermWrapper termWrapper = ObjectPropertyUtils.getPropertyValue(acalForm,collectionGroup.getBindingInfo().getBindByNamePrefix());
 462  
 
 463  
         try {
 464  0
             List<TypeInfo> keyDateGroupTypes = getAcalService().getKeyDateTypesForTermType(termWrapper.getTermType(),getContextInfo());
 465  0
             for (TypeInfo keyDateGroupType : keyDateGroupTypes) {
 466  0
                 if (!termWrapper.isKeyDateGroupExists(keyDateGroupType.getKey())){
 467  0
                     keyValues.add(new ConcreteKeyValue(keyDateGroupType.getKey(),keyDateGroupType.getName()));
 468  
                 }
 469  
             }
 470  0
         } catch (Exception e) {
 471  0
             throw new RuntimeException(e);
 472  0
         }
 473  
 
 474  0
         ((SelectControl) field.getControl()).setOptions(keyValues);
 475  
 
 476  0
     }
 477  
 
 478  
     public void validateHolidayCalendar(HolidayCalendarForm hcForm){
 479  0
         HolidayCalendarInfo hcInfo = hcForm.getHolidayCalendarInfo();
 480  
 
 481  0
         if (!isValidHcalName(hcInfo)){
 482  0
             GlobalVariables.getMessageMap().putError("holidayCalendarInfo.name", "error.enroll.calendar.duplicateName");
 483  
         }
 484  
 
 485  0
         if (!CommonUtils.isValidDateRange(hcInfo.getStartDate(),hcInfo.getEndDate())){
 486  0
             GlobalVariables.getMessageMap().putErrorForSectionId("KS-HolidayCalendar-MetaSection",
 487  
                     "error.enroll.daterange.invalid", "Calendar",
 488  
                     CommonUtils.formatDate(hcInfo.getStartDate()), CommonUtils.formatDate(hcInfo.getEndDate()));
 489  
         }
 490  
 
 491  
         //Validate Events
 492  0
         for (HolidayWrapper holiday : hcForm.getHolidays()) {
 493  0
             if (!CommonUtils.isDateWithinRange(hcInfo.getStartDate(),hcInfo.getEndDate(),holiday.getStartDate()) ||
 494  
                 !CommonUtils.isDateWithinRange(hcInfo.getStartDate(),hcInfo.getEndDate(),holiday.getEndDate())){
 495  0
                 GlobalVariables.getMessageMap().putWarningForSectionId("KS-HolidayCalendar-HolidaySection",
 496  
                         "error.enroll.holiday.dateNotInHcal", holiday.getTypeName());
 497  
             }
 498  
 
 499  
             // NOTE: next 2 edits not needed if KRAD validation is working properly
 500  
             // KRAD 2.0 bug where endDate not filled but gets prior value anyway; gets past this edit
 501  0
             if (holiday.isDateRange() && (null == holiday.getEndDate())) {
 502  0
                 GlobalVariables.getMessageMap().putErrorForSectionId( "KS-HolidayCalendar-HolidaySection",
 503  
                         CalendarConstants.MSG_ERROR_DATE_END_REQUIRED, holiday.getTypeName());
 504  
             }
 505  0
             if (!holiday.isAllDay()) { // time fields are enabled and can be filled in
 506  0
                 if ( (!StringUtils.isEmpty(holiday.getStartTime()) && StringUtils.isEmpty(holiday.getStartTimeAmPm()))
 507  
                 ||   (!StringUtils.isEmpty(holiday.getEndTime()) && StringUtils.isEmpty(holiday.getEndTimeAmPm())) ) {
 508  0
                     GlobalVariables.getMessageMap().putError( "holidays",
 509  
                             CalendarConstants.MSG_ERROR_TIME_AMPM_REQUIRED, holiday.getTypeName());
 510  
                 }
 511  
             }
 512  
         }
 513  
 
 514  0
     }
 515  
 
 516  
     public void populateHolidayCalendarDefaults(HolidayCalendarForm hcForm){
 517  
 
 518  0
         for (HolidayWrapper holidayWrapper : hcForm.getHolidays()) {
 519  0
             holidayWrapper.getHolidayInfo().setStartDate(getStartDateWithUpdatedTime(holidayWrapper,false));
 520  0
             setHolidayEndDate(holidayWrapper);
 521  
         }
 522  0
     }
 523  
 
 524  
     public void populateAcademicCalendarDefaults(AcademicCalendarForm acalForm){
 525  
 
 526  0
         for (AcalEventWrapper eventWrapper : acalForm.getEvents()) {
 527  0
             eventWrapper.getAcalEventInfo().setStartDate(getStartDateWithUpdatedTime(eventWrapper,false));
 528  0
             setEventEndDate(eventWrapper);
 529  
         }
 530  
 
 531  0
         for (AcademicTermWrapper academicTermWrapper : acalForm.getTermWrapperList()) {
 532  0
             for (KeyDatesGroupWrapper keyDatesGroupWrapper : academicTermWrapper.getKeyDatesGroupWrappers()){
 533  0
                 for(KeyDateWrapper keyDateWrapper : keyDatesGroupWrapper.getKeydates()){
 534  0
                     keyDateWrapper.getKeyDateInfo().setStartDate(getStartDateWithUpdatedTime(keyDateWrapper,false));
 535  0
                     setKeyDateEndDate(keyDateWrapper);
 536  
                 }
 537  
             }
 538  
         }
 539  0
     }
 540  
 
 541  
     public void validateAcademicCalendar(AcademicCalendarForm acalForm){
 542  
 
 543  0
         AcademicCalendarInfo acal = acalForm.getAcademicCalendarInfo();
 544  
 
 545  
         //Validate Acal Name for duplication
 546  0
         if (!isValidAcalName(acalForm.getAcademicCalendarInfo())){
 547  0
             GlobalVariables.getMessageMap().putError("academicCalendarInfo.name", "error.enroll.calendar.duplicateName");
 548  
         }
 549  
 
 550  0
         if (!CommonUtils.isValidDateRange(acal.getStartDate(),acal.getEndDate())){
 551  0
             GlobalVariables.getMessageMap().putErrorForSectionId("KS-AcademicCalendar-MetaSection", "error.enroll.daterange.invalid","Calendar",CommonUtils.formatDate(acal.getStartDate()),CommonUtils.formatDate(acal.getEndDate()));
 552  
         }
 553  
 
 554  
         //Validate Events
 555  0
         for (AcalEventWrapper eventWrapper : acalForm.getEvents()) {
 556  0
             if (!CommonUtils.isDateWithinRange(acal.getStartDate(),acal.getEndDate(),eventWrapper.getStartDate()) ||
 557  
                 !CommonUtils.isDateWithinRange(acal.getStartDate(),acal.getEndDate(),eventWrapper.getEndDate())){
 558  0
                 GlobalVariables.getMessageMap().putWarningForSectionId("acal-info-event", "error.enroll.event.dateNotInAcal",eventWrapper.getEventTypeName());
 559  
             }
 560  0
             if (eventWrapper.isDateRange() && !CommonUtils.isValidDateRange(eventWrapper.getStartDate(),eventWrapper.getEndDate())){
 561  0
                 GlobalVariables.getMessageMap().putWarning("acal-info-event", "error.enroll.daterange.invalid",eventWrapper.getEventTypeName(),CommonUtils.formatDate(eventWrapper.getStartDate()),CommonUtils.formatDate(eventWrapper.getEndDate()));
 562  
             }
 563  
         }
 564  
 
 565  
         //Validate Terms and keydates
 566  0
         validateTerms(acalForm.getTermWrapperList(),acal);
 567  
 
 568  0
     }
 569  
 
 570  
     private boolean isValidAcalName(AcademicCalendarInfo acal){
 571  
 
 572  0
         QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
 573  0
         List<Predicate> pList = new ArrayList<Predicate>();
 574  
 
 575  0
         Predicate p = equal("atpType",AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_TYPE_KEY);
 576  0
         pList.add(p);
 577  
 
 578  0
         p = equalIgnoreCase("name", acal.getName());
 579  0
         pList.add(p);
 580  
 
 581  0
         Predicate[] preds = new Predicate[pList.size()];
 582  0
         pList.toArray(preds);
 583  0
         qBuilder.setPredicates(and(preds));
 584  
 
 585  
         try {
 586  0
             List<AcademicCalendarInfo> acals = getAcalService().searchForAcademicCalendars(qBuilder.build(),getContextInfo());
 587  0
             boolean valid = acals.isEmpty();
 588  
             //Make sure it's not the same Acal which is being edited by the user
 589  0
             if (!valid && StringUtils.isNotBlank(acal.getId())){
 590  0
                 for (AcademicCalendarInfo academicCalendarInfo : acals) {
 591  0
                     if (!StringUtils.equals(academicCalendarInfo.getId(),acal.getId())){
 592  0
                         valid = false;
 593  0
                         break;
 594  
                     }
 595  0
                     valid = true;
 596  
                 }
 597  
             }
 598  
 
 599  0
             return valid;
 600  0
         } catch (Exception e) {
 601  0
             throw new RuntimeException(e);
 602  
         }
 603  
     }
 604  
 
 605  
     private boolean isValidHcalName(HolidayCalendarInfo hcal){
 606  
 
 607  0
         QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
 608  0
         List<Predicate> pList = new ArrayList<Predicate>();
 609  0
         Predicate p = equal("atpType",AcademicCalendarServiceConstants.HOLIDAY_CALENDAR_TYPE_KEY);
 610  0
         pList.add(p);
 611  
 
 612  0
         p = equalIgnoreCase("name", hcal.getName());
 613  0
         pList.add(p);
 614  
 
 615  0
         Predicate[] preds = new Predicate[pList.size()];
 616  0
         pList.toArray(preds);
 617  0
         qBuilder.setPredicates(and(preds));
 618  
 
 619  
         try {
 620  0
             List<HolidayCalendarInfo> hcals = getAcalService().searchForHolidayCalendars(qBuilder.build(), getContextInfo());
 621  0
             boolean valid = hcals.isEmpty();
 622  
             //Make sure it's not the same Hcal which is being edited by the user
 623  0
             if (!valid && StringUtils.isNotBlank(hcal.getId())){
 624  0
                 for (HolidayCalendarInfo hc : hcals) {
 625  0
                     if (!StringUtils.equals(hc.getId(),hcal.getId())){
 626  0
                         valid = false;
 627  0
                         break;
 628  
                     }
 629  0
                     valid = true;
 630  
                 }
 631  
             }
 632  0
             return valid;
 633  0
         } catch (Exception e) {
 634  0
             throw new RuntimeException(e);
 635  
         }
 636  
     }
 637  
 
 638  
     private void validateTerms(List<AcademicTermWrapper> termWrapper,AcademicCalendarInfo acal) {
 639  0
         for (int index=0; index < termWrapper.size(); index++) {
 640  0
             validateTerm(termWrapper,index,acal);
 641  
         }
 642  0
     }
 643  
 
 644  
     public void validateTerm(List<AcademicTermWrapper> termWrapper,int termToValidateIndex,AcademicCalendarInfo acal) {
 645  
 
 646  0
         AcademicTermWrapper termWrapperToValidate = termWrapper.get(termToValidateIndex);
 647  
 
 648  0
         int index2 = 0;
 649  
         //Validate duplicate term name
 650  0
         for (AcademicTermWrapper wrapper : termWrapper) {
 651  0
             index2++;
 652  0
             if (wrapper != termWrapperToValidate){
 653  0
                 if (StringUtils.equalsIgnoreCase(wrapper.getName(),termWrapperToValidate.getName())){
 654  0
                     GlobalVariables.getMessageMap().putErrorForSectionId("acal-term", "error.enroll.term.duplicateName",""+ NumberUtils.min(new int[]{termToValidateIndex,index2}),""+NumberUtils.max(new int[]{termToValidateIndex,index2}));
 655  
                 }
 656  
             }
 657  
         }
 658  
 
 659  0
         if (!CommonUtils.isValidDateRange(termWrapperToValidate.getStartDate(),termWrapperToValidate.getEndDate())){
 660  0
             GlobalVariables.getMessageMap().putErrorForSectionId("acal-term", "error.enroll.daterange.invalid",termWrapperToValidate.getName(),CommonUtils.formatDate(termWrapperToValidate.getStartDate()),CommonUtils.formatDate(termWrapperToValidate.getEndDate()));
 661  
         }
 662  
 
 663  0
         if (!CommonUtils.isDateWithinRange(acal.getStartDate(),acal.getEndDate(),termWrapperToValidate.getStartDate()) ||
 664  
             !CommonUtils.isDateWithinRange(acal.getStartDate(),acal.getEndDate(),termWrapperToValidate.getEndDate())){
 665  0
             GlobalVariables.getMessageMap().putWarningForSectionId("acal-term", "error.enroll.term.dateNotInAcal",termWrapperToValidate.getName());
 666  
         }
 667  
 
 668  0
         for (KeyDatesGroupWrapper keyDatesGroupWrapper : termWrapperToValidate.getKeyDatesGroupWrappers()){
 669  0
             for(KeyDateWrapper keyDateWrapper : keyDatesGroupWrapper.getKeydates()){
 670  
 
 671  0
                 if (keyDateWrapper.isDateRange() && !CommonUtils.isValidDateRange(keyDateWrapper.getStartDate(),keyDateWrapper.getEndDate())){
 672  0
                     GlobalVariables.getMessageMap().putWarningForSectionId("acal-term", "error.enroll.daterange.invalid",keyDateWrapper.getKeyDateNameUI(),CommonUtils.formatDate(keyDateWrapper.getStartDate()),CommonUtils.formatDate(keyDateWrapper.getEndDate()));
 673  
                 }
 674  
 
 675  0
                 if (!CommonUtils.isDateWithinRange(termWrapperToValidate.getStartDate(),termWrapperToValidate.getEndDate(),keyDateWrapper.getStartDate()) ||
 676  
                     !CommonUtils.isDateWithinRange(termWrapperToValidate.getStartDate(),termWrapperToValidate.getEndDate(),keyDateWrapper.getEndDate())){
 677  0
                     GlobalVariables.getMessageMap().putWarningForSectionId("acal-term-keydates", "error.enroll.keydate.dateNotInTerm",keyDateWrapper.getKeyDateNameUI(),termWrapperToValidate.getName());
 678  
                 }
 679  
             }
 680  
         }
 681  
 
 682  0
     }
 683  
 
 684  
     public void populateInstructionalDays(List<AcademicTermWrapper> termWrapperList)
 685  
     throws Exception {
 686  0
          for (AcademicTermWrapper termWrapper : termWrapperList) {
 687  0
              populateInstructionalDays(termWrapper);
 688  
         }
 689  0
     }
 690  
 
 691  
     public void populateInstructionalDays(AcademicTermWrapper termWrapper)
 692  
     throws Exception {
 693  0
         if (termWrapper.getKeyDatesGroupWrappers() != null){
 694  0
             for (KeyDatesGroupWrapper keyDatesGroupWrapper : termWrapper.getKeyDatesGroupWrappers()) {
 695  0
                  if (keyDatesGroupWrapper.getKeydates() != null){
 696  0
                      for (KeyDateWrapper keydate : keyDatesGroupWrapper.getKeydates()) {
 697  0
                          if (StringUtils.equals(keydate.getKeyDateType(),AtpServiceConstants.MILESTONE_INSTRUCTIONAL_PERIOD_TYPE_KEY) &&
 698  
                              termWrapper.getTermInfo() != null && StringUtils.isNotBlank(termWrapper.getTermInfo().getId())){
 699  0
                              int instructionalDays = getAcalService().getInstructionalDaysForTerm(termWrapper.getTermInfo().getId(),getContextInfo());
 700  0
                              termWrapper.setInstructionalDays(instructionalDays);
 701  0
                              break;
 702  
                          }
 703  
                      }
 704  
                  }
 705  
 
 706  
             }
 707  
         }
 708  0
     }
 709  
 
 710  
     public void saveTerm(AcademicTermWrapper termWrapper, String acalId,boolean isOfficial) throws Exception {
 711  
 
 712  0
         TermInfo term = termWrapper.getTermInfo();
 713  
 
 714  0
         term.setEndDate(termWrapper.getEndDate());
 715  0
         term.setStartDate(termWrapper.getStartDate());
 716  0
         term.setName(termWrapper.getName());
 717  0
         term.setTypeKey(termWrapper.getTermType());
 718  
 
 719  0
         if (isOfficial){
 720  0
            termWrapper.getTermInfo().setStateKey(AtpServiceConstants.ATP_OFFICIAL_STATE_KEY);
 721  
         }
 722  
 
 723  0
         if (termWrapper.isNew()){
 724  0
             TermInfo newTerm = getAcalService().createTerm(termWrapper.getTermType(),term,getContextInfo());
 725  0
             termWrapper.setTermInfo(getAcalService().getTerm(newTerm.getId(),getContextInfo()));
 726  0
             getAcalService().addTermToAcademicCalendar(acalId,termWrapper.getTermInfo().getId(),getContextInfo());
 727  0
         }else{
 728  0
             TermInfo updatedTerm = getAcalService().updateTerm(term.getId(),term,getContextInfo());
 729  0
             termWrapper.setTermInfo(getAcalService().getTerm(updatedTerm.getId(),getContextInfo()));
 730  
         }
 731  
 
 732  0
         for (KeyDateWrapper keyDateWrapper : termWrapper.getKeyDatesToDeleteOnSave()){
 733  0
             getAcalService().deleteKeyDate(keyDateWrapper.getKeyDateInfo().getId(),getContextInfo());
 734  
         }
 735  
 
 736  
         //Keydates
 737  0
         if (termWrapper.getKeyDatesGroupWrappers() != null){
 738  0
             for (KeyDatesGroupWrapper groupWrapper : termWrapper.getKeyDatesGroupWrappers()){
 739  0
                 for (KeyDateWrapper keyDateWrapper : groupWrapper.getKeydates()) {
 740  
 
 741  0
                     if (isOfficial){
 742  0
                         keyDateWrapper.getKeyDateInfo().setStateKey(AtpServiceConstants.MILESTONE_OFFICIAL_STATE_KEY);
 743  
                     }
 744  
 
 745  0
                     KeyDateInfo keyDate = keyDateWrapper.getKeyDateInfo();
 746  
 
 747  0
                     keyDate.setTypeKey(keyDateWrapper.getKeyDateType());
 748  
                     //Add by Bonnie
 749  0
                     keyDate.setName(keyDateWrapper.getKeyDateNameUI());
 750  0
                     keyDate.setStartDate(keyDateWrapper.getStartDate());
 751  0
                     keyDate.setEndDate(keyDateWrapper.getEndDate());
 752  0
                     keyDate.setIsAllDay(keyDateWrapper.isAllDay());
 753  0
                     keyDate.setStartDate(getStartDateWithUpdatedTime(keyDateWrapper,true));
 754  0
                     setKeyDateEndDate(keyDateWrapper);
 755  
 
 756  0
                     if (keyDateWrapper.isNew()){
 757  0
                         KeyDateInfo newKeyDate = getAcalService().createKeyDate(termWrapper.getTermInfo().getId(),keyDate.getTypeKey(),keyDate,getContextInfo());
 758  0
                         keyDateWrapper.setKeyDateInfo(getAcalService().getKeyDate(newKeyDate.getId(),getContextInfo()));
 759  0
                     } else {
 760  0
                         KeyDateInfo updatedKeyDate = getAcalService().updateKeyDate(keyDate.getId(), keyDate, getContextInfo());
 761  0
                         keyDateWrapper.setKeyDateInfo(getAcalService().getKeyDate(updatedKeyDate.getId(),getContextInfo()));
 762  
                     }
 763  0
                 }
 764  
             }
 765  
         }
 766  
 
 767  0
     }
 768  
 
 769  
     private Date getStartDateWithUpdatedTime(TimeSetWrapper timeSetWrapper,boolean isSaveAction){
 770  
         //If start time not blank, set that with the date. If it's empty, just update with default
 771  0
         if (!timeSetWrapper.isAllDay()){
 772  0
             if (StringUtils.isNotBlank(timeSetWrapper.getStartTime())){
 773  0
                 String startTime = timeSetWrapper.getStartTime();
 774  0
                 String startTimeApPm = timeSetWrapper.getStartTimeAmPm();
 775  
                 //On save to DB, have to replace 12AM to 00AM insead of DB considers as 12PM
 776  0
                 if (isSaveAction && StringUtils.startsWith(startTime,"12:") && StringUtils.equalsIgnoreCase(startTimeApPm,"am")){
 777  0
                     startTime = StringUtils.replace(startTime,"12:","00:");
 778  
                 }
 779  0
                 return updateTime(timeSetWrapper.getStartDate(),startTime,startTimeApPm);
 780  
             }else{
 781  0
                 timeSetWrapper.setStartTime("12:00");
 782  0
                 timeSetWrapper.setStartTimeAmPm("AM");
 783  0
                 return updateTime(timeSetWrapper.getStartDate(),timeSetWrapper.getStartTime(),timeSetWrapper.getStartTimeAmPm());
 784  
             }
 785  
         }else{
 786  0
             return timeSetWrapper.getStartDate();
 787  
         }
 788  
 
 789  
     }
 790  
 
 791  
     private void setHolidayEndDate(HolidayWrapper holidayWrapper){
 792  0
         if (!holidayWrapper.isAllDay()){
 793  0
              String endTime = holidayWrapper.getEndTime();
 794  0
              String endTimeApPm = holidayWrapper.getEndTimeAmPm();
 795  0
              Date endDate = holidayWrapper.getEndDate();
 796  
 
 797  
             //If it's not date range, then set
 798  0
              if (!holidayWrapper.isDateRange()){
 799  0
                   endDate = holidayWrapper.getStartDate();
 800  0
                   holidayWrapper.setEndDate(null);
 801  
              }
 802  
 
 803  0
              if (StringUtils.isBlank(endTime)){
 804  0
                 endTime = CalendarConstants.DEFAULT_END_TIME;
 805  0
                 endTimeApPm = "PM";
 806  0
                 holidayWrapper.setEndTime(endTime);
 807  0
                 holidayWrapper.setEndTimeAmPm(endTimeApPm);
 808  
             }
 809  
 
 810  0
             Date endDateToInfo = updateTime(endDate,endTime,endTimeApPm);
 811  0
             holidayWrapper.getHolidayInfo().setEndDate(endDateToInfo);
 812  0
             holidayWrapper.getHolidayInfo().setIsDateRange(true);
 813  0
         }else{
 814  
             Date endDateToInfo;
 815  0
             if (holidayWrapper.isDateRange()) {
 816  
                 //just clearing out any time already set in end date
 817  0
                 endDateToInfo = updateTime(holidayWrapper.getEndDate(),"00:00",StringUtils.EMPTY );
 818  0
                 holidayWrapper.getHolidayInfo().setIsDateRange(true);
 819  
             }else{
 820  0
                 endDateToInfo =  null;
 821  0
                 holidayWrapper.getHolidayInfo().setIsDateRange(false);
 822  0
                 holidayWrapper.setEndDate(null);
 823  
             }
 824  0
             holidayWrapper.getHolidayInfo().setEndDate(endDateToInfo);
 825  
         }
 826  0
     }
 827  
 
 828  
     private void setEventEndDate(AcalEventWrapper eventWrapper){
 829  0
         if (!eventWrapper.isAllDay()){
 830  0
              String endTime = eventWrapper.getEndTime();
 831  0
              String endTimeApPm = eventWrapper.getEndTimeAmPm();
 832  0
              Date endDate = eventWrapper.getEndDate();
 833  
 
 834  
             //If it's not date range, then set
 835  0
              if (!eventWrapper.isDateRange()){
 836  0
                   endDate = eventWrapper.getStartDate();
 837  0
                   eventWrapper.setEndDate(null);
 838  
              }
 839  
 
 840  0
              if (StringUtils.isBlank(endTime)){
 841  0
                 endTime = CalendarConstants.DEFAULT_END_TIME;
 842  0
                 endTimeApPm = "PM";
 843  0
                 eventWrapper.setEndTime(endTime);
 844  0
                 eventWrapper.setEndTimeAmPm(endTimeApPm);
 845  
             }
 846  
 
 847  0
             Date endDateToInfo = updateTime(endDate,endTime,endTimeApPm);
 848  0
             eventWrapper.getAcalEventInfo().setEndDate(endDateToInfo);
 849  0
             eventWrapper.getAcalEventInfo().setIsDateRange(true);
 850  0
         }else{
 851  
             Date endDateToInfo;
 852  0
             if (eventWrapper.isDateRange()) {
 853  
                 //just clearing out any time already set in end date
 854  0
                 endDateToInfo = updateTime(eventWrapper.getEndDate(),"00:00",StringUtils.EMPTY );
 855  0
                 eventWrapper.getAcalEventInfo().setIsDateRange(true);
 856  
             }else{
 857  0
                 endDateToInfo =  null;
 858  0
                 eventWrapper.getAcalEventInfo().setIsDateRange(false);
 859  0
                 eventWrapper.setEndDate(null);
 860  
             }
 861  0
             eventWrapper.getAcalEventInfo().setEndDate(endDateToInfo);
 862  
         }
 863  0
     }
 864  
 
 865  
     private void setKeyDateEndDate(KeyDateWrapper keyDateWrapper){
 866  0
         if (!keyDateWrapper.isAllDay()){
 867  0
              String endTime = keyDateWrapper.getEndTime();
 868  0
              String endTimeApPm = keyDateWrapper.getEndTimeAmPm();
 869  0
              Date endDate = keyDateWrapper.getEndDate();
 870  
 
 871  
             //If it's not date range, then set
 872  0
              if (!keyDateWrapper.isDateRange()){
 873  0
                   endDate = keyDateWrapper.getStartDate();
 874  0
                   keyDateWrapper.setEndDate(null);
 875  
              }
 876  
 
 877  0
              if (StringUtils.isBlank(endTime)){
 878  0
                 endTime = CalendarConstants.DEFAULT_END_TIME;
 879  0
                 endTimeApPm = "PM";
 880  0
                 keyDateWrapper.setEndTime(endTime);
 881  0
                 keyDateWrapper.setEndTimeAmPm(endTimeApPm);
 882  
             }
 883  
 
 884  0
             Date endDateToInfo = updateTime(endDate,endTime,endTimeApPm);
 885  0
             keyDateWrapper.getKeyDateInfo().setEndDate(endDateToInfo);
 886  0
             keyDateWrapper.getKeyDateInfo().setIsDateRange(true);
 887  0
         }else{
 888  
             Date endDateToInfo;
 889  0
             if (keyDateWrapper.isDateRange()) {
 890  
                 //just clearing out any time already set in end date
 891  0
                 endDateToInfo = updateTime(keyDateWrapper.getEndDate(),"00:00",StringUtils.EMPTY );
 892  0
                 keyDateWrapper.getKeyDateInfo().setIsDateRange(true);
 893  
             }else{
 894  0
                 endDateToInfo =  null;
 895  0
                 keyDateWrapper.getKeyDateInfo().setIsDateRange(false);
 896  0
                 keyDateWrapper.setEndDate(null);
 897  
             }
 898  0
             keyDateWrapper.getKeyDateInfo().setEndDate(endDateToInfo);
 899  
         }
 900  0
     }
 901  
 
 902  
     private Date updateTime(Date date,String time,String amPm) {
 903  
 
 904  
         // Get Calendar object set to the date and time of the given Date object
 905  0
         Calendar cal = Calendar.getInstance();
 906  0
         cal.setTime(date);
 907  
 
 908  
         // Set time fields to zero
 909  0
         if (StringUtils.isNotBlank(time)){
 910  0
             cal.set(Calendar.HOUR, Integer.parseInt(StringUtils.substringBefore(time,":")));
 911  0
             cal.set(Calendar.MINUTE, Integer.parseInt(StringUtils.substringAfter(time,":")));
 912  0
             cal.set(Calendar.SECOND, 0);
 913  0
             cal.set(Calendar.MILLISECOND, 0);
 914  0
             if (StringUtils.isNotBlank(amPm)){
 915  0
                 if (StringUtils.equalsIgnoreCase(amPm,"am")){
 916  0
                     cal.set(Calendar.AM_PM,Calendar.AM);
 917  
                 }else {
 918  0
                     cal.set(Calendar.AM_PM,Calendar.PM);
 919  
                 }
 920  
             }
 921  
         }
 922  
 
 923  0
         return cal.getTime();
 924  
     }
 925  
 
 926  
     public void deleteTerm(List<AcademicTermWrapper> termWrapperList,int selectedIndex, String acalId) throws Exception{
 927  0
         AcademicTermWrapper termWrapper = termWrapperList.get(selectedIndex);
 928  0
         if (StringUtils.isNotBlank(termWrapper.getTermInfo().getId())){
 929  0
             if (termWrapper.getKeyDatesGroupWrappers() != null){
 930  0
                 for (KeyDatesGroupWrapper groupWrapper : termWrapper.getKeyDatesGroupWrappers()){
 931  0
                     for (KeyDateWrapper keyDateWrapper : groupWrapper.getKeydates()) {
 932  0
                         if (StringUtils.isNotBlank(keyDateWrapper.getKeyDateInfo().getId())){
 933  0
                             getAcalService().deleteKeyDate(keyDateWrapper.getKeyDateInfo().getId(),getContextInfo());
 934  
                         }
 935  
                     }
 936  
                 }
 937  
             }
 938  0
             getAcalService().deleteTerm(termWrapper.getTermInfo().getId(), getContextInfo());
 939  
         }
 940  0
         termWrapperList.remove(selectedIndex);
 941  0
     }
 942  
 
 943  
     public void deleteKeyDateGroup(AcademicTermWrapper termWrapper,int selectedIndex) throws Exception {
 944  0
         KeyDatesGroupWrapper keydateGroup = termWrapper.getKeyDatesGroupWrappers().get(selectedIndex);
 945  0
         if (keydateGroup != null){
 946  0
             for (int index = 0; index < keydateGroup.getKeydates().size();index++) {
 947  0
                 deleteKeyDate(keydateGroup,index++);
 948  
             }
 949  0
             termWrapper.getKeyDatesGroupWrappers().remove(keydateGroup);
 950  
         }
 951  
 
 952  0
     }
 953  
 
 954  
     public void deleteKeyDate(KeyDatesGroupWrapper keyDatesGroup,int selectedIndex) throws Exception{
 955  0
         KeyDateWrapper keydate = keyDatesGroup.getKeydates().get(selectedIndex);
 956  0
         if (StringUtils.isNotBlank(keydate.getKeyDateInfo().getId())){
 957  0
             getAcalService().deleteKeyDate(keydate.getKeyDateInfo().getId(),getContextInfo());
 958  
         }
 959  0
         keyDatesGroup.getKeydates().remove(selectedIndex);
 960  0
     }
 961  
 
 962  
     protected void processBeforeAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) {
 963  0
         if (addLine instanceof AcademicTermWrapper){
 964  0
             AcademicTermWrapper newLine = (AcademicTermWrapper)addLine;
 965  
             try {
 966  0
                 TypeInfo termType = getAcalService().getTermType(((AcademicTermWrapper) addLine).getTermType(),getContextInfo());
 967  
 
 968  0
                 newLine.setTermNameForUI(termType.getName());
 969  0
                 SimpleDateFormat simpleDateformat=new SimpleDateFormat("yyyy");
 970  0
                 newLine.setName(termType.getName() + " " + simpleDateformat.format(newLine.getStartDate()));
 971  0
                 newLine.setTypeInfo(termType);
 972  0
             } catch (Exception e) {
 973  0
                 throw new RuntimeException(e);
 974  0
             }
 975  0
         }else if (addLine instanceof KeyDatesGroupWrapper){
 976  0
             KeyDatesGroupWrapper group = (KeyDatesGroupWrapper)addLine;
 977  
             try {
 978  0
                 TypeInfo termType = getTypeService().getType(group.getKeyDateGroupType(),getContextInfo());
 979  0
                 group.setKeyDateGroupNameUI(termType.getName());
 980  0
                 group.setTypeInfo(termType);
 981  0
             } catch (Exception e) {
 982  0
                 throw new RuntimeException(e);
 983  0
             }
 984  0
         }else if (addLine instanceof HolidayCalendarInfo) {
 985  0
             HolidayCalendarInfo inputLine = (HolidayCalendarInfo)addLine;
 986  
             try {
 987  0
                 System.out.println("HC id =" +inputLine.getId());
 988  
 
 989  0
                 HolidayCalendarInfo exists = getAcalService().getHolidayCalendar(inputLine.getId(), getContextInfo());
 990  
 
 991  0
                 inputLine.setName(exists.getName());
 992  0
                 inputLine.setId(exists.getId());
 993  0
                 inputLine.setTypeKey(exists.getTypeKey());
 994  0
                 inputLine.setAdminOrgId(exists.getAdminOrgId());
 995  0
                 inputLine.setStartDate(exists.getStartDate());
 996  0
                 inputLine.setEndDate(exists.getEndDate());
 997  0
             }catch (Exception e) {
 998  0
                 throw new RuntimeException(e);
 999  0
             }
 1000  
 
 1001  0
         }else if (addLine instanceof HolidayCalendarWrapper){
 1002  0
             HolidayCalendarWrapper inputLine = (HolidayCalendarWrapper)addLine;
 1003  0
             List<HolidayWrapper> holidays = new ArrayList<HolidayWrapper>();
 1004  
             try {
 1005  0
                 HolidayCalendarInfo hcInfo = getAcalService().getHolidayCalendar(inputLine.getId(), getContextInfo());
 1006  0
                 inputLine.setHolidayCalendarInfo(hcInfo);
 1007  0
                 inputLine.setAdminOrgName(getAdminOrgNameById(hcInfo.getAdminOrgId()));
 1008  0
                 StateInfo hcState = getAcalService().getHolidayCalendarState(hcInfo.getStateKey(), getContextInfo());
 1009  0
                 inputLine.setStateName(hcState.getName());
 1010  0
                 List<HolidayInfo> holidayInfoList = getAcalService().getHolidaysForHolidayCalendar(hcInfo.getId(), getContextInfo());
 1011  0
                 for(HolidayInfo holidayInfo : holidayInfoList){
 1012  0
                     HolidayWrapper holiday = new HolidayWrapper(holidayInfo);
 1013  0
                     TypeInfo typeInfo = getAcalService().getHolidayType(holidayInfo.getTypeKey(), getContextInfo());
 1014  0
                     holiday.setTypeName(typeInfo.getName());
 1015  0
                     holidays.add(holiday);
 1016  0
                 }
 1017  0
                 inputLine.setHolidays(holidays);
 1018  0
             }catch (Exception e){
 1019  0
                 throw new RuntimeException(e);
 1020  0
             }
 1021  
 
 1022  0
         }else if (addLine instanceof AcalEventWrapper){
 1023  0
             AcalEventWrapper acalEventWrapper = (AcalEventWrapper)addLine;
 1024  
             try {
 1025  0
                 TypeInfo type = getTypeService().getType(acalEventWrapper.getEventTypeKey(), getContextInfo());
 1026  0
                 acalEventWrapper.setEventTypeName(type.getName());
 1027  0
             }catch (Exception e) {
 1028  0
                 throw new RuntimeException(e);
 1029  0
             }
 1030  0
             if (!CommonUtils.isValidDateRange(acalEventWrapper.getStartDate(),acalEventWrapper.getEndDate())){
 1031  0
                GlobalVariables.getMessageMap().putWarningForSectionId("acal-info-event","error.enroll.daterange.invalid",acalEventWrapper.getEventTypeName(),CommonUtils.formatDate(acalEventWrapper.getStartDate()),CommonUtils.formatDate(acalEventWrapper.getEndDate()));
 1032  
             }
 1033  0
         }else if (addLine instanceof KeyDateWrapper){
 1034  0
             KeyDateWrapper keydate = (KeyDateWrapper)addLine;
 1035  
             try {
 1036  0
                 if(!StringUtils.isEmpty(keydate.getKeyDateType())) {
 1037  0
                     TypeInfo type = getTypeService().getType(keydate.getKeyDateType(),getContextInfo());
 1038  0
                     keydate.setKeyDateNameUI(type.getName());
 1039  0
                     keydate.setTypeInfo(type);
 1040  0
                     if (!CommonUtils.isValidDateRange(keydate.getStartDate(),keydate.getEndDate())){
 1041  0
                         GlobalVariables.getMessageMap().putWarningForSectionId("acal-term-keydates", "error.enroll.daterange.invalid",keydate.getKeyDateNameUI(),CommonUtils.formatDate(keydate.getStartDate()),CommonUtils.formatDate(keydate.getEndDate()));
 1042  
                     }
 1043  
                 }
 1044  0
             } catch (Exception e) {
 1045  0
                 throw new RuntimeException(e);
 1046  0
             }
 1047  0
         } else if (addLine instanceof HolidayWrapper){
 1048  0
             HolidayWrapper holiday = (HolidayWrapper)addLine;
 1049  
             try {
 1050  0
                 holiday.setTypeName(getHolidayTypeName(holiday.getTypeKey()));
 1051  0
             } catch (Exception e) {
 1052  0
                 throw new RuntimeException(e);
 1053  0
             }
 1054  0
             if (!CommonUtils.isValidDateRange(holiday.getStartDate(),holiday.getEndDate())){
 1055  0
                 GlobalVariables.getMessageMap().putWarningForSectionId("KS-HolidayCalendar-HolidaySection", "error.enroll.daterange.invalid",holiday.getTypeName(),CommonUtils.formatDate(holiday.getStartDate()),CommonUtils.formatDate(holiday.getEndDate()));
 1056  
             }
 1057  0
         } else {
 1058  0
             super.processBeforeAddLine(view, collectionGroup, model, addLine);
 1059  
         }
 1060  0
     }
 1061  
 
 1062  
     public List<AcademicTermWrapper> populateTermWrappers(String acalId, boolean isCopy){
 1063  
 
 1064  0
         List<AcademicTermWrapper> termWrappers = new ArrayList<AcademicTermWrapper>();
 1065  
 
 1066  
         try {
 1067  0
             List<TermInfo> termInfos = getAcalService().getTermsForAcademicCalendar(acalId, getContextInfo());
 1068  0
             for (TermInfo termInfo : termInfos) {
 1069  0
                 AcademicTermWrapper termWrapper = populateTermWrapper(termInfo, isCopy);
 1070  0
                 termWrappers.add(termWrapper);
 1071  0
             }
 1072  0
         } catch (Exception e) {
 1073  0
             throw new RuntimeException(e);
 1074  0
         }
 1075  
 
 1076  0
         return termWrappers;
 1077  
     }
 1078  
 
 1079  
     public AcademicTermWrapper populateTermWrapper(TermInfo termInfo, boolean isCopy){
 1080  
         try{
 1081  
 
 1082  0
             TypeInfo type = getAcalService().getTermType(termInfo.getTypeKey(),getContextInfo());
 1083  
 
 1084  0
             AcademicTermWrapper termWrapper = new AcademicTermWrapper(termInfo,isCopy);
 1085  0
             termWrapper.setTypeInfo(type);
 1086  0
             termWrapper.setTermNameForUI(type.getName());
 1087  0
             if (isCopy){
 1088  0
                 termWrapper.setName(type.getName());
 1089  
             }
 1090  
 
 1091  
             //Populate keydates
 1092  0
             List<KeyDateInfo> keydateList = getAcalService().getKeyDatesForTerm(termInfo.getId(),getContextInfo());
 1093  0
             List<TypeInfo> keyDateTypes = getTypeService().getAllowedTypesForType(termInfo.getTypeKey(),getContextInfo());
 1094  
 
 1095  0
             Map<String,KeyDatesGroupWrapper> keyDateGroup = new HashMap<String,KeyDatesGroupWrapper>();
 1096  
 
 1097  0
             for (KeyDateInfo keyDateInfo : keydateList) {
 1098  0
                 KeyDateWrapper keyDateWrapper = new KeyDateWrapper(keyDateInfo,isCopy);
 1099  0
                 type = getTypeService().getType(keyDateInfo.getTypeKey(),getContextInfo());
 1100  0
                 keyDateWrapper.setTypeInfo(type);
 1101  0
                 keyDateWrapper.setKeyDateNameUI(type.getName());
 1102  
 
 1103  0
                 addKeyDateGroup(keyDateTypes,keyDateWrapper,keyDateGroup);
 1104  0
             }
 1105  
 
 1106  0
             for (KeyDatesGroupWrapper group : keyDateGroup.values()) {
 1107  0
                 if (!group.getKeydates().isEmpty()){
 1108  0
                     termWrapper.getKeyDatesGroupWrappers().add(group);
 1109  
                 }
 1110  
             }
 1111  
 
 1112  0
             return termWrapper;
 1113  0
         }catch(Exception e){
 1114  0
             throw new RuntimeException(e);
 1115  
         }
 1116  
     }
 1117  
 
 1118  
     private void addKeyDateGroup(List<TypeInfo> keyDateTypes,KeyDateWrapper keyDateWrapper,Map<String,KeyDatesGroupWrapper> keyDateGroup){
 1119  0
         for (TypeInfo keyDateType : keyDateTypes) {
 1120  
             try {
 1121  
                 // TODO
 1122  0
                 List<TypeInfo> allowedTypes = getTypesForGroupType(keyDateType.getKey());
 1123  0
                 for (TypeInfo allowedType : allowedTypes) {
 1124  0
                     if (StringUtils.equals(allowedType.getKey(),keyDateWrapper.getKeyDateType())){
 1125  0
                         KeyDatesGroupWrapper keyDatesGroup = keyDateGroup.get(keyDateType.getKey());
 1126  0
                         if (keyDatesGroup == null){
 1127  0
                             keyDatesGroup = new KeyDatesGroupWrapper(keyDateType.getKey(),keyDateType.getName());
 1128  0
                             keyDateGroup.put(keyDateType.getKey(),keyDatesGroup);
 1129  
                         }
 1130  0
                         keyDatesGroup.getKeydates().add(keyDateWrapper);
 1131  0
                         break;
 1132  
                     }
 1133  
                 }
 1134  0
             } catch (Exception e) {
 1135  0
                 throw new RuntimeException(e);
 1136  0
             }
 1137  
         }
 1138  0
     }
 1139  
 
 1140  
     public AcademicCalendarService getAcalService() {
 1141  0
            if(acalService == null) {
 1142  0
              acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
 1143  
         }
 1144  0
         return this.acalService;
 1145  
     }
 1146  
 
 1147  
     public TypeService getTypeService() {
 1148  0
            if(typeService == null) {
 1149  0
              typeService = (TypeService) GlobalResourceLoader.getService(new QName(TypeServiceConstants.NAMESPACE, TypeServiceConstants.SERVICE_NAME_LOCAL_PART));
 1150  
         }
 1151  0
         return this.typeService;
 1152  
     }
 1153  
 
 1154  
     public ContextInfo getContextInfo() {
 1155  0
         if (null == contextInfo) {
 1156  0
             contextInfo = new ContextInfo();
 1157  0
             contextInfo.setAuthenticatedPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
 1158  0
             contextInfo.setPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
 1159  0
             LocaleInfo localeInfo = new LocaleInfo();
 1160  0
             localeInfo.setLocaleLanguage(Locale.getDefault().getLanguage());
 1161  0
             localeInfo.setLocaleRegion(Locale.getDefault().getCountry());
 1162  0
             contextInfo.setLocale(localeInfo);
 1163  
         }
 1164  0
         return contextInfo;
 1165  
     }
 1166  
 
 1167  
     private String getAdminOrgNameById(String id){
 1168  
         //TODO: hard-coded for now, going to call OrgService
 1169  0
         String adminOrgName = null;
 1170  0
         Map<String, String> allHcOrgs = new HashMap<String, String>();
 1171  0
         allHcOrgs.put("102", "Registrar's Office");
 1172  
 
 1173  0
         if(allHcOrgs.containsKey(id)){
 1174  0
             adminOrgName = allHcOrgs.get(id);
 1175  
         }
 1176  
 
 1177  0
         return adminOrgName;
 1178  
     }
 1179  
 
 1180  
     public List<TypeInfo> getHolidayTypes() throws InvalidParameterException, MissingParameterException, PermissionDeniedException, OperationFailedException {
 1181  0
         if(holidayTypes == null) {
 1182  0
             holidayTypes = Collections.unmodifiableList(getAcalService().getHolidayTypes(getContextInfo()));
 1183  
         }
 1184  0
         return holidayTypes;
 1185  
     }
 1186  
 
 1187  
     public List<TypeInfo> getTypesForGroupType(String groupTypeKey) throws InvalidParameterException, MissingParameterException, DoesNotExistException, PermissionDeniedException, OperationFailedException {
 1188  
 
 1189  0
         List<TypeInfo> types = typesByGroupTypeMap.get(groupTypeKey);
 1190  0
         if(types == null) {
 1191  0
             types = Collections.unmodifiableList(getTypeService().getTypesForGroupType(groupTypeKey, getContextInfo()));
 1192  0
             typesByGroupTypeMap.put(groupTypeKey, types);
 1193  
         }
 1194  0
         return types;
 1195  
     }
 1196  
 }