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.portlet.wiki.service.persistence;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.dao.DynamicQuery;
025: import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.OrderByComparator;
028: import com.liferay.portal.kernel.util.StringMaker;
029: import com.liferay.portal.kernel.util.StringPool;
030: import com.liferay.portal.kernel.util.Validator;
031: import com.liferay.portal.model.ModelListener;
032: import com.liferay.portal.service.persistence.BasePersistence;
033: import com.liferay.portal.spring.hibernate.FinderCache;
034: import com.liferay.portal.spring.hibernate.HibernateUtil;
035: import com.liferay.portal.util.PropsUtil;
036:
037: import com.liferay.portlet.wiki.NoSuchPageResourceException;
038: import com.liferay.portlet.wiki.model.WikiPageResource;
039: import com.liferay.portlet.wiki.model.impl.WikiPageResourceImpl;
040: import com.liferay.portlet.wiki.model.impl.WikiPageResourceModelImpl;
041:
042: import com.liferay.util.dao.hibernate.QueryUtil;
043:
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046:
047: import org.hibernate.Query;
048: import org.hibernate.Session;
049:
050: import java.util.Collections;
051: import java.util.Iterator;
052: import java.util.List;
053:
054: /**
055: * <a href="WikiPageResourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
056: *
057: * @author Brian Wing Shun Chan
058: *
059: */
060: public class WikiPageResourcePersistenceImpl extends BasePersistence
061: implements WikiPageResourcePersistence {
062: public WikiPageResource create(long resourcePrimKey) {
063: WikiPageResource wikiPageResource = new WikiPageResourceImpl();
064:
065: wikiPageResource.setNew(true);
066: wikiPageResource.setPrimaryKey(resourcePrimKey);
067:
068: return wikiPageResource;
069: }
070:
071: public WikiPageResource remove(long resourcePrimKey)
072: throws NoSuchPageResourceException, SystemException {
073: Session session = null;
074:
075: try {
076: session = openSession();
077:
078: WikiPageResource wikiPageResource = (WikiPageResource) session
079: .get(WikiPageResourceImpl.class, new Long(
080: resourcePrimKey));
081:
082: if (wikiPageResource == null) {
083: if (_log.isWarnEnabled()) {
084: _log
085: .warn("No WikiPageResource exists with the primary key "
086: + resourcePrimKey);
087: }
088:
089: throw new NoSuchPageResourceException(
090: "No WikiPageResource exists with the primary key "
091: + resourcePrimKey);
092: }
093:
094: return remove(wikiPageResource);
095: } catch (NoSuchPageResourceException nsee) {
096: throw nsee;
097: } catch (Exception e) {
098: throw HibernateUtil.processException(e);
099: } finally {
100: closeSession(session);
101: }
102: }
103:
104: public WikiPageResource remove(WikiPageResource wikiPageResource)
105: throws SystemException {
106: ModelListener listener = _getListener();
107:
108: if (listener != null) {
109: listener.onBeforeRemove(wikiPageResource);
110: }
111:
112: wikiPageResource = removeImpl(wikiPageResource);
113:
114: if (listener != null) {
115: listener.onAfterRemove(wikiPageResource);
116: }
117:
118: return wikiPageResource;
119: }
120:
121: protected WikiPageResource removeImpl(
122: WikiPageResource wikiPageResource) throws SystemException {
123: Session session = null;
124:
125: try {
126: session = openSession();
127:
128: session.delete(wikiPageResource);
129:
130: session.flush();
131:
132: return wikiPageResource;
133: } catch (Exception e) {
134: throw HibernateUtil.processException(e);
135: } finally {
136: closeSession(session);
137:
138: FinderCache.clearCache(WikiPageResource.class.getName());
139: }
140: }
141:
142: public WikiPageResource update(WikiPageResource wikiPageResource)
143: throws SystemException {
144: return update(wikiPageResource, false);
145: }
146:
147: public WikiPageResource update(WikiPageResource wikiPageResource,
148: boolean merge) throws SystemException {
149: ModelListener listener = _getListener();
150:
151: boolean isNew = wikiPageResource.isNew();
152:
153: if (listener != null) {
154: if (isNew) {
155: listener.onBeforeCreate(wikiPageResource);
156: } else {
157: listener.onBeforeUpdate(wikiPageResource);
158: }
159: }
160:
161: wikiPageResource = updateImpl(wikiPageResource, merge);
162:
163: if (listener != null) {
164: if (isNew) {
165: listener.onAfterCreate(wikiPageResource);
166: } else {
167: listener.onAfterUpdate(wikiPageResource);
168: }
169: }
170:
171: return wikiPageResource;
172: }
173:
174: public WikiPageResource updateImpl(
175: com.liferay.portlet.wiki.model.WikiPageResource wikiPageResource,
176: boolean merge) throws SystemException {
177: Session session = null;
178:
179: try {
180: session = openSession();
181:
182: if (merge) {
183: session.merge(wikiPageResource);
184: } else {
185: if (wikiPageResource.isNew()) {
186: session.save(wikiPageResource);
187: }
188: }
189:
190: session.flush();
191:
192: wikiPageResource.setNew(false);
193:
194: return wikiPageResource;
195: } catch (Exception e) {
196: throw HibernateUtil.processException(e);
197: } finally {
198: closeSession(session);
199:
200: FinderCache.clearCache(WikiPageResource.class.getName());
201: }
202: }
203:
204: public WikiPageResource findByPrimaryKey(long resourcePrimKey)
205: throws NoSuchPageResourceException, SystemException {
206: WikiPageResource wikiPageResource = fetchByPrimaryKey(resourcePrimKey);
207:
208: if (wikiPageResource == null) {
209: if (_log.isWarnEnabled()) {
210: _log
211: .warn("No WikiPageResource exists with the primary key "
212: + resourcePrimKey);
213: }
214:
215: throw new NoSuchPageResourceException(
216: "No WikiPageResource exists with the primary key "
217: + resourcePrimKey);
218: }
219:
220: return wikiPageResource;
221: }
222:
223: public WikiPageResource fetchByPrimaryKey(long resourcePrimKey)
224: throws SystemException {
225: Session session = null;
226:
227: try {
228: session = openSession();
229:
230: return (WikiPageResource) session.get(
231: WikiPageResourceImpl.class, new Long(
232: resourcePrimKey));
233: } catch (Exception e) {
234: throw HibernateUtil.processException(e);
235: } finally {
236: closeSession(session);
237: }
238: }
239:
240: public WikiPageResource findByN_T(long nodeId, String title)
241: throws NoSuchPageResourceException, SystemException {
242: WikiPageResource wikiPageResource = fetchByN_T(nodeId, title);
243:
244: if (wikiPageResource == null) {
245: StringMaker msg = new StringMaker();
246:
247: msg.append("No WikiPageResource exists with the key {");
248:
249: msg.append("nodeId=" + nodeId);
250:
251: msg.append(", ");
252: msg.append("title=" + title);
253:
254: msg.append(StringPool.CLOSE_CURLY_BRACE);
255:
256: if (_log.isWarnEnabled()) {
257: _log.warn(msg.toString());
258: }
259:
260: throw new NoSuchPageResourceException(msg.toString());
261: }
262:
263: return wikiPageResource;
264: }
265:
266: public WikiPageResource fetchByN_T(long nodeId, String title)
267: throws SystemException {
268: boolean finderClassNameCacheEnabled = WikiPageResourceModelImpl.CACHE_ENABLED;
269: String finderClassName = WikiPageResource.class.getName();
270: String finderMethodName = "fetchByN_T";
271: String[] finderParams = new String[] { Long.class.getName(),
272: String.class.getName() };
273: Object[] finderArgs = new Object[] { new Long(nodeId), title };
274:
275: Object result = null;
276:
277: if (finderClassNameCacheEnabled) {
278: result = FinderCache.getResult(finderClassName,
279: finderMethodName, finderParams, finderArgs,
280: getSessionFactory());
281: }
282:
283: if (result == null) {
284: Session session = null;
285:
286: try {
287: session = openSession();
288:
289: StringMaker query = new StringMaker();
290:
291: query
292: .append("FROM com.liferay.portlet.wiki.model.WikiPageResource WHERE ");
293:
294: query.append("nodeId = ?");
295:
296: query.append(" AND ");
297:
298: if (title == null) {
299: query.append("title IS NULL");
300: } else {
301: query.append("title = ?");
302: }
303:
304: query.append(" ");
305:
306: Query q = session.createQuery(query.toString());
307:
308: int queryPos = 0;
309:
310: q.setLong(queryPos++, nodeId);
311:
312: if (title != null) {
313: q.setString(queryPos++, title);
314: }
315:
316: List list = q.list();
317:
318: FinderCache.putResult(finderClassNameCacheEnabled,
319: finderClassName, finderMethodName,
320: finderParams, finderArgs, list);
321:
322: if (list.size() == 0) {
323: return null;
324: } else {
325: return (WikiPageResource) list.get(0);
326: }
327: } catch (Exception e) {
328: throw HibernateUtil.processException(e);
329: } finally {
330: closeSession(session);
331: }
332: } else {
333: List list = (List) result;
334:
335: if (list.size() == 0) {
336: return null;
337: } else {
338: return (WikiPageResource) list.get(0);
339: }
340: }
341: }
342:
343: public List findWithDynamicQuery(
344: DynamicQueryInitializer queryInitializer)
345: throws SystemException {
346: Session session = null;
347:
348: try {
349: session = openSession();
350:
351: DynamicQuery query = queryInitializer.initialize(session);
352:
353: return query.list();
354: } catch (Exception e) {
355: throw HibernateUtil.processException(e);
356: } finally {
357: closeSession(session);
358: }
359: }
360:
361: public List findWithDynamicQuery(
362: DynamicQueryInitializer queryInitializer, int begin, int end)
363: throws SystemException {
364: Session session = null;
365:
366: try {
367: session = openSession();
368:
369: DynamicQuery query = queryInitializer.initialize(session);
370:
371: query.setLimit(begin, end);
372:
373: return query.list();
374: } catch (Exception e) {
375: throw HibernateUtil.processException(e);
376: } finally {
377: closeSession(session);
378: }
379: }
380:
381: public List findAll() throws SystemException {
382: return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
383: }
384:
385: public List findAll(int begin, int end) throws SystemException {
386: return findAll(begin, end, null);
387: }
388:
389: public List findAll(int begin, int end, OrderByComparator obc)
390: throws SystemException {
391: boolean finderClassNameCacheEnabled = WikiPageResourceModelImpl.CACHE_ENABLED;
392: String finderClassName = WikiPageResource.class.getName();
393: String finderMethodName = "findAll";
394: String[] finderParams = new String[] { "java.lang.Integer",
395: "java.lang.Integer",
396: "com.liferay.portal.kernel.util.OrderByComparator" };
397: Object[] finderArgs = new Object[] { String.valueOf(begin),
398: String.valueOf(end), String.valueOf(obc) };
399:
400: Object result = null;
401:
402: if (finderClassNameCacheEnabled) {
403: result = FinderCache.getResult(finderClassName,
404: finderMethodName, finderParams, finderArgs,
405: getSessionFactory());
406: }
407:
408: if (result == null) {
409: Session session = null;
410:
411: try {
412: session = openSession();
413:
414: StringMaker query = new StringMaker();
415:
416: query
417: .append("FROM com.liferay.portlet.wiki.model.WikiPageResource ");
418:
419: if (obc != null) {
420: query.append("ORDER BY ");
421: query.append(obc.getOrderBy());
422: }
423:
424: Query q = session.createQuery(query.toString());
425:
426: List list = QueryUtil.list(q, getDialect(), begin, end);
427:
428: if (obc == null) {
429: Collections.sort(list);
430: }
431:
432: FinderCache.putResult(finderClassNameCacheEnabled,
433: finderClassName, finderMethodName,
434: finderParams, finderArgs, list);
435:
436: return list;
437: } catch (Exception e) {
438: throw HibernateUtil.processException(e);
439: } finally {
440: closeSession(session);
441: }
442: } else {
443: return (List) result;
444: }
445: }
446:
447: public void removeByN_T(long nodeId, String title)
448: throws NoSuchPageResourceException, SystemException {
449: WikiPageResource wikiPageResource = findByN_T(nodeId, title);
450:
451: remove(wikiPageResource);
452: }
453:
454: public void removeAll() throws SystemException {
455: Iterator itr = findAll().iterator();
456:
457: while (itr.hasNext()) {
458: remove((WikiPageResource) itr.next());
459: }
460: }
461:
462: public int countByN_T(long nodeId, String title)
463: throws SystemException {
464: boolean finderClassNameCacheEnabled = WikiPageResourceModelImpl.CACHE_ENABLED;
465: String finderClassName = WikiPageResource.class.getName();
466: String finderMethodName = "countByN_T";
467: String[] finderParams = new String[] { Long.class.getName(),
468: String.class.getName() };
469: Object[] finderArgs = new Object[] { new Long(nodeId), title };
470:
471: Object result = null;
472:
473: if (finderClassNameCacheEnabled) {
474: result = FinderCache.getResult(finderClassName,
475: finderMethodName, finderParams, finderArgs,
476: getSessionFactory());
477: }
478:
479: if (result == null) {
480: Session session = null;
481:
482: try {
483: session = openSession();
484:
485: StringMaker query = new StringMaker();
486:
487: query.append("SELECT COUNT(*) ");
488: query
489: .append("FROM com.liferay.portlet.wiki.model.WikiPageResource WHERE ");
490:
491: query.append("nodeId = ?");
492:
493: query.append(" AND ");
494:
495: if (title == null) {
496: query.append("title IS NULL");
497: } else {
498: query.append("title = ?");
499: }
500:
501: query.append(" ");
502:
503: Query q = session.createQuery(query.toString());
504:
505: int queryPos = 0;
506:
507: q.setLong(queryPos++, nodeId);
508:
509: if (title != null) {
510: q.setString(queryPos++, title);
511: }
512:
513: Long count = null;
514:
515: Iterator itr = q.list().iterator();
516:
517: if (itr.hasNext()) {
518: count = (Long) itr.next();
519: }
520:
521: if (count == null) {
522: count = new Long(0);
523: }
524:
525: FinderCache.putResult(finderClassNameCacheEnabled,
526: finderClassName, finderMethodName,
527: finderParams, finderArgs, count);
528:
529: return count.intValue();
530: } catch (Exception e) {
531: throw HibernateUtil.processException(e);
532: } finally {
533: closeSession(session);
534: }
535: } else {
536: return ((Long) result).intValue();
537: }
538: }
539:
540: public int countAll() throws SystemException {
541: boolean finderClassNameCacheEnabled = WikiPageResourceModelImpl.CACHE_ENABLED;
542: String finderClassName = WikiPageResource.class.getName();
543: String finderMethodName = "countAll";
544: String[] finderParams = new String[] {};
545: Object[] finderArgs = new Object[] {};
546:
547: Object result = null;
548:
549: if (finderClassNameCacheEnabled) {
550: result = FinderCache.getResult(finderClassName,
551: finderMethodName, finderParams, finderArgs,
552: getSessionFactory());
553: }
554:
555: if (result == null) {
556: Session session = null;
557:
558: try {
559: session = openSession();
560:
561: Query q = session
562: .createQuery("SELECT COUNT(*) FROM com.liferay.portlet.wiki.model.WikiPageResource");
563:
564: Long count = null;
565:
566: Iterator itr = q.list().iterator();
567:
568: if (itr.hasNext()) {
569: count = (Long) itr.next();
570: }
571:
572: if (count == null) {
573: count = new Long(0);
574: }
575:
576: FinderCache.putResult(finderClassNameCacheEnabled,
577: finderClassName, finderMethodName,
578: finderParams, finderArgs, count);
579:
580: return count.intValue();
581: } catch (Exception e) {
582: throw HibernateUtil.processException(e);
583: } finally {
584: closeSession(session);
585: }
586: } else {
587: return ((Long) result).intValue();
588: }
589: }
590:
591: protected void initDao() {
592: }
593:
594: private static ModelListener _getListener() {
595: if (Validator.isNotNull(_LISTENER)) {
596: try {
597: return (ModelListener) Class.forName(_LISTENER)
598: .newInstance();
599: } catch (Exception e) {
600: _log.error(e);
601: }
602: }
603:
604: return null;
605: }
606:
607: private static final String _LISTENER = GetterUtil
608: .getString(PropsUtil
609: .get("value.object.listener.com.liferay.portlet.wiki.model.WikiPageResource"));
610: private static Log _log = LogFactory
611: .getLog(WikiPageResourcePersistenceImpl.class);
612: }
|