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