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