KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > utils > manager > hibernate > PersistenceManager


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25
// $Id: PersistenceManager.java,v 1.1 2004/09/02 09:13:08 spyromus Exp $
26
//
27

28 package de.nava.informa.utils.manager.hibernate;
29
30 import de.nava.informa.utils.manager.PersistenceManagerException;
31 import de.nava.informa.core.ChannelGroupIF;
32
33 import java.util.Map JavaDoc;
34 import java.util.HashMap JavaDoc;
35
36 /**
37  * Hibernate Persistence Manager. This manager talks to Hibernate to store / restore
38  * persistence data. Manager is multi-thread safe. It follows the rule of identities and
39  * multiple calls to getting methods (for example, <code>getGroups()</code>) return
40  * the same instances of objects. This makes life of developers and client applications
41  * easier.
42  * <p>
43  * It's not enough to directly update the fields of objects in order to have the same
44  * fields updated persistently. You should explicitly call <code>updateXXXX()</code>
45  * methods to transfer changes to the storage. The decision to make explicit updates
46  * based on the fact that automatic flushing changes to database each time the value
47  * of some property changes will take too much resources when many properties are
48  * updated in a single block of code.</p>
49  * <p>
50  * <b>Note that the manager itself does no efforts to initialize Hibernate!</b><br>
51  * It uses system properties as overrides of normal Hibernate inialization ways, like
52  * <code>hibernate.properties</code> and <code>hibernate.cfg.xml</code> files which
53  * are loaded when this manager requires Hibernate services for the first time. Please,
54  * read Hibernate documentation to learn what files and where should be placed
55  * to initialize the product properly.</p>
56  * <p>
57  * <b>Please also note, that this implementation requires to be the only source of
58  * changes to Hibernate data storage (database or something else) to operate normally!</b>
59  * Basically it is not a problem at all, but if you find that it's not your case, please
60  * let us know.
61  * </p>
62  *
63  * @author Aleksey Gureev (spyromus@noizeramp.com)
64  */

65 public class PersistenceManager extends NonCachingPersistenceManager {
66
67   private Map JavaDoc groups = new HashMap JavaDoc();
68   private boolean groupsRead = false;
69
70   /**
71    * Creates new group of channels in persistent storage.
72    *
73    * @param title title of the group.
74    * @return initialized and persisted group object.
75    * @throws PersistenceManagerException in case of any problems.
76    */

77   public ChannelGroupIF createGroup(String JavaDoc title) throws PersistenceManagerException {
78
79     // Create new group in persistent storage
80
ChannelGroupIF group = super.createGroup(title);
81
82     // Save new group in local cache
83
groups.put(new Long JavaDoc(group.getId()), group);
84
85     return group;
86   }
87
88   /**
89    * Deletes group from persistent storage.
90    *
91    * @param group group to delete.
92    * @throws PersistenceManagerException in case of any problems.
93    */

94   public void deleteGroup(ChannelGroupIF group) throws PersistenceManagerException {
95
96     // Save ID and delete group in persistent storage
97
long groupId = group.getId();
98     super.deleteGroup(group);
99
100     // If object existed then remove it from cache
101
if (groupId > -1) {
102       groups.remove(new Long JavaDoc(groupId));
103     }
104   }
105
106   /**
107    * Returns the list of groups available in database.
108    *
109    * @return list of groups.
110    * @throws PersistenceManagerException in case of any problems.
111    */

112   public ChannelGroupIF[] getGroups() throws PersistenceManagerException {
113
114     // Thing is that we should read the list of groups from Hibernate
115
// only once per run. It's so because if we are the only source of update
116
// to the storage (and it should be so) then we have up-to-date cache of
117
// objects at any time due to the fact that we control all additions and
118
// removals of groups.
119
// So, when lost of groups required and we have not read it from Hibernate yet
120
// then we do so and put the groups we don't have yet in our cache in it.
121
if (!groupsRead) {
122       final ChannelGroupIF[] hibernateGroups = super.getGroups();
123       for (int i = 0; i < hibernateGroups.length; i++) {
124         final ChannelGroupIF hibernateGroup = hibernateGroups[i];
125         final Long JavaDoc groupId = new Long JavaDoc(hibernateGroup.getId());
126         if (!groups.containsKey(groupId)) {
127           groups.put(groupId, hibernateGroup);
128         }
129       }
130
131       // Put mark that we already successfully read the list of groups from Hibernate.
132
groupsRead = true;
133     }
134
135     return (ChannelGroupIF[]) groups.values().toArray(new ChannelGroupIF[0]);
136   }
137 }
138
Popular Tags