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