persistence.AuthorDao.java Source code

Java tutorial

Introduction

Here is the source code for persistence.AuthorDao.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 persistence;

import entity.Author;
import entity.Branch;
import entity.Direction;
import entity.Order;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import persistence.parent.Dao;
import support.CollectionUtils;
import support.StringAdapter;

/**
 *
 * @author Rice Pavel
 */
@Repository("authorDao")
public class AuthorDao extends Dao<Author> {

    public Class getSupportedClass() {
        return Author.class;
    }

    public List<Author> search(String name, String surname, String middlename, String email, String phone,
            String login, Direction direction, String active) {
        Criteria crit = currentSession().createCriteria(Author.class);
        if (StringAdapter.NotNull(name)) {
            crit.add(Restrictions.like("name", "%" + name + "%"));
        }
        if (StringAdapter.NotNull(surname)) {
            crit.add(Restrictions.like("surname", "%" + surname + "%"));
        }
        if (StringAdapter.NotNull(middlename)) {
            crit.add(Restrictions.like("middlename", "%" + middlename + "%"));
        }
        if (StringAdapter.NotNull(phone)) {
            crit.add(Restrictions.like("phone", "%" + phone + "%"));
        }
        if (StringAdapter.NotNull(login)) {
            crit.add(Restrictions.like("login", "%" + login + "%"));
        }
        if (direction != null) {
            crit.createAlias("directions", "d");
            crit.add(Restrictions.eq("d.directionId", direction.getDirectionId()));
        }
        if (StringAdapter.NotNull(active)) {
            boolean activeBoolean = Boolean.parseBoolean(active);
            crit.add(Restrictions.eq("active", activeBoolean));
        }
        crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        List<Author> list = crit.list();
        return list;
    }

    public List<Author> getAuthorsWhichHasMessages(Long orderId) {
        String hql = "select distinct a " + " from Author as a inner join a.authorMessages as am "
                + " where am.order.orderId = :orderId ";
        Query query = currentSession().createQuery(hql);
        query.setParameter("orderId", orderId);
        return query.list();
    }

    public List<Author> getAll() {
        Criteria cr = currentSession().createCriteria(getSupportedClass());
        cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        List<Author> authorList = cr.list();
        return authorList;
    }

}