AccessionActionNotifications.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.AccessionAction;
import org.gringlobal.model.SysUser;
import org.gringlobal.model.notification.NotificationSchedule;
import org.gringlobal.notification.schedule.ScheduledNotification;
import org.gringlobal.service.AccessionActionService;
import org.gringlobal.service.AccessionActionService.AccessionActionRequest;
import org.gringlobal.service.AccessionActionService.AccessionActionScheduleFilter;
import org.gringlobal.service.filter.AccessionActionFilter;
import org.gringlobal.service.filter.AclSidFilter;
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 AccessionActionNotifications extends ActionNotificationsBase<AccessionAction, AccessionActionFilter, AccessionActionRequest, AccessionActionScheduleFilter> {

	@Autowired
	private AccessionActionService actionService;

	@Override
	protected String getFrontendUrl() {
		return frontendUrl + "/a/schedule";
	}

	@Override
	protected AccessionActionService getService() {
		return actionService;
	}

	@Override
	protected String getCodeValueGroup() {
		return "ACCESSION_ACTION";
	}

	@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 AccessionActionScheduleFilter().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), (AccessionActionScheduleFilter) filter);
				return Void.TYPE;
			});
		}
	}

	@ScheduledNotification
	@Transactional(readOnly = true)
	public void generateWeeklyAccessionSchedule(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 AccessionActionScheduleFilter().fromInclusive(fromDate).toExclusive(toDate);
		generateNotification(schedule, recipients, (AccessionActionScheduleFilter) filter);
	}

	@ScheduledNotification
	@Transactional(readOnly = true)
	public void generateMonthlyAccessionSchedule(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 AccessionActionScheduleFilter().fromInclusive(fromDate).toExclusive(toDate);

		generateNotification(schedule, recipients, (AccessionActionScheduleFilter) filter);
	}

}