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   * @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                     "SELECT expandoTable FROM ExpandoTable expandoTable WHERE ");
415 
416                 query.append("expandoTable.companyId = ?");
417 
418                 query.append(" AND ");
419 
420                 query.append("expandoTable.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                     "SELECT expandoTable FROM ExpandoTable expandoTable WHERE ");
480 
481                 query.append("expandoTable.companyId = ?");
482 
483                 query.append(" AND ");
484 
485                 query.append("expandoTable.classNameId = ?");
486 
487                 query.append(" ");
488 
489                 if (obc != null) {
490                     query.append("ORDER BY ");
491 
492                     String[] orderByFields = obc.getOrderByFields();
493 
494                     for (int i = 0; i < orderByFields.length; i++) {
495                         query.append("expandoTable.");
496                         query.append(orderByFields[i]);
497 
498                         if (obc.isAscending()) {
499                             query.append(" ASC");
500                         }
501                         else {
502                             query.append(" DESC");
503                         }
504 
505                         if ((i + 1) < orderByFields.length) {
506                             query.append(", ");
507                         }
508                     }
509                 }
510 
511                 Query q = session.createQuery(query.toString());
512 
513                 QueryPos qPos = QueryPos.getInstance(q);
514 
515                 qPos.add(companyId);
516 
517                 qPos.add(classNameId);
518 
519                 list = (List<ExpandoTable>)QueryUtil.list(q, getDialect(),
520                         start, end);
521             }
522             catch (Exception e) {
523                 throw processException(e);
524             }
525             finally {
526                 if (list == null) {
527                     list = new ArrayList<ExpandoTable>();
528                 }
529 
530                 cacheResult(list);
531 
532                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_C_C,
533                     finderArgs, list);
534 
535                 closeSession(session);
536             }
537         }
538 
539         return list;
540     }
541 
542     public ExpandoTable findByC_C_First(long companyId, long classNameId,
543         OrderByComparator obc) throws NoSuchTableException, SystemException {
544         List<ExpandoTable> list = findByC_C(companyId, classNameId, 0, 1, obc);
545 
546         if (list.isEmpty()) {
547             StringBuilder msg = new StringBuilder();
548 
549             msg.append("No ExpandoTable exists with the key {");
550 
551             msg.append("companyId=" + companyId);
552 
553             msg.append(", ");
554             msg.append("classNameId=" + classNameId);
555 
556             msg.append(StringPool.CLOSE_CURLY_BRACE);
557 
558             throw new NoSuchTableException(msg.toString());
559         }
560         else {
561             return list.get(0);
562         }
563     }
564 
565     public ExpandoTable findByC_C_Last(long companyId, long classNameId,
566         OrderByComparator obc) throws NoSuchTableException, SystemException {
567         int count = countByC_C(companyId, classNameId);
568 
569         List<ExpandoTable> list = findByC_C(companyId, classNameId, count - 1,
570                 count, obc);
571 
572         if (list.isEmpty()) {
573             StringBuilder msg = new StringBuilder();
574 
575             msg.append("No ExpandoTable exists with the key {");
576 
577             msg.append("companyId=" + companyId);
578 
579             msg.append(", ");
580             msg.append("classNameId=" + classNameId);
581 
582             msg.append(StringPool.CLOSE_CURLY_BRACE);
583 
584             throw new NoSuchTableException(msg.toString());
585         }
586         else {
587             return list.get(0);
588         }
589     }
590 
591     public ExpandoTable[] findByC_C_PrevAndNext(long tableId, long companyId,
592         long classNameId, OrderByComparator obc)
593         throws NoSuchTableException, SystemException {
594         ExpandoTable expandoTable = findByPrimaryKey(tableId);
595 
596         int count = countByC_C(companyId, classNameId);
597 
598         Session session = null;
599 
600         try {
601             session = openSession();
602 
603             StringBuilder query = new StringBuilder();
604 
605             query.append(
606                 "SELECT expandoTable FROM ExpandoTable expandoTable WHERE ");
607 
608             query.append("expandoTable.companyId = ?");
609 
610             query.append(" AND ");
611 
612             query.append("expandoTable.classNameId = ?");
613 
614             query.append(" ");
615 
616             if (obc != null) {
617                 query.append("ORDER BY ");
618 
619                 String[] orderByFields = obc.getOrderByFields();
620 
621                 for (int i = 0; i < orderByFields.length; i++) {
622                     query.append("expandoTable.");
623                     query.append(orderByFields[i]);
624 
625                     if (obc.isAscending()) {
626                         query.append(" ASC");
627                     }
628                     else {
629                         query.append(" DESC");
630                     }
631 
632                     if ((i + 1) < orderByFields.length) {
633                         query.append(", ");
634                     }
635                 }
636             }
637 
638             Query q = session.createQuery(query.toString());
639 
640             QueryPos qPos = QueryPos.getInstance(q);
641 
642             qPos.add(companyId);
643 
644             qPos.add(classNameId);
645 
646             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
647                     expandoTable);
648 
649             ExpandoTable[] array = new ExpandoTableImpl[3];
650 
651             array[0] = (ExpandoTable)objArray[0];
652             array[1] = (ExpandoTable)objArray[1];
653             array[2] = (ExpandoTable)objArray[2];
654 
655             return array;
656         }
657         catch (Exception e) {
658             throw processException(e);
659         }
660         finally {
661             closeSession(session);
662         }
663     }
664 
665     public ExpandoTable findByC_C_N(long companyId, long classNameId,
666         String name) throws NoSuchTableException, SystemException {
667         ExpandoTable expandoTable = fetchByC_C_N(companyId, classNameId, name);
668 
669         if (expandoTable == null) {
670             StringBuilder msg = new StringBuilder();
671 
672             msg.append("No ExpandoTable exists with the key {");
673 
674             msg.append("companyId=" + companyId);
675 
676             msg.append(", ");
677             msg.append("classNameId=" + classNameId);
678 
679             msg.append(", ");
680             msg.append("name=" + name);
681 
682             msg.append(StringPool.CLOSE_CURLY_BRACE);
683 
684             if (_log.isWarnEnabled()) {
685                 _log.warn(msg.toString());
686             }
687 
688             throw new NoSuchTableException(msg.toString());
689         }
690 
691         return expandoTable;
692     }
693 
694     public ExpandoTable fetchByC_C_N(long companyId, long classNameId,
695         String name) throws SystemException {
696         return fetchByC_C_N(companyId, classNameId, name, true);
697     }
698 
699     public ExpandoTable fetchByC_C_N(long companyId, long classNameId,
700         String name, boolean retrieveFromCache) throws SystemException {
701         Object[] finderArgs = new Object[] {
702                 new Long(companyId), new Long(classNameId),
703                 
704                 name
705             };
706 
707         Object result = null;
708 
709         if (retrieveFromCache) {
710             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C_N,
711                     finderArgs, this);
712         }
713 
714         if (result == null) {
715             Session session = null;
716 
717             try {
718                 session = openSession();
719 
720                 StringBuilder query = new StringBuilder();
721 
722                 query.append(
723                     "SELECT expandoTable FROM ExpandoTable expandoTable WHERE ");
724 
725                 query.append("expandoTable.companyId = ?");
726 
727                 query.append(" AND ");
728 
729                 query.append("expandoTable.classNameId = ?");
730 
731                 query.append(" AND ");
732 
733                 if (name == null) {
734                     query.append("expandoTable.name IS NULL");
735                 }
736                 else {
737                     query.append("expandoTable.name = ?");
738                 }
739 
740                 query.append(" ");
741 
742                 Query q = session.createQuery(query.toString());
743 
744                 QueryPos qPos = QueryPos.getInstance(q);
745 
746                 qPos.add(companyId);
747 
748                 qPos.add(classNameId);
749 
750                 if (name != null) {
751                     qPos.add(name);
752                 }
753 
754                 List<ExpandoTable> list = q.list();
755 
756                 result = list;
757 
758                 ExpandoTable expandoTable = null;
759 
760                 if (list.isEmpty()) {
761                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C_N,
762                         finderArgs, list);
763                 }
764                 else {
765                     expandoTable = list.get(0);
766 
767                     cacheResult(expandoTable);
768 
769                     if ((expandoTable.getCompanyId() != companyId) ||
770                             (expandoTable.getClassNameId() != classNameId) ||
771                             (expandoTable.getName() == null) ||
772                             !expandoTable.getName().equals(name)) {
773                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C_N,
774                             finderArgs, expandoTable);
775                     }
776                 }
777 
778                 return expandoTable;
779             }
780             catch (Exception e) {
781                 throw processException(e);
782             }
783             finally {
784                 if (result == null) {
785                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C_N,
786                         finderArgs, new ArrayList<ExpandoTable>());
787                 }
788 
789                 closeSession(session);
790             }
791         }
792         else {
793             if (result instanceof List) {
794                 return null;
795             }
796             else {
797                 return (ExpandoTable)result;
798             }
799         }
800     }
801 
802     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
803         throws SystemException {
804         Session session = null;
805 
806         try {
807             session = openSession();
808 
809             dynamicQuery.compile(session);
810 
811             return dynamicQuery.list();
812         }
813         catch (Exception e) {
814             throw processException(e);
815         }
816         finally {
817             closeSession(session);
818         }
819     }
820 
821     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
822         int start, int end) throws SystemException {
823         Session session = null;
824 
825         try {
826             session = openSession();
827 
828             dynamicQuery.setLimit(start, end);
829 
830             dynamicQuery.compile(session);
831 
832             return dynamicQuery.list();
833         }
834         catch (Exception e) {
835             throw processException(e);
836         }
837         finally {
838             closeSession(session);
839         }
840     }
841 
842     public List<ExpandoTable> findAll() throws SystemException {
843         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
844     }
845 
846     public List<ExpandoTable> findAll(int start, int end)
847         throws SystemException {
848         return findAll(start, end, null);
849     }
850 
851     public List<ExpandoTable> findAll(int start, int end, OrderByComparator obc)
852         throws SystemException {
853         Object[] finderArgs = new Object[] {
854                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
855             };
856 
857         List<ExpandoTable> list = (List<ExpandoTable>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
858                 finderArgs, this);
859 
860         if (list == null) {
861             Session session = null;
862 
863             try {
864                 session = openSession();
865 
866                 StringBuilder query = new StringBuilder();
867 
868                 query.append(
869                     "SELECT expandoTable FROM ExpandoTable expandoTable ");
870 
871                 if (obc != null) {
872                     query.append("ORDER BY ");
873 
874                     String[] orderByFields = obc.getOrderByFields();
875 
876                     for (int i = 0; i < orderByFields.length; i++) {
877                         query.append("expandoTable.");
878                         query.append(orderByFields[i]);
879 
880                         if (obc.isAscending()) {
881                             query.append(" ASC");
882                         }
883                         else {
884                             query.append(" DESC");
885                         }
886 
887                         if ((i + 1) < orderByFields.length) {
888                             query.append(", ");
889                         }
890                     }
891                 }
892 
893                 Query q = session.createQuery(query.toString());
894 
895                 if (obc == null) {
896                     list = (List<ExpandoTable>)QueryUtil.list(q, getDialect(),
897                             start, end, false);
898 
899                     Collections.sort(list);
900                 }
901                 else {
902                     list = (List<ExpandoTable>)QueryUtil.list(q, getDialect(),
903                             start, end);
904                 }
905             }
906             catch (Exception e) {
907                 throw processException(e);
908             }
909             finally {
910                 if (list == null) {
911                     list = new ArrayList<ExpandoTable>();
912                 }
913 
914                 cacheResult(list);
915 
916                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
917 
918                 closeSession(session);
919             }
920         }
921 
922         return list;
923     }
924 
925     public void removeByC_C(long companyId, long classNameId)
926         throws SystemException {
927         for (ExpandoTable expandoTable : findByC_C(companyId, classNameId)) {
928             remove(expandoTable);
929         }
930     }
931 
932     public void removeByC_C_N(long companyId, long classNameId, String name)
933         throws NoSuchTableException, SystemException {
934         ExpandoTable expandoTable = findByC_C_N(companyId, classNameId, name);
935 
936         remove(expandoTable);
937     }
938 
939     public void removeAll() throws SystemException {
940         for (ExpandoTable expandoTable : findAll()) {
941             remove(expandoTable);
942         }
943     }
944 
945     public int countByC_C(long companyId, long classNameId)
946         throws SystemException {
947         Object[] finderArgs = new Object[] {
948                 new Long(companyId), new Long(classNameId)
949             };
950 
951         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
952                 finderArgs, this);
953 
954         if (count == null) {
955             Session session = null;
956 
957             try {
958                 session = openSession();
959 
960                 StringBuilder query = new StringBuilder();
961 
962                 query.append("SELECT COUNT(expandoTable) ");
963                 query.append("FROM ExpandoTable expandoTable WHERE ");
964 
965                 query.append("expandoTable.companyId = ?");
966 
967                 query.append(" AND ");
968 
969                 query.append("expandoTable.classNameId = ?");
970 
971                 query.append(" ");
972 
973                 Query q = session.createQuery(query.toString());
974 
975                 QueryPos qPos = QueryPos.getInstance(q);
976 
977                 qPos.add(companyId);
978 
979                 qPos.add(classNameId);
980 
981                 count = (Long)q.uniqueResult();
982             }
983             catch (Exception e) {
984                 throw processException(e);
985             }
986             finally {
987                 if (count == null) {
988                     count = Long.valueOf(0);
989                 }
990 
991                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
992                     count);
993 
994                 closeSession(session);
995             }
996         }
997 
998         return count.intValue();
999     }
1000
1001    public int countByC_C_N(long companyId, long classNameId, String name)
1002        throws SystemException {
1003        Object[] finderArgs = new Object[] {
1004                new Long(companyId), new Long(classNameId),
1005                
1006                name
1007            };
1008
1009        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C_N,
1010                finderArgs, this);
1011
1012        if (count == null) {
1013            Session session = null;
1014
1015            try {
1016                session = openSession();
1017
1018                StringBuilder query = new StringBuilder();
1019
1020                query.append("SELECT COUNT(expandoTable) ");
1021                query.append("FROM ExpandoTable expandoTable WHERE ");
1022
1023                query.append("expandoTable.companyId = ?");
1024
1025                query.append(" AND ");
1026
1027                query.append("expandoTable.classNameId = ?");
1028
1029                query.append(" AND ");
1030
1031                if (name == null) {
1032                    query.append("expandoTable.name IS NULL");
1033                }
1034                else {
1035                    query.append("expandoTable.name = ?");
1036                }
1037
1038                query.append(" ");
1039
1040                Query q = session.createQuery(query.toString());
1041
1042                QueryPos qPos = QueryPos.getInstance(q);
1043
1044                qPos.add(companyId);
1045
1046                qPos.add(classNameId);
1047
1048                if (name != null) {
1049                    qPos.add(name);
1050                }
1051
1052                count = (Long)q.uniqueResult();
1053            }
1054            catch (Exception e) {
1055                throw processException(e);
1056            }
1057            finally {
1058                if (count == null) {
1059                    count = Long.valueOf(0);
1060                }
1061
1062                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C_N,
1063                    finderArgs, count);
1064
1065                closeSession(session);
1066            }
1067        }
1068
1069        return count.intValue();
1070    }
1071
1072    public int countAll() throws SystemException {
1073        Object[] finderArgs = new Object[0];
1074
1075        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1076                finderArgs, this);
1077
1078        if (count == null) {
1079            Session session = null;
1080
1081            try {
1082                session = openSession();
1083
1084                Query q = session.createQuery(
1085                        "SELECT COUNT(expandoTable) FROM ExpandoTable expandoTable");
1086
1087                count = (Long)q.uniqueResult();
1088            }
1089            catch (Exception e) {
1090                throw processException(e);
1091            }
1092            finally {
1093                if (count == null) {
1094                    count = Long.valueOf(0);
1095                }
1096
1097                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1098                    count);
1099
1100                closeSession(session);
1101            }
1102        }
1103
1104        return count.intValue();
1105    }
1106
1107    public void afterPropertiesSet() {
1108        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1109                    com.liferay.portal.util.PropsUtil.get(
1110                        "value.object.listener.com.liferay.portlet.expando.model.ExpandoTable")));
1111
1112        if (listenerClassNames.length > 0) {
1113            try {
1114                List<ModelListener<ExpandoTable>> listenersList = new ArrayList<ModelListener<ExpandoTable>>();
1115
1116                for (String listenerClassName : listenerClassNames) {
1117                    listenersList.add((ModelListener<ExpandoTable>)Class.forName(
1118                            listenerClassName).newInstance());
1119                }
1120
1121                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1122            }
1123            catch (Exception e) {
1124                _log.error(e);
1125            }
1126        }
1127    }
1128
1129    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoColumnPersistence.impl")
1130    protected com.liferay.portlet.expando.service.persistence.ExpandoColumnPersistence expandoColumnPersistence;
1131    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoRowPersistence.impl")
1132    protected com.liferay.portlet.expando.service.persistence.ExpandoRowPersistence expandoRowPersistence;
1133    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoTablePersistence.impl")
1134    protected com.liferay.portlet.expando.service.persistence.ExpandoTablePersistence expandoTablePersistence;
1135    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoValuePersistence.impl")
1136    protected com.liferay.portlet.expando.service.persistence.ExpandoValuePersistence expandoValuePersistence;
1137    private static Log _log = LogFactoryUtil.getLog(ExpandoTablePersistenceImpl.class);
1138}