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