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.bean.InitializingBean;
27  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
28  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.QueryPos;
31  import com.liferay.portal.kernel.dao.orm.QueryUtil;
32  import com.liferay.portal.kernel.dao.orm.Session;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.ListUtil;
35  import com.liferay.portal.kernel.util.OrderByComparator;
36  import com.liferay.portal.kernel.util.StringPool;
37  import com.liferay.portal.kernel.util.StringUtil;
38  import com.liferay.portal.model.ModelListener;
39  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
40  
41  import com.liferay.portlet.expando.NoSuchColumnException;
42  import com.liferay.portlet.expando.model.ExpandoColumn;
43  import com.liferay.portlet.expando.model.impl.ExpandoColumnImpl;
44  import com.liferay.portlet.expando.model.impl.ExpandoColumnModelImpl;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.Iterator;
52  import java.util.List;
53  
54  /**
55   * <a href="ExpandoColumnPersistenceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class ExpandoColumnPersistenceImpl extends BasePersistenceImpl
61      implements ExpandoColumnPersistence, InitializingBean {
62      public ExpandoColumn create(long columnId) {
63          ExpandoColumn expandoColumn = new ExpandoColumnImpl();
64  
65          expandoColumn.setNew(true);
66          expandoColumn.setPrimaryKey(columnId);
67  
68          return expandoColumn;
69      }
70  
71      public ExpandoColumn remove(long columnId)
72          throws NoSuchColumnException, SystemException {
73          Session session = null;
74  
75          try {
76              session = openSession();
77  
78              ExpandoColumn expandoColumn = (ExpandoColumn)session.get(ExpandoColumnImpl.class,
79                      new Long(columnId));
80  
81              if (expandoColumn == null) {
82                  if (_log.isWarnEnabled()) {
83                      _log.warn("No ExpandoColumn exists with the primary key " +
84                          columnId);
85                  }
86  
87                  throw new NoSuchColumnException(
88                      "No ExpandoColumn exists with the primary key " + columnId);
89              }
90  
91              return remove(expandoColumn);
92          }
93          catch (NoSuchColumnException nsee) {
94              throw nsee;
95          }
96          catch (Exception e) {
97              throw processException(e);
98          }
99          finally {
100             closeSession(session);
101         }
102     }
103 
104     public ExpandoColumn remove(ExpandoColumn expandoColumn)
105         throws SystemException {
106         if (_listeners.length > 0) {
107             for (ModelListener listener : _listeners) {
108                 listener.onBeforeRemove(expandoColumn);
109             }
110         }
111 
112         expandoColumn = removeImpl(expandoColumn);
113 
114         if (_listeners.length > 0) {
115             for (ModelListener listener : _listeners) {
116                 listener.onAfterRemove(expandoColumn);
117             }
118         }
119 
120         return expandoColumn;
121     }
122 
123     protected ExpandoColumn removeImpl(ExpandoColumn expandoColumn)
124         throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             session.delete(expandoColumn);
131 
132             session.flush();
133 
134             return expandoColumn;
135         }
136         catch (Exception e) {
137             throw processException(e);
138         }
139         finally {
140             closeSession(session);
141 
142             FinderCacheUtil.clearCache(ExpandoColumn.class.getName());
143         }
144     }
145 
146     /**
147      * @deprecated Use <code>update(ExpandoColumn expandoColumn, boolean merge)</code>.
148      */
149     public ExpandoColumn update(ExpandoColumn expandoColumn)
150         throws SystemException {
151         if (_log.isWarnEnabled()) {
152             _log.warn(
153                 "Using the deprecated update(ExpandoColumn expandoColumn) method. Use update(ExpandoColumn expandoColumn, boolean merge) instead.");
154         }
155 
156         return update(expandoColumn, false);
157     }
158 
159     /**
160      * Add, update, or merge, the entity. This method also calls the model
161      * listeners to trigger the proper events associated with adding, deleting,
162      * or updating an entity.
163      *
164      * @param        expandoColumn the entity to add, update, or merge
165      * @param        merge boolean value for whether to merge the entity. The
166      *                default value is false. Setting merge to true is more
167      *                expensive and should only be true when expandoColumn is
168      *                transient. See LEP-5473 for a detailed discussion of this
169      *                method.
170      * @return        true if the portlet can be displayed via Ajax
171      */
172     public ExpandoColumn update(ExpandoColumn expandoColumn, boolean merge)
173         throws SystemException {
174         boolean isNew = expandoColumn.isNew();
175 
176         if (_listeners.length > 0) {
177             for (ModelListener listener : _listeners) {
178                 if (isNew) {
179                     listener.onBeforeCreate(expandoColumn);
180                 }
181                 else {
182                     listener.onBeforeUpdate(expandoColumn);
183                 }
184             }
185         }
186 
187         expandoColumn = updateImpl(expandoColumn, merge);
188 
189         if (_listeners.length > 0) {
190             for (ModelListener listener : _listeners) {
191                 if (isNew) {
192                     listener.onAfterCreate(expandoColumn);
193                 }
194                 else {
195                     listener.onAfterUpdate(expandoColumn);
196                 }
197             }
198         }
199 
200         return expandoColumn;
201     }
202 
203     public ExpandoColumn updateImpl(
204         com.liferay.portlet.expando.model.ExpandoColumn expandoColumn,
205         boolean merge) throws SystemException {
206         Session session = null;
207 
208         try {
209             session = openSession();
210 
211             if (merge) {
212                 session.merge(expandoColumn);
213             }
214             else {
215                 if (expandoColumn.isNew()) {
216                     session.save(expandoColumn);
217                 }
218             }
219 
220             session.flush();
221 
222             expandoColumn.setNew(false);
223 
224             return expandoColumn;
225         }
226         catch (Exception e) {
227             throw processException(e);
228         }
229         finally {
230             closeSession(session);
231 
232             FinderCacheUtil.clearCache(ExpandoColumn.class.getName());
233         }
234     }
235 
236     public ExpandoColumn findByPrimaryKey(long columnId)
237         throws NoSuchColumnException, SystemException {
238         ExpandoColumn expandoColumn = fetchByPrimaryKey(columnId);
239 
240         if (expandoColumn == null) {
241             if (_log.isWarnEnabled()) {
242                 _log.warn("No ExpandoColumn exists with the primary key " +
243                     columnId);
244             }
245 
246             throw new NoSuchColumnException(
247                 "No ExpandoColumn exists with the primary key " + columnId);
248         }
249 
250         return expandoColumn;
251     }
252 
253     public ExpandoColumn fetchByPrimaryKey(long columnId)
254         throws SystemException {
255         Session session = null;
256 
257         try {
258             session = openSession();
259 
260             return (ExpandoColumn)session.get(ExpandoColumnImpl.class,
261                 new Long(columnId));
262         }
263         catch (Exception e) {
264             throw processException(e);
265         }
266         finally {
267             closeSession(session);
268         }
269     }
270 
271     public List<ExpandoColumn> findByTableId(long tableId)
272         throws SystemException {
273         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
274         String finderClassName = ExpandoColumn.class.getName();
275         String finderMethodName = "findByTableId";
276         String[] finderParams = new String[] { Long.class.getName() };
277         Object[] finderArgs = new Object[] { new Long(tableId) };
278 
279         Object result = null;
280 
281         if (finderClassNameCacheEnabled) {
282             result = FinderCacheUtil.getResult(finderClassName,
283                     finderMethodName, finderParams, finderArgs, this);
284         }
285 
286         if (result == null) {
287             Session session = null;
288 
289             try {
290                 session = openSession();
291 
292                 StringBuilder query = new StringBuilder();
293 
294                 query.append(
295                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
296 
297                 query.append("tableId = ?");
298 
299                 query.append(" ");
300 
301                 Query q = session.createQuery(query.toString());
302 
303                 QueryPos qPos = QueryPos.getInstance(q);
304 
305                 qPos.add(tableId);
306 
307                 List<ExpandoColumn> list = q.list();
308 
309                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
310                     finderClassName, finderMethodName, finderParams,
311                     finderArgs, list);
312 
313                 return list;
314             }
315             catch (Exception e) {
316                 throw processException(e);
317             }
318             finally {
319                 closeSession(session);
320             }
321         }
322         else {
323             return (List<ExpandoColumn>)result;
324         }
325     }
326 
327     public List<ExpandoColumn> findByTableId(long tableId, int start, int end)
328         throws SystemException {
329         return findByTableId(tableId, start, end, null);
330     }
331 
332     public List<ExpandoColumn> findByTableId(long tableId, int start, int end,
333         OrderByComparator obc) throws SystemException {
334         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
335         String finderClassName = ExpandoColumn.class.getName();
336         String finderMethodName = "findByTableId";
337         String[] finderParams = new String[] {
338                 Long.class.getName(),
339                 
340                 "java.lang.Integer", "java.lang.Integer",
341                 "com.liferay.portal.kernel.util.OrderByComparator"
342             };
343         Object[] finderArgs = new Object[] {
344                 new Long(tableId),
345                 
346                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
347             };
348 
349         Object result = null;
350 
351         if (finderClassNameCacheEnabled) {
352             result = FinderCacheUtil.getResult(finderClassName,
353                     finderMethodName, finderParams, finderArgs, this);
354         }
355 
356         if (result == null) {
357             Session session = null;
358 
359             try {
360                 session = openSession();
361 
362                 StringBuilder query = new StringBuilder();
363 
364                 query.append(
365                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
366 
367                 query.append("tableId = ?");
368 
369                 query.append(" ");
370 
371                 if (obc != null) {
372                     query.append("ORDER BY ");
373                     query.append(obc.getOrderBy());
374                 }
375 
376                 Query q = session.createQuery(query.toString());
377 
378                 QueryPos qPos = QueryPos.getInstance(q);
379 
380                 qPos.add(tableId);
381 
382                 List<ExpandoColumn> list = (List<ExpandoColumn>)QueryUtil.list(q,
383                         getDialect(), start, end);
384 
385                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
386                     finderClassName, finderMethodName, finderParams,
387                     finderArgs, list);
388 
389                 return list;
390             }
391             catch (Exception e) {
392                 throw processException(e);
393             }
394             finally {
395                 closeSession(session);
396             }
397         }
398         else {
399             return (List<ExpandoColumn>)result;
400         }
401     }
402 
403     public ExpandoColumn findByTableId_First(long tableId, OrderByComparator obc)
404         throws NoSuchColumnException, SystemException {
405         List<ExpandoColumn> list = findByTableId(tableId, 0, 1, obc);
406 
407         if (list.size() == 0) {
408             StringBuilder msg = new StringBuilder();
409 
410             msg.append("No ExpandoColumn exists with the key {");
411 
412             msg.append("tableId=" + tableId);
413 
414             msg.append(StringPool.CLOSE_CURLY_BRACE);
415 
416             throw new NoSuchColumnException(msg.toString());
417         }
418         else {
419             return list.get(0);
420         }
421     }
422 
423     public ExpandoColumn findByTableId_Last(long tableId, OrderByComparator obc)
424         throws NoSuchColumnException, SystemException {
425         int count = countByTableId(tableId);
426 
427         List<ExpandoColumn> list = findByTableId(tableId, count - 1, count, obc);
428 
429         if (list.size() == 0) {
430             StringBuilder msg = new StringBuilder();
431 
432             msg.append("No ExpandoColumn exists with the key {");
433 
434             msg.append("tableId=" + tableId);
435 
436             msg.append(StringPool.CLOSE_CURLY_BRACE);
437 
438             throw new NoSuchColumnException(msg.toString());
439         }
440         else {
441             return list.get(0);
442         }
443     }
444 
445     public ExpandoColumn[] findByTableId_PrevAndNext(long columnId,
446         long tableId, OrderByComparator obc)
447         throws NoSuchColumnException, SystemException {
448         ExpandoColumn expandoColumn = findByPrimaryKey(columnId);
449 
450         int count = countByTableId(tableId);
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.ExpandoColumn WHERE ");
461 
462             query.append("tableId = ?");
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(tableId);
476 
477             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
478                     expandoColumn);
479 
480             ExpandoColumn[] array = new ExpandoColumnImpl[3];
481 
482             array[0] = (ExpandoColumn)objArray[0];
483             array[1] = (ExpandoColumn)objArray[1];
484             array[2] = (ExpandoColumn)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 ExpandoColumn findByT_N(long tableId, String name)
497         throws NoSuchColumnException, SystemException {
498         ExpandoColumn expandoColumn = fetchByT_N(tableId, name);
499 
500         if (expandoColumn == null) {
501             StringBuilder msg = new StringBuilder();
502 
503             msg.append("No ExpandoColumn exists with the key {");
504 
505             msg.append("tableId=" + tableId);
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 NoSuchColumnException(msg.toString());
517         }
518 
519         return expandoColumn;
520     }
521 
522     public ExpandoColumn fetchByT_N(long tableId, String name)
523         throws SystemException {
524         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
525         String finderClassName = ExpandoColumn.class.getName();
526         String finderMethodName = "fetchByT_N";
527         String[] finderParams = new String[] {
528                 Long.class.getName(), String.class.getName()
529             };
530         Object[] finderArgs = new Object[] { new Long(tableId), 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.ExpandoColumn WHERE ");
549 
550                 query.append("tableId = ?");
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(tableId);
568 
569                 if (name != null) {
570                     qPos.add(name);
571                 }
572 
573                 List<ExpandoColumn> 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<ExpandoColumn> list = (List<ExpandoColumn>)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<ExpandoColumn> findAll() throws SystemException {
646         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
647     }
648 
649     public List<ExpandoColumn> findAll(int start, int end)
650         throws SystemException {
651         return findAll(start, end, null);
652     }
653 
654     public List<ExpandoColumn> findAll(int start, int end, OrderByComparator obc)
655         throws SystemException {
656         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
657         String finderClassName = ExpandoColumn.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.ExpandoColumn ");
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<ExpandoColumn> list = (List<ExpandoColumn>)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<ExpandoColumn>)result;
714         }
715     }
716 
717     public void removeByTableId(long tableId) throws SystemException {
718         for (ExpandoColumn expandoColumn : findByTableId(tableId)) {
719             remove(expandoColumn);
720         }
721     }
722 
723     public void removeByT_N(long tableId, String name)
724         throws NoSuchColumnException, SystemException {
725         ExpandoColumn expandoColumn = findByT_N(tableId, name);
726 
727         remove(expandoColumn);
728     }
729 
730     public void removeAll() throws SystemException {
731         for (ExpandoColumn expandoColumn : findAll()) {
732             remove(expandoColumn);
733         }
734     }
735 
736     public int countByTableId(long tableId) throws SystemException {
737         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
738         String finderClassName = ExpandoColumn.class.getName();
739         String finderMethodName = "countByTableId";
740         String[] finderParams = new String[] { Long.class.getName() };
741         Object[] finderArgs = new Object[] { new Long(tableId) };
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.ExpandoColumn WHERE ");
761 
762                 query.append("tableId = ?");
763 
764                 query.append(" ");
765 
766                 Query q = session.createQuery(query.toString());
767 
768                 QueryPos qPos = QueryPos.getInstance(q);
769 
770                 qPos.add(tableId);
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 countByT_N(long tableId, String name) throws SystemException {
803         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
804         String finderClassName = ExpandoColumn.class.getName();
805         String finderMethodName = "countByT_N";
806         String[] finderParams = new String[] {
807                 Long.class.getName(), String.class.getName()
808             };
809         Object[] finderArgs = new Object[] { new Long(tableId), name };
810 
811         Object result = null;
812 
813         if (finderClassNameCacheEnabled) {
814             result = FinderCacheUtil.getResult(finderClassName,
815                     finderMethodName, finderParams, finderArgs, this);
816         }
817 
818         if (result == null) {
819             Session session = null;
820 
821             try {
822                 session = openSession();
823 
824                 StringBuilder query = new StringBuilder();
825 
826                 query.append("SELECT COUNT(*) ");
827                 query.append(
828                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
829 
830                 query.append("tableId = ?");
831 
832                 query.append(" AND ");
833 
834                 if (name == null) {
835                     query.append("name IS NULL");
836                 }
837                 else {
838                     query.append("name = ?");
839                 }
840 
841                 query.append(" ");
842 
843                 Query q = session.createQuery(query.toString());
844 
845                 QueryPos qPos = QueryPos.getInstance(q);
846 
847                 qPos.add(tableId);
848 
849                 if (name != null) {
850                     qPos.add(name);
851                 }
852 
853                 Long count = null;
854 
855                 Iterator<Long> itr = q.list().iterator();
856 
857                 if (itr.hasNext()) {
858                     count = itr.next();
859                 }
860 
861                 if (count == null) {
862                     count = new Long(0);
863                 }
864 
865                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
866                     finderClassName, finderMethodName, finderParams,
867                     finderArgs, count);
868 
869                 return count.intValue();
870             }
871             catch (Exception e) {
872                 throw processException(e);
873             }
874             finally {
875                 closeSession(session);
876             }
877         }
878         else {
879             return ((Long)result).intValue();
880         }
881     }
882 
883     public int countAll() throws SystemException {
884         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
885         String finderClassName = ExpandoColumn.class.getName();
886         String finderMethodName = "countAll";
887         String[] finderParams = new String[] {  };
888         Object[] finderArgs = new Object[] {  };
889 
890         Object result = null;
891 
892         if (finderClassNameCacheEnabled) {
893             result = FinderCacheUtil.getResult(finderClassName,
894                     finderMethodName, finderParams, finderArgs, this);
895         }
896 
897         if (result == null) {
898             Session session = null;
899 
900             try {
901                 session = openSession();
902 
903                 Query q = session.createQuery(
904                         "SELECT COUNT(*) FROM com.liferay.portlet.expando.model.ExpandoColumn");
905 
906                 Long count = null;
907 
908                 Iterator<Long> itr = q.list().iterator();
909 
910                 if (itr.hasNext()) {
911                     count = itr.next();
912                 }
913 
914                 if (count == null) {
915                     count = new Long(0);
916                 }
917 
918                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
919                     finderClassName, finderMethodName, finderParams,
920                     finderArgs, count);
921 
922                 return count.intValue();
923             }
924             catch (Exception e) {
925                 throw processException(e);
926             }
927             finally {
928                 closeSession(session);
929             }
930         }
931         else {
932             return ((Long)result).intValue();
933         }
934     }
935 
936     public void registerListener(ModelListener listener) {
937         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
938 
939         listeners.add(listener);
940 
941         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
942     }
943 
944     public void unregisterListener(ModelListener listener) {
945         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
946 
947         listeners.remove(listener);
948 
949         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
950     }
951 
952     public void afterPropertiesSet() {
953         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
954                     com.liferay.portal.util.PropsUtil.get(
955                         "value.object.listener.com.liferay.portlet.expando.model.ExpandoColumn")));
956 
957         if (listenerClassNames.length > 0) {
958             try {
959                 List<ModelListener> listeners = new ArrayList<ModelListener>();
960 
961                 for (String listenerClassName : listenerClassNames) {
962                     listeners.add((ModelListener)Class.forName(
963                             listenerClassName).newInstance());
964                 }
965 
966                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
967             }
968             catch (Exception e) {
969                 _log.error(e);
970             }
971         }
972     }
973 
974     private static Log _log = LogFactory.getLog(ExpandoColumnPersistenceImpl.class);
975     private ModelListener[] _listeners = new ModelListener[0];
976 }