/** * Gets the current date/time as an XMLGregorianCalendar with the default time zone in the default locale. * * @return the current date/time. */ public static XMLGregorianCalendar now() { return getXMLGregorianCalendarValue(null); }
/** * Adds a number of days to a timestamp returning a new object. The original {@code Timestamp} is unchanged. * * @param timestamp the timestamp, not null * @param amount the amount to add, may be negative * * @return the new {@code Timestamp} with the amount added */ public static Timestamp addDays(Timestamp timestamp, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(timestamp.getTime()); return new Timestamp(addDays(calendar.getTime(), amount).getTime()); }
/** * Adds a number of minutes to a timestamp returning a new object. The original {@code Timestamp} is unchanged. * * @param timestamp the timestamp, not null * @param amount the amount to add, may be negative * * @return the new {@code Timestamp} with the amount added */ public static Timestamp addMinutes(Timestamp timestamp, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(timestamp.getTime()); return new Timestamp(addMinutes(calendar.getTime(), amount).getTime()); }
/** * Apply a predicate for registration date range filter. * * @param registrationDateRangeFilter the registration date range filter, not null * @param businessObjectDataEntity the business object data entity * @param builder the query builder * @param predicate the predicate to be updated * * @return the predicate with added registration date range filter */ private Predicate applyRegistrationDateRangeFilter(RegistrationDateRangeFilter registrationDateRangeFilter, Root<BusinessObjectDataEntity> businessObjectDataEntity, CriteriaBuilder builder, Predicate predicate) { // Apply predicate for registration start date and removed the time portion of the date. if (registrationDateRangeFilter.getStartRegistrationDate() != null) { predicate = builder.and(predicate, builder.greaterThanOrEqualTo(businessObjectDataEntity.get(BusinessObjectDataEntity_.createdOn), HerdDateUtils.resetTimeToMidnight(registrationDateRangeFilter.getStartRegistrationDate()))); } // Apply predicate for registration end date. Removed time portion of the date and added one day to get the result till the end of the day if (registrationDateRangeFilter.getEndRegistrationDate() != null) { predicate = builder.and(predicate, builder.lessThan(businessObjectDataEntity.get(BusinessObjectDataEntity_.createdOn), HerdDateUtils.addDays(HerdDateUtils.resetTimeToMidnight(registrationDateRangeFilter.getEndRegistrationDate()), 1))); } return predicate; }
start = HerdDateUtils.getXMLGregorianCalendarValue(HerdDateUtils.addDays(businessObjectDataEntity.getCreatedOn(), -1)); end = HerdDateUtils.getXMLGregorianCalendarValue(HerdDateUtils.addDays(businessObjectDataEntity.getCreatedOn(), 1)); businessObjectDataSearchKey.setRegistrationDateRangeFilter(new RegistrationDateRangeFilter(start, end)); List<BusinessObjectData> result = businessObjectDataDao.searchBusinessObjectData(businessObjectDataSearchKey, DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE); start = HerdDateUtils.getXMLGregorianCalendarValue(HerdDateUtils.addDays(businessObjectDataEntity.getCreatedOn(), -1)); businessObjectDataSearchKey.setRegistrationDateRangeFilter(new RegistrationDateRangeFilter(start, null)); result = businessObjectDataDao.searchBusinessObjectData(businessObjectDataSearchKey, DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE); end = HerdDateUtils.getXMLGregorianCalendarValue(HerdDateUtils.addDays(businessObjectDataEntity.getCreatedOn(), 1)); businessObjectDataSearchKey.setRegistrationDateRangeFilter(new RegistrationDateRangeFilter(null, end)); result = businessObjectDataDao.searchBusinessObjectData(businessObjectDataSearchKey, DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE); start = HerdDateUtils.getXMLGregorianCalendarValue(HerdDateUtils.addDays(businessObjectDataEntity.getCreatedOn(), 1)); businessObjectDataSearchKey.setRegistrationDateRangeFilter(new RegistrationDateRangeFilter(start, null)); result = businessObjectDataDao.searchBusinessObjectData(businessObjectDataSearchKey, DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE); end = HerdDateUtils.getXMLGregorianCalendarValue(HerdDateUtils.addDays(businessObjectDataEntity.getCreatedOn(), -1)); businessObjectDataSearchKey.setRegistrationDateRangeFilter(new RegistrationDateRangeFilter(null, end)); result = businessObjectDataDao.searchBusinessObjectData(businessObjectDataSearchKey, DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE); start = HerdDateUtils.getXMLGregorianCalendarValue(businessObjectDataEntity.getCreatedOn()); end = HerdDateUtils.getXMLGregorianCalendarValue(businessObjectDataEntity.getCreatedOn()); businessObjectDataSearchKey.setRegistrationDateRangeFilter(new RegistrationDateRangeFilter(start, end)); result = businessObjectDataDao.searchBusinessObjectData(businessObjectDataSearchKey, DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE);
targetMethodSignature.getName(), durationMilliseconds, HerdDateUtils.formatDuration(durationMilliseconds));
/** * Returns Velocity context map of the keys and values common across all notification message types. * * @param username the username or user id of the logged in user that caused this message to be generated * @param herdEnvironmentKey the name of the herd environment property * @param herdEnvironmentValue the value of the herd environment property * @param herdNotificationSqsEnvironmentKey the name of the herd notification sqs environment property * @param herdNotificationSqsEnvironmentValue the value of the herd notification sqs environment property * * @return the Velocity context map */ Map<String, Object> getBaseVelocityContextMapHelper(String username, String herdEnvironmentKey, String herdEnvironmentValue, String herdNotificationSqsEnvironmentKey, String herdNotificationSqsEnvironmentValue) { Map<String, Object> context = new HashMap<>(); context.put(herdEnvironmentKey, herdEnvironmentValue); context.put(escapeJson(herdEnvironmentKey) + WITH_JSON_SNAKE_CASE, escapeJson(herdEnvironmentValue)); context.put(escapeXml(herdEnvironmentKey) + WITH_XML_SNAKE_CASE, escapeXml(herdEnvironmentValue)); context.put(herdNotificationSqsEnvironmentKey, herdNotificationSqsEnvironmentValue); context.put(escapeJson(herdNotificationSqsEnvironmentKey) + WITH_JSON_SNAKE_CASE, escapeJson(herdNotificationSqsEnvironmentValue)); context.put(escapeXml(herdNotificationSqsEnvironmentKey) + WITH_XML_SNAKE_CASE, escapeXml(herdNotificationSqsEnvironmentValue)); context.put("current_time", HerdDateUtils.now().toString()); context.put("uuid", UUID.randomUUID().toString()); addStringPropertyToContext(context, "username", username); context.put("StringUtils", StringUtils.class); context.put("CollectionUtils", CollectionUtils.class); context.put("Collections", Collections.class); // Return the message text. return context; }
@Test public void testResetTimeToMidnight() throws DatatypeConfigurationException { XMLGregorianCalendar gregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar("2015-12-21T21:32:52"); Timestamp result = HerdDateUtils.resetTimeToMidnight(gregorianCalendar); assertEquals(Timestamp.valueOf("2015-12-21 00:00:00.0"), result); } }
targetMethodSignature.getName(), durationMilliseconds, HerdDateUtils.formatDuration(durationMilliseconds));
/** * Apply a predicate for registration date range filter. * * @param registrationDateRangeFilter the registration date range filter, not null * @param businessObjectDataEntity the business object data entity * @param builder the query builder * @param predicate the predicate to be updated * * @return the predicate with added registration date range filter */ private Predicate applyRegistrationDateRangeFilter(RegistrationDateRangeFilter registrationDateRangeFilter, Root<BusinessObjectDataEntity> businessObjectDataEntity, CriteriaBuilder builder, Predicate predicate) { // Apply predicate for registration start date and removed the time portion of the date. if (registrationDateRangeFilter.getStartRegistrationDate() != null) { predicate = builder.and(predicate, builder.greaterThanOrEqualTo(businessObjectDataEntity.get(BusinessObjectDataEntity_.createdOn), HerdDateUtils.resetTimeToMidnight(registrationDateRangeFilter.getStartRegistrationDate()))); } // Apply predicate for registration end date. Removed time portion of the date and added one day to get the result till the end of the day if (registrationDateRangeFilter.getEndRegistrationDate() != null) { predicate = builder.and(predicate, builder.lessThan(businessObjectDataEntity.get(BusinessObjectDataEntity_.createdOn), HerdDateUtils.addDays(HerdDateUtils.resetTimeToMidnight(registrationDateRangeFilter.getEndRegistrationDate()), 1))); } return predicate; }
/** * Returns Velocity context map of the keys and values common across all notification message types. * * @param username the username or user id of the logged in user that caused this message to be generated * @param herdEnvironmentKey the name of the herd environment property * @param herdEnvironmentValue the value of the herd environment property * @param herdNotificationSqsEnvironmentKey the name of the herd notification sqs environment property * @param herdNotificationSqsEnvironmentValue the value of the herd notification sqs environment property * * @return the Velocity context map */ Map<String, Object> getBaseVelocityContextMapHelper(String username, String herdEnvironmentKey, String herdEnvironmentValue, String herdNotificationSqsEnvironmentKey, String herdNotificationSqsEnvironmentValue) { Map<String, Object> context = new HashMap<>(); context.put(herdEnvironmentKey, herdEnvironmentValue); context.put(escapeJson(herdEnvironmentKey) + WITH_JSON_SNAKE_CASE, escapeJson(herdEnvironmentValue)); context.put(escapeXml(herdEnvironmentKey) + WITH_XML_SNAKE_CASE, escapeXml(herdEnvironmentValue)); context.put(herdNotificationSqsEnvironmentKey, herdNotificationSqsEnvironmentValue); context.put(escapeJson(herdNotificationSqsEnvironmentKey) + WITH_JSON_SNAKE_CASE, escapeJson(herdNotificationSqsEnvironmentValue)); context.put(escapeXml(herdNotificationSqsEnvironmentKey) + WITH_XML_SNAKE_CASE, escapeXml(herdNotificationSqsEnvironmentValue)); context.put("current_time", HerdDateUtils.now().toString()); context.put("uuid", UUID.randomUUID().toString()); addStringPropertyToContext(context, "username", username); context.put("StringUtils", StringUtils.class); context.put("CollectionUtils", CollectionUtils.class); context.put("Collections", Collections.class); // Return the message text. return context; }
/** * Gets the current date/time as an XMLGregorianCalendar with the default time zone in the default locale. * * @return the current date/time. */ public static XMLGregorianCalendar now() { return getXMLGregorianCalendarValue(null); }
/** * Adds a number of days to a timestamp returning a new object. The original {@code Timestamp} is unchanged. * * @param timestamp the timestamp, not null * @param amount the amount to add, may be negative * * @return the new {@code Timestamp} with the amount added */ public static Timestamp addDays(Timestamp timestamp, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(timestamp.getTime()); return new Timestamp(addDays(calendar.getTime(), amount).getTime()); }
@Test public void testFormatDurationMilliseconds() { String result = HerdDateUtils.formatDuration(1); assertEquals("1 Millisecond", result); int length = 999; // Don't go above 1 second. result = HerdDateUtils.formatDuration(length); assertEquals(length + " Milliseconds", result); }
/** * Adds a number of minutes to a timestamp returning a new object. The original {@code Timestamp} is unchanged. * * @param timestamp the timestamp, not null * @param amount the amount to add, may be negative * * @return the new {@code Timestamp} with the amount added */ public static Timestamp addMinutes(Timestamp timestamp, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(timestamp.getTime()); return new Timestamp(addMinutes(calendar.getTime(), amount).getTime()); }
jsonHelper.unmarshallJsonToObject(BusinessObjectDataStatusChangeJsonMessagePayload.class, notificationMessage.getMessageText()); assertEquals(StringUtils.length(businessObjectDataStatusChangeJsonMessagePayload.eventDate), StringUtils.length(HerdDateUtils.now().toString())); assertEquals(expectedBusinessObjectDataKey, businessObjectDataStatusChangeJsonMessagePayload.businessObjectDataKey); assertEquals(expectedNewBusinessObjectDataStatus, businessObjectDataStatusChangeJsonMessagePayload.newBusinessObjectDataStatus);
/** * Builds the {@link XMLGregorianCalendar} for the given {@link Date} * * @param date date * * @return XMLGregorianCalendar */ private XMLGregorianCalendar toXmlGregorianCalendar(Date date) { XMLGregorianCalendar result = null; if (date != null) { result = HerdDateUtils.getXMLGregorianCalendarValue(date); } return result; }
@Test public void testAddDays() { Timestamp testTimestamp = Timestamp.valueOf("2015-12-21 10:34:11"); int daysDiff = 99; assertEquals(Timestamp.valueOf("2016-03-29 10:34:11"), HerdDateUtils.addDays(testTimestamp, daysDiff)); assertEquals(testTimestamp, HerdDateUtils.addDays(testTimestamp, 0)); assertEquals(Timestamp.valueOf("2015-09-13 10:34:11"), HerdDateUtils.addDays(testTimestamp, -daysDiff)); }
@Test public void testFormatDurationZeroMilliseconds() { String result = HerdDateUtils.formatDuration(0); assertEquals("0 Milliseconds", result); }
@Override public List<BusinessObjectDataEntity> getBusinessObjectDataFromStorageOlderThan(StorageEntity storageEntity, int thresholdMinutes, List<String> businessObjectDataStatuses) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<BusinessObjectDataEntity> criteria = builder.createQuery(BusinessObjectDataEntity.class); // The criteria root is the business object data. Root<BusinessObjectDataEntity> businessObjectDataEntityRoot = criteria.from(BusinessObjectDataEntity.class); // Join to the other tables we can filter on. Join<BusinessObjectDataEntity, StorageUnitEntity> storageUnitEntityJoin = businessObjectDataEntityRoot.join(BusinessObjectDataEntity_.storageUnits); // Compute threshold timestamp based on the current database timestamp and threshold minutes. Timestamp thresholdTimestamp = HerdDateUtils.addMinutes(getCurrentTimestamp(), -thresholdMinutes); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(builder.equal(storageUnitEntityJoin.get(StorageUnitEntity_.storageName), storageEntity.getName())); predicates.add(businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.statusCode).in(businessObjectDataStatuses)); predicates.add(builder.lessThanOrEqualTo(businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.createdOn), thresholdTimestamp)); // Order results by "created on" timestamp. Order orderBy = builder.asc(businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.createdOn)); // Add all clauses to the query. criteria.select(businessObjectDataEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).orderBy(orderBy); // Execute the query and return the results. return entityManager.createQuery(criteria).getResultList(); }