InventoryActionNotifications.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.InventoryAction;
import org.gringlobal.model.SysUser;
import org.gringlobal.model.notification.NotificationSchedule;
import org.gringlobal.notification.schedule.ScheduledNotification;
import org.gringlobal.service.InventoryActionService;
import org.gringlobal.service.InventoryActionService.InventoryActionRequest;
import org.gringlobal.service.InventoryActionService.InventoryActionScheduleFilter;
import org.gringlobal.service.filter.AclSidFilter;
import org.gringlobal.service.filter.InventoryActionFilter;
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 InventoryActionNotifications extends ActionNotificationsBase<InventoryAction, InventoryActionFilter, InventoryActionRequest, InventoryActionScheduleFilter> {

	@Autowired
	private InventoryActionService actionService;

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

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

	@Override
	protected InventoryActionService 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 InventoryActionScheduleFilter().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), (InventoryActionScheduleFilter) filter);
				return Void.TYPE;
			});
		}
	}

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

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

}