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.portal.service.persistence;
022:
023: import com.liferay.portal.NoSuchAccountException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.dao.DynamicQuery;
026: import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
027: import com.liferay.portal.kernel.util.GetterUtil;
028: import com.liferay.portal.kernel.util.OrderByComparator;
029: import com.liferay.portal.kernel.util.StringMaker;
030: import com.liferay.portal.kernel.util.Validator;
031: import com.liferay.portal.model.Account;
032: import com.liferay.portal.model.ModelListener;
033: import com.liferay.portal.model.impl.AccountImpl;
034: import com.liferay.portal.model.impl.AccountModelImpl;
035: import com.liferay.portal.spring.hibernate.FinderCache;
036: import com.liferay.portal.spring.hibernate.HibernateUtil;
037: import com.liferay.portal.util.PropsUtil;
038:
039: import com.liferay.util.dao.hibernate.QueryUtil;
040:
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043:
044: import org.hibernate.Query;
045: import org.hibernate.Session;
046:
047: import java.util.Collections;
048: import java.util.Iterator;
049: import java.util.List;
050:
051: /**
052: * <a href="AccountPersistenceImpl.java.html"><b><i>View Source</i></b></a>
053: *
054: * @author Brian Wing Shun Chan
055: *
056: */
057: public class AccountPersistenceImpl extends BasePersistence implements
058: AccountPersistence {
059: public Account create(long accountId) {
060: Account account = new AccountImpl();
061:
062: account.setNew(true);
063: account.setPrimaryKey(accountId);
064:
065: return account;
066: }
067:
068: public Account remove(long accountId)
069: throws NoSuchAccountException, SystemException {
070: Session session = null;
071:
072: try {
073: session = openSession();
074:
075: Account account = (Account) session.get(AccountImpl.class,
076: new Long(accountId));
077:
078: if (account == null) {
079: if (_log.isWarnEnabled()) {
080: _log.warn("No Account exists with the primary key "
081: + accountId);
082: }
083:
084: throw new NoSuchAccountException(
085: "No Account exists with the primary key "
086: + accountId);
087: }
088:
089: return remove(account);
090: } catch (NoSuchAccountException nsee) {
091: throw nsee;
092: } catch (Exception e) {
093: throw HibernateUtil.processException(e);
094: } finally {
095: closeSession(session);
096: }
097: }
098:
099: public Account remove(Account account) throws SystemException {
100: ModelListener listener = _getListener();
101:
102: if (listener != null) {
103: listener.onBeforeRemove(account);
104: }
105:
106: account = removeImpl(account);
107:
108: if (listener != null) {
109: listener.onAfterRemove(account);
110: }
111:
112: return account;
113: }
114:
115: protected Account removeImpl(Account account)
116: throws SystemException {
117: Session session = null;
118:
119: try {
120: session = openSession();
121:
122: session.delete(account);
123:
124: session.flush();
125:
126: return account;
127: } catch (Exception e) {
128: throw HibernateUtil.processException(e);
129: } finally {
130: closeSession(session);
131:
132: FinderCache.clearCache(Account.class.getName());
133: }
134: }
135:
136: public Account update(Account account) throws SystemException {
137: return update(account, false);
138: }
139:
140: public Account update(Account account, boolean merge)
141: throws SystemException {
142: ModelListener listener = _getListener();
143:
144: boolean isNew = account.isNew();
145:
146: if (listener != null) {
147: if (isNew) {
148: listener.onBeforeCreate(account);
149: } else {
150: listener.onBeforeUpdate(account);
151: }
152: }
153:
154: account = updateImpl(account, merge);
155:
156: if (listener != null) {
157: if (isNew) {
158: listener.onAfterCreate(account);
159: } else {
160: listener.onAfterUpdate(account);
161: }
162: }
163:
164: return account;
165: }
166:
167: public Account updateImpl(com.liferay.portal.model.Account account,
168: boolean merge) throws SystemException {
169: Session session = null;
170:
171: try {
172: session = openSession();
173:
174: if (merge) {
175: session.merge(account);
176: } else {
177: if (account.isNew()) {
178: session.save(account);
179: }
180: }
181:
182: session.flush();
183:
184: account.setNew(false);
185:
186: return account;
187: } catch (Exception e) {
188: throw HibernateUtil.processException(e);
189: } finally {
190: closeSession(session);
191:
192: FinderCache.clearCache(Account.class.getName());
193: }
194: }
195:
196: public Account findByPrimaryKey(long accountId)
197: throws NoSuchAccountException, SystemException {
198: Account account = fetchByPrimaryKey(accountId);
199:
200: if (account == null) {
201: if (_log.isWarnEnabled()) {
202: _log.warn("No Account exists with the primary key "
203: + accountId);
204: }
205:
206: throw new NoSuchAccountException(
207: "No Account exists with the primary key "
208: + accountId);
209: }
210:
211: return account;
212: }
213:
214: public Account fetchByPrimaryKey(long accountId)
215: throws SystemException {
216: Session session = null;
217:
218: try {
219: session = openSession();
220:
221: return (Account) session.get(AccountImpl.class, new Long(
222: accountId));
223: } catch (Exception e) {
224: throw HibernateUtil.processException(e);
225: } finally {
226: closeSession(session);
227: }
228: }
229:
230: public List findWithDynamicQuery(
231: DynamicQueryInitializer queryInitializer)
232: throws SystemException {
233: Session session = null;
234:
235: try {
236: session = openSession();
237:
238: DynamicQuery query = queryInitializer.initialize(session);
239:
240: return query.list();
241: } catch (Exception e) {
242: throw HibernateUtil.processException(e);
243: } finally {
244: closeSession(session);
245: }
246: }
247:
248: public List findWithDynamicQuery(
249: DynamicQueryInitializer queryInitializer, int begin, int end)
250: throws SystemException {
251: Session session = null;
252:
253: try {
254: session = openSession();
255:
256: DynamicQuery query = queryInitializer.initialize(session);
257:
258: query.setLimit(begin, end);
259:
260: return query.list();
261: } catch (Exception e) {
262: throw HibernateUtil.processException(e);
263: } finally {
264: closeSession(session);
265: }
266: }
267:
268: public List findAll() throws SystemException {
269: return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
270: }
271:
272: public List findAll(int begin, int end) throws SystemException {
273: return findAll(begin, end, null);
274: }
275:
276: public List findAll(int begin, int end, OrderByComparator obc)
277: throws SystemException {
278: boolean finderClassNameCacheEnabled = AccountModelImpl.CACHE_ENABLED;
279: String finderClassName = Account.class.getName();
280: String finderMethodName = "findAll";
281: String[] finderParams = new String[] { "java.lang.Integer",
282: "java.lang.Integer",
283: "com.liferay.portal.kernel.util.OrderByComparator" };
284: Object[] finderArgs = new Object[] { String.valueOf(begin),
285: String.valueOf(end), String.valueOf(obc) };
286:
287: Object result = null;
288:
289: if (finderClassNameCacheEnabled) {
290: result = FinderCache.getResult(finderClassName,
291: finderMethodName, finderParams, finderArgs,
292: getSessionFactory());
293: }
294:
295: if (result == null) {
296: Session session = null;
297:
298: try {
299: session = openSession();
300:
301: StringMaker query = new StringMaker();
302:
303: query.append("FROM com.liferay.portal.model.Account ");
304:
305: if (obc != null) {
306: query.append("ORDER BY ");
307: query.append(obc.getOrderBy());
308: }
309:
310: Query q = session.createQuery(query.toString());
311:
312: List list = QueryUtil.list(q, getDialect(), begin, end);
313:
314: if (obc == null) {
315: Collections.sort(list);
316: }
317:
318: FinderCache.putResult(finderClassNameCacheEnabled,
319: finderClassName, finderMethodName,
320: finderParams, finderArgs, list);
321:
322: return list;
323: } catch (Exception e) {
324: throw HibernateUtil.processException(e);
325: } finally {
326: closeSession(session);
327: }
328: } else {
329: return (List) result;
330: }
331: }
332:
333: public void removeAll() throws SystemException {
334: Iterator itr = findAll().iterator();
335:
336: while (itr.hasNext()) {
337: remove((Account) itr.next());
338: }
339: }
340:
341: public int countAll() throws SystemException {
342: boolean finderClassNameCacheEnabled = AccountModelImpl.CACHE_ENABLED;
343: String finderClassName = Account.class.getName();
344: String finderMethodName = "countAll";
345: String[] finderParams = new String[] {};
346: Object[] finderArgs = new Object[] {};
347:
348: Object result = null;
349:
350: if (finderClassNameCacheEnabled) {
351: result = FinderCache.getResult(finderClassName,
352: finderMethodName, finderParams, finderArgs,
353: getSessionFactory());
354: }
355:
356: if (result == null) {
357: Session session = null;
358:
359: try {
360: session = openSession();
361:
362: Query q = session
363: .createQuery("SELECT COUNT(*) FROM com.liferay.portal.model.Account");
364:
365: Long count = null;
366:
367: Iterator itr = q.list().iterator();
368:
369: if (itr.hasNext()) {
370: count = (Long) itr.next();
371: }
372:
373: if (count == null) {
374: count = new Long(0);
375: }
376:
377: FinderCache.putResult(finderClassNameCacheEnabled,
378: finderClassName, finderMethodName,
379: finderParams, finderArgs, count);
380:
381: return count.intValue();
382: } catch (Exception e) {
383: throw HibernateUtil.processException(e);
384: } finally {
385: closeSession(session);
386: }
387: } else {
388: return ((Long) result).intValue();
389: }
390: }
391:
392: protected void initDao() {
393: }
394:
395: private static ModelListener _getListener() {
396: if (Validator.isNotNull(_LISTENER)) {
397: try {
398: return (ModelListener) Class.forName(_LISTENER)
399: .newInstance();
400: } catch (Exception e) {
401: _log.error(e);
402: }
403: }
404:
405: return null;
406: }
407:
408: private static final String _LISTENER = GetterUtil
409: .getString(PropsUtil
410: .get("value.object.listener.com.liferay.portal.model.Account"));
411: private static Log _log = LogFactory
412: .getLog(AccountPersistenceImpl.class);
413: }
|