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