Commit 68b3bfcc authored by COTONNEC's avatar COTONNEC
Browse files

[enh] Check if a referenceTaxon exists

parent 3f83ed2a
......@@ -24,10 +24,17 @@ package net.sumaris.core.dao.referential.taxon;
import net.sumaris.core.model.referential.taxon.ReferenceTaxon;
import net.sumaris.core.vo.referential.ReferenceTaxonVO;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface ReferenceTaxonRepository extends JpaRepository<ReferenceTaxon, Integer> {
ReferenceTaxonVO get(int id);
Optional<ReferenceTaxonVO> findById(int id);
ReferenceTaxon save(ReferenceTaxon referenceTaxon);
}
package net.sumaris.core.dao.referential.taxon;
/*-
* #%L
* SUMARiS:: Core
* %%
* Copyright (C) 2018 - 2020 SUMARiS Consortium
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import net.sumaris.core.dao.technical.jpa.IFetchOptions;
import net.sumaris.core.dao.technical.jpa.SumarisJpaRepositoryImpl;
import net.sumaris.core.model.referential.taxon.ReferenceTaxon;
import net.sumaris.core.util.Beans;
import net.sumaris.core.vo.referential.ReferenceTaxonVO;
import org.springframework.beans.factory.annotation.Autowired;
import javax.persistence.EntityManager;
import java.util.Optional;
public class ReferenceTaxonRepositoryImpl<O extends IFetchOptions>
extends SumarisJpaRepositoryImpl<ReferenceTaxon, Integer, ReferenceTaxonVO>
implements ReferenceTaxonRepository {
@Autowired
ReferenceTaxonRepositoryImpl(EntityManager entityManager) {
super(ReferenceTaxon.class, ReferenceTaxonVO.class, entityManager);
}
@Override
public ReferenceTaxonVO get(int id) {
return toVO(this.getById(id));
}
@Override
public Optional<ReferenceTaxonVO> findById(int id) {
return findById(id, null);
}
public Optional<ReferenceTaxonVO> findById(int id, O fetchOptions) {
return super.findById(id).map(entity -> toVO(entity, fetchOptions));
}
@Override
public ReferenceTaxon save(ReferenceTaxon referenceTaxon) {
return super.save(referenceTaxon);
}
protected ReferenceTaxonVO toVO(ReferenceTaxon source, O fetchOptions) {
if (source == null) return null;
ReferenceTaxonVO target = createVO();
toVO(source, target, fetchOptions, true);
return target;
}
protected void toVO(ReferenceTaxon source, ReferenceTaxonVO target, O fetchOptions, boolean copyIfNull) {
Beans.copyProperties(source, target);
}
}
......@@ -92,6 +92,18 @@ public class TaxonNameRepositoryImpl
@Override
public Optional<TaxonNameVO> findReferentByReferenceTaxonId(int referenceTaxonId) {
List<TaxonNameVO> taxonNames = findAllReferentByReferenceTaxonId(referenceTaxonId);
if (CollectionUtils.isEmpty(taxonNames)) return Optional.empty();
if (taxonNames.size() > 1) {
log.warn(String.format("ReferenceTaxon {id=%s} has more than one TaxonNames, with IS_REFERENT=1. Will use the first found.", referenceTaxonId));
}
return Optional.ofNullable(taxonNames.get(0));
}
@Override
public List<TaxonNameVO> findAllReferentByReferenceTaxonId(int referenceTaxonId) {
List<TaxonNameVO> taxonNames = findByFilter(
TaxonNameFilterVO.builder()
.referenceTaxonId(referenceTaxonId)
......@@ -99,13 +111,10 @@ public class TaxonNameRepositoryImpl
.build(),
Pageable.unpaged()
);
if (CollectionUtils.isEmpty(taxonNames)) return Optional.empty();
if (taxonNames.size() > 1) {
log.warn(String.format("ReferenceTaxon {id=%s} has more than one TaxonNames, with IS_REFERENT=1. Will use the first found.", referenceTaxonId));
}
return Optional.ofNullable(taxonNames.get(0));
return taxonNames;
}
@Override
public List<TaxonNameVO> getAllByTaxonGroupId(int taxonGroupId) {
......
......@@ -85,6 +85,8 @@ public interface TaxonNameSpecifications extends ReferentialSpecifications<Taxon
@Cacheable(cacheNames = CacheConfiguration.Names.TAXON_NAME_BY_TAXON_REFERENCE_ID, unless = "#result == null")
Optional<TaxonNameVO> findReferentByReferenceTaxonId(int referenceTaxonId);
List<TaxonNameVO> findAllReferentByReferenceTaxonId(int referenceTaxonId);
@Cacheable(cacheNames = CacheConfiguration.Names.TAXON_NAMES_BY_TAXON_GROUP_ID, unless = "#result == null")
List<TaxonNameVO> getAllByTaxonGroupId(int taxonGroupId);
......
......@@ -23,6 +23,7 @@ package net.sumaris.core.service.referential.taxon;
*/
import net.sumaris.core.dao.technical.SortDirection;
import net.sumaris.core.model.referential.taxon.ReferenceTaxon;
import net.sumaris.core.vo.filter.TaxonNameFilterVO;
import net.sumaris.core.vo.referential.TaxonNameVO;
import org.springframework.transaction.annotation.Transactional;
......@@ -48,5 +49,4 @@ public interface TaxonNameService {
List<TaxonNameVO> getAllByTaxonGroupId(Integer taxonGroupId);
TaxonNameVO save(TaxonNameVO taxonName);
}
......@@ -75,14 +75,17 @@ public class TaxonNameServiceImpl implements TaxonNameService {
public TaxonNameVO save(TaxonNameVO source) {
Preconditions.checkNotNull(source);
if (source.getReferenceTaxonId() == null){
if (source.getReferenceTaxonId() == null) {
source.setReferenceTaxonId(referenceTaxonRepository.save(new ReferenceTaxon()).getId());
} else if (source.getIsReferent()) {
List<TaxonNameVO> taxonNameReferents = taxonNameRepository.findAllReferentByReferenceTaxonId(source.getReferenceTaxonId());
for (TaxonNameVO taxonNameVO : taxonNameReferents) {
if (source.getId() == null || taxonNameVO.getId().intValue() != source.getId().intValue()) {
taxonNameVO.setIsReferent(false);
save(taxonNameVO);
}
}
}
// else{
// List<TaxonNameVO> taxonNameReferents = taxonNameRepository.
// }
return taxonNameRepository.save(source);
}
}
package net.sumaris.core.dao.referential;
/*-
* #%L
* SUMARiS:: Core
* %%
* Copyright (C) 2018 SUMARiS Consortium
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.google.common.collect.ImmutableList;
import lombok.extern.slf4j.Slf4j;
import net.sumaris.core.dao.AbstractDaoTest;
import net.sumaris.core.dao.DatabaseResource;
import net.sumaris.core.dao.referential.taxon.ReferenceTaxonRepository;
import net.sumaris.core.dao.referential.taxon.TaxonGroupRepository;
import net.sumaris.core.dao.referential.taxon.TaxonNameRepository;
import net.sumaris.core.dao.technical.SortDirection;
import net.sumaris.core.model.referential.taxon.*;
import net.sumaris.core.vo.filter.TaxonNameFilterVO;
import net.sumaris.core.vo.referential.TaxonNameVO;
import org.junit.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@Slf4j
public class ReferenceTaxonRepositoryReadTest extends AbstractDaoTest {
@ClassRule
public static final DatabaseResource dbResource = DatabaseResource.writeDb();
@Autowired
protected ReferenceTaxonRepository referenceTaxonRepository;
@Autowired
private TaxonNameRepository taxonNameRepository;
@Override
public void setUp() throws Exception {
super.setUp();
setCommitOnTearDown(false);
}
@Test
public void getById() {
ReferenceTaxon rt = referenceTaxonRepository.getById(1001);
Assert.assertNotNull(rt);
}
}
......@@ -26,13 +26,11 @@ import com.google.common.collect.ImmutableList;
import lombok.extern.slf4j.Slf4j;
import net.sumaris.core.dao.AbstractDaoTest;
import net.sumaris.core.dao.DatabaseResource;
import net.sumaris.core.dao.referential.taxon.ReferenceTaxonRepository;
import net.sumaris.core.dao.referential.taxon.TaxonGroupRepository;
import net.sumaris.core.dao.referential.taxon.TaxonNameRepository;
import net.sumaris.core.dao.technical.SortDirection;
import net.sumaris.core.model.referential.taxon.TaxonGroup;
import net.sumaris.core.model.referential.taxon.TaxonGroupTypeEnum;
import net.sumaris.core.model.referential.taxon.TaxonName;
import net.sumaris.core.model.referential.taxon.TaxonomicLevelEnum;
import net.sumaris.core.model.referential.taxon.*;
import net.sumaris.core.vo.filter.TaxonNameFilterVO;
import net.sumaris.core.vo.referential.TaxonNameVO;
import org.junit.*;
......
......@@ -27,12 +27,14 @@ import io.leangen.graphql.annotations.*;
import io.leangen.graphql.execution.ResolutionEnvironment;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import net.sumaris.core.dao.referential.taxon.ReferenceTaxonRepository;
import net.sumaris.core.dao.technical.SortDirection;
import net.sumaris.core.model.administration.programStrategy.Program;
import net.sumaris.core.model.referential.taxon.ReferenceTaxon;
import net.sumaris.core.model.referential.taxon.TaxonName;
import net.sumaris.core.service.referential.ReferentialService;
import net.sumaris.core.service.referential.taxon.TaxonNameService;
import net.sumaris.core.vo.filter.ReferentialFilterVO;
import net.sumaris.core.vo.filter.TaxonNameFilterVO;
import net.sumaris.core.vo.referential.*;
import net.sumaris.server.http.security.IsSupervisor;
......@@ -44,6 +46,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
@Transactional
......@@ -56,6 +59,9 @@ public class TaxonNameGraphQLService {
@Autowired
private TaxonNameService taxonNameService;
@Autowired
private ReferenceTaxonRepository referenceTaxonRepository;
@Autowired
private ChangesPublisherService changesPublisherService;
......@@ -100,16 +106,6 @@ public class TaxonNameGraphQLService {
return referentialService.countByFilter(Program.class.getSimpleName(), filter);
}
@GraphQLSubscription(name = "updateTaxonName", description = "Subscribe to changes on a taxon name")
@IsUser
public Publisher<TaxonNameVO> updateTaxonName(@GraphQLArgument(name = "id") final Integer id,
@GraphQLArgument(name = "interval", defaultValue = "30", description = "Minimum interval to find changes, in seconds.") final Integer minIntervalInSecond,
@GraphQLEnvironment ResolutionEnvironment env) {
Preconditions.checkArgument(id >= 0, "Invalid 'id' argument");
return changesPublisherService.getPublisher(TaxonName.class, TaxonNameVO.class, id, minIntervalInSecond, true);
}
/* -- Mutations -- */
@GraphQLMutation(name = "saveTaxonName", description = "Save a Taxon name")
......@@ -118,4 +114,12 @@ public class TaxonNameGraphQLService {
return taxonNameService.save(taxonName);
}
/* -- Reference Taxon -- */
@GraphQLQuery(name = "referenceTaxonExists", description = "Search in referenceTaxons")
@Transactional(readOnly = true)
public Boolean referenceTaxonExists(@GraphQLArgument(name = "id") final Integer id) {
Optional<ReferenceTaxon> referenceTaxon = referenceTaxonRepository.findById(id);
return referenceTaxon.isPresent();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment