1 28 package de.nava.informa.utils.manager.hibernate; 29 30 import de.nava.informa.core.ChannelGroupIF; 31 import de.nava.informa.core.ChannelIF; 32 import de.nava.informa.core.ItemIF; 33 import de.nava.informa.impl.hibernate.Channel; 34 import de.nava.informa.impl.hibernate.ChannelGroup; 35 import de.nava.informa.impl.hibernate.Item; 36 import de.nava.informa.utils.InformaUtils; 37 import de.nava.informa.utils.manager.PersistenceManagerException; 38 import de.nava.informa.utils.manager.PersistenceManagerIF; 39 import net.sf.hibernate.HibernateException; 40 import net.sf.hibernate.Session; 41 import net.sf.hibernate.Transaction; 42 43 import java.net.URL ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.logging.Level ; 47 import java.util.logging.Logger ; 48 49 64 class NonCachingPersistenceManager implements PersistenceManagerIF { 65 66 private static final Logger LOG = Logger.getLogger(NonCachingPersistenceManager.class.getName()); 67 68 76 public ChannelGroupIF createGroup(String title) 77 throws PersistenceManagerException { 78 79 final ChannelGroupIF group = new ChannelGroup(title); 81 HibernateUtil.saveObject(group); 82 83 return group; 84 } 85 86 93 public void updateGroup(ChannelGroupIF group) 94 throws PersistenceManagerException { 95 96 HibernateUtil.updateObject(group); 97 } 98 99 106 public void deleteGroup(ChannelGroupIF group) 107 throws PersistenceManagerException { 108 109 deleteGroup(group, null); 111 group.setId(-1); 112 } 113 114 123 public void mergeGroups(ChannelGroupIF first, ChannelGroupIF second) 124 throws PersistenceManagerException { 125 126 Transaction tx = null; 127 try { 128 final Session session = HibernateUtil.openSession(); 129 tx = session.beginTransaction(); 130 131 HibernateUtil.lock(first, session); 132 HibernateUtil.lock(second, session); 133 mergeGroups(first, second, session); 134 135 tx.commit(); 136 137 second.setId(-1); 138 } catch (HibernateException e) { 139 if (tx != null) { 140 try { 141 tx.rollback(); 142 } catch (HibernateException e1) { 143 } 145 } 146 147 LOG.log(Level.SEVERE, "Could not merge groups.", e); 148 throw new PersistenceManagerException("Could not merge groups.", e); 149 } finally { 150 HibernateUtil.closeSession(); 151 } 152 } 153 154 161 public ChannelGroupIF[] getGroups() 162 throws PersistenceManagerException { 163 164 ChannelGroupIF[] groups = null; 168 try { 169 final Session session = HibernateUtil.openSession(); 170 groups = getGroups(session); 171 } catch (HibernateException e) { 172 LOG.log(Level.SEVERE, "Could not read the list of groups.", e); 173 throw new PersistenceManagerException("Could not read the list of groups.", e); 174 } finally { 175 HibernateUtil.closeSession(); 176 } 177 178 return groups == null ? new ChannelGroupIF[0] : groups; 179 } 180 181 191 public ChannelIF createChannel(String title, URL location) 192 throws PersistenceManagerException { 193 194 final ChannelIF channel = new Channel(title); 196 channel.setLocation(location); 197 HibernateUtil.saveObject(channel); 198 199 return channel; 200 } 201 202 209 public void updateChannel(ChannelIF channel) 210 throws PersistenceManagerException { 211 212 HibernateUtil.updateObject(channel); 213 } 214 215 223 public void addChannelToGroup(ChannelIF channel, ChannelGroupIF group) 224 throws PersistenceManagerException { 225 226 channel.hashCode(); 229 230 Transaction tx = null; 231 try { 232 final Session session = HibernateUtil.openSession(); 233 tx = session.beginTransaction(); 234 235 HibernateUtil.lock(channel, session); 236 group.add(channel); 237 HibernateUtil.updateObject(group, session); 238 239 tx.commit(); 240 } catch (HibernateException e) { 241 if (tx != null) { 242 try { 243 tx.rollback(); 244 } catch (HibernateException e1) { 245 } 247 } 248 249 LOG.log(Level.SEVERE, "Could add channel to group.", e); 250 throw new PersistenceManagerException("Could add channel to group.", e); 251 } finally { 252 HibernateUtil.closeSession(); 253 } 254 } 255 256 266 public void removeChannelFromGroup(ChannelIF channel, ChannelGroupIF group) 267 throws PersistenceManagerException { 268 269 Transaction tx = null; 270 try { 271 final Session session = HibernateUtil.openSession(); 272 tx = session.beginTransaction(); 273 274 HibernateUtil.lock(channel, session); 275 group.remove(channel); 276 HibernateUtil.updateObject(group, session); 277 278 tx.commit(); 279 } catch (HibernateException e) { 280 if (tx != null) { 281 try { 282 tx.rollback(); 283 } catch (HibernateException e1) { 284 } 286 } 287 288 LOG.log(Level.SEVERE, "Could add channel to group.", e); 289 throw new PersistenceManagerException("Could add channel to group.", e); 290 } finally { 291 HibernateUtil.closeSession(); 292 } 293 } 294 295 302 public void deleteChannel(ChannelIF channel) 303 throws PersistenceManagerException { 304 305 final ChannelGroupIF[] groups = getGroups(); 312 ItemIF[] items = null; 313 314 Transaction tx = null; 315 try { 316 final Session session = HibernateUtil.openSession(); 317 tx = session.beginTransaction(); 318 319 HibernateUtil.lock(channel, session); 320 items = deleteChannel(channel, groups, session); 321 322 tx.commit(); 323 324 channel.setId(-1); 326 for (int i = 0; i < items.length; i++) { 327 items[i].setId(-1); 328 } 329 } catch (HibernateException e) { 330 if (tx != null) { 331 try { 332 tx.rollback(); 333 } catch (HibernateException e1) { 334 } 336 } 337 338 LOG.log(Level.SEVERE, "Could not delete channel.", e); 339 throw new PersistenceManagerException("Could not delete channel.", e); 340 } finally { 341 HibernateUtil.closeSession(); 342 } 343 } 344 345 355 public ItemIF createItem(ChannelIF channel, String title) 356 throws PersistenceManagerException { 357 358 final ItemIF item = new Item(channel, title, null, null); 359 360 saveCreatedItem(channel, item); 361 362 return item; 363 } 364 365 377 public ItemIF createItem(ChannelIF channel, ItemIF ethalon) 378 throws PersistenceManagerException { 379 380 final ItemIF item = new Item(channel, null, null, null); 382 InformaUtils.copyItemProperties(ethalon, item); 383 384 saveCreatedItem(channel, item); 385 386 return item; 387 } 388 389 396 public void updateItem(ItemIF item) 397 throws PersistenceManagerException { 398 399 HibernateUtil.updateObject(item); 400 } 401 402 409 public void deleteItem(ItemIF item) 410 throws PersistenceManagerException { 411 412 Transaction tx = null; 413 try { 414 final Session session = HibernateUtil.openSession(); 415 tx = session.beginTransaction(); 416 417 deleteItem(item, session); 419 420 tx.commit(); 421 item.setId(-1); 422 } catch (HibernateException e) { 423 if (tx != null) { 424 try { 425 tx.rollback(); 426 } catch (HibernateException e1) { 427 } 429 } 430 431 LOG.log(Level.SEVERE, "Could not delete item.", e); 432 throw new PersistenceManagerException("Could not delete item.", e); 433 } finally { 434 HibernateUtil.closeSession(); 435 } 436 } 437 438 442 451 private static void mergeGroups(ChannelGroupIF first, ChannelGroupIF second, 452 final Session session) 453 throws PersistenceManagerException { 454 455 first.getAll().addAll(second.getAll()); 457 HibernateUtil.updateObject(first, session); 458 459 deleteGroup(second, session); 461 } 462 463 471 private static void deleteGroup(ChannelGroupIF group, Session session) 472 throws PersistenceManagerException { 473 474 if (session != null) { 475 HibernateUtil.lock(group, session); 476 } 477 478 group.getAll().clear(); 479 HibernateUtil.deleteObject(group, session); 480 } 481 482 491 private static ChannelGroupIF[] getGroups(final Session session) 492 throws PersistenceManagerException { 493 494 ChannelGroupIF[] groups; 495 496 try { 497 final List groupsList = session.find("from ChannelGroup"); 498 groups = (ChannelGroupIF[]) groupsList.toArray(new ChannelGroupIF[0]); 499 500 for (int i = 0; i < groups.length; i++) { 503 ChannelGroupIF group = groups[i]; 504 initGroupCollections(group); 505 } 506 } catch (HibernateException e) { 507 LOG.log(Level.SEVERE, "Could not read the list of groups.", e); 508 throw new PersistenceManagerException("Could not read the list of groups.", e); 509 } 510 511 return groups; 512 } 513 514 519 private static void initGroupCollections(ChannelGroupIF group) { 520 521 ChannelIF[] channels = (ChannelIF[]) group.getAll().toArray(new ChannelIF[0]); 523 for (int i = 0; i < channels.length; i++) { 524 ChannelIF channel = channels[i]; 525 channel.getCategories().size(); 526 ((Channel)channel).getGroups().size(); 527 for (Iterator it = channel.getItems().iterator(); it.hasNext();) { 528 ((ItemIF)it.next()).getCategories().size(); 529 } 530 } 531 } 532 533 545 private ItemIF[] deleteChannel(ChannelIF channel, ChannelGroupIF[] groups, final Session session) 546 throws PersistenceManagerException { 547 548 for (int i = 0; i < groups.length; i++) { 550 ChannelGroupIF group = groups[i]; 551 if (group.getAll().contains(channel)) { 552 group.remove(channel); 553 HibernateUtil.updateObject(group, session); 554 } 555 } 556 557 final ItemIF[] items = (ItemIF[]) channel.getItems().toArray(new ItemIF[0]); 559 for (int i = 0; i < items.length; i++) { 560 ItemIF item = items[i]; 561 channel.removeItem(item); 562 HibernateUtil.deleteObject(item, session); 563 } 564 565 HibernateUtil.deleteObject(channel, session); 567 568 return items; 569 } 570 571 580 private static void createItem(final ItemIF item, ChannelIF channel, Session session) 581 throws PersistenceManagerException { 582 583 HibernateUtil.saveObject(item, session); 585 586 channel.addItem(item); 588 HibernateUtil.updateObject(channel, session); 589 } 590 591 599 private static void deleteItem(ItemIF item, Session session) 600 throws PersistenceManagerException { 601 602 final ChannelIF channel = item.getChannel(); 604 if (channel != null) { 605 channel.removeItem(item); 606 } 607 608 HibernateUtil.deleteObject(item, session); 610 } 611 612 616 624 private void saveCreatedItem(ChannelIF channel, final ItemIF item) 625 throws PersistenceManagerException { 626 627 Transaction tx = null; 628 try { 629 final Session session = HibernateUtil.openSession(); 630 tx = session.beginTransaction(); 631 632 HibernateUtil.lock(channel, session); 634 createItem(item, channel, session); 635 636 tx.commit(); 637 } catch (HibernateException e) { 638 if (tx != null) { 639 try { 640 tx.rollback(); 641 } catch (HibernateException e1) { 642 } 644 } 645 646 LOG.log(Level.SEVERE, "Could not create item.", e); 647 throw new PersistenceManagerException("Could not create item.", e); 648 } finally { 649 HibernateUtil.closeSession(); 650 } 651 } 652 } 653 | Popular Tags |