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