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