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