001: /*
002: * Created on May 7, 2003
003: */
004: package net.sf.jportlet.portlets.bookmark.impl;
005:
006: import java.net.URL;
007:
008: import java.sql.SQLException;
009:
010: import java.util.ArrayList;
011: import java.util.Collection;
012: import java.util.Iterator;
013:
014: import cirrus.hibernate.Hibernate;
015: import cirrus.hibernate.HibernateException;
016: import cirrus.hibernate.ObjectDeletedException;
017: import cirrus.hibernate.Session;
018: import cirrus.hibernate.SessionFactory;
019: import cirrus.hibernate.Transaction;
020:
021: import net.sf.jportlet.portlet.PortletContext;
022: import net.sf.jportlet.portlet.PortletException;
023: import net.sf.jportlet.portlet.User;
024: import net.sf.jportlet.portlets.ObjectNotFoundException;
025: import net.sf.jportlet.portlets.PersistenceException;
026: import net.sf.jportlet.portlets.bookmark.Bookmark;
027: import net.sf.jportlet.portlets.bookmark.BookmarkManager;
028: import net.sf.jportlet.service.hibernate.HibernateService;
029:
030: /**
031: * Implementation of {@link Bookmark}
032: * @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
033: */
034: public class BookmarkManagerImpl implements BookmarkManager {
035: //~ Instance fields --------------------------------------------------------
036:
037: private SessionFactory _sessionFactory;
038:
039: //~ Constructors -----------------------------------------------------------
040:
041: /**
042: *
043: */
044: public BookmarkManagerImpl(PortletContext context)
045: throws PortletException {
046: HibernateService hib = (HibernateService) context
047: .getService(HibernateService.NAME);
048: _sessionFactory = hib.getSessionFactory();
049: }
050:
051: //~ Methods ----------------------------------------------------------------
052:
053: /**
054: * @see net.sf.jportlet.portlets.bookmark.BookmarkManager#createBookmark(java.lang.String, java.net.URL, java.lang.String, net.sf.jportlet.portlet.User)
055: */
056: public String createBookmark(String title, URL url,
057: String description, User user) {
058: Session session = null;
059: Transaction tx = null;
060:
061: try {
062: session = _sessionFactory.openSession();
063:
064: BookmarkImpl bk = new BookmarkImpl(null, title, url
065: .toString(), description, user);
066:
067: tx = session.beginTransaction();
068: session.save(bk);
069: tx.commit();
070:
071: return bk.getId();
072: } catch (HibernateException e) {
073: rollback(tx);
074: throw new PersistenceException(e);
075: } catch (SQLException e) {
076: rollback(tx);
077: throw new PersistenceException(e);
078: } finally {
079: close(session);
080: }
081: }
082:
083: /**
084: * @see net.sf.jportlet.portlets.bookmark.BookmarkManager#updateBookmark(java.lang.String, java.lang.String, java.net.URL, java.lang.String)
085: */
086: public void updateBookmark(String id, String title, URL url,
087: String description) throws ObjectNotFoundException {
088: Session session = null;
089: Transaction tx = null;
090:
091: try {
092: session = _sessionFactory.openSession();
093:
094: BookmarkImpl bk = (BookmarkImpl) session.load(
095: BookmarkImpl.class, id);
096: bk.setTitle(title);
097: bk.setUrl(url.toString());
098: bk.setDescription(description);
099:
100: tx = session.beginTransaction();
101: session.update(bk);
102: tx.commit();
103: } catch (cirrus.hibernate.ObjectNotFoundException o) {
104: throw new ObjectNotFoundException("Bookmark#" + id
105: + " not found");
106: } catch (HibernateException e) {
107: rollback(tx);
108: throw new PersistenceException(e);
109: } catch (SQLException e) {
110: rollback(tx);
111: throw new PersistenceException(e);
112: } finally {
113: close(session);
114: }
115: }
116:
117: /**
118: * @see net.sf.jportlet.portlets.bookmark.BookmarkManager#deleteBookmarks(java.lang.String[])
119: */
120: public void deleteBookmarks(String ids[]) {
121: Session session = null;
122: Transaction tx = null;
123:
124: try {
125: session = _sessionFactory.openSession();
126:
127: tx = session.beginTransaction();
128: for (int i = 0; i < ids.length; i++) {
129: BookmarkImpl bk = (BookmarkImpl) session.load(
130: BookmarkImpl.class, ids[i]);
131: try {
132: session.delete(bk);
133: } catch (ObjectDeletedException o) {
134: o.printStackTrace();
135: }
136: }
137:
138: tx.commit();
139: } catch (HibernateException e) {
140: rollback(tx);
141: throw new PersistenceException(e);
142: } catch (SQLException e) {
143: rollback(tx);
144: throw new PersistenceException(e);
145: } finally {
146: close(session);
147: }
148: }
149:
150: /**
151: * @see net.sf.jportlet.portlets.bookmark.BookmarkManager#findBookmark(java.lang.String)
152: */
153: public Bookmark findBookmark(String id)
154: throws ObjectNotFoundException {
155: Session session = null;
156:
157: try {
158: session = _sessionFactory.openSession();
159: BookmarkImpl bookmark = (BookmarkImpl) session.load(
160: BookmarkImpl.class, id);
161:
162: /*
163: * Call a function so that the proxy get effectively loaded.
164: * This will avoir LazyInstantiation error
165: */
166: bookmark.getTitle();
167:
168: return bookmark;
169: } catch (cirrus.hibernate.ObjectNotFoundException o) {
170: throw new ObjectNotFoundException("Bookmark#" + id
171: + " not found");
172: } catch (HibernateException e) {
173: throw new PersistenceException(e);
174: } catch (SQLException e) {
175: throw new PersistenceException(e);
176: } finally {
177: close(session);
178: }
179: }
180:
181: /**
182: * @see net.sf.jportlet.portlets.bookmark.BookmarkManager#findBookmarks(net.sf.jportlet.portlet.User, java.lang.String)
183: */
184: public Collection findBookmarks(User user) {
185: return findBookmarks(user, -1);
186: }
187:
188: /**
189: * @see net.sf.jportlet.portlets.bookmark.BookmarkManager#findBookmarks(net.sf.jportlet.portlet.User, int, java.lang.String)
190: */
191: public Collection findBookmarks(User user, int maxsize) {
192: Session session = null;
193:
194: try {
195: session = _sessionFactory.openSession();
196:
197: String oql;
198: Collection col;
199: if (user != null) {
200: oql = "FROM b IN CLASS " + BookmarkImpl.class.getName()
201: + " WHERE b.userId=? ORDER BY b.title";
202: col = session.find(oql, user.getId(), Hibernate.STRING);
203: } else {
204: oql = "FROM b IN CLASS " + BookmarkImpl.class.getName()
205: + " WHERE b.userId IS NULL ORDER BY b.title";
206: col = session.find(oql);
207: }
208:
209: if (maxsize >= 0) {
210: ArrayList lst = new ArrayList();
211: Iterator it = col.iterator();
212: for (int i = 0; (i < maxsize) && it.hasNext(); i++) {
213: lst.add(it.next());
214: }
215:
216: return lst;
217: } else {
218: return col;
219: }
220: } catch (HibernateException e) {
221: throw new PersistenceException(e);
222: } catch (SQLException e) {
223: throw new PersistenceException(e);
224: } finally {
225: close(session);
226: }
227: }
228:
229: private void close(Session session) throws PersistenceException {
230: if (session != null) {
231: try {
232: session.close();
233: } catch (Exception e) {
234: throw new PersistenceException(
235: "Error while closing the session", e);
236: }
237: }
238: }
239:
240: private void rollback(Transaction tx) throws PersistenceException {
241: if (tx != null) {
242: try {
243: tx.rollback();
244: } catch (Exception ee) {
245: throw new PersistenceException(
246: "Error while roll-backing the transaction", ee);
247: }
248: }
249: }
250: }
|