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            /**
326             * Caches the class name in the entity cache if it is enabled.
327             *
328             * @param className the class name
329             */
330            @Override
331            public void cacheResult(ClassName className) {
332                    EntityCacheUtil.putResult(ClassNameModelImpl.ENTITY_CACHE_ENABLED,
333                            ClassNameImpl.class, className.getPrimaryKey(), className);
334    
335                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_VALUE,
336                            new Object[] { className.getValue() }, className);
337    
338                    className.resetOriginalValues();
339            }
340    
341            /**
342             * Caches the class names in the entity cache if it is enabled.
343             *
344             * @param classNames the class names
345             */
346            @Override
347            public void cacheResult(List<ClassName> classNames) {
348                    for (ClassName className : classNames) {
349                            if (EntityCacheUtil.getResult(
350                                                    ClassNameModelImpl.ENTITY_CACHE_ENABLED,
351                                                    ClassNameImpl.class, className.getPrimaryKey()) == null) {
352                                    cacheResult(className);
353                            }
354                            else {
355                                    className.resetOriginalValues();
356                            }
357                    }
358            }
359    
360            /**
361             * Clears the cache for all class names.
362             *
363             * <p>
364             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
365             * </p>
366             */
367            @Override
368            public void clearCache() {
369                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
370                            CacheRegistryUtil.clear(ClassNameImpl.class.getName());
371                    }
372    
373                    EntityCacheUtil.clearCache(ClassNameImpl.class.getName());
374    
375                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
376                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
377                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
378            }
379    
380            /**
381             * Clears the cache for the class name.
382             *
383             * <p>
384             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
385             * </p>
386             */
387            @Override
388            public void clearCache(ClassName className) {
389                    EntityCacheUtil.removeResult(ClassNameModelImpl.ENTITY_CACHE_ENABLED,
390                            ClassNameImpl.class, className.getPrimaryKey());
391    
392                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
393                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
394    
395                    clearUniqueFindersCache(className);
396            }
397    
398            @Override
399            public void clearCache(List<ClassName> classNames) {
400                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
401                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
402    
403                    for (ClassName className : classNames) {
404                            EntityCacheUtil.removeResult(ClassNameModelImpl.ENTITY_CACHE_ENABLED,
405                                    ClassNameImpl.class, className.getPrimaryKey());
406    
407                            clearUniqueFindersCache(className);
408                    }
409            }
410    
411            protected void cacheUniqueFindersCache(ClassName className) {
412                    if (className.isNew()) {
413                            Object[] args = new Object[] { className.getValue() };
414    
415                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_VALUE, args,
416                                    Long.valueOf(1));
417                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_VALUE, args,
418                                    className);
419                    }
420                    else {
421                            ClassNameModelImpl classNameModelImpl = (ClassNameModelImpl)className;
422    
423                            if ((classNameModelImpl.getColumnBitmask() &
424                                            FINDER_PATH_FETCH_BY_VALUE.getColumnBitmask()) != 0) {
425                                    Object[] args = new Object[] { className.getValue() };
426    
427                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_VALUE, args,
428                                            Long.valueOf(1));
429                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_VALUE, args,
430                                            className);
431                            }
432                    }
433            }
434    
435            protected void clearUniqueFindersCache(ClassName className) {
436                    ClassNameModelImpl classNameModelImpl = (ClassNameModelImpl)className;
437    
438                    Object[] args = new Object[] { className.getValue() };
439    
440                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_VALUE, args);
441                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_VALUE, args);
442    
443                    if ((classNameModelImpl.getColumnBitmask() &
444                                    FINDER_PATH_FETCH_BY_VALUE.getColumnBitmask()) != 0) {
445                            args = new Object[] { classNameModelImpl.getOriginalValue() };
446    
447                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_VALUE, args);
448                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_VALUE, args);
449                    }
450            }
451    
452            /**
453             * Creates a new class name with the primary key. Does not add the class name to the database.
454             *
455             * @param classNameId the primary key for the new class name
456             * @return the new class name
457             */
458            @Override
459            public ClassName create(long classNameId) {
460                    ClassName className = new ClassNameImpl();
461    
462                    className.setNew(true);
463                    className.setPrimaryKey(classNameId);
464    
465                    return className;
466            }
467    
468            /**
469             * Removes the class name with the primary key from the database. Also notifies the appropriate model listeners.
470             *
471             * @param classNameId the primary key of the class name
472             * @return the class name that was removed
473             * @throws com.liferay.portal.NoSuchClassNameException if a class name with the primary key could not be found
474             * @throws SystemException if a system exception occurred
475             */
476            @Override
477            public ClassName remove(long classNameId)
478                    throws NoSuchClassNameException, SystemException {
479                    return remove((Serializable)classNameId);
480            }
481    
482            /**
483             * Removes the class name with the primary key from the database. Also notifies the appropriate model listeners.
484             *
485             * @param primaryKey the primary key of the class name
486             * @return the class name that was removed
487             * @throws com.liferay.portal.NoSuchClassNameException if a class name with the primary key could not be found
488             * @throws SystemException if a system exception occurred
489             */
490            @Override
491            public ClassName remove(Serializable primaryKey)
492                    throws NoSuchClassNameException, SystemException {
493                    Session session = null;
494    
495                    try {
496                            session = openSession();
497    
498                            ClassName className = (ClassName)session.get(ClassNameImpl.class,
499                                            primaryKey);
500    
501                            if (className == null) {
502                                    if (_log.isWarnEnabled()) {
503                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
504                                    }
505    
506                                    throw new NoSuchClassNameException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
507                                            primaryKey);
508                            }
509    
510                            return remove(className);
511                    }
512                    catch (NoSuchClassNameException nsee) {
513                            throw nsee;
514                    }
515                    catch (Exception e) {
516                            throw processException(e);
517                    }
518                    finally {
519                            closeSession(session);
520                    }
521            }
522    
523            @Override
524            protected ClassName removeImpl(ClassName className)
525                    throws SystemException {
526                    className = toUnwrappedModel(className);
527    
528                    Session session = null;
529    
530                    try {
531                            session = openSession();
532    
533                            if (!session.contains(className)) {
534                                    className = (ClassName)session.get(ClassNameImpl.class,
535                                                    className.getPrimaryKeyObj());
536                            }
537    
538                            if (className != null) {
539                                    session.delete(className);
540                            }
541                    }
542                    catch (Exception e) {
543                            throw processException(e);
544                    }
545                    finally {
546                            closeSession(session);
547                    }
548    
549                    if (className != null) {
550                            clearCache(className);
551                    }
552    
553                    return className;
554            }
555    
556            @Override
557            public ClassName updateImpl(com.liferay.portal.model.ClassName className)
558                    throws SystemException {
559                    className = toUnwrappedModel(className);
560    
561                    boolean isNew = className.isNew();
562    
563                    Session session = null;
564    
565                    try {
566                            session = openSession();
567    
568                            if (className.isNew()) {
569                                    session.save(className);
570    
571                                    className.setNew(false);
572                            }
573                            else {
574                                    session.merge(className);
575                            }
576                    }
577                    catch (Exception e) {
578                            throw processException(e);
579                    }
580                    finally {
581                            closeSession(session);
582                    }
583    
584                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
585    
586                    if (isNew || !ClassNameModelImpl.COLUMN_BITMASK_ENABLED) {
587                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
588                    }
589    
590                    EntityCacheUtil.putResult(ClassNameModelImpl.ENTITY_CACHE_ENABLED,
591                            ClassNameImpl.class, className.getPrimaryKey(), className);
592    
593                    clearUniqueFindersCache(className);
594                    cacheUniqueFindersCache(className);
595    
596                    return className;
597            }
598    
599            protected ClassName toUnwrappedModel(ClassName className) {
600                    if (className instanceof ClassNameImpl) {
601                            return className;
602                    }
603    
604                    ClassNameImpl classNameImpl = new ClassNameImpl();
605    
606                    classNameImpl.setNew(className.isNew());
607                    classNameImpl.setPrimaryKey(className.getPrimaryKey());
608    
609                    classNameImpl.setClassNameId(className.getClassNameId());
610                    classNameImpl.setValue(className.getValue());
611    
612                    return classNameImpl;
613            }
614    
615            /**
616             * Returns the class name with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
617             *
618             * @param primaryKey the primary key of the class name
619             * @return the class name
620             * @throws com.liferay.portal.NoSuchClassNameException if a class name with the primary key could not be found
621             * @throws SystemException if a system exception occurred
622             */
623            @Override
624            public ClassName findByPrimaryKey(Serializable primaryKey)
625                    throws NoSuchClassNameException, SystemException {
626                    ClassName className = fetchByPrimaryKey(primaryKey);
627    
628                    if (className == null) {
629                            if (_log.isWarnEnabled()) {
630                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
631                            }
632    
633                            throw new NoSuchClassNameException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
634                                    primaryKey);
635                    }
636    
637                    return className;
638            }
639    
640            /**
641             * Returns the class name with the primary key or throws a {@link com.liferay.portal.NoSuchClassNameException} if it could not be found.
642             *
643             * @param classNameId the primary key of the class name
644             * @return the class name
645             * @throws com.liferay.portal.NoSuchClassNameException if a class name with the primary key could not be found
646             * @throws SystemException if a system exception occurred
647             */
648            @Override
649            public ClassName findByPrimaryKey(long classNameId)
650                    throws NoSuchClassNameException, SystemException {
651                    return findByPrimaryKey((Serializable)classNameId);
652            }
653    
654            /**
655             * Returns the class name with the primary key or returns <code>null</code> if it could not be found.
656             *
657             * @param primaryKey the primary key of the class name
658             * @return the class name, or <code>null</code> if a class name with the primary key could not be found
659             * @throws SystemException if a system exception occurred
660             */
661            @Override
662            public ClassName fetchByPrimaryKey(Serializable primaryKey)
663                    throws SystemException {
664                    ClassName className = (ClassName)EntityCacheUtil.getResult(ClassNameModelImpl.ENTITY_CACHE_ENABLED,
665                                    ClassNameImpl.class, primaryKey);
666    
667                    if (className == _nullClassName) {
668                            return null;
669                    }
670    
671                    if (className == null) {
672                            Session session = null;
673    
674                            try {
675                                    session = openSession();
676    
677                                    className = (ClassName)session.get(ClassNameImpl.class,
678                                                    primaryKey);
679    
680                                    if (className != null) {
681                                            cacheResult(className);
682                                    }
683                                    else {
684                                            EntityCacheUtil.putResult(ClassNameModelImpl.ENTITY_CACHE_ENABLED,
685                                                    ClassNameImpl.class, primaryKey, _nullClassName);
686                                    }
687                            }
688                            catch (Exception e) {
689                                    EntityCacheUtil.removeResult(ClassNameModelImpl.ENTITY_CACHE_ENABLED,
690                                            ClassNameImpl.class, primaryKey);
691    
692                                    throw processException(e);
693                            }
694                            finally {
695                                    closeSession(session);
696                            }
697                    }
698    
699                    return className;
700            }
701    
702            /**
703             * Returns the class name with the primary key or returns <code>null</code> if it could not be found.
704             *
705             * @param classNameId the primary key of the class name
706             * @return the class name, or <code>null</code> if a class name with the primary key could not be found
707             * @throws SystemException if a system exception occurred
708             */
709            @Override
710            public ClassName fetchByPrimaryKey(long classNameId)
711                    throws SystemException {
712                    return fetchByPrimaryKey((Serializable)classNameId);
713            }
714    
715            /**
716             * Returns all the class names.
717             *
718             * @return the class names
719             * @throws SystemException if a system exception occurred
720             */
721            @Override
722            public List<ClassName> findAll() throws SystemException {
723                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
724            }
725    
726            /**
727             * Returns a range of all the class names.
728             *
729             * <p>
730             * 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.
731             * </p>
732             *
733             * @param start the lower bound of the range of class names
734             * @param end the upper bound of the range of class names (not inclusive)
735             * @return the range of class names
736             * @throws SystemException if a system exception occurred
737             */
738            @Override
739            public List<ClassName> findAll(int start, int end)
740                    throws SystemException {
741                    return findAll(start, end, null);
742            }
743    
744            /**
745             * Returns an ordered range of all the class names.
746             *
747             * <p>
748             * 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.
749             * </p>
750             *
751             * @param start the lower bound of the range of class names
752             * @param end the upper bound of the range of class names (not inclusive)
753             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
754             * @return the ordered range of class names
755             * @throws SystemException if a system exception occurred
756             */
757            @Override
758            public List<ClassName> findAll(int start, int end,
759                    OrderByComparator orderByComparator) throws SystemException {
760                    boolean pagination = true;
761                    FinderPath finderPath = null;
762                    Object[] finderArgs = null;
763    
764                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
765                                    (orderByComparator == null)) {
766                            pagination = false;
767                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
768                            finderArgs = FINDER_ARGS_EMPTY;
769                    }
770                    else {
771                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
772                            finderArgs = new Object[] { start, end, orderByComparator };
773                    }
774    
775                    List<ClassName> list = (List<ClassName>)FinderCacheUtil.getResult(finderPath,
776                                    finderArgs, this);
777    
778                    if (list == null) {
779                            StringBundler query = null;
780                            String sql = null;
781    
782                            if (orderByComparator != null) {
783                                    query = new StringBundler(2 +
784                                                    (orderByComparator.getOrderByFields().length * 3));
785    
786                                    query.append(_SQL_SELECT_CLASSNAME);
787    
788                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
789                                            orderByComparator);
790    
791                                    sql = query.toString();
792                            }
793                            else {
794                                    sql = _SQL_SELECT_CLASSNAME;
795    
796                                    if (pagination) {
797                                            sql = sql.concat(ClassNameModelImpl.ORDER_BY_JPQL);
798                                    }
799                            }
800    
801                            Session session = null;
802    
803                            try {
804                                    session = openSession();
805    
806                                    Query q = session.createQuery(sql);
807    
808                                    if (!pagination) {
809                                            list = (List<ClassName>)QueryUtil.list(q, getDialect(),
810                                                            start, end, false);
811    
812                                            Collections.sort(list);
813    
814                                            list = new UnmodifiableList<ClassName>(list);
815                                    }
816                                    else {
817                                            list = (List<ClassName>)QueryUtil.list(q, getDialect(),
818                                                            start, end);
819                                    }
820    
821                                    cacheResult(list);
822    
823                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
824                            }
825                            catch (Exception e) {
826                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
827    
828                                    throw processException(e);
829                            }
830                            finally {
831                                    closeSession(session);
832                            }
833                    }
834    
835                    return list;
836            }
837    
838            /**
839             * Removes all the class names from the database.
840             *
841             * @throws SystemException if a system exception occurred
842             */
843            @Override
844            public void removeAll() throws SystemException {
845                    for (ClassName className : findAll()) {
846                            remove(className);
847                    }
848            }
849    
850            /**
851             * Returns the number of class names.
852             *
853             * @return the number of class names
854             * @throws SystemException if a system exception occurred
855             */
856            @Override
857            public int countAll() throws SystemException {
858                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
859                                    FINDER_ARGS_EMPTY, this);
860    
861                    if (count == null) {
862                            Session session = null;
863    
864                            try {
865                                    session = openSession();
866    
867                                    Query q = session.createQuery(_SQL_COUNT_CLASSNAME);
868    
869                                    count = (Long)q.uniqueResult();
870    
871                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
872                                            FINDER_ARGS_EMPTY, count);
873                            }
874                            catch (Exception e) {
875                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
876                                            FINDER_ARGS_EMPTY);
877    
878                                    throw processException(e);
879                            }
880                            finally {
881                                    closeSession(session);
882                            }
883                    }
884    
885                    return count.intValue();
886            }
887    
888            /**
889             * Initializes the class name persistence.
890             */
891            public void afterPropertiesSet() {
892                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
893                                            com.liferay.portal.util.PropsUtil.get(
894                                                    "value.object.listener.com.liferay.portal.model.ClassName")));
895    
896                    if (listenerClassNames.length > 0) {
897                            try {
898                                    List<ModelListener<ClassName>> listenersList = new ArrayList<ModelListener<ClassName>>();
899    
900                                    for (String listenerClassName : listenerClassNames) {
901                                            listenersList.add((ModelListener<ClassName>)InstanceFactory.newInstance(
902                                                            getClassLoader(), listenerClassName));
903                                    }
904    
905                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
906                            }
907                            catch (Exception e) {
908                                    _log.error(e);
909                            }
910                    }
911            }
912    
913            public void destroy() {
914                    EntityCacheUtil.removeCache(ClassNameImpl.class.getName());
915                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
916                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
917                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
918            }
919    
920            private static final String _SQL_SELECT_CLASSNAME = "SELECT className FROM ClassName className";
921            private static final String _SQL_SELECT_CLASSNAME_WHERE = "SELECT className FROM ClassName className WHERE ";
922            private static final String _SQL_COUNT_CLASSNAME = "SELECT COUNT(className) FROM ClassName className";
923            private static final String _SQL_COUNT_CLASSNAME_WHERE = "SELECT COUNT(className) FROM ClassName className WHERE ";
924            private static final String _ORDER_BY_ENTITY_ALIAS = "className.";
925            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ClassName exists with the primary key ";
926            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ClassName exists with the key {";
927            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
928            private static Log _log = LogFactoryUtil.getLog(ClassNamePersistenceImpl.class);
929            private static ClassName _nullClassName = new ClassNameImpl() {
930                            @Override
931                            public Object clone() {
932                                    return this;
933                            }
934    
935                            @Override
936                            public CacheModel<ClassName> toCacheModel() {
937                                    return _nullClassNameCacheModel;
938                            }
939                    };
940    
941            private static CacheModel<ClassName> _nullClassNameCacheModel = new CacheModel<ClassName>() {
942                            @Override
943                            public ClassName toEntityModel() {
944                                    return _nullClassName;
945                            }
946                    };
947    }