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