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