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