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