1 25 26 package com.j2biz.blogunity.dao; 27 28 import java.io.Serializable ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import net.sf.hibernate.Criteria; 33 import net.sf.hibernate.HibernateException; 34 import net.sf.hibernate.Query; 35 import net.sf.hibernate.Session; 36 import net.sf.hibernate.expression.Expression; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 import com.j2biz.blogunity.exception.BlogunityException; 42 import com.j2biz.blogunity.i18n.I18N; 43 import com.j2biz.blogunity.i18n.I18NStatusFactory; 44 import com.j2biz.blogunity.pojo.Category; 45 import com.j2biz.blogunity.util.HibernateUtil; 46 47 54 public class CategoryDAO extends AbstractDAO { 55 56 private static final Log log = LogFactory.getLog(CategoryDAO.class); 57 58 61 public CategoryDAO() { 62 super(); 63 } 64 65 70 public Category getCategoryByID(long catId) throws BlogunityException { 71 return getCategoryByID(new Long (catId)); 72 } 73 74 79 public Category getCategoryByID(Long catId) throws BlogunityException { 80 Session session = HibernateUtil.getSession(); 81 Category blogCategory = null; 82 try { 83 84 Criteria criteria = session.createCriteria(Category.class).add( 85 Expression.eq("id", catId)); 86 87 return (Category) criteria.uniqueResult(); 88 89 } catch (HibernateException ex) { 90 log.error("getCategoryByID(id)", ex); 91 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_ID, 92 new String []{"Category"}, ex)); 93 } 94 } 95 96 100 public List getGlobalCategories() throws BlogunityException { 101 Session session = HibernateUtil.getSession(); 102 List categories; 103 try { 104 105 Query q = session.createQuery("from Category c where c.type = :type"); 106 q.setInteger("type", Category.GLOBAL); 107 108 return q.list(); 109 110 } catch (HibernateException ex) { 111 112 log.error("getGlobalCategories()", ex); 113 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FETCH_PAGINATED_LIST, 114 new String []{"Categories"}, ex)); 115 116 } 117 } 118 119 123 public boolean existsGlobalCategoryWithName(String name) { 124 125 List categories; 126 try { 127 categories = getGlobalCategories(); 128 } catch (BlogunityException e) { 129 return false; 130 } 131 132 for (Iterator i = categories.iterator(); i.hasNext();) { 133 Category c = (Category) i.next(); 134 if (c.getName().equals(name)) return true; 135 } 136 137 return false; 138 } 139 140 145 public Category getGlobalCategoryByName(String name) throws BlogunityException { 146 147 Session session = HibernateUtil.getSession(); 148 try { 149 Criteria criteria = session.createCriteria(Category.class); 152 criteria.add(Expression.eq("type", new Integer (Category.GLOBAL))); 153 criteria.add(Expression.eq("name", name)); 154 155 return (Category) criteria.uniqueResult(); 156 157 } catch (HibernateException ex) { 158 log.error("getCategoryByName(String)", ex); 159 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_NAME, 160 new String []{"Category"}, ex)); 161 162 } 163 164 } 165 166 171 public Serializable createCategory(Category category) throws BlogunityException { 172 Session session = HibernateUtil.getSession(); 173 try { 174 return session.save(category); 175 } catch (HibernateException e) { 176 177 log.error("createCategory(category)", e); 178 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.CREATE, 179 new String []{"Category"}, e)); 180 181 } 182 } 183 184 188 public void deleteCategory(Category category) throws BlogunityException { 189 Session session = HibernateUtil.getSession(); 190 try { 191 session.delete(category); 192 } catch (HibernateException e) { 193 194 log.error("deleteCategory(category)", e); 195 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE, 196 new String []{"Category"}, e)); 197 198 } 199 } 200 201 205 public void updateCategory(Category category) throws BlogunityException { 206 Session session = HibernateUtil.getSession(); 207 try { 208 session.update(category); 209 } catch (HibernateException e) { 210 211 log.error("updateCategory(category)", e); 212 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UPDATE, 213 new String []{"Category"}, e)); 214 } 215 } 216 } | Popular Tags |