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