MapstructElasticMapper.java

/*
 * Copyright 2026 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.component.elastic;

import org.genesys.blocks.security.model.AclSid;
import org.gringlobal.model.Accession;
import org.gringlobal.model.Cooperator;
import org.gringlobal.model.Crop;
import org.gringlobal.model.CropTrait;
import org.gringlobal.model.Feedback;
import org.gringlobal.model.Geography;
import org.gringlobal.model.Inventory;
import org.gringlobal.model.InventoryExtra;
import org.gringlobal.model.Materiel;
import org.gringlobal.model.OrderRequest;
import org.gringlobal.model.SeedInventoryExtra;
import org.gringlobal.model.Site;
import org.gringlobal.model.SourceDescriptor;
import org.gringlobal.model.SysLang;
import org.gringlobal.model.TaxonomyFamily;
import org.gringlobal.model.TaxonomyGenus;
import org.gringlobal.model.TaxonomySpecies;
import org.gringlobal.model.TissueCultureExtra;
import org.gringlobal.model.WebCooperator;
import org.gringlobal.model.WebUser;
import org.gringlobal.model.elastic.*;
import org.mapstruct.InjectionStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.Objects;

@Mapper(componentModel = "spring", injectionStrategy = InjectionStrategy.FIELD,
	imports = { Objects .class }
)
public interface MapstructElasticMapper {

	default Long mapSidId(AclSid value) {
		return value == null ? null : value.getId();
	}

	SiteDTO mapES(Site site);
	CooperatorDTO mapES(Cooperator cooperator);
	WebUserDTO mapES(WebUser webUser);

	@Mapping(target = "currentGeography", source = "currentGeography.id")
	GeographyDTO mapES(Geography geography);
	MaterielDTO mapES(Materiel materiel);
	CropTraitDTO mapES(CropTrait cropTrait);
	SourceDescriptorDTO mapES(SourceDescriptor sourceDescriptor);
	OrderRequestDTO mapES(OrderRequest orderRequest);
	TaxonomyGenusDTO mapES(TaxonomyGenus taxonomyGenus);
	
	@Mapping(target = "currentTaxonomySpecies", source = "currentTaxonomySpecies.id")
	TaxonomySpeciesDTO mapES(TaxonomySpecies taxonomySpecies);
	AccessionDTO mapES(Accession accession);
	InventoryDTO mapES(Inventory inventory);

	SiteInfo mapESInfo(Site site);
	GeographyInfo mapESInfo(Geography geography);
	SysLangInfo mapESInfo(SysLang sysLang);
	WebCooperatorInfo mapESInfo(WebCooperator webCooperator);
	CropInfo mapESInfo(Crop crop);
	FeedbackInfo mapESInfo(Feedback feedback);
	OrderRequestInfo mapESInfo(OrderRequest orderRequest);
	TaxonomyFamilyInfo mapESInfo(TaxonomyFamily taxonomyFamily);

	@Mapping(target = "familyName", source = "taxonomyFamily.familyName")
	@Mapping(target = "subfamilyName", source = "taxonomyFamily.subfamilyName")
	@Mapping(target = "tribeName", source = "taxonomyFamily.tribeName")
	@Mapping(target = "subtribeName", source = "taxonomyFamily.subtribeName")
	TaxonomyGenusInfo mapESInfo(TaxonomyGenus taxonomyGenus);

	@Mapping(target = "hybridCode", expression = "java(ts.getTaxonomyGenus() == null ? null : ts.getTaxonomyGenus().getHybridCode())")
	@Mapping(target = "genusName", expression = "java(ts.getTaxonomyGenus() == null ? null : ts.getTaxonomyGenus().getGenusName())")
	TaxonomySpeciesInfo mapESInfo(TaxonomySpecies ts);

	default InventoryExtraDTO mapES(InventoryExtra extra) {
		if (extra instanceof TissueCultureExtra)
			return mapES((TissueCultureExtra) extra);
		else if (extra instanceof SeedInventoryExtra) {
			return mapES((SeedInventoryExtra) extra);
		} else if (extra == null) {
			return null; // Handle null
		} else {
			throw new RuntimeException("Invalid extra type!");
		}
	}

	TissueCultureExtraDTO mapES(TissueCultureExtra extra);
	SeedInventoryExtraDTO mapES(SeedInventoryExtra extra);

}