001: /* Copyright 2001 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.layout.simple;
007:
008: import java.sql.Connection;
009: import java.sql.ResultSet;
010: import java.sql.Statement;
011:
012: import org.jasig.portal.RDBMServices;
013: import org.jasig.portal.layout.IUserLayoutStore;
014:
015: /**
016: * Oracle optimized SQL implementation of the 2.x relational database model
017: *
018: * Sequence numbers have the form of {Table Name}_SEQ and, at the moment, they must
019: * have been created by hand before uPortal is started. See UP_SEQUENCE in properties/data.xml
020: * for the tables that expect sequence counters, and the expected starting value.
021: *
022: * Prior to uPortal 2.5 this class existed in the package org.jasig.portal. It was moved
023: * to its present package to express that it is part of the Simple Layout Manager
024: * implementation.
025: *
026: * @author George Lindholm
027: * @version $Revision: 35732 $
028: */
029: public final class OracleUserLayoutStore extends RDBMUserLayoutStore
030: implements IUserLayoutStore {
031:
032: public OracleUserLayoutStore() throws Exception {
033: super ();
034: }
035:
036: /* DBCounter */
037: /*
038: * get&increment method.
039: */
040:
041: /**
042: * Get the next incremental value
043: * @param tableName
044: * @exception Exception
045: */
046: public int getIncrementIntegerId(String tableName) throws Exception {
047: int id;
048: Connection con = RDBMServices.getConnection();
049: try {
050: Statement stmt = con.createStatement();
051: try {
052: String sQuery = "SELECT " + tableName
053: + "_SEQ.NEXTVAL FROM DUAL";
054: if (log.isDebugEnabled())
055: log
056: .debug("OracleUserLayoutStore::getIncrementInteger(): "
057: + sQuery);
058: ResultSet rs = stmt.executeQuery(sQuery);
059: try {
060: rs.next(); // If this doesn't work then the database is munged up
061: id = rs.getInt(1);
062: } finally {
063: rs.close();
064: }
065: } finally {
066: stmt.close();
067: }
068: } finally {
069: RDBMServices.releaseConnection(con);
070: }
071: return id;
072: }
073:
074: /**
075: * Create a sequence counter
076: * @param tableName
077: * @exception Exception
078: */
079: public void createCounter(String tableName) throws Exception {
080: createCounter(tableName, 1);
081: }
082:
083: /**
084: * Create a sequence counter, starting with a specific value
085: * @param tableName
086: * @param startAt
087: * @exception Exception
088: */
089: protected void createCounter(String tableName, int startAt)
090: throws Exception {
091: Connection con = RDBMServices.getConnection();
092: try {
093: Statement stmt = con.createStatement();
094: try {
095: String sInsert = "CREATE SEQUENCE " + tableName
096: + "_SEQ INCREMENT BY 1 START WITH " + startAt
097: + " NOMAXVALUE NOCYCLE";
098: log.debug("OracleUserLayoutStore::createCounter(): "
099: + sInsert);
100: stmt.executeUpdate(sInsert);
101: } finally {
102: stmt.close();
103: }
104: } finally {
105: RDBMServices.releaseConnection(con);
106: }
107: }
108:
109: /**
110: * Modify the current value of a counter
111: * @param tableName
112: * @param value
113: * @exception Exception
114: */
115: public synchronized void setCounter(String tableName, int value)
116: throws Exception {
117: Connection con = RDBMServices.getConnection();
118: try {
119: Statement stmt = con.createStatement();
120: try {
121:
122: /* This is dangerous */
123: String sUpdate = "DROP SEQUENCE " + tableName + "_SEQ";
124: log.debug("OracleUserLayoutStore::setCounter(): "
125: + sUpdate);
126: stmt.executeUpdate(sUpdate);
127: createCounter(tableName, value);
128: } finally {
129: stmt.close();
130: }
131: } finally {
132: RDBMServices.releaseConnection(con);
133: }
134: }
135: }
|