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