1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.expando.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
24  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
25  import com.liferay.portal.kernel.dao.orm.Query;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.Session;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.StringUtil;
35  import com.liferay.portal.model.ModelListener;
36  import com.liferay.portal.service.persistence.BatchSessionUtil;
37  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
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 java.util.ArrayList;
45  import java.util.Collections;
46  import java.util.Iterator;
47  import java.util.List;
48  
49  /**
50   * <a href="ExpandoColumnPersistenceImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class ExpandoColumnPersistenceImpl extends BasePersistenceImpl
56      implements ExpandoColumnPersistence {
57      public ExpandoColumn create(long columnId) {
58          ExpandoColumn expandoColumn = new ExpandoColumnImpl();
59  
60          expandoColumn.setNew(true);
61          expandoColumn.setPrimaryKey(columnId);
62  
63          return expandoColumn;
64      }
65  
66      public ExpandoColumn remove(long columnId)
67          throws NoSuchColumnException, SystemException {
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              ExpandoColumn expandoColumn = (ExpandoColumn)session.get(ExpandoColumnImpl.class,
74                      new Long(columnId));
75  
76              if (expandoColumn == null) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn("No ExpandoColumn exists with the primary key " +
79                          columnId);
80                  }
81  
82                  throw new NoSuchColumnException(
83                      "No ExpandoColumn exists with the primary key " + columnId);
84              }
85  
86              return remove(expandoColumn);
87          }
88          catch (NoSuchColumnException nsee) {
89              throw nsee;
90          }
91          catch (Exception e) {
92              throw processException(e);
93          }
94          finally {
95              closeSession(session);
96          }
97      }
98  
99      public ExpandoColumn remove(ExpandoColumn expandoColumn)
100         throws SystemException {
101         for (ModelListener listener : listeners) {
102             listener.onBeforeRemove(expandoColumn);
103         }
104 
105         expandoColumn = removeImpl(expandoColumn);
106 
107         for (ModelListener listener : listeners) {
108             listener.onAfterRemove(expandoColumn);
109         }
110 
111         return expandoColumn;
112     }
113 
114     protected ExpandoColumn removeImpl(ExpandoColumn expandoColumn)
115         throws SystemException {
116         Session session = null;
117 
118         try {
119             session = openSession();
120 
121             if (BatchSessionUtil.isEnabled()) {
122                 Object staleObject = session.get(ExpandoColumnImpl.class,
123                         expandoColumn.getPrimaryKeyObj());
124 
125                 if (staleObject != null) {
126                     session.evict(staleObject);
127                 }
128             }
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         for (ModelListener listener : listeners) {
177             if (isNew) {
178                 listener.onBeforeCreate(expandoColumn);
179             }
180             else {
181                 listener.onBeforeUpdate(expandoColumn);
182             }
183         }
184 
185         expandoColumn = updateImpl(expandoColumn, merge);
186 
187         for (ModelListener listener : listeners) {
188             if (isNew) {
189                 listener.onAfterCreate(expandoColumn);
190             }
191             else {
192                 listener.onAfterUpdate(expandoColumn);
193             }
194         }
195 
196         return expandoColumn;
197     }
198 
199     public ExpandoColumn updateImpl(
200         com.liferay.portlet.expando.model.ExpandoColumn expandoColumn,
201         boolean merge) throws SystemException {
202         Session session = null;
203 
204         try {
205             session = openSession();
206 
207             BatchSessionUtil.update(session, expandoColumn, merge);
208 
209             expandoColumn.setNew(false);
210 
211             return expandoColumn;
212         }
213         catch (Exception e) {
214             throw processException(e);
215         }
216         finally {
217             closeSession(session);
218 
219             FinderCacheUtil.clearCache(ExpandoColumn.class.getName());
220         }
221     }
222 
223     public ExpandoColumn findByPrimaryKey(long columnId)
224         throws NoSuchColumnException, SystemException {
225         ExpandoColumn expandoColumn = fetchByPrimaryKey(columnId);
226 
227         if (expandoColumn == null) {
228             if (_log.isWarnEnabled()) {
229                 _log.warn("No ExpandoColumn exists with the primary key " +
230                     columnId);
231             }
232 
233             throw new NoSuchColumnException(
234                 "No ExpandoColumn exists with the primary key " + columnId);
235         }
236 
237         return expandoColumn;
238     }
239 
240     public ExpandoColumn fetchByPrimaryKey(long columnId)
241         throws SystemException {
242         Session session = null;
243 
244         try {
245             session = openSession();
246 
247             return (ExpandoColumn)session.get(ExpandoColumnImpl.class,
248                 new Long(columnId));
249         }
250         catch (Exception e) {
251             throw processException(e);
252         }
253         finally {
254             closeSession(session);
255         }
256     }
257 
258     public List<ExpandoColumn> findByTableId(long tableId)
259         throws SystemException {
260         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
261         String finderClassName = ExpandoColumn.class.getName();
262         String finderMethodName = "findByTableId";
263         String[] finderParams = new String[] { Long.class.getName() };
264         Object[] finderArgs = new Object[] { new Long(tableId) };
265 
266         Object result = null;
267 
268         if (finderClassNameCacheEnabled) {
269             result = FinderCacheUtil.getResult(finderClassName,
270                     finderMethodName, finderParams, finderArgs, this);
271         }
272 
273         if (result == null) {
274             Session session = null;
275 
276             try {
277                 session = openSession();
278 
279                 StringBuilder query = new StringBuilder();
280 
281                 query.append(
282                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
283 
284                 query.append("tableId = ?");
285 
286                 query.append(" ");
287 
288                 Query q = session.createQuery(query.toString());
289 
290                 QueryPos qPos = QueryPos.getInstance(q);
291 
292                 qPos.add(tableId);
293 
294                 List<ExpandoColumn> list = q.list();
295 
296                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
297                     finderClassName, finderMethodName, finderParams,
298                     finderArgs, list);
299 
300                 return list;
301             }
302             catch (Exception e) {
303                 throw processException(e);
304             }
305             finally {
306                 closeSession(session);
307             }
308         }
309         else {
310             return (List<ExpandoColumn>)result;
311         }
312     }
313 
314     public List<ExpandoColumn> findByTableId(long tableId, int start, int end)
315         throws SystemException {
316         return findByTableId(tableId, start, end, null);
317     }
318 
319     public List<ExpandoColumn> findByTableId(long tableId, int start, int end,
320         OrderByComparator obc) throws SystemException {
321         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
322         String finderClassName = ExpandoColumn.class.getName();
323         String finderMethodName = "findByTableId";
324         String[] finderParams = new String[] {
325                 Long.class.getName(),
326                 
327                 "java.lang.Integer", "java.lang.Integer",
328                 "com.liferay.portal.kernel.util.OrderByComparator"
329             };
330         Object[] finderArgs = new Object[] {
331                 new Long(tableId),
332                 
333                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
334             };
335 
336         Object result = null;
337 
338         if (finderClassNameCacheEnabled) {
339             result = FinderCacheUtil.getResult(finderClassName,
340                     finderMethodName, finderParams, finderArgs, this);
341         }
342 
343         if (result == null) {
344             Session session = null;
345 
346             try {
347                 session = openSession();
348 
349                 StringBuilder query = new StringBuilder();
350 
351                 query.append(
352                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
353 
354                 query.append("tableId = ?");
355 
356                 query.append(" ");
357 
358                 if (obc != null) {
359                     query.append("ORDER BY ");
360                     query.append(obc.getOrderBy());
361                 }
362 
363                 Query q = session.createQuery(query.toString());
364 
365                 QueryPos qPos = QueryPos.getInstance(q);
366 
367                 qPos.add(tableId);
368 
369                 List<ExpandoColumn> list = (List<ExpandoColumn>)QueryUtil.list(q,
370                         getDialect(), start, end);
371 
372                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
373                     finderClassName, finderMethodName, finderParams,
374                     finderArgs, list);
375 
376                 return list;
377             }
378             catch (Exception e) {
379                 throw processException(e);
380             }
381             finally {
382                 closeSession(session);
383             }
384         }
385         else {
386             return (List<ExpandoColumn>)result;
387         }
388     }
389 
390     public ExpandoColumn findByTableId_First(long tableId, OrderByComparator obc)
391         throws NoSuchColumnException, SystemException {
392         List<ExpandoColumn> list = findByTableId(tableId, 0, 1, obc);
393 
394         if (list.size() == 0) {
395             StringBuilder msg = new StringBuilder();
396 
397             msg.append("No ExpandoColumn exists with the key {");
398 
399             msg.append("tableId=" + tableId);
400 
401             msg.append(StringPool.CLOSE_CURLY_BRACE);
402 
403             throw new NoSuchColumnException(msg.toString());
404         }
405         else {
406             return list.get(0);
407         }
408     }
409 
410     public ExpandoColumn findByTableId_Last(long tableId, OrderByComparator obc)
411         throws NoSuchColumnException, SystemException {
412         int count = countByTableId(tableId);
413 
414         List<ExpandoColumn> list = findByTableId(tableId, count - 1, count, obc);
415 
416         if (list.size() == 0) {
417             StringBuilder msg = new StringBuilder();
418 
419             msg.append("No ExpandoColumn exists with the key {");
420 
421             msg.append("tableId=" + tableId);
422 
423             msg.append(StringPool.CLOSE_CURLY_BRACE);
424 
425             throw new NoSuchColumnException(msg.toString());
426         }
427         else {
428             return list.get(0);
429         }
430     }
431 
432     public ExpandoColumn[] findByTableId_PrevAndNext(long columnId,
433         long tableId, OrderByComparator obc)
434         throws NoSuchColumnException, SystemException {
435         ExpandoColumn expandoColumn = findByPrimaryKey(columnId);
436 
437         int count = countByTableId(tableId);
438 
439         Session session = null;
440 
441         try {
442             session = openSession();
443 
444             StringBuilder query = new StringBuilder();
445 
446             query.append(
447                 "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
448 
449             query.append("tableId = ?");
450 
451             query.append(" ");
452 
453             if (obc != null) {
454                 query.append("ORDER BY ");
455                 query.append(obc.getOrderBy());
456             }
457 
458             Query q = session.createQuery(query.toString());
459 
460             QueryPos qPos = QueryPos.getInstance(q);
461 
462             qPos.add(tableId);
463 
464             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
465                     expandoColumn);
466 
467             ExpandoColumn[] array = new ExpandoColumnImpl[3];
468 
469             array[0] = (ExpandoColumn)objArray[0];
470             array[1] = (ExpandoColumn)objArray[1];
471             array[2] = (ExpandoColumn)objArray[2];
472 
473             return array;
474         }
475         catch (Exception e) {
476             throw processException(e);
477         }
478         finally {
479             closeSession(session);
480         }
481     }
482 
483     public ExpandoColumn findByT_N(long tableId, String name)
484         throws NoSuchColumnException, SystemException {
485         ExpandoColumn expandoColumn = fetchByT_N(tableId, name);
486 
487         if (expandoColumn == null) {
488             StringBuilder msg = new StringBuilder();
489 
490             msg.append("No ExpandoColumn exists with the key {");
491 
492             msg.append("tableId=" + tableId);
493 
494             msg.append(", ");
495             msg.append("name=" + name);
496 
497             msg.append(StringPool.CLOSE_CURLY_BRACE);
498 
499             if (_log.isWarnEnabled()) {
500                 _log.warn(msg.toString());
501             }
502 
503             throw new NoSuchColumnException(msg.toString());
504         }
505 
506         return expandoColumn;
507     }
508 
509     public ExpandoColumn fetchByT_N(long tableId, String name)
510         throws SystemException {
511         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
512         String finderClassName = ExpandoColumn.class.getName();
513         String finderMethodName = "fetchByT_N";
514         String[] finderParams = new String[] {
515                 Long.class.getName(), String.class.getName()
516             };
517         Object[] finderArgs = new Object[] { new Long(tableId), name };
518 
519         Object result = null;
520 
521         if (finderClassNameCacheEnabled) {
522             result = FinderCacheUtil.getResult(finderClassName,
523                     finderMethodName, finderParams, finderArgs, this);
524         }
525 
526         if (result == null) {
527             Session session = null;
528 
529             try {
530                 session = openSession();
531 
532                 StringBuilder query = new StringBuilder();
533 
534                 query.append(
535                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
536 
537                 query.append("tableId = ?");
538 
539                 query.append(" AND ");
540 
541                 if (name == null) {
542                     query.append("name IS NULL");
543                 }
544                 else {
545                     query.append("name = ?");
546                 }
547 
548                 query.append(" ");
549 
550                 Query q = session.createQuery(query.toString());
551 
552                 QueryPos qPos = QueryPos.getInstance(q);
553 
554                 qPos.add(tableId);
555 
556                 if (name != null) {
557                     qPos.add(name);
558                 }
559 
560                 List<ExpandoColumn> list = q.list();
561 
562                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
563                     finderClassName, finderMethodName, finderParams,
564                     finderArgs, list);
565 
566                 if (list.size() == 0) {
567                     return null;
568                 }
569                 else {
570                     return list.get(0);
571                 }
572             }
573             catch (Exception e) {
574                 throw processException(e);
575             }
576             finally {
577                 closeSession(session);
578             }
579         }
580         else {
581             List<ExpandoColumn> list = (List<ExpandoColumn>)result;
582 
583             if (list.size() == 0) {
584                 return null;
585             }
586             else {
587                 return list.get(0);
588             }
589         }
590     }
591 
592     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
593         throws SystemException {
594         Session session = null;
595 
596         try {
597             session = openSession();
598 
599             dynamicQuery.compile(session);
600 
601             return dynamicQuery.list();
602         }
603         catch (Exception e) {
604             throw processException(e);
605         }
606         finally {
607             closeSession(session);
608         }
609     }
610 
611     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
612         int start, int end) throws SystemException {
613         Session session = null;
614 
615         try {
616             session = openSession();
617 
618             dynamicQuery.setLimit(start, end);
619 
620             dynamicQuery.compile(session);
621 
622             return dynamicQuery.list();
623         }
624         catch (Exception e) {
625             throw processException(e);
626         }
627         finally {
628             closeSession(session);
629         }
630     }
631 
632     public List<ExpandoColumn> findAll() throws SystemException {
633         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
634     }
635 
636     public List<ExpandoColumn> findAll(int start, int end)
637         throws SystemException {
638         return findAll(start, end, null);
639     }
640 
641     public List<ExpandoColumn> findAll(int start, int end, OrderByComparator obc)
642         throws SystemException {
643         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
644         String finderClassName = ExpandoColumn.class.getName();
645         String finderMethodName = "findAll";
646         String[] finderParams = new String[] {
647                 "java.lang.Integer", "java.lang.Integer",
648                 "com.liferay.portal.kernel.util.OrderByComparator"
649             };
650         Object[] finderArgs = new Object[] {
651                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
652             };
653 
654         Object result = null;
655 
656         if (finderClassNameCacheEnabled) {
657             result = FinderCacheUtil.getResult(finderClassName,
658                     finderMethodName, finderParams, finderArgs, this);
659         }
660 
661         if (result == null) {
662             Session session = null;
663 
664             try {
665                 session = openSession();
666 
667                 StringBuilder query = new StringBuilder();
668 
669                 query.append(
670                     "FROM com.liferay.portlet.expando.model.ExpandoColumn ");
671 
672                 if (obc != null) {
673                     query.append("ORDER BY ");
674                     query.append(obc.getOrderBy());
675                 }
676 
677                 Query q = session.createQuery(query.toString());
678 
679                 List<ExpandoColumn> list = null;
680 
681                 if (obc == null) {
682                     list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
683                             start, end, false);
684 
685                     Collections.sort(list);
686                 }
687                 else {
688                     list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
689                             start, end);
690                 }
691 
692                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
693                     finderClassName, finderMethodName, finderParams,
694                     finderArgs, list);
695 
696                 return list;
697             }
698             catch (Exception e) {
699                 throw processException(e);
700             }
701             finally {
702                 closeSession(session);
703             }
704         }
705         else {
706             return (List<ExpandoColumn>)result;
707         }
708     }
709 
710     public void removeByTableId(long tableId) throws SystemException {
711         for (ExpandoColumn expandoColumn : findByTableId(tableId)) {
712             remove(expandoColumn);
713         }
714     }
715 
716     public void removeByT_N(long tableId, String name)
717         throws NoSuchColumnException, SystemException {
718         ExpandoColumn expandoColumn = findByT_N(tableId, name);
719 
720         remove(expandoColumn);
721     }
722 
723     public void removeAll() throws SystemException {
724         for (ExpandoColumn expandoColumn : findAll()) {
725             remove(expandoColumn);
726         }
727     }
728 
729     public int countByTableId(long tableId) throws SystemException {
730         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
731         String finderClassName = ExpandoColumn.class.getName();
732         String finderMethodName = "countByTableId";
733         String[] finderParams = new String[] { Long.class.getName() };
734         Object[] finderArgs = new Object[] { new Long(tableId) };
735 
736         Object result = null;
737 
738         if (finderClassNameCacheEnabled) {
739             result = FinderCacheUtil.getResult(finderClassName,
740                     finderMethodName, finderParams, finderArgs, this);
741         }
742 
743         if (result == null) {
744             Session session = null;
745 
746             try {
747                 session = openSession();
748 
749                 StringBuilder query = new StringBuilder();
750 
751                 query.append("SELECT COUNT(*) ");
752                 query.append(
753                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
754 
755                 query.append("tableId = ?");
756 
757                 query.append(" ");
758 
759                 Query q = session.createQuery(query.toString());
760 
761                 QueryPos qPos = QueryPos.getInstance(q);
762 
763                 qPos.add(tableId);
764 
765                 Long count = null;
766 
767                 Iterator<Long> itr = q.list().iterator();
768 
769                 if (itr.hasNext()) {
770                     count = itr.next();
771                 }
772 
773                 if (count == null) {
774                     count = new Long(0);
775                 }
776 
777                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
778                     finderClassName, finderMethodName, finderParams,
779                     finderArgs, count);
780 
781                 return count.intValue();
782             }
783             catch (Exception e) {
784                 throw processException(e);
785             }
786             finally {
787                 closeSession(session);
788             }
789         }
790         else {
791             return ((Long)result).intValue();
792         }
793     }
794 
795     public int countByT_N(long tableId, String name) throws SystemException {
796         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
797         String finderClassName = ExpandoColumn.class.getName();
798         String finderMethodName = "countByT_N";
799         String[] finderParams = new String[] {
800                 Long.class.getName(), String.class.getName()
801             };
802         Object[] finderArgs = new Object[] { new Long(tableId), name };
803 
804         Object result = null;
805 
806         if (finderClassNameCacheEnabled) {
807             result = FinderCacheUtil.getResult(finderClassName,
808                     finderMethodName, finderParams, finderArgs, this);
809         }
810 
811         if (result == null) {
812             Session session = null;
813 
814             try {
815                 session = openSession();
816 
817                 StringBuilder query = new StringBuilder();
818 
819                 query.append("SELECT COUNT(*) ");
820                 query.append(
821                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
822 
823                 query.append("tableId = ?");
824 
825                 query.append(" AND ");
826 
827                 if (name == null) {
828                     query.append("name IS NULL");
829                 }
830                 else {
831                     query.append("name = ?");
832                 }
833 
834                 query.append(" ");
835 
836                 Query q = session.createQuery(query.toString());
837 
838                 QueryPos qPos = QueryPos.getInstance(q);
839 
840                 qPos.add(tableId);
841 
842                 if (name != null) {
843                     qPos.add(name);
844                 }
845 
846                 Long count = null;
847 
848                 Iterator<Long> itr = q.list().iterator();
849 
850                 if (itr.hasNext()) {
851                     count = itr.next();
852                 }
853 
854                 if (count == null) {
855                     count = new Long(0);
856                 }
857 
858                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
859                     finderClassName, finderMethodName, finderParams,
860                     finderArgs, count);
861 
862                 return count.intValue();
863             }
864             catch (Exception e) {
865                 throw processException(e);
866             }
867             finally {
868                 closeSession(session);
869             }
870         }
871         else {
872             return ((Long)result).intValue();
873         }
874     }
875 
876     public int countAll() throws SystemException {
877         boolean finderClassNameCacheEnabled = ExpandoColumnModelImpl.CACHE_ENABLED;
878         String finderClassName = ExpandoColumn.class.getName();
879         String finderMethodName = "countAll";
880         String[] finderParams = new String[] {  };
881         Object[] finderArgs = new Object[] {  };
882 
883         Object result = null;
884 
885         if (finderClassNameCacheEnabled) {
886             result = FinderCacheUtil.getResult(finderClassName,
887                     finderMethodName, finderParams, finderArgs, this);
888         }
889 
890         if (result == null) {
891             Session session = null;
892 
893             try {
894                 session = openSession();
895 
896                 Query q = session.createQuery(
897                         "SELECT COUNT(*) FROM com.liferay.portlet.expando.model.ExpandoColumn");
898 
899                 Long count = null;
900 
901                 Iterator<Long> itr = q.list().iterator();
902 
903                 if (itr.hasNext()) {
904                     count = itr.next();
905                 }
906 
907                 if (count == null) {
908                     count = new Long(0);
909                 }
910 
911                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
912                     finderClassName, finderMethodName, finderParams,
913                     finderArgs, count);
914 
915                 return count.intValue();
916             }
917             catch (Exception e) {
918                 throw processException(e);
919             }
920             finally {
921                 closeSession(session);
922             }
923         }
924         else {
925             return ((Long)result).intValue();
926         }
927     }
928 
929     public void afterPropertiesSet() {
930         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
931                     com.liferay.portal.util.PropsUtil.get(
932                         "value.object.listener.com.liferay.portlet.expando.model.ExpandoColumn")));
933 
934         if (listenerClassNames.length > 0) {
935             try {
936                 List<ModelListener> listenersList = new ArrayList<ModelListener>();
937 
938                 for (String listenerClassName : listenerClassNames) {
939                     listenersList.add((ModelListener)Class.forName(
940                             listenerClassName).newInstance());
941                 }
942 
943                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
944             }
945             catch (Exception e) {
946                 _log.error(e);
947             }
948         }
949     }
950 
951     private static Log _log = LogFactoryUtil.getLog(ExpandoColumnPersistenceImpl.class);
952 }