GenotypeRepositoryCustomImpl.java
/*
* Copyright 2025 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.persistence;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.genesys.AccessionId;
import org.genesys.server.model.impl.FaoInstitute;
import org.genesys.server.model.impl.Genotype;
import org.genesys.server.model.impl.QGenotype;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import com.querydsl.jpa.impl.JPAQuery;
@Repository
public class GenotypeRepositoryCustomImpl extends AccessionRefRepositoryCustomImpl<FaoInstitute, Genotype> implements GenotypeRepositoryCustom {
@Override
protected Class<Genotype> getAccessionRefType() {
return Genotype.class;
}
@Override
public Page<Genotype> findByList(FaoInstitute list, Pageable page) {
JPAQuery<Genotype> q = jpaQueryFactory.selectFrom(QGenotype.genotype).where(QGenotype.genotype.list().eq(list));
long total = jpaQueryFactory.from(QGenotype.genotype).where(QGenotype.genotype.list().eq(list)).select(QGenotype.genotype.id.count()).fetchOne();
List<Genotype> l = q.orderBy(QGenotype.genotype.accession().seqNo.asc(), QGenotype.genotype.acceNumb.asc()).leftJoin(QGenotype.genotype.accession()).fetchJoin().offset(page.getOffset()).limit(page.getPageSize()).fetch();
return new PageImpl<>(l, page, total);
}
@Override
public List<AccessionId> findAccessionIdsByList(FaoInstitute list) {
JPAQuery<AccessionId> q = jpaQueryFactory.select(QGenotype.genotype.accession().accessionId())
.from(QGenotype.genotype)
.where(QGenotype.genotype.list().eq(list));
return q.fetch();
}
@Override
public Map<Long, Long> countByAccessions(Collection<Accession> accessions) {
var q = jpaQueryFactory
.select(QGenotype.genotype.accession().id, QGenotype.genotype.id.count())
.from(QGenotype.genotype)
.groupBy(QGenotype.genotype.accession())
.where(QGenotype.genotype.accession().in(accessions));
return q.stream().collect(Collectors.toMap(
tuple -> tuple.get(0, Long.class),
tuple -> tuple.get(1, Long.class)
));
}
}