List of usage examples for org.joda.time LocalDateTime toLocalDate
public LocalDate toLocalDate()
From source file:energy.usef.core.repository.MessageRepository.java
License:Apache License
/** * Checks whether each outbound {@link CommonReferenceQuery} message has a related inbound {@link * CommonReferenceQueryResponse} for the given period. * * @param period {@link LocalDate} period. * @return <code>true</code> if every query has a response. *//*from ww w.j av a 2 s. c o m*/ public boolean hasEveryCommonReferenceQuerySentAResponseReceived(LocalDateTime period) { StringBuilder sql = new StringBuilder(); sql.append("SELECT queries FROM Message queries WHERE queries.conversationId NOT IN ("); sql.append(" SELECT responses.conversationId FROM Message responses "); sql.append(" WHERE responses.xml LIKE :responseType "); sql.append(" AND YEAR(responses.creationTime) = :year "); sql.append(" AND MONTH(responses.creationTime) = :month "); sql.append(" AND DAY(responses.creationTime) = :day "); sql.append(" )"); sql.append(" AND queries.xml LIKE :queryType "); sql.append(" AND YEAR(queries.creationTime) = :year "); sql.append(" AND MONTH(queries.creationTime) = :month "); sql.append(" AND DAY(queries.creationTime) = :day "); List<Message> messages = getEntityManager().createQuery(sql.toString(), Message.class) .setParameter("year", period.toLocalDate().getYear()) .setParameter("month", period.toLocalDate().getMonthOfYear()) .setParameter("day", period.toLocalDate().getDayOfMonth()) .setParameter("queryType", "%" + COMMON_REFERENCE_QUERY + "%") .setParameter("responseType", "%" + COMMON_REFERENCE_QUERY_RESPONSE + "%").getResultList(); return messages.isEmpty(); }
From source file:energy.usef.core.service.business.CorePlanboardBusinessService.java
License:Apache License
/** * Intializes ptu's for the current day and index. *//*ww w . ja v a2s . c o m*/ @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void initialisePtuContainers() { LocalDateTime timestamp = DateTimeUtil.getCurrentDateTime(); LocalDate period = timestamp.toLocalDate(); int ptuIndex = PtuUtil.getPtuIndex(timestamp, config.getIntegerProperty(ConfigParam.PTU_DURATION)); LOGGER.error("Initialising PTUs at startup, current PTU is {} {}", period, ptuIndex); int updated = ptuContainerRepository.initialisePtuContainers(period, ptuIndex); LOGGER.debug("Updated {} PTUs", updated); }
From source file:energy.usef.core.service.business.PrognosisConsolidationBusinessService.java
License:Apache License
private List<PtuPrognosis> filterPtuPrognosisForIntradayGateClosureValidity(List<PtuPrognosis> ptuPrognosisList, LocalDateTime planboardMessageCreationTime) { List<PtuPrognosis> filteredPrognoses = new ArrayList<>(); LocalDate prognosisPeriod = ptuPrognosisList.get(0).getPtuContainer().getPtuDate(); Integer gateClosurePtus = config.getIntegerProperty(ConfigParam.INTRADAY_GATE_CLOSURE_PTUS); Integer ptuDuration = config.getIntegerProperty(ConfigParam.PTU_DURATION); LocalDateTime creationTimePlusGateClosure = planboardMessageCreationTime .plusMinutes(gateClosurePtus * ptuDuration); Integer pivotIndex = PtuUtil.getPtuIndex(creationTimePlusGateClosure, ptuDuration); if (creationTimePlusGateClosure.toLocalDate().isEqual(prognosisPeriod)) { filteredPrognoses = ptuPrognosisList.stream() .filter(ptuPrognosis -> ptuPrognosis.getPtuContainer().getPtuIndex() > pivotIndex) .collect(Collectors.toList()); } else if (creationTimePlusGateClosure.toLocalDate().isBefore(prognosisPeriod)) { filteredPrognoses = ptuPrognosisList.stream().collect(Collectors.toList()); }//from w ww .j a v a2 s . c om return filteredPrognoses; }
From source file:energy.usef.core.service.validation.CorePlanboardValidatorService.java
License:Apache License
/** * Verify whether PTU Container is within IntradayGateClosureTime. * * @param ptuContainer PTU Container// w ww . j av a2s. co m * @return true if the PTU Container is within IntradayGateClosureTime, false otherwise */ public boolean isPtuContainerWithinIntradayGateClosureTime(PtuContainer ptuContainer) { Integer gateClosure = config.getIntegerProperty(ConfigParam.INTRADAY_GATE_CLOSURE_PTUS); Integer ptuDuration = config.getIntegerProperty(ConfigParam.PTU_DURATION); LocalDateTime todayNow = DateTimeUtil.getCurrentDateTime(); Integer ptusToday = PtuUtil.getNumberOfPtusPerDay(todayNow.toLocalDate(), ptuDuration); Integer minutesToday = DateTimeUtil.getElapsedMinutesSinceMidnight(todayNow); Integer ptuIndexPivot = 1 + gateClosure + minutesToday / ptuDuration; LocalDate ptuDatePivot = todayNow.toLocalDate(); if (ptuIndexPivot > ptusToday) { ptuIndexPivot = ptuIndexPivot % ptusToday; ptuDatePivot = ptuDatePivot.plusDays(1); } int ptuDateComparedToDatePivot = new LocalDate(ptuContainer.getPtuDate()).compareTo(ptuDatePivot); if (ptuDateComparedToDatePivot == 1) { // PTU date is the one day after the current limit, no problem return false; } else if (ptuDateComparedToDatePivot == 0 && ptuContainer.getPtuIndex().compareTo(ptuIndexPivot) == 1) { // PTU date is the same day as the limit but the PTU index is after the index limit, no problem return false; } // PTU date is the same day (or before) and the PTU index is the same or before the index limit, problem return true; }
From source file:energy.usef.core.timer.CorePlanboardEventTrigger.java
License:Apache License
private void registerIntraDayClosureEventTimer() { final Integer intraDayClosurePtus = config.getIntegerProperty(ConfigParam.INTRADAY_GATE_CLOSURE_PTUS); final Integer ptuDuration = config.getIntegerProperty(ConfigParam.PTU_DURATION); LocalTime nextPtuShiftTime = getNextPtuShiftTime(); schedulerService.registerScheduledCall("INTRA_DAY_CLOSURE_EVENT", () -> { LocalDateTime intradayClosure = DateTimeUtil.getCurrentDateTime() .plusMinutes(ptuDuration * intraDayClosurePtus); LocalDate period = intradayClosure.toLocalDate(); Integer ptuIndex = PtuUtil.getPtuIndex(intradayClosure, ptuDuration); intraDayClosureEventManager.fire(new IntraDayClosureEvent(period, ptuIndex)); }, DateTimeUtil.millisecondDelayUntilNextTime(nextPtuShiftTime), Minutes.minutes(ptuDuration).toStandardDuration().getMillis()); }
From source file:energy.usef.core.timer.CorePlanboardEventTrigger.java
License:Apache License
private void registerMoveToOperateEventTimer() { final int ptuDuration = config.getIntegerProperty(ConfigParam.PTU_DURATION); LocalTime nextPtuShiftTime = getNextPtuShiftTime(); schedulerService.registerScheduledCall("MOVE_TO_OPERATE_EVENT", () -> { LocalDateTime timestamp = DateTimeUtil.getCurrentDateTime().plusSeconds(PTU_OFFSET_IN_SECONDS); Integer ptuIndex = PtuUtil.getPtuIndex(timestamp, ptuDuration); moveToOperateEventManager.fire(new MoveToOperateEvent(timestamp.toLocalDate(), ptuIndex)); }, DateTimeUtil.millisecondDelayUntilNextTime(nextPtuShiftTime), Minutes.minutes(ptuDuration).toStandardDuration().getMillis()); }
From source file:energy.usef.core.util.DateTimeUtil.java
License:Apache License
/** * Creates a LocalDateTime set to the start of the given date time (date/time at 00:00:00.000) * * @param dateTime - the date/time for which to determine the start of day. * @return LocalDateTime set to the start of the given date. *///from w ww . j a v a 2 s . c o m public static LocalDateTime getStartOfDay(LocalDateTime dateTime) { return getStartOfDay(dateTime.toLocalDate()); }
From source file:energy.usef.core.util.DateTimeUtil.java
License:Apache License
/** * Calculate the number of minutes from midnight to now. The number of minutes depends on summer- and winter time. * * @param dateTime the date time which includes the timezone. When setting the DateTime with the default constructor, the * default timezone where the application runs, is used. * @return the number of minutes from midnight. *//*from ww w . j a va 2 s . c om*/ public static Integer getElapsedMinutesSinceMidnight(LocalDateTime dateTime) { DateTime midnight = dateTime.toLocalDate().toDateMidnight().toDateTime(); Duration duration = new Interval(midnight, dateTime.toDateTime()).toDuration(); return (int) duration.getStandardSeconds() / SECONDS_PER_MINUTE; }
From source file:energy.usef.dso.workflow.operate.DsoOperateCoordinator.java
License:Apache License
/** * This method starts the workflow when triggered by an event. * * @param event {@link SendOperateEvent} event which starts the workflow. *///from w ww . j a v a 2 s .c o m @Asynchronous public void sendOperate(@Observes SendOperateEvent event) throws BusinessException { LOGGER.info(LOG_COORDINATOR_START_HANDLING_EVENT, event); LocalDateTime currentDateTime = DateTimeUtil.getCurrentDateTime(); int currentPtuIndex = PtuUtil.getPtuIndex(currentDateTime, config.getIntegerProperty(ConfigParam.PTU_DURATION)); // Map: Congestion point entity address -> Map: PTU Date -> Flex Offer list Map<String, Map<LocalDate, List<PlanboardMessage>>> offersPerCongestionPointPerDateMap = dsoPlanboardBusinessService .findOrderableFlexOffers(); // Map: Congestion point entity address -> Map: PTU Date -> GridSafetyAnalysis DTO Map<String, Map<LocalDate, GridSafetyAnalysisDto>> gridSafetyAnalysisPerCongestionPointPerDateMap = dsoPlanboardBusinessService .createGridSafetyAnalysisRelatedToFlexOffersDtoMap(); PtuContainer currentPtuContainer = dsoPlanboardBusinessService .findPtuContainer(currentDateTime.toLocalDate(), currentPtuIndex); PtuContainer previousPtuContainer = fetchPreviousPtuContainer(currentDateTime, currentPtuIndex); if (currentPtuContainer == null) { throw new BusinessException(CoreBusinessError.NOT_INITIALIZED_PLANBOARD, currentDateTime); } for (CongestionPointConnectionGroup congestionPoint : dsoPlanboardBusinessService .findActiveCongestionPointConnectionGroup(currentDateTime.toLocalDate())) { PtuState ptuState = dsoPlanboardBusinessService.findOrCreatePtuState(currentPtuContainer, congestionPoint); Optional<Long> currentLimitedPower = dsoPlanboardBusinessService.findLimitedPower(currentPtuContainer, congestionPoint); if (!currentLimitedPower.isPresent() && previousPtuContainer != null) { currentLimitedPower = dsoPlanboardBusinessService.findLimitedPower(previousPtuContainer, congestionPoint); } Long sumOfPower = currentLimitedPower.orElse(0L); if (sumOfPower != 0L) { ptuState.setRegime(RegimeType.ORANGE); } WorkflowContext monitorGridResultContext = invokeMonitorGridPbc(currentDateTime, currentPtuIndex, congestionPoint, sumOfPower); WorkflowUtil.validateContext(DsoWorkflowStep.DSO_MONITOR_GRID.name(), monitorGridResultContext, DsoMonitorGridStepParameter.OUT.values()); long actualLoad = (Long) monitorGridResultContext .getValue(DsoMonitorGridStepParameter.OUT.ACTUAL_LOAD.name()); long maxLoad = (Long) monitorGridResultContext .getValue(DsoMonitorGridStepParameter.OUT.MAX_LOAD.name()); long minLoad = (Long) monitorGridResultContext .getValue(DsoMonitorGridStepParameter.OUT.MIN_LOAD.name()); boolean congestion = (Boolean) monitorGridResultContext .getValue(DsoMonitorGridStepParameter.OUT.CONGESTION.name()); dsoPlanboardBusinessService.setActualPower(currentPtuContainer, actualLoad, congestionPoint); if (congestion) { Long orderedPower = handleCongestion(offersPerCongestionPointPerDateMap, gridSafetyAnalysisPerCongestionPointPerDateMap, congestionPoint); if (isPowerOutsideLoadLimits(actualLoad, orderedPower, maxLoad, minLoad)) { long powerDecrease = invokeLimitedConnectionsPBC(congestionPoint.getUsefIdentifier(), currentPtuContainer.getPtuDate(), currentPtuIndex); dsoPlanboardBusinessService.setLimitedPower(currentPtuContainer, powerDecrease, congestionPoint); ptuState.setRegime(RegimeType.ORANGE); // set next ptu to orange setNextPtuContainerToOrange(currentPtuContainer, congestionPoint); } else { ptuState.setRegime(RegimeType.YELLOW); } } else if (ptuState.getRegime() == RegimeType.ORANGE) { // No congestion any more, so connections can be restored // Invoking Restore Limited Connections PBC invokeRestoreLimitedConnectionsPBC(congestionPoint.getUsefIdentifier(), currentDateTime.toLocalDate(), currentPtuIndex); dsoPlanboardBusinessService.setLimitedPower(currentPtuContainer, null, congestionPoint); ptuState.setRegime(RegimeType.YELLOW); } } LOGGER.info(LOG_COORDINATOR_FINISHED_HANDLING_EVENT, event); }
From source file:energy.usef.dso.workflow.operate.DsoOperateCoordinator.java
License:Apache License
private WorkflowContext invokeMonitorGridPbc(LocalDateTime currentDateTime, int currentPtuIndex, CongestionPointConnectionGroup congestionPoint, Long sumOfPower) { WorkflowContext contextIn = new DefaultWorkflowContext(); int ptuDuration = config.getIntegerProperty(ConfigParam.PTU_DURATION); contextIn.setValue(DsoMonitorGridStepParameter.IN.PTU_DURATION.name(), ptuDuration); contextIn.setValue(DsoMonitorGridStepParameter.IN.CONGESTION_POINT_ENTITY_ADDRESS.name(), congestionPoint.getUsefIdentifier()); contextIn.setValue(DsoMonitorGridStepParameter.IN.LIMITED_POWER.name(), sumOfPower); Long numConnections = dsoPlanboardBusinessService .findConnectionCountByUsefIdentifier(congestionPoint.getUsefIdentifier()); contextIn.setValue(DsoMonitorGridStepParameter.IN.NUM_CONNECTIONS.name(), numConnections); contextIn.setValue(DsoMonitorGridStepParameter.IN.PTU_INDEX.name(), currentPtuIndex); contextIn.setValue(DsoMonitorGridStepParameter.IN.PERIOD.name(), currentDateTime.toLocalDate()); WorkflowContext contextOut = workflowStepExecuter.invoke(DSO_MONITOR_GRID.name(), contextIn); WorkflowUtil.validateContext(DSO_MONITOR_GRID.name(), contextOut, DsoMonitorGridStepParameter.OUT.values()); return contextOut; }