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