Source Code Cross Referenced for RDBMPortletPreferencesStore.java in  » Portal » uPortal_rel-2-6-1-GA » org » jasig » portal » 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 » uPortal_rel 2 6 1 GA » org.jasig.portal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright 2001, 2002 The JA-SIG Collaborative.  All rights reserved.
002:         *  See license distributed with this file and
003:         *  available online at http://www.uportal.org/license.html
004:         */
005:
006:        package org.jasig.portal;
007:
008:        import java.io.StringReader;
009:        import java.sql.Connection;
010:        import java.sql.PreparedStatement;
011:        import java.sql.ResultSet;
012:        import java.sql.SQLException;
013:        import java.util.HashMap;
014:        import java.util.Iterator;
015:        import java.util.LinkedList;
016:        import java.util.List;
017:        import java.util.Map;
018:
019:        import org.apache.commons.logging.LogFactory;
020:        import org.apache.commons.logging.Log;
021:        import org.apache.pluto.om.common.Preference;
022:        import org.apache.pluto.om.common.PreferenceSet;
023:        import org.jasig.portal.container.om.common.PreferenceSetImpl;
024:        import org.jasig.portal.utils.CounterStoreFactory;
025:        import org.jasig.portal.utils.ICounterStore;
026:
027:        /**
028:         * An implementation of IPortletPreferencesStore which uses a RDBM data source to
029:         * persist the data.
030:         *
031:         * @author Eric Dalquist <a href="mailto:edalquist@unicon.net">edalquist@unicon.net </a>
032:         * @version $Revision: 42091 $
033:         */
034:        public class RDBMPortletPreferencesStore implements 
035:                IPortletPreferencesStore {
036:
037:            private static final Log log = LogFactory
038:                    .getLog(RDBMPortletPreferencesStore.class);
039:
040:            private static final String READ_ONLY_TRUE = "Y";
041:            private static final String READ_ONLY_FALSE = "N";
042:            private static final String UP_PORTLET_PREFERENCE_VALUE = "UP_PORTLET_PREFERENCE_VALUE";
043:            private static final String PREFIX = "UP_PORTLET_PREF_PREFIX__";
044:
045:            /**
046:             * @see org.jasig.portal.IPortletPreferencesStore#setDefinitionPreferences(int, org.apache.pluto.om.common.PreferenceSet)
047:             */
048:            public void setDefinitionPreferences(final int chanId,
049:                    final PreferenceSet prefs) throws Exception {
050:                final Connection con = RDBMServices.getConnection();
051:
052:                final String selectPrefIds = "SELECT PREF_ID "
053:                        + "FROM UP_PORTLET_DEFINITION_PREFS "
054:                        + "WHERE CHAN_ID=?";
055:
056:                final String deletePrefValues = "DELETE FROM UP_PORTLET_PREF_VALUES "
057:                        + "WHERE PREF_ID=?";
058:
059:                final String deletePrefNames = "DELETE FROM UP_PORTLET_DEFINITION_PREFS "
060:                        + "WHERE CHAN_ID=?";
061:
062:                final String insertPrefName = "INSERT INTO UP_PORTLET_DEFINITION_PREFS "
063:                        + "(CHAN_ID, PORTLET_PREF_NAME, PORTLET_PREF_READONLY, PREF_ID) "
064:                        + "VALUES (?, ?, ?, ?)";
065:
066:                final String insertPrefValue = "INSERT INTO UP_PORTLET_PREF_VALUES "
067:                        + "(PREF_ID, PORTLET_PREF_VALUE) " + "VALUES (?, ?)";
068:
069:                if (log.isDebugEnabled())
070:                    log
071:                            .debug("RDBMPortletPreferencesStore::setDefinitionPreferences(chanId="
072:                                    + chanId + ")");
073:
074:                try {
075:                    // Set autocommit false for the connection
076:                    if (RDBMServices.getDbMetaData().supportsTransactions())
077:                        RDBMServices.setAutoCommit(con, false);
078:
079:                    PreparedStatement selectPrefIdsPstmt = null;
080:                    PreparedStatement deletePrefValuesPstmt = null;
081:                    PreparedStatement deletePrefNamesPstmt = null;
082:                    PreparedStatement insertPrefNamePstmt = null;
083:                    PreparedStatement insertPrefValuePstmt = null;
084:
085:                    try {
086:                        final LinkedList prefIds = new LinkedList();
087:
088:                        selectPrefIdsPstmt = con
089:                                .prepareStatement(selectPrefIds);
090:                        deletePrefValuesPstmt = con
091:                                .prepareStatement(deletePrefValues);
092:                        deletePrefNamesPstmt = con
093:                                .prepareStatement(deletePrefNames);
094:                        insertPrefNamePstmt = con
095:                                .prepareStatement(insertPrefName);
096:                        insertPrefValuePstmt = con
097:                                .prepareStatement(insertPrefValue);
098:
099:                        //Get a list of all the preference names for this portlet
100:                        if (log.isDebugEnabled())
101:                            log
102:                                    .debug("RDBMPortletPreferencesStore::setDefinitionPreferences(): "
103:                                            + selectPrefIds);
104:                        selectPrefIdsPstmt.setInt(1, chanId);
105:                        ResultSet rs = selectPrefIdsPstmt.executeQuery();
106:
107:                        //Go through and remove all the values. Catalog the removed pref_id's so they can be re-used so
108:                        //the counter doesn't have to get hammered as much
109:                        try {
110:
111:                            while (rs.next()) {
112:                                int prefId = rs.getInt("PREF_ID");
113:                                prefIds.add(new Integer(prefId));
114:
115:                                if (log.isDebugEnabled())
116:                                    log
117:                                            .debug("RDBMPortletPreferencesStore::setDefinitionPreferences(PREF_ID="
118:                                                    + prefId
119:                                                    + "): "
120:                                                    + deletePrefValues);
121:                                deletePrefValuesPstmt.setInt(1, prefId);
122:                                deletePrefValuesPstmt.executeUpdate();
123:                            }
124:                        } finally {
125:                            try {
126:                                rs.close();
127:                            } catch (Exception e) {
128:                            }
129:                            ;
130:                        }
131:
132:                        //Delete all the preference names for this portlet
133:                        if (log.isDebugEnabled())
134:                            log
135:                                    .debug("RDBMPortletPreferencesStore::setDefinitionPreferences(): "
136:                                            + deletePrefNames);
137:                        deletePrefNamesPstmt.setInt(1, chanId);
138:                        deletePrefNamesPstmt.executeUpdate();
139:
140:                        //Loop through the prefs, inserting each name then the values
141:                        for (Iterator prefItr = prefs.iterator(); prefItr
142:                                .hasNext();) {
143:
144:                            final Preference pref = (Preference) prefItr.next();
145:                            int prefId = -1;
146:
147:                            insertPrefNamePstmt.setInt(1, chanId);
148:                            insertPrefNamePstmt.setString(2, pref.getName());
149:
150:                            if (pref.isReadOnly()) {
151:                                insertPrefNamePstmt
152:                                        .setString(3, READ_ONLY_TRUE);
153:                            } else {
154:                                insertPrefNamePstmt.setString(3,
155:                                        READ_ONLY_FALSE);
156:                            }
157:
158:                            //Use the list of removed ids to re-use IDs before generating new ones
159:                            if (prefIds.size() > 0) {
160:                                prefId = ((Integer) prefIds.removeLast())
161:                                        .intValue();
162:                            } else {
163:                                final ICounterStore counterStore = CounterStoreFactory
164:                                        .getCounterStoreImpl();
165:                                prefId = counterStore
166:                                        .getIncrementIntegerId(UP_PORTLET_PREFERENCE_VALUE);
167:                            }
168:
169:                            insertPrefNamePstmt.setInt(4, prefId);
170:
171:                            //Insert the name row
172:                            if (log.isDebugEnabled())
173:                                log
174:                                        .debug("RDBMPortletPreferencesStore::setDefinitionPreferences(): "
175:                                                + insertPrefName);
176:                            insertPrefNamePstmt.executeUpdate();
177:
178:                            //For each value a row will be inserted in the values table
179:                            for (final Iterator valueItr = pref.getValues(); valueItr
180:                                    .hasNext();) {
181:                                String value = (String) valueItr.next();
182:
183:                                if (log.isDebugEnabled())
184:                                    log
185:                                            .debug("RDBMPortletPreferencesStore::setDefinitionPreferences(): "
186:                                                    + insertPrefValue);
187:                                insertPrefValuePstmt.setInt(1, prefId);
188:                                insertPrefValuePstmt.setString(2, this 
189:                                        .prefixValue(value));
190:                                insertPrefValuePstmt.executeUpdate();
191:                            }
192:                        }
193:
194:                        if (RDBMServices.getDbMetaData().supportsTransactions())
195:                            RDBMServices.commit(con);
196:                    } catch (Exception e) {
197:                        // Roll back the transaction
198:                        if (RDBMServices.getDbMetaData().supportsTransactions())
199:                            RDBMServices.rollback(con);
200:                        throw e;
201:                    } finally {
202:                        try {
203:                            selectPrefIdsPstmt.close();
204:                        } catch (Exception e) {
205:                        }
206:                        try {
207:                            deletePrefValuesPstmt.close();
208:                        } catch (Exception e) {
209:                        }
210:                        try {
211:                            deletePrefNamesPstmt.close();
212:                        } catch (Exception e) {
213:                        }
214:                        try {
215:                            insertPrefNamePstmt.close();
216:                        } catch (Exception e) {
217:                        }
218:                        try {
219:                            insertPrefValuePstmt.close();
220:                        } catch (Exception e) {
221:                        }
222:                    }
223:                } finally {
224:                    RDBMServices.releaseConnection(con);
225:                }
226:            }
227:
228:            /**
229:             * @see org.jasig.portal.IPortletPreferencesStore#getDefinitionPreferences(int)
230:             */
231:            public PreferenceSet getDefinitionPreferences(final int chanId)
232:                    throws Exception {
233:                final PreferenceSetImpl prefs = new PreferenceSetImpl();
234:                final Connection con = RDBMServices.getConnection();
235:
236:                final String selectPrefs = "SELECT UPDP.PORTLET_PREF_NAME, UPDP.PORTLET_PREF_READONLY, UPPV.PORTLET_PREF_VALUE "
237:                        + "FROM UP_PORTLET_DEFINITION_PREFS UPDP, UP_PORTLET_PREF_VALUES UPPV "
238:                        + "WHERE UPDP.PREF_ID=UPPV.PREF_ID AND CHAN_ID=?";
239:
240:                if (log.isDebugEnabled())
241:                    log
242:                            .debug("RDBMPortletPreferencesStore::getDefinitionPreferences(chanId="
243:                                    + chanId + ")");
244:
245:                try {
246:                    PreparedStatement selectCurrentPrefsPstmt = null;
247:
248:                    try {
249:                        selectCurrentPrefsPstmt = con
250:                                .prepareStatement(selectPrefs);
251:
252:                        if (log.isDebugEnabled())
253:                            log
254:                                    .debug("RDBMPortletPreferencesStore::getDefinitionPreferences(): "
255:                                            + selectPrefs);
256:                        selectCurrentPrefsPstmt.setInt(1, chanId);
257:                        final ResultSet rs = selectCurrentPrefsPstmt
258:                                .executeQuery();
259:
260:                        final Map prefsBuilder = new HashMap();
261:                        final Map readOnlyMap = new HashMap();
262:
263:                        try {
264:                            while (rs.next()) {
265:                                final String prefName = rs
266:                                        .getString("PORTLET_PREF_NAME");
267:                                final String prefReadOnly = rs
268:                                        .getString("PORTLET_PREF_READONLY");
269:                                final String prefValue = this .unPrefixValue(rs
270:                                        .getString("PORTLET_PREF_VALUE"));
271:
272:                                if (!readOnlyMap.containsKey(prefName)) {
273:                                    if (READ_ONLY_TRUE.equals(prefReadOnly)) {
274:                                        readOnlyMap.put(prefName, Boolean.TRUE);
275:                                    } else {
276:                                        readOnlyMap
277:                                                .put(prefName, Boolean.FALSE);
278:                                    }
279:                                }
280:
281:                                List prefList = (List) prefsBuilder
282:                                        .get(prefName);
283:
284:                                if (prefList == null) {
285:                                    prefList = new LinkedList();
286:                                    prefsBuilder.put(prefName, prefList);
287:                                }
288:
289:                                prefList.add(prefValue);
290:                            }
291:                        } finally {
292:                            try {
293:                                rs.close();
294:                            } catch (Exception e) {
295:                            }
296:                        }
297:
298:                        for (final Iterator prefKeyItr = prefsBuilder.keySet()
299:                                .iterator(); prefKeyItr.hasNext();) {
300:                            final String prefName = (String) prefKeyItr.next();
301:                            final List prefValues = (List) prefsBuilder
302:                                    .get(prefName);
303:                            final boolean readOnly = Boolean.TRUE
304:                                    .equals(readOnlyMap.get(prefName));
305:
306:                            prefs.add(prefName, prefValues, readOnly);
307:                        }
308:                    } finally {
309:                        try {
310:                            selectCurrentPrefsPstmt.close();
311:                        } catch (Exception e) {
312:                        }
313:                    }
314:                } finally {
315:                    RDBMServices.releaseConnection(con);
316:                }
317:
318:                return prefs;
319:            }
320:
321:            /**
322:             * @see org.jasig.portal.IPortletPreferencesStore#setEntityPreferences(int, int, String, org.apache.pluto.om.common.PreferenceSet)
323:             */
324:            public void setEntityPreferences(final int userId,
325:                    final int layoutId, final String chanDescId,
326:                    final PreferenceSet prefs) throws Exception {
327:                final Connection con = RDBMServices.getConnection();
328:
329:                final String selectPrefIds = "SELECT PREF_ID "
330:                        + "FROM UP_PORTLET_ENTITY_PREFS "
331:                        + "WHERE USER_ID=? AND LAYOUT_ID=? AND CHAN_DESC_ID=?";
332:
333:                final String deletePrefValues = "DELETE FROM UP_PORTLET_PREF_VALUES "
334:                        + "WHERE PREF_ID=?";
335:
336:                final String deletePrefNames = "DELETE FROM UP_PORTLET_ENTITY_PREFS "
337:                        + "WHERE USER_ID=? AND LAYOUT_ID=? AND CHAN_DESC_ID=?";
338:
339:                final String insertPrefName = "INSERT INTO UP_PORTLET_ENTITY_PREFS "
340:                        + "(USER_ID, LAYOUT_ID, CHAN_DESC_ID, PORTLET_PREF_NAME, PREF_ID) "
341:                        + "VALUES (?, ?, ?, ?, ?)";
342:
343:                final String insertPrefValue = "INSERT INTO UP_PORTLET_PREF_VALUES "
344:                        + "(PREF_ID, PORTLET_PREF_VALUE) " + "VALUES (?, ?)";
345:
346:                if (log.isDebugEnabled())
347:                    log
348:                            .debug("RDBMPortletPreferencesStore::setEntityPreferences(userId="
349:                                    + userId
350:                                    + ", layoutId="
351:                                    + layoutId
352:                                    + ", chanDescId=" + chanDescId + ")");
353:
354:                try {
355:                    // Set autocommit false for the connection
356:                    if (RDBMServices.getDbMetaData().supportsTransactions())
357:                        RDBMServices.setAutoCommit(con, false);
358:
359:                    PreparedStatement selectPrefIdsPstmt = null;
360:                    PreparedStatement deletePrefValuesPstmt = null;
361:                    PreparedStatement deletePrefNamesPstmt = null;
362:                    PreparedStatement insertPrefNamePstmt = null;
363:                    PreparedStatement insertPrefValuePstmt = null;
364:
365:                    try {
366:                        final LinkedList prefIds = new LinkedList();
367:
368:                        selectPrefIdsPstmt = con
369:                                .prepareStatement(selectPrefIds);
370:                        deletePrefValuesPstmt = con
371:                                .prepareStatement(deletePrefValues);
372:                        deletePrefNamesPstmt = con
373:                                .prepareStatement(deletePrefNames);
374:                        insertPrefNamePstmt = con
375:                                .prepareStatement(insertPrefName);
376:                        insertPrefValuePstmt = con
377:                                .prepareStatement(insertPrefValue);
378:
379:                        //Get a list of all the preference names for this instance of this portlet
380:                        if (log.isDebugEnabled())
381:                            log
382:                                    .debug("RDBMPortletPreferencesStore::setEntityPreferences(): "
383:                                            + selectPrefIds);
384:                        selectPrefIdsPstmt.setInt(1, userId);
385:                        selectPrefIdsPstmt.setInt(2, layoutId);
386:                        selectPrefIdsPstmt.setString(3, chanDescId);
387:                        ResultSet rs = selectPrefIdsPstmt.executeQuery();
388:
389:                        //Go through and remove all the values. Catalog the removed pref_id's so they can be re-used so
390:                        //the counter doesn't have to get hammered as much
391:                        try {
392:                            while (rs.next()) {
393:                                int prefId = rs.getInt("PREF_ID");
394:                                prefIds.add(new Integer(prefId));
395:
396:                                if (log.isDebugEnabled())
397:                                    log
398:                                            .debug("RDBMPortletPreferencesStore::setEntityPreferences(PREF_ID="
399:                                                    + prefId
400:                                                    + "): "
401:                                                    + deletePrefValues);
402:                                deletePrefValuesPstmt.setInt(1, prefId);
403:                                deletePrefValuesPstmt.executeUpdate();
404:                            }
405:                        } finally {
406:                            try {
407:                                rs.close();
408:                            } catch (Exception e) {
409:                            }
410:                            ;
411:                        }
412:
413:                        //Delete all the preference names for this instance of this portlet
414:                        if (log.isDebugEnabled())
415:                            log
416:                                    .debug("RDBMPortletPreferencesStore::setEntityPreferences(): "
417:                                            + deletePrefNames);
418:                        deletePrefNamesPstmt.setInt(1, userId);
419:                        deletePrefNamesPstmt.setInt(2, layoutId);
420:                        deletePrefNamesPstmt.setString(3, chanDescId);
421:                        deletePrefNamesPstmt.executeUpdate();
422:
423:                        //Loop through the prefs, inserting each name then the values
424:                        for (Iterator prefItr = prefs.iterator(); prefItr
425:                                .hasNext();) {
426:                            final Preference pref = (Preference) prefItr.next();
427:                            int prefId = -1;
428:
429:                            insertPrefNamePstmt.setInt(1, userId);
430:                            insertPrefNamePstmt.setInt(2, layoutId);
431:                            insertPrefNamePstmt.setString(3, chanDescId);
432:                            insertPrefNamePstmt.setString(4, pref.getName());
433:
434:                            //Use the list of removed ids to re-use IDs before generating new ones
435:                            if (prefIds.size() > 0) {
436:                                prefId = ((Integer) prefIds.removeLast())
437:                                        .intValue();
438:                            } else {
439:                                final ICounterStore counterStore = CounterStoreFactory
440:                                        .getCounterStoreImpl();
441:
442:                                try {
443:                                    prefId = counterStore
444:                                            .getIncrementIntegerId(UP_PORTLET_PREFERENCE_VALUE);
445:                                } catch (Exception e) {
446:                                    counterStore
447:                                            .createCounter(UP_PORTLET_PREFERENCE_VALUE);
448:                                    prefId = counterStore
449:                                            .getIncrementIntegerId(UP_PORTLET_PREFERENCE_VALUE);
450:                                }
451:                            }
452:
453:                            insertPrefNamePstmt.setInt(5, prefId);
454:
455:                            //Insert the name row
456:                            if (log.isDebugEnabled())
457:                                log
458:                                        .debug("RDBMPortletPreferencesStore::setEntityPreferences(): "
459:                                                + insertPrefName);
460:                            insertPrefNamePstmt.executeUpdate();
461:
462:                            //For each value a row will be inserted in the values table
463:                            for (final Iterator valueItr = pref.getValues(); valueItr
464:                                    .hasNext();) {
465:                                String value = (String) valueItr.next();
466:                                if (log.isDebugEnabled())
467:                                    log
468:                                            .debug("RDBMPortletPreferencesStore::setEntityPreferences(): "
469:                                                    + insertPrefValue);
470:                                final String prefValue = this 
471:                                        .prefixValue(value);
472:                                try {
473:                                    insertPrefValuePstmt.setInt(1, prefId);
474:                                    insertPrefValuePstmt
475:                                            .setString(2, prefValue);
476:                                    insertPrefValuePstmt.executeUpdate();
477:                                } catch (SQLException e) {
478:                                    // Oracle fails if you try to process a string longer than 4k
479:                                    insertPrefValuePstmt.clearParameters();
480:                                    insertPrefValuePstmt.setInt(1, prefId);
481:                                    insertPrefValuePstmt.setCharacterStream(2,
482:                                            new StringReader(prefValue),
483:                                            prefValue.length());
484:                                    insertPrefValuePstmt.executeUpdate();
485:                                }
486:                            }
487:                        }
488:
489:                        if (RDBMServices.getDbMetaData().supportsTransactions())
490:                            RDBMServices.commit(con);
491:                    } catch (Exception e) {
492:                        // Roll back the transaction
493:                        if (RDBMServices.getDbMetaData().supportsTransactions())
494:                            RDBMServices.rollback(con);
495:                        throw e;
496:                    } finally {
497:                        try {
498:                            selectPrefIdsPstmt.close();
499:                        } catch (Exception e) {
500:                        }
501:                        try {
502:                            deletePrefValuesPstmt.close();
503:                        } catch (Exception e) {
504:                        }
505:                        try {
506:                            deletePrefNamesPstmt.close();
507:                        } catch (Exception e) {
508:                        }
509:                        try {
510:                            insertPrefNamePstmt.close();
511:                        } catch (Exception e) {
512:                        }
513:                        try {
514:                            insertPrefValuePstmt.close();
515:                        } catch (Exception e) {
516:                        }
517:                    }
518:                } finally {
519:                    RDBMServices.releaseConnection(con);
520:                }
521:            }
522:
523:            /**
524:             * @see org.jasig.portal.IPortletPreferencesStore#getEntityPreferences(int, int, java.lang.String)
525:             */
526:            public PreferenceSet getEntityPreferences(final int userId,
527:                    final int layoutId, final String chanDescId)
528:                    throws Exception {
529:                final PreferenceSetImpl prefs = new PreferenceSetImpl();
530:                final Connection con = RDBMServices.getConnection();
531:
532:                final String selectPrefs = "SELECT UPEP.PORTLET_PREF_NAME, UPPV.PORTLET_PREF_VALUE "
533:                        + "FROM UP_PORTLET_ENTITY_PREFS UPEP, UP_PORTLET_PREF_VALUES UPPV "
534:                        + "WHERE UPEP.PREF_ID=UPPV.PREF_ID AND UPEP.USER_ID=? AND UPEP.LAYOUT_ID=? AND UPEP.CHAN_DESC_ID=?";
535:
536:                if (log.isDebugEnabled())
537:                    log
538:                            .debug("RDBMPortletPreferencesStore::getEntityPreferences(userId="
539:                                    + userId
540:                                    + ", layoutId="
541:                                    + layoutId
542:                                    + ", chanDescId=" + chanDescId + ")");
543:
544:                try {
545:                    PreparedStatement selectCurrentPrefsPstmt = null;
546:
547:                    try {
548:                        selectCurrentPrefsPstmt = con
549:                                .prepareStatement(selectPrefs);
550:
551:                        if (log.isDebugEnabled())
552:                            log
553:                                    .debug("RDBMPortletPreferencesStore::getEntityPreferences(): "
554:                                            + selectPrefs);
555:
556:                        selectCurrentPrefsPstmt.setInt(1, userId);
557:                        selectCurrentPrefsPstmt.setInt(2, layoutId);
558:                        selectCurrentPrefsPstmt.setString(3, chanDescId);
559:                        ResultSet rs = selectCurrentPrefsPstmt.executeQuery();
560:
561:                        final Map prefsBuilder = new HashMap();
562:
563:                        try {
564:                            while (rs.next()) {
565:                                final String prefName = rs
566:                                        .getString("PORTLET_PREF_NAME");
567:                                final String prefValue = this .unPrefixValue(rs
568:                                        .getString("PORTLET_PREF_VALUE"));
569:
570:                                List prefList = (List) prefsBuilder
571:                                        .get(prefName);
572:
573:                                if (prefList == null) {
574:                                    prefList = new LinkedList();
575:                                    prefsBuilder.put(prefName, prefList);
576:                                }
577:
578:                                prefList.add(prefValue);
579:                            }
580:                        } finally {
581:                            try {
582:                                rs.close();
583:                            } catch (Exception e) {
584:                            }
585:                        }
586:
587:                        for (final Iterator prefKeyItr = prefsBuilder.keySet()
588:                                .iterator(); prefKeyItr.hasNext();) {
589:                            final String prefName = (String) prefKeyItr.next();
590:                            final List prefValues = (List) prefsBuilder
591:                                    .get(prefName);
592:
593:                            prefs.add(prefName, prefValues);
594:                        }
595:                    } finally {
596:                        try {
597:                            selectCurrentPrefsPstmt.close();
598:                        } catch (Exception e) {
599:                        }
600:                    }
601:                } finally {
602:                    RDBMServices.releaseConnection(con);
603:                }
604:
605:                return prefs;
606:            }
607:
608:            /**
609:             * @see org.jasig.portal.IPortletPreferencesStore#deletePortletPreferencesByUser(int)
610:             */
611:            public void deletePortletPreferencesByUser(int userId)
612:                    throws Exception {
613:                final Connection con = RDBMServices.getConnection();
614:
615:                final String selectPrefIds = "SELECT PREF_ID "
616:                        + "FROM UP_PORTLET_ENTITY_PREFS " + "WHERE USER_ID=?";
617:
618:                final String deletePrefNames = "DELETE FROM UP_PORTLET_ENTITY_PREFS "
619:                        + "WHERE USER_ID=?";
620:
621:                final String deletePrefValues = "DELETE FROM UP_PORTLET_PREF_VALUES "
622:                        + "WHERE PREF_ID=?";
623:
624:                if (log.isDebugEnabled())
625:                    log
626:                            .debug("RDBMPortletPreferencesStore::deletePortletPreferencesByUser(userId="
627:                                    + userId + ")");
628:
629:                try {
630:                    // Set autocommit false for the connection
631:                    if (RDBMServices.getDbMetaData().supportsTransactions())
632:                        RDBMServices.setAutoCommit(con, false);
633:
634:                    PreparedStatement selectPrefIdsPstmt = null;
635:                    PreparedStatement deletePrefNamesPstmt = null;
636:                    PreparedStatement deletePrefValuesPstmt = null;
637:
638:                    try {
639:                        selectPrefIdsPstmt = con
640:                                .prepareStatement(selectPrefIds);
641:                        deletePrefNamesPstmt = con
642:                                .prepareStatement(deletePrefNames);
643:                        deletePrefValuesPstmt = con
644:                                .prepareStatement(deletePrefValues);
645:
646:                        if (log.isDebugEnabled())
647:                            log
648:                                    .debug("RDBMPortletPreferencesStore::deletePortletPreferencesByUser(): "
649:                                            + selectPrefIds);
650:                        selectPrefIdsPstmt.setInt(1, userId);
651:                        ResultSet rs = selectPrefIdsPstmt.executeQuery();
652:
653:                        try {
654:                            while (rs.next()) {
655:                                int prefId = rs.getInt("PREF_ID");
656:
657:                                if (log.isDebugEnabled())
658:                                    log
659:                                            .debug("RDBMPortletPreferencesStore::deletePortletPreferencesByUser(): "
660:                                                    + deletePrefValues);
661:                                deletePrefValuesPstmt.setInt(1, prefId);
662:                                deletePrefValuesPstmt.executeUpdate();
663:                            }
664:                        } finally {
665:                            try {
666:                                rs.close();
667:                            } catch (Exception e) {
668:                            }
669:                        }
670:
671:                        deletePrefNamesPstmt.setInt(1, userId);
672:                        deletePrefNamesPstmt.executeUpdate();
673:
674:                        if (RDBMServices.getDbMetaData().supportsTransactions())
675:                            RDBMServices.commit(con);
676:                    } catch (Exception e) {
677:                        // Roll back the transaction
678:                        if (RDBMServices.getDbMetaData().supportsTransactions())
679:                            RDBMServices.rollback(con);
680:                        throw e;
681:                    } finally {
682:                        try {
683:                            selectPrefIdsPstmt.close();
684:                        } catch (Exception e) {
685:                        }
686:                        try {
687:                            deletePrefNamesPstmt.close();
688:                        } catch (Exception e) {
689:                        }
690:                        try {
691:                            deletePrefValuesPstmt.close();
692:                        } catch (Exception e) {
693:                        }
694:                    }
695:                } finally {
696:                    RDBMServices.releaseConnection(con);
697:                }
698:            }
699:
700:            /**
701:             * @see org.jasig.portal.IPortletPreferencesStore#deletePortletPreferencesByInstance(int, int, String)
702:             */
703:            public void deletePortletPreferencesByInstance(final int userId,
704:                    final int layoutId, final String chanDescId)
705:                    throws Exception {
706:                final Connection con = RDBMServices.getConnection();
707:
708:                final String selectPrefIds = "SELECT PREF_ID "
709:                        + "FROM UP_PORTLET_ENTITY_PREFS "
710:                        + "WHERE USER_ID=? AND LAYOUT_ID=? AND CHAN_DESC_ID=?";
711:
712:                final String deletePrefNames = "DELETE FROM UP_PORTLET_ENTITY_PREFS "
713:                        + "WHERE USER_ID=? AND LAYOUT_ID=? AND CHAN_DESC_ID=?";
714:
715:                final String deletePrefValues = "DELETE FROM UP_PORTLET_PREF_VALUES "
716:                        + "WHERE PREF_ID=?";
717:
718:                if (log.isDebugEnabled())
719:                    log
720:                            .debug("RDBMPortletPreferencesStore::deletePortletPreferencesByInstance(userId="
721:                                    + userId
722:                                    + ", layoutId="
723:                                    + layoutId
724:                                    + ", chanDescId=" + chanDescId + ")");
725:
726:                try {
727:                    // Set autocommit false for the connection
728:                    if (RDBMServices.getDbMetaData().supportsTransactions())
729:                        RDBMServices.setAutoCommit(con, false);
730:
731:                    PreparedStatement selectPrefIdsPstmt = null;
732:                    PreparedStatement deletePrefNamesPstmt = null;
733:                    PreparedStatement deletePrefValuesPstmt = null;
734:
735:                    try {
736:                        selectPrefIdsPstmt = con
737:                                .prepareStatement(selectPrefIds);
738:                        deletePrefNamesPstmt = con
739:                                .prepareStatement(deletePrefNames);
740:                        deletePrefValuesPstmt = con
741:                                .prepareStatement(deletePrefValues);
742:
743:                        if (log.isDebugEnabled())
744:                            log
745:                                    .debug("RDBMPortletPreferencesStore::deletePortletPreferencesByInstance(): "
746:                                            + selectPrefIds);
747:                        selectPrefIdsPstmt.setInt(1, userId);
748:                        selectPrefIdsPstmt.setInt(2, layoutId);
749:                        selectPrefIdsPstmt.setString(3, chanDescId);
750:                        ResultSet rs = selectPrefIdsPstmt.executeQuery();
751:
752:                        try {
753:                            while (rs.next()) {
754:                                int prefId = rs.getInt("PREF_ID");
755:
756:                                if (log.isDebugEnabled())
757:                                    log
758:                                            .debug("RDBMPortletPreferencesStore::deletePortletPreferencesByInstance(): "
759:                                                    + deletePrefValues);
760:                                deletePrefValuesPstmt.setInt(1, prefId);
761:                                deletePrefValuesPstmt.executeUpdate();
762:                            }
763:                        } finally {
764:                            try {
765:                                rs.close();
766:                            } catch (Exception e) {
767:                            }
768:                        }
769:
770:                        deletePrefNamesPstmt.setInt(1, userId);
771:                        deletePrefNamesPstmt.setInt(2, layoutId);
772:                        deletePrefNamesPstmt.setString(3, chanDescId);
773:                        deletePrefNamesPstmt.executeUpdate();
774:
775:                        if (RDBMServices.getDbMetaData().supportsTransactions())
776:                            RDBMServices.commit(con);
777:                    } catch (Exception e) {
778:                        // Roll back the transaction
779:                        if (RDBMServices.getDbMetaData().supportsTransactions())
780:                            RDBMServices.rollback(con);
781:                        throw e;
782:                    } finally {
783:                        try {
784:                            selectPrefIdsPstmt.close();
785:                        } catch (Exception e) {
786:                        }
787:                        try {
788:                            deletePrefNamesPstmt.close();
789:                        } catch (Exception e) {
790:                        }
791:                        try {
792:                            deletePrefValuesPstmt.close();
793:                        } catch (Exception e) {
794:                        }
795:                    }
796:                } finally {
797:                    RDBMServices.releaseConnection(con);
798:                }
799:            }
800:
801:            private String prefixValue(String value) {
802:                if (value == null)
803:                    return value;
804:                else
805:                    return PREFIX + value;
806:            }
807:
808:            private String unPrefixValue(String value) {
809:                if (value != null && value.startsWith(PREFIX))
810:                    return value.substring(PREFIX.length());
811:                else
812:                    return value;
813:            }
814:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.