📄 techdataservices.java
字号:
* @param dateFrom the start date * @param amount the amount of millisecond to move forward * @return the dateTo */ public static Timestamp addForward(GenericValue techDataCalendar, Timestamp dateFrom, long amount) { Timestamp dateTo = (Timestamp) dateFrom.clone(); long nextCapacity = capacityRemaining(techDataCalendar, dateFrom); if (amount <= nextCapacity){ dateTo.setTime(dateTo.getTime()+amount); amount = 0; }else amount -= nextCapacity; Map result = new HashMap(); while (amount > 0) { result = startNextDay(techDataCalendar, dateTo); dateTo = (Timestamp) result.get("dateTo"); nextCapacity = ((Double) result.get("nextCapacity")).longValue(); if (amount <= nextCapacity){ dateTo.setTime(dateTo.getTime()+amount); amount = 0; }else amount -= nextCapacity; } return dateTo; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** Used to find the last day in the TechDataCalendarWeek where capacity != 0, ending at dayEnd, dayEnd included. * * @param techDataCalendarWeek The TechDataCalendarWeek cover * @param dayEnd * @return a map with the capacity (Double) available, the startTime and moveDay (int): the number of day it's necessary to move to have capacity available */ public static Map dayEndCapacityAvailable(GenericValue techDataCalendarWeek, int dayEnd) { Map result = new HashMap(); int moveDay = 0; Double capacity = null; Time startTime = null; while (capacity == null || capacity.doubleValue() == 0) { switch( dayEnd){ case Calendar.MONDAY: capacity = techDataCalendarWeek.getDouble("mondayCapacity"); startTime = techDataCalendarWeek.getTime("mondayStartTime"); break; case Calendar.TUESDAY: capacity = techDataCalendarWeek.getDouble("tuesdayCapacity"); startTime = techDataCalendarWeek.getTime("tuesdayStartTime"); break; case Calendar.WEDNESDAY: capacity = techDataCalendarWeek.getDouble("wednesdayCapacity"); startTime = techDataCalendarWeek.getTime("wednesdayStartTime"); break; case Calendar.THURSDAY: capacity = techDataCalendarWeek.getDouble("thursdayCapacity"); startTime = techDataCalendarWeek.getTime("thursdayStartTime"); break; case Calendar.FRIDAY: capacity = techDataCalendarWeek.getDouble("fridayCapacity"); startTime = techDataCalendarWeek.getTime("fridayStartTime"); break; case Calendar.SATURDAY: capacity = techDataCalendarWeek.getDouble("saturdayCapacity"); startTime = techDataCalendarWeek.getTime("saturdayStartTime"); break; case Calendar.SUNDAY: capacity = techDataCalendarWeek.getDouble("sundayCapacity"); startTime = techDataCalendarWeek.getTime("sundayStartTime"); break; } if (capacity == null || capacity.doubleValue() == 0) { moveDay -=1; dayEnd = (dayEnd==1) ? 7 : dayEnd - 1; } // Debug.logInfo("capacity loop: " + capacity+ " moveDay=" +moveDay, module); } result.put("capacity",capacity); result.put("startTime",startTime); result.put("moveDay",new Integer(moveDay)); return result; } /** Used to request the remaining capacity available for dateFrom in a TechDataCalenda, * If the dateFrom (param in) is not in an available TechDataCalendar period, the return value is zero. * * @param techDataCalendar The TechDataCalendar cover * @param dateFrom the date * @return long capacityRemaining */ public static long capacityRemainingBackward(GenericValue techDataCalendar, Timestamp dateFrom) { GenericValue techDataCalendarWeek = null; // TODO read TechDataCalendarExcWeek to manage exception week (maybe it's needed to refactor the entity definition try{ techDataCalendarWeek = techDataCalendar.getRelatedOneCache("TechDataCalendarWeek"); } catch (GenericEntityException e) { Debug.logError("Pb reading Calendar Week associated with calendar"+e.getMessage(), module); // return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PbReadingTechDataCalendarWeekAssociated", locale)); return 0; } // TODO read TechDataCalendarExcDay to manage execption day Calendar cDateTrav = Calendar.getInstance(); cDateTrav.setTime((Date) dateFrom); Map position = dayEndCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK)); int moveDay = ((Integer) position.get("moveDay")).intValue(); if (moveDay != 0) return 0; Time startTime = (Time) position.get("startTime"); Double capacity = (Double) position.get("capacity"); //TODO after test (01:00:00).getTime() = 0 and not 3600000 so currently we add this value to be correct but it's needed to find why (maybe GMT pb) Timestamp startAvailablePeriod = new Timestamp(UtilDateTime.getDayStart(dateFrom).getTime() + startTime.getTime() + 3600000); if (dateFrom.before(startAvailablePeriod) ) return 0; Timestamp endAvailablePeriod = new Timestamp(startAvailablePeriod.getTime()+capacity.longValue()); if (dateFrom.after(endAvailablePeriod)) return 0; return dateFrom.getTime() - startAvailablePeriod.getTime(); } /** Used to move in a TechDataCalenda, produce the Timestamp for the end of the previous day available and its associated capacity. * If the dateFrom (param in) is not in an available TechDataCalendar period, the return value is the previous day available * * @param techDataCalendar The TechDataCalendar cover * @param dateFrom the date * @return a map with Timestamp dateTo, Double previousCapacity */ public static Map endPreviousDay(GenericValue techDataCalendar, Timestamp dateFrom) { Map result = new HashMap(); Timestamp dateTo = null; GenericValue techDataCalendarWeek = null; // TODO read TechDataCalendarExcWeek to manage exception week (maybe it's needed to refactor the entity definition try{ techDataCalendarWeek = techDataCalendar.getRelatedOneCache("TechDataCalendarWeek"); } catch (GenericEntityException e) { Debug.logError("Pb reading Calendar Week associated with calendar"+e.getMessage(), module); // return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PbReadingTechDataCalendarWeekAssociated", locale)); return ServiceUtil.returnError("Pb reading Calendar Week associated with calendar"); } // TODO read TechDataCalendarExcDay to manage execption day Calendar cDateTrav = Calendar.getInstance(); cDateTrav.setTime((Date) dateFrom); Map position = dayEndCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK)); Time startTime = (Time) position.get("startTime"); int moveDay = ((Integer) position.get("moveDay")).intValue(); Double capacity = (Double) position.get("capacity"); dateTo = (moveDay == 0) ? dateFrom : UtilDateTime.getDayEnd(dateFrom,moveDay); //TODO after test (01:00:00).getTime() = 0 and not 3600000 so currently we add this value to be correct but it's needed to find why (maybe GMT pb) Timestamp endAvailablePeriod = new Timestamp(UtilDateTime.getDayStart(dateTo).getTime() + startTime.getTime() + 3600000 + capacity.longValue()); if (dateTo.after(endAvailablePeriod) ) { dateTo = endAvailablePeriod; } else { dateTo = UtilDateTime.getDayStart(dateTo, -1); cDateTrav.setTime((Date) dateTo); position = dayEndCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK)); startTime = (Time) position.get("startTime"); moveDay = ((Integer) position.get("moveDay")).intValue(); capacity = (Double) position.get("capacity"); if (moveDay != 0) dateTo = UtilDateTime.getDayStart(dateTo,moveDay); dateTo.setTime(dateTo.getTime() + startTime.getTime() + 3600000 + capacity.longValue()); } result.put("dateTo",dateTo); result.put("previousCapacity",position.get("capacity")); return result; } /** Used to move backward in a TechDataCalenda, start from the dateFrom and move backward only on available period. * If the dateFrom (param in) is not a available TechDataCalendar period, the startDate is the end of the previous day available * * @param techDataCalendar The TechDataCalendar cover * @param dateFrom the start date * @param amount the amount of millisecond to move backward * @return the dateTo */ public static Timestamp addBackward(GenericValue techDataCalendar, Timestamp dateFrom, long amount) { Timestamp dateTo = (Timestamp) dateFrom.clone(); long previousCapacity = capacityRemainingBackward(techDataCalendar, dateFrom); if (amount <= previousCapacity){ dateTo.setTime(dateTo.getTime()-amount); amount = 0; }else amount -= previousCapacity; Map result = new HashMap(); while (amount > 0) { result = endPreviousDay(techDataCalendar, dateTo); dateTo = (Timestamp) result.get("dateTo"); previousCapacity = ((Double) result.get("previousCapacity")).longValue(); if (amount <= previousCapacity){ dateTo.setTime(dateTo.getTime()-amount); amount = 0; }else amount -= previousCapacity; } return dateTo; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -