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