Coverage Report - org.kuali.student.enrollment.class2.appointment.util.AppointmentSlotRuleTypeConversion
 
Classes in this File Line Coverage Branch Coverage Complexity
AppointmentSlotRuleTypeConversion
0%
0/54
0%
0/20
3
 
 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  
  * Created by Adi Rajesh on 4/11/12
 16  
  */
 17  
 package org.kuali.student.enrollment.class2.appointment.util;
 18  
 
 19  
 
 20  
 import org.kuali.student.r2.common.dto.TimeAmountInfo;
 21  
 import org.kuali.student.r2.common.dto.TimeOfDayInfo;
 22  
 import org.kuali.student.r2.core.appointment.dto.AppointmentSlotRuleInfo;
 23  
 
 24  
 import java.util.ArrayList;
 25  
 import java.util.List;
 26  
 
 27  
 /**
 28  
  * This class //TODO ...
 29  
  *
 30  
  * @author Kuali Student Team
 31  
  */
 32  0
 public class AppointmentSlotRuleTypeConversion {
 33  
 
 34  0
     public static int SECOND_IN_MILLIS = 1000;
 35  0
     public static int MINUTE_IN_SECS =  60;
 36  0
     public static int HOUR_IN_MINUTES = 60;
 37  0
     public static String DELIMITER = ";";
 38  
 
 39  
     //Converting appt. rule type code to object
 40  
     // Ex: 1,3,4;{mins};{mins};kuali.type.duration.minutes;15;kuali.type.duration.minutes;0
 41  
     public static AppointmentSlotRuleInfo convToAppointmentSlotRuleInfo(String slotRuleTypeCode){
 42  0
         AppointmentSlotRuleInfo slotRule = new AppointmentSlotRuleInfo();
 43  0
         String[] codeWords = slotRuleTypeCode.split(";");
 44  0
         String weekDaysArray[] = codeWords[0].split(",");
 45  0
         if(weekDaysArray.length >0){
 46  0
             List<Integer> weekdays = new ArrayList<Integer>();
 47  0
             for (String s : weekDaysArray) {
 48  0
                 weekdays.add(Integer.parseInt(s));
 49  
             }
 50  0
             slotRule.setWeekdays(weekdays);
 51  
         }
 52  
         
 53  0
         slotRule.setStartTimeOfDay(convertToTimeOfDayInfo(convertToMilliSecs(codeWords[1])));
 54  0
         slotRule.setEndTimeOfDay(convertToTimeOfDayInfo(convertToMilliSecs(codeWords[2])));
 55  0
         slotRule.setSlotStartInterval(convertToTimeAmountInfo(codeWords[3],Integer.parseInt(codeWords[4])));
 56  0
         slotRule.setSlotDuration(convertToTimeAmountInfo(codeWords[5],Integer.parseInt(codeWords[6])));
 57  0
         return slotRule;
 58  
     }
 59  
 
 60  
     //Converting appt. rule type object to code
 61  
     public static String convTotAppointmentSlotRuleCode(AppointmentSlotRuleInfo slotRuleInfo){
 62  0
         String slotRule = "";
 63  0
         StringBuilder tempSlotRule = new StringBuilder();
 64  
 
 65  0
         if(slotRuleInfo != null){
 66  0
             if(slotRuleInfo.getWeekdays() != null){
 67  
                 // Generate comma delimited days of week to save (max length is 13 characters)
 68  0
                 List<Integer> weekdays = slotRuleInfo.getWeekdays(); // not null
 69  0
                 for (Integer day : weekdays) {
 70  0
                     if (tempSlotRule.length() > 0) {
 71  0
                         tempSlotRule.append(",");
 72  
                     }
 73  0
                     tempSlotRule.append(day);
 74  
                 }
 75  
             }
 76  0
             tempSlotRule.append(DELIMITER + convertToMins(slotRuleInfo.getStartTimeOfDay().getMilliSeconds()));
 77  0
             tempSlotRule.append(DELIMITER + convertToMins(slotRuleInfo.getEndTimeOfDay().getMilliSeconds()));
 78  0
             tempSlotRule.append(DELIMITER + slotRuleInfo.getSlotStartInterval().getAtpDurationTypeKey());
 79  0
             tempSlotRule.append(DELIMITER + slotRuleInfo.getSlotStartInterval().getTimeQuantity());
 80  0
             tempSlotRule.append(DELIMITER + slotRuleInfo.getSlotDuration().getAtpDurationTypeKey());
 81  0
             tempSlotRule.append(DELIMITER + slotRuleInfo.getSlotDuration().getTimeQuantity());
 82  0
             slotRule = tempSlotRule.toString();
 83  
         }
 84  0
         return slotRule;
 85  
     }
 86  
 
 87  
     private static TimeOfDayInfo convertToTimeOfDayInfo(Long time) {
 88  0
         if(time == null){
 89  0
             return null;
 90  
         }
 91  0
         TimeOfDayInfo info = new TimeOfDayInfo();
 92  0
         info.setMilliSeconds(time);
 93  0
         return info;
 94  
     }
 95  
 
 96  
     private static TimeAmountInfo convertToTimeAmountInfo(String typeKey, Integer quantity) {
 97  0
         if ((typeKey == null) && (quantity == null)) {
 98  0
             return null;
 99  
         }
 100  0
         TimeAmountInfo info = new TimeAmountInfo();
 101  0
         info.setAtpDurationTypeKey(typeKey);
 102  0
         if (quantity != null) {
 103  0
             info.setTimeQuantity(quantity);
 104  
         }
 105  0
         return info;
 106  
     }
 107  
     
 108  
     private static Long convertToMilliSecs(String time_in_mins){
 109  0
         Long time_in_millis = 0L;
 110  0
             time_in_millis = Long.parseLong(time_in_mins) *  MINUTE_IN_SECS * SECOND_IN_MILLIS;
 111  
 
 112  0
         return time_in_millis;
 113  
     }
 114  
 
 115  
     private static String convertToMins(Long milliSecs){
 116  0
         String time_in_mins = "";
 117  
 
 118  0
             int temp =  (int)(milliSecs/(SECOND_IN_MILLIS * MINUTE_IN_SECS));
 119  0
             time_in_mins = String.valueOf(temp);
 120  
 
 121  0
         return time_in_mins;
 122  
     }
 123  
 
 124  
 }