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