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