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