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