CropTraitObservationDataApiService.java

/*
 * Copyright 2025 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.api.v2.facade;

import org.gringlobal.api.model.CropTraitDTO;
import org.gringlobal.api.model.CropTraitObservationDataDTO;
import org.gringlobal.api.model.InventoryDTO;
import org.gringlobal.api.model.MethodDTO;
import org.gringlobal.api.model.TranslatedCropTraitDTO;
import org.gringlobal.model.CropTraitObservationData;
import org.gringlobal.service.filter.CropTraitObservationDataFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.Objects;
import java.util.Set;

public interface CropTraitObservationDataApiService extends APIFilteredServiceFacade<CropTraitObservationDataDTO, CropTraitObservationData, CropTraitObservationDataFilter> {

	Page<InventoryDTO> getObservationDataInventoriesByMethod(Long methodId, Pageable pageable);
	
	Page<CropTraitDTO> getObservationDataTraitsByMethod(Long methodId, Pageable pageable);

	int ensureObservationData(CropTraitObservationApiService.EnsureObservationsRequest request);

	FilteredObservationsData search(CropTraitObservationDataFilter filter, Pageable pageable);

	class FilteredObservationsData {
		public Set<MethodDTO> methods;
		public Set<TranslatedCropTraitDTO> cropTraits;
		public Set<InventoryWithObservationsData> observationsData;

		/** The filter. */
		public CropTraitObservationDataFilter filter;
	}

	class InventoryWithObservationsData {
		public InventoryDTO i;
		public Set<CropTraitObservationDataDTO> o;

		public InventoryWithObservationsData(InventoryDTO i, Set<CropTraitObservationDataDTO> o) {
			this.i = i;
			this.o = o;
		}

		@Override
		public boolean equals(Object o1) {
			if (this == o1) return true;
			if (o1 == null || getClass() != o1.getClass()) return false;
			InventoryWithObservationsData that = (InventoryWithObservationsData) o1;
			return Objects.equals(i, that.i) && Objects.equals(o.size(), that.o.size());
		}

		@Override
		public int hashCode() {
			return Objects.hash(i, o);
		}
	}
}