NotificationSchedule.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.model.notification;

import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.model.Copyable;
import org.gringlobal.custom.validation.javax.CronExpressionValidation;
import org.gringlobal.custom.validation.javax.ZoneIdValidation;
import org.gringlobal.model.CooperatorOwnedModel;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "notification_schedule", uniqueConstraints = { @UniqueConstraint(name = "UQ_notification_schedule", columnNames = { "generator", "method", "cron" }) })
@Getter
@Setter
public class NotificationSchedule extends CooperatorOwnedModel implements Copyable<NotificationSchedule> {

	private static final long serialVersionUID = 3866061416397511273L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "notification_schedule_id", columnDefinition = "int")
	private Long id;
	
	@Size(max = 256)
	@Column(name = "generator", length = 256, nullable = false)
	@NotNull
	private String generator;

	@Size(max = 128)
	@Column(name = "method", length = 128, nullable = false)
	@NotNull
	private String method;

	@Size(max = 128)
	@Column(name = "cron", length = 128, nullable = false)
	@NotNull
	@CronExpressionValidation
	private String cron;

	@Size(max = 20)
	@Column(name = "timezone", length = 20, nullable = false)
	@NotNull
	@ZoneIdValidation
	private String timezone;

	@Size(max = 100)
	@Basic
	@Column(length = 100)
	private String title;

	@Size(max = 200)
	@Basic
	@Column(length = 200)
	private String description;

	@Basic
	@NotBlank
	@Size(max = 1)
	@Column(name = "is_active", nullable = false, length = 1)
	private String isActive = "Y";

	@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, mappedBy = "notificationSchedule")
	private List<NotificationScheduleSubscriber> subscribers = new ArrayList<>();

	@Override
	public boolean canEqual(Object other) {
		return other instanceof NotificationSchedule;
	}

	@Override
	public Long getId() {
		return id;
	}
}