001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.dao;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024:
025: import java.sql.Connection;
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028:
029: /**
030: * <a href="CounterDAO.java.html"><b><i>View Source</i></b></a>
031: *
032: * @author Brian Wing Shun Chan
033: *
034: */
035: public class CounterDAO {
036:
037: public static String COUNTER_TABLE = "Counter";
038:
039: public synchronized static long increment(String location,
040: String rowName) throws DataAccessException {
041:
042: return increment(location, COUNTER_TABLE, rowName);
043: }
044:
045: public synchronized static long increment(String location,
046: String tableName, String rowName)
047: throws DataAccessException {
048:
049: long currentId = 0;
050:
051: Connection con = null;
052: PreparedStatement ps = null;
053: ResultSet rs = null;
054:
055: try {
056: con = DataAccess.getConnection(location);
057:
058: StringMaker query = new StringMaker();
059:
060: query.append("SELECT currentId FROM " + tableName
061: + " WHERE name = ?");
062:
063: ps = con.prepareStatement(query.toString());
064:
065: ps.setString(1, rowName);
066:
067: rs = ps.executeQuery();
068:
069: while (rs.next()) {
070: currentId = rs.getInt(1);
071: }
072:
073: if (currentId == 0) {
074: ps = con.prepareStatement("INSERT INTO " + tableName
075: + " (name, currentId) VALUES (?, ?)");
076:
077: ps.setString(1, rowName);
078: ps.setLong(2, ++currentId);
079:
080: ps.executeUpdate();
081: } else {
082: ps = con.prepareStatement("UPDATE " + tableName
083: + " SET currentId = ? WHERE name = ?");
084:
085: ps.setLong(1, ++currentId);
086: ps.setString(2, rowName);
087:
088: ps.executeUpdate();
089: }
090: } catch (Exception e) {
091: throw new DataAccessException(e);
092: } finally {
093: DataAccess.cleanUp(con, ps, rs);
094: }
095:
096: return currentId;
097: }
098:
099: public synchronized static void reset(String location,
100: String rowName) throws DataAccessException {
101:
102: reset(location, COUNTER_TABLE, rowName);
103: }
104:
105: public synchronized static void reset(String location,
106: String tableName, String rowName)
107: throws DataAccessException {
108:
109: Connection con = null;
110: PreparedStatement ps = null;
111: ResultSet rs = null;
112:
113: try {
114: con = DataAccess.getConnection(location);
115:
116: StringMaker update = new StringMaker();
117:
118: update.append("DELETE FROM " + tableName
119: + " WHERE name = ?");
120:
121: ps = con.prepareStatement(update.toString());
122:
123: ps.setString(1, rowName);
124:
125: ps.executeUpdate();
126: } catch (Exception e) {
127: throw new DataAccessException(e);
128: } finally {
129: DataAccess.cleanUp(con, ps, rs);
130: }
131: }
132:
133: }
|