All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.developframework.resource.spring.jpa.JpaResourceCacheManager Maven / Gradle / Ivy

The newest version!
package com.github.developframework.resource.spring.jpa;

import com.github.developframework.resource.*;
import com.github.developframework.resource.spring.SpringDataResourceCacheManager;
import com.github.developframework.resource.spring.cache.CacheType;
import com.github.developframework.resource.utils.ResourceAssert;
import develop.toolkit.base.utils.CollectionAdvice;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.io.Serializable;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * @author qiushui on 2020-04-30.
 */
@Getter
public abstract class JpaResourceCacheManager<
        PO extends com.github.developframework.resource.spring.jpa.PO,
        ID extends Serializable,
        REPOSITORY extends PagingAndSortingRepository & JpaSpecificationExecutor
        > extends SpringDataResourceCacheManager {

    @PersistenceContext
    protected EntityManager entityManager;

    public JpaResourceCacheManager(REPOSITORY repository, Class entityClass, String resourceName, String cacheKey, Duration timeout, CacheType cacheType) {
        super(repository, new ResourceDefinition<>(entityClass, resourceName), cacheKey, timeout, cacheType);
    }

    @Autowired
    public void setJpaTransactionManager(JpaTransactionManager jpaTransactionManager) {
        super.transactionTemplate = new TransactionTemplate(jpaTransactionManager);
    }

    @PostConstruct
    public void init() {
        this.resourceHandler = new JpaResourceHandler<>(repository, resourceDefinition, entityManager);
        this.resourceOperateRegistry = new ResourceOperateRegistry<>(this);
    }

    public List listForIds(ID[] ids) {
        return listForIds("id", ids);
    }

    public List listForIds(List ids) {
        return listForIds("id", ids);
    }

    public Optional findOneByIdForUpdate(ID id) {
        return resourceHandler.queryByIdForUpdate(id).map(this::execSearchOperate);
    }

    @SuppressWarnings("unchecked")
    public PO findOneByIdRequiredForUpdate(ID id) {
        return (PO) ResourceAssert
                .resourceExistAssertBuilder(resourceDefinition.getResourceName(), resourceHandler.queryByIdForUpdate(id))
                .addParameter("id", id)
                .returnValue();
    }

    @Override
    public List listForIds(String idProperty, ID[] ids) {
        if (ids.length == 0) {
            return new ArrayList<>();
        }
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery query = builder.createQuery(resourceDefinition.getEntityClass());
        Root root = query.from(resourceDefinition.getEntityClass());
        query.select(root).where(root.get(idProperty).in(ids));
        List list = entityManager.createQuery(query).getResultList();
        return CollectionAdvice.sort(list, ids, (po, id) -> po.getId().equals(id));
    }

    @Override
    public List listForIds(String idProperty, Collection ids) {
        if (ids.isEmpty()) {
            return new ArrayList<>();
        }
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery query = builder.createQuery(resourceDefinition.getEntityClass());
        Root root = query.from(resourceDefinition.getEntityClass());
        query.select(root).where(root.get(idProperty).in(ids));
        List list = entityManager.createQuery(query).getResultList();
        return ids instanceof List ? CollectionAdvice.sort(list, ids, (po, id) -> po.getId().equals(id)) : list;
    }

    @Override
    public  AddCheckExistsLogic byFieldAddCheck(Class dtoClass, String... fields) {
        return new ByFieldJpaAddCheckExistsLogic<>(resourceDefinition, entityManager, fields);
    }

    @Override
    public  ModifyCheckExistsLogic byFieldModifyCheck(Class dtoClass, String... fields) {
        return new ByFieldJpaModifyCheckExistsLogic<>(resourceDefinition, entityManager, fields);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy