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

import org.gringlobal.api.v1.impl.CropTraitObservationController;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.CropTrait;
import org.gringlobal.model.CropTraitObservation;
import org.gringlobal.model.Inventory;
import org.gringlobal.model.Method;
import org.gringlobal.service.CropTraitCodeTranslationService.TranslatedCropTraitCode;
import org.gringlobal.service.CropTraitTranslationService.TranslatedCropTrait;
import org.gringlobal.service.filter.CropTraitObservationFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.fasterxml.jackson.annotation.JsonIgnoreType;


public interface CropTraitObservationService extends FilteredCRUDService2<CropTraitObservation, CropTraitObservationFilter> {

	/** Update without checking versions (modifiedDate) */
	CropTraitObservation forceUpdate(CropTraitObservation cto);

	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);

	TranslatedCropTraitObservation getTranslated(long id);

	Page<TranslatedCropTraitObservation> listTranslated(CropTraitObservationFilter filter, Pageable page) throws SearchException;

	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);
		}
	}

	@JsonIgnoreType // Never serialize directly
	public static class TranslatedCropTraitObservation implements Serializable {
		private static final long serialVersionUID = -7141159760583224989L;
		public CropTraitObservation observation;
		public TranslatedCropTrait translatedCropTrait;
		public TranslatedCropTraitCode translatedCropTraitCode;
	}

}