1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.expando.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
27  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
28  import com.liferay.portal.kernel.dao.orm.Query;
29  import com.liferay.portal.kernel.dao.orm.QueryPos;
30  import com.liferay.portal.kernel.dao.orm.QueryUtil;
31  import com.liferay.portal.kernel.dao.orm.Session;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.ListUtil;
34  import com.liferay.portal.kernel.util.OrderByComparator;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.model.ModelListener;
38  import com.liferay.portal.service.persistence.BatchSessionUtil;
39  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
40  
41  import com.liferay.portlet.expando.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 {
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             if (BatchSessionUtil.isEnabled()) {
131                 Object staleObject = session.get(ExpandoColumnImpl.class,
132                         expandoColumn.getPrimaryKeyObj());
133 
134                 if (staleObject != null) {
135                     session.evict(staleObject);
136                 }
137             }
138 
139             session.delete(expandoColumn);
140 
141             session.flush();
142 
143             return expandoColumn;
144         }
145         catch (Exception e) {
146             throw processException(e);
147         }
148         finally {
149             closeSession(session);
150 
151             FinderCacheUtil.clearCache(ExpandoColumn.class.getName());
152         }
153     }
154 
155     /**
156      * @deprecated Use <code>update(ExpandoColumn expandoColumn, boolean merge)</code>.
157      */
158     public ExpandoColumn update(ExpandoColumn expandoColumn)
159         throws SystemException {
160         if (_log.isWarnEnabled()) {
161             _log.warn(
162                 "Using the deprecated update(ExpandoColumn expandoColumn) method. Use update(ExpandoColumn expandoColumn, boolean merge) instead.");
163         }
164 
165         return update(expandoColumn, false);
166     }
167 
168     /**
169      * Add, update, or merge, the entity. This method also calls the model
170      * listeners to trigger the proper events associated with adding, deleting,
171      * or updating an entity.
172      *
173      * @param        expandoColumn the entity to add, update, or merge
174      * @param        merge boolean value for whether to merge the entity. The
175      *                default value is false. Setting merge to true is more
176      *                expensive and should only be true when expandoColumn is
177      *                transient. See LEP-5473 for a detailed discussion of this
178      *                method.
179      * @return        true if the portlet can be displayed via Ajax
180      */
181     public ExpandoColumn update(ExpandoColumn expandoColumn, boolean merge)
182         throws SystemException {
183         boolean isNew = expandoColumn.isNew();
184 
185         if (_listeners.length > 0) {
186             for (ModelListener listener : _listeners) {
187                 if (isNew) {
188                     listener.onBeforeCreate(expandoColumn);
189                 }
190                 else {
191                     listener.onBeforeUpdate(expandoColumn);
192                 }
193             }
194         }
195 
196         expandoColumn = updateImpl(expandoColumn, merge);
197 
198         if (_listeners.length > 0) {
199             for (ModelListener listener : _listeners) {
200                 if (isNew) {
201                     listener.onAfterCreate(expandoColumn);
202                 }
203                 else {
204                     listener.onAfterUpdate(expandoColumn);
205                 }
206             }
207         }
208 
209         return expandoColumn;
210     }
211 
212     public ExpandoColumn updateImpl(
213         com.liferay.portlet.expando.model.ExpandoColumn expandoColumn,
214         boolean merge) throws SystemException {
215         Session session = null;
216 
217         try {
218             session = openSession();
219 
220             BatchSessionUtil.update(session, expandoColumn, merge);
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 = null;
693 
694                 if (obc == null) {
695                     list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
696                             start, end, false);
697 
698                     Collections.sort(list);
699                 }
700                 else {
701                     list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
702                             start, end);
703                 }
704 
705                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
706                     finderClassName, finderMethodName, finderParams,
707                     finderArgs, list);
708 
709                 return list;
710             }
711             catch (Exception e) {
712                 throw processException(e);
713             }
714             finally {
715                 closeSession(session);
716             }
717         }
718         else {
719             return (List<ExpandoColumn>)result;
720         }
721     }
722 
723     public void removeByTableId(long tableId) throws SystemException {
724         for (ExpandoColumn expandoColumn : findByTableId(tableId)) {
725             remove(expandoColumn);
726         }
727     }
728 
729     public void removeByT_N(long tableId, String name)
730         throws NoSuchColumnException, SystemException {
731         ExpandoColumn expandoColumn = findByT_N(tableId, name);
732 
733         remove(expandoColumn);
734     }
735 
736     public void removeAll() throws SystemException {
737         for (ExpandoColumn expandoColumn : findAll()) {
738             remove(expandoColumn);
739         }
740     }
741 
742     public int countByTableId(long tableId) throws SystemException {
743         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
744         String finderClassName = ExpandoColumn.class.getName();
745         String finderMethodName = "countByTableId";
746         String[] finderParams = new String[] { Long.class.getName() };
747         Object[] finderArgs = new Object[] { new Long(tableId) };
748 
749         Object result = null;
750 
751         if (finderClassNameCacheEnabled) {
752             result = FinderCacheUtil.getResult(finderClassName,
753                     finderMethodName, finderParams, finderArgs, this);
754         }
755 
756         if (result == null) {
757             Session session = null;
758 
759             try {
760                 session = openSession();
761 
762                 StringBuilder query = new StringBuilder();
763 
764                 query.append("SELECT COUNT(*) ");
765                 query.append(
766                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
767 
768                 query.append("tableId = ?");
769 
770                 query.append(" ");
771 
772                 Query q = session.createQuery(query.toString());
773 
774                 QueryPos qPos = QueryPos.getInstance(q);
775 
776                 qPos.add(tableId);
777 
778                 Long count = null;
779 
780                 Iterator<Long> itr = q.list().iterator();
781 
782                 if (itr.hasNext()) {
783                     count = itr.next();
784                 }
785 
786                 if (count == null) {
787                     count = new Long(0);
788                 }
789 
790                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
791                     finderClassName, finderMethodName, finderParams,
792                     finderArgs, count);
793 
794                 return count.intValue();
795             }
796             catch (Exception e) {
797                 throw processException(e);
798             }
799             finally {
800                 closeSession(session);
801             }
802         }
803         else {
804             return ((Long)result).intValue();
805         }
806     }
807 
808     public int countByT_N(long tableId, String name) throws SystemException {
809         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
810         String finderClassName = ExpandoColumn.class.getName();
811         String finderMethodName = "countByT_N";
812         String[] finderParams = new String[] {
813                 Long.class.getName(), String.class.getName()
814             };
815         Object[] finderArgs = new Object[] { new Long(tableId), name };
816 
817         Object result = null;
818 
819         if (finderClassNameCacheEnabled) {
820             result = FinderCacheUtil.getResult(finderClassName,
821                     finderMethodName, finderParams, finderArgs, this);
822         }
823 
824         if (result == null) {
825             Session session = null;
826 
827             try {
828                 session = openSession();
829 
830                 StringBuilder query = new StringBuilder();
831 
832                 query.append("SELECT COUNT(*) ");
833                 query.append(
834                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
835 
836                 query.append("tableId = ?");
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                 QueryPos qPos = QueryPos.getInstance(q);
852 
853                 qPos.add(tableId);
854 
855                 if (name != null) {
856                     qPos.add(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                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
872                     finderClassName, finderMethodName, finderParams,
873                     finderArgs, count);
874 
875                 return count.intValue();
876             }
877             catch (Exception e) {
878                 throw 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 = ExpandoColumnModelImpl.CACHE_ENABLED;
891         String finderClassName = ExpandoColumn.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 = FinderCacheUtil.getResult(finderClassName,
900                     finderMethodName, finderParams, finderArgs, this);
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.ExpandoColumn");
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                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
925                     finderClassName, finderMethodName, finderParams,
926                     finderArgs, count);
927 
928                 return count.intValue();
929             }
930             catch (Exception e) {
931                 throw processException(e);
932             }
933             finally {
934                 closeSession(session);
935             }
936         }
937         else {
938             return ((Long)result).intValue();
939         }
940     }
941 
942     public void registerListener(ModelListener listener) {
943         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
944 
945         listeners.add(listener);
946 
947         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
948     }
949 
950     public void unregisterListener(ModelListener listener) {
951         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
952 
953         listeners.remove(listener);
954 
955         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
956     }
957 
958     public void afterPropertiesSet() {
959         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
960                     com.liferay.portal.util.PropsUtil.get(
961                         "value.object.listener.com.liferay.portlet.expando.model.ExpandoColumn")));
962 
963         if (listenerClassNames.length > 0) {
964             try {
965                 List<ModelListener> listeners = new ArrayList<ModelListener>();
966 
967                 for (String listenerClassName : listenerClassNames) {
968                     listeners.add((ModelListener)Class.forName(
969                             listenerClassName).newInstance());
970                 }
971 
972                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
973             }
974             catch (Exception e) {
975                 _log.error(e);
976             }
977         }
978     }
979 
980     private static Log _log = LogFactory.getLog(ExpandoColumnPersistenceImpl.class);
981     private ModelListener[] _listeners = new ModelListener[0];
982 }