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