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.EntityCacheUtil;
22  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
23  import com.liferay.portal.kernel.dao.orm.FinderPath;
24  import com.liferay.portal.kernel.dao.orm.Query;
25  import com.liferay.portal.kernel.dao.orm.QueryPos;
26  import com.liferay.portal.kernel.dao.orm.QueryUtil;
27  import com.liferay.portal.kernel.dao.orm.Session;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.InstanceFactory;
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.ResourcePersistence;
40  import com.liferay.portal.service.persistence.UserPersistence;
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.io.Serializable;
49  
50  import java.util.ArrayList;
51  import java.util.Collections;
52  import java.util.List;
53  
54  /**
55   * <a href="ExpandoTablePersistenceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * <p>
58   * ServiceBuilder generated this class. Modifications in this class will be
59   * overwritten the next time is generated.
60   * </p>
61   *
62   * @author    Brian Wing Shun Chan
63   * @see       ExpandoTablePersistence
64   * @see       ExpandoTableUtil
65   * @generated
66   */
67  public class ExpandoTablePersistenceImpl extends BasePersistenceImpl<ExpandoTable>
68      implements ExpandoTablePersistence {
69      public static final String FINDER_CLASS_NAME_ENTITY = ExpandoTableImpl.class.getName();
70      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
71          ".List";
72      public static final FinderPath FINDER_PATH_FIND_BY_C_C = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
73              ExpandoTableModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
74              "findByC_C",
75              new String[] {
76                  Long.class.getName(), Long.class.getName(),
77                  
78              "java.lang.Integer", "java.lang.Integer",
79                  "com.liferay.portal.kernel.util.OrderByComparator"
80              });
81      public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
82              ExpandoTableModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
83              "countByC_C",
84              new String[] { Long.class.getName(), Long.class.getName() });
85      public static final FinderPath FINDER_PATH_FETCH_BY_C_C_N = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
86              ExpandoTableModelImpl.FINDER_CACHE_ENABLED,
87              FINDER_CLASS_NAME_ENTITY, "fetchByC_C_N",
88              new String[] {
89                  Long.class.getName(), Long.class.getName(),
90                  String.class.getName()
91              });
92      public static final FinderPath FINDER_PATH_COUNT_BY_C_C_N = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
93              ExpandoTableModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
94              "countByC_C_N",
95              new String[] {
96                  Long.class.getName(), Long.class.getName(),
97                  String.class.getName()
98              });
99      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
100             ExpandoTableModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
101             "findAll", new String[0]);
102     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
103             ExpandoTableModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
104             "countAll", new String[0]);
105 
106     public void cacheResult(ExpandoTable expandoTable) {
107         EntityCacheUtil.putResult(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
108             ExpandoTableImpl.class, expandoTable.getPrimaryKey(), expandoTable);
109 
110         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C_N,
111             new Object[] {
112                 new Long(expandoTable.getCompanyId()),
113                 new Long(expandoTable.getClassNameId()),
114                 
115             expandoTable.getName()
116             }, expandoTable);
117     }
118 
119     public void cacheResult(List<ExpandoTable> expandoTables) {
120         for (ExpandoTable expandoTable : expandoTables) {
121             if (EntityCacheUtil.getResult(
122                         ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
123                         ExpandoTableImpl.class, expandoTable.getPrimaryKey(),
124                         this) == null) {
125                 cacheResult(expandoTable);
126             }
127         }
128     }
129 
130     public void clearCache() {
131         CacheRegistry.clear(ExpandoTableImpl.class.getName());
132         EntityCacheUtil.clearCache(ExpandoTableImpl.class.getName());
133         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
134         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
135     }
136 
137     public void clearCache(ExpandoTable expandoTable) {
138         EntityCacheUtil.removeResult(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
139             ExpandoTableImpl.class, expandoTable.getPrimaryKey());
140 
141         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C_N,
142             new Object[] {
143                 new Long(expandoTable.getCompanyId()),
144                 new Long(expandoTable.getClassNameId()),
145                 
146             expandoTable.getName()
147             });
148     }
149 
150     public ExpandoTable create(long tableId) {
151         ExpandoTable expandoTable = new ExpandoTableImpl();
152 
153         expandoTable.setNew(true);
154         expandoTable.setPrimaryKey(tableId);
155 
156         return expandoTable;
157     }
158 
159     public ExpandoTable remove(Serializable primaryKey)
160         throws NoSuchModelException, SystemException {
161         return remove(((Long)primaryKey).longValue());
162     }
163 
164     public ExpandoTable remove(long tableId)
165         throws NoSuchTableException, SystemException {
166         Session session = null;
167 
168         try {
169             session = openSession();
170 
171             ExpandoTable expandoTable = (ExpandoTable)session.get(ExpandoTableImpl.class,
172                     new Long(tableId));
173 
174             if (expandoTable == null) {
175                 if (_log.isWarnEnabled()) {
176                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + tableId);
177                 }
178 
179                 throw new NoSuchTableException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
180                     tableId);
181             }
182 
183             return remove(expandoTable);
184         }
185         catch (NoSuchTableException nsee) {
186             throw nsee;
187         }
188         catch (Exception e) {
189             throw processException(e);
190         }
191         finally {
192             closeSession(session);
193         }
194     }
195 
196     protected ExpandoTable removeImpl(ExpandoTable expandoTable)
197         throws SystemException {
198         expandoTable = toUnwrappedModel(expandoTable);
199 
200         Session session = null;
201 
202         try {
203             session = openSession();
204 
205             BatchSessionUtil.delete(session, expandoTable);
206         }
207         catch (Exception e) {
208             throw processException(e);
209         }
210         finally {
211             closeSession(session);
212         }
213 
214         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
215 
216         ExpandoTableModelImpl expandoTableModelImpl = (ExpandoTableModelImpl)expandoTable;
217 
218         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C_N,
219             new Object[] {
220                 new Long(expandoTableModelImpl.getOriginalCompanyId()),
221                 new Long(expandoTableModelImpl.getOriginalClassNameId()),
222                 
223             expandoTableModelImpl.getOriginalName()
224             });
225 
226         EntityCacheUtil.removeResult(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
227             ExpandoTableImpl.class, expandoTable.getPrimaryKey());
228 
229         return expandoTable;
230     }
231 
232     /**
233      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
234      */
235     public ExpandoTable update(ExpandoTable expandoTable)
236         throws SystemException {
237         if (_log.isWarnEnabled()) {
238             _log.warn(
239                 "Using the deprecated update(ExpandoTable expandoTable) method. Use update(ExpandoTable expandoTable, boolean merge) instead.");
240         }
241 
242         return update(expandoTable, false);
243     }
244 
245     public ExpandoTable updateImpl(
246         com.liferay.portlet.expando.model.ExpandoTable expandoTable,
247         boolean merge) throws SystemException {
248         expandoTable = toUnwrappedModel(expandoTable);
249 
250         boolean isNew = expandoTable.isNew();
251 
252         ExpandoTableModelImpl expandoTableModelImpl = (ExpandoTableModelImpl)expandoTable;
253 
254         Session session = null;
255 
256         try {
257             session = openSession();
258 
259             BatchSessionUtil.update(session, expandoTable, merge);
260 
261             expandoTable.setNew(false);
262         }
263         catch (Exception e) {
264             throw processException(e);
265         }
266         finally {
267             closeSession(session);
268         }
269 
270         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
271 
272         EntityCacheUtil.putResult(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
273             ExpandoTableImpl.class, expandoTable.getPrimaryKey(), expandoTable);
274 
275         if (!isNew &&
276                 ((expandoTable.getCompanyId() != expandoTableModelImpl.getOriginalCompanyId()) ||
277                 (expandoTable.getClassNameId() != expandoTableModelImpl.getOriginalClassNameId()) ||
278                 !Validator.equals(expandoTable.getName(),
279                     expandoTableModelImpl.getOriginalName()))) {
280             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C_N,
281                 new Object[] {
282                     new Long(expandoTableModelImpl.getOriginalCompanyId()),
283                     new Long(expandoTableModelImpl.getOriginalClassNameId()),
284                     
285                 expandoTableModelImpl.getOriginalName()
286                 });
287         }
288 
289         if (isNew ||
290                 ((expandoTable.getCompanyId() != expandoTableModelImpl.getOriginalCompanyId()) ||
291                 (expandoTable.getClassNameId() != expandoTableModelImpl.getOriginalClassNameId()) ||
292                 !Validator.equals(expandoTable.getName(),
293                     expandoTableModelImpl.getOriginalName()))) {
294             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C_N,
295                 new Object[] {
296                     new Long(expandoTable.getCompanyId()),
297                     new Long(expandoTable.getClassNameId()),
298                     
299                 expandoTable.getName()
300                 }, expandoTable);
301         }
302 
303         return expandoTable;
304     }
305 
306     protected ExpandoTable toUnwrappedModel(ExpandoTable expandoTable) {
307         if (expandoTable instanceof ExpandoTableImpl) {
308             return expandoTable;
309         }
310 
311         ExpandoTableImpl expandoTableImpl = new ExpandoTableImpl();
312 
313         expandoTableImpl.setNew(expandoTable.isNew());
314         expandoTableImpl.setPrimaryKey(expandoTable.getPrimaryKey());
315 
316         expandoTableImpl.setTableId(expandoTable.getTableId());
317         expandoTableImpl.setCompanyId(expandoTable.getCompanyId());
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> findByC_C(long companyId, long classNameId)
380         throws SystemException {
381         return findByC_C(companyId, classNameId, QueryUtil.ALL_POS,
382             QueryUtil.ALL_POS, null);
383     }
384 
385     public List<ExpandoTable> findByC_C(long companyId, long classNameId,
386         int start, int end) throws SystemException {
387         return findByC_C(companyId, classNameId, start, end, null);
388     }
389 
390     public List<ExpandoTable> findByC_C(long companyId, long classNameId,
391         int start, int end, OrderByComparator orderByComparator)
392         throws SystemException {
393         Object[] finderArgs = new Object[] {
394                 companyId, classNameId,
395                 
396                 String.valueOf(start), String.valueOf(end),
397                 String.valueOf(orderByComparator)
398             };
399 
400         List<ExpandoTable> list = (List<ExpandoTable>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_C,
401                 finderArgs, this);
402 
403         if (list == null) {
404             StringBundler query = null;
405 
406             if (orderByComparator != null) {
407                 query = new StringBundler(4 +
408                         (orderByComparator.getOrderByFields().length * 3));
409             }
410             else {
411                 query = new StringBundler(3);
412             }
413 
414             query.append(_SQL_SELECT_EXPANDOTABLE_WHERE);
415 
416             query.append(_FINDER_COLUMN_C_C_COMPANYID_2);
417 
418             query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
419 
420             if (orderByComparator != null) {
421                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
422                     orderByComparator);
423             }
424 
425             String sql = query.toString();
426 
427             Session session = null;
428 
429             try {
430                 session = openSession();
431 
432                 Query q = session.createQuery(sql);
433 
434                 QueryPos qPos = QueryPos.getInstance(q);
435 
436                 qPos.add(companyId);
437 
438                 qPos.add(classNameId);
439 
440                 list = (List<ExpandoTable>)QueryUtil.list(q, getDialect(),
441                         start, end);
442             }
443             catch (Exception e) {
444                 throw processException(e);
445             }
446             finally {
447                 if (list == null) {
448                     list = new ArrayList<ExpandoTable>();
449                 }
450 
451                 cacheResult(list);
452 
453                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_C, finderArgs,
454                     list);
455 
456                 closeSession(session);
457             }
458         }
459 
460         return list;
461     }
462 
463     public ExpandoTable findByC_C_First(long companyId, long classNameId,
464         OrderByComparator orderByComparator)
465         throws NoSuchTableException, SystemException {
466         List<ExpandoTable> list = findByC_C(companyId, classNameId, 0, 1,
467                 orderByComparator);
468 
469         if (list.isEmpty()) {
470             StringBundler msg = new StringBundler(6);
471 
472             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
473 
474             msg.append("companyId=");
475             msg.append(companyId);
476 
477             msg.append(", classNameId=");
478             msg.append(classNameId);
479 
480             msg.append(StringPool.CLOSE_CURLY_BRACE);
481 
482             throw new NoSuchTableException(msg.toString());
483         }
484         else {
485             return list.get(0);
486         }
487     }
488 
489     public ExpandoTable findByC_C_Last(long companyId, long classNameId,
490         OrderByComparator orderByComparator)
491         throws NoSuchTableException, SystemException {
492         int count = countByC_C(companyId, classNameId);
493 
494         List<ExpandoTable> list = findByC_C(companyId, classNameId, count - 1,
495                 count, orderByComparator);
496 
497         if (list.isEmpty()) {
498             StringBundler msg = new StringBundler(6);
499 
500             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
501 
502             msg.append("companyId=");
503             msg.append(companyId);
504 
505             msg.append(", classNameId=");
506             msg.append(classNameId);
507 
508             msg.append(StringPool.CLOSE_CURLY_BRACE);
509 
510             throw new NoSuchTableException(msg.toString());
511         }
512         else {
513             return list.get(0);
514         }
515     }
516 
517     public ExpandoTable[] findByC_C_PrevAndNext(long tableId, long companyId,
518         long classNameId, OrderByComparator orderByComparator)
519         throws NoSuchTableException, SystemException {
520         ExpandoTable expandoTable = findByPrimaryKey(tableId);
521 
522         Session session = null;
523 
524         try {
525             session = openSession();
526 
527             ExpandoTable[] array = new ExpandoTableImpl[3];
528 
529             array[0] = getByC_C_PrevAndNext(session, expandoTable, companyId,
530                     classNameId, orderByComparator, true);
531 
532             array[1] = expandoTable;
533 
534             array[2] = getByC_C_PrevAndNext(session, expandoTable, companyId,
535                     classNameId, orderByComparator, false);
536 
537             return array;
538         }
539         catch (Exception e) {
540             throw processException(e);
541         }
542         finally {
543             closeSession(session);
544         }
545     }
546 
547     protected ExpandoTable getByC_C_PrevAndNext(Session session,
548         ExpandoTable expandoTable, long companyId, long classNameId,
549         OrderByComparator orderByComparator, boolean previous) {
550         StringBundler query = null;
551 
552         if (orderByComparator != null) {
553             query = new StringBundler(6 +
554                     (orderByComparator.getOrderByFields().length * 6));
555         }
556         else {
557             query = new StringBundler(3);
558         }
559 
560         query.append(_SQL_SELECT_EXPANDOTABLE_WHERE);
561 
562         query.append(_FINDER_COLUMN_C_C_COMPANYID_2);
563 
564         query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
565 
566         if (orderByComparator != null) {
567             String[] orderByFields = orderByComparator.getOrderByFields();
568 
569             if (orderByFields.length > 0) {
570                 query.append(WHERE_AND);
571             }
572 
573             for (int i = 0; i < orderByFields.length; i++) {
574                 query.append(_ORDER_BY_ENTITY_ALIAS);
575                 query.append(orderByFields[i]);
576 
577                 if ((i + 1) < orderByFields.length) {
578                     if (orderByComparator.isAscending() ^ previous) {
579                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
580                     }
581                     else {
582                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
583                     }
584                 }
585                 else {
586                     if (orderByComparator.isAscending() ^ previous) {
587                         query.append(WHERE_GREATER_THAN);
588                     }
589                     else {
590                         query.append(WHERE_LESSER_THAN);
591                     }
592                 }
593             }
594 
595             query.append(ORDER_BY_CLAUSE);
596 
597             for (int i = 0; i < orderByFields.length; i++) {
598                 query.append(_ORDER_BY_ENTITY_ALIAS);
599                 query.append(orderByFields[i]);
600 
601                 if ((i + 1) < orderByFields.length) {
602                     if (orderByComparator.isAscending() ^ previous) {
603                         query.append(ORDER_BY_ASC_HAS_NEXT);
604                     }
605                     else {
606                         query.append(ORDER_BY_DESC_HAS_NEXT);
607                     }
608                 }
609                 else {
610                     if (orderByComparator.isAscending() ^ previous) {
611                         query.append(ORDER_BY_ASC);
612                     }
613                     else {
614                         query.append(ORDER_BY_DESC);
615                     }
616                 }
617             }
618         }
619 
620         String sql = query.toString();
621 
622         Query q = session.createQuery(sql);
623 
624         q.setFirstResult(0);
625         q.setMaxResults(2);
626 
627         QueryPos qPos = QueryPos.getInstance(q);
628 
629         qPos.add(companyId);
630 
631         qPos.add(classNameId);
632 
633         if (orderByComparator != null) {
634             Object[] values = orderByComparator.getOrderByValues(expandoTable);
635 
636             for (Object value : values) {
637                 qPos.add(value);
638             }
639         }
640 
641         List<ExpandoTable> list = q.list();
642 
643         if (list.size() == 2) {
644             return list.get(1);
645         }
646         else {
647             return null;
648         }
649     }
650 
651     public ExpandoTable findByC_C_N(long companyId, long classNameId,
652         String name) throws NoSuchTableException, SystemException {
653         ExpandoTable expandoTable = fetchByC_C_N(companyId, classNameId, name);
654 
655         if (expandoTable == null) {
656             StringBundler msg = new StringBundler(8);
657 
658             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
659 
660             msg.append("companyId=");
661             msg.append(companyId);
662 
663             msg.append(", classNameId=");
664             msg.append(classNameId);
665 
666             msg.append(", name=");
667             msg.append(name);
668 
669             msg.append(StringPool.CLOSE_CURLY_BRACE);
670 
671             if (_log.isWarnEnabled()) {
672                 _log.warn(msg.toString());
673             }
674 
675             throw new NoSuchTableException(msg.toString());
676         }
677 
678         return expandoTable;
679     }
680 
681     public ExpandoTable fetchByC_C_N(long companyId, long classNameId,
682         String name) throws SystemException {
683         return fetchByC_C_N(companyId, classNameId, name, true);
684     }
685 
686     public ExpandoTable fetchByC_C_N(long companyId, long classNameId,
687         String name, boolean retrieveFromCache) throws SystemException {
688         Object[] finderArgs = new Object[] { companyId, classNameId, name };
689 
690         Object result = null;
691 
692         if (retrieveFromCache) {
693             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C_N,
694                     finderArgs, this);
695         }
696 
697         if (result == null) {
698             StringBundler query = new StringBundler(4);
699 
700             query.append(_SQL_SELECT_EXPANDOTABLE_WHERE);
701 
702             query.append(_FINDER_COLUMN_C_C_N_COMPANYID_2);
703 
704             query.append(_FINDER_COLUMN_C_C_N_CLASSNAMEID_2);
705 
706             if (name == null) {
707                 query.append(_FINDER_COLUMN_C_C_N_NAME_1);
708             }
709             else {
710                 if (name.equals(StringPool.BLANK)) {
711                     query.append(_FINDER_COLUMN_C_C_N_NAME_3);
712                 }
713                 else {
714                     query.append(_FINDER_COLUMN_C_C_N_NAME_2);
715                 }
716             }
717 
718             String sql = query.toString();
719 
720             Session session = null;
721 
722             try {
723                 session = openSession();
724 
725                 Query q = session.createQuery(sql);
726 
727                 QueryPos qPos = QueryPos.getInstance(q);
728 
729                 qPos.add(companyId);
730 
731                 qPos.add(classNameId);
732 
733                 if (name != null) {
734                     qPos.add(name);
735                 }
736 
737                 List<ExpandoTable> list = q.list();
738 
739                 result = list;
740 
741                 ExpandoTable expandoTable = null;
742 
743                 if (list.isEmpty()) {
744                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C_N,
745                         finderArgs, list);
746                 }
747                 else {
748                     expandoTable = list.get(0);
749 
750                     cacheResult(expandoTable);
751 
752                     if ((expandoTable.getCompanyId() != companyId) ||
753                             (expandoTable.getClassNameId() != classNameId) ||
754                             (expandoTable.getName() == null) ||
755                             !expandoTable.getName().equals(name)) {
756                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C_N,
757                             finderArgs, expandoTable);
758                     }
759                 }
760 
761                 return expandoTable;
762             }
763             catch (Exception e) {
764                 throw processException(e);
765             }
766             finally {
767                 if (result == null) {
768                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C_N,
769                         finderArgs, new ArrayList<ExpandoTable>());
770                 }
771 
772                 closeSession(session);
773             }
774         }
775         else {
776             if (result instanceof List<?>) {
777                 return null;
778             }
779             else {
780                 return (ExpandoTable)result;
781             }
782         }
783     }
784 
785     public List<ExpandoTable> findAll() throws SystemException {
786         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
787     }
788 
789     public List<ExpandoTable> findAll(int start, int end)
790         throws SystemException {
791         return findAll(start, end, null);
792     }
793 
794     public List<ExpandoTable> findAll(int start, int end,
795         OrderByComparator orderByComparator) throws SystemException {
796         Object[] finderArgs = new Object[] {
797                 String.valueOf(start), String.valueOf(end),
798                 String.valueOf(orderByComparator)
799             };
800 
801         List<ExpandoTable> list = (List<ExpandoTable>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
802                 finderArgs, this);
803 
804         if (list == null) {
805             StringBundler query = null;
806             String sql = null;
807 
808             if (orderByComparator != null) {
809                 query = new StringBundler(2 +
810                         (orderByComparator.getOrderByFields().length * 3));
811 
812                 query.append(_SQL_SELECT_EXPANDOTABLE);
813 
814                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
815                     orderByComparator);
816 
817                 sql = query.toString();
818             }
819             else {
820                 sql = _SQL_SELECT_EXPANDOTABLE;
821             }
822 
823             Session session = null;
824 
825             try {
826                 session = openSession();
827 
828                 Query q = session.createQuery(sql);
829 
830                 if (orderByComparator == null) {
831                     list = (List<ExpandoTable>)QueryUtil.list(q, getDialect(),
832                             start, end, false);
833 
834                     Collections.sort(list);
835                 }
836                 else {
837                     list = (List<ExpandoTable>)QueryUtil.list(q, getDialect(),
838                             start, end);
839                 }
840             }
841             catch (Exception e) {
842                 throw processException(e);
843             }
844             finally {
845                 if (list == null) {
846                     list = new ArrayList<ExpandoTable>();
847                 }
848 
849                 cacheResult(list);
850 
851                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
852 
853                 closeSession(session);
854             }
855         }
856 
857         return list;
858     }
859 
860     public void removeByC_C(long companyId, long classNameId)
861         throws SystemException {
862         for (ExpandoTable expandoTable : findByC_C(companyId, classNameId)) {
863             remove(expandoTable);
864         }
865     }
866 
867     public void removeByC_C_N(long companyId, long classNameId, String name)
868         throws NoSuchTableException, SystemException {
869         ExpandoTable expandoTable = findByC_C_N(companyId, classNameId, name);
870 
871         remove(expandoTable);
872     }
873 
874     public void removeAll() throws SystemException {
875         for (ExpandoTable expandoTable : findAll()) {
876             remove(expandoTable);
877         }
878     }
879 
880     public int countByC_C(long companyId, long classNameId)
881         throws SystemException {
882         Object[] finderArgs = new Object[] { companyId, classNameId };
883 
884         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
885                 finderArgs, this);
886 
887         if (count == null) {
888             StringBundler query = new StringBundler(3);
889 
890             query.append(_SQL_COUNT_EXPANDOTABLE_WHERE);
891 
892             query.append(_FINDER_COLUMN_C_C_COMPANYID_2);
893 
894             query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
895 
896             String sql = query.toString();
897 
898             Session session = null;
899 
900             try {
901                 session = openSession();
902 
903                 Query q = session.createQuery(sql);
904 
905                 QueryPos qPos = QueryPos.getInstance(q);
906 
907                 qPos.add(companyId);
908 
909                 qPos.add(classNameId);
910 
911                 count = (Long)q.uniqueResult();
912             }
913             catch (Exception e) {
914                 throw processException(e);
915             }
916             finally {
917                 if (count == null) {
918                     count = Long.valueOf(0);
919                 }
920 
921                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
922                     count);
923 
924                 closeSession(session);
925             }
926         }
927 
928         return count.intValue();
929     }
930 
931     public int countByC_C_N(long companyId, long classNameId, String name)
932         throws SystemException {
933         Object[] finderArgs = new Object[] { companyId, classNameId, name };
934 
935         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C_N,
936                 finderArgs, this);
937 
938         if (count == null) {
939             StringBundler query = new StringBundler(4);
940 
941             query.append(_SQL_COUNT_EXPANDOTABLE_WHERE);
942 
943             query.append(_FINDER_COLUMN_C_C_N_COMPANYID_2);
944 
945             query.append(_FINDER_COLUMN_C_C_N_CLASSNAMEID_2);
946 
947             if (name == null) {
948                 query.append(_FINDER_COLUMN_C_C_N_NAME_1);
949             }
950             else {
951                 if (name.equals(StringPool.BLANK)) {
952                     query.append(_FINDER_COLUMN_C_C_N_NAME_3);
953                 }
954                 else {
955                     query.append(_FINDER_COLUMN_C_C_N_NAME_2);
956                 }
957             }
958 
959             String sql = query.toString();
960 
961             Session session = null;
962 
963             try {
964                 session = openSession();
965 
966                 Query q = session.createQuery(sql);
967 
968                 QueryPos qPos = QueryPos.getInstance(q);
969 
970                 qPos.add(companyId);
971 
972                 qPos.add(classNameId);
973 
974                 if (name != null) {
975                     qPos.add(name);
976                 }
977 
978                 count = (Long)q.uniqueResult();
979             }
980             catch (Exception e) {
981                 throw processException(e);
982             }
983             finally {
984                 if (count == null) {
985                     count = Long.valueOf(0);
986                 }
987 
988                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C_N,
989                     finderArgs, count);
990 
991                 closeSession(session);
992             }
993         }
994 
995         return count.intValue();
996     }
997 
998     public int countAll() throws SystemException {
999         Object[] finderArgs = new Object[0];
1000
1001        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1002                finderArgs, this);
1003
1004        if (count == null) {
1005            Session session = null;
1006
1007            try {
1008                session = openSession();
1009
1010                Query q = session.createQuery(_SQL_COUNT_EXPANDOTABLE);
1011
1012                count = (Long)q.uniqueResult();
1013            }
1014            catch (Exception e) {
1015                throw processException(e);
1016            }
1017            finally {
1018                if (count == null) {
1019                    count = Long.valueOf(0);
1020                }
1021
1022                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1023                    count);
1024
1025                closeSession(session);
1026            }
1027        }
1028
1029        return count.intValue();
1030    }
1031
1032    public void afterPropertiesSet() {
1033        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1034                    com.liferay.portal.util.PropsUtil.get(
1035                        "value.object.listener.com.liferay.portlet.expando.model.ExpandoTable")));
1036
1037        if (listenerClassNames.length > 0) {
1038            try {
1039                List<ModelListener<ExpandoTable>> listenersList = new ArrayList<ModelListener<ExpandoTable>>();
1040
1041                for (String listenerClassName : listenerClassNames) {
1042                    listenersList.add((ModelListener<ExpandoTable>)InstanceFactory.newInstance(
1043                            listenerClassName));
1044                }
1045
1046                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1047            }
1048            catch (Exception e) {
1049                _log.error(e);
1050            }
1051        }
1052    }
1053
1054    public void destroy() {
1055        EntityCacheUtil.removeCache(ExpandoTableImpl.class.getName());
1056        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1057        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1058    }
1059
1060    @BeanReference(type = ExpandoColumnPersistence.class)
1061    protected ExpandoColumnPersistence expandoColumnPersistence;
1062    @BeanReference(type = ExpandoRowPersistence.class)
1063    protected ExpandoRowPersistence expandoRowPersistence;
1064    @BeanReference(type = ExpandoTablePersistence.class)
1065    protected ExpandoTablePersistence expandoTablePersistence;
1066    @BeanReference(type = ExpandoValuePersistence.class)
1067    protected ExpandoValuePersistence expandoValuePersistence;
1068    @BeanReference(type = ResourcePersistence.class)
1069    protected ResourcePersistence resourcePersistence;
1070    @BeanReference(type = UserPersistence.class)
1071    protected UserPersistence userPersistence;
1072    private static final String _SQL_SELECT_EXPANDOTABLE = "SELECT expandoTable FROM ExpandoTable expandoTable";
1073    private static final String _SQL_SELECT_EXPANDOTABLE_WHERE = "SELECT expandoTable FROM ExpandoTable expandoTable WHERE ";
1074    private static final String _SQL_COUNT_EXPANDOTABLE = "SELECT COUNT(expandoTable) FROM ExpandoTable expandoTable";
1075    private static final String _SQL_COUNT_EXPANDOTABLE_WHERE = "SELECT COUNT(expandoTable) FROM ExpandoTable expandoTable WHERE ";
1076    private static final String _FINDER_COLUMN_C_C_COMPANYID_2 = "expandoTable.companyId = ? AND ";
1077    private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "expandoTable.classNameId = ?";
1078    private static final String _FINDER_COLUMN_C_C_N_COMPANYID_2 = "expandoTable.companyId = ? AND ";
1079    private static final String _FINDER_COLUMN_C_C_N_CLASSNAMEID_2 = "expandoTable.classNameId = ? AND ";
1080    private static final String _FINDER_COLUMN_C_C_N_NAME_1 = "expandoTable.name IS NULL";
1081    private static final String _FINDER_COLUMN_C_C_N_NAME_2 = "expandoTable.name = ?";
1082    private static final String _FINDER_COLUMN_C_C_N_NAME_3 = "(expandoTable.name IS NULL OR expandoTable.name = ?)";
1083    private static final String _ORDER_BY_ENTITY_ALIAS = "expandoTable.";
1084    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ExpandoTable exists with the primary key ";
1085    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ExpandoTable exists with the key {";
1086    private static Log _log = LogFactoryUtil.getLog(ExpandoTablePersistenceImpl.class);
1087}