Source Code Cross Referenced for BookmarkManagerImpl.java in  » Portal » jportlet » net » sf » jportlet » portlets » bookmark » impl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » jportlet » net.sf.jportlet.portlets.bookmark.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.