OrderRequestNotifications.java
/*
* Copyright 2024 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gringlobal.notification;
import org.genesys.blocks.model.filters.StringFilter;
import org.gringlobal.model.OrderRequestAction;
import org.gringlobal.model.SysUser;
import org.gringlobal.model.notification.NotificationSchedule;
import org.gringlobal.notification.schedule.ScheduledNotification;
import org.gringlobal.service.OrderRequestActionService;
import org.gringlobal.service.OrderRequestActionService.OrderRequestActionRequest;
import org.gringlobal.service.OrderRequestActionService.OrderRequestActionScheduleFilter;
import org.gringlobal.service.filter.AclSidFilter;
import org.gringlobal.service.filter.OrderRequestActionFilter;
import org.gringlobal.util.LocaleContextHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
@Component
public class OrderRequestNotifications extends ActionNotificationsBase<OrderRequestAction, OrderRequestActionFilter, OrderRequestActionRequest, OrderRequestActionScheduleFilter> {
@Autowired
private OrderRequestActionService actionService;
@Override
protected String getFrontendUrl() {
return frontendUrl + "/dist/schedule";
}
@Override
protected String getCodeValueGroup() {
return "ORDER_REQUEST_ACTION";
}
@Override
protected OrderRequestActionService getService() {
return actionService;
}
@ScheduledNotification
@Transactional(readOnly = true)
public void myWeeklySchedule(NotificationSchedule schedule, List<SysUser> recipients) throws Exception {
var tz = ZoneId.of(schedule.getTimezone());
LocalDate today = LocalDate.now(tz);
var fromDate = today.atStartOfDay(tz).toInstant();
var toDate = today.plus(8, ChronoUnit.DAYS).atStartOfDay(tz).toInstant();
var filter = new OrderRequestActionScheduleFilter().fromInclusive(fromDate).toExclusive(toDate);
for (var sysUser : recipients) {
LocaleContextHelper.withLocaleAndTimezone(LocaleContextHelper.getLocale(sysUser), TimeZone.getTimeZone(tz), () -> {
filter.assignee(new AclSidFilter().sid(new StringFilter().eq(Set.of(sysUser.getSid()))));
generateNotification(schedule, List.of(sysUser), (OrderRequestActionScheduleFilter) filter);
return Void.TYPE;
});
}
}
@ScheduledNotification
@Transactional(readOnly = true)
public void generateWeeklyOrderRequestSchedule(NotificationSchedule schedule, List<SysUser> recipients) throws Exception {
var tz = ZoneId.of(schedule.getTimezone());
LocalDate today = LocalDate.now(tz);
var fromDate = today.atStartOfDay(tz).toInstant();
var toDate = today.plus(8, ChronoUnit.DAYS).atStartOfDay(tz).toInstant();
var filter = new OrderRequestActionScheduleFilter().fromInclusive(fromDate).toExclusive(toDate);
generateNotification(schedule, recipients, (OrderRequestActionScheduleFilter) filter);
}
@ScheduledNotification
@Transactional(readOnly = true)
public void generateMonthlyOrderRequestSchedule(NotificationSchedule schedule, List<SysUser> recipients) throws Exception {
var tz = ZoneId.of(schedule.getTimezone());
LocalDate today = LocalDate.now(tz);
var fromDate = today.withDayOfMonth(1).atStartOfDay(tz).toInstant();
var toDate = today.withDayOfMonth(1).plusMonths(1).atStartOfDay(tz).toInstant();
var filter = new OrderRequestActionScheduleFilter().fromInclusive(fromDate).toExclusive(toDate);
generateNotification(schedule, recipients, (OrderRequestActionScheduleFilter) filter);
}
}