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.NoSuchModelException;
018    import com.liferay.portal.NoSuchShardException;
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.ModelListener;
040    import com.liferay.portal.model.Shard;
041    import com.liferay.portal.model.impl.ShardImpl;
042    import com.liferay.portal.model.impl.ShardModelImpl;
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 shard 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 ShardPersistence
060     * @see ShardUtil
061     * @generated
062     */
063    public class ShardPersistenceImpl extends BasePersistenceImpl<Shard>
064            implements ShardPersistence {
065            /*
066             * NOTE FOR DEVELOPERS:
067             *
068             * Never modify or reference this class directly. Always use {@link ShardUtil} to access the shard persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
069             */
070            public static final String FINDER_CLASS_NAME_ENTITY = ShardImpl.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(ShardModelImpl.ENTITY_CACHE_ENABLED,
076                            ShardModelImpl.FINDER_CACHE_ENABLED, ShardImpl.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(ShardModelImpl.ENTITY_CACHE_ENABLED,
079                            ShardModelImpl.FINDER_CACHE_ENABLED, ShardImpl.class,
080                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
081            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ShardModelImpl.ENTITY_CACHE_ENABLED,
082                            ShardModelImpl.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_NAME = new FinderPath(ShardModelImpl.ENTITY_CACHE_ENABLED,
085                            ShardModelImpl.FINDER_CACHE_ENABLED, ShardImpl.class,
086                            FINDER_CLASS_NAME_ENTITY, "fetchByName",
087                            new String[] { String.class.getName() },
088                            ShardModelImpl.NAME_COLUMN_BITMASK);
089            public static final FinderPath FINDER_PATH_COUNT_BY_NAME = new FinderPath(ShardModelImpl.ENTITY_CACHE_ENABLED,
090                            ShardModelImpl.FINDER_CACHE_ENABLED, Long.class,
091                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByName",
092                            new String[] { String.class.getName() });
093    
094            /**
095             * Returns the shard where name = &#63; or throws a {@link com.liferay.portal.NoSuchShardException} if it could not be found.
096             *
097             * @param name the name
098             * @return the matching shard
099             * @throws com.liferay.portal.NoSuchShardException if a matching shard could not be found
100             * @throws SystemException if a system exception occurred
101             */
102            public Shard findByName(String name)
103                    throws NoSuchShardException, SystemException {
104                    Shard shard = fetchByName(name);
105    
106                    if (shard == null) {
107                            StringBundler msg = new StringBundler(4);
108    
109                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
110    
111                            msg.append("name=");
112                            msg.append(name);
113    
114                            msg.append(StringPool.CLOSE_CURLY_BRACE);
115    
116                            if (_log.isWarnEnabled()) {
117                                    _log.warn(msg.toString());
118                            }
119    
120                            throw new NoSuchShardException(msg.toString());
121                    }
122    
123                    return shard;
124            }
125    
126            /**
127             * Returns the shard where name = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
128             *
129             * @param name the name
130             * @return the matching shard, or <code>null</code> if a matching shard could not be found
131             * @throws SystemException if a system exception occurred
132             */
133            public Shard fetchByName(String name) throws SystemException {
134                    return fetchByName(name, true);
135            }
136    
137            /**
138             * Returns the shard where name = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
139             *
140             * @param name the name
141             * @param retrieveFromCache whether to use the finder cache
142             * @return the matching shard, or <code>null</code> if a matching shard could not be found
143             * @throws SystemException if a system exception occurred
144             */
145            public Shard fetchByName(String name, boolean retrieveFromCache)
146                    throws SystemException {
147                    Object[] finderArgs = new Object[] { name };
148    
149                    Object result = null;
150    
151                    if (retrieveFromCache) {
152                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_NAME,
153                                            finderArgs, this);
154                    }
155    
156                    if (result instanceof Shard) {
157                            Shard shard = (Shard)result;
158    
159                            if (!Validator.equals(name, shard.getName())) {
160                                    result = null;
161                            }
162                    }
163    
164                    if (result == null) {
165                            StringBundler query = new StringBundler(3);
166    
167                            query.append(_SQL_SELECT_SHARD_WHERE);
168    
169                            if (name == null) {
170                                    query.append(_FINDER_COLUMN_NAME_NAME_1);
171                            }
172                            else {
173                                    if (name.equals(StringPool.BLANK)) {
174                                            query.append(_FINDER_COLUMN_NAME_NAME_3);
175                                    }
176                                    else {
177                                            query.append(_FINDER_COLUMN_NAME_NAME_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 (name != null) {
193                                            qPos.add(name);
194                                    }
195    
196                                    List<Shard> list = q.list();
197    
198                                    if (list.isEmpty()) {
199                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NAME,
200                                                    finderArgs, list);
201                                    }
202                                    else {
203                                            if ((list.size() > 1) && _log.isWarnEnabled()) {
204                                                    _log.warn(
205                                                            "ShardPersistenceImpl.fetchByName(String, boolean) with parameters (" +
206                                                            StringUtil.merge(finderArgs) +
207                                                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
208                                            }
209    
210                                            Shard shard = list.get(0);
211    
212                                            result = shard;
213    
214                                            cacheResult(shard);
215    
216                                            if ((shard.getName() == null) ||
217                                                            !shard.getName().equals(name)) {
218                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NAME,
219                                                            finderArgs, shard);
220                                            }
221                                    }
222                            }
223                            catch (Exception e) {
224                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_NAME,
225                                            finderArgs);
226    
227                                    throw processException(e);
228                            }
229                            finally {
230                                    closeSession(session);
231                            }
232                    }
233    
234                    if (result instanceof List<?>) {
235                            return null;
236                    }
237                    else {
238                            return (Shard)result;
239                    }
240            }
241    
242            /**
243             * Removes the shard where name = &#63; from the database.
244             *
245             * @param name the name
246             * @return the shard that was removed
247             * @throws SystemException if a system exception occurred
248             */
249            public Shard removeByName(String name)
250                    throws NoSuchShardException, SystemException {
251                    Shard shard = findByName(name);
252    
253                    return remove(shard);
254            }
255    
256            /**
257             * Returns the number of shards where name = &#63;.
258             *
259             * @param name the name
260             * @return the number of matching shards
261             * @throws SystemException if a system exception occurred
262             */
263            public int countByName(String name) throws SystemException {
264                    FinderPath finderPath = FINDER_PATH_COUNT_BY_NAME;
265    
266                    Object[] finderArgs = new Object[] { name };
267    
268                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
269                                    this);
270    
271                    if (count == null) {
272                            StringBundler query = new StringBundler(2);
273    
274                            query.append(_SQL_COUNT_SHARD_WHERE);
275    
276                            if (name == null) {
277                                    query.append(_FINDER_COLUMN_NAME_NAME_1);
278                            }
279                            else {
280                                    if (name.equals(StringPool.BLANK)) {
281                                            query.append(_FINDER_COLUMN_NAME_NAME_3);
282                                    }
283                                    else {
284                                            query.append(_FINDER_COLUMN_NAME_NAME_2);
285                                    }
286                            }
287    
288                            String sql = query.toString();
289    
290                            Session session = null;
291    
292                            try {
293                                    session = openSession();
294    
295                                    Query q = session.createQuery(sql);
296    
297                                    QueryPos qPos = QueryPos.getInstance(q);
298    
299                                    if (name != null) {
300                                            qPos.add(name);
301                                    }
302    
303                                    count = (Long)q.uniqueResult();
304    
305                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
306                            }
307                            catch (Exception e) {
308                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
309    
310                                    throw processException(e);
311                            }
312                            finally {
313                                    closeSession(session);
314                            }
315                    }
316    
317                    return count.intValue();
318            }
319    
320            private static final String _FINDER_COLUMN_NAME_NAME_1 = "shard.name IS NULL";
321            private static final String _FINDER_COLUMN_NAME_NAME_2 = "shard.name = ?";
322            private static final String _FINDER_COLUMN_NAME_NAME_3 = "(shard.name IS NULL OR shard.name = ?)";
323            public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(ShardModelImpl.ENTITY_CACHE_ENABLED,
324                            ShardModelImpl.FINDER_CACHE_ENABLED, ShardImpl.class,
325                            FINDER_CLASS_NAME_ENTITY, "fetchByC_C",
326                            new String[] { Long.class.getName(), Long.class.getName() },
327                            ShardModelImpl.CLASSNAMEID_COLUMN_BITMASK |
328                            ShardModelImpl.CLASSPK_COLUMN_BITMASK);
329            public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(ShardModelImpl.ENTITY_CACHE_ENABLED,
330                            ShardModelImpl.FINDER_CACHE_ENABLED, Long.class,
331                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C",
332                            new String[] { Long.class.getName(), Long.class.getName() });
333    
334            /**
335             * Returns the shard where classNameId = &#63; and classPK = &#63; or throws a {@link com.liferay.portal.NoSuchShardException} if it could not be found.
336             *
337             * @param classNameId the class name ID
338             * @param classPK the class p k
339             * @return the matching shard
340             * @throws com.liferay.portal.NoSuchShardException if a matching shard could not be found
341             * @throws SystemException if a system exception occurred
342             */
343            public Shard findByC_C(long classNameId, long classPK)
344                    throws NoSuchShardException, SystemException {
345                    Shard shard = fetchByC_C(classNameId, classPK);
346    
347                    if (shard == null) {
348                            StringBundler msg = new StringBundler(6);
349    
350                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
351    
352                            msg.append("classNameId=");
353                            msg.append(classNameId);
354    
355                            msg.append(", classPK=");
356                            msg.append(classPK);
357    
358                            msg.append(StringPool.CLOSE_CURLY_BRACE);
359    
360                            if (_log.isWarnEnabled()) {
361                                    _log.warn(msg.toString());
362                            }
363    
364                            throw new NoSuchShardException(msg.toString());
365                    }
366    
367                    return shard;
368            }
369    
370            /**
371             * Returns the shard where classNameId = &#63; and classPK = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
372             *
373             * @param classNameId the class name ID
374             * @param classPK the class p k
375             * @return the matching shard, or <code>null</code> if a matching shard could not be found
376             * @throws SystemException if a system exception occurred
377             */
378            public Shard fetchByC_C(long classNameId, long classPK)
379                    throws SystemException {
380                    return fetchByC_C(classNameId, classPK, true);
381            }
382    
383            /**
384             * Returns the shard where classNameId = &#63; and classPK = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
385             *
386             * @param classNameId the class name ID
387             * @param classPK the class p k
388             * @param retrieveFromCache whether to use the finder cache
389             * @return the matching shard, or <code>null</code> if a matching shard could not be found
390             * @throws SystemException if a system exception occurred
391             */
392            public Shard fetchByC_C(long classNameId, long classPK,
393                    boolean retrieveFromCache) throws SystemException {
394                    Object[] finderArgs = new Object[] { classNameId, classPK };
395    
396                    Object result = null;
397    
398                    if (retrieveFromCache) {
399                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
400                                            finderArgs, this);
401                    }
402    
403                    if (result instanceof Shard) {
404                            Shard shard = (Shard)result;
405    
406                            if ((classNameId != shard.getClassNameId()) ||
407                                            (classPK != shard.getClassPK())) {
408                                    result = null;
409                            }
410                    }
411    
412                    if (result == null) {
413                            StringBundler query = new StringBundler(4);
414    
415                            query.append(_SQL_SELECT_SHARD_WHERE);
416    
417                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
418    
419                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
420    
421                            String sql = query.toString();
422    
423                            Session session = null;
424    
425                            try {
426                                    session = openSession();
427    
428                                    Query q = session.createQuery(sql);
429    
430                                    QueryPos qPos = QueryPos.getInstance(q);
431    
432                                    qPos.add(classNameId);
433    
434                                    qPos.add(classPK);
435    
436                                    List<Shard> list = q.list();
437    
438                                    if (list.isEmpty()) {
439                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
440                                                    finderArgs, list);
441                                    }
442                                    else {
443                                            if ((list.size() > 1) && _log.isWarnEnabled()) {
444                                                    _log.warn(
445                                                            "ShardPersistenceImpl.fetchByC_C(long, long, boolean) with parameters (" +
446                                                            StringUtil.merge(finderArgs) +
447                                                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
448                                            }
449    
450                                            Shard shard = list.get(0);
451    
452                                            result = shard;
453    
454                                            cacheResult(shard);
455    
456                                            if ((shard.getClassNameId() != classNameId) ||
457                                                            (shard.getClassPK() != classPK)) {
458                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
459                                                            finderArgs, shard);
460                                            }
461                                    }
462                            }
463                            catch (Exception e) {
464                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
465                                            finderArgs);
466    
467                                    throw processException(e);
468                            }
469                            finally {
470                                    closeSession(session);
471                            }
472                    }
473    
474                    if (result instanceof List<?>) {
475                            return null;
476                    }
477                    else {
478                            return (Shard)result;
479                    }
480            }
481    
482            /**
483             * Removes the shard where classNameId = &#63; and classPK = &#63; from the database.
484             *
485             * @param classNameId the class name ID
486             * @param classPK the class p k
487             * @return the shard that was removed
488             * @throws SystemException if a system exception occurred
489             */
490            public Shard removeByC_C(long classNameId, long classPK)
491                    throws NoSuchShardException, SystemException {
492                    Shard shard = findByC_C(classNameId, classPK);
493    
494                    return remove(shard);
495            }
496    
497            /**
498             * Returns the number of shards where classNameId = &#63; and classPK = &#63;.
499             *
500             * @param classNameId the class name ID
501             * @param classPK the class p k
502             * @return the number of matching shards
503             * @throws SystemException if a system exception occurred
504             */
505            public int countByC_C(long classNameId, long classPK)
506                    throws SystemException {
507                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C;
508    
509                    Object[] finderArgs = new Object[] { classNameId, classPK };
510    
511                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
512                                    this);
513    
514                    if (count == null) {
515                            StringBundler query = new StringBundler(3);
516    
517                            query.append(_SQL_COUNT_SHARD_WHERE);
518    
519                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
520    
521                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
522    
523                            String sql = query.toString();
524    
525                            Session session = null;
526    
527                            try {
528                                    session = openSession();
529    
530                                    Query q = session.createQuery(sql);
531    
532                                    QueryPos qPos = QueryPos.getInstance(q);
533    
534                                    qPos.add(classNameId);
535    
536                                    qPos.add(classPK);
537    
538                                    count = (Long)q.uniqueResult();
539    
540                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
541                            }
542                            catch (Exception e) {
543                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
544    
545                                    throw processException(e);
546                            }
547                            finally {
548                                    closeSession(session);
549                            }
550                    }
551    
552                    return count.intValue();
553            }
554    
555            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "shard.classNameId = ? AND ";
556            private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "shard.classPK = ?";
557    
558            /**
559             * Caches the shard in the entity cache if it is enabled.
560             *
561             * @param shard the shard
562             */
563            public void cacheResult(Shard shard) {
564                    EntityCacheUtil.putResult(ShardModelImpl.ENTITY_CACHE_ENABLED,
565                            ShardImpl.class, shard.getPrimaryKey(), shard);
566    
567                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NAME,
568                            new Object[] { shard.getName() }, shard);
569    
570                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
571                            new Object[] {
572                                    Long.valueOf(shard.getClassNameId()),
573                                    Long.valueOf(shard.getClassPK())
574                            }, shard);
575    
576                    shard.resetOriginalValues();
577            }
578    
579            /**
580             * Caches the shards in the entity cache if it is enabled.
581             *
582             * @param shards the shards
583             */
584            public void cacheResult(List<Shard> shards) {
585                    for (Shard shard : shards) {
586                            if (EntityCacheUtil.getResult(ShardModelImpl.ENTITY_CACHE_ENABLED,
587                                                    ShardImpl.class, shard.getPrimaryKey()) == null) {
588                                    cacheResult(shard);
589                            }
590                            else {
591                                    shard.resetOriginalValues();
592                            }
593                    }
594            }
595    
596            /**
597             * Clears the cache for all shards.
598             *
599             * <p>
600             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
601             * </p>
602             */
603            @Override
604            public void clearCache() {
605                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
606                            CacheRegistryUtil.clear(ShardImpl.class.getName());
607                    }
608    
609                    EntityCacheUtil.clearCache(ShardImpl.class.getName());
610    
611                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
612                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
613                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
614            }
615    
616            /**
617             * Clears the cache for the shard.
618             *
619             * <p>
620             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
621             * </p>
622             */
623            @Override
624            public void clearCache(Shard shard) {
625                    EntityCacheUtil.removeResult(ShardModelImpl.ENTITY_CACHE_ENABLED,
626                            ShardImpl.class, shard.getPrimaryKey());
627    
628                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
629                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
630    
631                    clearUniqueFindersCache(shard);
632            }
633    
634            @Override
635            public void clearCache(List<Shard> shards) {
636                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
637                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
638    
639                    for (Shard shard : shards) {
640                            EntityCacheUtil.removeResult(ShardModelImpl.ENTITY_CACHE_ENABLED,
641                                    ShardImpl.class, shard.getPrimaryKey());
642    
643                            clearUniqueFindersCache(shard);
644                    }
645            }
646    
647            protected void cacheUniqueFindersCache(Shard shard) {
648                    if (shard.isNew()) {
649                            Object[] args = new Object[] { shard.getName() };
650    
651                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_NAME, args,
652                                    Long.valueOf(1));
653                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NAME, args, shard);
654    
655                            args = new Object[] {
656                                            Long.valueOf(shard.getClassNameId()),
657                                            Long.valueOf(shard.getClassPK())
658                                    };
659    
660                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, args,
661                                    Long.valueOf(1));
662                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C, args, shard);
663                    }
664                    else {
665                            ShardModelImpl shardModelImpl = (ShardModelImpl)shard;
666    
667                            if ((shardModelImpl.getColumnBitmask() &
668                                            FINDER_PATH_FETCH_BY_NAME.getColumnBitmask()) != 0) {
669                                    Object[] args = new Object[] { shard.getName() };
670    
671                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_NAME, args,
672                                            Long.valueOf(1));
673                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NAME, args, shard);
674                            }
675    
676                            if ((shardModelImpl.getColumnBitmask() &
677                                            FINDER_PATH_FETCH_BY_C_C.getColumnBitmask()) != 0) {
678                                    Object[] args = new Object[] {
679                                                    Long.valueOf(shard.getClassNameId()),
680                                                    Long.valueOf(shard.getClassPK())
681                                            };
682    
683                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, args,
684                                            Long.valueOf(1));
685                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C, args, shard);
686                            }
687                    }
688            }
689    
690            protected void clearUniqueFindersCache(Shard shard) {
691                    ShardModelImpl shardModelImpl = (ShardModelImpl)shard;
692    
693                    Object[] args = new Object[] { shard.getName() };
694    
695                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_NAME, args);
696                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_NAME, args);
697    
698                    if ((shardModelImpl.getColumnBitmask() &
699                                    FINDER_PATH_FETCH_BY_NAME.getColumnBitmask()) != 0) {
700                            args = new Object[] { shardModelImpl.getOriginalName() };
701    
702                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_NAME, args);
703                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_NAME, args);
704                    }
705    
706                    args = new Object[] {
707                                    Long.valueOf(shard.getClassNameId()),
708                                    Long.valueOf(shard.getClassPK())
709                            };
710    
711                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
712                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C, args);
713    
714                    if ((shardModelImpl.getColumnBitmask() &
715                                    FINDER_PATH_FETCH_BY_C_C.getColumnBitmask()) != 0) {
716                            args = new Object[] {
717                                            Long.valueOf(shardModelImpl.getOriginalClassNameId()),
718                                            Long.valueOf(shardModelImpl.getOriginalClassPK())
719                                    };
720    
721                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
722                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C, args);
723                    }
724            }
725    
726            /**
727             * Creates a new shard with the primary key. Does not add the shard to the database.
728             *
729             * @param shardId the primary key for the new shard
730             * @return the new shard
731             */
732            public Shard create(long shardId) {
733                    Shard shard = new ShardImpl();
734    
735                    shard.setNew(true);
736                    shard.setPrimaryKey(shardId);
737    
738                    return shard;
739            }
740    
741            /**
742             * Removes the shard with the primary key from the database. Also notifies the appropriate model listeners.
743             *
744             * @param shardId the primary key of the shard
745             * @return the shard that was removed
746             * @throws com.liferay.portal.NoSuchShardException if a shard with the primary key could not be found
747             * @throws SystemException if a system exception occurred
748             */
749            public Shard remove(long shardId)
750                    throws NoSuchShardException, SystemException {
751                    return remove(Long.valueOf(shardId));
752            }
753    
754            /**
755             * Removes the shard with the primary key from the database. Also notifies the appropriate model listeners.
756             *
757             * @param primaryKey the primary key of the shard
758             * @return the shard that was removed
759             * @throws com.liferay.portal.NoSuchShardException if a shard with the primary key could not be found
760             * @throws SystemException if a system exception occurred
761             */
762            @Override
763            public Shard remove(Serializable primaryKey)
764                    throws NoSuchShardException, SystemException {
765                    Session session = null;
766    
767                    try {
768                            session = openSession();
769    
770                            Shard shard = (Shard)session.get(ShardImpl.class, primaryKey);
771    
772                            if (shard == null) {
773                                    if (_log.isWarnEnabled()) {
774                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
775                                    }
776    
777                                    throw new NoSuchShardException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
778                                            primaryKey);
779                            }
780    
781                            return remove(shard);
782                    }
783                    catch (NoSuchShardException nsee) {
784                            throw nsee;
785                    }
786                    catch (Exception e) {
787                            throw processException(e);
788                    }
789                    finally {
790                            closeSession(session);
791                    }
792            }
793    
794            @Override
795            protected Shard removeImpl(Shard shard) throws SystemException {
796                    shard = toUnwrappedModel(shard);
797    
798                    Session session = null;
799    
800                    try {
801                            session = openSession();
802    
803                            if (!session.contains(shard)) {
804                                    shard = (Shard)session.get(ShardImpl.class,
805                                                    shard.getPrimaryKeyObj());
806                            }
807    
808                            if (shard != null) {
809                                    session.delete(shard);
810                            }
811                    }
812                    catch (Exception e) {
813                            throw processException(e);
814                    }
815                    finally {
816                            closeSession(session);
817                    }
818    
819                    if (shard != null) {
820                            clearCache(shard);
821                    }
822    
823                    return shard;
824            }
825    
826            @Override
827            public Shard updateImpl(com.liferay.portal.model.Shard shard)
828                    throws SystemException {
829                    shard = toUnwrappedModel(shard);
830    
831                    boolean isNew = shard.isNew();
832    
833                    Session session = null;
834    
835                    try {
836                            session = openSession();
837    
838                            if (shard.isNew()) {
839                                    session.save(shard);
840    
841                                    shard.setNew(false);
842                            }
843                            else {
844                                    session.merge(shard);
845                            }
846                    }
847                    catch (Exception e) {
848                            throw processException(e);
849                    }
850                    finally {
851                            closeSession(session);
852                    }
853    
854                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
855    
856                    if (isNew || !ShardModelImpl.COLUMN_BITMASK_ENABLED) {
857                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
858                    }
859    
860                    EntityCacheUtil.putResult(ShardModelImpl.ENTITY_CACHE_ENABLED,
861                            ShardImpl.class, shard.getPrimaryKey(), shard);
862    
863                    clearUniqueFindersCache(shard);
864                    cacheUniqueFindersCache(shard);
865    
866                    return shard;
867            }
868    
869            protected Shard toUnwrappedModel(Shard shard) {
870                    if (shard instanceof ShardImpl) {
871                            return shard;
872                    }
873    
874                    ShardImpl shardImpl = new ShardImpl();
875    
876                    shardImpl.setNew(shard.isNew());
877                    shardImpl.setPrimaryKey(shard.getPrimaryKey());
878    
879                    shardImpl.setShardId(shard.getShardId());
880                    shardImpl.setClassNameId(shard.getClassNameId());
881                    shardImpl.setClassPK(shard.getClassPK());
882                    shardImpl.setName(shard.getName());
883    
884                    return shardImpl;
885            }
886    
887            /**
888             * Returns the shard with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
889             *
890             * @param primaryKey the primary key of the shard
891             * @return the shard
892             * @throws com.liferay.portal.NoSuchModelException if a shard with the primary key could not be found
893             * @throws SystemException if a system exception occurred
894             */
895            @Override
896            public Shard findByPrimaryKey(Serializable primaryKey)
897                    throws NoSuchModelException, SystemException {
898                    return findByPrimaryKey(((Long)primaryKey).longValue());
899            }
900    
901            /**
902             * Returns the shard with the primary key or throws a {@link com.liferay.portal.NoSuchShardException} if it could not be found.
903             *
904             * @param shardId the primary key of the shard
905             * @return the shard
906             * @throws com.liferay.portal.NoSuchShardException if a shard with the primary key could not be found
907             * @throws SystemException if a system exception occurred
908             */
909            public Shard findByPrimaryKey(long shardId)
910                    throws NoSuchShardException, SystemException {
911                    Shard shard = fetchByPrimaryKey(shardId);
912    
913                    if (shard == null) {
914                            if (_log.isWarnEnabled()) {
915                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + shardId);
916                            }
917    
918                            throw new NoSuchShardException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
919                                    shardId);
920                    }
921    
922                    return shard;
923            }
924    
925            /**
926             * Returns the shard with the primary key or returns <code>null</code> if it could not be found.
927             *
928             * @param primaryKey the primary key of the shard
929             * @return the shard, or <code>null</code> if a shard with the primary key could not be found
930             * @throws SystemException if a system exception occurred
931             */
932            @Override
933            public Shard fetchByPrimaryKey(Serializable primaryKey)
934                    throws SystemException {
935                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
936            }
937    
938            /**
939             * Returns the shard with the primary key or returns <code>null</code> if it could not be found.
940             *
941             * @param shardId the primary key of the shard
942             * @return the shard, or <code>null</code> if a shard with the primary key could not be found
943             * @throws SystemException if a system exception occurred
944             */
945            public Shard fetchByPrimaryKey(long shardId) throws SystemException {
946                    Shard shard = (Shard)EntityCacheUtil.getResult(ShardModelImpl.ENTITY_CACHE_ENABLED,
947                                    ShardImpl.class, shardId);
948    
949                    if (shard == _nullShard) {
950                            return null;
951                    }
952    
953                    if (shard == null) {
954                            Session session = null;
955    
956                            try {
957                                    session = openSession();
958    
959                                    shard = (Shard)session.get(ShardImpl.class,
960                                                    Long.valueOf(shardId));
961    
962                                    if (shard != null) {
963                                            cacheResult(shard);
964                                    }
965                                    else {
966                                            EntityCacheUtil.putResult(ShardModelImpl.ENTITY_CACHE_ENABLED,
967                                                    ShardImpl.class, shardId, _nullShard);
968                                    }
969                            }
970                            catch (Exception e) {
971                                    EntityCacheUtil.removeResult(ShardModelImpl.ENTITY_CACHE_ENABLED,
972                                            ShardImpl.class, shardId);
973    
974                                    throw processException(e);
975                            }
976                            finally {
977                                    closeSession(session);
978                            }
979                    }
980    
981                    return shard;
982            }
983    
984            /**
985             * Returns all the shards.
986             *
987             * @return the shards
988             * @throws SystemException if a system exception occurred
989             */
990            public List<Shard> findAll() throws SystemException {
991                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
992            }
993    
994            /**
995             * Returns a range of all the shards.
996             *
997             * <p>
998             * 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.ShardModelImpl}. 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.
999             * </p>
1000             *
1001             * @param start the lower bound of the range of shards
1002             * @param end the upper bound of the range of shards (not inclusive)
1003             * @return the range of shards
1004             * @throws SystemException if a system exception occurred
1005             */
1006            public List<Shard> findAll(int start, int end) throws SystemException {
1007                    return findAll(start, end, null);
1008            }
1009    
1010            /**
1011             * Returns an ordered range of all the shards.
1012             *
1013             * <p>
1014             * 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.ShardModelImpl}. 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.
1015             * </p>
1016             *
1017             * @param start the lower bound of the range of shards
1018             * @param end the upper bound of the range of shards (not inclusive)
1019             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1020             * @return the ordered range of shards
1021             * @throws SystemException if a system exception occurred
1022             */
1023            public List<Shard> findAll(int start, int end,
1024                    OrderByComparator orderByComparator) throws SystemException {
1025                    boolean pagination = true;
1026                    FinderPath finderPath = null;
1027                    Object[] finderArgs = null;
1028    
1029                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1030                                    (orderByComparator == null)) {
1031                            pagination = false;
1032                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1033                            finderArgs = FINDER_ARGS_EMPTY;
1034                    }
1035                    else {
1036                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1037                            finderArgs = new Object[] { start, end, orderByComparator };
1038                    }
1039    
1040                    List<Shard> list = (List<Shard>)FinderCacheUtil.getResult(finderPath,
1041                                    finderArgs, this);
1042    
1043                    if (list == null) {
1044                            StringBundler query = null;
1045                            String sql = null;
1046    
1047                            if (orderByComparator != null) {
1048                                    query = new StringBundler(2 +
1049                                                    (orderByComparator.getOrderByFields().length * 3));
1050    
1051                                    query.append(_SQL_SELECT_SHARD);
1052    
1053                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1054                                            orderByComparator);
1055    
1056                                    sql = query.toString();
1057                            }
1058                            else {
1059                                    sql = _SQL_SELECT_SHARD;
1060    
1061                                    if (pagination) {
1062                                            sql = sql.concat(ShardModelImpl.ORDER_BY_JPQL);
1063                                    }
1064                            }
1065    
1066                            Session session = null;
1067    
1068                            try {
1069                                    session = openSession();
1070    
1071                                    Query q = session.createQuery(sql);
1072    
1073                                    if (!pagination) {
1074                                            list = (List<Shard>)QueryUtil.list(q, getDialect(), start,
1075                                                            end, false);
1076    
1077                                            Collections.sort(list);
1078    
1079                                            list = new UnmodifiableList<Shard>(list);
1080                                    }
1081                                    else {
1082                                            list = (List<Shard>)QueryUtil.list(q, getDialect(), start,
1083                                                            end);
1084                                    }
1085    
1086                                    cacheResult(list);
1087    
1088                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1089                            }
1090                            catch (Exception e) {
1091                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1092    
1093                                    throw processException(e);
1094                            }
1095                            finally {
1096                                    closeSession(session);
1097                            }
1098                    }
1099    
1100                    return list;
1101            }
1102    
1103            /**
1104             * Removes all the shards from the database.
1105             *
1106             * @throws SystemException if a system exception occurred
1107             */
1108            public void removeAll() throws SystemException {
1109                    for (Shard shard : findAll()) {
1110                            remove(shard);
1111                    }
1112            }
1113    
1114            /**
1115             * Returns the number of shards.
1116             *
1117             * @return the number of shards
1118             * @throws SystemException if a system exception occurred
1119             */
1120            public int countAll() throws SystemException {
1121                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1122                                    FINDER_ARGS_EMPTY, this);
1123    
1124                    if (count == null) {
1125                            Session session = null;
1126    
1127                            try {
1128                                    session = openSession();
1129    
1130                                    Query q = session.createQuery(_SQL_COUNT_SHARD);
1131    
1132                                    count = (Long)q.uniqueResult();
1133    
1134                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1135                                            FINDER_ARGS_EMPTY, count);
1136                            }
1137                            catch (Exception e) {
1138                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1139                                            FINDER_ARGS_EMPTY);
1140    
1141                                    throw processException(e);
1142                            }
1143                            finally {
1144                                    closeSession(session);
1145                            }
1146                    }
1147    
1148                    return count.intValue();
1149            }
1150    
1151            /**
1152             * Initializes the shard persistence.
1153             */
1154            public void afterPropertiesSet() {
1155                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1156                                            com.liferay.portal.util.PropsUtil.get(
1157                                                    "value.object.listener.com.liferay.portal.model.Shard")));
1158    
1159                    if (listenerClassNames.length > 0) {
1160                            try {
1161                                    List<ModelListener<Shard>> listenersList = new ArrayList<ModelListener<Shard>>();
1162    
1163                                    for (String listenerClassName : listenerClassNames) {
1164                                            listenersList.add((ModelListener<Shard>)InstanceFactory.newInstance(
1165                                                            listenerClassName));
1166                                    }
1167    
1168                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1169                            }
1170                            catch (Exception e) {
1171                                    _log.error(e);
1172                            }
1173                    }
1174            }
1175    
1176            public void destroy() {
1177                    EntityCacheUtil.removeCache(ShardImpl.class.getName());
1178                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1179                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1180                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1181            }
1182    
1183            private static final String _SQL_SELECT_SHARD = "SELECT shard FROM Shard shard";
1184            private static final String _SQL_SELECT_SHARD_WHERE = "SELECT shard FROM Shard shard WHERE ";
1185            private static final String _SQL_COUNT_SHARD = "SELECT COUNT(shard) FROM Shard shard";
1186            private static final String _SQL_COUNT_SHARD_WHERE = "SELECT COUNT(shard) FROM Shard shard WHERE ";
1187            private static final String _ORDER_BY_ENTITY_ALIAS = "shard.";
1188            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Shard exists with the primary key ";
1189            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Shard exists with the key {";
1190            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1191            private static Log _log = LogFactoryUtil.getLog(ShardPersistenceImpl.class);
1192            private static Shard _nullShard = new ShardImpl() {
1193                            @Override
1194                            public Object clone() {
1195                                    return this;
1196                            }
1197    
1198                            @Override
1199                            public CacheModel<Shard> toCacheModel() {
1200                                    return _nullShardCacheModel;
1201                            }
1202                    };
1203    
1204            private static CacheModel<Shard> _nullShardCacheModel = new CacheModel<Shard>() {
1205                            public Shard toEntityModel() {
1206                                    return _nullShard;
1207                            }
1208                    };
1209    }