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