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