DatasetApiServiceImpl.java

/*
 * Copyright 2024 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.genesys.server.api.v1.facade.impl;

import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.server.api.v1.facade.DatasetApiService;
import org.genesys.server.api.v1.mapper.APIv1Mapper;
import org.genesys.server.api.v1.model.Dataset;
import org.genesys.server.api.v1.model.Descriptor;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.dataset.DatasetAccessionRef;
import org.genesys.server.model.dataset.DatasetCreator;
import org.genesys.server.model.dataset.DatasetLocation;
import org.genesys.server.model.filters.DatasetFilter;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.impl.FaoInstitute;
import org.genesys.server.service.DatasetService;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.filter.AccessionFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

@Service("datasetV1APIService")
@Validated
@Transactional(readOnly = true)
public class DatasetApiServiceImpl implements DatasetApiService {

	@Autowired
	private APIv1Mapper mapper;

	@Autowired
	private DatasetService service;

	@Override
	@Transactional
	public Dataset setAccessionRefs(Dataset dataset, Collection<DatasetAccessionRef> accessionRefs) {
		return mapper.map(service.setAccessionRefs(mapper.map(dataset), accessionRefs));
	}

	@Override
	@Transactional
	public Dataset addDescriptors(Dataset dataset, Descriptor... descriptors) {
		return mapper.map(service.addDescriptors(mapper.map(dataset), mapper.map(descriptors, mapper::map, new org.genesys.server.model.traits.Descriptor[descriptors.length])));
	}

	@Override
	@Transactional
	public Dataset removeDescriptors(Dataset dataset, Descriptor... descriptors) {
		return mapper.map(service.removeDescriptors(mapper.map(dataset), mapper.map(descriptors, mapper::map, new org.genesys.server.model.traits.Descriptor[descriptors.length])));
	}

	@Override
	@Transactional
	public Dataset updateDescriptors(Dataset dataset, List<Descriptor> descriptors) {
		return mapper.map(service.updateDescriptors(mapper.map(dataset), mapper.map(descriptors, mapper::map)));
	}

	@Override
	public Dataset loadDataset(Dataset input) {
		return mapper.map(service.loadDataset(mapper.map(input)));
	}

	@Override
	public Dataset loadDataset(UUID uuid, int version) {
		return mapper.map(service.loadDataset(uuid, version));
	}

	@Override
	public Page<DatasetAccessionRef> listAccessions(Dataset dataset, Pageable page) {
		return service.listAccessions(mapper.map(dataset), page);
	}

	@Override
	public List<Dataset> listByAccession(Accession accession) {
		return mapper.map(service.listByAccession(accession), mapper::map);
	}

	@Override
	public Map<String, ElasticsearchService.TermResult> getSuggestions(DatasetFilter filter) throws SearchException, IOException {
		return service.getSuggestions(filter);
	}

	@Override
	public Page<Dataset> listDatasetsForCurrentUser(DatasetFilter filter, Pageable page) throws SearchException {
		return mapper.map(service.listDatasetsForCurrentUser(filter, page), mapper::map);
	}

	@Override
	public Page<Dataset> loadCurrentVersionList(Pageable page) {
		return mapper.map(service.loadCurrentVersionList(page), mapper::map);
	}

	@Override
	public Dataset loadDataset(UUID uuid) throws NotFoundElement {
		return mapper.map(service.loadDataset(uuid));
	}

	@Override
	@Transactional
	public Dataset addDatasetFile(Dataset dataset, MultipartFile file) throws NotFoundElement, IOException, InvalidRepositoryPathException, InvalidRepositoryFileDataException {
		return mapper.map(service.addDatasetFile(mapper.map(dataset), file));
	}

	@Override
	@Transactional
	public Dataset removeDatasetFile(Dataset dataset, UUID fileUuid) throws NotFoundElement, NoSuchRepositoryFileException, IOException {
		return mapper.map(service.removeDatasetFile(mapper.map(dataset), fileUuid));
	}

	@Override
	public List<RepositoryFile> listDatasetFiles(Dataset dataset) throws NotFoundElement {
		return service.listDatasetFiles(mapper.map(dataset));
	}

	@Override
	@Transactional
	public Dataset addAccessionRefs(Dataset dataset, Collection<DatasetAccessionRef> accessionRefs) throws NotFoundElement {
		return mapper.map(service.addAccessionRefs(mapper.map(dataset), accessionRefs));
	}

	@Override
	@Transactional
	public Dataset reviewDataset(Dataset dataset) {
		return mapper.map(service.reviewDataset(mapper.map(dataset)));
	}

	@Override
	@Transactional
	public Dataset rejectDataset(Dataset dataset) {
		return mapper.map(service.rejectDataset(mapper.map(dataset)));
	}

	@Override
	@Transactional
	public Dataset approveDataset(Dataset dataset) {
		return mapper.map(service.approveDataset(mapper.map(dataset)));
	}

	@Override
	@Transactional
	public Dataset updateDatasetFile(Dataset dataset, RepositoryFile metadata) throws NoSuchRepositoryFileException {
		return mapper.map(service.updateDatasetFile(mapper.map(dataset), metadata));
	}

	@Override
	public long countDatasets(DatasetFilter filter) throws SearchException {
		return service.countDatasets(filter);
	}

	@Override
	public List<Object[]> lastPublished() {
		return service.lastPublished();
	}

	@Override
	@Transactional
	public void rematchDatasetAccessions() {
		service.rematchDatasetAccessions();
	}

	@Override
	@Transactional
	public Dataset rematchDatasetAccessions(Dataset dataset) {
		return mapper.map(service.rematchDatasetAccessions(mapper.map(dataset)));
	}

	@Override
	@Transactional
	public DatasetCreator createDatasetCreator(Dataset dataset, DatasetCreator input) throws NotFoundElement {
		return service.createDatasetCreator(mapper.map(dataset), input);
	}

	@Override
	@Transactional
	public DatasetCreator removeDatasetCreator(Dataset dataset, DatasetCreator input) throws NotFoundElement {
		return service.removeDatasetCreator(mapper.map(dataset), input);
	}

	@Override
	public Page<DatasetCreator> listDatasetCreators(UUID datasetUuid, Pageable page) {
		return service.listDatasetCreators(datasetUuid, page);
	}

	@Override
	public DatasetCreator loadDatasetCreator(DatasetCreator datasetCreator) throws NotFoundElement {
		return service.loadDatasetCreator(datasetCreator);
	}

	@Override
	public DatasetCreator loadDatasetCreator(UUID datasetCreatorUuid) throws NotFoundElement {
		return service.loadDatasetCreator(datasetCreatorUuid);
	}

	@Override
	public List<DatasetCreator> loadDatasetCreators(Dataset dataset) throws NotFoundElement {
		return service.loadDatasetCreators(mapper.map(dataset));
	}

	@Override
	@Transactional
	public DatasetCreator updateDatasetCreator(Dataset dataset, DatasetCreator datasetCreator) throws NotFoundElement {
		return service.updateDatasetCreator(mapper.map(dataset), datasetCreator);
	}

	@Override
	public List<DatasetCreator> autocompleteCreators(String text) {
		return service.autocompleteCreators(text);
	}

	@Override
	@Transactional
	public DatasetLocation createLocation(Dataset dataset, DatasetLocation input) throws NotFoundElement {
		return service.createLocation(mapper.map(dataset), input);
	}

	@Override
	@Transactional
	public DatasetLocation removeLocation(Dataset dataset, DatasetLocation input) throws NotFoundElement {
		return service.removeLocation(mapper.map(dataset), input);
	}

	@Override
	public List<DatasetLocation> listLocation(Dataset dataset) throws NotFoundElement {
		return service.listLocation(mapper.map(dataset));
	}

	@Override
	public Page<DatasetLocation> listLocation(UUID datasetUuid, Pageable page) {
		return service.listLocation(datasetUuid, page);
	}

	@Override
	public DatasetLocation loadLocation(DatasetLocation input) throws NotFoundElement {
		return service.loadLocation(input);
	}

	@Override
	public DatasetLocation loadLocation(UUID locationUuid) throws NotFoundElement {
		return service.loadLocation(locationUuid);
	}

	@Override
	@Transactional
	public DatasetLocation updateLocation(Dataset dataset, DatasetLocation input) throws NotFoundElement {
		return service.updateLocation(mapper.map(dataset), input);
	}

	@Override
	@Transactional
	public int clearAccessionRefs(Collection<Accession> accessions) {
		return service.clearAccessionRefs(accessions);
	}

	@Override
	public Path getDatasetRepositoryFolder(Dataset dataset) {
		return service.getDatasetRepositoryFolder(mapper.map(dataset));
	}

	@Override
	public Dataset getDataset(UUID uuid) {
		return mapper.map(service.getDataset(uuid));
	}

	@Override
	public Dataset getDataset(UUID uuid, Integer version) {
		return mapper.map(service.getDataset(uuid, version));
	}

	@Override
	public void writeXlsxMCPD(Dataset dataset, OutputStream outputStream) throws IOException {
		service.writeXlsxMCPD(mapper.map(dataset), outputStream);
	}

	@Override
	@Transactional
	public Dataset createNewVersion(Dataset source) {
		return mapper.map(service.createNewVersion(mapper.map(source)));
	}

	@Override
	@Transactional
	public Dataset create(Dataset dataset) {
		return mapper.map(service.create(mapper.map(dataset)));
	}

	@Override
	@Transactional
	public Dataset update(Dataset dataset) {
		return mapper.map(service.update(mapper.map(dataset)));
	}

	@Override
	public Dataset remove(Dataset dataset) {
		return mapper.map(service.remove(mapper.map(dataset)));
	}

	@Override
	public Page<Dataset> list(DatasetFilter filter, Pageable page) throws SearchException {
		return mapper.map(service.list(filter, page), mapper::map);
	}

	@Override
	@Transactional
	public long changeInstitute(FaoInstitute currentInstitute, FaoInstitute newInstitute) {
		return service.changeInstitute(currentInstitute, newInstitute);
	}

	@Override
	public List<Descriptor> getDatasetDescriptors(Dataset dataset) {
		return mapper.map(service.getDatasetDescriptors(mapper.map(dataset)), mapper::map);
	}

	@Override
	@Transactional
	public List<Descriptor> synchronizeDescriptors(Dataset dataset) {
		return mapper.map(service.synchronizeDescriptors(mapper.map(dataset)), mapper::map);
	}

	@Override
	@Transactional
	public void batchRematchAccessionRefs(List<DatasetAccessionRef> accessionRefs) {
		service.batchRematchAccessionRefs(accessionRefs);
	}

	@Override
	@Transactional
	public Set<UUID> findAccessionsAmphibianDatasets(AccessionFilter accessionFilter) {
		return service.findAccessionsAmphibianDatasets(accessionFilter);
	}
}