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