Commit 0065da86 authored by Matija Obreza's avatar Matija Obreza
Browse files

Added AccessionCustomRepository, use Collection<Long> instead of List<Long> in Accession*Repository

parent 33f36d56
......@@ -40,5 +40,5 @@ public interface AccessionAliasRepository extends JpaRepository<AccessionAlias,
Accession findAccession(String instCode, String name, int aliasType);
@Query("from AccessionAlias aa where aa.accession.id in ?1")
List<AccessionAlias> findAllFor(List<Long> accessionIds);
List<AccessionAlias> findAllFor(Collection<Long> accessionIds);
}
......@@ -34,5 +34,5 @@ public interface AccessionBreedingRepository extends JpaRepository<AccessionBree
void deleteForAccessions(Collection<Long> accessionIds);
@Query("from AccessionBreeding ab where ab.accession.id in ?1")
List<AccessionBreeding> findAllFor(List<Long> accessionIds);
List<AccessionBreeding> findAllFor(Collection<Long> accessionIds);
}
......@@ -34,5 +34,5 @@ public interface AccessionCollectRepository extends JpaRepository<AccessionColle
void deleteForAccessions(Collection<Long> accessionIds);
@Query("from AccessionCollect ac where ac.accession.id in ?1")
List<AccessionCollect> findAllFor(List<Long> accessionId);
List<AccessionCollect> findAllFor(Collection<Long> accessionIds);
}
/**
* Copyright 2014 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.genesys2.server.persistence.domain;
import java.util.List;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
public interface AccessionCustomRepository {
List<Accession> find(FaoInstitute institute, List<String> acceNumbs, List<String> genera) throws NonUniqueAccessionException;
}
/**
* Copyright 2014 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.genesys2.server.persistence.domain;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional(readOnly = true)
public class AccessionCustomRepositoryImpl implements AccessionCustomRepository, InitializingBean {
public static final Log LOG = LogFactory.getLog(AccessionCustomRepositoryImpl.class);
@PersistenceContext
private EntityManager em;
private CriteriaBuilder criteriaBuilder;
@Override
public List<Accession> find(FaoInstitute institute, List<String> acceNumbs, List<String> genera) throws NonUniqueAccessionException {
boolean uniqueAcceNumbs = institute.hasUniqueAcceNumbs();
CriteriaQuery<Accession> cq = criteriaBuilder.createQuery(Accession.class);
Root<Accession> root = cq.from(Accession.class);
cq.select(root);
root.fetch("stoRage", JoinType.LEFT);
// Join<Accession, Taxonomy2> tax = root.join("taxonomy");
List<Predicate> restrictions = new ArrayList<Predicate>();
if (uniqueAcceNumbs) {
restrictions.add(root.get("accessionName").in(acceNumbs));
if (LOG.isDebugEnabled())
LOG.debug("*** " + institute.getCode() + " " + acceNumbs);
} else {
// A lot of .. (acceNumb=? and genus=?)
for (int i = 0; i < acceNumbs.size(); i++) {
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("accessionName"), acceNumbs.get(i)),
criteriaBuilder.equal(root.get("taxonomy.genus"), genera.get(i))));
}
}
cq.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("institute"), institute), criteriaBuilder.or(restrictions.toArray(new Predicate[] {}))));
cq.distinct(true);
List<Accession> res = em.createQuery(cq).getResultList();
if (LOG.isDebugEnabled())
LOG.debug("*** Loaded accessions " + res.size() + " of " + acceNumbs.size());
// Check for duplicate names if institute is using unique acceNumbs
if (uniqueAcceNumbs) {
Set<String> s = new HashSet<String>();
for (Accession a : res) {
if (s.contains(a.getAccessionName())) {
LOG.error("Duplicate accession name instCode=" + a.getInstituteCode() + " acceNumb=" + a.getAccessionName());
throw new NonUniqueAccessionException(a.getInstituteCode(), a.getAccessionName());
}
s.add(a.getAccessionName());
}
}
return res;
}
@Override
public void afterPropertiesSet() throws Exception {
this.criteriaBuilder = em.getCriteriaBuilder();
}
}
......@@ -34,5 +34,5 @@ public interface AccessionExchangeRepository extends JpaRepository<AccessionExch
void deleteForAccessions(Collection<Long> accessionIds);
@Query("from AccessionExchange ae where ae.accession.id in ?1")
List<AccessionExchange> findAllFor(List<Long> accessionIds);
List<AccessionExchange> findAllFor(Collection<Long> accessionIds);
}
......@@ -38,6 +38,6 @@ public interface AccessionGeoRepository extends JpaRepository<AccessionGeo, Long
void deleteForAccessions(Collection<Long> accessionIds);
@Query("from AccessionGeo ag where ag.accession.id in ?1")
List<AccessionGeo> findAllFor(List<Long> accessionIds);
List<AccessionGeo> findAllFor(Collection<Long> accessionIds);
}
......@@ -38,5 +38,5 @@ public interface AccessionRemarkRepository extends JpaRepository<AccessionRemark
void deleteForAccessions(@Param("ids") Collection<Long> accessionIds);
@Query("from AccessionRemark ar where ar.accession.id in ?1")
List<AccessionRemark> findAllFor(List<Long> accessionIds);
List<AccessionRemark> findAllFor(Collection<Long> accessionIds);
}
......@@ -62,6 +62,8 @@ public interface AccessionRepository extends JpaRepository<Accession, Long> {
List<Accession> findByInstituteAndAccessionName(FaoInstitute faoInstitute, String accessionName);
public List<Accession> findByInstituteAndAccessionName(FaoInstitute institute, List<String> acceNumbs);
@Query("from Accession a where a.id in ( ?1 )")
Page<Accession> findById(Collection<Long> accessionIds, Pageable pageable);
......@@ -75,6 +77,7 @@ public interface AccessionRepository extends JpaRepository<Accession, Long> {
@Query("select a from Accession a where a.taxonomy in ( ?1 )")
Page<Accession> findByTaxonomy(Collection<Taxonomy2> taxonomies, Pageable pageable);
@Query("select a from Accession a where a.institute.code = ?1 and a.accessionName = ?2")
Accession findByInstituteCodeAndAccessionName(String instCode, String accessionName);
@Query("select a from Accession a where a.accessionName=?2 and (a.institute.code=?1 or a.institute.codeSGSV=?1) and a.taxonomy.genus=?3")
......
......@@ -16,6 +16,8 @@
package org.genesys2.server.persistence.domain;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.genesys2.server.model.genesys.SvalbardData;
......@@ -29,4 +31,7 @@ public interface SvalbardRepository extends JpaRepository<SvalbardData, Long> {
@Query("delete from SvalbardData sd where sd.id in ?1")
void deleteForAccessions(Set<Long> accessionIds);
@Query("from SvalbardData sd where sd.id in ?1")
List<SvalbardData> findAllFor(Collection<Long> accessionIds);
}
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