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