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             if (BatchSessionUtil.isEnabled()) {
130                 Object staleObject = session.get(WebDAVPropsImpl.class,
131                         webDAVProps.getPrimaryKeyObj());
132 
133                 if (staleObject != null) {
134                     session.evict(staleObject);
135                 }
136             }
137 
138             session.delete(webDAVProps);
139 
140             session.flush();
141 
142             return webDAVProps;
143         }
144         catch (Exception e) {
145             throw processException(e);
146         }
147         finally {
148             closeSession(session);
149 
150             FinderCacheUtil.clearCache(WebDAVProps.class.getName());
151         }
152     }
153 
154     /**
155      * @deprecated Use <code>update(WebDAVProps webDAVProps, boolean merge)</code>.
156      */
157     public WebDAVProps update(WebDAVProps webDAVProps)
158         throws SystemException {
159         if (_log.isWarnEnabled()) {
160             _log.warn(
161                 "Using the deprecated update(WebDAVProps webDAVProps) method. Use update(WebDAVProps webDAVProps, boolean merge) instead.");
162         }
163 
164         return update(webDAVProps, false);
165     }
166 
167     /**
168      * Add, update, or merge, the entity. This method also calls the model
169      * listeners to trigger the proper events associated with adding, deleting,
170      * or updating an entity.
171      *
172      * @param        webDAVProps the entity to add, update, or merge
173      * @param        merge boolean value for whether to merge the entity. The
174      *                default value is false. Setting merge to true is more
175      *                expensive and should only be true when webDAVProps is
176      *                transient. See LEP-5473 for a detailed discussion of this
177      *                method.
178      * @return        true if the portlet can be displayed via Ajax
179      */
180     public WebDAVProps update(WebDAVProps webDAVProps, boolean merge)
181         throws SystemException {
182         boolean isNew = webDAVProps.isNew();
183 
184         if (_listeners.length > 0) {
185             for (ModelListener listener : _listeners) {
186                 if (isNew) {
187                     listener.onBeforeCreate(webDAVProps);
188                 }
189                 else {
190                     listener.onBeforeUpdate(webDAVProps);
191                 }
192             }
193         }
194 
195         webDAVProps = updateImpl(webDAVProps, merge);
196 
197         if (_listeners.length > 0) {
198             for (ModelListener listener : _listeners) {
199                 if (isNew) {
200                     listener.onAfterCreate(webDAVProps);
201                 }
202                 else {
203                     listener.onAfterUpdate(webDAVProps);
204                 }
205             }
206         }
207 
208         return webDAVProps;
209     }
210 
211     public WebDAVProps updateImpl(
212         com.liferay.portal.model.WebDAVProps webDAVProps, boolean merge)
213         throws SystemException {
214         Session session = null;
215 
216         try {
217             session = openSession();
218 
219             BatchSessionUtil.update(session, webDAVProps, merge);
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 = null;
460 
461                 if (obc == null) {
462                     list = (List<WebDAVProps>)QueryUtil.list(q, getDialect(),
463                             start, end, false);
464 
465                     Collections.sort(list);
466                 }
467                 else {
468                     list = (List<WebDAVProps>)QueryUtil.list(q, getDialect(),
469                             start, end);
470                 }
471 
472                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
473                     finderClassName, finderMethodName, finderParams,
474                     finderArgs, list);
475 
476                 return list;
477             }
478             catch (Exception e) {
479                 throw processException(e);
480             }
481             finally {
482                 closeSession(session);
483             }
484         }
485         else {
486             return (List<WebDAVProps>)result;
487         }
488     }
489 
490     public void removeByC_C(long classNameId, long classPK)
491         throws NoSuchWebDAVPropsException, SystemException {
492         WebDAVProps webDAVProps = findByC_C(classNameId, classPK);
493 
494         remove(webDAVProps);
495     }
496 
497     public void removeAll() throws SystemException {
498         for (WebDAVProps webDAVProps : findAll()) {
499             remove(webDAVProps);
500         }
501     }
502 
503     public int countByC_C(long classNameId, long classPK)
504         throws SystemException {
505         boolean finderClassNameCacheEnabled = WebDAVPropsModelImpl.CACHE_ENABLED;
506         String finderClassName = WebDAVProps.class.getName();
507         String finderMethodName = "countByC_C";
508         String[] finderParams = new String[] {
509                 Long.class.getName(), Long.class.getName()
510             };
511         Object[] finderArgs = new Object[] {
512                 new Long(classNameId), new Long(classPK)
513             };
514 
515         Object result = null;
516 
517         if (finderClassNameCacheEnabled) {
518             result = FinderCacheUtil.getResult(finderClassName,
519                     finderMethodName, finderParams, finderArgs, this);
520         }
521 
522         if (result == null) {
523             Session session = null;
524 
525             try {
526                 session = openSession();
527 
528                 StringBuilder query = new StringBuilder();
529 
530                 query.append("SELECT COUNT(*) ");
531                 query.append("FROM com.liferay.portal.model.WebDAVProps WHERE ");
532 
533                 query.append("classNameId = ?");
534 
535                 query.append(" AND ");
536 
537                 query.append("classPK = ?");
538 
539                 query.append(" ");
540 
541                 Query q = session.createQuery(query.toString());
542 
543                 QueryPos qPos = QueryPos.getInstance(q);
544 
545                 qPos.add(classNameId);
546 
547                 qPos.add(classPK);
548 
549                 Long count = null;
550 
551                 Iterator<Long> itr = q.list().iterator();
552 
553                 if (itr.hasNext()) {
554                     count = itr.next();
555                 }
556 
557                 if (count == null) {
558                     count = new Long(0);
559                 }
560 
561                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
562                     finderClassName, finderMethodName, finderParams,
563                     finderArgs, count);
564 
565                 return count.intValue();
566             }
567             catch (Exception e) {
568                 throw processException(e);
569             }
570             finally {
571                 closeSession(session);
572             }
573         }
574         else {
575             return ((Long)result).intValue();
576         }
577     }
578 
579     public int countAll() throws SystemException {
580         boolean finderClassNameCacheEnabled = WebDAVPropsModelImpl.CACHE_ENABLED;
581         String finderClassName = WebDAVProps.class.getName();
582         String finderMethodName = "countAll";
583         String[] finderParams = new String[] {  };
584         Object[] finderArgs = new Object[] {  };
585 
586         Object result = null;
587 
588         if (finderClassNameCacheEnabled) {
589             result = FinderCacheUtil.getResult(finderClassName,
590                     finderMethodName, finderParams, finderArgs, this);
591         }
592 
593         if (result == null) {
594             Session session = null;
595 
596             try {
597                 session = openSession();
598 
599                 Query q = session.createQuery(
600                         "SELECT COUNT(*) FROM com.liferay.portal.model.WebDAVProps");
601 
602                 Long count = null;
603 
604                 Iterator<Long> itr = q.list().iterator();
605 
606                 if (itr.hasNext()) {
607                     count = itr.next();
608                 }
609 
610                 if (count == null) {
611                     count = new Long(0);
612                 }
613 
614                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
615                     finderClassName, finderMethodName, finderParams,
616                     finderArgs, count);
617 
618                 return count.intValue();
619             }
620             catch (Exception e) {
621                 throw processException(e);
622             }
623             finally {
624                 closeSession(session);
625             }
626         }
627         else {
628             return ((Long)result).intValue();
629         }
630     }
631 
632     public void registerListener(ModelListener listener) {
633         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
634 
635         listeners.add(listener);
636 
637         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
638     }
639 
640     public void unregisterListener(ModelListener listener) {
641         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
642 
643         listeners.remove(listener);
644 
645         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
646     }
647 
648     public void afterPropertiesSet() {
649         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
650                     com.liferay.portal.util.PropsUtil.get(
651                         "value.object.listener.com.liferay.portal.model.WebDAVProps")));
652 
653         if (listenerClassNames.length > 0) {
654             try {
655                 List<ModelListener> listeners = new ArrayList<ModelListener>();
656 
657                 for (String listenerClassName : listenerClassNames) {
658                     listeners.add((ModelListener)Class.forName(
659                             listenerClassName).newInstance());
660                 }
661 
662                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
663             }
664             catch (Exception e) {
665                 _log.error(e);
666             }
667         }
668     }
669 
670     private static Log _log = LogFactory.getLog(WebDAVPropsPersistenceImpl.class);
671     private ModelListener[] _listeners = new ModelListener[0];
672 }