CropTraitObservationService.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.util.Objects;
import java.util.Set;

import org.gringlobal.api.v1.impl.CropTraitObservationController;
import org.gringlobal.model.CropTrait;
import org.gringlobal.model.CropTraitObservation;
import org.gringlobal.model.Inventory;
import org.gringlobal.model.Method;
import org.gringlobal.service.filter.CropTraitObservationFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;


public interface CropTraitObservationService extends FilteredCRUDService<CropTraitObservation, CropTraitObservationFilter> {

	FilteredObservations search(CropTraitObservationFilter filter, Pageable page);

	Page<Inventory> getObservationInventoriesByMethod(Long methodId, Pageable pageable);

	Page<CropTrait> getObservationTraitsByMethod(Long methodId, Pageable pageable);

	int ensureObservations(CropTraitObservationController.EnsureObservationsRequest request);

	class FilteredObservations {
		public Set<Method> methods;
		public Set<CropTraitTranslationService.TranslatedCropTrait> cropTraits;
		public Set<InventoryWithObservations> observations;

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

	class InventoryWithObservations {
		public Inventory i;
		public Set<CropTraitObservation> o;

		public InventoryWithObservations(Inventory i, Set<CropTraitObservation> 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;
			InventoryWithObservations that = (InventoryWithObservations) o1;
			return Objects.equals(i, that.i) && Objects.equals(o.size(), that.o.size());
		}

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

}