cz.swi2.mendeluis.dataaccesslayer.repository.BaseRepository.java Source code

Java tutorial

Introduction

Here is the source code for cz.swi2.mendeluis.dataaccesslayer.repository.BaseRepository.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.swi2.mendeluis.dataaccesslayer.repository;

import cz.swi2.mendeluis.dataaccesslayer.domain.IEntity;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Table;
import org.springframework.stereotype.Repository;

/**
 * Zkladn repozit; vyuv soft-delete - tzn. pimzn nedojde k vymazn
 * ale pouze ke zmn pznaku (true ve sloupci deleted)
 *
 * @author Roman
 * @param <TEntity> Entita, kter implementuje IEntity
 */
@Repository
public class BaseRepository<TEntity extends IEntity> /*implements IRepository<TEntity>*/ {

    /**
     * Datov typ (tda) aktuln pouitho typu - doplnno v konstruktoru pi
     * vytvoen instance repozite
     */
    protected Class<TEntity> tEntityType;

    /**
     * EntityManager pro manipulaci s entitami
     */
    @PersistenceContext
    EntityManager entityManager;

    /**
     * Vrt konkrtn entitu dle ID
     *
     * @param id Id entity
     * @return IEntity
     */
    public TEntity getById(int id) {
        TEntity entity = this.entityManager.find(this.tEntityType, id);

        if (entity.getIsDeleted()) {
            return null;
        }

        return entity;
    }

    /**
     * Vrt kolekci vech entit
     *
     * @return
     */
    public List<TEntity> getAll() {
        String entityName = getEntityName();
        return this.entityManager
                .createQuery("SELECT e FROM " + entityName + " e WHERE e.isDeleted = 0", this.tEntityType)
                .getResultList();
    }

    /**
     * Vrati nazev entity.
     * @return 
     */
    protected String getEntityName() {
        String entityName = this.tEntityType.getSimpleName();
        return entityName;
    }

    /**
     * Vrati nazev tabulky v db pro danou entitu. 
     * @return string nazev tabulky
     */
    protected String getTableName() {
        String tableName = this.tEntityType.getSimpleName();

        // Pokud m entita anotaci Table, na?teme definovan jmno
        Table tableAnotation = this.tEntityType.getAnnotation(Table.class);

        //Ovme existenci anotace a validitu jmna
        if (tableAnotation != null) {
            String anotName = tableAnotation.name();

            if (anotName.trim().length() > 0) {
                tableName = anotName;
            }
        }

        // make first letter upper
        tableName = Character.toString(tableName.charAt(0)).toUpperCase() + tableName.substring(1);

        return tableName;
    }

    /**
     * prava existujc entity
     *
     * @param entity
     */
    public void update(TEntity entity) {
        this.entityManager.merge(entity);
    }

    /**
     * Zape novou entitu do databze, po vloen bude do entity doplnno z DB
     * vygenerovan ID
     *
     * @param entity
     */
    public void insert(TEntity entity) {
        this.entityManager.persist(entity);
        this.entityManager.flush();
    }

    /**
     * Vymae entitu
     * @param entity 
     */
    public void delete(TEntity entity) {
        entity.setIsDeleted(true);
        this.update(entity);
    }

    /**
     * Vymae entitu dle ID
     * @param id 
     */
    public void delete(int id) {
        TEntity entity = this.getById(id);

        if (entity != null) {
            this.delete(entity);
        }
    }

    /**
     * Vymaze vsechny trvale z db. Pouzito pro vycisteni db pred nahranim testovacich dat. 
     */
    public void deleteAllPernamently() {
        String hql = String.format("delete from %s", this.getEntityName());
        Query query = this.entityManager.createQuery(hql);
        query.executeUpdate();
    }
}