DatasetAccessionRefRepositoryCustomImpl.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.dataset;

import java.util.List;

import org.genesys.server.model.dataset.Dataset;
import org.genesys.server.model.dataset.DatasetAccessionRef;
import org.genesys.server.model.dataset.QDatasetAccessionRef;
import org.genesys.server.model.genesys.AccessionId;
import org.genesys.server.persistence.AccessionRefRepositoryCustomImpl;
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 DatasetAccessionRefRepositoryCustomImpl extends AccessionRefRepositoryCustomImpl<Dataset, DatasetAccessionRef> {

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

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

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

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