AdminServiceImpl.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.genesys.server.service.impl;
import java.util.List;
import java.util.Set;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.impl.FaoInstitute;
import org.genesys.server.model.impl.Subset;
import org.genesys.server.service.AccessionService;
import org.genesys.server.service.AdminService;
import org.genesys.server.service.DatasetService;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.GenesysService;
import org.genesys.server.service.InstituteService;
import org.genesys.server.service.RequestService;
import org.genesys.server.service.SubsetService;
import org.genesys.server.service.filter.AccessionFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class AdminServiceImpl implements AdminService {
public static final Logger LOG = LoggerFactory.getLogger(AdminServiceImpl.class);
@Autowired
private GenesysService genesysService;
@Autowired
private AccessionService accessionService;
@Autowired
private InstituteService instituteService;
@Autowired
private DatasetService datasetService;
@Autowired
private SubsetService subsetService;
@Autowired
private RequestService requestService;
@Autowired(required = false)
private ElasticsearchService elasticService;
@Override
@Transactional
public String changeInstituteCode(String oldInstCode, String newInstCode) {
assert (oldInstCode != null);
assert (newInstCode != null);
assert (!newInstCode.equals(oldInstCode)); // the codes are different
// Assert that FaoInstitute record with newInstCode exists
var currentInstitute = instituteService.getInstitute(oldInstCode);
var newInstitute = instituteService.getInstitute(newInstCode);
if (currentInstitute == null) {
throw new NotFoundElement("Record not found by code " + oldInstCode);
}
if (newInstitute == null) {
throw new NotFoundElement("Record not found by code " + newInstCode);
}
assert (!currentInstitute.getId().equals(newInstitute.getId())); // assert they are not null and have different IDs
// Update institute data
newInstitute = instituteService.changeInstitute(currentInstitute, newInstitute);
// Check that newInstCode has no data in Genesys
assert (newInstitute.getAccessionCount() == 0l);
// Change accession.instCode and institute
accessionService.changeInstitute(currentInstitute, newInstitute);
// Change requests
requestService.changeInstitute(currentInstitute, newInstitute);
// Change accession refs in subsets and datasets
datasetService.changeInstitute(currentInstitute, newInstitute);
subsetService.changeInstitute(currentInstitute, newInstitute);
genesysService.updatePDCI(currentInstitute);
genesysService.updatePDCI(newInstitute);
if (elasticService != null) {
// Reindex
try {
// Update institutes
elasticService.update(FaoInstitute.class, List.of(currentInstitute.getId(), newInstitute.getId()));
// Update accessions
AccessionFilter accessionFilter = new AccessionFilter();
accessionFilter
.historic(null)
.institute()
.code(Set.of(oldInstCode));
elasticService.remove(Accession.class, accessionFilter);
accessionFilter.institute().code(Set.of(newInstCode));
elasticService.reindex(Accession.class, accessionFilter);
// Update subsets
elasticService.reindex(Subset.class);
} catch (Throwable e) {
LOG.warn("Reindexing failed with: {}", e.getMessage(), e);
}
}
return newInstitute.getCode();
}
}