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.counter.service.persistence;
022:
023: import com.liferay.counter.model.Counter;
024: import com.liferay.counter.model.CounterRegister;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.service.persistence.BasePersistence;
028: import com.liferay.portal.spring.hibernate.HibernateUtil;
029: import com.liferay.portal.util.PropsUtil;
030:
031: import java.util.ArrayList;
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Map;
037:
038: import org.hibernate.LockMode;
039: import org.hibernate.ObjectNotFoundException;
040: import org.hibernate.Query;
041: import org.hibernate.Session;
042:
043: /**
044: * <a href="CounterPersistence.java.html"><b><i>View Source</i></b></a>
045: *
046: * @author Brian Wing Shun Chan
047: * @author Harry Mark
048: *
049: */
050: public class CounterPersistence extends BasePersistence {
051:
052: public CounterPersistence() {
053: }
054:
055: public List getNames() throws SystemException {
056: Session session = null;
057:
058: try {
059: session = openSession();
060:
061: List list = new ArrayList();
062:
063: Query q = session.createQuery("FROM "
064: + Counter.class.getName());
065:
066: Iterator itr = q.iterate();
067:
068: while (itr.hasNext()) {
069: Counter counter = (Counter) itr.next();
070:
071: list.add(counter.getName());
072: }
073:
074: Collections.sort(list);
075:
076: return list;
077: } catch (Exception e) {
078: throw HibernateUtil.processException(e);
079: } finally {
080: closeSession(session);
081: }
082: }
083:
084: public long increment() throws SystemException {
085: return increment(_NAME);
086: }
087:
088: public long increment(String name) throws SystemException {
089: return increment(name, _MINIMUM_INCREMENT_SIZE);
090: }
091:
092: public long increment(String name, int size) throws SystemException {
093:
094: if (size < _MINIMUM_INCREMENT_SIZE) {
095: size = _MINIMUM_INCREMENT_SIZE;
096: }
097:
098: CounterRegister register = getCounterRegister(name);
099:
100: synchronized (register) {
101: long newValue = register.getCurrentValue() + size;
102:
103: if (newValue > register.getRangeMax()) {
104: Session session = null;
105:
106: try {
107: session = openSession();
108:
109: Counter counter = (Counter) session.get(
110: Counter.class, register.getName(),
111: LockMode.UPGRADE);
112:
113: newValue = counter.getCurrentId() + 1;
114:
115: long rangeMax = counter.getCurrentId()
116: + register.getRangeSize();
117:
118: counter.setCurrentId(rangeMax);
119:
120: session.save(counter);
121: session.flush();
122:
123: register.setCurrentValue(newValue);
124: register.setRangeMax(rangeMax);
125: } catch (Exception e) {
126: throw HibernateUtil.processException(e);
127: } finally {
128: closeSession(session);
129: }
130: } else {
131: register.setCurrentValue(newValue);
132: }
133:
134: return newValue;
135: }
136: }
137:
138: public void rename(String oldName, String newName)
139: throws SystemException {
140:
141: CounterRegister register = getCounterRegister(oldName);
142:
143: synchronized (register) {
144: if (registerLookup.containsKey(newName)) {
145: throw new SystemException("Cannot rename " + oldName
146: + " to " + newName);
147: }
148:
149: Session session = null;
150:
151: try {
152: session = openSession();
153:
154: Counter counter = (Counter) session.load(Counter.class,
155: oldName);
156:
157: long currentId = counter.getCurrentId();
158:
159: session.delete(counter);
160:
161: counter = new Counter();
162:
163: counter.setName(newName);
164: counter.setCurrentId(currentId);
165:
166: session.save(counter);
167:
168: session.flush();
169: } catch (ObjectNotFoundException onfe) {
170: } catch (Exception e) {
171: throw HibernateUtil.processException(e);
172: } finally {
173: closeSession(session);
174: }
175:
176: register.setName(newName);
177:
178: registerLookup.put(newName, register);
179: registerLookup.remove(oldName);
180: }
181: }
182:
183: public void reset(String name) throws SystemException {
184: CounterRegister register = getCounterRegister(name);
185:
186: synchronized (register) {
187: Session session = null;
188:
189: try {
190: session = openSession();
191:
192: Counter counter = (Counter) session.load(Counter.class,
193: name);
194:
195: session.delete(counter);
196:
197: session.flush();
198: } catch (ObjectNotFoundException onfe) {
199: } catch (Exception e) {
200: throw HibernateUtil.processException(e);
201: } finally {
202: closeSession(session);
203: }
204:
205: registerLookup.remove(name);
206: }
207: }
208:
209: public void reset(String name, long size) throws SystemException {
210: CounterRegister register = createCounterRegister(name, size);
211:
212: synchronized (register) {
213: registerLookup.put(name, register);
214: }
215: }
216:
217: protected synchronized CounterRegister getCounterRegister(
218: String name) throws SystemException {
219:
220: CounterRegister register = (CounterRegister) registerLookup
221: .get(name);
222:
223: if (register == null) {
224: register = createCounterRegister(name);
225:
226: registerLookup.put(name, register);
227: }
228:
229: return register;
230: }
231:
232: protected synchronized CounterRegister createCounterRegister(
233: String name) throws SystemException {
234:
235: return createCounterRegister(name, -1);
236: }
237:
238: protected synchronized CounterRegister createCounterRegister(
239: String name, long size) throws SystemException {
240:
241: long rangeMin = 0;
242: long rangeMax = 0;
243:
244: Session session = null;
245:
246: try {
247: session = openSession();
248:
249: Counter counter = (Counter) session.get(Counter.class,
250: name, LockMode.UPGRADE);
251:
252: if (counter == null) {
253: rangeMin = _DEFAULT_CURRENT_ID;
254:
255: counter = new Counter();
256:
257: counter.setName(name);
258: } else {
259: rangeMin = counter.getCurrentId();
260: }
261:
262: if (size >= _DEFAULT_CURRENT_ID) {
263: rangeMin = size;
264: }
265:
266: rangeMax = rangeMin + _COUNTER_INCREMENT;
267:
268: counter.setCurrentId(rangeMax);
269:
270: session.save(counter);
271: session.flush();
272: } finally {
273: closeSession(session);
274: }
275:
276: CounterRegister register = new CounterRegister(name, rangeMin,
277: rangeMax, _COUNTER_INCREMENT);
278:
279: return register;
280: }
281:
282: private static final int _DEFAULT_CURRENT_ID = 0;
283:
284: private static final int _MINIMUM_INCREMENT_SIZE = 1;
285:
286: private static final int _COUNTER_INCREMENT = GetterUtil
287: .getInteger(PropsUtil.get(PropsUtil.COUNTER_INCREMENT),
288: _MINIMUM_INCREMENT_SIZE);
289:
290: private static final String _NAME = Counter.class.getName();
291:
292: private static Map registerLookup = new HashMap();
293:
294: }
|