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