1 4 package org.roller.business.hibernate; 5 import net.sf.hibernate.Criteria; 6 import net.sf.hibernate.HibernateException; 7 import net.sf.hibernate.Session; 8 import net.sf.hibernate.expression.Expression; 9 import org.roller.RollerException; 10 import org.roller.business.BookmarkManagerImpl; 11 import org.roller.business.PersistenceStrategy; 12 import org.roller.pojos.Assoc; 13 import org.roller.pojos.BookmarkData; 14 import org.roller.pojos.FolderAssoc; 15 import org.roller.pojos.FolderData; 16 import org.roller.pojos.WebsiteData; 17 import java.util.Iterator ; 18 import java.util.LinkedList ; 19 import java.util.List ; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 28 public class HibernateBookmarkManagerImpl extends BookmarkManagerImpl 29 { 30 static final long serialVersionUID = 5286654557062382772L; 31 32 private static Log mLogger = 33 LogFactory.getFactory().getInstance(HibernateBookmarkManagerImpl.class); 34 35 39 public HibernateBookmarkManagerImpl(PersistenceStrategy pstrategy) 40 { 41 super(pstrategy); 42 mLogger.debug("Instantiating Bookmark Manager"); 43 } 44 45 49 public List retrieveBookmarks(FolderData folder, boolean subfolders) 50 throws RollerException 51 { 52 try 53 { 54 Session session = ((HibernateStrategy) mStrategy).getSession(); 55 List bookmarks = new LinkedList (); 56 if (subfolders) 57 { 58 Criteria assocsQuery = session 60 .createCriteria(FolderAssoc.class); 61 assocsQuery.add(Expression.eq("ancestorFolder", folder)); 62 Iterator assocs = assocsQuery.list().iterator(); 63 while (assocs.hasNext()) 64 { 65 FolderAssoc assoc = (FolderAssoc) assocs.next(); 66 Criteria bookmarksQuery = session 67 .createCriteria(BookmarkData.class); 68 bookmarksQuery.add(Expression.eq("folder", assoc 69 .getFolder())); 70 Iterator bookmarkIter = bookmarksQuery.list().iterator(); 71 while (bookmarkIter.hasNext()) 72 { 73 BookmarkData entry = (BookmarkData) bookmarkIter.next(); 74 bookmarks.add(entry); 75 } 76 } 77 } 78 Criteria bookmarksQuery = session 80 .createCriteria(BookmarkData.class); 81 bookmarksQuery.add(Expression.eq("folder", folder)); 82 Iterator bookmarkIter = bookmarksQuery.list().iterator(); 83 while (bookmarkIter.hasNext()) 84 { 85 BookmarkData bookmark = (BookmarkData) bookmarkIter.next(); 86 bookmarks.add(bookmark); 87 } 88 return bookmarks; 89 } 90 catch (HibernateException e) 91 { 92 throw new RollerException(e); 93 } 94 } 95 96 public FolderData getRootFolder(WebsiteData website) throws RollerException 97 { 98 if (website == null) 99 throw new RollerException("website is null"); 100 try 101 { 102 Session session = ((HibernateStrategy) mStrategy).getSession(); 103 Criteria criteria = session.createCriteria(FolderAssoc.class); 104 criteria.createAlias("folder", "f"); 105 criteria.add(Expression.eq("f.website", website)); 106 criteria.add(Expression.isNull("ancestorFolder")); 107 criteria.add(Expression.eq("relation", FolderAssoc.PARENT)); 108 List results = criteria.list(); 109 if (results.size() > 1) 110 { 111 throw new RollerException( 113 "More than one root folder found for website " 114 + website.getId()); 115 } 116 else if (results.size() == 1) 117 { 118 return ((FolderAssoc) results.get(0)).getFolder(); 120 } 121 return null; 122 } 123 catch (HibernateException e) 124 { 125 throw new RollerException(e); 126 } 127 } 128 129 public List getAllFolders(WebsiteData website) throws RollerException 130 { 131 if (website == null) 132 throw new RollerException("Website is null"); 133 134 try 135 { 136 Session session = ((HibernateStrategy) mStrategy).getSession(); 137 Criteria criteria = session.createCriteria(FolderData.class); 138 criteria.add(Expression.eq("website", website)); 139 return criteria.list(); 140 } 141 catch (HibernateException e) 142 { 143 throw new RollerException(e); 144 } 145 146 } 147 148 151 public boolean isDuplicateFolderName(FolderData folder) throws RollerException 152 { 153 boolean isNewFolder = (folder.getId() == null); 155 FolderData parent = 156 isNewFolder ? (FolderData)folder.getNewParent() : folder.getParent(); 157 158 if (null != parent) 159 { 160 List sameNames; 161 try 162 { 163 Session session = ((HibernateStrategy) mStrategy).getSession(); 164 Criteria criteria = session.createCriteria(FolderAssoc.class); 165 criteria.createAlias("folder", "f"); 166 criteria.add(Expression.eq("f.name", folder.getName())); 167 criteria.add(Expression.eq("ancestorFolder", parent)); 168 criteria.add(Expression.eq("relation", Assoc.PARENT)); 169 sameNames = criteria.list(); 170 } 171 catch (HibernateException e) 172 { 173 throw new RollerException(e); 174 } 175 if (sameNames.size() > 0) 177 { 178 if (isNewFolder) return true; 180 if (!(sameNames.size() == 1 && folder.getId().equals(((FolderAssoc)sameNames.get(0)).getFolder().getId()))) 182 return true; 183 } 184 } 185 return false; 186 } 187 188 192 public Assoc getFolderParentAssoc(FolderData folder) throws RollerException 193 { 194 try 195 { 196 Session session = ((HibernateStrategy)mStrategy).getSession(); 197 Criteria criteria = session.createCriteria(FolderAssoc.class); 198 criteria.add(Expression.eq("folder", folder)); 199 criteria.add(Expression.eq("relation", Assoc.PARENT)); 200 List parents = criteria.list(); 201 if (parents.size() > 1) 202 { 203 throw new RollerException("ERROR: more than one parent"); 204 } 205 else if (parents.size() == 1) 206 { 207 return (Assoc) parents.get(0); 208 } 209 else 210 { 211 return null; 212 } 213 } 214 catch (HibernateException e) 215 { 216 throw new RollerException(e); 217 } 218 } 219 220 224 public List getFolderChildAssocs(FolderData folder) throws RollerException 225 { 226 try 227 { 228 Session session = ((HibernateStrategy)mStrategy).getSession(); 229 Criteria criteria = session.createCriteria(FolderAssoc.class); 230 criteria.add(Expression.eq("ancestorFolder", folder)); 231 criteria.add(Expression.eq("relation", Assoc.PARENT)); 232 return criteria.list(); 233 } 234 catch (HibernateException e) 235 { 236 throw new RollerException(e); 237 } 238 } 239 240 244 public List getAllFolderDecscendentAssocs(FolderData folder) throws RollerException 245 { 246 try 247 { 248 Session session = ((HibernateStrategy)mStrategy).getSession(); 249 Criteria criteria = session.createCriteria(FolderAssoc.class); 250 criteria.add(Expression.eq("ancestorFolder", folder)); 251 return criteria.list(); 252 } 253 catch (HibernateException e) 254 { 255 throw new RollerException(e); 256 } 257 } 258 259 263 public List getFolderAncestorAssocs(FolderData folder) throws RollerException 264 { 265 try 266 { 267 Session session = ((HibernateStrategy)mStrategy).getSession(); 268 Criteria criteria = session.createCriteria(FolderAssoc.class); 269 criteria.add(Expression.eq("folder", folder)); 270 return criteria.list(); 271 } 272 catch (HibernateException e) 273 { 274 throw new RollerException(e); 275 } 276 } 277 278 281 public boolean isFolderInUse(FolderData folder) throws RollerException 282 { 283 try 284 { 285 288 Session session = ((HibernateStrategy)mStrategy).getSession(); 294 Criteria criteria = session.createCriteria(BookmarkData.class); 295 criteria.add(Expression.eq("folder", folder)); 296 criteria.setMaxResults(1); 297 int entryCount = criteria.list().size(); 298 299 return (entryCount > 0 || folder.getFolders().size() > 0); 301 } 302 catch (HibernateException e) 303 { 304 throw new RollerException(e); 305 } 306 } 307 308 public boolean isDescendentOf(FolderData child, FolderData ancestor) 309 throws RollerException 310 { 311 boolean ret = false; 312 try 313 { 314 Session session = ((HibernateStrategy)mStrategy).getSession(); 315 Criteria criteria = session.createCriteria(FolderAssoc.class); 316 criteria.add(Expression.eq("folder", child)); 317 criteria.add(Expression.eq("ancestorFolder", ancestor)); 318 ret = criteria.list().size() > 0; 319 } 320 catch (HibernateException e) 321 { 322 throw new RollerException(e); 323 } 324 return ret; 325 } 326 } 327 | Popular Tags |