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