1 26 28 package de.nava.informa.impl.hibernate; 29 30 import java.net.URL ; 31 import java.util.*; 32 33 import net.sf.hibernate.*; 34 35 import org.apache.commons.logging.*; 36 import org.jdom.Element; 37 38 import de.nava.informa.core.*; 39 import de.nava.informa.utils.InformaUtils; 40 41 68 public class ChannelBuilder implements ChannelBuilderIF { 69 70 private static Log logger = LogFactory.getLog(ChannelBuilder.class); 71 private Session session; 72 private SessionHandler handler; 73 private Transaction transaction; 74 75 79 public ChannelBuilder(Session session) { 80 logger.info("New Channel Builder for: " + session); 81 this.session = session; 82 this.handler = null; 83 } 84 85 90 public ChannelBuilder(SessionHandler handler) { 91 logger.debug("New Channel Builder for: " + handler); 92 this.handler = handler; 93 this.session = null; 94 } 95 96 100 105 public void beginTransaction() throws ChannelBuilderException { 106 logger.info("beginTransaction"); 107 if (session != null || handler == null) 108 throw new IllegalStateException ("Session != null || handler == null"); 109 try { 110 session = handler.getSession(); 111 transaction = session.beginTransaction(); 112 } catch (HibernateException e) { 113 e.printStackTrace(); 114 transaction = null; 115 throw new ChannelBuilderException(e); 116 } 117 } 118 119 126 public void endTransaction() throws ChannelBuilderException { 127 logger.info("endTransaction"); 128 if (handler == null || transaction == null || session == null) 129 throw new IllegalStateException ("handler == null || transaction == null || session == null"); 130 try { 131 transaction.commit(); 132 session.flush(); 133 session.close(); 134 session = null; 135 transaction = null; 136 137 } catch (HibernateException he) { 138 if (transaction != null) 139 try { 140 he.printStackTrace(); 141 transaction.rollback(); 142 transaction = null; 143 if (session.isOpen()) { 144 session.close(); 145 session = null; 146 } 147 } catch (HibernateException e) { 148 if (session.isOpen()) { 149 session = null; 150 } 151 e.printStackTrace(); 152 throw new ChannelBuilderException(e); 153 } 154 throw new ChannelBuilderException(he); 155 } 156 } 157 158 165 public boolean inTransaction() 166 { 167 return session != null && transaction != null; 168 } 169 176 public void resetTransaction() 177 { 178 logger.debug("Transaction being reset."); 179 if (transaction != null) 180 { 181 try { 182 transaction.commit(); 183 transaction = null; 184 } 185 catch (HibernateException e) { 186 transaction = null; 187 e.printStackTrace(); 188 } 189 } 190 if (session != null) { 191 try { 192 session.flush(); 193 session.close(); 194 session = null; 195 } 196 catch (HibernateException e) { 197 e.printStackTrace(); 198 session = null; 199 } 200 } 201 } 202 203 208 public Session getSession() { 209 if (handler == null || session == null) 210 throw new IllegalStateException ("getSession must be bracketed by begin/endTransaction"); 211 if (!handler.isSessionOpen()) 212 throw new IllegalStateException ("Hibernate Handler must be open"); 213 return session; 214 } 215 216 222 public void update(Object o) throws ChannelBuilderException { 223 try { 224 session.update(o); 225 } catch (HibernateException e) { 226 e.printStackTrace(); 227 throw new ChannelBuilderException("update() Failed"); 228 } 229 } 230 231 237 public void delete(Object o) throws ChannelBuilderException { 238 try { 239 session.delete(o); 240 } catch (HibernateException e) { 241 e.printStackTrace(); 242 throw new ChannelBuilderException("delete() Failed"); 243 } 244 } 245 246 247 251 public void init(Properties props) throws ChannelBuilderException { 252 logger.debug("initialising channel builder for hibernate backend"); 253 } 254 255 public ChannelGroupIF createChannelGroup(String title) { 256 ChannelGroupIF obj = new ChannelGroup(title); 257 save(obj); 258 return obj; 259 } 260 261 public ChannelIF createChannel(String title) { 262 ChannelIF obj = new Channel(title); 263 save(obj); 264 return obj; 265 } 266 267 public ChannelIF createChannel(Element channelElement, String title) { 268 ChannelIF obj = new Channel(channelElement, title); 269 save(obj); 270 return obj; 271 } 272 273 public ItemIF createItem(ChannelIF channel, String title, String description, 274 URL link) { 275 return createItem(null, channel, title, description, link); 276 } 277 278 public ItemIF createItem(Element itemElement, ChannelIF channel, 279 String title, String description, URL link) { 280 ItemIF obj = new Item(itemElement, channel, title, description, link); 281 save(obj); 282 if (channel != null) { 283 channel.addItem(obj); 284 } 285 return obj; 286 } 287 288 297 public ItemIF createItem(ChannelIF chan, ItemIF oldItem) { 298 ItemIF obj = createItem(null, chan, oldItem.getTitle(), oldItem.getDescription(), oldItem.getLink()); 299 InformaUtils.copyItemProperties(oldItem, obj); 300 save(obj); 301 return obj; 302 } 303 304 public ImageIF createImage(String title, URL location, URL link) { 305 ImageIF obj = new Image(title, location, link); 306 save(obj); 307 return obj; 308 } 309 310 public CloudIF createCloud(String domain, int port, String path, String registerProcedure, String protocol) { 311 logger.info("ChannelBuilder is creating a Persistent Cloud"); 312 CloudIF obj = new Cloud(domain, port, path, registerProcedure, protocol); 313 save(obj); 314 return obj; 315 } 316 317 public TextInputIF createTextInput(String title, String description, 318 String name, URL link) { 319 TextInputIF obj = new TextInput(title, description, name, link); 320 save(obj); 321 return obj; 322 } 323 324 public ItemSourceIF createItemSource(ItemIF item, String name, 325 String location, Date timestamp) { 326 return new ItemSource(item, name, location, timestamp); 327 } 328 329 public ItemEnclosureIF createItemEnclosure(ItemIF item, URL location, 330 String type, int length) { 331 return new ItemEnclosure(item, location, type, length); 332 } 333 334 public ItemGuidIF createItemGuid(ItemIF item, String location, boolean permaLink) { 335 return new ItemGuid(item, location, permaLink); 336 } 337 338 public CategoryIF createCategory(CategoryIF parent, String title) { 339 CategoryIF cat = new Category(title); 340 save(cat); 341 if (parent != null) { 342 parent.addChild(cat); 343 } 344 return cat; 345 } 346 347 public void close() throws ChannelBuilderException { 348 logger.debug("closing channel builder for hibernate backend"); 349 } 350 351 360 public ChannelGroup reload(ChannelGroup group) 361 throws ChannelBuilderException 362 { 363 try 364 { 365 getSession().load(group, new Integer (group.getIntId())); 366 } catch (HibernateException e) 367 { 368 throw new ChannelBuilderException("Unable to reload group: " + e.getMessage()); 369 } 370 371 return group; 372 } 373 374 383 public Channel reload(Channel channel) 384 throws ChannelBuilderException 385 { 386 try 387 { 388 getSession().load(channel, new Integer (channel.getIntId())); 389 } catch (HibernateException e) 390 { 391 throw new ChannelBuilderException("Unable to reload channel: " + e.getMessage()); 392 } 393 394 return channel; 395 } 396 397 406 public Item reload(Item item) 407 throws ChannelBuilderException 408 { 409 try 410 { 411 getSession().load(item, new Integer (item.getIntId())); 412 } catch (HibernateException e) 413 { 414 throw new ChannelBuilderException("Unable to reload item: " + e.getMessage()); 415 } 416 417 return item; 418 } 419 420 424 protected void save(Object dataObject) { 425 if (session == null) 426 throw new IllegalStateException ("Session == null"); 427 try { 428 session.save(dataObject); 429 } catch (HibernateException he) { 430 throw new RuntimeException (he.getMessage()); 431 } 432 } 433 } 434 | Popular Tags |