InventoryRepositoryCustom.java

/*
 * Copyright 2020 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.persistence;

import com.querydsl.core.types.Predicate;
import org.gringlobal.model.Accession;
import org.gringlobal.model.Inventory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

/**
 * Custom InventoryRepository.
 * 
 * @author Matija Obreza
 */
public interface InventoryRepositoryCustom {

	/**
	 * Find max number part 2 for inventories using number part 1 (+ part 3 + form type code if not null)
	 *
	 * @param accession the accession
	 * @param inventoryNumberPart1 the inventory number part 1
	 * @param inventoryNumberPart3 the inventory number part 3 (may be null)
	 * @param formType form type code (may be null)
	 * @return the highest part number, 0 if nothing is using this prefix
	 */
	long findMaxNumberPart2(Accession accession, String inventoryNumberPart1, String inventoryNumberPart3, String formType);

	/**
	 * Get system inventory (type code **) of the accession
	 * 
	 * @param accession the accession
	 * @return system inventory of the accession
	 */
	Inventory getSystemInventory(Accession accession);

	/**
	 * Update inventoryNumber column without changing audited data
	 *
	 * @param inventoryId the ID of inventory
	 * @param inventoryNumber the inventoryNumber
	 */
	void setInventoryNumber(Long inventoryId, String inventoryNumber);

	/**
	 *  Calculate the aggregate of inventory quantities
	 *
	 * @param filters predicate for where() clause
	 * @param page the page
	 * @return list of results
	 */
	Page<AggregatedInventoryQuantity> aggregateQuantity(Predicate filters, Pageable page);

	/**
	 * DTO for aggregated inventory quantities
	 */
	static class AggregatedInventoryQuantity {

		public Accession accession;
		public String formTypeCode;
		public String availabilityStatusCode;
		public String quantityOnHandUnitCode;
		public long inventoryMaintenancePolicyCount;
		public long parentInventoryCount;
		public long inventoryCount;
		public double quantityOnHand;

		public AggregatedInventoryQuantity() {
		}

		public AggregatedInventoryQuantity(Long accessionId, String formTypeCode, String availabilityStatusCode, String quantityOnHandUnitCode,
				long inventoryMaintenancePolicyCount, long parentInventoryCount, long inventoryCount, double quantityOnHand) {
			this.accession = new Accession(accessionId);
			this.formTypeCode = formTypeCode;
			this.availabilityStatusCode = availabilityStatusCode;
			this.quantityOnHandUnitCode = quantityOnHandUnitCode;
			this.inventoryMaintenancePolicyCount = inventoryMaintenancePolicyCount;
			this.parentInventoryCount = parentInventoryCount;
			this.inventoryCount = inventoryCount;
			this.quantityOnHand = quantityOnHand;
		}

	}
}