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("SELECT tagsSource FROM TagsSource tagsSource ");
376 
377                 if (obc != null) {
378                     query.append("ORDER BY ");
379 
380                     String[] orderByFields = obc.getOrderByFields();
381 
382                     for (int i = 0; i < orderByFields.length; i++) {
383                         query.append("tagsSource.");
384                         query.append(orderByFields[i]);
385 
386                         if (obc.isAscending()) {
387                             query.append(" ASC");
388                         }
389                         else {
390                             query.append(" DESC");
391                         }
392 
393                         if ((i + 1) < orderByFields.length) {
394                             query.append(", ");
395                         }
396                     }
397                 }
398 
399                 Query q = session.createQuery(query.toString());
400 
401                 if (obc == null) {
402                     list = (List<TagsSource>)QueryUtil.list(q, getDialect(),
403                             start, end, false);
404 
405                     Collections.sort(list);
406                 }
407                 else {
408                     list = (List<TagsSource>)QueryUtil.list(q, getDialect(),
409                             start, end);
410                 }
411             }
412             catch (Exception e) {
413                 throw processException(e);
414             }
415             finally {
416                 if (list == null) {
417                     list = new ArrayList<TagsSource>();
418                 }
419 
420                 cacheResult(list);
421 
422                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
423 
424                 closeSession(session);
425             }
426         }
427 
428         return list;
429     }
430 
431     public void removeAll() throws SystemException {
432         for (TagsSource tagsSource : findAll()) {
433             remove(tagsSource);
434         }
435     }
436 
437     public int countAll() throws SystemException {
438         Object[] finderArgs = new Object[0];
439 
440         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
441                 finderArgs, this);
442 
443         if (count == null) {
444             Session session = null;
445 
446             try {
447                 session = openSession();
448 
449                 Query q = session.createQuery(
450                         "SELECT COUNT(tagsSource) FROM TagsSource tagsSource");
451 
452                 count = (Long)q.uniqueResult();
453             }
454             catch (Exception e) {
455                 throw processException(e);
456             }
457             finally {
458                 if (count == null) {
459                     count = Long.valueOf(0);
460                 }
461 
462                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
463                     count);
464 
465                 closeSession(session);
466             }
467         }
468 
469         return count.intValue();
470     }
471 
472     public void afterPropertiesSet() {
473         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
474                     com.liferay.portal.util.PropsUtil.get(
475                         "value.object.listener.com.liferay.portlet.tags.model.TagsSource")));
476 
477         if (listenerClassNames.length > 0) {
478             try {
479                 List<ModelListener<TagsSource>> listenersList = new ArrayList<ModelListener<TagsSource>>();
480 
481                 for (String listenerClassName : listenerClassNames) {
482                     listenersList.add((ModelListener<TagsSource>)Class.forName(
483                             listenerClassName).newInstance());
484                 }
485 
486                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
487             }
488             catch (Exception e) {
489                 _log.error(e);
490             }
491         }
492     }
493 
494     @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsAssetPersistence.impl")
495     protected com.liferay.portlet.tags.service.persistence.TagsAssetPersistence tagsAssetPersistence;
496     @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsEntryPersistence.impl")
497     protected com.liferay.portlet.tags.service.persistence.TagsEntryPersistence tagsEntryPersistence;
498     @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsPropertyPersistence.impl")
499     protected com.liferay.portlet.tags.service.persistence.TagsPropertyPersistence tagsPropertyPersistence;
500     @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsSourcePersistence.impl")
501     protected com.liferay.portlet.tags.service.persistence.TagsSourcePersistence tagsSourcePersistence;
502     private static Log _log = LogFactoryUtil.getLog(TagsSourcePersistenceImpl.class);
503 }