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

com.kasinf.framework.rest.repository.BaseTreeRepositoryImpl Maven / Gradle / Ivy

The newest version!
package com.kasinf.framework.rest.repository;

import com.kasinf.framework.rest.eneity.type.LogicDeletable;
import com.kasinf.framework.rest.eneity.type.TreeEntity;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.util.Assert;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.io.Serializable;

/**
 * @author lkhsh
 */
public class BaseTreeRepositoryImpl extends BaseRepositoryImpl {

    private final String DELETE_CHILDREN_QL;
    private final String LOGIC_DELETE_CHILDREN_QL;
    private final String LOGIC_DELETE_QL;

    public BaseTreeRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) {
        super(entityInformation, entityManager);
        LOGIC_DELETE_QL = String.format("update %s x set x.isDelete=true where x.parentIds like :parentIds", entityName);
        DELETE_CHILDREN_QL = String.format("delete from %s x where x.parentIds like :parentIds", entityName);
        if (super.logicDelete != null) {
            LOGIC_DELETE_CHILDREN_QL = String.format("update %s x set x.%s=:deleteValue where x.parentIds like :parentIds", entityName, super.logicDelete.of());
        } else {
            LOGIC_DELETE_CHILDREN_QL = null;
        }
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public void delete(T t) {
        Assert.notNull(t, "Entity must not be null!");
        Query query;
        if (logicDelete != null) {
            query = em.createQuery(LOGIC_DELETE_CHILDREN_QL);
            query.setParameter("deleteValue", deleteValue);
        }else if (t instanceof LogicDeletable) {
            query = em.createQuery(LOGIC_DELETE_QL);
        } else {
            query = em.createQuery(DELETE_CHILDREN_QL);
        }
        query.setParameter("parentIds", "%" + t.getParentIds() + "%");
        query.executeUpdate();
    }

    @Override
    public void deleteInBatch(Iterable entities) {
        entities.forEach(this::delete);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy