1 16 17 package org.roller.business.hibernate; 18 19 import java.util.ArrayList ; 20 import java.util.Date ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import net.sf.hibernate.Criteria; 25 import net.sf.hibernate.HibernateException; 26 import net.sf.hibernate.Query; 27 import net.sf.hibernate.Session; 28 import net.sf.hibernate.expression.Expression; 29 import net.sf.hibernate.expression.Order; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.roller.RollerException; 34 import org.roller.business.PersistenceStrategy; 35 import org.roller.business.PlanetManagerImpl; 36 import org.roller.model.Roller; 37 import org.roller.pojos.PlanetConfigData; 38 import org.roller.pojos.PlanetEntryData; 39 import org.roller.pojos.PlanetGroupData; 40 import org.roller.pojos.PlanetGroupSubscriptionAssoc; 41 import org.roller.pojos.PlanetSubscriptionData; 42 43 44 48 public class HibernatePlanetManagerImpl extends PlanetManagerImpl 49 { 50 private static final String NO_GROUP = "zzz_nogroup_zzz"; 51 52 private static Log logger = 53 LogFactory.getFactory().getInstance(HibernatePlanetManagerImpl.class); 54 55 public HibernatePlanetManagerImpl( 56 PersistenceStrategy strategy, Roller roller) 57 { 58 super(strategy, roller); 59 } 60 61 public void saveConfiguration(PlanetConfigData config) 62 throws RollerException 63 { 64 config.save(); 65 } 66 67 public void saveGroup(PlanetGroupData group) throws RollerException 68 { 69 Iterator assocs = group.getGroupSubscriptionAssocs().iterator(); 70 while (assocs.hasNext()) 71 { 72 PlanetGroupSubscriptionAssoc assoc = 73 (PlanetGroupSubscriptionAssoc)assocs.next(); 74 assoc.save(); 75 } 76 group.save(); 77 } 78 79 public void saveEntry(PlanetEntryData entry) throws RollerException 80 { 81 entry.save(); 82 } 83 84 public void saveSubscription(PlanetSubscriptionData sub) 85 throws RollerException 86 { 87 PlanetSubscriptionData existing = getSubscription(sub.getFeedUrl()); 88 if (existing == null || (existing.getId().equals(sub.getId()))) 89 { 90 sub.save(); 91 } 92 else 93 { 94 throw new RollerException("ERROR: duplicate feed URLs not allowed"); 95 } 96 } 97 98 public PlanetConfigData getConfiguration() throws RollerException 99 { 100 try 101 { 102 Session session = ((HibernateStrategy)strategy).getSession(); 103 Criteria criteria = session.createCriteria(PlanetConfigData.class); 104 criteria.setMaxResults(1); 105 List list = criteria.list(); 106 return list.size()!=0 ? (PlanetConfigData)list.get(0) : null; 107 } 108 catch (HibernateException e) 109 { 110 throw new RollerException(e); 111 } 112 } 113 114 public List getGroups() throws RollerException 115 { 116 try 117 { 118 Session session = ((HibernateStrategy)strategy).getSession(); 119 Criteria criteria = session.createCriteria(PlanetGroupData.class); 120 return criteria.list(); 121 } 122 catch (HibernateException e) 123 { 124 throw new RollerException(e); 125 } 126 } 127 128 public List getGroupHandles() throws RollerException 129 { 130 List handles = new ArrayList (); 131 Iterator list = getGroups().iterator(); 132 while (list.hasNext()) 133 { 134 PlanetGroupData group = (PlanetGroupData)list.next(); 135 handles.add(group.getHandle()); 136 } 137 return handles; 138 } 139 140 public PlanetSubscriptionData getSubscription(String feedUrl) 141 throws RollerException 142 { 143 try 144 { 145 Session session = ((HibernateStrategy)strategy).getSession(); 146 Criteria criteria = 147 session.createCriteria(PlanetSubscriptionData.class); 148 criteria.setMaxResults(1); 149 criteria.add(Expression.eq("feedUrl", feedUrl)); 150 List list = criteria.list(); 151 return list.size()!=0 ? (PlanetSubscriptionData)list.get(0) : null; 152 } 153 catch (HibernateException e) 154 { 155 throw new RollerException(e); 156 } 157 } 158 159 public PlanetSubscriptionData getSubscriptionById(String id) 160 throws RollerException 161 { 162 return (PlanetSubscriptionData) 163 strategy.load(id, PlanetSubscriptionData.class); 164 } 165 166 public PlanetGroupData getGroup(String handle) throws RollerException 167 { 168 try 169 { 170 Session session = ((HibernateStrategy)strategy).getSession(); 171 Criteria criteria = session.createCriteria(PlanetGroupData.class); 172 criteria.setMaxResults(1); 173 criteria.add(Expression.eq("handle", handle)); 174 List list = criteria.list(); 175 return list.size()!=0 ? (PlanetGroupData)list.get(0) : null; 176 } 177 catch (HibernateException e) 178 { 179 throw new RollerException(e); 180 } 181 } 182 183 public PlanetGroupData getGroupById(String id) throws RollerException 184 { 185 return (PlanetGroupData) 186 strategy.load(id, PlanetGroupData.class); 187 } 188 189 public synchronized List getAggregation(int maxEntries) throws RollerException 190 { 191 return getAggregation(null, maxEntries); 192 } 193 194 public synchronized List getAggregation(PlanetGroupData group, int maxEntries) 195 throws RollerException 196 { 197 List ret = null; 198 try 199 { 200 String groupHandle = (group == null) ? NO_GROUP : group.getHandle(); 201 ret = (List )aggregationsByGroup.get(groupHandle); 202 if (ret == null) 203 { 204 long startTime = System.currentTimeMillis(); 205 Session session = 206 ((HibernateStrategy)strategy).getSession(); 207 if (group != null) 208 { 209 Query query = session.createQuery( 210 "select entry from org.roller.pojos.PlanetEntryData entry " 211 +"join entry.subscription.groupSubscriptionAssocs assoc " 212 +"where assoc.group=:group order by entry.published desc"); 213 query.setEntity("group", group); 214 query.setMaxResults(maxEntries); 215 ret = query.list(); 216 } 217 else 218 { 219 Query query = session.createQuery( 220 "select entry from org.roller.pojos.PlanetEntryData entry " 221 +"join entry.subscription.groupSubscriptionAssocs assoc " 222 +"where " 223 +"assoc.group.handle='external' or assoc.group.handle='all'" 224 +" order by entry.published desc"); 225 query.setMaxResults(maxEntries); 226 ret = query.list(); 227 } 228 Date retLastUpdated = null; 229 if (ret.size() > 0) 230 { 231 PlanetEntryData entry = (PlanetEntryData)ret.get(0); 232 retLastUpdated = entry.getPublished(); 233 } 234 else 235 { 236 retLastUpdated = new Date (); 237 } 238 aggregationsByGroup.put(groupHandle, ret); 239 lastUpdatedByGroup.put(groupHandle, retLastUpdated); 240 241 long endTime = System.currentTimeMillis(); 242 logger.info("Generated aggregation in " 243 +((endTime-startTime)/1000.0)+" seconds"); 244 } 245 } 246 catch (Exception e) 247 { 248 logger.error("ERROR: building aggregation for: "+group, e); 249 throw new RollerException(e); 250 } 251 return ret; 252 } 253 254 public void deleteEntry(PlanetEntryData entry) throws RollerException 255 { 256 entry.remove(); 257 } 258 259 public void deleteGroup(PlanetGroupData group) throws RollerException 260 { 261 group.remove(); 262 } 263 264 public void deleteSubscription(PlanetSubscriptionData sub) 265 throws RollerException 266 { 267 sub.remove(); 268 } 269 270 public Iterator getAllSubscriptions() 271 { 272 try 273 { 274 Session session = ((HibernateStrategy)strategy).getSession(); 275 Criteria criteria = 276 session.createCriteria(PlanetSubscriptionData.class); 277 criteria.addOrder(Order.asc("feedUrl")); 278 List list = criteria.list(); 279 return list.iterator(); 280 } 281 catch (Exception e) 282 { 283 throw new RuntimeException ( 284 "ERROR fetching subscription collection", e); 285 } 286 } 287 288 public int getSubscriptionCount() throws RollerException 289 { 290 try 291 { 292 Session session = ((HibernateStrategy)strategy).getSession(); 293 Integer count = (Integer )session.createQuery( 294 "select count(*) from org.roller.pojos.PlanetSubscriptionData").uniqueResult(); 295 return count.intValue(); 296 } 297 catch (Exception e) 298 { 299 throw new RuntimeException ( 300 "ERROR fetching subscription count", e); 301 } 302 } 303 304 public synchronized List getTopSubscriptions(int max) throws RollerException 305 { 306 String groupHandle = NO_GROUP; 307 List ret = (List )topSubscriptionsByGroup.get(groupHandle); 308 if (ret == null) 309 { 310 try 311 { 312 Session session = ((HibernateStrategy)strategy).getSession(); 313 Criteria criteria = 314 session.createCriteria(PlanetSubscriptionData.class); 315 criteria.setMaxResults(max); 316 criteria.addOrder(Order.desc("inboundblogs")); 317 ret = criteria.list(); 318 } 319 catch (HibernateException e) 320 { 321 throw new RollerException(e); 322 } 323 topSubscriptionsByGroup.put(groupHandle, ret); 324 } 325 return ret; 326 } 327 328 public synchronized List getTopSubscriptions( 329 PlanetGroupData group, int max) throws RollerException 330 { 331 String groupHandle = (group == null) ? NO_GROUP : group.getHandle(); 332 List ret = (List )topSubscriptionsByGroup.get(groupHandle); 333 if (ret == null) 334 { 335 try 336 { 337 Session session = ((HibernateStrategy)strategy).getSession(); 338 Query query = session.createQuery( 339 "select sub from org.roller.pojos.PlanetSubscriptionData sub " 340 +"join sub.groupSubscriptionAssocs assoc " 341 +"where " 342 +"assoc.group.handle=:groupHandle " 343 +"order by sub.inboundblogs desc"); 344 query.setString("groupHandle", group.getHandle()); 345 query.setMaxResults(max); 346 ret = query.list(); 347 } 348 catch (HibernateException e) 349 { 350 throw new RollerException(e); 351 } 352 topSubscriptionsByGroup.put(groupHandle, ret); 353 } 354 return ret; 355 } 356 } 357 358 | Popular Tags |