com.ebiz.modules.persistence.repository.support.MyRepositoryImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.ebiz.modules.persistence.repository.support.MyRepositoryImpl.java

Source

/*
 * Copyright 2002-2012 the original author or authors.
 *
 * 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 com.ebiz.modules.persistence.repository.support;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.commons.beanutils.ConvertUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

import com.ebiz.modules.persistence.annotation.LogicallyDelete;
import com.ebiz.modules.persistence.repository.MyRepository;

public class MyRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
        implements MyRepository<T, ID> {

    private static final Logger logger = LoggerFactory.getLogger(MyRepositoryImpl.class);

    private final EntityManager em;
    private JpaEntityInformation<T, ?> ei;

    public MyRepositoryImpl(Class<T> domainClass, EntityManager em) {
        super(domainClass, em);
        this.em = em;
        logger.trace("----->MyRepositoryImpl.MyRepositoryImpl(Class<T> domainClass, EntityManager em)");
    }

    public MyRepositoryImpl(JpaEntityInformation<T, ?> ei, EntityManager em) {
        super(ei, em);
        this.ei = ei;
        this.em = em;
        logger.trace("----->MyRepositoryImpl.MyRepositoryImpl(JpaEntityInformation<T, ?> ei, EntityManager em)");
    }

    @Override
    @Transactional
    public void delete(T entity) {
        logger.trace("----->MyRepositoryImpl.delete(T entity)");
        Assert.notNull(entity, "The entity must not be null!");
        Class<?> clazz = entity.getClass();
        if (clazz.isAnnotationPresent(LogicallyDelete.class)) {
            LogicallyDelete logicallyDelete = clazz.getAnnotation(LogicallyDelete.class);
            Object value = ConvertUtils.convert(logicallyDelete.value(), logicallyDelete.type().getClazz());
            Field field = ReflectionUtils.findField(entity.getClass(), logicallyDelete.name());
            ReflectionUtils.makeAccessible(field);
            ReflectionUtils.setField(field, entity, value);
            save(entity);
        } else {
            super.delete(entity);
        }
    }

    @Override
    @Transactional
    public void delete(ID id) {
        logger.trace("----->MyRepositoryImpl.deletedelete(ID id)");
        Assert.notNull(id, "The given id must not be null!");

        T entity = findOne(id);

        if (entity == null) {
            throw new EmptyResultDataAccessException(
                    String.format("No %s entity with id %s exists!", ei.getJavaType(), id), 1);
        }

        this.delete(entity);
    }

    @Override
    @Transactional
    public void delete(Iterable<? extends T> entities) {
        logger.trace("----->MyRepositoryImpl.delete(Iterable<? extends T> entities)");
        Assert.notNull(entities, "The given Iterable of entities not be null!");

        for (T entity : entities) {
            this.delete(entity);
        }
    }

    @Override
    @Transactional
    public void deleteInBatch(Iterable<T> entities) {
        logger.warn("this method is not implent logically delete!!!");
        super.deleteInBatch(entities);
    }

    @Override
    @Transactional
    public void deleteAll() {
        logger.trace("----->MyRepositoryImpl.deleteAll()");
        for (T entity : findAll()) {
            this.delete(entity);
        }
    }

    @Override
    @Transactional
    public void deleteAllInBatch() {
        logger.warn("this method is not implent logically delete!!!");
        super.deleteAllInBatch();
    }

    @Override
    @Transactional
    public void delete(ID[] ids) {
        logger.trace("----->MyRepositoryImpl.delete(ID[] ids)");
        String qlString;
        Class<T> clazz = ei.getJavaType();
        if (clazz.isAnnotationPresent(LogicallyDelete.class)) {
            LogicallyDelete logicallyDelete = clazz.getAnnotation(LogicallyDelete.class);
            Object value = ConvertUtils.convert(logicallyDelete.value(), logicallyDelete.type().getClazz());
            qlString = String.format(STATE_DELETE_QUERY_STRING, ei.getEntityName(), logicallyDelete.name(), value);
        } else {
            qlString = String.format(DELETE_QUERY_STRING, ei.getEntityName());
        }
        logger.debug("......qlString:={}", qlString);
        Query query = em.createQuery(qlString);
        query.setParameter(1, Arrays.asList(ids));
        query.executeUpdate();
    }
}