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