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.SystemException;
26  import com.liferay.portal.kernel.annotation.BeanReference;
27  import com.liferay.portal.kernel.cache.CacheRegistry;
28  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
29  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
30  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
31  import com.liferay.portal.kernel.dao.orm.FinderPath;
32  import com.liferay.portal.kernel.dao.orm.Query;
33  import com.liferay.portal.kernel.dao.orm.QueryUtil;
34  import com.liferay.portal.kernel.dao.orm.Session;
35  import com.liferay.portal.kernel.log.Log;
36  import com.liferay.portal.kernel.log.LogFactoryUtil;
37  import com.liferay.portal.kernel.util.GetterUtil;
38  import com.liferay.portal.kernel.util.OrderByComparator;
39  import com.liferay.portal.kernel.util.StringUtil;
40  import com.liferay.portal.model.ModelListener;
41  import com.liferay.portal.service.persistence.BatchSessionUtil;
42  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
43  
44  import com.liferay.portlet.tags.NoSuchSourceException;
45  import com.liferay.portlet.tags.model.TagsSource;
46  import com.liferay.portlet.tags.model.impl.TagsSourceImpl;
47  import com.liferay.portlet.tags.model.impl.TagsSourceModelImpl;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.List;
52  
53  /**
54   * <a href="TagsSourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * <p>
57   * ServiceBuilder generated this class. Modifications in this class will be
58   * overwritten the next time is generated.
59   * </p>
60   *
61   * @author    Brian Wing Shun Chan
62   * @see       TagsSourcePersistence
63   * @see       TagsSourceUtil
64   * @generated
65   */
66  public class TagsSourcePersistenceImpl extends BasePersistenceImpl
67      implements TagsSourcePersistence {
68      public static final String FINDER_CLASS_NAME_ENTITY = TagsSourceImpl.class.getName();
69      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
70          ".List";
71      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(TagsSourceModelImpl.ENTITY_CACHE_ENABLED,
72              TagsSourceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
73              "findAll", new String[0]);
74      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(TagsSourceModelImpl.ENTITY_CACHE_ENABLED,
75              TagsSourceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
76              "countAll", new String[0]);
77  
78      public void cacheResult(TagsSource tagsSource) {
79          EntityCacheUtil.putResult(TagsSourceModelImpl.ENTITY_CACHE_ENABLED,
80              TagsSourceImpl.class, tagsSource.getPrimaryKey(), tagsSource);
81      }
82  
83      public void cacheResult(List<TagsSource> tagsSources) {
84          for (TagsSource tagsSource : tagsSources) {
85              if (EntityCacheUtil.getResult(
86                          TagsSourceModelImpl.ENTITY_CACHE_ENABLED,
87                          TagsSourceImpl.class, tagsSource.getPrimaryKey(), this) == null) {
88                  cacheResult(tagsSource);
89              }
90          }
91      }
92  
93      public void clearCache() {
94          CacheRegistry.clear(TagsSourceImpl.class.getName());
95          EntityCacheUtil.clearCache(TagsSourceImpl.class.getName());
96          FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
97          FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
98      }
99  
100     public TagsSource create(long sourceId) {
101         TagsSource tagsSource = new TagsSourceImpl();
102 
103         tagsSource.setNew(true);
104         tagsSource.setPrimaryKey(sourceId);
105 
106         return tagsSource;
107     }
108 
109     public TagsSource remove(long sourceId)
110         throws NoSuchSourceException, SystemException {
111         Session session = null;
112 
113         try {
114             session = openSession();
115 
116             TagsSource tagsSource = (TagsSource)session.get(TagsSourceImpl.class,
117                     new Long(sourceId));
118 
119             if (tagsSource == null) {
120                 if (_log.isWarnEnabled()) {
121                     _log.warn("No TagsSource exists with the primary key " +
122                         sourceId);
123                 }
124 
125                 throw new NoSuchSourceException(
126                     "No TagsSource exists with the primary key " + 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 #update(TagsSource, boolean merge)}.
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     /**
206      * Add, update, or merge, the entity. This method also calls the model
207      * listeners to trigger the proper events associated with adding, deleting,
208      * or updating an entity.
209      *
210      * @param  tagsSource the entity to add, update, or merge
211      * @param  merge boolean value for whether to merge the entity. The default
212      *         value is false. Setting merge to true is more expensive and
213      *         should only be true when tagsSource is transient. See
214      *         LEP-5473 for a detailed discussion of this method.
215      * @return the entity that was added, updated, or merged
216      */
217     public TagsSource update(TagsSource tagsSource, boolean merge)
218         throws SystemException {
219         boolean isNew = tagsSource.isNew();
220 
221         for (ModelListener<TagsSource> listener : listeners) {
222             if (isNew) {
223                 listener.onBeforeCreate(tagsSource);
224             }
225             else {
226                 listener.onBeforeUpdate(tagsSource);
227             }
228         }
229 
230         tagsSource = updateImpl(tagsSource, merge);
231 
232         for (ModelListener<TagsSource> listener : listeners) {
233             if (isNew) {
234                 listener.onAfterCreate(tagsSource);
235             }
236             else {
237                 listener.onAfterUpdate(tagsSource);
238             }
239         }
240 
241         return tagsSource;
242     }
243 
244     public TagsSource updateImpl(
245         com.liferay.portlet.tags.model.TagsSource tagsSource, boolean merge)
246         throws SystemException {
247         tagsSource = toUnwrappedModel(tagsSource);
248 
249         Session session = null;
250 
251         try {
252             session = openSession();
253 
254             BatchSessionUtil.update(session, tagsSource, merge);
255 
256             tagsSource.setNew(false);
257         }
258         catch (Exception e) {
259             throw processException(e);
260         }
261         finally {
262             closeSession(session);
263         }
264 
265         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
266 
267         EntityCacheUtil.putResult(TagsSourceModelImpl.ENTITY_CACHE_ENABLED,
268             TagsSourceImpl.class, tagsSource.getPrimaryKey(), tagsSource);
269 
270         return tagsSource;
271     }
272 
273     protected TagsSource toUnwrappedModel(TagsSource tagsSource) {
274         if (tagsSource instanceof TagsSourceImpl) {
275             return tagsSource;
276         }
277 
278         TagsSourceImpl tagsSourceImpl = new TagsSourceImpl();
279 
280         tagsSourceImpl.setNew(tagsSource.isNew());
281         tagsSourceImpl.setPrimaryKey(tagsSource.getPrimaryKey());
282 
283         tagsSourceImpl.setSourceId(tagsSource.getSourceId());
284         tagsSourceImpl.setParentSourceId(tagsSource.getParentSourceId());
285         tagsSourceImpl.setName(tagsSource.getName());
286         tagsSourceImpl.setAcronym(tagsSource.getAcronym());
287 
288         return tagsSourceImpl;
289     }
290 
291     public TagsSource findByPrimaryKey(long sourceId)
292         throws NoSuchSourceException, SystemException {
293         TagsSource tagsSource = fetchByPrimaryKey(sourceId);
294 
295         if (tagsSource == null) {
296             if (_log.isWarnEnabled()) {
297                 _log.warn("No TagsSource exists with the primary key " +
298                     sourceId);
299             }
300 
301             throw new NoSuchSourceException(
302                 "No TagsSource exists with the primary key " + sourceId);
303         }
304 
305         return tagsSource;
306     }
307 
308     public TagsSource fetchByPrimaryKey(long sourceId)
309         throws SystemException {
310         TagsSource tagsSource = (TagsSource)EntityCacheUtil.getResult(TagsSourceModelImpl.ENTITY_CACHE_ENABLED,
311                 TagsSourceImpl.class, sourceId, this);
312 
313         if (tagsSource == null) {
314             Session session = null;
315 
316             try {
317                 session = openSession();
318 
319                 tagsSource = (TagsSource)session.get(TagsSourceImpl.class,
320                         new Long(sourceId));
321             }
322             catch (Exception e) {
323                 throw processException(e);
324             }
325             finally {
326                 if (tagsSource != null) {
327                     cacheResult(tagsSource);
328                 }
329 
330                 closeSession(session);
331             }
332         }
333 
334         return tagsSource;
335     }
336 
337     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
338         throws SystemException {
339         Session session = null;
340 
341         try {
342             session = openSession();
343 
344             dynamicQuery.compile(session);
345 
346             return dynamicQuery.list();
347         }
348         catch (Exception e) {
349             throw processException(e);
350         }
351         finally {
352             closeSession(session);
353         }
354     }
355 
356     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
357         int start, int end) throws SystemException {
358         Session session = null;
359 
360         try {
361             session = openSession();
362 
363             dynamicQuery.setLimit(start, end);
364 
365             dynamicQuery.compile(session);
366 
367             return dynamicQuery.list();
368         }
369         catch (Exception e) {
370             throw processException(e);
371         }
372         finally {
373             closeSession(session);
374         }
375     }
376 
377     public List<TagsSource> findAll() throws SystemException {
378         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
379     }
380 
381     public List<TagsSource> findAll(int start, int end)
382         throws SystemException {
383         return findAll(start, end, null);
384     }
385 
386     public List<TagsSource> findAll(int start, int end, OrderByComparator obc)
387         throws SystemException {
388         Object[] finderArgs = new Object[] {
389                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
390             };
391 
392         List<TagsSource> list = (List<TagsSource>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
393                 finderArgs, this);
394 
395         if (list == null) {
396             Session session = null;
397 
398             try {
399                 session = openSession();
400 
401                 StringBuilder query = new StringBuilder();
402 
403                 query.append("SELECT tagsSource FROM TagsSource tagsSource ");
404 
405                 if (obc != null) {
406                     query.append("ORDER BY ");
407 
408                     String[] orderByFields = obc.getOrderByFields();
409 
410                     for (int i = 0; i < orderByFields.length; i++) {
411                         query.append("tagsSource.");
412                         query.append(orderByFields[i]);
413 
414                         if (obc.isAscending()) {
415                             query.append(" ASC");
416                         }
417                         else {
418                             query.append(" DESC");
419                         }
420 
421                         if ((i + 1) < orderByFields.length) {
422                             query.append(", ");
423                         }
424                     }
425                 }
426 
427                 Query q = session.createQuery(query.toString());
428 
429                 if (obc == null) {
430                     list = (List<TagsSource>)QueryUtil.list(q, getDialect(),
431                             start, end, false);
432 
433                     Collections.sort(list);
434                 }
435                 else {
436                     list = (List<TagsSource>)QueryUtil.list(q, getDialect(),
437                             start, end);
438                 }
439             }
440             catch (Exception e) {
441                 throw processException(e);
442             }
443             finally {
444                 if (list == null) {
445                     list = new ArrayList<TagsSource>();
446                 }
447 
448                 cacheResult(list);
449 
450                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
451 
452                 closeSession(session);
453             }
454         }
455 
456         return list;
457     }
458 
459     public void removeAll() throws SystemException {
460         for (TagsSource tagsSource : findAll()) {
461             remove(tagsSource);
462         }
463     }
464 
465     public int countAll() throws SystemException {
466         Object[] finderArgs = new Object[0];
467 
468         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
469                 finderArgs, this);
470 
471         if (count == null) {
472             Session session = null;
473 
474             try {
475                 session = openSession();
476 
477                 Query q = session.createQuery(
478                         "SELECT COUNT(tagsSource) FROM TagsSource tagsSource");
479 
480                 count = (Long)q.uniqueResult();
481             }
482             catch (Exception e) {
483                 throw processException(e);
484             }
485             finally {
486                 if (count == null) {
487                     count = Long.valueOf(0);
488                 }
489 
490                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
491                     count);
492 
493                 closeSession(session);
494             }
495         }
496 
497         return count.intValue();
498     }
499 
500     public void afterPropertiesSet() {
501         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
502                     com.liferay.portal.util.PropsUtil.get(
503                         "value.object.listener.com.liferay.portlet.tags.model.TagsSource")));
504 
505         if (listenerClassNames.length > 0) {
506             try {
507                 List<ModelListener<TagsSource>> listenersList = new ArrayList<ModelListener<TagsSource>>();
508 
509                 for (String listenerClassName : listenerClassNames) {
510                     listenersList.add((ModelListener<TagsSource>)Class.forName(
511                             listenerClassName).newInstance());
512                 }
513 
514                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
515             }
516             catch (Exception e) {
517                 _log.error(e);
518             }
519         }
520     }
521 
522     @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsAssetPersistence.impl")
523     protected com.liferay.portlet.tags.service.persistence.TagsAssetPersistence tagsAssetPersistence;
524     @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsEntryPersistence.impl")
525     protected com.liferay.portlet.tags.service.persistence.TagsEntryPersistence tagsEntryPersistence;
526     @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsPropertyPersistence.impl")
527     protected com.liferay.portlet.tags.service.persistence.TagsPropertyPersistence tagsPropertyPersistence;
528     @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsSourcePersistence.impl")
529     protected com.liferay.portlet.tags.service.persistence.TagsSourcePersistence tagsSourcePersistence;
530     @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsVocabularyPersistence.impl")
531     protected com.liferay.portlet.tags.service.persistence.TagsVocabularyPersistence tagsVocabularyPersistence;
532     @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
533     protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
534     @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
535     protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
536     private static Log _log = LogFactoryUtil.getLog(TagsSourcePersistenceImpl.class);
537 }