AccessionService.java

/*
 * Copyright 2021 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.service;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.model.Accession;
import org.gringlobal.model.AccessionAction;
import org.gringlobal.model.AccessionInvAnnotation;
import org.gringlobal.model.AccessionInvAttach;
import org.gringlobal.model.AccessionInvGroup;
import org.gringlobal.model.AccessionInvName;
import org.gringlobal.model.AccessionIpr;
import org.gringlobal.model.AccessionPedigree;
import org.gringlobal.model.AccessionQuarantine;
import org.gringlobal.model.AccessionSource;
import org.gringlobal.model.AccessionSourceMap;
import org.gringlobal.model.Citation;
import org.gringlobal.model.Cooperator;
import org.gringlobal.model.community.AccessionMCPD;
import org.gringlobal.model.community.CommunityCodeValues;
import org.gringlobal.service.ShortFilterService.FilterInfo;
import org.gringlobal.service.filter.AccessionFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.fasterxml.jackson.annotation.JsonUnwrapped;

/**
 * The Interface AccessionService.
 */
public interface AccessionService extends FilteredCRUDService2<Accession, AccessionFilter> {

	/**
	 * Value <b>-1</b> is used to indicate that the system should automatically assign the
	 * next (accession) number in the sequence to {@code accessionNumberPart2}.
	 */
	public final static Long AUTO_GENERATE_VALUE = -1L;

	AccessionDetails getAccessionDetails(Accession accession);

	AccessionInvGroup acquire(AcquisitionData acquisitionBatch);

	/**
	 * Accession overview.
	 *
	 * @param groupBy Group by property of Accession
	 * @param filter the accession filters
	 * @return the map with statistics
	 */
	Map<Object, Number> accessionOverview(String groupBy, AccessionFilter filter);

	/**
	 * Recalculate all accession_number fields in the DB for existing records.
	 */
	void recalculateAllAccessionNumbers();

	/**
	 * Calculate {@link Accession#accessionNumber} if null
	 */
	void assignMissingAccessionNumbers();

	/**
	 * Delete the only default Inventory before Accession is deleted.
	 * 
	 * @param accession The accession for which the remaining System inventory should be removed
	 */
	public void deleteDefaultInventory(Accession accession);

	class AccessionDetails {
		@JsonUnwrapped
		public Accession accession;

		public List<AccessionSource> sources;
		public List<AccessionSourceMap> sourceCooperators;
		public List<AccessionAction> actions;
		public List<AccessionIpr> ipr;
		public AccessionPedigree pedigree;
		public List<AccessionQuarantine> quarantine;
		public List<Citation> citations;
		public List<AccessionInvName> names;
		public List<AccessionInvAttach> attachments;
		public List<AccessionInvGroup> groups;
		public List<AccessionInvAnnotation> annotations;
	}

	public static class AcquisitionData {
		// New group template
		public String groupName;
		public String note;
		public Long methodId;

		// List of new accessions
		public List<Accession> accessions;

		// Inventory data template
		public String inventoryNumberPart1;
		public String formTypeCode;
		public long siteId;
		public long inventoryMaintenancePolicyId;
		public String availabilityStatusCode = CommunityCodeValues.INVENTORY_AVAILABILITY_NOTSET.value; // default

		// Accession source data applied to all
		@CodeValueField(CommunityCodeValues.ACCESSION_SOURCE_TYPE)
		public String sourceTypeCode;
		public Date sourceDate;
		@CodeValueField(CommunityCodeValues.DATE_FORMAT)
		public String sourceDateCode;
		public String sourceNote;
		public Cooperator sourceCooperator;
	}

	/**
	 * Gets the accession in MCPD format.
	 *
	 * @param id the accession id
	 * @return the MCPD representation
	 */
	AccessionMCPD getMCPD(Long id);

	/**
	 * List filtered accessions in MCPD format.
	 *
	 * @param filter the filter
	 * @param page the page
	 * @return the page
	 * @throws SearchException the search exception
	 */
	Page<AccessionMCPD> listMCPD(AccessionFilter filter, Pageable page) throws SearchException;

	/**
	 * Share attachment for accessions.
	 */
	void shareAttachment(Long attachId, List<Long> accessionIds);

	/**
	 * Gets the tile image for applied filters
	 *
	 * @param filters the filters
	 * @param zoom the zoom
	 * @param xtile the xtile
	 * @param ytile the ytile
	 * @return the tile
	 * @throws IOException 
	 */
	byte[] getTile(AccessionFilter filter, int zoom, int xtile, int ytile) throws IOException;

	/** Get Map Info */
	MapInfo<AccessionFilter> mapInfo(FilterInfo<AccessionFilter> filterInfo);

	static class MapInfo<F> {
		/** Filter code for tiles */
		public String filterCode;
		/** The filter */
		public F filter;

		/** Array of [[ minLat, minLon ], [ maxLat, maxLon ]] */
		public Number[][] bounds;
		/** Number of records matching the filter */
		public long count;
	}
}