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