1 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 ; 39 import java.util.HashMap ; 40 import java.util.Map ; 41 42 47 public class PersistenceManager implements PersistenceManagerIF { 48 49 private Map groups; 50 private Map channels; 51 private Map items; 52 53 56 public PersistenceManager() { 57 groups = new HashMap (); 58 channels = new HashMap (); 59 items = new HashMap (); 60 } 61 62 68 public final ChannelGroupIF createGroup(String title) { 69 final long id = IdGenerator.getNextId(); 70 final ChannelGroup group = new ChannelGroup(id, title); 71 72 groups.put(new Long (id), group); 74 75 return group; 76 } 77 78 83 public void updateGroup(ChannelGroupIF group) { 84 } 87 88 93 public final void deleteGroup(ChannelGroupIF group) { 94 group.getAll().clear(); 95 groups.remove(new Long (group.getId())); 96 group.setId(-1); 97 } 98 99 106 public final void mergeGroups(ChannelGroupIF first, ChannelGroupIF second) { 107 moveChannels(second, first); 108 deleteGroup(second); 109 } 110 111 116 public final ChannelGroupIF[] getGroups() { 117 return (ChannelGroupIF[]) groups.values().toArray(new ChannelGroupIF[0]); 118 } 119 120 127 public final ChannelIF createChannel(String title, URL location) { 128 final long id = IdGenerator.getNextId(); 129 final Channel channel = new Channel(id, title, location); 130 131 channels.put(new Long (id), channel); 133 134 return channel; 135 } 136 137 142 public void updateChannel(ChannelIF channel) { 143 } 146 147 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 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 183 public final void deleteChannel(ChannelIF channel) { 184 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 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 channels.remove(new Long (channel.getId())); 202 channel.setId(-1); 203 } 204 205 212 public final ItemIF createItem(ChannelIF channel, String title) { 213 final long id = IdGenerator.getNextId(); 214 final Item item = new Item(id, title, channel); 215 216 items.put(new Long (item.getId()), item); 218 219 if (channel.getItems().contains(item)) { 221 channel.removeItem(item); 222 } 223 224 channel.addItem(item); 225 226 return item; 227 } 228 229 238 public final ItemIF createItem(ChannelIF channel, ItemIF ethalon) { 239 final ItemIF item = createItem(channel, ethalon.getTitle()); 240 241 InformaUtils.copyItemProperties(ethalon, item); 243 244 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 258 public void updateItem(ItemIF item) { 259 } 262 263 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 (item.getId())); 275 item.setId(-1); 276 } 277 278 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 |