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