KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > utils > manager > memory > 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.4 2004/09/02 09:11:13 spyromus Exp $
26
//
27

28 package de.nava.informa.utils.manager.memory;
29
30 import de.nava.informa.core.ChannelGroupIF;
31 import de.nava.informa.core.ChannelIF;
32 import de.nava.informa.core.ItemGuidIF;
33 import de.nava.informa.core.ItemIF;
34 import de.nava.informa.impl.basic.ItemGuid;
35 import de.nava.informa.utils.InformaUtils;
36 import de.nava.informa.utils.manager.PersistenceManagerIF;
37
38 import java.net.URL JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Map JavaDoc;
41
42 /**
43  * In-memory implementation of persistence manager. Uses local memory to store data.
44  *
45  * @author Aleksey Gureev (spyromus@noizeramp.com)
46  */

47 public class PersistenceManager implements PersistenceManagerIF {
48
49   private Map JavaDoc groups;
50   private Map JavaDoc channels;
51   private Map JavaDoc items;
52
53   /**
54    * Creates persistence manager.
55    */

56   public PersistenceManager() {
57     groups = new HashMap JavaDoc();
58     channels = new HashMap JavaDoc();
59     items = new HashMap JavaDoc();
60   }
61
62   /**
63    * Creates new group of channels in persistent storage.
64    *
65    * @param title title of the group.
66    * @return initialized and persisted group object.
67    */

68   public final ChannelGroupIF createGroup(String JavaDoc title) {
69     final long id = IdGenerator.getNextId();
70     final ChannelGroup group = new ChannelGroup(id, title);
71
72     // put group in map
73
groups.put(new Long JavaDoc(id), group);
74
75     return group;
76   }
77
78   /**
79    * Updates data in storage with data from the group object.
80    *
81    * @param group group object
82    */

83   public void updateGroup(ChannelGroupIF group) {
84     // Explicit update isn't necessary as we have only memory objects
85
// which are updated when we change properties values.
86
}
87
88   /**
89    * Deletes group from persistent storage.
90    *
91    * @param group group to delete.
92    */

93   public final void deleteGroup(ChannelGroupIF group) {
94     group.getAll().clear();
95     groups.remove(new Long JavaDoc(group.getId()));
96     group.setId(-1);
97   }
98
99   /**
100    * Takes channels from the <code>second</code> group and put them all in <code>first</code>
101    * group. Then <code>second</code> group is deleted.
102    *
103    * @param first first group of channels.
104    * @param second second group of channels.
105    */

106   public final void mergeGroups(ChannelGroupIF first, ChannelGroupIF second) {
107     moveChannels(second, first);
108     deleteGroup(second);
109   }
110
111   /**
112    * Returns the list of groups available in database.
113    *
114    * @return list of groups.
115    */

116   public final ChannelGroupIF[] getGroups() {
117     return (ChannelGroupIF[]) groups.values().toArray(new ChannelGroupIF[0]);
118   }
119
120   /**
121    * Creates new channel object and persists it into storage.
122    *
123    * @param title title of the channel.
124    * @param location location of channel data resource.
125    * @return newly created object.
126    */

127   public final ChannelIF createChannel(String JavaDoc title, URL JavaDoc location) {
128     final long id = IdGenerator.getNextId();
129     final Channel channel = new Channel(id, title, location);
130
131     // put channel in map
132
channels.put(new Long JavaDoc(id), channel);
133
134     return channel;
135   }
136
137   /**
138    * Updates data in database with data from channel object.
139    *
140    * @param channel channel object.
141    */

142   public void updateChannel(ChannelIF channel) {
143     // Explicit update isn't necessary as we have only memory objects
144
// which are updated when we change properties values.
145
}
146
147   /**
148    * Adds <code>channel</code> to the <code>group</code>.
149    *
150    * @param channel channel to add.
151    * @param group group to use.
152    */

153   public final void addChannelToGroup(ChannelIF channel, ChannelGroupIF group) {
154     if (!group.getAll().contains(channel)) {
155       group.add(channel);
156     }
157
158     if (channel instanceof Channel) {
159       ((Channel) channel).addParentGroup(group);
160     }
161   }
162
163   /**
164    * Deletes <code>channel</code> from the <code>group</code>.
165    * This method doesn't delete channel from persistent storage. It only
166    * breaks the association between channel and group.
167    *
168    * @param channel channel to delete.
169    * @param group group to use.
170    */

171   public final void removeChannelFromGroup(ChannelIF channel, ChannelGroupIF group) {
172     group.remove(channel);
173     if (channel instanceof Channel) {
174       ((Channel) channel).removeParentGroup(group);
175     }
176   }
177
178   /**
179    * Deletes channel from persistent storage.
180    *
181    * @param channel channel to delete.
182    */

183   public final void deleteChannel(ChannelIF channel) {
184     // remove all associations with parent groups
185
if (channel instanceof Channel) {
186       final ChannelGroupIF[] groupsList = ((Channel) channel).getParentGroups();
187       for (int i = 0; i < groupsList.length; i++) {
188         ChannelGroupIF group = groupsList[i];
189
190         removeChannelFromGroup(channel, group);
191       }
192     }
193
194     // remove all items
195
final ItemIF[] itemsList = (ItemIF[]) channel.getItems().toArray(new ItemIF[0]);
196     for (int i = 0; i < itemsList.length; i++) {
197       deleteItem(itemsList[i]);
198     }
199
200     // remove channel from map
201
channels.remove(new Long JavaDoc(channel.getId()));
202     channel.setId(-1);
203   }
204
205   /**
206    * Creates new item in the channel.
207    *
208    * @param channel channel to put new item into.
209    * @param title title of new item.
210    * @return new item object.
211    */

212   public final ItemIF createItem(ChannelIF channel, String JavaDoc title) {
213     final long id = IdGenerator.getNextId();
214     final Item item = new Item(id, title, channel);
215
216     // put item in map
217
items.put(new Long JavaDoc(item.getId()), item);
218
219     // replace item in channel if it's already there
220
if (channel.getItems().contains(item)) {
221       channel.removeItem(item);
222     }
223
224     channel.addItem(item);
225
226     return item;
227   }
228
229   /**
230    * Creates new item using specified object as ethalon.
231    * <b>Note that application <i>could</i> already add object to the channel and
232    * only persistent modifications required.</b>
233    *
234    * @param channel channel to put new item into.
235    * @param ethalon object to copy properties values from.
236    * @return new item object.
237    */

238   public final ItemIF createItem(ChannelIF channel, ItemIF ethalon) {
239     final ItemIF item = createItem(channel, ethalon.getTitle());
240
241     // Copy values
242
InformaUtils.copyItemProperties(ethalon, item);
243
244     // Copy guid if present
245
final ItemGuidIF ethalonGuid = ethalon.getGuid();
246     if (ethalonGuid != null) {
247       item.setGuid(new ItemGuid(item, ethalonGuid.getLocation(), ethalonGuid.isPermaLink()));
248     }
249
250     return item;
251   }
252
253   /**
254    * Updates data in database with data from item object.
255    *
256    * @param item item object.
257    */

258   public void updateItem(ItemIF item) {
259     // Explicit update isn't necessary as we have only memory objects
260
// which are updated when we change properties values.
261
}
262
263   /**
264    * Deletes the item from the persistent storage.
265    *
266    * @param item item to delete.
267    */

268   public final void deleteItem(ItemIF item) {
269     if (item instanceof Item) {
270       final ChannelIF parent = ((Item) item).getParent();
271       parent.removeItem(item);
272     }
273
274     items.remove(new Long JavaDoc(item.getId()));
275     item.setId(-1);
276   }
277
278   /**
279    * Moves channels from source to destination group.
280    *
281    * @param src source group to take channels from.
282    * @param dest destination group to put channel into.
283    */

284   final void moveChannels(ChannelGroupIF src, ChannelGroupIF dest) {
285     final ChannelIF[] secondChannels = (ChannelIF[]) src.getAll().toArray(new ChannelIF[0]);
286     for (int i = 0; i < secondChannels.length; i++) {
287       ChannelIF channel = secondChannels[i];
288
289       addChannelToGroup(channel, dest);
290       removeChannelFromGroup(channel, src);
291     }
292   }
293 }
294
Popular Tags