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