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