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