001    /**
002     * Copyright (c) 2000-2013 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.portal.service.persistence;
016    
017    import com.liferay.portal.NoSuchPreferencesException;
018    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
019    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
020    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderPath;
022    import com.liferay.portal.kernel.dao.orm.Query;
023    import com.liferay.portal.kernel.dao.orm.QueryPos;
024    import com.liferay.portal.kernel.dao.orm.QueryUtil;
025    import com.liferay.portal.kernel.dao.orm.Session;
026    import com.liferay.portal.kernel.exception.SystemException;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.InstanceFactory;
031    import com.liferay.portal.kernel.util.OrderByComparator;
032    import com.liferay.portal.kernel.util.StringBundler;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.kernel.util.StringUtil;
035    import com.liferay.portal.kernel.util.UnmodifiableList;
036    import com.liferay.portal.model.CacheModel;
037    import com.liferay.portal.model.ModelListener;
038    import com.liferay.portal.model.PortalPreferences;
039    import com.liferay.portal.model.impl.PortalPreferencesImpl;
040    import com.liferay.portal.model.impl.PortalPreferencesModelImpl;
041    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
042    
043    import java.io.Serializable;
044    
045    import java.util.ArrayList;
046    import java.util.Collections;
047    import java.util.List;
048    
049    /**
050     * The persistence implementation for the portal preferences service.
051     *
052     * <p>
053     * Caching information and settings can be found in <code>portal.properties</code>
054     * </p>
055     *
056     * @author Brian Wing Shun Chan
057     * @see PortalPreferencesPersistence
058     * @see PortalPreferencesUtil
059     * @generated
060     */
061    public class PortalPreferencesPersistenceImpl extends BasePersistenceImpl<PortalPreferences>
062            implements PortalPreferencesPersistence {
063            /*
064             * NOTE FOR DEVELOPERS:
065             *
066             * Never modify or reference this class directly. Always use {@link PortalPreferencesUtil} to access the portal preferences persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
067             */
068            public static final String FINDER_CLASS_NAME_ENTITY = PortalPreferencesImpl.class.getName();
069            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
070                    ".List1";
071            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
072                    ".List2";
073            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(PortalPreferencesModelImpl.ENTITY_CACHE_ENABLED,
074                            PortalPreferencesModelImpl.FINDER_CACHE_ENABLED,
075                            PortalPreferencesImpl.class,
076                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
077            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(PortalPreferencesModelImpl.ENTITY_CACHE_ENABLED,
078                            PortalPreferencesModelImpl.FINDER_CACHE_ENABLED,
079                            PortalPreferencesImpl.class,
080                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
081            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PortalPreferencesModelImpl.ENTITY_CACHE_ENABLED,
082                            PortalPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
083                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
084            public static final FinderPath FINDER_PATH_FETCH_BY_O_O = new FinderPath(PortalPreferencesModelImpl.ENTITY_CACHE_ENABLED,
085                            PortalPreferencesModelImpl.FINDER_CACHE_ENABLED,
086                            PortalPreferencesImpl.class, FINDER_CLASS_NAME_ENTITY,
087                            "fetchByO_O",
088                            new String[] { Long.class.getName(), Integer.class.getName() },
089                            PortalPreferencesModelImpl.OWNERID_COLUMN_BITMASK |
090                            PortalPreferencesModelImpl.OWNERTYPE_COLUMN_BITMASK);
091            public static final FinderPath FINDER_PATH_COUNT_BY_O_O = new FinderPath(PortalPreferencesModelImpl.ENTITY_CACHE_ENABLED,
092                            PortalPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
093                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByO_O",
094                            new String[] { Long.class.getName(), Integer.class.getName() });
095    
096            /**
097             * Returns the portal preferences where ownerId = &#63; and ownerType = &#63; or throws a {@link com.liferay.portal.NoSuchPreferencesException} if it could not be found.
098             *
099             * @param ownerId the owner ID
100             * @param ownerType the owner type
101             * @return the matching portal preferences
102             * @throws com.liferay.portal.NoSuchPreferencesException if a matching portal preferences could not be found
103             * @throws SystemException if a system exception occurred
104             */
105            @Override
106            public PortalPreferences findByO_O(long ownerId, int ownerType)
107                    throws NoSuchPreferencesException, SystemException {
108                    PortalPreferences portalPreferences = fetchByO_O(ownerId, ownerType);
109    
110                    if (portalPreferences == null) {
111                            StringBundler msg = new StringBundler(6);
112    
113                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
114    
115                            msg.append("ownerId=");
116                            msg.append(ownerId);
117    
118                            msg.append(", ownerType=");
119                            msg.append(ownerType);
120    
121                            msg.append(StringPool.CLOSE_CURLY_BRACE);
122    
123                            if (_log.isWarnEnabled()) {
124                                    _log.warn(msg.toString());
125                            }
126    
127                            throw new NoSuchPreferencesException(msg.toString());
128                    }
129    
130                    return portalPreferences;
131            }
132    
133            /**
134             * Returns the portal preferences where ownerId = &#63; and ownerType = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
135             *
136             * @param ownerId the owner ID
137             * @param ownerType the owner type
138             * @return the matching portal preferences, or <code>null</code> if a matching portal preferences could not be found
139             * @throws SystemException if a system exception occurred
140             */
141            @Override
142            public PortalPreferences fetchByO_O(long ownerId, int ownerType)
143                    throws SystemException {
144                    return fetchByO_O(ownerId, ownerType, true);
145            }
146    
147            /**
148             * Returns the portal preferences where ownerId = &#63; and ownerType = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
149             *
150             * @param ownerId the owner ID
151             * @param ownerType the owner type
152             * @param retrieveFromCache whether to use the finder cache
153             * @return the matching portal preferences, or <code>null</code> if a matching portal preferences could not be found
154             * @throws SystemException if a system exception occurred
155             */
156            @Override
157            public PortalPreferences fetchByO_O(long ownerId, int ownerType,
158                    boolean retrieveFromCache) throws SystemException {
159                    Object[] finderArgs = new Object[] { ownerId, ownerType };
160    
161                    Object result = null;
162    
163                    if (retrieveFromCache) {
164                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_O_O,
165                                            finderArgs, this);
166                    }
167    
168                    if (result instanceof PortalPreferences) {
169                            PortalPreferences portalPreferences = (PortalPreferences)result;
170    
171                            if ((ownerId != portalPreferences.getOwnerId()) ||
172                                            (ownerType != portalPreferences.getOwnerType())) {
173                                    result = null;
174                            }
175                    }
176    
177                    if (result == null) {
178                            StringBundler query = new StringBundler(4);
179    
180                            query.append(_SQL_SELECT_PORTALPREFERENCES_WHERE);
181    
182                            query.append(_FINDER_COLUMN_O_O_OWNERID_2);
183    
184                            query.append(_FINDER_COLUMN_O_O_OWNERTYPE_2);
185    
186                            String sql = query.toString();
187    
188                            Session session = null;
189    
190                            try {
191                                    session = openSession();
192    
193                                    Query q = session.createQuery(sql);
194    
195                                    QueryPos qPos = QueryPos.getInstance(q);
196    
197                                    qPos.add(ownerId);
198    
199                                    qPos.add(ownerType);
200    
201                                    List<PortalPreferences> list = q.list();
202    
203                                    if (list.isEmpty()) {
204                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O,
205                                                    finderArgs, list);
206                                    }
207                                    else {
208                                            if ((list.size() > 1) && _log.isWarnEnabled()) {
209                                                    _log.warn(
210                                                            "PortalPreferencesPersistenceImpl.fetchByO_O(long, int, boolean) with parameters (" +
211                                                            StringUtil.merge(finderArgs) +
212                                                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
213                                            }
214    
215                                            PortalPreferences portalPreferences = list.get(0);
216    
217                                            result = portalPreferences;
218    
219                                            cacheResult(portalPreferences);
220    
221                                            if ((portalPreferences.getOwnerId() != ownerId) ||
222                                                            (portalPreferences.getOwnerType() != ownerType)) {
223                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O,
224                                                            finderArgs, portalPreferences);
225                                            }
226                                    }
227                            }
228                            catch (Exception e) {
229                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_O_O,
230                                            finderArgs);
231    
232                                    throw processException(e);
233                            }
234                            finally {
235                                    closeSession(session);
236                            }
237                    }
238    
239                    if (result instanceof List<?>) {
240                            return null;
241                    }
242                    else {
243                            return (PortalPreferences)result;
244                    }
245            }
246    
247            /**
248             * Removes the portal preferences where ownerId = &#63; and ownerType = &#63; from the database.
249             *
250             * @param ownerId the owner ID
251             * @param ownerType the owner type
252             * @return the portal preferences that was removed
253             * @throws SystemException if a system exception occurred
254             */
255            @Override
256            public PortalPreferences removeByO_O(long ownerId, int ownerType)
257                    throws NoSuchPreferencesException, SystemException {
258                    PortalPreferences portalPreferences = findByO_O(ownerId, ownerType);
259    
260                    return remove(portalPreferences);
261            }
262    
263            /**
264             * Returns the number of portal preferenceses where ownerId = &#63; and ownerType = &#63;.
265             *
266             * @param ownerId the owner ID
267             * @param ownerType the owner type
268             * @return the number of matching portal preferenceses
269             * @throws SystemException if a system exception occurred
270             */
271            @Override
272            public int countByO_O(long ownerId, int ownerType)
273                    throws SystemException {
274                    FinderPath finderPath = FINDER_PATH_COUNT_BY_O_O;
275    
276                    Object[] finderArgs = new Object[] { ownerId, ownerType };
277    
278                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
279                                    this);
280    
281                    if (count == null) {
282                            StringBundler query = new StringBundler(3);
283    
284                            query.append(_SQL_COUNT_PORTALPREFERENCES_WHERE);
285    
286                            query.append(_FINDER_COLUMN_O_O_OWNERID_2);
287    
288                            query.append(_FINDER_COLUMN_O_O_OWNERTYPE_2);
289    
290                            String sql = query.toString();
291    
292                            Session session = null;
293    
294                            try {
295                                    session = openSession();
296    
297                                    Query q = session.createQuery(sql);
298    
299                                    QueryPos qPos = QueryPos.getInstance(q);
300    
301                                    qPos.add(ownerId);
302    
303                                    qPos.add(ownerType);
304    
305                                    count = (Long)q.uniqueResult();
306    
307                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
308                            }
309                            catch (Exception e) {
310                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
311    
312                                    throw processException(e);
313                            }
314                            finally {
315                                    closeSession(session);
316                            }
317                    }
318    
319                    return count.intValue();
320            }
321    
322            private static final String _FINDER_COLUMN_O_O_OWNERID_2 = "portalPreferences.ownerId = ? AND ";
323            private static final String _FINDER_COLUMN_O_O_OWNERTYPE_2 = "portalPreferences.ownerType = ?";
324    
325            public PortalPreferencesPersistenceImpl() {
326                    setModelClass(PortalPreferences.class);
327            }
328    
329            /**
330             * Caches the portal preferences in the entity cache if it is enabled.
331             *
332             * @param portalPreferences the portal preferences
333             */
334            @Override
335            public void cacheResult(PortalPreferences portalPreferences) {
336                    EntityCacheUtil.putResult(PortalPreferencesModelImpl.ENTITY_CACHE_ENABLED,
337                            PortalPreferencesImpl.class, portalPreferences.getPrimaryKey(),
338                            portalPreferences);
339    
340                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O,
341                            new Object[] {
342                                    portalPreferences.getOwnerId(), portalPreferences.getOwnerType()
343                            }, portalPreferences);
344    
345                    portalPreferences.resetOriginalValues();
346            }
347    
348            /**
349             * Caches the portal preferenceses in the entity cache if it is enabled.
350             *
351             * @param portalPreferenceses the portal preferenceses
352             */
353            @Override
354            public void cacheResult(List<PortalPreferences> portalPreferenceses) {
355                    for (PortalPreferences portalPreferences : portalPreferenceses) {
356                            if (EntityCacheUtil.getResult(
357                                                    PortalPreferencesModelImpl.ENTITY_CACHE_ENABLED,
358                                                    PortalPreferencesImpl.class,
359                                                    portalPreferences.getPrimaryKey()) == null) {
360                                    cacheResult(portalPreferences);
361                            }
362                            else {
363                                    portalPreferences.resetOriginalValues();
364                            }
365                    }
366            }
367    
368            /**
369             * Clears the cache for all portal preferenceses.
370             *
371             * <p>
372             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
373             * </p>
374             */
375            @Override
376            public void clearCache() {
377                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
378                            CacheRegistryUtil.clear(PortalPreferencesImpl.class.getName());
379                    }
380    
381                    EntityCacheUtil.clearCache(PortalPreferencesImpl.class.getName());
382    
383                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
384                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
385                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
386            }
387    
388            /**
389             * Clears the cache for the portal preferences.
390             *
391             * <p>
392             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
393             * </p>
394             */
395            @Override
396            public void clearCache(PortalPreferences portalPreferences) {
397                    EntityCacheUtil.removeResult(PortalPreferencesModelImpl.ENTITY_CACHE_ENABLED,
398                            PortalPreferencesImpl.class, portalPreferences.getPrimaryKey());
399    
400                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
401                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
402    
403                    clearUniqueFindersCache(portalPreferences);
404            }
405    
406            @Override
407            public void clearCache(List<PortalPreferences> portalPreferenceses) {
408                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
409                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
410    
411                    for (PortalPreferences portalPreferences : portalPreferenceses) {
412                            EntityCacheUtil.removeResult(PortalPreferencesModelImpl.ENTITY_CACHE_ENABLED,
413                                    PortalPreferencesImpl.class, portalPreferences.getPrimaryKey());
414    
415                            clearUniqueFindersCache(portalPreferences);
416                    }
417            }
418    
419            protected void cacheUniqueFindersCache(PortalPreferences portalPreferences) {
420                    if (portalPreferences.isNew()) {
421                            Object[] args = new Object[] {
422                                            portalPreferences.getOwnerId(),
423                                            portalPreferences.getOwnerType()
424                                    };
425    
426                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_O_O, args,
427                                    Long.valueOf(1));
428                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O, args,
429                                    portalPreferences);
430                    }
431                    else {
432                            PortalPreferencesModelImpl portalPreferencesModelImpl = (PortalPreferencesModelImpl)portalPreferences;
433    
434                            if ((portalPreferencesModelImpl.getColumnBitmask() &
435                                            FINDER_PATH_FETCH_BY_O_O.getColumnBitmask()) != 0) {
436                                    Object[] args = new Object[] {
437                                                    portalPreferences.getOwnerId(),
438                                                    portalPreferences.getOwnerType()
439                                            };
440    
441                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_O_O, args,
442                                            Long.valueOf(1));
443                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O, args,
444                                            portalPreferences);
445                            }
446                    }
447            }
448    
449            protected void clearUniqueFindersCache(PortalPreferences portalPreferences) {
450                    PortalPreferencesModelImpl portalPreferencesModelImpl = (PortalPreferencesModelImpl)portalPreferences;
451    
452                    Object[] args = new Object[] {
453                                    portalPreferences.getOwnerId(), portalPreferences.getOwnerType()
454                            };
455    
456                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_O, args);
457                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_O_O, args);
458    
459                    if ((portalPreferencesModelImpl.getColumnBitmask() &
460                                    FINDER_PATH_FETCH_BY_O_O.getColumnBitmask()) != 0) {
461                            args = new Object[] {
462                                            portalPreferencesModelImpl.getOriginalOwnerId(),
463                                            portalPreferencesModelImpl.getOriginalOwnerType()
464                                    };
465    
466                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_O, args);
467                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_O_O, args);
468                    }
469            }
470    
471            /**
472             * Creates a new portal preferences with the primary key. Does not add the portal preferences to the database.
473             *
474             * @param portalPreferencesId the primary key for the new portal preferences
475             * @return the new portal preferences
476             */
477            @Override
478            public PortalPreferences create(long portalPreferencesId) {
479                    PortalPreferences portalPreferences = new PortalPreferencesImpl();
480    
481                    portalPreferences.setNew(true);
482                    portalPreferences.setPrimaryKey(portalPreferencesId);
483    
484                    return portalPreferences;
485            }
486    
487            /**
488             * Removes the portal preferences with the primary key from the database. Also notifies the appropriate model listeners.
489             *
490             * @param portalPreferencesId the primary key of the portal preferences
491             * @return the portal preferences that was removed
492             * @throws com.liferay.portal.NoSuchPreferencesException if a portal preferences with the primary key could not be found
493             * @throws SystemException if a system exception occurred
494             */
495            @Override
496            public PortalPreferences remove(long portalPreferencesId)
497                    throws NoSuchPreferencesException, SystemException {
498                    return remove((Serializable)portalPreferencesId);
499            }
500    
501            /**
502             * Removes the portal preferences with the primary key from the database. Also notifies the appropriate model listeners.
503             *
504             * @param primaryKey the primary key of the portal preferences
505             * @return the portal preferences that was removed
506             * @throws com.liferay.portal.NoSuchPreferencesException if a portal preferences with the primary key could not be found
507             * @throws SystemException if a system exception occurred
508             */
509            @Override
510            public PortalPreferences remove(Serializable primaryKey)
511                    throws NoSuchPreferencesException, SystemException {
512                    Session session = null;
513    
514                    try {
515                            session = openSession();
516    
517                            PortalPreferences portalPreferences = (PortalPreferences)session.get(PortalPreferencesImpl.class,
518                                            primaryKey);
519    
520                            if (portalPreferences == null) {
521                                    if (_log.isWarnEnabled()) {
522                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
523                                    }
524    
525                                    throw new NoSuchPreferencesException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
526                                            primaryKey);
527                            }
528    
529                            return remove(portalPreferences);
530                    }
531                    catch (NoSuchPreferencesException nsee) {
532                            throw nsee;
533                    }
534                    catch (Exception e) {
535                            throw processException(e);
536                    }
537                    finally {
538                            closeSession(session);
539                    }
540            }
541    
542            @Override
543            protected PortalPreferences removeImpl(PortalPreferences portalPreferences)
544                    throws SystemException {
545                    portalPreferences = toUnwrappedModel(portalPreferences);
546    
547                    Session session = null;
548    
549                    try {
550                            session = openSession();
551    
552                            if (!session.contains(portalPreferences)) {
553                                    portalPreferences = (PortalPreferences)session.get(PortalPreferencesImpl.class,
554                                                    portalPreferences.getPrimaryKeyObj());
555                            }
556    
557                            if (portalPreferences != null) {
558                                    session.delete(portalPreferences);
559                            }
560                    }
561                    catch (Exception e) {
562                            throw processException(e);
563                    }
564                    finally {
565                            closeSession(session);
566                    }
567    
568                    if (portalPreferences != null) {
569                            clearCache(portalPreferences);
570                    }
571    
572                    return portalPreferences;
573            }
574    
575            @Override
576            public PortalPreferences updateImpl(
577                    com.liferay.portal.model.PortalPreferences portalPreferences)
578                    throws SystemException {
579                    portalPreferences = toUnwrappedModel(portalPreferences);
580    
581                    boolean isNew = portalPreferences.isNew();
582    
583                    Session session = null;
584    
585                    try {
586                            session = openSession();
587    
588                            if (portalPreferences.isNew()) {
589                                    session.save(portalPreferences);
590    
591                                    portalPreferences.setNew(false);
592                            }
593                            else {
594                                    session.merge(portalPreferences);
595                            }
596                    }
597                    catch (Exception e) {
598                            throw processException(e);
599                    }
600                    finally {
601                            closeSession(session);
602                    }
603    
604                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
605    
606                    if (isNew || !PortalPreferencesModelImpl.COLUMN_BITMASK_ENABLED) {
607                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
608                    }
609    
610                    EntityCacheUtil.putResult(PortalPreferencesModelImpl.ENTITY_CACHE_ENABLED,
611                            PortalPreferencesImpl.class, portalPreferences.getPrimaryKey(),
612                            portalPreferences);
613    
614                    clearUniqueFindersCache(portalPreferences);
615                    cacheUniqueFindersCache(portalPreferences);
616    
617                    return portalPreferences;
618            }
619    
620            protected PortalPreferences toUnwrappedModel(
621                    PortalPreferences portalPreferences) {
622                    if (portalPreferences instanceof PortalPreferencesImpl) {
623                            return portalPreferences;
624                    }
625    
626                    PortalPreferencesImpl portalPreferencesImpl = new PortalPreferencesImpl();
627    
628                    portalPreferencesImpl.setNew(portalPreferences.isNew());
629                    portalPreferencesImpl.setPrimaryKey(portalPreferences.getPrimaryKey());
630    
631                    portalPreferencesImpl.setPortalPreferencesId(portalPreferences.getPortalPreferencesId());
632                    portalPreferencesImpl.setOwnerId(portalPreferences.getOwnerId());
633                    portalPreferencesImpl.setOwnerType(portalPreferences.getOwnerType());
634                    portalPreferencesImpl.setPreferences(portalPreferences.getPreferences());
635    
636                    return portalPreferencesImpl;
637            }
638    
639            /**
640             * Returns the portal preferences with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
641             *
642             * @param primaryKey the primary key of the portal preferences
643             * @return the portal preferences
644             * @throws com.liferay.portal.NoSuchPreferencesException if a portal preferences with the primary key could not be found
645             * @throws SystemException if a system exception occurred
646             */
647            @Override
648            public PortalPreferences findByPrimaryKey(Serializable primaryKey)
649                    throws NoSuchPreferencesException, SystemException {
650                    PortalPreferences portalPreferences = fetchByPrimaryKey(primaryKey);
651    
652                    if (portalPreferences == null) {
653                            if (_log.isWarnEnabled()) {
654                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
655                            }
656    
657                            throw new NoSuchPreferencesException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
658                                    primaryKey);
659                    }
660    
661                    return portalPreferences;
662            }
663    
664            /**
665             * Returns the portal preferences with the primary key or throws a {@link com.liferay.portal.NoSuchPreferencesException} if it could not be found.
666             *
667             * @param portalPreferencesId the primary key of the portal preferences
668             * @return the portal preferences
669             * @throws com.liferay.portal.NoSuchPreferencesException if a portal preferences with the primary key could not be found
670             * @throws SystemException if a system exception occurred
671             */
672            @Override
673            public PortalPreferences findByPrimaryKey(long portalPreferencesId)
674                    throws NoSuchPreferencesException, SystemException {
675                    return findByPrimaryKey((Serializable)portalPreferencesId);
676            }
677    
678            /**
679             * Returns the portal preferences with the primary key or returns <code>null</code> if it could not be found.
680             *
681             * @param primaryKey the primary key of the portal preferences
682             * @return the portal preferences, or <code>null</code> if a portal preferences with the primary key could not be found
683             * @throws SystemException if a system exception occurred
684             */
685            @Override
686            public PortalPreferences fetchByPrimaryKey(Serializable primaryKey)
687                    throws SystemException {
688                    PortalPreferences portalPreferences = (PortalPreferences)EntityCacheUtil.getResult(PortalPreferencesModelImpl.ENTITY_CACHE_ENABLED,
689                                    PortalPreferencesImpl.class, primaryKey);
690    
691                    if (portalPreferences == _nullPortalPreferences) {
692                            return null;
693                    }
694    
695                    if (portalPreferences == null) {
696                            Session session = null;
697    
698                            try {
699                                    session = openSession();
700    
701                                    portalPreferences = (PortalPreferences)session.get(PortalPreferencesImpl.class,
702                                                    primaryKey);
703    
704                                    if (portalPreferences != null) {
705                                            cacheResult(portalPreferences);
706                                    }
707                                    else {
708                                            EntityCacheUtil.putResult(PortalPreferencesModelImpl.ENTITY_CACHE_ENABLED,
709                                                    PortalPreferencesImpl.class, primaryKey,
710                                                    _nullPortalPreferences);
711                                    }
712                            }
713                            catch (Exception e) {
714                                    EntityCacheUtil.removeResult(PortalPreferencesModelImpl.ENTITY_CACHE_ENABLED,
715                                            PortalPreferencesImpl.class, primaryKey);
716    
717                                    throw processException(e);
718                            }
719                            finally {
720                                    closeSession(session);
721                            }
722                    }
723    
724                    return portalPreferences;
725            }
726    
727            /**
728             * Returns the portal preferences with the primary key or returns <code>null</code> if it could not be found.
729             *
730             * @param portalPreferencesId the primary key of the portal preferences
731             * @return the portal preferences, or <code>null</code> if a portal preferences with the primary key could not be found
732             * @throws SystemException if a system exception occurred
733             */
734            @Override
735            public PortalPreferences fetchByPrimaryKey(long portalPreferencesId)
736                    throws SystemException {
737                    return fetchByPrimaryKey((Serializable)portalPreferencesId);
738            }
739    
740            /**
741             * Returns all the portal preferenceses.
742             *
743             * @return the portal preferenceses
744             * @throws SystemException if a system exception occurred
745             */
746            @Override
747            public List<PortalPreferences> findAll() throws SystemException {
748                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
749            }
750    
751            /**
752             * Returns a range of all the portal preferenceses.
753             *
754             * <p>
755             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortalPreferencesModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
756             * </p>
757             *
758             * @param start the lower bound of the range of portal preferenceses
759             * @param end the upper bound of the range of portal preferenceses (not inclusive)
760             * @return the range of portal preferenceses
761             * @throws SystemException if a system exception occurred
762             */
763            @Override
764            public List<PortalPreferences> findAll(int start, int end)
765                    throws SystemException {
766                    return findAll(start, end, null);
767            }
768    
769            /**
770             * Returns an ordered range of all the portal preferenceses.
771             *
772             * <p>
773             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortalPreferencesModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
774             * </p>
775             *
776             * @param start the lower bound of the range of portal preferenceses
777             * @param end the upper bound of the range of portal preferenceses (not inclusive)
778             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
779             * @return the ordered range of portal preferenceses
780             * @throws SystemException if a system exception occurred
781             */
782            @Override
783            public List<PortalPreferences> findAll(int start, int end,
784                    OrderByComparator orderByComparator) throws SystemException {
785                    boolean pagination = true;
786                    FinderPath finderPath = null;
787                    Object[] finderArgs = null;
788    
789                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
790                                    (orderByComparator == null)) {
791                            pagination = false;
792                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
793                            finderArgs = FINDER_ARGS_EMPTY;
794                    }
795                    else {
796                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
797                            finderArgs = new Object[] { start, end, orderByComparator };
798                    }
799    
800                    List<PortalPreferences> list = (List<PortalPreferences>)FinderCacheUtil.getResult(finderPath,
801                                    finderArgs, this);
802    
803                    if (list == null) {
804                            StringBundler query = null;
805                            String sql = null;
806    
807                            if (orderByComparator != null) {
808                                    query = new StringBundler(2 +
809                                                    (orderByComparator.getOrderByFields().length * 3));
810    
811                                    query.append(_SQL_SELECT_PORTALPREFERENCES);
812    
813                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
814                                            orderByComparator);
815    
816                                    sql = query.toString();
817                            }
818                            else {
819                                    sql = _SQL_SELECT_PORTALPREFERENCES;
820    
821                                    if (pagination) {
822                                            sql = sql.concat(PortalPreferencesModelImpl.ORDER_BY_JPQL);
823                                    }
824                            }
825    
826                            Session session = null;
827    
828                            try {
829                                    session = openSession();
830    
831                                    Query q = session.createQuery(sql);
832    
833                                    if (!pagination) {
834                                            list = (List<PortalPreferences>)QueryUtil.list(q,
835                                                            getDialect(), start, end, false);
836    
837                                            Collections.sort(list);
838    
839                                            list = new UnmodifiableList<PortalPreferences>(list);
840                                    }
841                                    else {
842                                            list = (List<PortalPreferences>)QueryUtil.list(q,
843                                                            getDialect(), start, end);
844                                    }
845    
846                                    cacheResult(list);
847    
848                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
849                            }
850                            catch (Exception e) {
851                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
852    
853                                    throw processException(e);
854                            }
855                            finally {
856                                    closeSession(session);
857                            }
858                    }
859    
860                    return list;
861            }
862    
863            /**
864             * Removes all the portal preferenceses from the database.
865             *
866             * @throws SystemException if a system exception occurred
867             */
868            @Override
869            public void removeAll() throws SystemException {
870                    for (PortalPreferences portalPreferences : findAll()) {
871                            remove(portalPreferences);
872                    }
873            }
874    
875            /**
876             * Returns the number of portal preferenceses.
877             *
878             * @return the number of portal preferenceses
879             * @throws SystemException if a system exception occurred
880             */
881            @Override
882            public int countAll() throws SystemException {
883                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
884                                    FINDER_ARGS_EMPTY, this);
885    
886                    if (count == null) {
887                            Session session = null;
888    
889                            try {
890                                    session = openSession();
891    
892                                    Query q = session.createQuery(_SQL_COUNT_PORTALPREFERENCES);
893    
894                                    count = (Long)q.uniqueResult();
895    
896                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
897                                            FINDER_ARGS_EMPTY, count);
898                            }
899                            catch (Exception e) {
900                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
901                                            FINDER_ARGS_EMPTY);
902    
903                                    throw processException(e);
904                            }
905                            finally {
906                                    closeSession(session);
907                            }
908                    }
909    
910                    return count.intValue();
911            }
912    
913            /**
914             * Initializes the portal preferences persistence.
915             */
916            public void afterPropertiesSet() {
917                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
918                                            com.liferay.portal.util.PropsUtil.get(
919                                                    "value.object.listener.com.liferay.portal.model.PortalPreferences")));
920    
921                    if (listenerClassNames.length > 0) {
922                            try {
923                                    List<ModelListener<PortalPreferences>> listenersList = new ArrayList<ModelListener<PortalPreferences>>();
924    
925                                    for (String listenerClassName : listenerClassNames) {
926                                            listenersList.add((ModelListener<PortalPreferences>)InstanceFactory.newInstance(
927                                                            getClassLoader(), listenerClassName));
928                                    }
929    
930                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
931                            }
932                            catch (Exception e) {
933                                    _log.error(e);
934                            }
935                    }
936            }
937    
938            public void destroy() {
939                    EntityCacheUtil.removeCache(PortalPreferencesImpl.class.getName());
940                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
941                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
942                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
943            }
944    
945            private static final String _SQL_SELECT_PORTALPREFERENCES = "SELECT portalPreferences FROM PortalPreferences portalPreferences";
946            private static final String _SQL_SELECT_PORTALPREFERENCES_WHERE = "SELECT portalPreferences FROM PortalPreferences portalPreferences WHERE ";
947            private static final String _SQL_COUNT_PORTALPREFERENCES = "SELECT COUNT(portalPreferences) FROM PortalPreferences portalPreferences";
948            private static final String _SQL_COUNT_PORTALPREFERENCES_WHERE = "SELECT COUNT(portalPreferences) FROM PortalPreferences portalPreferences WHERE ";
949            private static final String _ORDER_BY_ENTITY_ALIAS = "portalPreferences.";
950            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No PortalPreferences exists with the primary key ";
951            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No PortalPreferences exists with the key {";
952            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
953            private static Log _log = LogFactoryUtil.getLog(PortalPreferencesPersistenceImpl.class);
954            private static PortalPreferences _nullPortalPreferences = new PortalPreferencesImpl() {
955                            @Override
956                            public Object clone() {
957                                    return this;
958                            }
959    
960                            @Override
961                            public CacheModel<PortalPreferences> toCacheModel() {
962                                    return _nullPortalPreferencesCacheModel;
963                            }
964                    };
965    
966            private static CacheModel<PortalPreferences> _nullPortalPreferencesCacheModel =
967                    new CacheModel<PortalPreferences>() {
968                            @Override
969                            public PortalPreferences toEntityModel() {
970                                    return _nullPortalPreferences;
971                            }
972                    };
973    }