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