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