1
19
20 package com.liferay.portlet.wiki.service.persistence;
21
22 import com.liferay.portal.SystemException;
23 import com.liferay.portal.kernel.annotation.BeanReference;
24 import com.liferay.portal.kernel.cache.CacheRegistry;
25 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
26 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
27 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
28 import com.liferay.portal.kernel.dao.orm.FinderPath;
29 import com.liferay.portal.kernel.dao.orm.Query;
30 import com.liferay.portal.kernel.dao.orm.QueryPos;
31 import com.liferay.portal.kernel.dao.orm.QueryUtil;
32 import com.liferay.portal.kernel.dao.orm.Session;
33 import com.liferay.portal.kernel.log.Log;
34 import com.liferay.portal.kernel.log.LogFactoryUtil;
35 import com.liferay.portal.kernel.util.GetterUtil;
36 import com.liferay.portal.kernel.util.OrderByComparator;
37 import com.liferay.portal.kernel.util.StringPool;
38 import com.liferay.portal.kernel.util.StringUtil;
39 import com.liferay.portal.model.ModelListener;
40 import com.liferay.portal.service.persistence.BatchSessionUtil;
41 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
42
43 import com.liferay.portlet.wiki.NoSuchPageResourceException;
44 import com.liferay.portlet.wiki.model.WikiPageResource;
45 import com.liferay.portlet.wiki.model.impl.WikiPageResourceImpl;
46 import com.liferay.portlet.wiki.model.impl.WikiPageResourceModelImpl;
47
48 import java.util.ArrayList;
49 import java.util.Collections;
50 import java.util.List;
51
52
58 public class WikiPageResourcePersistenceImpl extends BasePersistenceImpl
59 implements WikiPageResourcePersistence {
60 public static final String FINDER_CLASS_NAME_ENTITY = WikiPageResourceImpl.class.getName();
61 public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
62 ".List";
63 public static final FinderPath FINDER_PATH_FETCH_BY_N_T = new FinderPath(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
64 WikiPageResourceModelImpl.FINDER_CACHE_ENABLED,
65 FINDER_CLASS_NAME_ENTITY, "fetchByN_T",
66 new String[] { Long.class.getName(), String.class.getName() });
67 public static final FinderPath FINDER_PATH_COUNT_BY_N_T = new FinderPath(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
68 WikiPageResourceModelImpl.FINDER_CACHE_ENABLED,
69 FINDER_CLASS_NAME_LIST, "countByN_T",
70 new String[] { Long.class.getName(), String.class.getName() });
71 public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
72 WikiPageResourceModelImpl.FINDER_CACHE_ENABLED,
73 FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
74 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
75 WikiPageResourceModelImpl.FINDER_CACHE_ENABLED,
76 FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
77
78 public void cacheResult(WikiPageResource wikiPageResource) {
79 EntityCacheUtil.putResult(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
80 WikiPageResourceImpl.class, wikiPageResource.getPrimaryKey(),
81 wikiPageResource);
82
83 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T,
84 new Object[] {
85 new Long(wikiPageResource.getNodeId()),
86
87 wikiPageResource.getTitle()
88 }, wikiPageResource);
89 }
90
91 public void cacheResult(List<WikiPageResource> wikiPageResources) {
92 for (WikiPageResource wikiPageResource : wikiPageResources) {
93 if (EntityCacheUtil.getResult(
94 WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
95 WikiPageResourceImpl.class,
96 wikiPageResource.getPrimaryKey(), this) == null) {
97 cacheResult(wikiPageResource);
98 }
99 }
100 }
101
102 public void clearCache() {
103 CacheRegistry.clear(WikiPageResourceImpl.class.getName());
104 EntityCacheUtil.clearCache(WikiPageResourceImpl.class.getName());
105 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
106 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
107 }
108
109 public WikiPageResource create(long resourcePrimKey) {
110 WikiPageResource wikiPageResource = new WikiPageResourceImpl();
111
112 wikiPageResource.setNew(true);
113 wikiPageResource.setPrimaryKey(resourcePrimKey);
114
115 return wikiPageResource;
116 }
117
118 public WikiPageResource remove(long resourcePrimKey)
119 throws NoSuchPageResourceException, SystemException {
120 Session session = null;
121
122 try {
123 session = openSession();
124
125 WikiPageResource wikiPageResource = (WikiPageResource)session.get(WikiPageResourceImpl.class,
126 new Long(resourcePrimKey));
127
128 if (wikiPageResource == null) {
129 if (_log.isWarnEnabled()) {
130 _log.warn(
131 "No WikiPageResource exists with the primary key " +
132 resourcePrimKey);
133 }
134
135 throw new NoSuchPageResourceException(
136 "No WikiPageResource exists with the primary key " +
137 resourcePrimKey);
138 }
139
140 return remove(wikiPageResource);
141 }
142 catch (NoSuchPageResourceException nsee) {
143 throw nsee;
144 }
145 catch (Exception e) {
146 throw processException(e);
147 }
148 finally {
149 closeSession(session);
150 }
151 }
152
153 public WikiPageResource remove(WikiPageResource wikiPageResource)
154 throws SystemException {
155 for (ModelListener<WikiPageResource> listener : listeners) {
156 listener.onBeforeRemove(wikiPageResource);
157 }
158
159 wikiPageResource = removeImpl(wikiPageResource);
160
161 for (ModelListener<WikiPageResource> listener : listeners) {
162 listener.onAfterRemove(wikiPageResource);
163 }
164
165 return wikiPageResource;
166 }
167
168 protected WikiPageResource removeImpl(WikiPageResource wikiPageResource)
169 throws SystemException {
170 Session session = null;
171
172 try {
173 session = openSession();
174
175 if (wikiPageResource.isCachedModel() ||
176 BatchSessionUtil.isEnabled()) {
177 Object staleObject = session.get(WikiPageResourceImpl.class,
178 wikiPageResource.getPrimaryKeyObj());
179
180 if (staleObject != null) {
181 session.evict(staleObject);
182 }
183 }
184
185 session.delete(wikiPageResource);
186
187 session.flush();
188 }
189 catch (Exception e) {
190 throw processException(e);
191 }
192 finally {
193 closeSession(session);
194 }
195
196 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
197
198 WikiPageResourceModelImpl wikiPageResourceModelImpl = (WikiPageResourceModelImpl)wikiPageResource;
199
200 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_N_T,
201 new Object[] {
202 new Long(wikiPageResourceModelImpl.getOriginalNodeId()),
203
204 wikiPageResourceModelImpl.getOriginalTitle()
205 });
206
207 EntityCacheUtil.removeResult(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
208 WikiPageResourceImpl.class, wikiPageResource.getPrimaryKey());
209
210 return wikiPageResource;
211 }
212
213
216 public WikiPageResource update(WikiPageResource wikiPageResource)
217 throws SystemException {
218 if (_log.isWarnEnabled()) {
219 _log.warn(
220 "Using the deprecated update(WikiPageResource wikiPageResource) method. Use update(WikiPageResource wikiPageResource, boolean merge) instead.");
221 }
222
223 return update(wikiPageResource, false);
224 }
225
226
239 public WikiPageResource update(WikiPageResource wikiPageResource,
240 boolean merge) throws SystemException {
241 boolean isNew = wikiPageResource.isNew();
242
243 for (ModelListener<WikiPageResource> listener : listeners) {
244 if (isNew) {
245 listener.onBeforeCreate(wikiPageResource);
246 }
247 else {
248 listener.onBeforeUpdate(wikiPageResource);
249 }
250 }
251
252 wikiPageResource = updateImpl(wikiPageResource, merge);
253
254 for (ModelListener<WikiPageResource> listener : listeners) {
255 if (isNew) {
256 listener.onAfterCreate(wikiPageResource);
257 }
258 else {
259 listener.onAfterUpdate(wikiPageResource);
260 }
261 }
262
263 return wikiPageResource;
264 }
265
266 public WikiPageResource updateImpl(
267 com.liferay.portlet.wiki.model.WikiPageResource wikiPageResource,
268 boolean merge) throws SystemException {
269 boolean isNew = wikiPageResource.isNew();
270
271 WikiPageResourceModelImpl wikiPageResourceModelImpl = (WikiPageResourceModelImpl)wikiPageResource;
272
273 Session session = null;
274
275 try {
276 session = openSession();
277
278 BatchSessionUtil.update(session, wikiPageResource, merge);
279
280 wikiPageResource.setNew(false);
281 }
282 catch (Exception e) {
283 throw processException(e);
284 }
285 finally {
286 closeSession(session);
287 }
288
289 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
290
291 EntityCacheUtil.putResult(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
292 WikiPageResourceImpl.class, wikiPageResource.getPrimaryKey(),
293 wikiPageResource);
294
295 if (!isNew &&
296 ((wikiPageResource.getNodeId() != wikiPageResourceModelImpl.getOriginalNodeId()) ||
297 !wikiPageResource.getTitle()
298 .equals(wikiPageResourceModelImpl.getOriginalTitle()))) {
299 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_N_T,
300 new Object[] {
301 new Long(wikiPageResourceModelImpl.getOriginalNodeId()),
302
303 wikiPageResourceModelImpl.getOriginalTitle()
304 });
305 }
306
307 if (isNew ||
308 ((wikiPageResource.getNodeId() != wikiPageResourceModelImpl.getOriginalNodeId()) ||
309 !wikiPageResource.getTitle()
310 .equals(wikiPageResourceModelImpl.getOriginalTitle()))) {
311 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T,
312 new Object[] {
313 new Long(wikiPageResource.getNodeId()),
314
315 wikiPageResource.getTitle()
316 }, wikiPageResource);
317 }
318
319 return wikiPageResource;
320 }
321
322 public WikiPageResource findByPrimaryKey(long resourcePrimKey)
323 throws NoSuchPageResourceException, SystemException {
324 WikiPageResource wikiPageResource = fetchByPrimaryKey(resourcePrimKey);
325
326 if (wikiPageResource == null) {
327 if (_log.isWarnEnabled()) {
328 _log.warn("No WikiPageResource exists with the primary key " +
329 resourcePrimKey);
330 }
331
332 throw new NoSuchPageResourceException(
333 "No WikiPageResource exists with the primary key " +
334 resourcePrimKey);
335 }
336
337 return wikiPageResource;
338 }
339
340 public WikiPageResource fetchByPrimaryKey(long resourcePrimKey)
341 throws SystemException {
342 WikiPageResource wikiPageResource = (WikiPageResource)EntityCacheUtil.getResult(WikiPageResourceModelImpl.ENTITY_CACHE_ENABLED,
343 WikiPageResourceImpl.class, resourcePrimKey, this);
344
345 if (wikiPageResource == null) {
346 Session session = null;
347
348 try {
349 session = openSession();
350
351 wikiPageResource = (WikiPageResource)session.get(WikiPageResourceImpl.class,
352 new Long(resourcePrimKey));
353 }
354 catch (Exception e) {
355 throw processException(e);
356 }
357 finally {
358 if (wikiPageResource != null) {
359 cacheResult(wikiPageResource);
360 }
361
362 closeSession(session);
363 }
364 }
365
366 return wikiPageResource;
367 }
368
369 public WikiPageResource findByN_T(long nodeId, String title)
370 throws NoSuchPageResourceException, SystemException {
371 WikiPageResource wikiPageResource = fetchByN_T(nodeId, title);
372
373 if (wikiPageResource == null) {
374 StringBuilder msg = new StringBuilder();
375
376 msg.append("No WikiPageResource exists with the key {");
377
378 msg.append("nodeId=" + nodeId);
379
380 msg.append(", ");
381 msg.append("title=" + title);
382
383 msg.append(StringPool.CLOSE_CURLY_BRACE);
384
385 if (_log.isWarnEnabled()) {
386 _log.warn(msg.toString());
387 }
388
389 throw new NoSuchPageResourceException(msg.toString());
390 }
391
392 return wikiPageResource;
393 }
394
395 public WikiPageResource fetchByN_T(long nodeId, String title)
396 throws SystemException {
397 return fetchByN_T(nodeId, title, true);
398 }
399
400 public WikiPageResource fetchByN_T(long nodeId, String title,
401 boolean retrieveFromCache) throws SystemException {
402 Object[] finderArgs = new Object[] { new Long(nodeId), title };
403
404 Object result = null;
405
406 if (retrieveFromCache) {
407 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_N_T,
408 finderArgs, this);
409 }
410
411 if (result == null) {
412 Session session = null;
413
414 try {
415 session = openSession();
416
417 StringBuilder query = new StringBuilder();
418
419 query.append(
420 "FROM com.liferay.portlet.wiki.model.WikiPageResource WHERE ");
421
422 query.append("nodeId = ?");
423
424 query.append(" AND ");
425
426 if (title == null) {
427 query.append("title IS NULL");
428 }
429 else {
430 query.append("title = ?");
431 }
432
433 query.append(" ");
434
435 Query q = session.createQuery(query.toString());
436
437 QueryPos qPos = QueryPos.getInstance(q);
438
439 qPos.add(nodeId);
440
441 if (title != null) {
442 qPos.add(title);
443 }
444
445 List<WikiPageResource> list = q.list();
446
447 result = list;
448
449 WikiPageResource wikiPageResource = null;
450
451 if (list.isEmpty()) {
452 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T,
453 finderArgs, list);
454 }
455 else {
456 wikiPageResource = list.get(0);
457
458 cacheResult(wikiPageResource);
459
460 if ((wikiPageResource.getNodeId() != nodeId) ||
461 (wikiPageResource.getTitle() == null) ||
462 !wikiPageResource.getTitle().equals(title)) {
463 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T,
464 finderArgs, list);
465 }
466 }
467
468 return wikiPageResource;
469 }
470 catch (Exception e) {
471 throw processException(e);
472 }
473 finally {
474 if (result == null) {
475 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T,
476 finderArgs, new ArrayList<WikiPageResource>());
477 }
478
479 closeSession(session);
480 }
481 }
482 else {
483 if (result instanceof List) {
484 return null;
485 }
486 else {
487 return (WikiPageResource)result;
488 }
489 }
490 }
491
492 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
493 throws SystemException {
494 Session session = null;
495
496 try {
497 session = openSession();
498
499 dynamicQuery.compile(session);
500
501 return dynamicQuery.list();
502 }
503 catch (Exception e) {
504 throw processException(e);
505 }
506 finally {
507 closeSession(session);
508 }
509 }
510
511 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
512 int start, int end) throws SystemException {
513 Session session = null;
514
515 try {
516 session = openSession();
517
518 dynamicQuery.setLimit(start, end);
519
520 dynamicQuery.compile(session);
521
522 return dynamicQuery.list();
523 }
524 catch (Exception e) {
525 throw processException(e);
526 }
527 finally {
528 closeSession(session);
529 }
530 }
531
532 public List<WikiPageResource> findAll() throws SystemException {
533 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
534 }
535
536 public List<WikiPageResource> findAll(int start, int end)
537 throws SystemException {
538 return findAll(start, end, null);
539 }
540
541 public List<WikiPageResource> findAll(int start, int end,
542 OrderByComparator obc) throws SystemException {
543 Object[] finderArgs = new Object[] {
544 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
545 };
546
547 List<WikiPageResource> list = (List<WikiPageResource>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
548 finderArgs, this);
549
550 if (list == null) {
551 Session session = null;
552
553 try {
554 session = openSession();
555
556 StringBuilder query = new StringBuilder();
557
558 query.append(
559 "FROM com.liferay.portlet.wiki.model.WikiPageResource ");
560
561 if (obc != null) {
562 query.append("ORDER BY ");
563 query.append(obc.getOrderBy());
564 }
565
566 Query q = session.createQuery(query.toString());
567
568 if (obc == null) {
569 list = (List<WikiPageResource>)QueryUtil.list(q,
570 getDialect(), start, end, false);
571
572 Collections.sort(list);
573 }
574 else {
575 list = (List<WikiPageResource>)QueryUtil.list(q,
576 getDialect(), start, end);
577 }
578 }
579 catch (Exception e) {
580 throw processException(e);
581 }
582 finally {
583 if (list == null) {
584 list = new ArrayList<WikiPageResource>();
585 }
586
587 cacheResult(list);
588
589 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
590
591 closeSession(session);
592 }
593 }
594
595 return list;
596 }
597
598 public void removeByN_T(long nodeId, String title)
599 throws NoSuchPageResourceException, SystemException {
600 WikiPageResource wikiPageResource = findByN_T(nodeId, title);
601
602 remove(wikiPageResource);
603 }
604
605 public void removeAll() throws SystemException {
606 for (WikiPageResource wikiPageResource : findAll()) {
607 remove(wikiPageResource);
608 }
609 }
610
611 public int countByN_T(long nodeId, String title) throws SystemException {
612 Object[] finderArgs = new Object[] { new Long(nodeId), title };
613
614 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_N_T,
615 finderArgs, this);
616
617 if (count == null) {
618 Session session = null;
619
620 try {
621 session = openSession();
622
623 StringBuilder query = new StringBuilder();
624
625 query.append("SELECT COUNT(*) ");
626 query.append(
627 "FROM com.liferay.portlet.wiki.model.WikiPageResource WHERE ");
628
629 query.append("nodeId = ?");
630
631 query.append(" AND ");
632
633 if (title == null) {
634 query.append("title IS NULL");
635 }
636 else {
637 query.append("title = ?");
638 }
639
640 query.append(" ");
641
642 Query q = session.createQuery(query.toString());
643
644 QueryPos qPos = QueryPos.getInstance(q);
645
646 qPos.add(nodeId);
647
648 if (title != null) {
649 qPos.add(title);
650 }
651
652 count = (Long)q.uniqueResult();
653 }
654 catch (Exception e) {
655 throw processException(e);
656 }
657 finally {
658 if (count == null) {
659 count = Long.valueOf(0);
660 }
661
662 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_N_T, finderArgs,
663 count);
664
665 closeSession(session);
666 }
667 }
668
669 return count.intValue();
670 }
671
672 public int countAll() throws SystemException {
673 Object[] finderArgs = new Object[0];
674
675 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
676 finderArgs, this);
677
678 if (count == null) {
679 Session session = null;
680
681 try {
682 session = openSession();
683
684 Query q = session.createQuery(
685 "SELECT COUNT(*) FROM com.liferay.portlet.wiki.model.WikiPageResource");
686
687 count = (Long)q.uniqueResult();
688 }
689 catch (Exception e) {
690 throw processException(e);
691 }
692 finally {
693 if (count == null) {
694 count = Long.valueOf(0);
695 }
696
697 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
698 count);
699
700 closeSession(session);
701 }
702 }
703
704 return count.intValue();
705 }
706
707 public void afterPropertiesSet() {
708 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
709 com.liferay.portal.util.PropsUtil.get(
710 "value.object.listener.com.liferay.portlet.wiki.model.WikiPageResource")));
711
712 if (listenerClassNames.length > 0) {
713 try {
714 List<ModelListener<WikiPageResource>> listenersList = new ArrayList<ModelListener<WikiPageResource>>();
715
716 for (String listenerClassName : listenerClassNames) {
717 listenersList.add((ModelListener<WikiPageResource>)Class.forName(
718 listenerClassName).newInstance());
719 }
720
721 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
722 }
723 catch (Exception e) {
724 _log.error(e);
725 }
726 }
727 }
728
729 @BeanReference(name = "com.liferay.portlet.wiki.service.persistence.WikiNodePersistence.impl")
730 protected com.liferay.portlet.wiki.service.persistence.WikiNodePersistence wikiNodePersistence;
731 @BeanReference(name = "com.liferay.portlet.wiki.service.persistence.WikiPagePersistence.impl")
732 protected com.liferay.portlet.wiki.service.persistence.WikiPagePersistence wikiPagePersistence;
733 @BeanReference(name = "com.liferay.portlet.wiki.service.persistence.WikiPageResourcePersistence.impl")
734 protected com.liferay.portlet.wiki.service.persistence.WikiPageResourcePersistence wikiPageResourcePersistence;
735 private static Log _log = LogFactoryUtil.getLog(WikiPageResourcePersistenceImpl.class);
736 }