AccessionIdRepositoryCustomImpl.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.filerepository.model.RepositoryFolder;
import org.genesys.server.model.genesys.AccessionId;
import org.genesys.server.model.genesys.QAccessionId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.querydsl.core.types.dsl.NumberPath;
import com.querydsl.jpa.impl.JPAQueryFactory;

/**
 * @author Maxym Borodenko
 */
@Repository
public class AccessionIdRepositoryCustomImpl implements AccessionIdRepositoryCustom {

	@Autowired
	private JPAQueryFactory jpaQueryFactory;

	@Override
	@Transactional
	public void increaseDatasetCount(List<AccessionId> accessionIds) {
		increaseCount(accessionIds, QAccessionId.accessionId.datasetCount);
	}

	@Override
	@Transactional
	public void decreaseDatasetCount(List<AccessionId> accessionIds) {
		decreaseCount(accessionIds, QAccessionId.accessionId.datasetCount);
	}

	@Override
	@Transactional
	public void increaseSubsetCount(List<AccessionId> accessionIds) {
		increaseCount(accessionIds, QAccessionId.accessionId.subsetCount);
	}

	@Override
	@Transactional
	public void decreaseSubsetCount(List<AccessionId> accessionIds) {
		decreaseCount(accessionIds, QAccessionId.accessionId.subsetCount);
	}

	@Override
	public void resetSubsetAndDatasetCounters() {
		NumberPath<Long> subsetCount = QAccessionId.accessionId.subsetCount;
		NumberPath<Long> datasetCount = QAccessionId.accessionId.datasetCount;

		jpaQueryFactory.update(QAccessionId.accessionId)
				.where(datasetCount.gt(0).or(subsetCount.gt(0)))
				.set(datasetCount, 0L).set(subsetCount, 0L)
				.execute();
	}

	@Override
	public void updateImageCount(AccessionId accessionId, int imageCount) {
		jpaQueryFactory.update(QAccessionId.accessionId)
				.where(QAccessionId.accessionId.eq(accessionId))
				.set(QAccessionId.accessionId.imageCount, Math.max(imageCount, 0))
				.execute();
	}

	@Override
	public void updateRepositoryFolder(AccessionId accessionId, RepositoryFolder repositoryFolder) {
		jpaQueryFactory.update(QAccessionId.accessionId)
				.where(QAccessionId.accessionId.eq(accessionId))
				.set(QAccessionId.accessionId.repositoryFolder(), repositoryFolder)
				.execute();
	}

	private void decreaseCount(List<AccessionId> accessionIds, NumberPath<Long> path) {
		jpaQueryFactory.update(QAccessionId.accessionId)
				.where(QAccessionId.accessionId.in(accessionIds).and(path.gt(0)))
				.set(path, path.subtract(1))
				.execute();
	}

	private void increaseCount(List<AccessionId> accessionIds, NumberPath<Long> path) {
		jpaQueryFactory.update(QAccessionId.accessionId)
				.where(QAccessionId.accessionId.in(accessionIds))
				.set(path, path.add(1))
				.execute();
	}

}