1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.expando.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.bean.InitializingBean;
27  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
28  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.QueryPos;
31  import com.liferay.portal.kernel.dao.orm.QueryUtil;
32  import com.liferay.portal.kernel.dao.orm.Session;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.ListUtil;
35  import com.liferay.portal.kernel.util.OrderByComparator;
36  import com.liferay.portal.kernel.util.StringPool;
37  import com.liferay.portal.kernel.util.StringUtil;
38  import com.liferay.portal.model.ModelListener;
39  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
40  
41  import com.liferay.portlet.expando.NoSuchTableException;
42  import com.liferay.portlet.expando.model.ExpandoTable;
43  import com.liferay.portlet.expando.model.impl.ExpandoTableImpl;
44  import com.liferay.portlet.expando.model.impl.ExpandoTableModelImpl;
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="ExpandoTablePersistenceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class ExpandoTablePersistenceImpl extends BasePersistenceImpl
61      implements ExpandoTablePersistence, InitializingBean {
62      public ExpandoTable create(long tableId) {
63          ExpandoTable expandoTable = new ExpandoTableImpl();
64  
65          expandoTable.setNew(true);
66          expandoTable.setPrimaryKey(tableId);
67  
68          return expandoTable;
69      }
70  
71      public ExpandoTable remove(long tableId)
72          throws NoSuchTableException, SystemException {
73          Session session = null;
74  
75          try {
76              session = openSession();
77  
78              ExpandoTable expandoTable = (ExpandoTable)session.get(ExpandoTableImpl.class,
79                      new Long(tableId));
80  
81              if (expandoTable == null) {
82                  if (_log.isWarnEnabled()) {
83                      _log.warn("No ExpandoTable exists with the primary key " +
84                          tableId);
85                  }
86  
87                  throw new NoSuchTableException(
88                      "No ExpandoTable exists with the primary key " + tableId);
89              }
90  
91              return remove(expandoTable);
92          }
93          catch (NoSuchTableException nsee) {
94              throw nsee;
95          }
96          catch (Exception e) {
97              throw processException(e);
98          }
99          finally {
100             closeSession(session);
101         }
102     }
103 
104     public ExpandoTable remove(ExpandoTable expandoTable)
105         throws SystemException {
106         if (_listeners.length > 0) {
107             for (ModelListener listener : _listeners) {
108                 listener.onBeforeRemove(expandoTable);
109             }
110         }
111 
112         expandoTable = removeImpl(expandoTable);
113 
114         if (_listeners.length > 0) {
115             for (ModelListener listener : _listeners) {
116                 listener.onAfterRemove(expandoTable);
117             }
118         }
119 
120         return expandoTable;
121     }
122 
123     protected ExpandoTable removeImpl(ExpandoTable expandoTable)
124         throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             session.delete(expandoTable);
131 
132             session.flush();
133 
134             return expandoTable;
135         }
136         catch (Exception e) {
137             throw processException(e);
138         }
139         finally {
140             closeSession(session);
141 
142             FinderCacheUtil.clearCache(ExpandoTable.class.getName());
143         }
144     }
145 
146     /**
147      * @deprecated Use <code>update(ExpandoTable expandoTable, boolean merge)</code>.
148      */
149     public ExpandoTable update(ExpandoTable expandoTable)
150         throws SystemException {
151         if (_log.isWarnEnabled()) {
152             _log.warn(
153                 "Using the deprecated update(ExpandoTable expandoTable) method. Use update(ExpandoTable expandoTable, boolean merge) instead.");
154         }
155 
156         return update(expandoTable, 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        expandoTable 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 expandoTable 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 ExpandoTable update(ExpandoTable expandoTable, boolean merge)
173         throws SystemException {
174         boolean isNew = expandoTable.isNew();
175 
176         if (_listeners.length > 0) {
177             for (ModelListener listener : _listeners) {
178                 if (isNew) {
179                     listener.onBeforeCreate(expandoTable);
180                 }
181                 else {
182                     listener.onBeforeUpdate(expandoTable);
183                 }
184             }
185         }
186 
187         expandoTable = updateImpl(expandoTable, merge);
188 
189         if (_listeners.length > 0) {
190             for (ModelListener listener : _listeners) {
191                 if (isNew) {
192                     listener.onAfterCreate(expandoTable);
193                 }
194                 else {
195                     listener.onAfterUpdate(expandoTable);
196                 }
197             }
198         }
199 
200         return expandoTable;
201     }
202 
203     public ExpandoTable updateImpl(
204         com.liferay.portlet.expando.model.ExpandoTable expandoTable,
205         boolean merge) throws SystemException {
206         Session session = null;
207 
208         try {
209             session = openSession();
210 
211             if (merge) {
212                 session.merge(expandoTable);
213             }
214             else {
215                 if (expandoTable.isNew()) {
216                     session.save(expandoTable);
217                 }
218             }
219 
220             session.flush();
221 
222             expandoTable.setNew(false);
223 
224             return expandoTable;
225         }
226         catch (Exception e) {
227             throw processException(e);
228         }
229         finally {
230             closeSession(session);
231 
232             FinderCacheUtil.clearCache(ExpandoTable.class.getName());
233         }
234     }
235 
236     public ExpandoTable findByPrimaryKey(long tableId)
237         throws NoSuchTableException, SystemException {
238         ExpandoTable expandoTable = fetchByPrimaryKey(tableId);
239 
240         if (expandoTable == null) {
241             if (_log.isWarnEnabled()) {
242                 _log.warn("No ExpandoTable exists with the primary key " +
243                     tableId);
244             }
245 
246             throw new NoSuchTableException(
247                 "No ExpandoTable exists with the primary key " + tableId);
248         }
249 
250         return expandoTable;
251     }
252 
253     public ExpandoTable fetchByPrimaryKey(long tableId)
254         throws SystemException {
255         Session session = null;
256 
257         try {
258             session = openSession();
259 
260             return (ExpandoTable)session.get(ExpandoTableImpl.class,
261                 new Long(tableId));
262         }
263         catch (Exception e) {
264             throw processException(e);
265         }
266         finally {
267             closeSession(session);
268         }
269     }
270 
271     public List<ExpandoTable> findByClassNameId(long classNameId)
272         throws SystemException {
273         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
274         String finderClassName = ExpandoTable.class.getName();
275         String finderMethodName = "findByClassNameId";
276         String[] finderParams = new String[] { Long.class.getName() };
277         Object[] finderArgs = new Object[] { new Long(classNameId) };
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.ExpandoTable WHERE ");
296 
297                 query.append("classNameId = ?");
298 
299                 query.append(" ");
300 
301                 Query q = session.createQuery(query.toString());
302 
303                 QueryPos qPos = QueryPos.getInstance(q);
304 
305                 qPos.add(classNameId);
306 
307                 List<ExpandoTable> 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<ExpandoTable>)result;
324         }
325     }
326 
327     public List<ExpandoTable> findByClassNameId(long classNameId, int start,
328         int end) throws SystemException {
329         return findByClassNameId(classNameId, start, end, null);
330     }
331 
332     public List<ExpandoTable> findByClassNameId(long classNameId, int start,
333         int end, OrderByComparator obc) throws SystemException {
334         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
335         String finderClassName = ExpandoTable.class.getName();
336         String finderMethodName = "findByClassNameId";
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(classNameId),
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.ExpandoTable WHERE ");
366 
367                 query.append("classNameId = ?");
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(classNameId);
381 
382                 List<ExpandoTable> list = (List<ExpandoTable>)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<ExpandoTable>)result;
400         }
401     }
402 
403     public ExpandoTable findByClassNameId_First(long classNameId,
404         OrderByComparator obc) throws NoSuchTableException, SystemException {
405         List<ExpandoTable> list = findByClassNameId(classNameId, 0, 1, obc);
406 
407         if (list.size() == 0) {
408             StringBuilder msg = new StringBuilder();
409 
410             msg.append("No ExpandoTable exists with the key {");
411 
412             msg.append("classNameId=" + classNameId);
413 
414             msg.append(StringPool.CLOSE_CURLY_BRACE);
415 
416             throw new NoSuchTableException(msg.toString());
417         }
418         else {
419             return list.get(0);
420         }
421     }
422 
423     public ExpandoTable findByClassNameId_Last(long classNameId,
424         OrderByComparator obc) throws NoSuchTableException, SystemException {
425         int count = countByClassNameId(classNameId);
426 
427         List<ExpandoTable> list = findByClassNameId(classNameId, count - 1,
428                 count, obc);
429 
430         if (list.size() == 0) {
431             StringBuilder msg = new StringBuilder();
432 
433             msg.append("No ExpandoTable exists with the key {");
434 
435             msg.append("classNameId=" + classNameId);
436 
437             msg.append(StringPool.CLOSE_CURLY_BRACE);
438 
439             throw new NoSuchTableException(msg.toString());
440         }
441         else {
442             return list.get(0);
443         }
444     }
445 
446     public ExpandoTable[] findByClassNameId_PrevAndNext(long tableId,
447         long classNameId, OrderByComparator obc)
448         throws NoSuchTableException, SystemException {
449         ExpandoTable expandoTable = findByPrimaryKey(tableId);
450 
451         int count = countByClassNameId(classNameId);
452 
453         Session session = null;
454 
455         try {
456             session = openSession();
457 
458             StringBuilder query = new StringBuilder();
459 
460             query.append(
461                 "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
462 
463             query.append("classNameId = ?");
464 
465             query.append(" ");
466 
467             if (obc != null) {
468                 query.append("ORDER BY ");
469                 query.append(obc.getOrderBy());
470             }
471 
472             Query q = session.createQuery(query.toString());
473 
474             QueryPos qPos = QueryPos.getInstance(q);
475 
476             qPos.add(classNameId);
477 
478             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
479                     expandoTable);
480 
481             ExpandoTable[] array = new ExpandoTableImpl[3];
482 
483             array[0] = (ExpandoTable)objArray[0];
484             array[1] = (ExpandoTable)objArray[1];
485             array[2] = (ExpandoTable)objArray[2];
486 
487             return array;
488         }
489         catch (Exception e) {
490             throw processException(e);
491         }
492         finally {
493             closeSession(session);
494         }
495     }
496 
497     public ExpandoTable findByC_N(long classNameId, String name)
498         throws NoSuchTableException, SystemException {
499         ExpandoTable expandoTable = fetchByC_N(classNameId, name);
500 
501         if (expandoTable == null) {
502             StringBuilder msg = new StringBuilder();
503 
504             msg.append("No ExpandoTable exists with the key {");
505 
506             msg.append("classNameId=" + classNameId);
507 
508             msg.append(", ");
509             msg.append("name=" + name);
510 
511             msg.append(StringPool.CLOSE_CURLY_BRACE);
512 
513             if (_log.isWarnEnabled()) {
514                 _log.warn(msg.toString());
515             }
516 
517             throw new NoSuchTableException(msg.toString());
518         }
519 
520         return expandoTable;
521     }
522 
523     public ExpandoTable fetchByC_N(long classNameId, String name)
524         throws SystemException {
525         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
526         String finderClassName = ExpandoTable.class.getName();
527         String finderMethodName = "fetchByC_N";
528         String[] finderParams = new String[] {
529                 Long.class.getName(), String.class.getName()
530             };
531         Object[] finderArgs = new Object[] { new Long(classNameId), name };
532 
533         Object result = null;
534 
535         if (finderClassNameCacheEnabled) {
536             result = FinderCacheUtil.getResult(finderClassName,
537                     finderMethodName, finderParams, finderArgs, this);
538         }
539 
540         if (result == null) {
541             Session session = null;
542 
543             try {
544                 session = openSession();
545 
546                 StringBuilder query = new StringBuilder();
547 
548                 query.append(
549                     "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
550 
551                 query.append("classNameId = ?");
552 
553                 query.append(" AND ");
554 
555                 if (name == null) {
556                     query.append("name IS NULL");
557                 }
558                 else {
559                     query.append("name = ?");
560                 }
561 
562                 query.append(" ");
563 
564                 Query q = session.createQuery(query.toString());
565 
566                 QueryPos qPos = QueryPos.getInstance(q);
567 
568                 qPos.add(classNameId);
569 
570                 if (name != null) {
571                     qPos.add(name);
572                 }
573 
574                 List<ExpandoTable> list = q.list();
575 
576                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
577                     finderClassName, finderMethodName, finderParams,
578                     finderArgs, list);
579 
580                 if (list.size() == 0) {
581                     return null;
582                 }
583                 else {
584                     return list.get(0);
585                 }
586             }
587             catch (Exception e) {
588                 throw processException(e);
589             }
590             finally {
591                 closeSession(session);
592             }
593         }
594         else {
595             List<ExpandoTable> list = (List<ExpandoTable>)result;
596 
597             if (list.size() == 0) {
598                 return null;
599             }
600             else {
601                 return list.get(0);
602             }
603         }
604     }
605 
606     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
607         throws SystemException {
608         Session session = null;
609 
610         try {
611             session = openSession();
612 
613             dynamicQuery.compile(session);
614 
615             return dynamicQuery.list();
616         }
617         catch (Exception e) {
618             throw processException(e);
619         }
620         finally {
621             closeSession(session);
622         }
623     }
624 
625     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
626         int start, int end) throws SystemException {
627         Session session = null;
628 
629         try {
630             session = openSession();
631 
632             dynamicQuery.setLimit(start, end);
633 
634             dynamicQuery.compile(session);
635 
636             return dynamicQuery.list();
637         }
638         catch (Exception e) {
639             throw processException(e);
640         }
641         finally {
642             closeSession(session);
643         }
644     }
645 
646     public List<ExpandoTable> findAll() throws SystemException {
647         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
648     }
649 
650     public List<ExpandoTable> findAll(int start, int end)
651         throws SystemException {
652         return findAll(start, end, null);
653     }
654 
655     public List<ExpandoTable> findAll(int start, int end, OrderByComparator obc)
656         throws SystemException {
657         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
658         String finderClassName = ExpandoTable.class.getName();
659         String finderMethodName = "findAll";
660         String[] finderParams = new String[] {
661                 "java.lang.Integer", "java.lang.Integer",
662                 "com.liferay.portal.kernel.util.OrderByComparator"
663             };
664         Object[] finderArgs = new Object[] {
665                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
666             };
667 
668         Object result = null;
669 
670         if (finderClassNameCacheEnabled) {
671             result = FinderCacheUtil.getResult(finderClassName,
672                     finderMethodName, finderParams, finderArgs, this);
673         }
674 
675         if (result == null) {
676             Session session = null;
677 
678             try {
679                 session = openSession();
680 
681                 StringBuilder query = new StringBuilder();
682 
683                 query.append(
684                     "FROM com.liferay.portlet.expando.model.ExpandoTable ");
685 
686                 if (obc != null) {
687                     query.append("ORDER BY ");
688                     query.append(obc.getOrderBy());
689                 }
690 
691                 Query q = session.createQuery(query.toString());
692 
693                 List<ExpandoTable> list = (List<ExpandoTable>)QueryUtil.list(q,
694                         getDialect(), start, end);
695 
696                 if (obc == null) {
697                     Collections.sort(list);
698                 }
699 
700                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
701                     finderClassName, finderMethodName, finderParams,
702                     finderArgs, list);
703 
704                 return list;
705             }
706             catch (Exception e) {
707                 throw processException(e);
708             }
709             finally {
710                 closeSession(session);
711             }
712         }
713         else {
714             return (List<ExpandoTable>)result;
715         }
716     }
717 
718     public void removeByClassNameId(long classNameId) throws SystemException {
719         for (ExpandoTable expandoTable : findByClassNameId(classNameId)) {
720             remove(expandoTable);
721         }
722     }
723 
724     public void removeByC_N(long classNameId, String name)
725         throws NoSuchTableException, SystemException {
726         ExpandoTable expandoTable = findByC_N(classNameId, name);
727 
728         remove(expandoTable);
729     }
730 
731     public void removeAll() throws SystemException {
732         for (ExpandoTable expandoTable : findAll()) {
733             remove(expandoTable);
734         }
735     }
736 
737     public int countByClassNameId(long classNameId) throws SystemException {
738         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
739         String finderClassName = ExpandoTable.class.getName();
740         String finderMethodName = "countByClassNameId";
741         String[] finderParams = new String[] { Long.class.getName() };
742         Object[] finderArgs = new Object[] { new Long(classNameId) };
743 
744         Object result = null;
745 
746         if (finderClassNameCacheEnabled) {
747             result = FinderCacheUtil.getResult(finderClassName,
748                     finderMethodName, finderParams, finderArgs, this);
749         }
750 
751         if (result == null) {
752             Session session = null;
753 
754             try {
755                 session = openSession();
756 
757                 StringBuilder query = new StringBuilder();
758 
759                 query.append("SELECT COUNT(*) ");
760                 query.append(
761                     "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
762 
763                 query.append("classNameId = ?");
764 
765                 query.append(" ");
766 
767                 Query q = session.createQuery(query.toString());
768 
769                 QueryPos qPos = QueryPos.getInstance(q);
770 
771                 qPos.add(classNameId);
772 
773                 Long count = null;
774 
775                 Iterator<Long> itr = q.list().iterator();
776 
777                 if (itr.hasNext()) {
778                     count = itr.next();
779                 }
780 
781                 if (count == null) {
782                     count = new Long(0);
783                 }
784 
785                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
786                     finderClassName, finderMethodName, finderParams,
787                     finderArgs, count);
788 
789                 return count.intValue();
790             }
791             catch (Exception e) {
792                 throw processException(e);
793             }
794             finally {
795                 closeSession(session);
796             }
797         }
798         else {
799             return ((Long)result).intValue();
800         }
801     }
802 
803     public int countByC_N(long classNameId, String name)
804         throws SystemException {
805         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
806         String finderClassName = ExpandoTable.class.getName();
807         String finderMethodName = "countByC_N";
808         String[] finderParams = new String[] {
809                 Long.class.getName(), String.class.getName()
810             };
811         Object[] finderArgs = new Object[] { new Long(classNameId), name };
812 
813         Object result = null;
814 
815         if (finderClassNameCacheEnabled) {
816             result = FinderCacheUtil.getResult(finderClassName,
817                     finderMethodName, finderParams, finderArgs, this);
818         }
819 
820         if (result == null) {
821             Session session = null;
822 
823             try {
824                 session = openSession();
825 
826                 StringBuilder query = new StringBuilder();
827 
828                 query.append("SELECT COUNT(*) ");
829                 query.append(
830                     "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
831 
832                 query.append("classNameId = ?");
833 
834                 query.append(" AND ");
835 
836                 if (name == null) {
837                     query.append("name IS NULL");
838                 }
839                 else {
840                     query.append("name = ?");
841                 }
842 
843                 query.append(" ");
844 
845                 Query q = session.createQuery(query.toString());
846 
847                 QueryPos qPos = QueryPos.getInstance(q);
848 
849                 qPos.add(classNameId);
850 
851                 if (name != null) {
852                     qPos.add(name);
853                 }
854 
855                 Long count = null;
856 
857                 Iterator<Long> itr = q.list().iterator();
858 
859                 if (itr.hasNext()) {
860                     count = itr.next();
861                 }
862 
863                 if (count == null) {
864                     count = new Long(0);
865                 }
866 
867                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
868                     finderClassName, finderMethodName, finderParams,
869                     finderArgs, count);
870 
871                 return count.intValue();
872             }
873             catch (Exception e) {
874                 throw processException(e);
875             }
876             finally {
877                 closeSession(session);
878             }
879         }
880         else {
881             return ((Long)result).intValue();
882         }
883     }
884 
885     public int countAll() throws SystemException {
886         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
887         String finderClassName = ExpandoTable.class.getName();
888         String finderMethodName = "countAll";
889         String[] finderParams = new String[] {  };
890         Object[] finderArgs = new Object[] {  };
891 
892         Object result = null;
893 
894         if (finderClassNameCacheEnabled) {
895             result = FinderCacheUtil.getResult(finderClassName,
896                     finderMethodName, finderParams, finderArgs, this);
897         }
898 
899         if (result == null) {
900             Session session = null;
901 
902             try {
903                 session = openSession();
904 
905                 Query q = session.createQuery(
906                         "SELECT COUNT(*) FROM com.liferay.portlet.expando.model.ExpandoTable");
907 
908                 Long count = null;
909 
910                 Iterator<Long> itr = q.list().iterator();
911 
912                 if (itr.hasNext()) {
913                     count = itr.next();
914                 }
915 
916                 if (count == null) {
917                     count = new Long(0);
918                 }
919 
920                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
921                     finderClassName, finderMethodName, finderParams,
922                     finderArgs, count);
923 
924                 return count.intValue();
925             }
926             catch (Exception e) {
927                 throw processException(e);
928             }
929             finally {
930                 closeSession(session);
931             }
932         }
933         else {
934             return ((Long)result).intValue();
935         }
936     }
937 
938     public void registerListener(ModelListener listener) {
939         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
940 
941         listeners.add(listener);
942 
943         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
944     }
945 
946     public void unregisterListener(ModelListener listener) {
947         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
948 
949         listeners.remove(listener);
950 
951         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
952     }
953 
954     public void afterPropertiesSet() {
955         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
956                     com.liferay.portal.util.PropsUtil.get(
957                         "value.object.listener.com.liferay.portlet.expando.model.ExpandoTable")));
958 
959         if (listenerClassNames.length > 0) {
960             try {
961                 List<ModelListener> listeners = new ArrayList<ModelListener>();
962 
963                 for (String listenerClassName : listenerClassNames) {
964                     listeners.add((ModelListener)Class.forName(
965                             listenerClassName).newInstance());
966                 }
967 
968                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
969             }
970             catch (Exception e) {
971                 _log.error(e);
972             }
973         }
974     }
975 
976     private static Log _log = LogFactory.getLog(ExpandoTablePersistenceImpl.class);
977     private ModelListener[] _listeners = new ModelListener[0];
978 }