1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence;
24  
25  import com.liferay.portal.NoSuchUserIdMapperException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.DynamicQuery;
28  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.OrderByComparator;
31  import com.liferay.portal.kernel.util.StringMaker;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.model.ModelListener;
35  import com.liferay.portal.model.UserIdMapper;
36  import com.liferay.portal.model.impl.UserIdMapperImpl;
37  import com.liferay.portal.model.impl.UserIdMapperModelImpl;
38  import com.liferay.portal.spring.hibernate.FinderCache;
39  import com.liferay.portal.spring.hibernate.HibernateUtil;
40  import com.liferay.portal.util.PropsUtil;
41  
42  import com.liferay.util.dao.hibernate.QueryUtil;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  import org.hibernate.Query;
48  import org.hibernate.Session;
49  
50  import java.util.Collections;
51  import java.util.Iterator;
52  import java.util.List;
53  
54  /**
55   * <a href="UserIdMapperPersistenceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class UserIdMapperPersistenceImpl extends BasePersistence
61      implements UserIdMapperPersistence {
62      public UserIdMapper create(long userIdMapperId) {
63          UserIdMapper userIdMapper = new UserIdMapperImpl();
64  
65          userIdMapper.setNew(true);
66          userIdMapper.setPrimaryKey(userIdMapperId);
67  
68          return userIdMapper;
69      }
70  
71      public UserIdMapper remove(long userIdMapperId)
72          throws NoSuchUserIdMapperException, SystemException {
73          Session session = null;
74  
75          try {
76              session = openSession();
77  
78              UserIdMapper userIdMapper = (UserIdMapper)session.get(UserIdMapperImpl.class,
79                      new Long(userIdMapperId));
80  
81              if (userIdMapper == null) {
82                  if (_log.isWarnEnabled()) {
83                      _log.warn("No UserIdMapper exists with the primary key " +
84                          userIdMapperId);
85                  }
86  
87                  throw new NoSuchUserIdMapperException(
88                      "No UserIdMapper exists with the primary key " +
89                      userIdMapperId);
90              }
91  
92              return remove(userIdMapper);
93          }
94          catch (NoSuchUserIdMapperException nsee) {
95              throw nsee;
96          }
97          catch (Exception e) {
98              throw HibernateUtil.processException(e);
99          }
100         finally {
101             closeSession(session);
102         }
103     }
104 
105     public UserIdMapper remove(UserIdMapper userIdMapper)
106         throws SystemException {
107         ModelListener listener = _getListener();
108 
109         if (listener != null) {
110             listener.onBeforeRemove(userIdMapper);
111         }
112 
113         userIdMapper = removeImpl(userIdMapper);
114 
115         if (listener != null) {
116             listener.onAfterRemove(userIdMapper);
117         }
118 
119         return userIdMapper;
120     }
121 
122     protected UserIdMapper removeImpl(UserIdMapper userIdMapper)
123         throws SystemException {
124         Session session = null;
125 
126         try {
127             session = openSession();
128 
129             session.delete(userIdMapper);
130 
131             session.flush();
132 
133             return userIdMapper;
134         }
135         catch (Exception e) {
136             throw HibernateUtil.processException(e);
137         }
138         finally {
139             closeSession(session);
140 
141             FinderCache.clearCache(UserIdMapper.class.getName());
142         }
143     }
144 
145     public UserIdMapper update(UserIdMapper userIdMapper)
146         throws SystemException {
147         return update(userIdMapper, false);
148     }
149 
150     public UserIdMapper update(UserIdMapper userIdMapper, boolean merge)
151         throws SystemException {
152         ModelListener listener = _getListener();
153 
154         boolean isNew = userIdMapper.isNew();
155 
156         if (listener != null) {
157             if (isNew) {
158                 listener.onBeforeCreate(userIdMapper);
159             }
160             else {
161                 listener.onBeforeUpdate(userIdMapper);
162             }
163         }
164 
165         userIdMapper = updateImpl(userIdMapper, merge);
166 
167         if (listener != null) {
168             if (isNew) {
169                 listener.onAfterCreate(userIdMapper);
170             }
171             else {
172                 listener.onAfterUpdate(userIdMapper);
173             }
174         }
175 
176         return userIdMapper;
177     }
178 
179     public UserIdMapper updateImpl(
180         com.liferay.portal.model.UserIdMapper userIdMapper, boolean merge)
181         throws SystemException {
182         Session session = null;
183 
184         try {
185             session = openSession();
186 
187             if (merge) {
188                 session.merge(userIdMapper);
189             }
190             else {
191                 if (userIdMapper.isNew()) {
192                     session.save(userIdMapper);
193                 }
194             }
195 
196             session.flush();
197 
198             userIdMapper.setNew(false);
199 
200             return userIdMapper;
201         }
202         catch (Exception e) {
203             throw HibernateUtil.processException(e);
204         }
205         finally {
206             closeSession(session);
207 
208             FinderCache.clearCache(UserIdMapper.class.getName());
209         }
210     }
211 
212     public UserIdMapper findByPrimaryKey(long userIdMapperId)
213         throws NoSuchUserIdMapperException, SystemException {
214         UserIdMapper userIdMapper = fetchByPrimaryKey(userIdMapperId);
215 
216         if (userIdMapper == null) {
217             if (_log.isWarnEnabled()) {
218                 _log.warn("No UserIdMapper exists with the primary key " +
219                     userIdMapperId);
220             }
221 
222             throw new NoSuchUserIdMapperException(
223                 "No UserIdMapper exists with the primary key " +
224                 userIdMapperId);
225         }
226 
227         return userIdMapper;
228     }
229 
230     public UserIdMapper fetchByPrimaryKey(long userIdMapperId)
231         throws SystemException {
232         Session session = null;
233 
234         try {
235             session = openSession();
236 
237             return (UserIdMapper)session.get(UserIdMapperImpl.class,
238                 new Long(userIdMapperId));
239         }
240         catch (Exception e) {
241             throw HibernateUtil.processException(e);
242         }
243         finally {
244             closeSession(session);
245         }
246     }
247 
248     public List findByUserId(long userId) throws SystemException {
249         boolean finderClassNameCacheEnabled = UserIdMapperModelImpl.CACHE_ENABLED;
250         String finderClassName = UserIdMapper.class.getName();
251         String finderMethodName = "findByUserId";
252         String[] finderParams = new String[] { Long.class.getName() };
253         Object[] finderArgs = new Object[] { new Long(userId) };
254 
255         Object result = null;
256 
257         if (finderClassNameCacheEnabled) {
258             result = FinderCache.getResult(finderClassName, finderMethodName,
259                     finderParams, finderArgs, getSessionFactory());
260         }
261 
262         if (result == null) {
263             Session session = null;
264 
265             try {
266                 session = openSession();
267 
268                 StringMaker query = new StringMaker();
269 
270                 query.append(
271                     "FROM com.liferay.portal.model.UserIdMapper WHERE ");
272 
273                 query.append("userId = ?");
274 
275                 query.append(" ");
276 
277                 Query q = session.createQuery(query.toString());
278 
279                 int queryPos = 0;
280 
281                 q.setLong(queryPos++, userId);
282 
283                 List list = q.list();
284 
285                 FinderCache.putResult(finderClassNameCacheEnabled,
286                     finderClassName, finderMethodName, finderParams,
287                     finderArgs, list);
288 
289                 return list;
290             }
291             catch (Exception e) {
292                 throw HibernateUtil.processException(e);
293             }
294             finally {
295                 closeSession(session);
296             }
297         }
298         else {
299             return (List)result;
300         }
301     }
302 
303     public List findByUserId(long userId, int begin, int end)
304         throws SystemException {
305         return findByUserId(userId, begin, end, null);
306     }
307 
308     public List findByUserId(long userId, int begin, int end,
309         OrderByComparator obc) throws SystemException {
310         boolean finderClassNameCacheEnabled = UserIdMapperModelImpl.CACHE_ENABLED;
311         String finderClassName = UserIdMapper.class.getName();
312         String finderMethodName = "findByUserId";
313         String[] finderParams = new String[] {
314                 Long.class.getName(),
315                 
316                 "java.lang.Integer", "java.lang.Integer",
317                 "com.liferay.portal.kernel.util.OrderByComparator"
318             };
319         Object[] finderArgs = new Object[] {
320                 new Long(userId),
321                 
322                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
323             };
324 
325         Object result = null;
326 
327         if (finderClassNameCacheEnabled) {
328             result = FinderCache.getResult(finderClassName, finderMethodName,
329                     finderParams, finderArgs, getSessionFactory());
330         }
331 
332         if (result == null) {
333             Session session = null;
334 
335             try {
336                 session = openSession();
337 
338                 StringMaker query = new StringMaker();
339 
340                 query.append(
341                     "FROM com.liferay.portal.model.UserIdMapper WHERE ");
342 
343                 query.append("userId = ?");
344 
345                 query.append(" ");
346 
347                 if (obc != null) {
348                     query.append("ORDER BY ");
349                     query.append(obc.getOrderBy());
350                 }
351 
352                 Query q = session.createQuery(query.toString());
353 
354                 int queryPos = 0;
355 
356                 q.setLong(queryPos++, userId);
357 
358                 List list = QueryUtil.list(q, getDialect(), begin, end);
359 
360                 FinderCache.putResult(finderClassNameCacheEnabled,
361                     finderClassName, finderMethodName, finderParams,
362                     finderArgs, list);
363 
364                 return list;
365             }
366             catch (Exception e) {
367                 throw HibernateUtil.processException(e);
368             }
369             finally {
370                 closeSession(session);
371             }
372         }
373         else {
374             return (List)result;
375         }
376     }
377 
378     public UserIdMapper findByUserId_First(long userId, OrderByComparator obc)
379         throws NoSuchUserIdMapperException, SystemException {
380         List list = findByUserId(userId, 0, 1, obc);
381 
382         if (list.size() == 0) {
383             StringMaker msg = new StringMaker();
384 
385             msg.append("No UserIdMapper exists with the key {");
386 
387             msg.append("userId=" + userId);
388 
389             msg.append(StringPool.CLOSE_CURLY_BRACE);
390 
391             throw new NoSuchUserIdMapperException(msg.toString());
392         }
393         else {
394             return (UserIdMapper)list.get(0);
395         }
396     }
397 
398     public UserIdMapper findByUserId_Last(long userId, OrderByComparator obc)
399         throws NoSuchUserIdMapperException, SystemException {
400         int count = countByUserId(userId);
401 
402         List list = findByUserId(userId, count - 1, count, obc);
403 
404         if (list.size() == 0) {
405             StringMaker msg = new StringMaker();
406 
407             msg.append("No UserIdMapper exists with the key {");
408 
409             msg.append("userId=" + userId);
410 
411             msg.append(StringPool.CLOSE_CURLY_BRACE);
412 
413             throw new NoSuchUserIdMapperException(msg.toString());
414         }
415         else {
416             return (UserIdMapper)list.get(0);
417         }
418     }
419 
420     public UserIdMapper[] findByUserId_PrevAndNext(long userIdMapperId,
421         long userId, OrderByComparator obc)
422         throws NoSuchUserIdMapperException, SystemException {
423         UserIdMapper userIdMapper = findByPrimaryKey(userIdMapperId);
424 
425         int count = countByUserId(userId);
426 
427         Session session = null;
428 
429         try {
430             session = openSession();
431 
432             StringMaker query = new StringMaker();
433 
434             query.append("FROM com.liferay.portal.model.UserIdMapper WHERE ");
435 
436             query.append("userId = ?");
437 
438             query.append(" ");
439 
440             if (obc != null) {
441                 query.append("ORDER BY ");
442                 query.append(obc.getOrderBy());
443             }
444 
445             Query q = session.createQuery(query.toString());
446 
447             int queryPos = 0;
448 
449             q.setLong(queryPos++, userId);
450 
451             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
452                     userIdMapper);
453 
454             UserIdMapper[] array = new UserIdMapperImpl[3];
455 
456             array[0] = (UserIdMapper)objArray[0];
457             array[1] = (UserIdMapper)objArray[1];
458             array[2] = (UserIdMapper)objArray[2];
459 
460             return array;
461         }
462         catch (Exception e) {
463             throw HibernateUtil.processException(e);
464         }
465         finally {
466             closeSession(session);
467         }
468     }
469 
470     public UserIdMapper findByU_T(long userId, String type)
471         throws NoSuchUserIdMapperException, SystemException {
472         UserIdMapper userIdMapper = fetchByU_T(userId, type);
473 
474         if (userIdMapper == null) {
475             StringMaker msg = new StringMaker();
476 
477             msg.append("No UserIdMapper exists with the key {");
478 
479             msg.append("userId=" + userId);
480 
481             msg.append(", ");
482             msg.append("type=" + type);
483 
484             msg.append(StringPool.CLOSE_CURLY_BRACE);
485 
486             if (_log.isWarnEnabled()) {
487                 _log.warn(msg.toString());
488             }
489 
490             throw new NoSuchUserIdMapperException(msg.toString());
491         }
492 
493         return userIdMapper;
494     }
495 
496     public UserIdMapper fetchByU_T(long userId, String type)
497         throws SystemException {
498         boolean finderClassNameCacheEnabled = UserIdMapperModelImpl.CACHE_ENABLED;
499         String finderClassName = UserIdMapper.class.getName();
500         String finderMethodName = "fetchByU_T";
501         String[] finderParams = new String[] {
502                 Long.class.getName(), String.class.getName()
503             };
504         Object[] finderArgs = new Object[] { new Long(userId), type };
505 
506         Object result = null;
507 
508         if (finderClassNameCacheEnabled) {
509             result = FinderCache.getResult(finderClassName, finderMethodName,
510                     finderParams, finderArgs, getSessionFactory());
511         }
512 
513         if (result == null) {
514             Session session = null;
515 
516             try {
517                 session = openSession();
518 
519                 StringMaker query = new StringMaker();
520 
521                 query.append(
522                     "FROM com.liferay.portal.model.UserIdMapper WHERE ");
523 
524                 query.append("userId = ?");
525 
526                 query.append(" AND ");
527 
528                 if (type == null) {
529                     query.append("type_ IS NULL");
530                 }
531                 else {
532                     query.append("type_ = ?");
533                 }
534 
535                 query.append(" ");
536 
537                 Query q = session.createQuery(query.toString());
538 
539                 int queryPos = 0;
540 
541                 q.setLong(queryPos++, userId);
542 
543                 if (type != null) {
544                     q.setString(queryPos++, type);
545                 }
546 
547                 List list = q.list();
548 
549                 FinderCache.putResult(finderClassNameCacheEnabled,
550                     finderClassName, finderMethodName, finderParams,
551                     finderArgs, list);
552 
553                 if (list.size() == 0) {
554                     return null;
555                 }
556                 else {
557                     return (UserIdMapper)list.get(0);
558                 }
559             }
560             catch (Exception e) {
561                 throw HibernateUtil.processException(e);
562             }
563             finally {
564                 closeSession(session);
565             }
566         }
567         else {
568             List list = (List)result;
569 
570             if (list.size() == 0) {
571                 return null;
572             }
573             else {
574                 return (UserIdMapper)list.get(0);
575             }
576         }
577     }
578 
579     public UserIdMapper findByT_E(String type, String externalUserId)
580         throws NoSuchUserIdMapperException, SystemException {
581         UserIdMapper userIdMapper = fetchByT_E(type, externalUserId);
582 
583         if (userIdMapper == null) {
584             StringMaker msg = new StringMaker();
585 
586             msg.append("No UserIdMapper exists with the key {");
587 
588             msg.append("type=" + type);
589 
590             msg.append(", ");
591             msg.append("externalUserId=" + externalUserId);
592 
593             msg.append(StringPool.CLOSE_CURLY_BRACE);
594 
595             if (_log.isWarnEnabled()) {
596                 _log.warn(msg.toString());
597             }
598 
599             throw new NoSuchUserIdMapperException(msg.toString());
600         }
601 
602         return userIdMapper;
603     }
604 
605     public UserIdMapper fetchByT_E(String type, String externalUserId)
606         throws SystemException {
607         boolean finderClassNameCacheEnabled = UserIdMapperModelImpl.CACHE_ENABLED;
608         String finderClassName = UserIdMapper.class.getName();
609         String finderMethodName = "fetchByT_E";
610         String[] finderParams = new String[] {
611                 String.class.getName(), String.class.getName()
612             };
613         Object[] finderArgs = new Object[] { type, externalUserId };
614 
615         Object result = null;
616 
617         if (finderClassNameCacheEnabled) {
618             result = FinderCache.getResult(finderClassName, finderMethodName,
619                     finderParams, finderArgs, getSessionFactory());
620         }
621 
622         if (result == null) {
623             Session session = null;
624 
625             try {
626                 session = openSession();
627 
628                 StringMaker query = new StringMaker();
629 
630                 query.append(
631                     "FROM com.liferay.portal.model.UserIdMapper WHERE ");
632 
633                 if (type == null) {
634                     query.append("type_ IS NULL");
635                 }
636                 else {
637                     query.append("type_ = ?");
638                 }
639 
640                 query.append(" AND ");
641 
642                 if (externalUserId == null) {
643                     query.append("externalUserId IS NULL");
644                 }
645                 else {
646                     query.append("externalUserId = ?");
647                 }
648 
649                 query.append(" ");
650 
651                 Query q = session.createQuery(query.toString());
652 
653                 int queryPos = 0;
654 
655                 if (type != null) {
656                     q.setString(queryPos++, type);
657                 }
658 
659                 if (externalUserId != null) {
660                     q.setString(queryPos++, externalUserId);
661                 }
662 
663                 List list = q.list();
664 
665                 FinderCache.putResult(finderClassNameCacheEnabled,
666                     finderClassName, finderMethodName, finderParams,
667                     finderArgs, list);
668 
669                 if (list.size() == 0) {
670                     return null;
671                 }
672                 else {
673                     return (UserIdMapper)list.get(0);
674                 }
675             }
676             catch (Exception e) {
677                 throw HibernateUtil.processException(e);
678             }
679             finally {
680                 closeSession(session);
681             }
682         }
683         else {
684             List list = (List)result;
685 
686             if (list.size() == 0) {
687                 return null;
688             }
689             else {
690                 return (UserIdMapper)list.get(0);
691             }
692         }
693     }
694 
695     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
696         throws SystemException {
697         Session session = null;
698 
699         try {
700             session = openSession();
701 
702             DynamicQuery query = queryInitializer.initialize(session);
703 
704             return query.list();
705         }
706         catch (Exception e) {
707             throw HibernateUtil.processException(e);
708         }
709         finally {
710             closeSession(session);
711         }
712     }
713 
714     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
715         int begin, int end) throws SystemException {
716         Session session = null;
717 
718         try {
719             session = openSession();
720 
721             DynamicQuery query = queryInitializer.initialize(session);
722 
723             query.setLimit(begin, end);
724 
725             return query.list();
726         }
727         catch (Exception e) {
728             throw HibernateUtil.processException(e);
729         }
730         finally {
731             closeSession(session);
732         }
733     }
734 
735     public List findAll() throws SystemException {
736         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
737     }
738 
739     public List findAll(int begin, int end) throws SystemException {
740         return findAll(begin, end, null);
741     }
742 
743     public List findAll(int begin, int end, OrderByComparator obc)
744         throws SystemException {
745         boolean finderClassNameCacheEnabled = UserIdMapperModelImpl.CACHE_ENABLED;
746         String finderClassName = UserIdMapper.class.getName();
747         String finderMethodName = "findAll";
748         String[] finderParams = new String[] {
749                 "java.lang.Integer", "java.lang.Integer",
750                 "com.liferay.portal.kernel.util.OrderByComparator"
751             };
752         Object[] finderArgs = new Object[] {
753                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
754             };
755 
756         Object result = null;
757 
758         if (finderClassNameCacheEnabled) {
759             result = FinderCache.getResult(finderClassName, finderMethodName,
760                     finderParams, finderArgs, getSessionFactory());
761         }
762 
763         if (result == null) {
764             Session session = null;
765 
766             try {
767                 session = openSession();
768 
769                 StringMaker query = new StringMaker();
770 
771                 query.append("FROM com.liferay.portal.model.UserIdMapper ");
772 
773                 if (obc != null) {
774                     query.append("ORDER BY ");
775                     query.append(obc.getOrderBy());
776                 }
777 
778                 Query q = session.createQuery(query.toString());
779 
780                 List list = QueryUtil.list(q, getDialect(), begin, end);
781 
782                 if (obc == null) {
783                     Collections.sort(list);
784                 }
785 
786                 FinderCache.putResult(finderClassNameCacheEnabled,
787                     finderClassName, finderMethodName, finderParams,
788                     finderArgs, list);
789 
790                 return list;
791             }
792             catch (Exception e) {
793                 throw HibernateUtil.processException(e);
794             }
795             finally {
796                 closeSession(session);
797             }
798         }
799         else {
800             return (List)result;
801         }
802     }
803 
804     public void removeByUserId(long userId) throws SystemException {
805         Iterator itr = findByUserId(userId).iterator();
806 
807         while (itr.hasNext()) {
808             UserIdMapper userIdMapper = (UserIdMapper)itr.next();
809 
810             remove(userIdMapper);
811         }
812     }
813 
814     public void removeByU_T(long userId, String type)
815         throws NoSuchUserIdMapperException, SystemException {
816         UserIdMapper userIdMapper = findByU_T(userId, type);
817 
818         remove(userIdMapper);
819     }
820 
821     public void removeByT_E(String type, String externalUserId)
822         throws NoSuchUserIdMapperException, SystemException {
823         UserIdMapper userIdMapper = findByT_E(type, externalUserId);
824 
825         remove(userIdMapper);
826     }
827 
828     public void removeAll() throws SystemException {
829         Iterator itr = findAll().iterator();
830 
831         while (itr.hasNext()) {
832             remove((UserIdMapper)itr.next());
833         }
834     }
835 
836     public int countByUserId(long userId) throws SystemException {
837         boolean finderClassNameCacheEnabled = UserIdMapperModelImpl.CACHE_ENABLED;
838         String finderClassName = UserIdMapper.class.getName();
839         String finderMethodName = "countByUserId";
840         String[] finderParams = new String[] { Long.class.getName() };
841         Object[] finderArgs = new Object[] { new Long(userId) };
842 
843         Object result = null;
844 
845         if (finderClassNameCacheEnabled) {
846             result = FinderCache.getResult(finderClassName, finderMethodName,
847                     finderParams, finderArgs, getSessionFactory());
848         }
849 
850         if (result == null) {
851             Session session = null;
852 
853             try {
854                 session = openSession();
855 
856                 StringMaker query = new StringMaker();
857 
858                 query.append("SELECT COUNT(*) ");
859                 query.append(
860                     "FROM com.liferay.portal.model.UserIdMapper WHERE ");
861 
862                 query.append("userId = ?");
863 
864                 query.append(" ");
865 
866                 Query q = session.createQuery(query.toString());
867 
868                 int queryPos = 0;
869 
870                 q.setLong(queryPos++, userId);
871 
872                 Long count = null;
873 
874                 Iterator itr = q.list().iterator();
875 
876                 if (itr.hasNext()) {
877                     count = (Long)itr.next();
878                 }
879 
880                 if (count == null) {
881                     count = new Long(0);
882                 }
883 
884                 FinderCache.putResult(finderClassNameCacheEnabled,
885                     finderClassName, finderMethodName, finderParams,
886                     finderArgs, count);
887 
888                 return count.intValue();
889             }
890             catch (Exception e) {
891                 throw HibernateUtil.processException(e);
892             }
893             finally {
894                 closeSession(session);
895             }
896         }
897         else {
898             return ((Long)result).intValue();
899         }
900     }
901 
902     public int countByU_T(long userId, String type) throws SystemException {
903         boolean finderClassNameCacheEnabled = UserIdMapperModelImpl.CACHE_ENABLED;
904         String finderClassName = UserIdMapper.class.getName();
905         String finderMethodName = "countByU_T";
906         String[] finderParams = new String[] {
907                 Long.class.getName(), String.class.getName()
908             };
909         Object[] finderArgs = new Object[] { new Long(userId), type };
910 
911         Object result = null;
912 
913         if (finderClassNameCacheEnabled) {
914             result = FinderCache.getResult(finderClassName, finderMethodName,
915                     finderParams, finderArgs, getSessionFactory());
916         }
917 
918         if (result == null) {
919             Session session = null;
920 
921             try {
922                 session = openSession();
923 
924                 StringMaker query = new StringMaker();
925 
926                 query.append("SELECT COUNT(*) ");
927                 query.append(
928                     "FROM com.liferay.portal.model.UserIdMapper WHERE ");
929 
930                 query.append("userId = ?");
931 
932                 query.append(" AND ");
933 
934                 if (type == null) {
935                     query.append("type_ IS NULL");
936                 }
937                 else {
938                     query.append("type_ = ?");
939                 }
940 
941                 query.append(" ");
942 
943                 Query q = session.createQuery(query.toString());
944 
945                 int queryPos = 0;
946 
947                 q.setLong(queryPos++, userId);
948 
949                 if (type != null) {
950                     q.setString(queryPos++, type);
951                 }
952 
953                 Long count = null;
954 
955                 Iterator itr = q.list().iterator();
956 
957                 if (itr.hasNext()) {
958                     count = (Long)itr.next();
959                 }
960 
961                 if (count == null) {
962                     count = new Long(0);
963                 }
964 
965                 FinderCache.putResult(finderClassNameCacheEnabled,
966                     finderClassName, finderMethodName, finderParams,
967                     finderArgs, count);
968 
969                 return count.intValue();
970             }
971             catch (Exception e) {
972                 throw HibernateUtil.processException(e);
973             }
974             finally {
975                 closeSession(session);
976             }
977         }
978         else {
979             return ((Long)result).intValue();
980         }
981     }
982 
983     public int countByT_E(String type, String externalUserId)
984         throws SystemException {
985         boolean finderClassNameCacheEnabled = UserIdMapperModelImpl.CACHE_ENABLED;
986         String finderClassName = UserIdMapper.class.getName();
987         String finderMethodName = "countByT_E";
988         String[] finderParams = new String[] {
989                 String.class.getName(), String.class.getName()
990             };
991         Object[] finderArgs = new Object[] { type, externalUserId };
992 
993         Object result = null;
994 
995         if (finderClassNameCacheEnabled) {
996             result = FinderCache.getResult(finderClassName, finderMethodName,
997                     finderParams, finderArgs, getSessionFactory());
998         }
999 
1000        if (result == null) {
1001            Session session = null;
1002
1003            try {
1004                session = openSession();
1005
1006                StringMaker query = new StringMaker();
1007
1008                query.append("SELECT COUNT(*) ");
1009                query.append(
1010                    "FROM com.liferay.portal.model.UserIdMapper WHERE ");
1011
1012                if (type == null) {
1013                    query.append("type_ IS NULL");
1014                }
1015                else {
1016                    query.append("type_ = ?");
1017                }
1018
1019                query.append(" AND ");
1020
1021                if (externalUserId == null) {
1022                    query.append("externalUserId IS NULL");
1023                }
1024                else {
1025                    query.append("externalUserId = ?");
1026                }
1027
1028                query.append(" ");
1029
1030                Query q = session.createQuery(query.toString());
1031
1032                int queryPos = 0;
1033
1034                if (type != null) {
1035                    q.setString(queryPos++, type);
1036                }
1037
1038                if (externalUserId != null) {
1039                    q.setString(queryPos++, externalUserId);
1040                }
1041
1042                Long count = null;
1043
1044                Iterator itr = q.list().iterator();
1045
1046                if (itr.hasNext()) {
1047                    count = (Long)itr.next();
1048                }
1049
1050                if (count == null) {
1051                    count = new Long(0);
1052                }
1053
1054                FinderCache.putResult(finderClassNameCacheEnabled,
1055                    finderClassName, finderMethodName, finderParams,
1056                    finderArgs, count);
1057
1058                return count.intValue();
1059            }
1060            catch (Exception e) {
1061                throw HibernateUtil.processException(e);
1062            }
1063            finally {
1064                closeSession(session);
1065            }
1066        }
1067        else {
1068            return ((Long)result).intValue();
1069        }
1070    }
1071
1072    public int countAll() throws SystemException {
1073        boolean finderClassNameCacheEnabled = UserIdMapperModelImpl.CACHE_ENABLED;
1074        String finderClassName = UserIdMapper.class.getName();
1075        String finderMethodName = "countAll";
1076        String[] finderParams = new String[] {  };
1077        Object[] finderArgs = new Object[] {  };
1078
1079        Object result = null;
1080
1081        if (finderClassNameCacheEnabled) {
1082            result = FinderCache.getResult(finderClassName, finderMethodName,
1083                    finderParams, finderArgs, getSessionFactory());
1084        }
1085
1086        if (result == null) {
1087            Session session = null;
1088
1089            try {
1090                session = openSession();
1091
1092                Query q = session.createQuery(
1093                        "SELECT COUNT(*) FROM com.liferay.portal.model.UserIdMapper");
1094
1095                Long count = null;
1096
1097                Iterator itr = q.list().iterator();
1098
1099                if (itr.hasNext()) {
1100                    count = (Long)itr.next();
1101                }
1102
1103                if (count == null) {
1104                    count = new Long(0);
1105                }
1106
1107                FinderCache.putResult(finderClassNameCacheEnabled,
1108                    finderClassName, finderMethodName, finderParams,
1109                    finderArgs, count);
1110
1111                return count.intValue();
1112            }
1113            catch (Exception e) {
1114                throw HibernateUtil.processException(e);
1115            }
1116            finally {
1117                closeSession(session);
1118            }
1119        }
1120        else {
1121            return ((Long)result).intValue();
1122        }
1123    }
1124
1125    protected void initDao() {
1126    }
1127
1128    private static ModelListener _getListener() {
1129        if (Validator.isNotNull(_LISTENER)) {
1130            try {
1131                return (ModelListener)Class.forName(_LISTENER).newInstance();
1132            }
1133            catch (Exception e) {
1134                _log.error(e);
1135            }
1136        }
1137
1138        return null;
1139    }
1140
1141    private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(
1142                "value.object.listener.com.liferay.portal.model.UserIdMapper"));
1143    private static Log _log = LogFactory.getLog(UserIdMapperPersistenceImpl.class);
1144}