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