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