LocationReservation.java
/*
* Copyright 2022 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.community;
import java.time.LocalDate;
import javax.persistence.Basic;
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.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.model.SelfCleaning;
import org.gringlobal.api.exception.InvalidApiUsageException;
import org.gringlobal.model.CooperatorOwnedModel;
import org.gringlobal.model.Inventory;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* Describes a reservation of capacity at a {@link Location} for the specified
* time frame (start and end date). A reservation is PENDING if the start date is
* in the future, it is ACTIVE if `startDate < now < endDate`
*/
@Entity
@Table(name = "location_reservation")
@JsonIdentityInfo(scope = LocationReservation.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
@NoArgsConstructor
public class LocationReservation extends CooperatorOwnedModel implements SelfCleaning, Copyable<LocationReservation> {
private static final long serialVersionUID = 1090656215151169665L;
@Id
@JsonProperty
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "location_reservation_id", columnDefinition = "int")
private Long id;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "location_id", nullable = false)
@NotNull
private Location location;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "inventory_id", updatable = false)
private Inventory inventory;
@Column(name = "start_date", nullable = false)
@NotNull
protected LocalDate startDate; // inclusive
@Column(name = "end_date", nullable = true)
protected LocalDate endDate; // if not null it is inclusive and must be greater or equal to startDate!
@Min(1)
private int size; // The amount of reserved capacity at Location expressed in Location's capacityUnitCode
@Min(0)
private int offset = 0; // The offset is used to indicate where at the Location's capacity the space is reserved
@Basic
@Column
@Lob
private String note;
public LocationReservation(final Long id) {
this.id = id;
}
@Override
protected void prePersist() {
super.prePersist();
if (endDate != null && startDate.isAfter(endDate)) {
throw new InvalidApiUsageException("startDate is after endDate");
}
}
@Override
public void lazyLoad() {
super.lazyLoad();
lazyLoad(location);
lazyLoad(inventory);
}
@Override
public boolean canEqual(Object other) {
return other instanceof LocationReservation;
}
}