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.portlet.tags.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
27  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
28  import com.liferay.portal.kernel.dao.orm.Query;
29  import com.liferay.portal.kernel.dao.orm.QueryUtil;
30  import com.liferay.portal.kernel.dao.orm.Session;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.ListUtil;
33  import com.liferay.portal.kernel.util.OrderByComparator;
34  import com.liferay.portal.kernel.util.StringUtil;
35  import com.liferay.portal.model.ModelListener;
36  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
37  
38  import com.liferay.portlet.tags.NoSuchSourceException;
39  import com.liferay.portlet.tags.model.TagsSource;
40  import com.liferay.portlet.tags.model.impl.TagsSourceImpl;
41  import com.liferay.portlet.tags.model.impl.TagsSourceModelImpl;
42  
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  
46  import java.util.ArrayList;
47  import java.util.Collections;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  /**
52   * <a href="TagsSourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class TagsSourcePersistenceImpl extends BasePersistenceImpl
58      implements TagsSourcePersistence {
59      public TagsSource create(long sourceId) {
60          TagsSource tagsSource = new TagsSourceImpl();
61  
62          tagsSource.setNew(true);
63          tagsSource.setPrimaryKey(sourceId);
64  
65          return tagsSource;
66      }
67  
68      public TagsSource remove(long sourceId)
69          throws NoSuchSourceException, SystemException {
70          Session session = null;
71  
72          try {
73              session = openSession();
74  
75              TagsSource tagsSource = (TagsSource)session.get(TagsSourceImpl.class,
76                      new Long(sourceId));
77  
78              if (tagsSource == null) {
79                  if (_log.isWarnEnabled()) {
80                      _log.warn("No TagsSource exists with the primary key " +
81                          sourceId);
82                  }
83  
84                  throw new NoSuchSourceException(
85                      "No TagsSource exists with the primary key " + sourceId);
86              }
87  
88              return remove(tagsSource);
89          }
90          catch (NoSuchSourceException nsee) {
91              throw nsee;
92          }
93          catch (Exception e) {
94              throw processException(e);
95          }
96          finally {
97              closeSession(session);
98          }
99      }
100 
101     public TagsSource remove(TagsSource tagsSource) throws SystemException {
102         if (_listeners.length > 0) {
103             for (ModelListener listener : _listeners) {
104                 listener.onBeforeRemove(tagsSource);
105             }
106         }
107 
108         tagsSource = removeImpl(tagsSource);
109 
110         if (_listeners.length > 0) {
111             for (ModelListener listener : _listeners) {
112                 listener.onAfterRemove(tagsSource);
113             }
114         }
115 
116         return tagsSource;
117     }
118 
119     protected TagsSource removeImpl(TagsSource tagsSource)
120         throws SystemException {
121         Session session = null;
122 
123         try {
124             session = openSession();
125 
126             session.delete(tagsSource);
127 
128             session.flush();
129 
130             return tagsSource;
131         }
132         catch (Exception e) {
133             throw processException(e);
134         }
135         finally {
136             closeSession(session);
137 
138             FinderCacheUtil.clearCache(TagsSource.class.getName());
139         }
140     }
141 
142     /**
143      * @deprecated Use <code>update(TagsSource tagsSource, boolean merge)</code>.
144      */
145     public TagsSource update(TagsSource tagsSource) throws SystemException {
146         if (_log.isWarnEnabled()) {
147             _log.warn(
148                 "Using the deprecated update(TagsSource tagsSource) method. Use update(TagsSource tagsSource, boolean merge) instead.");
149         }
150 
151         return update(tagsSource, false);
152     }
153 
154     /**
155      * Add, update, or merge, the entity. This method also calls the model
156      * listeners to trigger the proper events associated with adding, deleting,
157      * or updating an entity.
158      *
159      * @param        tagsSource the entity to add, update, or merge
160      * @param        merge boolean value for whether to merge the entity. The
161      *                default value is false. Setting merge to true is more
162      *                expensive and should only be true when tagsSource is
163      *                transient. See LEP-5473 for a detailed discussion of this
164      *                method.
165      * @return        true if the portlet can be displayed via Ajax
166      */
167     public TagsSource update(TagsSource tagsSource, boolean merge)
168         throws SystemException {
169         boolean isNew = tagsSource.isNew();
170 
171         if (_listeners.length > 0) {
172             for (ModelListener listener : _listeners) {
173                 if (isNew) {
174                     listener.onBeforeCreate(tagsSource);
175                 }
176                 else {
177                     listener.onBeforeUpdate(tagsSource);
178                 }
179             }
180         }
181 
182         tagsSource = updateImpl(tagsSource, merge);
183 
184         if (_listeners.length > 0) {
185             for (ModelListener listener : _listeners) {
186                 if (isNew) {
187                     listener.onAfterCreate(tagsSource);
188                 }
189                 else {
190                     listener.onAfterUpdate(tagsSource);
191                 }
192             }
193         }
194 
195         return tagsSource;
196     }
197 
198     public TagsSource updateImpl(
199         com.liferay.portlet.tags.model.TagsSource tagsSource, boolean merge)
200         throws SystemException {
201         Session session = null;
202 
203         try {
204             session = openSession();
205 
206             if (merge) {
207                 session.merge(tagsSource);
208             }
209             else {
210                 if (tagsSource.isNew()) {
211                     session.save(tagsSource);
212                 }
213             }
214 
215             session.flush();
216 
217             tagsSource.setNew(false);
218 
219             return tagsSource;
220         }
221         catch (Exception e) {
222             throw processException(e);
223         }
224         finally {
225             closeSession(session);
226 
227             FinderCacheUtil.clearCache(TagsSource.class.getName());
228         }
229     }
230 
231     public TagsSource findByPrimaryKey(long sourceId)
232         throws NoSuchSourceException, SystemException {
233         TagsSource tagsSource = fetchByPrimaryKey(sourceId);
234 
235         if (tagsSource == null) {
236             if (_log.isWarnEnabled()) {
237                 _log.warn("No TagsSource exists with the primary key " +
238                     sourceId);
239             }
240 
241             throw new NoSuchSourceException(
242                 "No TagsSource exists with the primary key " + sourceId);
243         }
244 
245         return tagsSource;
246     }
247 
248     public TagsSource fetchByPrimaryKey(long sourceId)
249         throws SystemException {
250         Session session = null;
251 
252         try {
253             session = openSession();
254 
255             return (TagsSource)session.get(TagsSourceImpl.class,
256                 new Long(sourceId));
257         }
258         catch (Exception e) {
259             throw processException(e);
260         }
261         finally {
262             closeSession(session);
263         }
264     }
265 
266     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
267         throws SystemException {
268         Session session = null;
269 
270         try {
271             session = openSession();
272 
273             dynamicQuery.compile(session);
274 
275             return dynamicQuery.list();
276         }
277         catch (Exception e) {
278             throw processException(e);
279         }
280         finally {
281             closeSession(session);
282         }
283     }
284 
285     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
286         int start, int end) throws SystemException {
287         Session session = null;
288 
289         try {
290             session = openSession();
291 
292             dynamicQuery.setLimit(start, end);
293 
294             dynamicQuery.compile(session);
295 
296             return dynamicQuery.list();
297         }
298         catch (Exception e) {
299             throw processException(e);
300         }
301         finally {
302             closeSession(session);
303         }
304     }
305 
306     public List<TagsSource> findAll() throws SystemException {
307         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
308     }
309 
310     public List<TagsSource> findAll(int start, int end)
311         throws SystemException {
312         return findAll(start, end, null);
313     }
314 
315     public List<TagsSource> findAll(int start, int end, OrderByComparator obc)
316         throws SystemException {
317         boolean finderClassNameCacheEnabled = TagsSourceModelImpl.CACHE_ENABLED;
318         String finderClassName = TagsSource.class.getName();
319         String finderMethodName = "findAll";
320         String[] finderParams = new String[] {
321                 "java.lang.Integer", "java.lang.Integer",
322                 "com.liferay.portal.kernel.util.OrderByComparator"
323             };
324         Object[] finderArgs = new Object[] {
325                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
326             };
327 
328         Object result = null;
329 
330         if (finderClassNameCacheEnabled) {
331             result = FinderCacheUtil.getResult(finderClassName,
332                     finderMethodName, finderParams, finderArgs, this);
333         }
334 
335         if (result == null) {
336             Session session = null;
337 
338             try {
339                 session = openSession();
340 
341                 StringBuilder query = new StringBuilder();
342 
343                 query.append("FROM com.liferay.portlet.tags.model.TagsSource ");
344 
345                 if (obc != null) {
346                     query.append("ORDER BY ");
347                     query.append(obc.getOrderBy());
348                 }
349 
350                 Query q = session.createQuery(query.toString());
351 
352                 List<TagsSource> list = (List<TagsSource>)QueryUtil.list(q,
353                         getDialect(), start, end);
354 
355                 if (obc == null) {
356                     Collections.sort(list);
357                 }
358 
359                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
360                     finderClassName, finderMethodName, finderParams,
361                     finderArgs, list);
362 
363                 return list;
364             }
365             catch (Exception e) {
366                 throw processException(e);
367             }
368             finally {
369                 closeSession(session);
370             }
371         }
372         else {
373             return (List<TagsSource>)result;
374         }
375     }
376 
377     public void removeAll() throws SystemException {
378         for (TagsSource tagsSource : findAll()) {
379             remove(tagsSource);
380         }
381     }
382 
383     public int countAll() throws SystemException {
384         boolean finderClassNameCacheEnabled = TagsSourceModelImpl.CACHE_ENABLED;
385         String finderClassName = TagsSource.class.getName();
386         String finderMethodName = "countAll";
387         String[] finderParams = new String[] {  };
388         Object[] finderArgs = new Object[] {  };
389 
390         Object result = null;
391 
392         if (finderClassNameCacheEnabled) {
393             result = FinderCacheUtil.getResult(finderClassName,
394                     finderMethodName, finderParams, finderArgs, this);
395         }
396 
397         if (result == null) {
398             Session session = null;
399 
400             try {
401                 session = openSession();
402 
403                 Query q = session.createQuery(
404                         "SELECT COUNT(*) FROM com.liferay.portlet.tags.model.TagsSource");
405 
406                 Long count = null;
407 
408                 Iterator<Long> itr = q.list().iterator();
409 
410                 if (itr.hasNext()) {
411                     count = itr.next();
412                 }
413 
414                 if (count == null) {
415                     count = new Long(0);
416                 }
417 
418                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
419                     finderClassName, finderMethodName, finderParams,
420                     finderArgs, count);
421 
422                 return count.intValue();
423             }
424             catch (Exception e) {
425                 throw processException(e);
426             }
427             finally {
428                 closeSession(session);
429             }
430         }
431         else {
432             return ((Long)result).intValue();
433         }
434     }
435 
436     public void registerListener(ModelListener listener) {
437         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
438 
439         listeners.add(listener);
440 
441         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
442     }
443 
444     public void unregisterListener(ModelListener listener) {
445         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
446 
447         listeners.remove(listener);
448 
449         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
450     }
451 
452     public void afterPropertiesSet() {
453         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
454                     com.liferay.portal.util.PropsUtil.get(
455                         "value.object.listener.com.liferay.portlet.tags.model.TagsSource")));
456 
457         if (listenerClassNames.length > 0) {
458             try {
459                 List<ModelListener> listeners = new ArrayList<ModelListener>();
460 
461                 for (String listenerClassName : listenerClassNames) {
462                     listeners.add((ModelListener)Class.forName(
463                             listenerClassName).newInstance());
464                 }
465 
466                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
467             }
468             catch (Exception e) {
469                 _log.error(e);
470             }
471         }
472     }
473 
474     private static Log _log = LogFactory.getLog(TagsSourcePersistenceImpl.class);
475     private ModelListener[] _listeners = new ModelListener[0];
476 }