Package com.gtp.dao

Source Code of com.gtp.dao.AuthoryDaoImp

package com.gtp.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;  
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import com.gtp.domain.Authority;

import commons.ExistedException;
import commons.NotFoundException;
public class AuthoryDaoImp extends JpaDaoSupport implements AuthoryDao
 
  @Override
  public Authority addAuthory(Authority authory) throws ExistedException {
    getJpaTemplate().persist(authory);
    return authory;
  }

  @Override
  public Authority removeAuthroy(Authority a) throws NotFoundException {
    a=getJpaTemplate().merge(a);
    getJpaTemplate().remove(a);
    return a;
  }
 
  @Override
  public Authority updateAuthory(Authority a) throws NotFoundException, ExistedException {
    getJpaTemplate().merge(a);
    return a;
  }

  @Override
  public Authority findByName(final String name) {
    return (Authority)getJpaTemplate().execute(new JpaCallback<Authority>() {
      public Authority doInJpa(EntityManager em) {
        CriteriaBuilder criteriaBuilder =em.getCriteriaBuilder();
        CriteriaQuery<Authority> query= criteriaBuilder.createQuery(Authority.class);
        Root<Authority> auth = query.from(Authority.class);
        Authority authory=null;
        query.select(auth).where(criteriaBuilder.equal(auth.get("name"),name));
        List<Authority> result = em.createQuery(query).getResultList();
        if(result.isEmpty())
          return null;
        authory=(Authority) result.get(0);
        return authory;
      }
      });
  }
 
  @Override
  public List<Authority> findAllAuthories() {
    return (List<Authority>)getJpaTemplate().execute(new JpaCallback<List<Authority>>() {
      public List<Authority> doInJpa(EntityManager em) {
        CriteriaBuilder criteriaBuilder =em.getCriteriaBuilder();
        CriteriaQuery<Authority> query= criteriaBuilder.createQuery(Authority.class);
        Root<Authority> auth = query.from(Authority.class);
        query.select(auth);
        List<Authority> result = em.createQuery(query).getResultList();
        return  result;
      }
      });
  }

  @Override
  public Authority find(int id) {
    return getJpaTemplate().find(Authority.class, id);
  }

  @Override
  public Authority findByUrl(final String url) {
    return (Authority)getJpaTemplate().execute(new JpaCallback<Authority>() {
      public Authority doInJpa(EntityManager em) {
        CriteriaBuilder criteriaBuilder =em.getCriteriaBuilder();
        CriteriaQuery<Authority> query= criteriaBuilder.createQuery(Authority.class);
        Root<Authority> auth = query.from(Authority.class);
        Authority authory=null;
        query.select(auth).where(criteriaBuilder.equal(auth.get("url"),url));
        List<Authority> result = em.createQuery(query).getResultList();
        if(result.isEmpty())
          return null;
        authory=(Authority) result.get(0);
        return authory;
      }
      });
   
  }
}
TOP

Related Classes of com.gtp.dao.AuthoryDaoImp

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.