1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.persistence;
21  
22  import com.liferay.portal.NoSuchShardException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.annotation.BeanReference;
25  import com.liferay.portal.kernel.cache.CacheRegistry;
26  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
27  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
28  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.FinderPath;
30  import com.liferay.portal.kernel.dao.orm.Query;
31  import com.liferay.portal.kernel.dao.orm.QueryPos;
32  import com.liferay.portal.kernel.dao.orm.QueryUtil;
33  import com.liferay.portal.kernel.dao.orm.Session;
34  import com.liferay.portal.kernel.log.Log;
35  import com.liferay.portal.kernel.log.LogFactoryUtil;
36  import com.liferay.portal.kernel.util.GetterUtil;
37  import com.liferay.portal.kernel.util.OrderByComparator;
38  import com.liferay.portal.kernel.util.StringPool;
39  import com.liferay.portal.kernel.util.StringUtil;
40  import com.liferay.portal.kernel.util.Validator;
41  import com.liferay.portal.model.ModelListener;
42  import com.liferay.portal.model.Shard;
43  import com.liferay.portal.model.impl.ShardImpl;
44  import com.liferay.portal.model.impl.ShardModelImpl;
45  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
46  
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.List;
50  
51  /**
52   * <a href="ShardPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class ShardPersistenceImpl extends BasePersistenceImpl
58      implements ShardPersistence {
59      public static final String FINDER_CLASS_NAME_ENTITY = ShardImpl.class.getName();
60      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
61          ".List";
62      public static final FinderPath FINDER_PATH_FETCH_BY_NAME = new FinderPath(ShardModelImpl.ENTITY_CACHE_ENABLED,
63              ShardModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
64              "fetchByName", new String[] { String.class.getName() });
65      public static final FinderPath FINDER_PATH_COUNT_BY_NAME = new FinderPath(ShardModelImpl.ENTITY_CACHE_ENABLED,
66              ShardModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
67              "countByName", new String[] { String.class.getName() });
68      public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(ShardModelImpl.ENTITY_CACHE_ENABLED,
69              ShardModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
70              "fetchByC_C",
71              new String[] { Long.class.getName(), Long.class.getName() });
72      public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(ShardModelImpl.ENTITY_CACHE_ENABLED,
73              ShardModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
74              "countByC_C",
75              new String[] { Long.class.getName(), Long.class.getName() });
76      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ShardModelImpl.ENTITY_CACHE_ENABLED,
77              ShardModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
78              "findAll", new String[0]);
79      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ShardModelImpl.ENTITY_CACHE_ENABLED,
80              ShardModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
81              "countAll", new String[0]);
82  
83      public void cacheResult(Shard shard) {
84          EntityCacheUtil.putResult(ShardModelImpl.ENTITY_CACHE_ENABLED,
85              ShardImpl.class, shard.getPrimaryKey(), shard);
86  
87          FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NAME,
88              new Object[] { shard.getName() }, shard);
89  
90          FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
91              new Object[] {
92                  new Long(shard.getClassNameId()), new Long(shard.getClassPK())
93              }, shard);
94      }
95  
96      public void cacheResult(List<Shard> shards) {
97          for (Shard shard : shards) {
98              if (EntityCacheUtil.getResult(ShardModelImpl.ENTITY_CACHE_ENABLED,
99                          ShardImpl.class, shard.getPrimaryKey(), this) == null) {
100                 cacheResult(shard);
101             }
102         }
103     }
104 
105     public void clearCache() {
106         CacheRegistry.clear(ShardImpl.class.getName());
107         EntityCacheUtil.clearCache(ShardImpl.class.getName());
108         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
109         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
110     }
111 
112     public Shard create(long shardId) {
113         Shard shard = new ShardImpl();
114 
115         shard.setNew(true);
116         shard.setPrimaryKey(shardId);
117 
118         return shard;
119     }
120 
121     public Shard remove(long shardId)
122         throws NoSuchShardException, SystemException {
123         Session session = null;
124 
125         try {
126             session = openSession();
127 
128             Shard shard = (Shard)session.get(ShardImpl.class, new Long(shardId));
129 
130             if (shard == null) {
131                 if (_log.isWarnEnabled()) {
132                     _log.warn("No Shard exists with the primary key " +
133                         shardId);
134                 }
135 
136                 throw new NoSuchShardException(
137                     "No Shard exists with the primary key " + shardId);
138             }
139 
140             return remove(shard);
141         }
142         catch (NoSuchShardException nsee) {
143             throw nsee;
144         }
145         catch (Exception e) {
146             throw processException(e);
147         }
148         finally {
149             closeSession(session);
150         }
151     }
152 
153     public Shard remove(Shard shard) throws SystemException {
154         for (ModelListener<Shard> listener : listeners) {
155             listener.onBeforeRemove(shard);
156         }
157 
158         shard = removeImpl(shard);
159 
160         for (ModelListener<Shard> listener : listeners) {
161             listener.onAfterRemove(shard);
162         }
163 
164         return shard;
165     }
166 
167     protected Shard removeImpl(Shard shard) throws SystemException {
168         Session session = null;
169 
170         try {
171             session = openSession();
172 
173             if (shard.isCachedModel() || BatchSessionUtil.isEnabled()) {
174                 Object staleObject = session.get(ShardImpl.class,
175                         shard.getPrimaryKeyObj());
176 
177                 if (staleObject != null) {
178                     session.evict(staleObject);
179                 }
180             }
181 
182             session.delete(shard);
183 
184             session.flush();
185         }
186         catch (Exception e) {
187             throw processException(e);
188         }
189         finally {
190             closeSession(session);
191         }
192 
193         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
194 
195         ShardModelImpl shardModelImpl = (ShardModelImpl)shard;
196 
197         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_NAME,
198             new Object[] { shardModelImpl.getOriginalName() });
199 
200         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
201             new Object[] {
202                 new Long(shardModelImpl.getOriginalClassNameId()),
203                 new Long(shardModelImpl.getOriginalClassPK())
204             });
205 
206         EntityCacheUtil.removeResult(ShardModelImpl.ENTITY_CACHE_ENABLED,
207             ShardImpl.class, shard.getPrimaryKey());
208 
209         return shard;
210     }
211 
212     /**
213      * @deprecated Use <code>update(Shard shard, boolean merge)</code>.
214      */
215     public Shard update(Shard shard) throws SystemException {
216         if (_log.isWarnEnabled()) {
217             _log.warn(
218                 "Using the deprecated update(Shard shard) method. Use update(Shard shard, boolean merge) instead.");
219         }
220 
221         return update(shard, false);
222     }
223 
224     /**
225      * Add, update, or merge, the entity. This method also calls the model
226      * listeners to trigger the proper events associated with adding, deleting,
227      * or updating an entity.
228      *
229      * @param        shard the entity to add, update, or merge
230      * @param        merge boolean value for whether to merge the entity. The
231      *                default value is false. Setting merge to true is more
232      *                expensive and should only be true when shard is
233      *                transient. See LEP-5473 for a detailed discussion of this
234      *                method.
235      * @return        true if the portlet can be displayed via Ajax
236      */
237     public Shard update(Shard shard, boolean merge) throws SystemException {
238         boolean isNew = shard.isNew();
239 
240         for (ModelListener<Shard> listener : listeners) {
241             if (isNew) {
242                 listener.onBeforeCreate(shard);
243             }
244             else {
245                 listener.onBeforeUpdate(shard);
246             }
247         }
248 
249         shard = updateImpl(shard, merge);
250 
251         for (ModelListener<Shard> listener : listeners) {
252             if (isNew) {
253                 listener.onAfterCreate(shard);
254             }
255             else {
256                 listener.onAfterUpdate(shard);
257             }
258         }
259 
260         return shard;
261     }
262 
263     public Shard updateImpl(com.liferay.portal.model.Shard shard, boolean merge)
264         throws SystemException {
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     public Shard findByPrimaryKey(long shardId)
328         throws NoSuchShardException, SystemException {
329         Shard shard = fetchByPrimaryKey(shardId);
330 
331         if (shard == null) {
332             if (_log.isWarnEnabled()) {
333                 _log.warn("No Shard exists with the primary key " + shardId);
334             }
335 
336             throw new NoSuchShardException(
337                 "No Shard exists with the primary key " + shardId);
338         }
339 
340         return shard;
341     }
342 
343     public Shard fetchByPrimaryKey(long shardId) throws SystemException {
344         Shard shard = (Shard)EntityCacheUtil.getResult(ShardModelImpl.ENTITY_CACHE_ENABLED,
345                 ShardImpl.class, shardId, this);
346 
347         if (shard == null) {
348             Session session = null;
349 
350             try {
351                 session = openSession();
352 
353                 shard = (Shard)session.get(ShardImpl.class, new Long(shardId));
354             }
355             catch (Exception e) {
356                 throw processException(e);
357             }
358             finally {
359                 if (shard != null) {
360                     cacheResult(shard);
361                 }
362 
363                 closeSession(session);
364             }
365         }
366 
367         return shard;
368     }
369 
370     public Shard findByName(String name)
371         throws NoSuchShardException, SystemException {
372         Shard shard = fetchByName(name);
373 
374         if (shard == null) {
375             StringBuilder msg = new StringBuilder();
376 
377             msg.append("No Shard exists with the key {");
378 
379             msg.append("name=" + name);
380 
381             msg.append(StringPool.CLOSE_CURLY_BRACE);
382 
383             if (_log.isWarnEnabled()) {
384                 _log.warn(msg.toString());
385             }
386 
387             throw new NoSuchShardException(msg.toString());
388         }
389 
390         return shard;
391     }
392 
393     public Shard fetchByName(String name) throws SystemException {
394         return fetchByName(name, true);
395     }
396 
397     public Shard fetchByName(String name, boolean retrieveFromCache)
398         throws SystemException {
399         Object[] finderArgs = new Object[] { name };
400 
401         Object result = null;
402 
403         if (retrieveFromCache) {
404             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_NAME,
405                     finderArgs, this);
406         }
407 
408         if (result == null) {
409             Session session = null;
410 
411             try {
412                 session = openSession();
413 
414                 StringBuilder query = new StringBuilder();
415 
416                 query.append("SELECT shard FROM Shard shard WHERE ");
417 
418                 if (name == null) {
419                     query.append("shard.name IS NULL");
420                 }
421                 else {
422                     query.append("shard.name = ?");
423                 }
424 
425                 query.append(" ");
426 
427                 Query q = session.createQuery(query.toString());
428 
429                 QueryPos qPos = QueryPos.getInstance(q);
430 
431                 if (name != null) {
432                     qPos.add(name);
433                 }
434 
435                 List<Shard> list = q.list();
436 
437                 result = list;
438 
439                 Shard shard = null;
440 
441                 if (list.isEmpty()) {
442                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NAME,
443                         finderArgs, list);
444                 }
445                 else {
446                     shard = list.get(0);
447 
448                     cacheResult(shard);
449 
450                     if ((shard.getName() == null) ||
451                             !shard.getName().equals(name)) {
452                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NAME,
453                             finderArgs, shard);
454                     }
455                 }
456 
457                 return shard;
458             }
459             catch (Exception e) {
460                 throw processException(e);
461             }
462             finally {
463                 if (result == null) {
464                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_NAME,
465                         finderArgs, new ArrayList<Shard>());
466                 }
467 
468                 closeSession(session);
469             }
470         }
471         else {
472             if (result instanceof List) {
473                 return null;
474             }
475             else {
476                 return (Shard)result;
477             }
478         }
479     }
480 
481     public Shard findByC_C(long classNameId, long classPK)
482         throws NoSuchShardException, SystemException {
483         Shard shard = fetchByC_C(classNameId, classPK);
484 
485         if (shard == null) {
486             StringBuilder msg = new StringBuilder();
487 
488             msg.append("No Shard exists with the key {");
489 
490             msg.append("classNameId=" + classNameId);
491 
492             msg.append(", ");
493             msg.append("classPK=" + classPK);
494 
495             msg.append(StringPool.CLOSE_CURLY_BRACE);
496 
497             if (_log.isWarnEnabled()) {
498                 _log.warn(msg.toString());
499             }
500 
501             throw new NoSuchShardException(msg.toString());
502         }
503 
504         return shard;
505     }
506 
507     public Shard fetchByC_C(long classNameId, long classPK)
508         throws SystemException {
509         return fetchByC_C(classNameId, classPK, true);
510     }
511 
512     public Shard fetchByC_C(long classNameId, long classPK,
513         boolean retrieveFromCache) throws SystemException {
514         Object[] finderArgs = new Object[] {
515                 new Long(classNameId), new Long(classPK)
516             };
517 
518         Object result = null;
519 
520         if (retrieveFromCache) {
521             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
522                     finderArgs, this);
523         }
524 
525         if (result == null) {
526             Session session = null;
527 
528             try {
529                 session = openSession();
530 
531                 StringBuilder query = new StringBuilder();
532 
533                 query.append("SELECT shard FROM Shard shard WHERE ");
534 
535                 query.append("shard.classNameId = ?");
536 
537                 query.append(" AND ");
538 
539                 query.append("shard.classPK = ?");
540 
541                 query.append(" ");
542 
543                 Query q = session.createQuery(query.toString());
544 
545                 QueryPos qPos = QueryPos.getInstance(q);
546 
547                 qPos.add(classNameId);
548 
549                 qPos.add(classPK);
550 
551                 List<Shard> list = q.list();
552 
553                 result = list;
554 
555                 Shard shard = null;
556 
557                 if (list.isEmpty()) {
558                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
559                         finderArgs, list);
560                 }
561                 else {
562                     shard = list.get(0);
563 
564                     cacheResult(shard);
565 
566                     if ((shard.getClassNameId() != classNameId) ||
567                             (shard.getClassPK() != classPK)) {
568                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
569                             finderArgs, shard);
570                     }
571                 }
572 
573                 return shard;
574             }
575             catch (Exception e) {
576                 throw processException(e);
577             }
578             finally {
579                 if (result == null) {
580                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
581                         finderArgs, new ArrayList<Shard>());
582                 }
583 
584                 closeSession(session);
585             }
586         }
587         else {
588             if (result instanceof List) {
589                 return null;
590             }
591             else {
592                 return (Shard)result;
593             }
594         }
595     }
596 
597     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
598         throws SystemException {
599         Session session = null;
600 
601         try {
602             session = openSession();
603 
604             dynamicQuery.compile(session);
605 
606             return dynamicQuery.list();
607         }
608         catch (Exception e) {
609             throw processException(e);
610         }
611         finally {
612             closeSession(session);
613         }
614     }
615 
616     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
617         int start, int end) throws SystemException {
618         Session session = null;
619 
620         try {
621             session = openSession();
622 
623             dynamicQuery.setLimit(start, end);
624 
625             dynamicQuery.compile(session);
626 
627             return dynamicQuery.list();
628         }
629         catch (Exception e) {
630             throw processException(e);
631         }
632         finally {
633             closeSession(session);
634         }
635     }
636 
637     public List<Shard> findAll() throws SystemException {
638         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
639     }
640 
641     public List<Shard> findAll(int start, int end) throws SystemException {
642         return findAll(start, end, null);
643     }
644 
645     public List<Shard> findAll(int start, int end, OrderByComparator obc)
646         throws SystemException {
647         Object[] finderArgs = new Object[] {
648                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
649             };
650 
651         List<Shard> list = (List<Shard>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
652                 finderArgs, this);
653 
654         if (list == null) {
655             Session session = null;
656 
657             try {
658                 session = openSession();
659 
660                 StringBuilder query = new StringBuilder();
661 
662                 query.append("SELECT shard FROM Shard shard ");
663 
664                 if (obc != null) {
665                     query.append("ORDER BY ");
666 
667                     String[] orderByFields = obc.getOrderByFields();
668 
669                     for (int i = 0; i < orderByFields.length; i++) {
670                         query.append("shard.");
671                         query.append(orderByFields[i]);
672 
673                         if (obc.isAscending()) {
674                             query.append(" ASC");
675                         }
676                         else {
677                             query.append(" DESC");
678                         }
679 
680                         if ((i + 1) < orderByFields.length) {
681                             query.append(", ");
682                         }
683                     }
684                 }
685 
686                 Query q = session.createQuery(query.toString());
687 
688                 if (obc == null) {
689                     list = (List<Shard>)QueryUtil.list(q, getDialect(), start,
690                             end, false);
691 
692                     Collections.sort(list);
693                 }
694                 else {
695                     list = (List<Shard>)QueryUtil.list(q, getDialect(), start,
696                             end);
697                 }
698             }
699             catch (Exception e) {
700                 throw processException(e);
701             }
702             finally {
703                 if (list == null) {
704                     list = new ArrayList<Shard>();
705                 }
706 
707                 cacheResult(list);
708 
709                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
710 
711                 closeSession(session);
712             }
713         }
714 
715         return list;
716     }
717 
718     public void removeByName(String name)
719         throws NoSuchShardException, SystemException {
720         Shard shard = findByName(name);
721 
722         remove(shard);
723     }
724 
725     public void removeByC_C(long classNameId, long classPK)
726         throws NoSuchShardException, SystemException {
727         Shard shard = findByC_C(classNameId, classPK);
728 
729         remove(shard);
730     }
731 
732     public void removeAll() throws SystemException {
733         for (Shard shard : findAll()) {
734             remove(shard);
735         }
736     }
737 
738     public int countByName(String name) throws SystemException {
739         Object[] finderArgs = new Object[] { name };
740 
741         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_NAME,
742                 finderArgs, this);
743 
744         if (count == null) {
745             Session session = null;
746 
747             try {
748                 session = openSession();
749 
750                 StringBuilder query = new StringBuilder();
751 
752                 query.append("SELECT COUNT(shard) ");
753                 query.append("FROM Shard shard WHERE ");
754 
755                 if (name == null) {
756                     query.append("shard.name IS NULL");
757                 }
758                 else {
759                     query.append("shard.name = ?");
760                 }
761 
762                 query.append(" ");
763 
764                 Query q = session.createQuery(query.toString());
765 
766                 QueryPos qPos = QueryPos.getInstance(q);
767 
768                 if (name != null) {
769                     qPos.add(name);
770                 }
771 
772                 count = (Long)q.uniqueResult();
773             }
774             catch (Exception e) {
775                 throw processException(e);
776             }
777             finally {
778                 if (count == null) {
779                     count = Long.valueOf(0);
780                 }
781 
782                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_NAME,
783                     finderArgs, count);
784 
785                 closeSession(session);
786             }
787         }
788 
789         return count.intValue();
790     }
791 
792     public int countByC_C(long classNameId, long classPK)
793         throws SystemException {
794         Object[] finderArgs = new Object[] {
795                 new Long(classNameId), new Long(classPK)
796             };
797 
798         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
799                 finderArgs, this);
800 
801         if (count == null) {
802             Session session = null;
803 
804             try {
805                 session = openSession();
806 
807                 StringBuilder query = new StringBuilder();
808 
809                 query.append("SELECT COUNT(shard) ");
810                 query.append("FROM Shard shard WHERE ");
811 
812                 query.append("shard.classNameId = ?");
813 
814                 query.append(" AND ");
815 
816                 query.append("shard.classPK = ?");
817 
818                 query.append(" ");
819 
820                 Query q = session.createQuery(query.toString());
821 
822                 QueryPos qPos = QueryPos.getInstance(q);
823 
824                 qPos.add(classNameId);
825 
826                 qPos.add(classPK);
827 
828                 count = (Long)q.uniqueResult();
829             }
830             catch (Exception e) {
831                 throw processException(e);
832             }
833             finally {
834                 if (count == null) {
835                     count = Long.valueOf(0);
836                 }
837 
838                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
839                     count);
840 
841                 closeSession(session);
842             }
843         }
844 
845         return count.intValue();
846     }
847 
848     public int countAll() throws SystemException {
849         Object[] finderArgs = new Object[0];
850 
851         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
852                 finderArgs, this);
853 
854         if (count == null) {
855             Session session = null;
856 
857             try {
858                 session = openSession();
859 
860                 Query q = session.createQuery(
861                         "SELECT COUNT(shard) FROM Shard shard");
862 
863                 count = (Long)q.uniqueResult();
864             }
865             catch (Exception e) {
866                 throw processException(e);
867             }
868             finally {
869                 if (count == null) {
870                     count = Long.valueOf(0);
871                 }
872 
873                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
874                     count);
875 
876                 closeSession(session);
877             }
878         }
879 
880         return count.intValue();
881     }
882 
883     public void afterPropertiesSet() {
884         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
885                     com.liferay.portal.util.PropsUtil.get(
886                         "value.object.listener.com.liferay.portal.model.Shard")));
887 
888         if (listenerClassNames.length > 0) {
889             try {
890                 List<ModelListener<Shard>> listenersList = new ArrayList<ModelListener<Shard>>();
891 
892                 for (String listenerClassName : listenerClassNames) {
893                     listenersList.add((ModelListener<Shard>)Class.forName(
894                             listenerClassName).newInstance());
895                 }
896 
897                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
898             }
899             catch (Exception e) {
900                 _log.error(e);
901             }
902         }
903     }
904 
905     @BeanReference(name = "com.liferay.portal.service.persistence.AccountPersistence.impl")
906     protected com.liferay.portal.service.persistence.AccountPersistence accountPersistence;
907     @BeanReference(name = "com.liferay.portal.service.persistence.AddressPersistence.impl")
908     protected com.liferay.portal.service.persistence.AddressPersistence addressPersistence;
909     @BeanReference(name = "com.liferay.portal.service.persistence.BrowserTrackerPersistence.impl")
910     protected com.liferay.portal.service.persistence.BrowserTrackerPersistence browserTrackerPersistence;
911     @BeanReference(name = "com.liferay.portal.service.persistence.ClassNamePersistence.impl")
912     protected com.liferay.portal.service.persistence.ClassNamePersistence classNamePersistence;
913     @BeanReference(name = "com.liferay.portal.service.persistence.CompanyPersistence.impl")
914     protected com.liferay.portal.service.persistence.CompanyPersistence companyPersistence;
915     @BeanReference(name = "com.liferay.portal.service.persistence.ContactPersistence.impl")
916     protected com.liferay.portal.service.persistence.ContactPersistence contactPersistence;
917     @BeanReference(name = "com.liferay.portal.service.persistence.CountryPersistence.impl")
918     protected com.liferay.portal.service.persistence.CountryPersistence countryPersistence;
919     @BeanReference(name = "com.liferay.portal.service.persistence.EmailAddressPersistence.impl")
920     protected com.liferay.portal.service.persistence.EmailAddressPersistence emailAddressPersistence;
921     @BeanReference(name = "com.liferay.portal.service.persistence.GroupPersistence.impl")
922     protected com.liferay.portal.service.persistence.GroupPersistence groupPersistence;
923     @BeanReference(name = "com.liferay.portal.service.persistence.ImagePersistence.impl")
924     protected com.liferay.portal.service.persistence.ImagePersistence imagePersistence;
925     @BeanReference(name = "com.liferay.portal.service.persistence.LayoutPersistence.impl")
926     protected com.liferay.portal.service.persistence.LayoutPersistence layoutPersistence;
927     @BeanReference(name = "com.liferay.portal.service.persistence.LayoutSetPersistence.impl")
928     protected com.liferay.portal.service.persistence.LayoutSetPersistence layoutSetPersistence;
929     @BeanReference(name = "com.liferay.portal.service.persistence.ListTypePersistence.impl")
930     protected com.liferay.portal.service.persistence.ListTypePersistence listTypePersistence;
931     @BeanReference(name = "com.liferay.portal.service.persistence.MembershipRequestPersistence.impl")
932     protected com.liferay.portal.service.persistence.MembershipRequestPersistence membershipRequestPersistence;
933     @BeanReference(name = "com.liferay.portal.service.persistence.OrganizationPersistence.impl")
934     protected com.liferay.portal.service.persistence.OrganizationPersistence organizationPersistence;
935     @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupPermissionPersistence.impl")
936     protected com.liferay.portal.service.persistence.OrgGroupPermissionPersistence orgGroupPermissionPersistence;
937     @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupRolePersistence.impl")
938     protected com.liferay.portal.service.persistence.OrgGroupRolePersistence orgGroupRolePersistence;
939     @BeanReference(name = "com.liferay.portal.service.persistence.OrgLaborPersistence.impl")
940     protected com.liferay.portal.service.persistence.OrgLaborPersistence orgLaborPersistence;
941     @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyPersistence.impl")
942     protected com.liferay.portal.service.persistence.PasswordPolicyPersistence passwordPolicyPersistence;
943     @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyRelPersistence.impl")
944     protected com.liferay.portal.service.persistence.PasswordPolicyRelPersistence passwordPolicyRelPersistence;
945     @BeanReference(name = "com.liferay.portal.service.persistence.PasswordTrackerPersistence.impl")
946     protected com.liferay.portal.service.persistence.PasswordTrackerPersistence passwordTrackerPersistence;
947     @BeanReference(name = "com.liferay.portal.service.persistence.PermissionPersistence.impl")
948     protected com.liferay.portal.service.persistence.PermissionPersistence permissionPersistence;
949     @BeanReference(name = "com.liferay.portal.service.persistence.PhonePersistence.impl")
950     protected com.liferay.portal.service.persistence.PhonePersistence phonePersistence;
951     @BeanReference(name = "com.liferay.portal.service.persistence.PluginSettingPersistence.impl")
952     protected com.liferay.portal.service.persistence.PluginSettingPersistence pluginSettingPersistence;
953     @BeanReference(name = "com.liferay.portal.service.persistence.PortletPersistence.impl")
954     protected com.liferay.portal.service.persistence.PortletPersistence portletPersistence;
955     @BeanReference(name = "com.liferay.portal.service.persistence.PortletItemPersistence.impl")
956     protected com.liferay.portal.service.persistence.PortletItemPersistence portletItemPersistence;
957     @BeanReference(name = "com.liferay.portal.service.persistence.PortletPreferencesPersistence.impl")
958     protected com.liferay.portal.service.persistence.PortletPreferencesPersistence portletPreferencesPersistence;
959     @BeanReference(name = "com.liferay.portal.service.persistence.RegionPersistence.impl")
960     protected com.liferay.portal.service.persistence.RegionPersistence regionPersistence;
961     @BeanReference(name = "com.liferay.portal.service.persistence.ReleasePersistence.impl")
962     protected com.liferay.portal.service.persistence.ReleasePersistence releasePersistence;
963     @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
964     protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
965     @BeanReference(name = "com.liferay.portal.service.persistence.ResourceActionPersistence.impl")
966     protected com.liferay.portal.service.persistence.ResourceActionPersistence resourceActionPersistence;
967     @BeanReference(name = "com.liferay.portal.service.persistence.ResourceCodePersistence.impl")
968     protected com.liferay.portal.service.persistence.ResourceCodePersistence resourceCodePersistence;
969     @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePermissionPersistence.impl")
970     protected com.liferay.portal.service.persistence.ResourcePermissionPersistence resourcePermissionPersistence;
971     @BeanReference(name = "com.liferay.portal.service.persistence.RolePersistence.impl")
972     protected com.liferay.portal.service.persistence.RolePersistence rolePersistence;
973     @BeanReference(name = "com.liferay.portal.service.persistence.ServiceComponentPersistence.impl")
974     protected com.liferay.portal.service.persistence.ServiceComponentPersistence serviceComponentPersistence;
975     @BeanReference(name = "com.liferay.portal.service.persistence.ShardPersistence.impl")
976     protected com.liferay.portal.service.persistence.ShardPersistence shardPersistence;
977     @BeanReference(name = "com.liferay.portal.service.persistence.SubscriptionPersistence.impl")
978     protected com.liferay.portal.service.persistence.SubscriptionPersistence subscriptionPersistence;
979     @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
980     protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
981     @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupPersistence.impl")
982     protected com.liferay.portal.service.persistence.UserGroupPersistence userGroupPersistence;
983     @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupRolePersistence.impl")
984     protected com.liferay.portal.service.persistence.UserGroupRolePersistence userGroupRolePersistence;
985     @BeanReference(name = "com.liferay.portal.service.persistence.UserIdMapperPersistence.impl")
986     protected com.liferay.portal.service.persistence.UserIdMapperPersistence userIdMapperPersistence;
987     @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPersistence.impl")
988     protected com.liferay.portal.service.persistence.UserTrackerPersistence userTrackerPersistence;
989     @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPathPersistence.impl")
990     protected com.liferay.portal.service.persistence.UserTrackerPathPersistence userTrackerPathPersistence;
991     @BeanReference(name = "com.liferay.portal.service.persistence.WebDAVPropsPersistence.impl")
992     protected com.liferay.portal.service.persistence.WebDAVPropsPersistence webDAVPropsPersistence;
993     @BeanReference(name = "com.liferay.portal.service.persistence.WebsitePersistence.impl")
994     protected com.liferay.portal.service.persistence.WebsitePersistence websitePersistence;
995     private static Log _log = LogFactoryUtil.getLog(ShardPersistenceImpl.class);
996 }