1   /**
2    * Copyright (c) 2000-2007 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.NoSuchPasswordTrackerException;
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.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.PasswordTracker;
33  import com.liferay.portal.model.impl.PasswordTrackerImpl;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  import com.liferay.portal.spring.hibernate.FinderCache;
36  import com.liferay.portal.spring.hibernate.HibernateUtil;
37  
38  import com.liferay.util.dao.hibernate.QueryUtil;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  import org.hibernate.Query;
44  import org.hibernate.Session;
45  
46  import java.util.Collections;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  /**
51   * <a href="PasswordTrackerPersistenceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class PasswordTrackerPersistenceImpl extends BasePersistence
57      implements PasswordTrackerPersistence {
58      public PasswordTracker create(long passwordTrackerId) {
59          PasswordTracker passwordTracker = new PasswordTrackerImpl();
60          passwordTracker.setNew(true);
61          passwordTracker.setPrimaryKey(passwordTrackerId);
62  
63          return passwordTracker;
64      }
65  
66      public PasswordTracker remove(long passwordTrackerId)
67          throws NoSuchPasswordTrackerException, SystemException {
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              PasswordTracker passwordTracker = (PasswordTracker)session.get(PasswordTrackerImpl.class,
74                      new Long(passwordTrackerId));
75  
76              if (passwordTracker == null) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn("No PasswordTracker exists with the primary key " +
79                          passwordTrackerId);
80                  }
81  
82                  throw new NoSuchPasswordTrackerException(
83                      "No PasswordTracker exists with the primary key " +
84                      passwordTrackerId);
85              }
86  
87              return remove(passwordTracker);
88          }
89          catch (NoSuchPasswordTrackerException nsee) {
90              throw nsee;
91          }
92          catch (Exception e) {
93              throw HibernateUtil.processException(e);
94          }
95          finally {
96              closeSession(session);
97          }
98      }
99  
100     public PasswordTracker remove(PasswordTracker passwordTracker)
101         throws SystemException {
102         Session session = null;
103 
104         try {
105             session = openSession();
106             session.delete(passwordTracker);
107             session.flush();
108 
109             return passwordTracker;
110         }
111         catch (Exception e) {
112             throw HibernateUtil.processException(e);
113         }
114         finally {
115             closeSession(session);
116             FinderCache.clearCache(PasswordTracker.class.getName());
117         }
118     }
119 
120     public PasswordTracker update(
121         com.liferay.portal.model.PasswordTracker passwordTracker)
122         throws SystemException {
123         return update(passwordTracker, false);
124     }
125 
126     public PasswordTracker update(
127         com.liferay.portal.model.PasswordTracker passwordTracker, boolean merge)
128         throws SystemException {
129         Session session = null;
130 
131         try {
132             session = openSession();
133 
134             if (merge) {
135                 session.merge(passwordTracker);
136             }
137             else {
138                 if (passwordTracker.isNew()) {
139                     session.save(passwordTracker);
140                 }
141             }
142 
143             session.flush();
144             passwordTracker.setNew(false);
145 
146             return passwordTracker;
147         }
148         catch (Exception e) {
149             throw HibernateUtil.processException(e);
150         }
151         finally {
152             closeSession(session);
153             FinderCache.clearCache(PasswordTracker.class.getName());
154         }
155     }
156 
157     public PasswordTracker findByPrimaryKey(long passwordTrackerId)
158         throws NoSuchPasswordTrackerException, SystemException {
159         PasswordTracker passwordTracker = fetchByPrimaryKey(passwordTrackerId);
160 
161         if (passwordTracker == null) {
162             if (_log.isWarnEnabled()) {
163                 _log.warn("No PasswordTracker exists with the primary key " +
164                     passwordTrackerId);
165             }
166 
167             throw new NoSuchPasswordTrackerException(
168                 "No PasswordTracker exists with the primary key " +
169                 passwordTrackerId);
170         }
171 
172         return passwordTracker;
173     }
174 
175     public PasswordTracker fetchByPrimaryKey(long passwordTrackerId)
176         throws SystemException {
177         Session session = null;
178 
179         try {
180             session = openSession();
181 
182             return (PasswordTracker)session.get(PasswordTrackerImpl.class,
183                 new Long(passwordTrackerId));
184         }
185         catch (Exception e) {
186             throw HibernateUtil.processException(e);
187         }
188         finally {
189             closeSession(session);
190         }
191     }
192 
193     public List findByUserId(long userId) throws SystemException {
194         String finderClassName = PasswordTracker.class.getName();
195         String finderMethodName = "findByUserId";
196         String[] finderParams = new String[] { Long.class.getName() };
197         Object[] finderArgs = new Object[] { new Long(userId) };
198         Object result = FinderCache.getResult(finderClassName,
199                 finderMethodName, finderParams, finderArgs, getSessionFactory());
200 
201         if (result == null) {
202             Session session = null;
203 
204             try {
205                 session = openSession();
206 
207                 StringMaker query = new StringMaker();
208                 query.append(
209                     "FROM com.liferay.portal.model.PasswordTracker WHERE ");
210                 query.append("userId = ?");
211                 query.append(" ");
212                 query.append("ORDER BY ");
213                 query.append("userId DESC").append(", ");
214                 query.append("createDate DESC");
215 
216                 Query q = session.createQuery(query.toString());
217                 int queryPos = 0;
218                 q.setLong(queryPos++, userId);
219 
220                 List list = q.list();
221                 FinderCache.putResult(finderClassName, finderMethodName,
222                     finderParams, finderArgs, list);
223 
224                 return list;
225             }
226             catch (Exception e) {
227                 throw HibernateUtil.processException(e);
228             }
229             finally {
230                 closeSession(session);
231             }
232         }
233         else {
234             return (List)result;
235         }
236     }
237 
238     public List findByUserId(long userId, int begin, int end)
239         throws SystemException {
240         return findByUserId(userId, begin, end, null);
241     }
242 
243     public List findByUserId(long userId, int begin, int end,
244         OrderByComparator obc) throws SystemException {
245         String finderClassName = PasswordTracker.class.getName();
246         String finderMethodName = "findByUserId";
247         String[] finderParams = new String[] {
248                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
249                 "com.liferay.portal.kernel.util.OrderByComparator"
250             };
251         Object[] finderArgs = new Object[] {
252                 new Long(userId), String.valueOf(begin), String.valueOf(end),
253                 String.valueOf(obc)
254             };
255         Object result = FinderCache.getResult(finderClassName,
256                 finderMethodName, finderParams, finderArgs, getSessionFactory());
257 
258         if (result == null) {
259             Session session = null;
260 
261             try {
262                 session = openSession();
263 
264                 StringMaker query = new StringMaker();
265                 query.append(
266                     "FROM com.liferay.portal.model.PasswordTracker WHERE ");
267                 query.append("userId = ?");
268                 query.append(" ");
269 
270                 if (obc != null) {
271                     query.append("ORDER BY ");
272                     query.append(obc.getOrderBy());
273                 }
274                 else {
275                     query.append("ORDER BY ");
276                     query.append("userId DESC").append(", ");
277                     query.append("createDate DESC");
278                 }
279 
280                 Query q = session.createQuery(query.toString());
281                 int queryPos = 0;
282                 q.setLong(queryPos++, userId);
283 
284                 List list = QueryUtil.list(q, getDialect(), begin, end);
285                 FinderCache.putResult(finderClassName, finderMethodName,
286                     finderParams, finderArgs, list);
287 
288                 return list;
289             }
290             catch (Exception e) {
291                 throw HibernateUtil.processException(e);
292             }
293             finally {
294                 closeSession(session);
295             }
296         }
297         else {
298             return (List)result;
299         }
300     }
301 
302     public PasswordTracker findByUserId_First(long userId, OrderByComparator obc)
303         throws NoSuchPasswordTrackerException, SystemException {
304         List list = findByUserId(userId, 0, 1, obc);
305 
306         if (list.size() == 0) {
307             StringMaker msg = new StringMaker();
308             msg.append("No PasswordTracker exists with the key ");
309             msg.append(StringPool.OPEN_CURLY_BRACE);
310             msg.append("userId=");
311             msg.append(userId);
312             msg.append(StringPool.CLOSE_CURLY_BRACE);
313             throw new NoSuchPasswordTrackerException(msg.toString());
314         }
315         else {
316             return (PasswordTracker)list.get(0);
317         }
318     }
319 
320     public PasswordTracker findByUserId_Last(long userId, OrderByComparator obc)
321         throws NoSuchPasswordTrackerException, SystemException {
322         int count = countByUserId(userId);
323         List list = findByUserId(userId, count - 1, count, obc);
324 
325         if (list.size() == 0) {
326             StringMaker msg = new StringMaker();
327             msg.append("No PasswordTracker exists with the key ");
328             msg.append(StringPool.OPEN_CURLY_BRACE);
329             msg.append("userId=");
330             msg.append(userId);
331             msg.append(StringPool.CLOSE_CURLY_BRACE);
332             throw new NoSuchPasswordTrackerException(msg.toString());
333         }
334         else {
335             return (PasswordTracker)list.get(0);
336         }
337     }
338 
339     public PasswordTracker[] findByUserId_PrevAndNext(long passwordTrackerId,
340         long userId, OrderByComparator obc)
341         throws NoSuchPasswordTrackerException, SystemException {
342         PasswordTracker passwordTracker = findByPrimaryKey(passwordTrackerId);
343         int count = countByUserId(userId);
344         Session session = null;
345 
346         try {
347             session = openSession();
348 
349             StringMaker query = new StringMaker();
350             query.append("FROM com.liferay.portal.model.PasswordTracker WHERE ");
351             query.append("userId = ?");
352             query.append(" ");
353 
354             if (obc != null) {
355                 query.append("ORDER BY ");
356                 query.append(obc.getOrderBy());
357             }
358             else {
359                 query.append("ORDER BY ");
360                 query.append("userId DESC").append(", ");
361                 query.append("createDate DESC");
362             }
363 
364             Query q = session.createQuery(query.toString());
365             int queryPos = 0;
366             q.setLong(queryPos++, userId);
367 
368             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
369                     passwordTracker);
370             PasswordTracker[] array = new PasswordTrackerImpl[3];
371             array[0] = (PasswordTracker)objArray[0];
372             array[1] = (PasswordTracker)objArray[1];
373             array[2] = (PasswordTracker)objArray[2];
374 
375             return array;
376         }
377         catch (Exception e) {
378             throw HibernateUtil.processException(e);
379         }
380         finally {
381             closeSession(session);
382         }
383     }
384 
385     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
386         throws SystemException {
387         Session session = null;
388 
389         try {
390             session = openSession();
391 
392             DynamicQuery query = queryInitializer.initialize(session);
393 
394             return query.list();
395         }
396         catch (Exception e) {
397             throw HibernateUtil.processException(e);
398         }
399         finally {
400             closeSession(session);
401         }
402     }
403 
404     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
405         int begin, int end) throws SystemException {
406         Session session = null;
407 
408         try {
409             session = openSession();
410 
411             DynamicQuery query = queryInitializer.initialize(session);
412             query.setLimit(begin, end);
413 
414             return query.list();
415         }
416         catch (Exception e) {
417             throw HibernateUtil.processException(e);
418         }
419         finally {
420             closeSession(session);
421         }
422     }
423 
424     public List findAll() throws SystemException {
425         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
426     }
427 
428     public List findAll(int begin, int end) throws SystemException {
429         return findAll(begin, end, null);
430     }
431 
432     public List findAll(int begin, int end, OrderByComparator obc)
433         throws SystemException {
434         String finderClassName = PasswordTracker.class.getName();
435         String finderMethodName = "findAll";
436         String[] finderParams = new String[] {
437                 "java.lang.Integer", "java.lang.Integer",
438                 "com.liferay.portal.kernel.util.OrderByComparator"
439             };
440         Object[] finderArgs = new Object[] {
441                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
442             };
443         Object result = FinderCache.getResult(finderClassName,
444                 finderMethodName, finderParams, finderArgs, getSessionFactory());
445 
446         if (result == null) {
447             Session session = null;
448 
449             try {
450                 session = openSession();
451 
452                 StringMaker query = new StringMaker();
453                 query.append("FROM com.liferay.portal.model.PasswordTracker ");
454 
455                 if (obc != null) {
456                     query.append("ORDER BY ");
457                     query.append(obc.getOrderBy());
458                 }
459                 else {
460                     query.append("ORDER BY ");
461                     query.append("userId DESC").append(", ");
462                     query.append("createDate DESC");
463                 }
464 
465                 Query q = session.createQuery(query.toString());
466                 List list = QueryUtil.list(q, getDialect(), begin, end);
467 
468                 if (obc == null) {
469                     Collections.sort(list);
470                 }
471 
472                 FinderCache.putResult(finderClassName, finderMethodName,
473                     finderParams, finderArgs, list);
474 
475                 return list;
476             }
477             catch (Exception e) {
478                 throw HibernateUtil.processException(e);
479             }
480             finally {
481                 closeSession(session);
482             }
483         }
484         else {
485             return (List)result;
486         }
487     }
488 
489     public void removeByUserId(long userId) throws SystemException {
490         Iterator itr = findByUserId(userId).iterator();
491 
492         while (itr.hasNext()) {
493             PasswordTracker passwordTracker = (PasswordTracker)itr.next();
494             remove(passwordTracker);
495         }
496     }
497 
498     public void removeAll() throws SystemException {
499         Iterator itr = findAll().iterator();
500 
501         while (itr.hasNext()) {
502             remove((PasswordTracker)itr.next());
503         }
504     }
505 
506     public int countByUserId(long userId) throws SystemException {
507         String finderClassName = PasswordTracker.class.getName();
508         String finderMethodName = "countByUserId";
509         String[] finderParams = new String[] { Long.class.getName() };
510         Object[] finderArgs = new Object[] { new Long(userId) };
511         Object result = FinderCache.getResult(finderClassName,
512                 finderMethodName, finderParams, finderArgs, getSessionFactory());
513 
514         if (result == null) {
515             Session session = null;
516 
517             try {
518                 session = openSession();
519 
520                 StringMaker query = new StringMaker();
521                 query.append("SELECT COUNT(*) ");
522                 query.append(
523                     "FROM com.liferay.portal.model.PasswordTracker WHERE ");
524                 query.append("userId = ?");
525                 query.append(" ");
526 
527                 Query q = session.createQuery(query.toString());
528                 int queryPos = 0;
529                 q.setLong(queryPos++, userId);
530 
531                 Long count = null;
532                 Iterator itr = q.list().iterator();
533 
534                 if (itr.hasNext()) {
535                     count = (Long)itr.next();
536                 }
537 
538                 if (count == null) {
539                     count = new Long(0);
540                 }
541 
542                 FinderCache.putResult(finderClassName, finderMethodName,
543                     finderParams, finderArgs, count);
544 
545                 return count.intValue();
546             }
547             catch (Exception e) {
548                 throw HibernateUtil.processException(e);
549             }
550             finally {
551                 closeSession(session);
552             }
553         }
554         else {
555             return ((Long)result).intValue();
556         }
557     }
558 
559     public int countAll() throws SystemException {
560         String finderClassName = PasswordTracker.class.getName();
561         String finderMethodName = "countAll";
562         String[] finderParams = new String[] {  };
563         Object[] finderArgs = new Object[] {  };
564         Object result = FinderCache.getResult(finderClassName,
565                 finderMethodName, finderParams, finderArgs, getSessionFactory());
566 
567         if (result == null) {
568             Session session = null;
569 
570             try {
571                 session = openSession();
572 
573                 StringMaker query = new StringMaker();
574                 query.append("SELECT COUNT(*) ");
575                 query.append("FROM com.liferay.portal.model.PasswordTracker");
576 
577                 Query q = session.createQuery(query.toString());
578                 Long count = null;
579                 Iterator itr = q.list().iterator();
580 
581                 if (itr.hasNext()) {
582                     count = (Long)itr.next();
583                 }
584 
585                 if (count == null) {
586                     count = new Long(0);
587                 }
588 
589                 FinderCache.putResult(finderClassName, finderMethodName,
590                     finderParams, finderArgs, count);
591 
592                 return count.intValue();
593             }
594             catch (Exception e) {
595                 throw HibernateUtil.processException(e);
596             }
597             finally {
598                 closeSession(session);
599             }
600         }
601         else {
602             return ((Long)result).intValue();
603         }
604     }
605 
606     protected void initDao() {
607     }
608 
609     private static Log _log = LogFactory.getLog(PasswordTrackerPersistenceImpl.class);
610 }