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