use of com.google.cloud.asset.v1.TimeWindow in project BridgeServer2 by Sage-Bionetworks.
the class SessionValidator method validate.
@Override
public void validate(Object obj, Errors errors) {
Session session = (Session) obj;
if (isBlank(session.getGuid())) {
errors.rejectValue(GUID_FIELD, CANNOT_BE_BLANK);
}
if (isBlank(session.getName())) {
errors.rejectValue(NAME_FIELD, CANNOT_BE_BLANK);
} else if (!session.getName().matches(BRIDGE_RELAXED_ID_PATTERN)) {
errors.rejectValue(NAME_FIELD, BRIDGE_RELAXED_ID_ERROR);
}
validateStringLength(errors, 255, session.getName(), NAME_FIELD);
if (session.getStartEventIds().isEmpty() && session.getStudyBurstIds().isEmpty()) {
errors.rejectValue("", MUST_DEFINE_TRIGGER_ERROR);
}
validateStringLength(errors, 32, session.getSymbol(), SYMBOL_FIELD);
if (session.getSymbol() != null && isBlank(session.getSymbol())) {
errors.rejectValue(SYMBOL_FIELD, CANNOT_BE_BLANK);
}
// Not catching duplicates, here and in study bursts
Set<String> uniqueStartEventIds = new HashSet<>();
for (int i = 0; i < session.getStartEventIds().size(); i++) {
String eventId = session.getStartEventIds().get(i);
errors.pushNestedPath(START_EVENT_IDS_FIELD + '[' + i + ']');
if (uniqueStartEventIds.contains(eventId)) {
errors.rejectValue("", CANNOT_BE_DUPLICATE);
}
uniqueStartEventIds.add(eventId);
if (eventId == null) {
errors.rejectValue("", INVALID_EVENT_ID);
} else if (isBlank(eventId)) {
errors.rejectValue("", CANNOT_BE_BLANK);
}
errors.popNestedPath();
}
Set<String> uniqueStudyBurstIds = new HashSet<>();
for (int i = 0; i < session.getStudyBurstIds().size(); i++) {
String burstId = session.getStudyBurstIds().get(i);
errors.pushNestedPath(STUDY_BURST_IDS_FIELD + '[' + i + ']');
if (uniqueStudyBurstIds.contains(burstId)) {
errors.rejectValue("", CANNOT_BE_DUPLICATE);
}
uniqueStudyBurstIds.add(burstId);
if (burstId == null) {
errors.rejectValue("", CANNOT_BE_NULL);
} else if (isBlank(burstId)) {
errors.rejectValue("", CANNOT_BE_BLANK);
}
if (!studyBurstIds.contains(burstId)) {
errors.rejectValue("", UNDEFINED_STUDY_BURST);
}
errors.popNestedPath();
}
if (session.getOccurrences() != null && session.getOccurrences() < 1) {
errors.rejectValue(OCCURRENCES_FIELD, LESS_THAN_ONE_ERROR);
}
validateFixedLengthPeriod(errors, session.getDelay(), DELAY_FIELD, false);
if (scheduleDuration != null && session.getDelay() != null) {
if (periodInMinutes(session.getDelay()) > periodInMinutes(scheduleDuration)) {
errors.rejectValue(DELAY_FIELD, DELAY_LONGER_THAN_SCHEDULE_DURATION_ERROR);
}
}
validateFixedLengthLongPeriod(errors, session.getInterval(), INTERVAL_FIELD, false);
if (session.getPerformanceOrder() == null) {
errors.rejectValue(PERFORMANCE_ORDER_FIELD, CANNOT_BE_NULL);
}
if (!session.getLabels().isEmpty()) {
validateLabels(errors, session.getLabels());
}
validateJsonLength(errors, TEXT_SIZE, session.getLabels(), LABELS_FIELD);
if (session.getTimeWindows().isEmpty()) {
errors.rejectValue(TIME_WINDOWS_FIELD, CANNOT_BE_NULL_OR_EMPTY);
} else {
for (int i = 0; i < session.getTimeWindows().size(); i++) {
TimeWindow window = session.getTimeWindows().get(i);
errors.pushNestedPath(TIME_WINDOWS_FIELD + "[" + i + "]");
if (isBlank(window.getGuid())) {
errors.rejectValue(GUID_FIELD, CANNOT_BE_BLANK);
}
if (window.getStartTime() == null) {
errors.rejectValue(START_TIME_FIELD, CANNOT_BE_NULL);
} else if (window.getStartTime().getSecondOfMinute() > 0) {
errors.rejectValue(START_TIME_FIELD, START_TIME_SECONDS_INVALID_ERROR);
} else if (window.getStartTime().getMillisOfSecond() > 0) {
errors.rejectValue(START_TIME_FIELD, START_TIME_MILLIS_INVALID_ERROR);
}
validateFixedLengthPeriod(errors, window.getExpiration(), EXPIRATION_FIELD, false);
if (session.getInterval() != null) {
if (window.getExpiration() == null) {
errors.rejectValue(EXPIRATION_FIELD, EXPIRATION_REQUIRED_ERROR);
} else {
long intervalMin = periodInMinutes(session.getInterval());
long expMin = periodInMinutes(window.getExpiration());
if (expMin > intervalMin) {
errors.rejectValue(EXPIRATION_FIELD, EXPIRATION_LONGER_THAN_INTERVAL_ERROR);
}
}
}
if (window.getExpiration() != null) {
long windowExpiration = periodInMinutes(window.getExpiration());
if (session.getDelay() != null) {
windowExpiration += periodInMinutes(session.getDelay());
}
if (scheduleDuration != null) {
if (windowExpiration > periodInMinutes(scheduleDuration)) {
errors.rejectValue(EXPIRATION_FIELD, WINDOW_EXPIRATION_AFTER_SCHEDULE_DURATION);
}
}
}
errors.popNestedPath();
}
validateTimeWindowOverlaps(errors, session);
}
for (int i = 0; i < session.getAssessments().size(); i++) {
AssessmentReference asmt = session.getAssessments().get(i);
errors.pushNestedPath(ASSESSMENTS_FIELD + "[" + i + "]");
if (isBlank(asmt.getGuid())) {
errors.rejectValue(GUID_FIELD, CANNOT_BE_BLANK);
}
if (isBlank(asmt.getIdentifier())) {
errors.rejectValue(IDENTIFIER_FIELD, CANNOT_BE_BLANK);
}
validateStringLength(errors, 255, asmt.getIdentifier(), IDENTIFIER_FIELD);
if (isBlank(asmt.getAppId())) {
errors.rejectValue(APP_ID_FIELD, CANNOT_BE_BLANK);
}
validateStringLength(errors, 255, asmt.getTitle(), TITLE_FIELD);
validateLabels(errors, asmt.getLabels());
validateJsonLength(errors, TEXT_SIZE, asmt.getLabels(), LABELS_FIELD);
validateColorScheme(errors, asmt.getColorScheme(), COLOR_SCHEME_FIELD);
errors.popNestedPath();
}
for (int i = 0; i < session.getNotifications().size(); i++) {
Notification notification = session.getNotifications().get(i);
errors.pushNestedPath(NOTIFICATIONS_FIELD + "[" + i + "]");
if (notification.getNotifyAt() == null) {
errors.rejectValue(NOTIFY_AT_FIELD, CANNOT_BE_NULL);
}
validateFixedLengthPeriod(errors, notification.getOffset(), OFFSET_FIELD, false);
validateFixedLengthPeriod(errors, notification.getInterval(), INTERVAL_FIELD, false);
if (!session.getTimeWindows().isEmpty()) {
TimeWindow shortestWindow = shortestTimeWindow(session);
if (shortestWindow.getExpiration() != null) {
long winExpMinutes = periodInMinutes(shortestWindow.getExpiration());
if (periodInMinutes(notification.getOffset()) > winExpMinutes) {
errors.rejectValue(OFFSET_FIELD, LONGER_THAN_WINDOW_EXPIRATION_ERROR);
}
if (notification.getInterval() != null) {
if (winExpMinutes < (24 * 60)) {
errors.rejectValue(INTERVAL_FIELD, WINDOW_SHORTER_THAN_DAY_ERROR);
} else if (periodInMinutes(notification.getInterval()) > winExpMinutes) {
errors.rejectValue(INTERVAL_FIELD, LONGER_THAN_WINDOW_EXPIRATION_ERROR);
}
}
}
}
if (notification.getMessages() == null || notification.getMessages().isEmpty()) {
errors.rejectValue(MESSAGES_FIELD, CANNOT_BE_NULL_OR_EMPTY);
} else {
validateMessages(errors, notification.getMessages());
validateJsonLength(errors, TEXT_SIZE, notification.getMessages(), MESSAGES_FIELD);
}
errors.popNestedPath();
}
}
use of com.google.cloud.asset.v1.TimeWindow in project BridgeServer2 by Sage-Bionetworks.
the class Scheduler method calculateTimeline.
public final Timeline calculateTimeline(Schedule2 schedule) {
Timeline.Builder builder = new Timeline.Builder();
builder.withDuration(schedule.getDuration());
builder.withSchedule(schedule);
calculateLanguageKey(builder);
Map<String, Set<String>> studyBurstEventsMap = getStudyBurstEventIdsMap(schedule);
Map<String, StudyBurst> studyBurstsById = schedule.getStudyBursts().stream().collect(Collectors.toMap(StudyBurst::getIdentifier, sb -> sb));
for (Session session : schedule.getSessions()) {
if (!session.getAssessments().isEmpty()) {
List<String> startEventIds = session.getStartEventIds();
for (String studyBurstId : session.getStudyBurstIds()) {
startEventIds = addUniqueItemsToList(startEventIds, studyBurstEventsMap.get(studyBurstId));
}
for (TimeWindow window : session.getTimeWindows()) {
scheduleTimeWindowSequence(builder, schedule, session, window, startEventIds, studyBurstsById);
}
}
}
return builder.build();
}
use of com.google.cloud.asset.v1.TimeWindow in project BridgeServer2 by Sage-Bionetworks.
the class SessionValidatorTest method timeWindows_expirationExceedsScheduleDuration.
@Test
public void timeWindows_expirationExceedsScheduleDuration() {
// Schedule duration is 8 weeks.
Session session = createValidSession();
TimeWindow timeWindow = new TimeWindow();
timeWindow.setGuid(SESSION_GUID_1);
timeWindow.setStartTime(LocalTime.parse("08:00"));
timeWindow.setExpiration(Period.parse("P8WT1H"));
session.setTimeWindows(ImmutableList.of(timeWindow));
session.setInterval(Period.parse("P8W"));
session.setDelay(null);
assertValidatorMessage(INSTANCE, session, TIME_WINDOWS_FIELD + "[0].expiration", WINDOW_EXPIRATION_AFTER_SCHEDULE_DURATION);
}
use of com.google.cloud.asset.v1.TimeWindow in project java-asset by googleapis.
the class BatchGetAssetsHistoryExample method main.
// Export assets for a project.
// @param args path where the results will be exported to.
public static void main(String... args) throws Exception {
// Asset names, e.g.: "//storage.googleapis.com/[BUCKET_NAME]"
String[] assetNames = args[0].split(",");
try (AssetServiceClient client = AssetServiceClient.create()) {
ProjectName parent = ProjectName.of(projectId);
ContentType contentType = ContentType.CONTENT_TYPE_UNSPECIFIED;
TimeWindow readTimeWindow = TimeWindow.newBuilder().build();
BatchGetAssetsHistoryRequest request = BatchGetAssetsHistoryRequest.newBuilder().setParent(parent.toString()).addAllAssetNames(Arrays.asList(assetNames)).setContentType(contentType).setReadTimeWindow(readTimeWindow).build();
BatchGetAssetsHistoryResponse response = client.batchGetAssetsHistory(request);
System.out.println(response);
}
}
Aggregations