DiversityTreeAccessionRefRepositoryCustomImpl.java

/*
 * Copyright 2020 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.List;

import org.genesys.server.model.genesys.AccessionId;
import org.genesys.server.model.impl.DiversityTree;
import org.genesys.server.model.impl.DiversityTreeAccessionRef;
import org.genesys.server.model.impl.QDiversityTreeAccessionRef;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.google.common.collect.Lists;
import com.querydsl.jpa.impl.JPAQuery;

/**
 * The Class DiversityTreeAccessionRefRepositoryCustomImpl.
 */
public class DiversityTreeAccessionRefRepositoryCustomImpl extends AccessionRefRepositoryCustomImpl<DiversityTree, DiversityTreeAccessionRef> {
	@Override
	protected Class<DiversityTreeAccessionRef> getAccessionRefType() {
		return DiversityTreeAccessionRef.class;
	}

	@Override
	public Page<DiversityTreeAccessionRef> findByList(DiversityTree list, Pageable page) {
		final JPAQuery<DiversityTreeAccessionRef> q = jpaQueryFactory.selectFrom(QDiversityTreeAccessionRef.diversityTreeAccessionRef)
				.where(QDiversityTreeAccessionRef.diversityTreeAccessionRef.list().eq(list));

		long total = q.fetchCount();
		List<DiversityTreeAccessionRef> l = q.orderBy(QDiversityTreeAccessionRef.diversityTreeAccessionRef.accession().seqNo.asc(), QDiversityTreeAccessionRef.diversityTreeAccessionRef.acceNumb.asc())
				.leftJoin(QDiversityTreeAccessionRef.diversityTreeAccessionRef.accession()).fetchJoin()
				.offset(page.getOffset())
				.limit(page.getPageSize()).fetch();

		return new PageImpl<>(l, page, total);
	}

	@Override
	public List<AccessionId> findAccessionIdsByList(DiversityTree list) {
		final JPAQuery<AccessionId> q = jpaQueryFactory.select(QDiversityTreeAccessionRef.diversityTreeAccessionRef.accession().accessionId())
				.from(QDiversityTreeAccessionRef.diversityTreeAccessionRef)
				.where(QDiversityTreeAccessionRef.diversityTreeAccessionRef.list().eq(list));

		return Lists.newArrayList(q.fetch());
	}
}