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         Session session = null;
159 
160         try {
161             session = openSession();
162 
163             if (tagsSource.isCachedModel() || BatchSessionUtil.isEnabled()) {
164                 Object staleObject = session.get(TagsSourceImpl.class,
165                         tagsSource.getPrimaryKeyObj());
166 
167                 if (staleObject != null) {
168                     session.evict(staleObject);
169                 }
170             }
171 
172             session.delete(tagsSource);
173 
174             session.flush();
175         }
176         catch (Exception e) {
177             throw processException(e);
178         }
179         finally {
180             closeSession(session);
181         }
182 
183         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
184 
185         EntityCacheUtil.removeResult(TagsSourceModelImpl.ENTITY_CACHE_ENABLED,
186             TagsSourceImpl.class, tagsSource.getPrimaryKey());
187 
188         return tagsSource;
189     }
190 
191     /**
192      * @deprecated Use {@link #update(TagsSource, boolean merge)}.
193      */
194     public TagsSource update(TagsSource tagsSource) throws SystemException {
195         if (_log.isWarnEnabled()) {
196             _log.warn(
197                 "Using the deprecated update(TagsSource tagsSource) method. Use update(TagsSource tagsSource, boolean merge) instead.");
198         }
199 
200         return update(tagsSource, false);
201     }
202 
203     /**
204      * Add, update, or merge, the entity. This method also calls the model
205      * listeners to trigger the proper events associated with adding, deleting,
206      * or updating an entity.
207      *
208      * @param  tagsSource the entity to add, update, or merge
209      * @param  merge boolean value for whether to merge the entity. The default
210      *         value is false. Setting merge to true is more expensive and
211      *         should only be true when tagsSource is transient. See
212      *         LEP-5473 for a detailed discussion of this method.
213      * @return the entity that was added, updated, or merged
214      */
215     public TagsSource update(TagsSource tagsSource, boolean merge)
216         throws SystemException {
217         boolean isNew = tagsSource.isNew();
218 
219         for (ModelListener<TagsSource> listener : listeners) {
220             if (isNew) {
221                 listener.onBeforeCreate(tagsSource);
222             }
223             else {
224                 listener.onBeforeUpdate(tagsSource);
225             }
226         }
227 
228         tagsSource = updateImpl(tagsSource, merge);
229 
230         for (ModelListener<TagsSource> listener : listeners) {
231             if (isNew) {
232                 listener.onAfterCreate(tagsSource);
233             }
234             else {
235                 listener.onAfterUpdate(tagsSource);
236             }
237         }
238 
239         return tagsSource;
240     }
241 
242     public TagsSource updateImpl(
243         com.liferay.portlet.tags.model.TagsSource tagsSource, boolean merge)
244         throws SystemException {
245         Session session = null;
246 
247         try {
248             session = openSession();
249 
250             BatchSessionUtil.update(session, tagsSource, merge);
251 
252             tagsSource.setNew(false);
253         }
254         catch (Exception e) {
255             throw processException(e);
256         }
257         finally {
258             closeSession(session);
259         }
260 
261         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
262 
263         EntityCacheUtil.putResult(TagsSourceModelImpl.ENTITY_CACHE_ENABLED,
264             TagsSourceImpl.class, tagsSource.getPrimaryKey(), tagsSource);
265 
266         return tagsSource;
267     }
268 
269     public TagsSource findByPrimaryKey(long sourceId)
270         throws NoSuchSourceException, SystemException {
271         TagsSource tagsSource = fetchByPrimaryKey(sourceId);
272 
273         if (tagsSource == null) {
274             if (_log.isWarnEnabled()) {
275                 _log.warn("No TagsSource exists with the primary key " +
276                     sourceId);
277             }
278 
279             throw new NoSuchSourceException(
280                 "No TagsSource exists with the primary key " + sourceId);
281         }
282 
283         return tagsSource;
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                 StringBuilder query = new StringBuilder();
380 
381                 query.append("SELECT tagsSource FROM TagsSource tagsSource ");
382 
383                 if (obc != null) {
384                     query.append("ORDER BY ");
385 
386                     String[] orderByFields = obc.getOrderByFields();
387 
388                     for (int i = 0; i < orderByFields.length; i++) {
389                         query.append("tagsSource.");
390                         query.append(orderByFields[i]);
391 
392                         if (obc.isAscending()) {
393                             query.append(" ASC");
394                         }
395                         else {
396                             query.append(" DESC");
397                         }
398 
399                         if ((i + 1) < orderByFields.length) {
400                             query.append(", ");
401                         }
402                     }
403                 }
404 
405                 Query q = session.createQuery(query.toString());
406 
407                 if (obc == null) {
408                     list = (List<TagsSource>)QueryUtil.list(q, getDialect(),
409                             start, end, false);
410 
411                     Collections.sort(list);
412                 }
413                 else {
414                     list = (List<TagsSource>)QueryUtil.list(q, getDialect(),
415                             start, end);
416                 }
417             }
418             catch (Exception e) {
419                 throw processException(e);
420             }
421             finally {
422                 if (list == null) {
423                     list = new ArrayList<TagsSource>();
424                 }
425 
426                 cacheResult(list);
427 
428                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
429 
430                 closeSession(session);
431             }
432         }
433 
434         return list;
435     }
436 
437     public void removeAll() throws SystemException {
438         for (TagsSource tagsSource : findAll()) {
439             remove(tagsSource);
440         }
441     }
442 
443     public int countAll() throws SystemException {
444         Object[] finderArgs = new Object[0];
445 
446         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
447                 finderArgs, this);
448 
449         if (count == null) {
450             Session session = null;
451 
452             try {
453                 session = openSession();
454 
455                 Query q = session.createQuery(
456                         "SELECT COUNT(tagsSource) FROM TagsSource tagsSource");
457 
458                 count = (Long)q.uniqueResult();
459             }
460             catch (Exception e) {
461                 throw processException(e);
462             }
463             finally {
464                 if (count == null) {
465                     count = Long.valueOf(0);
466                 }
467 
468                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
469                     count);
470 
471                 closeSession(session);
472             }
473         }
474 
475         return count.intValue();
476     }
477 
478     public void afterPropertiesSet() {
479         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
480                     com.liferay.portal.util.PropsUtil.get(
481                         "value.object.listener.com.liferay.portlet.tags.model.TagsSource")));
482 
483         if (listenerClassNames.length > 0) {
484             try {
485                 List<ModelListener<TagsSource>> listenersList = new ArrayList<ModelListener<TagsSource>>();
486 
487                 for (String listenerClassName : listenerClassNames) {
488                     listenersList.add((ModelListener<TagsSource>)Class.forName(
489                             listenerClassName).newInstance());
490                 }
491 
492                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
493             }
494             catch (Exception e) {
495                 _log.error(e);
496             }
497         }
498     }
499 
500     @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsAssetPersistence.impl")
501     protected com.liferay.portlet.tags.service.persistence.TagsAssetPersistence tagsAssetPersistence;
502     @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsEntryPersistence.impl")
503     protected com.liferay.portlet.tags.service.persistence.TagsEntryPersistence tagsEntryPersistence;
504     @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsPropertyPersistence.impl")
505     protected com.liferay.portlet.tags.service.persistence.TagsPropertyPersistence tagsPropertyPersistence;
506     @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsSourcePersistence.impl")
507     protected com.liferay.portlet.tags.service.persistence.TagsSourcePersistence tagsSourcePersistence;
508     @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
509     protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
510     @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
511     protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
512     private static Log _log = LogFactoryUtil.getLog(TagsSourcePersistenceImpl.class);
513 }