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