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