// Path: chapter09/src/main/java/com/packtpub/pf/blueprint/persistence/HibernateUtil.java
// public class HibernateUtil {
// 
//     private static final SessionFactory sessionFactory = buildSessionFactory();
// 
//     private static SessionFactory buildSessionFactory() throws HibernateException {
//         Configuration configuration = new Configuration().configure();
//         // configures settings from hibernate.cfg.xml
// 
//         StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
// 
//         // If you miss the below line then it will complain about a missing dialect setting
//         serviceRegistryBuilder.applySettings(configuration.getProperties());
// 
//         ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
//         return configuration.buildSessionFactory(serviceRegistry);
//     }
// 
//     public static SessionFactory getSessionFactory() {
//         return sessionFactory;
//     }
// 
//     public static void shutdown() {
//         // Close caches and connection pools
//         getSessionFactory().close();
//     }
// 
// }


import com.packtpub.pf.blueprint.persistence.HibernateUtil;
import com.packtpub.pf.blueprint.persistence.entity.*;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import java.util.ArrayList;
import java.util.List;

    public List<String> getTagsStartWith(String chars){
        List<String> list = new ArrayList<>();
        org.hibernate.Transaction tx = getSession().beginTransaction();
        List tags = getSession().createCriteria(Tags.class)
                .add(Restrictions.ilike("name", chars))
                .addOrder(Order.asc("name")).list();
        tx.commit();
        getSession().close();
        if(tags != null){
            for (Tags t: (List<Tags>) tags){
               list.add(t.getName());
            }
        }
        _log.info("Listed Successfully....");
        return list;
    }

    public Tags getTagByName(String name){
        org.hibernate.Transaction tx = getSession().beginTransaction();
        Tags tag = (Tags) getSession().createCriteria(Tags.class)
                .add(Restrictions.eq("name", name).ignoreCase())
                .uniqueResult();
        tx.commit();
        getSession().close();
        _log.info("Listed Successfully....");
        return tag;
    }

    private Session getSession() {
// Path: chapter09/src/main/java/com/packtpub/pf/blueprint/persistence/HibernateUtil.java
// public class HibernateUtil {
// 
//     private static final SessionFactory sessionFactory = buildSessionFactory();
// 
//     private static SessionFactory buildSessionFactory() throws HibernateException {
//         Configuration configuration = new Configuration().configure();
//         // configures settings from hibernate.cfg.xml
// 
//         StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
// 
//         // If you miss the below line then it will complain about a missing dialect setting
//         serviceRegistryBuilder.applySettings(configuration.getProperties());
// 
//         ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
//         return configuration.buildSessionFactory(serviceRegistry);
//     }
// 
//     public static SessionFactory getSessionFactory() {
//         return sessionFactory;
//     }
// 
//     public static void shutdown() {
//         // Close caches and connection pools
//         getSessionFactory().close();
//     }
// 
// }


// Path: chapter07/src/main/java/com/packtpub/pf/blueprint/service/DAOService.java
import com.packtpub.pf.blueprint.persistence.HibernateUtil;
import com.packtpub.pf.blueprint.persistence.entity.*;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import java.util.ArrayList;
import java.util.List;


    public List<String> getTagsStartWith(String chars){
        List<String> list = new ArrayList<>();
        org.hibernate.Transaction tx = getSession().beginTransaction();
        List tags = getSession().createCriteria(Tags.class)
                .add(Restrictions.ilike("name", chars))
                .addOrder(Order.asc("name")).list();
        tx.commit();
        getSession().close();
        if(tags != null){
            for (Tags t: (List<Tags>) tags){
               list.add(t.getName());
            }
        }
        _log.info("Listed Successfully....");
        return list;
    }

    public Tags getTagByName(String name){
        org.hibernate.Transaction tx = getSession().beginTransaction();
        Tags tag = (Tags) getSession().createCriteria(Tags.class)
                .add(Restrictions.eq("name", name).ignoreCase())
                .uniqueResult();
        tx.commit();
        getSession().close();
        _log.info("Listed Successfully....");
        return tag;
    }

    private Session getSession() {
        SessionFactory sf = HibernateUtil.getSessionFactory();