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