/** * helper function to create the timer * @param instant the point in time when the code should be executed * @param closure string for job id * @param dataMap job data map, preconfigured with arguments * @return */ private static Timer makeTimer(AbstractInstant instant, String closure, JobDataMap dataMap) { JobKey jobKey = new JobKey(instant.toString() + ": " + closure.toString()); Trigger trigger = newTrigger().startAt(instant.toDate()).build(); Timer timer = new TimerImpl(jobKey, trigger.getKey(), instant); dataMap.put("timer", timer); try { JobDetail job = newJob(TimerExecutionJob.class) .withIdentity(jobKey) .usingJobData(dataMap) .build(); TimerImpl.scheduler.scheduleJob(job, trigger); logger.debug("Scheduled code for execution at {}", instant.toString()); return timer; } catch(SchedulerException e) { logger.error("Failed to schedule code for execution.", e); return null; } }
/** * Retrieves the state of a given <code>item</code> to a certain point in time through a {@link PersistenceService} identified * by the <code>serviceName</code>. * * @param item the item to retrieve the state for * @param the point in time for which the state should be retrieved * @param serviceName the name of the {@link PersistenceService} to use * @return the item state at the given point in time */ static public HistoricItem historicState(Item item, AbstractInstant timestamp, String serviceName) { PersistenceService service = services.get(serviceName); if (service instanceof QueryablePersistenceService) { QueryablePersistenceService qService = (QueryablePersistenceService) service; FilterCriteria filter = new FilterCriteria(); filter.setEndDate(timestamp.toDate()); filter.setItemName(item.getName()); filter.setPageSize(1); filter.setOrdering(Ordering.DESCENDING); Iterable<HistoricItem> result = qService.query(filter); if(result.iterator().hasNext()) { return result.iterator().next(); } else { return null; } } else { logger.warn("There is no queryable persistence service registered with the name '{}'", serviceName); return null; } }
/** * Checks if the state of a given <code>item</code> has changed since a certain point in time. * The {@link PersistenceService} identified by the <code>serviceName</code> is used. * * @param item the item to check for state changes * @param the point in time to start the check * @param serviceName the name of the {@link PersistenceService} to use * @return true, if item state had changed */ static public Boolean changedSince(Item item, AbstractInstant timestamp, String serviceName) { Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceName); Iterator<HistoricItem> it = result.iterator(); HistoricItem itemThen = historicState(item, timestamp); if(itemThen == null) { // Can't get the state at the start time // If we've got results more recent that this, it must have changed return(it.hasNext()); } State state = itemThen.getState(); while(it.hasNext()) { HistoricItem hItem = it.next(); if(state!=null && !hItem.getState().equals(state)) { return true; } state = hItem.getState(); } return false; }
/** * Gets the sum of the state of a given <code>item</code> since a certain point in time. * The {@link PersistenceService} identified by the <code>serviceName</code> is used. * * @param item the item to get the average state value for * @param the point in time to start the check * @param serviceName the name of the {@link PersistenceService} to use * @return the sum state value since the given point in time */ static public DecimalType sumSince(Item item, AbstractInstant timestamp, String serviceName) { Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceName); Iterator<HistoricItem> it = result.iterator(); double sum = 0; while(it.hasNext()) { State state = it.next().getState(); if (state instanceof DecimalType) { sum += ((DecimalType) state).doubleValue(); } } return new DecimalType(sum); }
public static void main(String[] args) { System.out.println("-----------------------------------------------"); System.out.println("-----------AbstractInstant---------------------"); Class cls = AbstractInstant.class; System.out.println("-----------ReadableDateTime--------------------"); cls = ReadableDateTime.class; System.out.println("-----------AbstractDateTime--------------------"); cls = AbstractDateTime.class; System.out.println("-----------DateTime----------------------------"); cls = DateTime.class; System.out.println("-----------DateTimeZone------------------------"); cls = DateTimeZone.class; System.out.println("-----------new DateTime()----------------------"); DateTime dt = new DateTime(); System.out.println("-----------new DateTime(ReadableInstant)-------"); dt = new DateTime(dt); System.out.println("-----------new DateTime(Long)------------------"); dt = new DateTime(new Long(0)); System.out.println("-----------------------------------------------"); }
public void testGetInstantChronology_RI() { DateTime dt = new DateTime(123L, BuddhistChronology.getInstance()); assertEquals(BuddhistChronology.getInstance(), DateTimeUtils.getInstantChronology(dt)); Instant i = new Instant(123L); assertEquals(ISOChronology.getInstanceUTC(), DateTimeUtils.getInstantChronology(i)); AbstractInstant ai = new AbstractInstant() { public long getMillis() { return 0L; } public Chronology getChronology() { return null; // testing for this } }; assertEquals(ISOChronology.getInstance(), DateTimeUtils.getInstantChronology(ai)); assertEquals(ISOChronology.getInstance(), DateTimeUtils.getInstantChronology(null)); }
protected HistoricItem getPreviousValue(Item theItem) { DateTime now = new DateTime(); AbstractInstant earlier = now.minusHours(6); if (persistenceService == null) { return PersistenceExtensions.historicState(theItem, earlier); } else { return PersistenceExtensions.historicState(theItem, earlier, persistenceService); } }
public boolean reschedule(AbstractInstant newTime) { try { Trigger trigger = newTrigger().startAt(newTime.toDate()).build(); scheduler.rescheduleJob(triggerKey, trigger); this.triggerKey = trigger.getKey(); this.cancelled = false; this.terminated = false; return true; } catch (SchedulerException e) { logger.warn("An error occured while rescheduling the job '{}': {}", new String[] { jobKey.toString(), e.getMessage() }); return false; } }
private static Timer makeTimer(AbstractInstant instant, String closure, JobDataMap dataMap) { JobKey jobKey = new JobKey(instant.toString() + ": " + closure.toString()); Trigger trigger = newTrigger().startAt(instant.toDate()).build(); Timer timer = new Timer(jobKey, trigger.getKey(), instant); dataMap.put("timer", timer); try { JobDetail job = newJob(TimerExecutionJob.class).withIdentity(jobKey).usingJobData(dataMap).build(); Timer.scheduler.scheduleJob(job, trigger); logger.debug("Scheduled code for execution at {}", instant.toString()); return timer; } catch (SchedulerException e) { logger.error("Failed to schedule code for execution.", e); return null; } }
/** * Retrieves the state of a given <code>item</code> to a certain point in time through the default persistence service. * * @param item the item to retrieve the state for * @param the point in time for which the state should be retrieved * @return the item state at the given point in time */ static public HistoricItem historicState(Item item, AbstractInstant timestamp) { if(isDefaultServiceAvailable()) { return historicState(item, timestamp, defaultService); } else { return null; } }
static private Iterable<HistoricItem> getAllStatesSince(Item item, AbstractInstant timestamp, String serviceName) { PersistenceService service = services.get(serviceName); if (service instanceof QueryablePersistenceService) { QueryablePersistenceService qService = (QueryablePersistenceService) service; FilterCriteria filter = new FilterCriteria(); filter.setBeginDate(timestamp.toDate()); filter.setItemName(item.getName()); filter.setOrdering(Ordering.ASCENDING); return qService.query(filter); } else { logger.warn("There is no queryable persistence service registered with the name '{}'", serviceName); return Collections.emptySet(); } }
@Override public String format(final AbstractInstant target, String pattern) { try { return DateTimeFormat.forPattern(pattern).withLocale(locale).print(target); } catch (final Exception e) { throw new TemplateProcessingException( "Error formatting Joda DateTime with pattern '" + pattern + "' for locale " + this.locale, e); } }
DateTime extractLastApprovedOrRejectDate(List<StatusChange> statusChangeHistory) { return statusChangeHistory.stream() .filter(statusChange -> asList(Status.APPROVED, Status.REJECTED).contains(statusChange.getTo())) .map(StatusChange::getChangedOn) .max(AbstractInstant::compareTo) .orElseGet(() -> null); }
DateTime extractFirstSubmissionDate(List<StatusChange> statusChangeHistory) { return statusChangeHistory.stream() .filter(statusChange -> Status.SUBMITTED.equals(statusChange.getTo())) .map(StatusChange::getChangedOn) .min(AbstractInstant::compareTo) .orElseGet(() -> null); }
/** * Sets last modified date. * * @param lastModifiedDate the last modified date */ public void setLastModifiedDate(final DateTime lastModifiedDate) { this.lastModifiedDate = Optional.ofNullable(lastModifiedDate) .map(AbstractInstant::toDate) .orElse(null); }
public TimerImpl(JobKey jobKey, TriggerKey triggerKey, AbstractInstant startTime) { this.jobKey = jobKey; this.triggerKey = triggerKey; this.startTime = startTime; }
public static Timer createTimer(AbstractInstant instant, Runnable closure) { JobDataMap dataMap = new JobDataMap(); dataMap.put("procedure", closure); return makeTimer(instant, closure.toString(), dataMap); }
public Timer(JobKey jobKey, TriggerKey triggerKey, AbstractInstant startTime) { this.jobKey = jobKey; this.triggerKey = triggerKey; this.startTime = startTime; }
public static Stream<String> timestampsBeforeNow( DateTime since ) { return Stream.of( since, AbstractInstant::isBeforeNow, t -> t.plusMinutes( 60 / BUCKETS_PER_HOUR ) ) .map( Timestamp::format ); }
@Override public long timestamp(final AbstractInstant target) { return target.getMillis(); }
@Override public boolean before(final AbstractInstant date1, final AbstractInstant date2) { return date1.isBefore(date2); }
@Override public boolean after(final AbstractInstant date1, final AbstractInstant date2) { return date1.isAfter(date2); }
@Override public boolean equals(final AbstractInstant date1, final AbstractInstant date2) { return date1.equals(date2); }
/** * Schedules a block of code (with argument) for later execution * * @param instant the point in time when the code should be executed * @param arg1 the argument to pass to the code block * @param closure the code block to execute * * @return a handle to the created timer, so that it can be canceled or rescheduled * @throws ScriptExecutionException if an error occurs during the execution */ public static Timer createTimerWithArgument(AbstractInstant instant, Object arg1, Procedure1<Object> closure) { JobDataMap dataMap = new JobDataMap(); dataMap.put("procedure1", closure); dataMap.put("argument1", arg1); return makeTimer(instant, closure.toString(), dataMap); }
/** * Checks if the state of a given <code>item</code> has changed since a certain point in time. * The default persistence service is used. * * @param item the item to check for state changes * @param the point in time to start the check * @return true, if item state had changed */ static public Boolean changedSince(Item item, AbstractInstant timestamp) { if(isDefaultServiceAvailable()) { return changedSince(item, timestamp, defaultService); } else { return null; } }
/** * Checks if the state of a given <code>item</code> has been updated since a certain point in time. * The default persistence service is used. * * @param item the item to check for state updates * @param the point in time to start the check * @return true, if item state was updated */ static public Boolean updatedSince(Item item, AbstractInstant timestamp) { if(isDefaultServiceAvailable()) { return updatedSince(item, timestamp, defaultService); } else { return null; } }
/** * Checks if the state of a given <code>item</code> has changed since a certain point in time. * The {@link PersistenceService} identified by the <code>serviceName</code> is used. * * @param item the item to check for state changes * @param the point in time to start the check * @param serviceName the name of the {@link PersistenceService} to use * @return true, if item state was updated */ static public Boolean updatedSince(Item item, AbstractInstant timestamp, String serviceName) { Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceName); if(result.iterator().hasNext()) { return true; } else { return false; } }
/** * Gets the historic item with the maximum value of the state of a given <code>item</code> since * a certain point in time. * The default persistence service is used. * * @param item the item to get the maximum state value for * @param the point in time to start the check * @return a historic item with the maximum state value since the given point in time */ static public HistoricItem maximumSince(Item item, AbstractInstant timestamp) { if(isDefaultServiceAvailable()) { return maximumSince(item, timestamp, defaultService); } else { return null; } }
/** * Gets the historic item with the minimum value of the state of a given <code>item</code> since * a certain point in time. * The default persistence service is used. * * @param item the item to get the minimum state value for * @param the point in time to start the check * @return the historic item with the minimum state value since the given point in time */ static public HistoricItem minimumSince(Item item, AbstractInstant timestamp) { if(isDefaultServiceAvailable()) { return minimumSince(item, timestamp, defaultService); } else { return null; } }
/** * Gets the average value of the state of a given <code>item</code> since a certain point in time. * The default persistence service is used. * * @param item the item to get the average state value for * @param the point in time to start the check * @return the average state value since the given point in time */ static public DecimalType averageSince(Item item, AbstractInstant timestamp) { if(isDefaultServiceAvailable()) { return averageSince(item, timestamp, defaultService); } else { return null; } }
/** * Gets the variance value of the state of a given <code>item</code> since a certain point in time. * The default persistence service is used. * * @param item the item to get the average state value for * @param the point in time to start the check * @return the variance of the value since the given point in time */ static public DecimalType varianceSince(Item item, AbstractInstant timestamp) { if(isDefaultServiceAvailable()) { return varianceSince(item, timestamp, defaultService); } else { return null; } }
/** * Gets the standard deviation value of the state of a given <code>item</code> since a certain point in time. * The default persistence service is used. * * @param item the item to get the average state value for * @param the point in time to start the check * @return the standard deviation of the value since the given point in time */ static public DecimalType deviationSince(Item item, AbstractInstant timestamp) { if(isDefaultServiceAvailable()) { return deviationSince(item, timestamp, defaultService); } else { return null; } }
/** * Gets the standard deviation value of the state of a given <code>item</code> since a certain point in time. * The {@link PersistenceService} identified by the <code>serviceName</code> is used. * * @param item the item to get the average state value for * @param the point in time to start the check * @param serviceName the name of the {@link PersistenceService} to use * @return the standard deviation of the value since the given point in time */ static public DecimalType deviationSince(Item item, AbstractInstant timestamp, String serviceName) { DecimalType variance = varianceSince(item, timestamp, serviceName); double deviation = Math.sqrt(variance.doubleValue()); return new DecimalType(deviation); }
/** * Gets the sum of the state of a given <code>item</code> since a certain point in time. * The default persistence service is used. * * @param item the item to get the average state value for * @param the point in time to start the check * @return the average state value since the given point in time */ static public DecimalType sumSince(Item item, AbstractInstant timestamp) { if(isDefaultServiceAvailable()) { return sumSince(item, timestamp, defaultService); } else { return null; } }