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