001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.getOriginalName() });
248    
249                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
250                            new Object[] {
251                                    new Long(shardModelImpl.getOriginalClassNameId()),
252                                    new Long(shardModelImpl.getOriginalClassPK())
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                            Session session = null;
491    
492                            try {
493                                    session = openSession();
494    
495                                    StringBundler query = new StringBundler(2);
496    
497                                    query.append(_SQL_SELECT_SHARD_WHERE);
498    
499                                    if (name == null) {
500                                            query.append(_FINDER_COLUMN_NAME_NAME_1);
501                                    }
502                                    else {
503                                            if (name.equals(StringPool.BLANK)) {
504                                                    query.append(_FINDER_COLUMN_NAME_NAME_3);
505                                            }
506                                            else {
507                                                    query.append(_FINDER_COLUMN_NAME_NAME_2);
508                                            }
509                                    }
510    
511                                    String sql = query.toString();
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.putResult(FINDER_PATH_FETCH_BY_NAME,
551                                                    finderArgs, new ArrayList<Shard>());
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                            Session session = null;
637    
638                            try {
639                                    session = openSession();
640    
641                                    StringBundler query = new StringBundler(3);
642    
643                                    query.append(_SQL_SELECT_SHARD_WHERE);
644    
645                                    query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
646    
647                                    query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
648    
649                                    String sql = query.toString();
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.putResult(FINDER_PATH_FETCH_BY_C_C,
689                                                    finderArgs, new ArrayList<Shard>());
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                            Session session = null;
756    
757                            try {
758                                    session = openSession();
759    
760                                    StringBundler query = null;
761                                    String sql = null;
762    
763                                    if (orderByComparator != null) {
764                                            query = new StringBundler(2 +
765                                                            (orderByComparator.getOrderByFields().length * 3));
766    
767                                            query.append(_SQL_SELECT_SHARD);
768    
769                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
770                                                    orderByComparator);
771    
772                                            sql = query.toString();
773                                    }
774                                    else {
775                                            sql = _SQL_SELECT_SHARD;
776                                    }
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                                            list = new ArrayList<Shard>();
797                                    }
798    
799                                    cacheResult(list);
800    
801                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
802    
803                                    closeSession(session);
804                            }
805                    }
806    
807                    return list;
808            }
809    
810            /**
811             * Removes the shard where name = &#63; from the database.
812             *
813             * @param name the name to search with
814             * @throws SystemException if a system exception occurred
815             */
816            public void removeByName(String name)
817                    throws NoSuchShardException, SystemException {
818                    Shard shard = findByName(name);
819    
820                    remove(shard);
821            }
822    
823            /**
824             * Removes the shard where classNameId = &#63; and classPK = &#63; from the database.
825             *
826             * @param classNameId the class name id to search with
827             * @param classPK the class p k to search with
828             * @throws SystemException if a system exception occurred
829             */
830            public void removeByC_C(long classNameId, long classPK)
831                    throws NoSuchShardException, SystemException {
832                    Shard shard = findByC_C(classNameId, classPK);
833    
834                    remove(shard);
835            }
836    
837            /**
838             * Removes all the shards from the database.
839             *
840             * @throws SystemException if a system exception occurred
841             */
842            public void removeAll() throws SystemException {
843                    for (Shard shard : findAll()) {
844                            remove(shard);
845                    }
846            }
847    
848            /**
849             * Counts all the shards where name = &#63;.
850             *
851             * @param name the name to search with
852             * @return the number of matching shards
853             * @throws SystemException if a system exception occurred
854             */
855            public int countByName(String name) throws SystemException {
856                    Object[] finderArgs = new Object[] { name };
857    
858                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_NAME,
859                                    finderArgs, this);
860    
861                    if (count == null) {
862                            Session session = null;
863    
864                            try {
865                                    session = openSession();
866    
867                                    StringBundler query = new StringBundler(2);
868    
869                                    query.append(_SQL_COUNT_SHARD_WHERE);
870    
871                                    if (name == null) {
872                                            query.append(_FINDER_COLUMN_NAME_NAME_1);
873                                    }
874                                    else {
875                                            if (name.equals(StringPool.BLANK)) {
876                                                    query.append(_FINDER_COLUMN_NAME_NAME_3);
877                                            }
878                                            else {
879                                                    query.append(_FINDER_COLUMN_NAME_NAME_2);
880                                            }
881                                    }
882    
883                                    String sql = query.toString();
884    
885                                    Query q = session.createQuery(sql);
886    
887                                    QueryPos qPos = QueryPos.getInstance(q);
888    
889                                    if (name != null) {
890                                            qPos.add(name);
891                                    }
892    
893                                    count = (Long)q.uniqueResult();
894                            }
895                            catch (Exception e) {
896                                    throw processException(e);
897                            }
898                            finally {
899                                    if (count == null) {
900                                            count = Long.valueOf(0);
901                                    }
902    
903                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_NAME,
904                                            finderArgs, count);
905    
906                                    closeSession(session);
907                            }
908                    }
909    
910                    return count.intValue();
911            }
912    
913            /**
914             * Counts all the shards where classNameId = &#63; and classPK = &#63;.
915             *
916             * @param classNameId the class name id to search with
917             * @param classPK the class p k to search with
918             * @return the number of matching shards
919             * @throws SystemException if a system exception occurred
920             */
921            public int countByC_C(long classNameId, long classPK)
922                    throws SystemException {
923                    Object[] finderArgs = new Object[] { classNameId, classPK };
924    
925                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
926                                    finderArgs, this);
927    
928                    if (count == null) {
929                            Session session = null;
930    
931                            try {
932                                    session = openSession();
933    
934                                    StringBundler query = new StringBundler(3);
935    
936                                    query.append(_SQL_COUNT_SHARD_WHERE);
937    
938                                    query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
939    
940                                    query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
941    
942                                    String sql = query.toString();
943    
944                                    Query q = session.createQuery(sql);
945    
946                                    QueryPos qPos = QueryPos.getInstance(q);
947    
948                                    qPos.add(classNameId);
949    
950                                    qPos.add(classPK);
951    
952                                    count = (Long)q.uniqueResult();
953                            }
954                            catch (Exception e) {
955                                    throw processException(e);
956                            }
957                            finally {
958                                    if (count == null) {
959                                            count = Long.valueOf(0);
960                                    }
961    
962                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
963                                            count);
964    
965                                    closeSession(session);
966                            }
967                    }
968    
969                    return count.intValue();
970            }
971    
972            /**
973             * Counts all the shards.
974             *
975             * @return the number of shards
976             * @throws SystemException if a system exception occurred
977             */
978            public int countAll() throws SystemException {
979                    Object[] finderArgs = new Object[0];
980    
981                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
982                                    finderArgs, this);
983    
984                    if (count == null) {
985                            Session session = null;
986    
987                            try {
988                                    session = openSession();
989    
990                                    Query q = session.createQuery(_SQL_COUNT_SHARD);
991    
992                                    count = (Long)q.uniqueResult();
993                            }
994                            catch (Exception e) {
995                                    throw processException(e);
996                            }
997                            finally {
998                                    if (count == null) {
999                                            count = Long.valueOf(0);
1000                                    }
1001    
1002                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1003                                            count);
1004    
1005                                    closeSession(session);
1006                            }
1007                    }
1008    
1009                    return count.intValue();
1010            }
1011    
1012            /**
1013             * Initializes the shard persistence.
1014             */
1015            public void afterPropertiesSet() {
1016                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1017                                            com.liferay.portal.util.PropsUtil.get(
1018                                                    "value.object.listener.com.liferay.portal.model.Shard")));
1019    
1020                    if (listenerClassNames.length > 0) {
1021                            try {
1022                                    List<ModelListener<Shard>> listenersList = new ArrayList<ModelListener<Shard>>();
1023    
1024                                    for (String listenerClassName : listenerClassNames) {
1025                                            listenersList.add((ModelListener<Shard>)InstanceFactory.newInstance(
1026                                                            listenerClassName));
1027                                    }
1028    
1029                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1030                            }
1031                            catch (Exception e) {
1032                                    _log.error(e);
1033                            }
1034                    }
1035            }
1036    
1037            public void destroy() {
1038                    EntityCacheUtil.removeCache(ShardImpl.class.getName());
1039                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1040                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1041            }
1042    
1043            @BeanReference(type = AccountPersistence.class)
1044            protected AccountPersistence accountPersistence;
1045            @BeanReference(type = AddressPersistence.class)
1046            protected AddressPersistence addressPersistence;
1047            @BeanReference(type = BrowserTrackerPersistence.class)
1048            protected BrowserTrackerPersistence browserTrackerPersistence;
1049            @BeanReference(type = ClassNamePersistence.class)
1050            protected ClassNamePersistence classNamePersistence;
1051            @BeanReference(type = ClusterGroupPersistence.class)
1052            protected ClusterGroupPersistence clusterGroupPersistence;
1053            @BeanReference(type = CompanyPersistence.class)
1054            protected CompanyPersistence companyPersistence;
1055            @BeanReference(type = ContactPersistence.class)
1056            protected ContactPersistence contactPersistence;
1057            @BeanReference(type = CountryPersistence.class)
1058            protected CountryPersistence countryPersistence;
1059            @BeanReference(type = EmailAddressPersistence.class)
1060            protected EmailAddressPersistence emailAddressPersistence;
1061            @BeanReference(type = GroupPersistence.class)
1062            protected GroupPersistence groupPersistence;
1063            @BeanReference(type = ImagePersistence.class)
1064            protected ImagePersistence imagePersistence;
1065            @BeanReference(type = LayoutPersistence.class)
1066            protected LayoutPersistence layoutPersistence;
1067            @BeanReference(type = LayoutPrototypePersistence.class)
1068            protected LayoutPrototypePersistence layoutPrototypePersistence;
1069            @BeanReference(type = LayoutSetPersistence.class)
1070            protected LayoutSetPersistence layoutSetPersistence;
1071            @BeanReference(type = LayoutSetPrototypePersistence.class)
1072            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1073            @BeanReference(type = ListTypePersistence.class)
1074            protected ListTypePersistence listTypePersistence;
1075            @BeanReference(type = LockPersistence.class)
1076            protected LockPersistence lockPersistence;
1077            @BeanReference(type = MembershipRequestPersistence.class)
1078            protected MembershipRequestPersistence membershipRequestPersistence;
1079            @BeanReference(type = OrganizationPersistence.class)
1080            protected OrganizationPersistence organizationPersistence;
1081            @BeanReference(type = OrgGroupPermissionPersistence.class)
1082            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1083            @BeanReference(type = OrgGroupRolePersistence.class)
1084            protected OrgGroupRolePersistence orgGroupRolePersistence;
1085            @BeanReference(type = OrgLaborPersistence.class)
1086            protected OrgLaborPersistence orgLaborPersistence;
1087            @BeanReference(type = PasswordPolicyPersistence.class)
1088            protected PasswordPolicyPersistence passwordPolicyPersistence;
1089            @BeanReference(type = PasswordPolicyRelPersistence.class)
1090            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1091            @BeanReference(type = PasswordTrackerPersistence.class)
1092            protected PasswordTrackerPersistence passwordTrackerPersistence;
1093            @BeanReference(type = PermissionPersistence.class)
1094            protected PermissionPersistence permissionPersistence;
1095            @BeanReference(type = PhonePersistence.class)
1096            protected PhonePersistence phonePersistence;
1097            @BeanReference(type = PluginSettingPersistence.class)
1098            protected PluginSettingPersistence pluginSettingPersistence;
1099            @BeanReference(type = PortletPersistence.class)
1100            protected PortletPersistence portletPersistence;
1101            @BeanReference(type = PortletItemPersistence.class)
1102            protected PortletItemPersistence portletItemPersistence;
1103            @BeanReference(type = PortletPreferencesPersistence.class)
1104            protected PortletPreferencesPersistence portletPreferencesPersistence;
1105            @BeanReference(type = RegionPersistence.class)
1106            protected RegionPersistence regionPersistence;
1107            @BeanReference(type = ReleasePersistence.class)
1108            protected ReleasePersistence releasePersistence;
1109            @BeanReference(type = ResourcePersistence.class)
1110            protected ResourcePersistence resourcePersistence;
1111            @BeanReference(type = ResourceActionPersistence.class)
1112            protected ResourceActionPersistence resourceActionPersistence;
1113            @BeanReference(type = ResourceCodePersistence.class)
1114            protected ResourceCodePersistence resourceCodePersistence;
1115            @BeanReference(type = ResourcePermissionPersistence.class)
1116            protected ResourcePermissionPersistence resourcePermissionPersistence;
1117            @BeanReference(type = RolePersistence.class)
1118            protected RolePersistence rolePersistence;
1119            @BeanReference(type = ServiceComponentPersistence.class)
1120            protected ServiceComponentPersistence serviceComponentPersistence;
1121            @BeanReference(type = ShardPersistence.class)
1122            protected ShardPersistence shardPersistence;
1123            @BeanReference(type = SubscriptionPersistence.class)
1124            protected SubscriptionPersistence subscriptionPersistence;
1125            @BeanReference(type = TicketPersistence.class)
1126            protected TicketPersistence ticketPersistence;
1127            @BeanReference(type = TeamPersistence.class)
1128            protected TeamPersistence teamPersistence;
1129            @BeanReference(type = UserPersistence.class)
1130            protected UserPersistence userPersistence;
1131            @BeanReference(type = UserGroupPersistence.class)
1132            protected UserGroupPersistence userGroupPersistence;
1133            @BeanReference(type = UserGroupGroupRolePersistence.class)
1134            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1135            @BeanReference(type = UserGroupRolePersistence.class)
1136            protected UserGroupRolePersistence userGroupRolePersistence;
1137            @BeanReference(type = UserIdMapperPersistence.class)
1138            protected UserIdMapperPersistence userIdMapperPersistence;
1139            @BeanReference(type = UserTrackerPersistence.class)
1140            protected UserTrackerPersistence userTrackerPersistence;
1141            @BeanReference(type = UserTrackerPathPersistence.class)
1142            protected UserTrackerPathPersistence userTrackerPathPersistence;
1143            @BeanReference(type = WebDAVPropsPersistence.class)
1144            protected WebDAVPropsPersistence webDAVPropsPersistence;
1145            @BeanReference(type = WebsitePersistence.class)
1146            protected WebsitePersistence websitePersistence;
1147            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1148            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1149            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1150            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1151            private static final String _SQL_SELECT_SHARD = "SELECT shard FROM Shard shard";
1152            private static final String _SQL_SELECT_SHARD_WHERE = "SELECT shard FROM Shard shard WHERE ";
1153            private static final String _SQL_COUNT_SHARD = "SELECT COUNT(shard) FROM Shard shard";
1154            private static final String _SQL_COUNT_SHARD_WHERE = "SELECT COUNT(shard) FROM Shard shard WHERE ";
1155            private static final String _FINDER_COLUMN_NAME_NAME_1 = "shard.name IS NULL";
1156            private static final String _FINDER_COLUMN_NAME_NAME_2 = "shard.name = ?";
1157            private static final String _FINDER_COLUMN_NAME_NAME_3 = "(shard.name IS NULL OR shard.name = ?)";
1158            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "shard.classNameId = ? AND ";
1159            private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "shard.classPK = ?";
1160            private static final String _ORDER_BY_ENTITY_ALIAS = "shard.";
1161            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Shard exists with the primary key ";
1162            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Shard exists with the key {";
1163            private static Log _log = LogFactoryUtil.getLog(ShardPersistenceImpl.class);
1164    }