SubsetAccessionRefRepositoryCustomImpl.java

/*
 * Copyright 2018 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.QSubsetAccessionRef;
import org.genesys.server.model.impl.Subset;
import org.genesys.server.model.impl.SubsetAccessionRef;
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.google.common.collect.Lists;
import com.querydsl.jpa.impl.JPAQuery;

/**
 * The Class SubsetAccessionRefRepositoryCustomImpl.
 */
@Repository
public class SubsetAccessionRefRepositoryCustomImpl extends AccessionRefRepositoryCustomImpl<Subset, SubsetAccessionRef> {

	@Override
	protected Class<SubsetAccessionRef> getAccessionRefType() {
		return SubsetAccessionRef.class;
	}

	@Override
	public Page<SubsetAccessionRef> findByList(Subset list, Pageable page) {
		JPAQuery<SubsetAccessionRef> q = jpaQueryFactory.selectFrom(QSubsetAccessionRef.subsetAccessionRef).where(QSubsetAccessionRef.subsetAccessionRef.list().eq(list));
		long total = q.fetchCount();
		List<SubsetAccessionRef> l = q.orderBy(QSubsetAccessionRef.subsetAccessionRef.accession().seqNo.asc(), QSubsetAccessionRef.subsetAccessionRef.acceNumb.asc()).leftJoin(QSubsetAccessionRef.subsetAccessionRef.accession()).fetchJoin().offset(page.getOffset()).limit(page.getPageSize()).fetch();
		return new PageImpl<>(l, page, total);
	}

	@Override
	public List<AccessionId> findAccessionIdsByList(Subset list) {
		JPAQuery<AccessionId> q = jpaQueryFactory.select(QSubsetAccessionRef.subsetAccessionRef.accession().accessionId())
				.from(QSubsetAccessionRef.subsetAccessionRef)
				.where(QSubsetAccessionRef.subsetAccessionRef.list().eq(list));

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