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.NoSuchTableException;
40  import com.liferay.portlet.expando.model.ExpandoTable;
41  import com.liferay.portlet.expando.model.impl.ExpandoTableImpl;
42  import com.liferay.portlet.expando.model.impl.ExpandoTableModelImpl;
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="ExpandoTablePersistenceImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class ExpandoTablePersistenceImpl extends BasePersistenceImpl
56      implements ExpandoTablePersistence {
57      public ExpandoTable create(long tableId) {
58          ExpandoTable expandoTable = new ExpandoTableImpl();
59  
60          expandoTable.setNew(true);
61          expandoTable.setPrimaryKey(tableId);
62  
63          return expandoTable;
64      }
65  
66      public ExpandoTable remove(long tableId)
67          throws NoSuchTableException, SystemException {
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              ExpandoTable expandoTable = (ExpandoTable)session.get(ExpandoTableImpl.class,
74                      new Long(tableId));
75  
76              if (expandoTable == null) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn("No ExpandoTable exists with the primary key " +
79                          tableId);
80                  }
81  
82                  throw new NoSuchTableException(
83                      "No ExpandoTable exists with the primary key " + tableId);
84              }
85  
86              return remove(expandoTable);
87          }
88          catch (NoSuchTableException nsee) {
89              throw nsee;
90          }
91          catch (Exception e) {
92              throw processException(e);
93          }
94          finally {
95              closeSession(session);
96          }
97      }
98  
99      public ExpandoTable remove(ExpandoTable expandoTable)
100         throws SystemException {
101         for (ModelListener listener : listeners) {
102             listener.onBeforeRemove(expandoTable);
103         }
104 
105         expandoTable = removeImpl(expandoTable);
106 
107         for (ModelListener listener : listeners) {
108             listener.onAfterRemove(expandoTable);
109         }
110 
111         return expandoTable;
112     }
113 
114     protected ExpandoTable removeImpl(ExpandoTable expandoTable)
115         throws SystemException {
116         Session session = null;
117 
118         try {
119             session = openSession();
120 
121             if (BatchSessionUtil.isEnabled()) {
122                 Object staleObject = session.get(ExpandoTableImpl.class,
123                         expandoTable.getPrimaryKeyObj());
124 
125                 if (staleObject != null) {
126                     session.evict(staleObject);
127                 }
128             }
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         for (ModelListener listener : listeners) {
177             if (isNew) {
178                 listener.onBeforeCreate(expandoTable);
179             }
180             else {
181                 listener.onBeforeUpdate(expandoTable);
182             }
183         }
184 
185         expandoTable = updateImpl(expandoTable, merge);
186 
187         for (ModelListener listener : listeners) {
188             if (isNew) {
189                 listener.onAfterCreate(expandoTable);
190             }
191             else {
192                 listener.onAfterUpdate(expandoTable);
193             }
194         }
195 
196         return expandoTable;
197     }
198 
199     public ExpandoTable updateImpl(
200         com.liferay.portlet.expando.model.ExpandoTable expandoTable,
201         boolean merge) throws SystemException {
202         Session session = null;
203 
204         try {
205             session = openSession();
206 
207             BatchSessionUtil.update(session, expandoTable, merge);
208 
209             expandoTable.setNew(false);
210 
211             return expandoTable;
212         }
213         catch (Exception e) {
214             throw processException(e);
215         }
216         finally {
217             closeSession(session);
218 
219             FinderCacheUtil.clearCache(ExpandoTable.class.getName());
220         }
221     }
222 
223     public ExpandoTable findByPrimaryKey(long tableId)
224         throws NoSuchTableException, SystemException {
225         ExpandoTable expandoTable = fetchByPrimaryKey(tableId);
226 
227         if (expandoTable == null) {
228             if (_log.isWarnEnabled()) {
229                 _log.warn("No ExpandoTable exists with the primary key " +
230                     tableId);
231             }
232 
233             throw new NoSuchTableException(
234                 "No ExpandoTable exists with the primary key " + tableId);
235         }
236 
237         return expandoTable;
238     }
239 
240     public ExpandoTable fetchByPrimaryKey(long tableId)
241         throws SystemException {
242         Session session = null;
243 
244         try {
245             session = openSession();
246 
247             return (ExpandoTable)session.get(ExpandoTableImpl.class,
248                 new Long(tableId));
249         }
250         catch (Exception e) {
251             throw processException(e);
252         }
253         finally {
254             closeSession(session);
255         }
256     }
257 
258     public List<ExpandoTable> findByClassNameId(long classNameId)
259         throws SystemException {
260         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
261         String finderClassName = ExpandoTable.class.getName();
262         String finderMethodName = "findByClassNameId";
263         String[] finderParams = new String[] { Long.class.getName() };
264         Object[] finderArgs = new Object[] { new Long(classNameId) };
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.ExpandoTable WHERE ");
283 
284                 query.append("classNameId = ?");
285 
286                 query.append(" ");
287 
288                 Query q = session.createQuery(query.toString());
289 
290                 QueryPos qPos = QueryPos.getInstance(q);
291 
292                 qPos.add(classNameId);
293 
294                 List<ExpandoTable> 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<ExpandoTable>)result;
311         }
312     }
313 
314     public List<ExpandoTable> findByClassNameId(long classNameId, int start,
315         int end) throws SystemException {
316         return findByClassNameId(classNameId, start, end, null);
317     }
318 
319     public List<ExpandoTable> findByClassNameId(long classNameId, int start,
320         int end, OrderByComparator obc) throws SystemException {
321         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
322         String finderClassName = ExpandoTable.class.getName();
323         String finderMethodName = "findByClassNameId";
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(classNameId),
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.ExpandoTable WHERE ");
353 
354                 query.append("classNameId = ?");
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(classNameId);
368 
369                 List<ExpandoTable> list = (List<ExpandoTable>)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<ExpandoTable>)result;
387         }
388     }
389 
390     public ExpandoTable findByClassNameId_First(long classNameId,
391         OrderByComparator obc) throws NoSuchTableException, SystemException {
392         List<ExpandoTable> list = findByClassNameId(classNameId, 0, 1, obc);
393 
394         if (list.size() == 0) {
395             StringBuilder msg = new StringBuilder();
396 
397             msg.append("No ExpandoTable exists with the key {");
398 
399             msg.append("classNameId=" + classNameId);
400 
401             msg.append(StringPool.CLOSE_CURLY_BRACE);
402 
403             throw new NoSuchTableException(msg.toString());
404         }
405         else {
406             return list.get(0);
407         }
408     }
409 
410     public ExpandoTable findByClassNameId_Last(long classNameId,
411         OrderByComparator obc) throws NoSuchTableException, SystemException {
412         int count = countByClassNameId(classNameId);
413 
414         List<ExpandoTable> list = findByClassNameId(classNameId, count - 1,
415                 count, obc);
416 
417         if (list.size() == 0) {
418             StringBuilder msg = new StringBuilder();
419 
420             msg.append("No ExpandoTable exists with the key {");
421 
422             msg.append("classNameId=" + classNameId);
423 
424             msg.append(StringPool.CLOSE_CURLY_BRACE);
425 
426             throw new NoSuchTableException(msg.toString());
427         }
428         else {
429             return list.get(0);
430         }
431     }
432 
433     public ExpandoTable[] findByClassNameId_PrevAndNext(long tableId,
434         long classNameId, OrderByComparator obc)
435         throws NoSuchTableException, SystemException {
436         ExpandoTable expandoTable = findByPrimaryKey(tableId);
437 
438         int count = countByClassNameId(classNameId);
439 
440         Session session = null;
441 
442         try {
443             session = openSession();
444 
445             StringBuilder query = new StringBuilder();
446 
447             query.append(
448                 "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
449 
450             query.append("classNameId = ?");
451 
452             query.append(" ");
453 
454             if (obc != null) {
455                 query.append("ORDER BY ");
456                 query.append(obc.getOrderBy());
457             }
458 
459             Query q = session.createQuery(query.toString());
460 
461             QueryPos qPos = QueryPos.getInstance(q);
462 
463             qPos.add(classNameId);
464 
465             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
466                     expandoTable);
467 
468             ExpandoTable[] array = new ExpandoTableImpl[3];
469 
470             array[0] = (ExpandoTable)objArray[0];
471             array[1] = (ExpandoTable)objArray[1];
472             array[2] = (ExpandoTable)objArray[2];
473 
474             return array;
475         }
476         catch (Exception e) {
477             throw processException(e);
478         }
479         finally {
480             closeSession(session);
481         }
482     }
483 
484     public ExpandoTable findByC_N(long classNameId, String name)
485         throws NoSuchTableException, SystemException {
486         ExpandoTable expandoTable = fetchByC_N(classNameId, name);
487 
488         if (expandoTable == null) {
489             StringBuilder msg = new StringBuilder();
490 
491             msg.append("No ExpandoTable exists with the key {");
492 
493             msg.append("classNameId=" + classNameId);
494 
495             msg.append(", ");
496             msg.append("name=" + name);
497 
498             msg.append(StringPool.CLOSE_CURLY_BRACE);
499 
500             if (_log.isWarnEnabled()) {
501                 _log.warn(msg.toString());
502             }
503 
504             throw new NoSuchTableException(msg.toString());
505         }
506 
507         return expandoTable;
508     }
509 
510     public ExpandoTable fetchByC_N(long classNameId, String name)
511         throws SystemException {
512         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
513         String finderClassName = ExpandoTable.class.getName();
514         String finderMethodName = "fetchByC_N";
515         String[] finderParams = new String[] {
516                 Long.class.getName(), String.class.getName()
517             };
518         Object[] finderArgs = new Object[] { new Long(classNameId), name };
519 
520         Object result = null;
521 
522         if (finderClassNameCacheEnabled) {
523             result = FinderCacheUtil.getResult(finderClassName,
524                     finderMethodName, finderParams, finderArgs, this);
525         }
526 
527         if (result == null) {
528             Session session = null;
529 
530             try {
531                 session = openSession();
532 
533                 StringBuilder query = new StringBuilder();
534 
535                 query.append(
536                     "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
537 
538                 query.append("classNameId = ?");
539 
540                 query.append(" AND ");
541 
542                 if (name == null) {
543                     query.append("name IS NULL");
544                 }
545                 else {
546                     query.append("name = ?");
547                 }
548 
549                 query.append(" ");
550 
551                 Query q = session.createQuery(query.toString());
552 
553                 QueryPos qPos = QueryPos.getInstance(q);
554 
555                 qPos.add(classNameId);
556 
557                 if (name != null) {
558                     qPos.add(name);
559                 }
560 
561                 List<ExpandoTable> list = q.list();
562 
563                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
564                     finderClassName, finderMethodName, finderParams,
565                     finderArgs, list);
566 
567                 if (list.size() == 0) {
568                     return null;
569                 }
570                 else {
571                     return list.get(0);
572                 }
573             }
574             catch (Exception e) {
575                 throw processException(e);
576             }
577             finally {
578                 closeSession(session);
579             }
580         }
581         else {
582             List<ExpandoTable> list = (List<ExpandoTable>)result;
583 
584             if (list.size() == 0) {
585                 return null;
586             }
587             else {
588                 return list.get(0);
589             }
590         }
591     }
592 
593     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
594         throws SystemException {
595         Session session = null;
596 
597         try {
598             session = openSession();
599 
600             dynamicQuery.compile(session);
601 
602             return dynamicQuery.list();
603         }
604         catch (Exception e) {
605             throw processException(e);
606         }
607         finally {
608             closeSession(session);
609         }
610     }
611 
612     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
613         int start, int end) throws SystemException {
614         Session session = null;
615 
616         try {
617             session = openSession();
618 
619             dynamicQuery.setLimit(start, end);
620 
621             dynamicQuery.compile(session);
622 
623             return dynamicQuery.list();
624         }
625         catch (Exception e) {
626             throw processException(e);
627         }
628         finally {
629             closeSession(session);
630         }
631     }
632 
633     public List<ExpandoTable> findAll() throws SystemException {
634         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
635     }
636 
637     public List<ExpandoTable> findAll(int start, int end)
638         throws SystemException {
639         return findAll(start, end, null);
640     }
641 
642     public List<ExpandoTable> findAll(int start, int end, OrderByComparator obc)
643         throws SystemException {
644         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
645         String finderClassName = ExpandoTable.class.getName();
646         String finderMethodName = "findAll";
647         String[] finderParams = new String[] {
648                 "java.lang.Integer", "java.lang.Integer",
649                 "com.liferay.portal.kernel.util.OrderByComparator"
650             };
651         Object[] finderArgs = new Object[] {
652                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
653             };
654 
655         Object result = null;
656 
657         if (finderClassNameCacheEnabled) {
658             result = FinderCacheUtil.getResult(finderClassName,
659                     finderMethodName, finderParams, finderArgs, this);
660         }
661 
662         if (result == null) {
663             Session session = null;
664 
665             try {
666                 session = openSession();
667 
668                 StringBuilder query = new StringBuilder();
669 
670                 query.append(
671                     "FROM com.liferay.portlet.expando.model.ExpandoTable ");
672 
673                 if (obc != null) {
674                     query.append("ORDER BY ");
675                     query.append(obc.getOrderBy());
676                 }
677 
678                 Query q = session.createQuery(query.toString());
679 
680                 List<ExpandoTable> list = null;
681 
682                 if (obc == null) {
683                     list = (List<ExpandoTable>)QueryUtil.list(q, getDialect(),
684                             start, end, false);
685 
686                     Collections.sort(list);
687                 }
688                 else {
689                     list = (List<ExpandoTable>)QueryUtil.list(q, getDialect(),
690                             start, end);
691                 }
692 
693                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
694                     finderClassName, finderMethodName, finderParams,
695                     finderArgs, list);
696 
697                 return list;
698             }
699             catch (Exception e) {
700                 throw processException(e);
701             }
702             finally {
703                 closeSession(session);
704             }
705         }
706         else {
707             return (List<ExpandoTable>)result;
708         }
709     }
710 
711     public void removeByClassNameId(long classNameId) throws SystemException {
712         for (ExpandoTable expandoTable : findByClassNameId(classNameId)) {
713             remove(expandoTable);
714         }
715     }
716 
717     public void removeByC_N(long classNameId, String name)
718         throws NoSuchTableException, SystemException {
719         ExpandoTable expandoTable = findByC_N(classNameId, name);
720 
721         remove(expandoTable);
722     }
723 
724     public void removeAll() throws SystemException {
725         for (ExpandoTable expandoTable : findAll()) {
726             remove(expandoTable);
727         }
728     }
729 
730     public int countByClassNameId(long classNameId) throws SystemException {
731         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
732         String finderClassName = ExpandoTable.class.getName();
733         String finderMethodName = "countByClassNameId";
734         String[] finderParams = new String[] { Long.class.getName() };
735         Object[] finderArgs = new Object[] { new Long(classNameId) };
736 
737         Object result = null;
738 
739         if (finderClassNameCacheEnabled) {
740             result = FinderCacheUtil.getResult(finderClassName,
741                     finderMethodName, finderParams, finderArgs, this);
742         }
743 
744         if (result == null) {
745             Session session = null;
746 
747             try {
748                 session = openSession();
749 
750                 StringBuilder query = new StringBuilder();
751 
752                 query.append("SELECT COUNT(*) ");
753                 query.append(
754                     "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
755 
756                 query.append("classNameId = ?");
757 
758                 query.append(" ");
759 
760                 Query q = session.createQuery(query.toString());
761 
762                 QueryPos qPos = QueryPos.getInstance(q);
763 
764                 qPos.add(classNameId);
765 
766                 Long count = null;
767 
768                 Iterator<Long> itr = q.list().iterator();
769 
770                 if (itr.hasNext()) {
771                     count = itr.next();
772                 }
773 
774                 if (count == null) {
775                     count = new Long(0);
776                 }
777 
778                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
779                     finderClassName, finderMethodName, finderParams,
780                     finderArgs, count);
781 
782                 return count.intValue();
783             }
784             catch (Exception e) {
785                 throw processException(e);
786             }
787             finally {
788                 closeSession(session);
789             }
790         }
791         else {
792             return ((Long)result).intValue();
793         }
794     }
795 
796     public int countByC_N(long classNameId, String name)
797         throws SystemException {
798         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
799         String finderClassName = ExpandoTable.class.getName();
800         String finderMethodName = "countByC_N";
801         String[] finderParams = new String[] {
802                 Long.class.getName(), String.class.getName()
803             };
804         Object[] finderArgs = new Object[] { new Long(classNameId), name };
805 
806         Object result = null;
807 
808         if (finderClassNameCacheEnabled) {
809             result = FinderCacheUtil.getResult(finderClassName,
810                     finderMethodName, finderParams, finderArgs, this);
811         }
812 
813         if (result == null) {
814             Session session = null;
815 
816             try {
817                 session = openSession();
818 
819                 StringBuilder query = new StringBuilder();
820 
821                 query.append("SELECT COUNT(*) ");
822                 query.append(
823                     "FROM com.liferay.portlet.expando.model.ExpandoTable WHERE ");
824 
825                 query.append("classNameId = ?");
826 
827                 query.append(" AND ");
828 
829                 if (name == null) {
830                     query.append("name IS NULL");
831                 }
832                 else {
833                     query.append("name = ?");
834                 }
835 
836                 query.append(" ");
837 
838                 Query q = session.createQuery(query.toString());
839 
840                 QueryPos qPos = QueryPos.getInstance(q);
841 
842                 qPos.add(classNameId);
843 
844                 if (name != null) {
845                     qPos.add(name);
846                 }
847 
848                 Long count = null;
849 
850                 Iterator<Long> itr = q.list().iterator();
851 
852                 if (itr.hasNext()) {
853                     count = itr.next();
854                 }
855 
856                 if (count == null) {
857                     count = new Long(0);
858                 }
859 
860                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
861                     finderClassName, finderMethodName, finderParams,
862                     finderArgs, count);
863 
864                 return count.intValue();
865             }
866             catch (Exception e) {
867                 throw processException(e);
868             }
869             finally {
870                 closeSession(session);
871             }
872         }
873         else {
874             return ((Long)result).intValue();
875         }
876     }
877 
878     public int countAll() throws SystemException {
879         boolean finderClassNameCacheEnabled = ExpandoTableModelImpl.CACHE_ENABLED;
880         String finderClassName = ExpandoTable.class.getName();
881         String finderMethodName = "countAll";
882         String[] finderParams = new String[] {  };
883         Object[] finderArgs = new Object[] {  };
884 
885         Object result = null;
886 
887         if (finderClassNameCacheEnabled) {
888             result = FinderCacheUtil.getResult(finderClassName,
889                     finderMethodName, finderParams, finderArgs, this);
890         }
891 
892         if (result == null) {
893             Session session = null;
894 
895             try {
896                 session = openSession();
897 
898                 Query q = session.createQuery(
899                         "SELECT COUNT(*) FROM com.liferay.portlet.expando.model.ExpandoTable");
900 
901                 Long count = null;
902 
903                 Iterator<Long> itr = q.list().iterator();
904 
905                 if (itr.hasNext()) {
906                     count = itr.next();
907                 }
908 
909                 if (count == null) {
910                     count = new Long(0);
911                 }
912 
913                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
914                     finderClassName, finderMethodName, finderParams,
915                     finderArgs, count);
916 
917                 return count.intValue();
918             }
919             catch (Exception e) {
920                 throw processException(e);
921             }
922             finally {
923                 closeSession(session);
924             }
925         }
926         else {
927             return ((Long)result).intValue();
928         }
929     }
930 
931     public void afterPropertiesSet() {
932         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
933                     com.liferay.portal.util.PropsUtil.get(
934                         "value.object.listener.com.liferay.portlet.expando.model.ExpandoTable")));
935 
936         if (listenerClassNames.length > 0) {
937             try {
938                 List<ModelListener> listenersList = new ArrayList<ModelListener>();
939 
940                 for (String listenerClassName : listenerClassNames) {
941                     listenersList.add((ModelListener)Class.forName(
942                             listenerClassName).newInstance());
943                 }
944 
945                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
946             }
947             catch (Exception e) {
948                 _log.error(e);
949             }
950         }
951     }
952 
953     private static Log _log = LogFactoryUtil.getLog(ExpandoTablePersistenceImpl.class);
954 }