1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.expando.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
27  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
28  import com.liferay.portal.kernel.dao.orm.Query;
29  import com.liferay.portal.kernel.dao.orm.QueryPos;
30  import com.liferay.portal.kernel.dao.orm.QueryUtil;
31  import com.liferay.portal.kernel.dao.orm.Session;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.ListUtil;
34  import com.liferay.portal.kernel.util.OrderByComparator;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.model.ModelListener;
38  import com.liferay.portal.service.persistence.BatchSessionUtil;
39  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
40  
41  import com.liferay.portlet.expando.NoSuchTableException;
42  import com.liferay.portlet.expando.model.ExpandoTable;
43  import com.liferay.portlet.expando.model.impl.ExpandoTableImpl;
44  import com.liferay.portlet.expando.model.impl.ExpandoTableModelImpl;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.Iterator;
52  import java.util.List;
53  
54  /**
55   * <a href="ExpandoTablePersistenceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class ExpandoTablePersistenceImpl extends BasePersistenceImpl
61      implements ExpandoTablePersistence {
62      public ExpandoTable create(long tableId) {
63          ExpandoTable expandoTable = new ExpandoTableImpl();
64  
65          expandoTable.setNew(true);
66          expandoTable.setPrimaryKey(tableId);
67  
68          return expandoTable;
69      }
70  
71      public ExpandoTable remove(long tableId)
72          throws NoSuchTableException, SystemException {
73          Session session = null;
74  
75          try {
76              session = openSession();
77  
78              ExpandoTable expandoTable = (ExpandoTable)session.get(ExpandoTableImpl.class,
79                      new Long(tableId));
80  
81              if (expandoTable == null) {
82                  if (_log.isWarnEnabled()) {
83                      _log.warn("No ExpandoTable exists with the primary key " +
84                          tableId);
85                  }
86  
87                  throw new NoSuchTableException(
88                      "No ExpandoTable exists with the primary key " + tableId);
89              }
90  
91              return remove(expandoTable);
92          }
93          catch (NoSuchTableException nsee) {
94              throw nsee;
95          }
96          catch (Exception e) {
97              throw processException(e);
98          }
99          finally {
100             closeSession(session);
101         }
102     }
103 
104     public ExpandoTable remove(ExpandoTable expandoTable)
105         throws SystemException {
106         if (_listeners.length > 0) {
107             for (ModelListener listener : _listeners) {
108                 listener.onBeforeRemove(expandoTable);
109             }
110         }
111 
112         expandoTable = removeImpl(expandoTable);
113 
114         if (_listeners.length > 0) {
115             for (ModelListener listener : _listeners) {
116                 listener.onAfterRemove(expandoTable);
117             }
118         }
119 
120         return expandoTable;
121     }
122 
123     protected ExpandoTable removeImpl(ExpandoTable expandoTable)
124         throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             if (BatchSessionUtil.isEnabled()) {
131                 Object staleObject = session.get(ExpandoTableImpl.class,
132                         expandoTable.getPrimaryKeyObj());
133 
134                 if (staleObject != null) {
135                     session.evict(staleObject);
136                 }
137             }
138 
139             session.delete(expandoTable);
140 
141             session.flush();
142 
143             return expandoTable;
144         }
145         catch (Exception e) {
146             throw processException(e);
147         }
148         finally {
149             closeSession(session);
150 
151             FinderCacheUtil.clearCache(ExpandoTable.class.getName());
152         }
153     }
154 
155     /**
156      * @deprecated Use <code>update(ExpandoTable expandoTable, boolean merge)</code>.
157      */
158     public ExpandoTable update(ExpandoTable expandoTable)
159         throws SystemException {
160         if (_log.isWarnEnabled()) {
161             _log.warn(
162                 "Using the deprecated update(ExpandoTable expandoTable) method. Use update(ExpandoTable expandoTable, boolean merge) instead.");
163         }
164 
165         return update(expandoTable, false);
166     }
167 
168     /**
169      * Add, update, or merge, the entity. This method also calls the model
170      * listeners to trigger the proper events associated with adding, deleting,
171      * or updating an entity.
172      *
173      * @param        expandoTable the entity to add, update, or merge
174      * @param        merge boolean value for whether to merge the entity. The
175      *                default value is false. Setting merge to true is more
176      *                expensive and should only be true when expandoTable is
177      *                transient. See LEP-5473 for a detailed discussion of this
178      *                method.
179      * @return        true if the portlet can be displayed via Ajax
180      */
181     public ExpandoTable update(ExpandoTable expandoTable, boolean merge)
182         throws SystemException {
183         boolean isNew = expandoTable.isNew();
184 
185         if (_listeners.length > 0) {
186             for (ModelListener listener : _listeners) {
187                 if (isNew) {
188                     listener.onBeforeCreate(expandoTable);
189                 }
190                 else {
191                     listener.onBeforeUpdate(expandoTable);
192                 }
193             }
194         }
195 
196         expandoTable = updateImpl(expandoTable, merge);
197 
198         if (_listeners.length > 0) {
199             for (ModelListener listener : _listeners) {
200                 if (isNew) {
201                     listener.onAfterCreate(expandoTable);
202                 }
203                 else {
204                     listener.onAfterUpdate(expandoTable);
205                 }
206             }
207         }
208 
209         return expandoTable;
210     }
211 
212     public ExpandoTable updateImpl(
213         com.liferay.portlet.expando.model.ExpandoTable expandoTable,
214         boolean merge) throws SystemException {
215         Session session = null;
216 
217         try {
218             session = openSession();
219 
220             BatchSessionUtil.update(session, expandoTable, merge);
221 
222             expandoTable.setNew(false);
223 
224             return expandoTable;
225         }
226         catch (Exception e) {
227             throw processException(e);
228         }
229         finally {
230             closeSession(session);
231 
232             FinderCacheUtil.clearCache(ExpandoTable.class.getName());
233         }
234     }
235 
236     public ExpandoTable findByPrimaryKey(long tableId)
237         throws NoSuchTableException, SystemException {
238         ExpandoTable expandoTable = fetchByPrimaryKey(tableId);
239 
240         if (expandoTable == null) {
241             if (_log.isWarnEnabled()) {
242                 _log.warn("No ExpandoTable exists with the primary key " +
243                     tableId);
244             }
245 
246             throw new NoSuchTableException(
247                 "No ExpandoTable exists with the primary key " + tableId);
248         }
249 
250         return expandoTable;
251     }
252 
253     public ExpandoTable fetchByPrimaryKey(long tableId)
254         throws SystemException {
255         Session session = null;
256 
257         try {
258             session = openSession();
259 
260             return (ExpandoTable)session.get(ExpandoTableImpl.class,
261                 new Long(tableId));
262         }
263         catch (Exception e) {
264             throw processException(e);
265         }
266         finally {
267             closeSession(session);
268         }
269     }
270 
271     public List<ExpandoTable> findByClassNameId(long classNameId)
272         throws SystemException {
273         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
274         String finderClassName = ExpandoTable.class.getName();
275         String finderMethodName = "findByClassNameId";
276         String[] finderParams = new String[] { Long.class.getName() };
277         Object[] finderArgs = new Object[] { new Long(classNameId) };
278 
279         Object result = null;
280 
281         if (finderClassNameCacheEnabled) {
282             result = FinderCacheUtil.getResult(finderClassName,
283                     finderMethodName, finderParams, finderArgs, this);
284         }
285 
286         if (result == null) {
287             Session session = null;
288 
289             try {
290                 session = openSession();
291 
292                 StringBuilder query = new StringBuilder();
293 
294                 query.append(
295                     "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
296 
297                 query.append("classNameId = ?");
298 
299                 query.append(" ");
300 
301                 Query q = session.createQuery(query.toString());
302 
303                 QueryPos qPos = QueryPos.getInstance(q);
304 
305                 qPos.add(classNameId);
306 
307                 List<ExpandoTable> list = q.list();
308 
309                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
310                     finderClassName, finderMethodName, finderParams,
311                     finderArgs, list);
312 
313                 return list;
314             }
315             catch (Exception e) {
316                 throw processException(e);
317             }
318             finally {
319                 closeSession(session);
320             }
321         }
322         else {
323             return (List<ExpandoTable>)result;
324         }
325     }
326 
327     public List<ExpandoTable> findByClassNameId(long classNameId, int start,
328         int end) throws SystemException {
329         return findByClassNameId(classNameId, start, end, null);
330     }
331 
332     public List<ExpandoTable> findByClassNameId(long classNameId, int start,
333         int end, OrderByComparator obc) throws SystemException {
334         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
335         String finderClassName = ExpandoTable.class.getName();
336         String finderMethodName = "findByClassNameId";
337         String[] finderParams = new String[] {
338                 Long.class.getName(),
339                 
340                 "java.lang.Integer", "java.lang.Integer",
341                 "com.liferay.portal.kernel.util.OrderByComparator"
342             };
343         Object[] finderArgs = new Object[] {
344                 new Long(classNameId),
345                 
346                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
347             };
348 
349         Object result = null;
350 
351         if (finderClassNameCacheEnabled) {
352             result = FinderCacheUtil.getResult(finderClassName,
353                     finderMethodName, finderParams, finderArgs, this);
354         }
355 
356         if (result == null) {
357             Session session = null;
358 
359             try {
360                 session = openSession();
361 
362                 StringBuilder query = new StringBuilder();
363 
364                 query.append(
365                     "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
366 
367                 query.append("classNameId = ?");
368 
369                 query.append(" ");
370 
371                 if (obc != null) {
372                     query.append("ORDER BY ");
373                     query.append(obc.getOrderBy());
374                 }
375 
376                 Query q = session.createQuery(query.toString());
377 
378                 QueryPos qPos = QueryPos.getInstance(q);
379 
380                 qPos.add(classNameId);
381 
382                 List<ExpandoTable> list = (List<ExpandoTable>)QueryUtil.list(q,
383                         getDialect(), start, end);
384 
385                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
386                     finderClassName, finderMethodName, finderParams,
387                     finderArgs, list);
388 
389                 return list;
390             }
391             catch (Exception e) {
392                 throw processException(e);
393             }
394             finally {
395                 closeSession(session);
396             }
397         }
398         else {
399             return (List<ExpandoTable>)result;
400         }
401     }
402 
403     public ExpandoTable findByClassNameId_First(long classNameId,
404         OrderByComparator obc) throws NoSuchTableException, SystemException {
405         List<ExpandoTable> list = findByClassNameId(classNameId, 0, 1, obc);
406 
407         if (list.size() == 0) {
408             StringBuilder msg = new StringBuilder();
409 
410             msg.append("No ExpandoTable exists with the key {");
411 
412             msg.append("classNameId=" + classNameId);
413 
414             msg.append(StringPool.CLOSE_CURLY_BRACE);
415 
416             throw new NoSuchTableException(msg.toString());
417         }
418         else {
419             return list.get(0);
420         }
421     }
422 
423     public ExpandoTable findByClassNameId_Last(long classNameId,
424         OrderByComparator obc) throws NoSuchTableException, SystemException {
425         int count = countByClassNameId(classNameId);
426 
427         List<ExpandoTable> list = findByClassNameId(classNameId, count - 1,
428                 count, obc);
429 
430         if (list.size() == 0) {
431             StringBuilder msg = new StringBuilder();
432 
433             msg.append("No ExpandoTable exists with the key {");
434 
435             msg.append("classNameId=" + classNameId);
436 
437             msg.append(StringPool.CLOSE_CURLY_BRACE);
438 
439             throw new NoSuchTableException(msg.toString());
440         }
441         else {
442             return list.get(0);
443         }
444     }
445 
446     public ExpandoTable[] findByClassNameId_PrevAndNext(long tableId,
447         long classNameId, OrderByComparator obc)
448         throws NoSuchTableException, SystemException {
449         ExpandoTable expandoTable = findByPrimaryKey(tableId);
450 
451         int count = countByClassNameId(classNameId);
452 
453         Session session = null;
454 
455         try {
456             session = openSession();
457 
458             StringBuilder query = new StringBuilder();
459 
460             query.append(
461                 "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
462 
463             query.append("classNameId = ?");
464 
465             query.append(" ");
466 
467             if (obc != null) {
468                 query.append("ORDER BY ");
469                 query.append(obc.getOrderBy());
470             }
471 
472             Query q = session.createQuery(query.toString());
473 
474             QueryPos qPos = QueryPos.getInstance(q);
475 
476             qPos.add(classNameId);
477 
478             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
479                     expandoTable);
480 
481             ExpandoTable[] array = new ExpandoTableImpl[3];
482 
483             array[0] = (ExpandoTable)objArray[0];
484             array[1] = (ExpandoTable)objArray[1];
485             array[2] = (ExpandoTable)objArray[2];
486 
487             return array;
488         }
489         catch (Exception e) {
490             throw processException(e);
491         }
492         finally {
493             closeSession(session);
494         }
495     }
496 
497     public ExpandoTable findByC_N(long classNameId, String name)
498         throws NoSuchTableException, SystemException {
499         ExpandoTable expandoTable = fetchByC_N(classNameId, name);
500 
501         if (expandoTable == null) {
502             StringBuilder msg = new StringBuilder();
503 
504             msg.append("No ExpandoTable exists with the key {");
505 
506             msg.append("classNameId=" + classNameId);
507 
508             msg.append(", ");
509             msg.append("name=" + name);
510 
511             msg.append(StringPool.CLOSE_CURLY_BRACE);
512 
513             if (_log.isWarnEnabled()) {
514                 _log.warn(msg.toString());
515             }
516 
517             throw new NoSuchTableException(msg.toString());
518         }
519 
520         return expandoTable;
521     }
522 
523     public ExpandoTable fetchByC_N(long classNameId, String name)
524         throws SystemException {
525         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
526         String finderClassName = ExpandoTable.class.getName();
527         String finderMethodName = "fetchByC_N";
528         String[] finderParams = new String[] {
529                 Long.class.getName(), String.class.getName()
530             };
531         Object[] finderArgs = new Object[] { new Long(classNameId), name };
532 
533         Object result = null;
534 
535         if (finderClassNameCacheEnabled) {
536             result = FinderCacheUtil.getResult(finderClassName,
537                     finderMethodName, finderParams, finderArgs, this);
538         }
539 
540         if (result == null) {
541             Session session = null;
542 
543             try {
544                 session = openSession();
545 
546                 StringBuilder query = new StringBuilder();
547 
548                 query.append(
549                     "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
550 
551                 query.append("classNameId = ?");
552 
553                 query.append(" AND ");
554 
555                 if (name == null) {
556                     query.append("name IS NULL");
557                 }
558                 else {
559                     query.append("name = ?");
560                 }
561 
562                 query.append(" ");
563 
564                 Query q = session.createQuery(query.toString());
565 
566                 QueryPos qPos = QueryPos.getInstance(q);
567 
568                 qPos.add(classNameId);
569 
570                 if (name != null) {
571                     qPos.add(name);
572                 }
573 
574                 List<ExpandoTable> list = q.list();
575 
576                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
577                     finderClassName, finderMethodName, finderParams,
578                     finderArgs, list);
579 
580                 if (list.size() == 0) {
581                     return null;
582                 }
583                 else {
584                     return list.get(0);
585                 }
586             }
587             catch (Exception e) {
588                 throw processException(e);
589             }
590             finally {
591                 closeSession(session);
592             }
593         }
594         else {
595             List<ExpandoTable> list = (List<ExpandoTable>)result;
596 
597             if (list.size() == 0) {
598                 return null;
599             }
600             else {
601                 return list.get(0);
602             }
603         }
604     }
605 
606     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
607         throws SystemException {
608         Session session = null;
609 
610         try {
611             session = openSession();
612 
613             dynamicQuery.compile(session);
614 
615             return dynamicQuery.list();
616         }
617         catch (Exception e) {
618             throw processException(e);
619         }
620         finally {
621             closeSession(session);
622         }
623     }
624 
625     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
626         int start, int end) throws SystemException {
627         Session session = null;
628 
629         try {
630             session = openSession();
631 
632             dynamicQuery.setLimit(start, end);
633 
634             dynamicQuery.compile(session);
635 
636             return dynamicQuery.list();
637         }
638         catch (Exception e) {
639             throw processException(e);
640         }
641         finally {
642             closeSession(session);
643         }
644     }
645 
646     public List<ExpandoTable> findAll() throws SystemException {
647         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
648     }
649 
650     public List<ExpandoTable> findAll(int start, int end)
651         throws SystemException {
652         return findAll(start, end, null);
653     }
654 
655     public List<ExpandoTable> findAll(int start, int end, OrderByComparator obc)
656         throws SystemException {
657         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
658         String finderClassName = ExpandoTable.class.getName();
659         String finderMethodName = "findAll";
660         String[] finderParams = new String[] {
661                 "java.lang.Integer", "java.lang.Integer",
662                 "com.liferay.portal.kernel.util.OrderByComparator"
663             };
664         Object[] finderArgs = new Object[] {
665                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
666             };
667 
668         Object result = null;
669 
670         if (finderClassNameCacheEnabled) {
671             result = FinderCacheUtil.getResult(finderClassName,
672                     finderMethodName, finderParams, finderArgs, this);
673         }
674 
675         if (result == null) {
676             Session session = null;
677 
678             try {
679                 session = openSession();
680 
681                 StringBuilder query = new StringBuilder();
682 
683                 query.append(
684                     "FROM com.liferay.portlet.expando.model.ExpandoTable ");
685 
686                 if (obc != null) {
687                     query.append("ORDER BY ");
688                     query.append(obc.getOrderBy());
689                 }
690 
691                 Query q = session.createQuery(query.toString());
692 
693                 List<ExpandoTable> list = null;
694 
695                 if (obc == null) {
696                     list = (List<ExpandoTable>)QueryUtil.list(q, getDialect(),
697                             start, end, false);
698 
699                     Collections.sort(list);
700                 }
701                 else {
702                     list = (List<ExpandoTable>)QueryUtil.list(q, getDialect(),
703                             start, end);
704                 }
705 
706                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
707                     finderClassName, finderMethodName, finderParams,
708                     finderArgs, list);
709 
710                 return list;
711             }
712             catch (Exception e) {
713                 throw processException(e);
714             }
715             finally {
716                 closeSession(session);
717             }
718         }
719         else {
720             return (List<ExpandoTable>)result;
721         }
722     }
723 
724     public void removeByClassNameId(long classNameId) throws SystemException {
725         for (ExpandoTable expandoTable : findByClassNameId(classNameId)) {
726             remove(expandoTable);
727         }
728     }
729 
730     public void removeByC_N(long classNameId, String name)
731         throws NoSuchTableException, SystemException {
732         ExpandoTable expandoTable = findByC_N(classNameId, name);
733 
734         remove(expandoTable);
735     }
736 
737     public void removeAll() throws SystemException {
738         for (ExpandoTable expandoTable : findAll()) {
739             remove(expandoTable);
740         }
741     }
742 
743     public int countByClassNameId(long classNameId) throws SystemException {
744         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
745         String finderClassName = ExpandoTable.class.getName();
746         String finderMethodName = "countByClassNameId";
747         String[] finderParams = new String[] { Long.class.getName() };
748         Object[] finderArgs = new Object[] { new Long(classNameId) };
749 
750         Object result = null;
751 
752         if (finderClassNameCacheEnabled) {
753             result = FinderCacheUtil.getResult(finderClassName,
754                     finderMethodName, finderParams, finderArgs, this);
755         }
756 
757         if (result == null) {
758             Session session = null;
759 
760             try {
761                 session = openSession();
762 
763                 StringBuilder query = new StringBuilder();
764 
765                 query.append("SELECT COUNT(*) ");
766                 query.append(
767                     "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
768 
769                 query.append("classNameId = ?");
770 
771                 query.append(" ");
772 
773                 Query q = session.createQuery(query.toString());
774 
775                 QueryPos qPos = QueryPos.getInstance(q);
776 
777                 qPos.add(classNameId);
778 
779                 Long count = null;
780 
781                 Iterator<Long> itr = q.list().iterator();
782 
783                 if (itr.hasNext()) {
784                     count = itr.next();
785                 }
786 
787                 if (count == null) {
788                     count = new Long(0);
789                 }
790 
791                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
792                     finderClassName, finderMethodName, finderParams,
793                     finderArgs, count);
794 
795                 return count.intValue();
796             }
797             catch (Exception e) {
798                 throw processException(e);
799             }
800             finally {
801                 closeSession(session);
802             }
803         }
804         else {
805             return ((Long)result).intValue();
806         }
807     }
808 
809     public int countByC_N(long classNameId, String name)
810         throws SystemException {
811         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
812         String finderClassName = ExpandoTable.class.getName();
813         String finderMethodName = "countByC_N";
814         String[] finderParams = new String[] {
815                 Long.class.getName(), String.class.getName()
816             };
817         Object[] finderArgs = new Object[] { new Long(classNameId), name };
818 
819         Object result = null;
820 
821         if (finderClassNameCacheEnabled) {
822             result = FinderCacheUtil.getResult(finderClassName,
823                     finderMethodName, finderParams, finderArgs, this);
824         }
825 
826         if (result == null) {
827             Session session = null;
828 
829             try {
830                 session = openSession();
831 
832                 StringBuilder query = new StringBuilder();
833 
834                 query.append("SELECT COUNT(*) ");
835                 query.append(
836                     "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
837 
838                 query.append("classNameId = ?");
839 
840                 query.append(" AND ");
841 
842                 if (name == null) {
843                     query.append("name IS NULL");
844                 }
845                 else {
846                     query.append("name = ?");
847                 }
848 
849                 query.append(" ");
850 
851                 Query q = session.createQuery(query.toString());
852 
853                 QueryPos qPos = QueryPos.getInstance(q);
854 
855                 qPos.add(classNameId);
856 
857                 if (name != null) {
858                     qPos.add(name);
859                 }
860 
861                 Long count = null;
862 
863                 Iterator<Long> itr = q.list().iterator();
864 
865                 if (itr.hasNext()) {
866                     count = itr.next();
867                 }
868 
869                 if (count == null) {
870                     count = new Long(0);
871                 }
872 
873                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
874                     finderClassName, finderMethodName, finderParams,
875                     finderArgs, count);
876 
877                 return count.intValue();
878             }
879             catch (Exception e) {
880                 throw processException(e);
881             }
882             finally {
883                 closeSession(session);
884             }
885         }
886         else {
887             return ((Long)result).intValue();
888         }
889     }
890 
891     public int countAll() throws SystemException {
892         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
893         String finderClassName = ExpandoTable.class.getName();
894         String finderMethodName = "countAll";
895         String[] finderParams = new String[] {  };
896         Object[] finderArgs = new Object[] {  };
897 
898         Object result = null;
899 
900         if (finderClassNameCacheEnabled) {
901             result = FinderCacheUtil.getResult(finderClassName,
902                     finderMethodName, finderParams, finderArgs, this);
903         }
904 
905         if (result == null) {
906             Session session = null;
907 
908             try {
909                 session = openSession();
910 
911                 Query q = session.createQuery(
912                         "SELECT COUNT(*) FROM com.liferay.portlet.expando.model.ExpandoTable");
913 
914                 Long count = null;
915 
916                 Iterator<Long> itr = q.list().iterator();
917 
918                 if (itr.hasNext()) {
919                     count = itr.next();
920                 }
921 
922                 if (count == null) {
923                     count = new Long(0);
924                 }
925 
926                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
927                     finderClassName, finderMethodName, finderParams,
928                     finderArgs, count);
929 
930                 return count.intValue();
931             }
932             catch (Exception e) {
933                 throw processException(e);
934             }
935             finally {
936                 closeSession(session);
937             }
938         }
939         else {
940             return ((Long)result).intValue();
941         }
942     }
943 
944     public void registerListener(ModelListener listener) {
945         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
946 
947         listeners.add(listener);
948 
949         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
950     }
951 
952     public void unregisterListener(ModelListener listener) {
953         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
954 
955         listeners.remove(listener);
956 
957         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
958     }
959 
960     public void afterPropertiesSet() {
961         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
962                     com.liferay.portal.util.PropsUtil.get(
963                         "value.object.listener.com.liferay.portlet.expando.model.ExpandoTable")));
964 
965         if (listenerClassNames.length > 0) {
966             try {
967                 List<ModelListener> listeners = new ArrayList<ModelListener>();
968 
969                 for (String listenerClassName : listenerClassNames) {
970                     listeners.add((ModelListener)Class.forName(
971                             listenerClassName).newInstance());
972                 }
973 
974                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
975             }
976             catch (Exception e) {
977                 _log.error(e);
978             }
979         }
980     }
981 
982     private static Log _log = LogFactory.getLog(ExpandoTablePersistenceImpl.class);
983     private ModelListener[] _listeners = new ModelListener[0];
984 }