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