1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.portal.service.persistence;
24  
25  import com.liferay.portal.NoSuchReleaseException;
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.StringUtil;
41  import com.liferay.portal.model.ModelListener;
42  import com.liferay.portal.model.Release;
43  import com.liferay.portal.model.impl.ReleaseImpl;
44  import com.liferay.portal.model.impl.ReleaseModelImpl;
45  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
46  
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.List;
50  
51  /**
52   * <a href="ReleasePersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class ReleasePersistenceImpl extends BasePersistenceImpl
58      implements ReleasePersistence {
59      public static final String FINDER_CLASS_NAME_ENTITY = ReleaseImpl.class.getName();
60      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
61          ".List";
62      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ReleaseModelImpl.ENTITY_CACHE_ENABLED,
63              ReleaseModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
64              "findAll", new String[0]);
65      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ReleaseModelImpl.ENTITY_CACHE_ENABLED,
66              ReleaseModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
67              "countAll", new String[0]);
68  
69      public void cacheResult(Release release) {
70          EntityCacheUtil.putResult(ReleaseModelImpl.ENTITY_CACHE_ENABLED,
71              ReleaseImpl.class, release.getPrimaryKey(), release);
72      }
73  
74      public void cacheResult(List<Release> releases) {
75          for (Release release : releases) {
76              if (EntityCacheUtil.getResult(
77                          ReleaseModelImpl.ENTITY_CACHE_ENABLED,
78                          ReleaseImpl.class, release.getPrimaryKey(), this) == null) {
79                  cacheResult(release);
80              }
81          }
82      }
83  
84      public void clearCache() {
85          CacheRegistry.clear(ReleaseImpl.class.getName());
86          EntityCacheUtil.clearCache(ReleaseImpl.class.getName());
87          FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
88          FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
89      }
90  
91      public Release create(long releaseId) {
92          Release release = new ReleaseImpl();
93  
94          release.setNew(true);
95          release.setPrimaryKey(releaseId);
96  
97          return release;
98      }
99  
100     public Release remove(long releaseId)
101         throws NoSuchReleaseException, SystemException {
102         Session session = null;
103 
104         try {
105             session = openSession();
106 
107             Release release = (Release)session.get(ReleaseImpl.class,
108                     new Long(releaseId));
109 
110             if (release == null) {
111                 if (_log.isWarnEnabled()) {
112                     _log.warn("No Release exists with the primary key " +
113                         releaseId);
114                 }
115 
116                 throw new NoSuchReleaseException(
117                     "No Release exists with the primary key " + releaseId);
118             }
119 
120             return remove(release);
121         }
122         catch (NoSuchReleaseException nsee) {
123             throw nsee;
124         }
125         catch (Exception e) {
126             throw processException(e);
127         }
128         finally {
129             closeSession(session);
130         }
131     }
132 
133     public Release remove(Release release) throws SystemException {
134         for (ModelListener<Release> listener : listeners) {
135             listener.onBeforeRemove(release);
136         }
137 
138         release = removeImpl(release);
139 
140         for (ModelListener<Release> listener : listeners) {
141             listener.onAfterRemove(release);
142         }
143 
144         return release;
145     }
146 
147     protected Release removeImpl(Release release) throws SystemException {
148         Session session = null;
149 
150         try {
151             session = openSession();
152 
153             if (release.isCachedModel() || BatchSessionUtil.isEnabled()) {
154                 Object staleObject = session.get(ReleaseImpl.class,
155                         release.getPrimaryKeyObj());
156 
157                 if (staleObject != null) {
158                     session.evict(staleObject);
159                 }
160             }
161 
162             session.delete(release);
163 
164             session.flush();
165         }
166         catch (Exception e) {
167             throw processException(e);
168         }
169         finally {
170             closeSession(session);
171         }
172 
173         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
174 
175         EntityCacheUtil.removeResult(ReleaseModelImpl.ENTITY_CACHE_ENABLED,
176             ReleaseImpl.class, release.getPrimaryKey());
177 
178         return release;
179     }
180 
181     /**
182      * @deprecated Use <code>update(Release release, boolean merge)</code>.
183      */
184     public Release update(Release release) throws SystemException {
185         if (_log.isWarnEnabled()) {
186             _log.warn(
187                 "Using the deprecated update(Release release) method. Use update(Release release, boolean merge) instead.");
188         }
189 
190         return update(release, false);
191     }
192 
193     /**
194      * Add, update, or merge, the entity. This method also calls the model
195      * listeners to trigger the proper events associated with adding, deleting,
196      * or updating an entity.
197      *
198      * @param        release the entity to add, update, or merge
199      * @param        merge boolean value for whether to merge the entity. The
200      *                default value is false. Setting merge to true is more
201      *                expensive and should only be true when release is
202      *                transient. See LEP-5473 for a detailed discussion of this
203      *                method.
204      * @return        true if the portlet can be displayed via Ajax
205      */
206     public Release update(Release release, boolean merge)
207         throws SystemException {
208         boolean isNew = release.isNew();
209 
210         for (ModelListener<Release> listener : listeners) {
211             if (isNew) {
212                 listener.onBeforeCreate(release);
213             }
214             else {
215                 listener.onBeforeUpdate(release);
216             }
217         }
218 
219         release = updateImpl(release, merge);
220 
221         for (ModelListener<Release> listener : listeners) {
222             if (isNew) {
223                 listener.onAfterCreate(release);
224             }
225             else {
226                 listener.onAfterUpdate(release);
227             }
228         }
229 
230         return release;
231     }
232 
233     public Release updateImpl(com.liferay.portal.model.Release release,
234         boolean merge) throws SystemException {
235         Session session = null;
236 
237         try {
238             session = openSession();
239 
240             BatchSessionUtil.update(session, release, merge);
241 
242             release.setNew(false);
243         }
244         catch (Exception e) {
245             throw processException(e);
246         }
247         finally {
248             closeSession(session);
249         }
250 
251         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
252 
253         EntityCacheUtil.putResult(ReleaseModelImpl.ENTITY_CACHE_ENABLED,
254             ReleaseImpl.class, release.getPrimaryKey(), release);
255 
256         return release;
257     }
258 
259     public Release findByPrimaryKey(long releaseId)
260         throws NoSuchReleaseException, SystemException {
261         Release release = fetchByPrimaryKey(releaseId);
262 
263         if (release == null) {
264             if (_log.isWarnEnabled()) {
265                 _log.warn("No Release exists with the primary key " +
266                     releaseId);
267             }
268 
269             throw new NoSuchReleaseException(
270                 "No Release exists with the primary key " + releaseId);
271         }
272 
273         return release;
274     }
275 
276     public Release fetchByPrimaryKey(long releaseId) throws SystemException {
277         Release release = (Release)EntityCacheUtil.getResult(ReleaseModelImpl.ENTITY_CACHE_ENABLED,
278                 ReleaseImpl.class, releaseId, this);
279 
280         if (release == null) {
281             Session session = null;
282 
283             try {
284                 session = openSession();
285 
286                 release = (Release)session.get(ReleaseImpl.class,
287                         new Long(releaseId));
288             }
289             catch (Exception e) {
290                 throw processException(e);
291             }
292             finally {
293                 if (release != null) {
294                     cacheResult(release);
295                 }
296 
297                 closeSession(session);
298             }
299         }
300 
301         return release;
302     }
303 
304     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
305         throws SystemException {
306         Session session = null;
307 
308         try {
309             session = openSession();
310 
311             dynamicQuery.compile(session);
312 
313             return dynamicQuery.list();
314         }
315         catch (Exception e) {
316             throw processException(e);
317         }
318         finally {
319             closeSession(session);
320         }
321     }
322 
323     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
324         int start, int end) throws SystemException {
325         Session session = null;
326 
327         try {
328             session = openSession();
329 
330             dynamicQuery.setLimit(start, end);
331 
332             dynamicQuery.compile(session);
333 
334             return dynamicQuery.list();
335         }
336         catch (Exception e) {
337             throw processException(e);
338         }
339         finally {
340             closeSession(session);
341         }
342     }
343 
344     public List<Release> findAll() throws SystemException {
345         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
346     }
347 
348     public List<Release> findAll(int start, int end) throws SystemException {
349         return findAll(start, end, null);
350     }
351 
352     public List<Release> findAll(int start, int end, OrderByComparator obc)
353         throws SystemException {
354         Object[] finderArgs = new Object[] {
355                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
356             };
357 
358         List<Release> list = (List<Release>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
359                 finderArgs, this);
360 
361         if (list == null) {
362             Session session = null;
363 
364             try {
365                 session = openSession();
366 
367                 StringBuilder query = new StringBuilder();
368 
369                 query.append("SELECT release FROM Release release ");
370 
371                 if (obc != null) {
372                     query.append("ORDER BY ");
373 
374                     String[] orderByFields = obc.getOrderByFields();
375 
376                     for (int i = 0; i < orderByFields.length; i++) {
377                         query.append("release.");
378                         query.append(orderByFields[i]);
379 
380                         if (obc.isAscending()) {
381                             query.append(" ASC");
382                         }
383                         else {
384                             query.append(" DESC");
385                         }
386 
387                         if ((i + 1) < orderByFields.length) {
388                             query.append(", ");
389                         }
390                     }
391                 }
392 
393                 Query q = session.createQuery(query.toString());
394 
395                 if (obc == null) {
396                     list = (List<Release>)QueryUtil.list(q, getDialect(),
397                             start, end, false);
398 
399                     Collections.sort(list);
400                 }
401                 else {
402                     list = (List<Release>)QueryUtil.list(q, getDialect(),
403                             start, end);
404                 }
405             }
406             catch (Exception e) {
407                 throw processException(e);
408             }
409             finally {
410                 if (list == null) {
411                     list = new ArrayList<Release>();
412                 }
413 
414                 cacheResult(list);
415 
416                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
417 
418                 closeSession(session);
419             }
420         }
421 
422         return list;
423     }
424 
425     public void removeAll() throws SystemException {
426         for (Release release : findAll()) {
427             remove(release);
428         }
429     }
430 
431     public int countAll() throws SystemException {
432         Object[] finderArgs = new Object[0];
433 
434         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
435                 finderArgs, this);
436 
437         if (count == null) {
438             Session session = null;
439 
440             try {
441                 session = openSession();
442 
443                 Query q = session.createQuery(
444                         "SELECT COUNT(release) FROM Release release");
445 
446                 count = (Long)q.uniqueResult();
447             }
448             catch (Exception e) {
449                 throw processException(e);
450             }
451             finally {
452                 if (count == null) {
453                     count = Long.valueOf(0);
454                 }
455 
456                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
457                     count);
458 
459                 closeSession(session);
460             }
461         }
462 
463         return count.intValue();
464     }
465 
466     public void afterPropertiesSet() {
467         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
468                     com.liferay.portal.util.PropsUtil.get(
469                         "value.object.listener.com.liferay.portal.model.Release")));
470 
471         if (listenerClassNames.length > 0) {
472             try {
473                 List<ModelListener<Release>> listenersList = new ArrayList<ModelListener<Release>>();
474 
475                 for (String listenerClassName : listenerClassNames) {
476                     listenersList.add((ModelListener<Release>)Class.forName(
477                             listenerClassName).newInstance());
478                 }
479 
480                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
481             }
482             catch (Exception e) {
483                 _log.error(e);
484             }
485         }
486     }
487 
488     @BeanReference(name = "com.liferay.portal.service.persistence.AccountPersistence.impl")
489     protected com.liferay.portal.service.persistence.AccountPersistence accountPersistence;
490     @BeanReference(name = "com.liferay.portal.service.persistence.AddressPersistence.impl")
491     protected com.liferay.portal.service.persistence.AddressPersistence addressPersistence;
492     @BeanReference(name = "com.liferay.portal.service.persistence.BrowserTrackerPersistence.impl")
493     protected com.liferay.portal.service.persistence.BrowserTrackerPersistence browserTrackerPersistence;
494     @BeanReference(name = "com.liferay.portal.service.persistence.ClassNamePersistence.impl")
495     protected com.liferay.portal.service.persistence.ClassNamePersistence classNamePersistence;
496     @BeanReference(name = "com.liferay.portal.service.persistence.CompanyPersistence.impl")
497     protected com.liferay.portal.service.persistence.CompanyPersistence companyPersistence;
498     @BeanReference(name = "com.liferay.portal.service.persistence.ContactPersistence.impl")
499     protected com.liferay.portal.service.persistence.ContactPersistence contactPersistence;
500     @BeanReference(name = "com.liferay.portal.service.persistence.CountryPersistence.impl")
501     protected com.liferay.portal.service.persistence.CountryPersistence countryPersistence;
502     @BeanReference(name = "com.liferay.portal.service.persistence.EmailAddressPersistence.impl")
503     protected com.liferay.portal.service.persistence.EmailAddressPersistence emailAddressPersistence;
504     @BeanReference(name = "com.liferay.portal.service.persistence.GroupPersistence.impl")
505     protected com.liferay.portal.service.persistence.GroupPersistence groupPersistence;
506     @BeanReference(name = "com.liferay.portal.service.persistence.ImagePersistence.impl")
507     protected com.liferay.portal.service.persistence.ImagePersistence imagePersistence;
508     @BeanReference(name = "com.liferay.portal.service.persistence.LayoutPersistence.impl")
509     protected com.liferay.portal.service.persistence.LayoutPersistence layoutPersistence;
510     @BeanReference(name = "com.liferay.portal.service.persistence.LayoutSetPersistence.impl")
511     protected com.liferay.portal.service.persistence.LayoutSetPersistence layoutSetPersistence;
512     @BeanReference(name = "com.liferay.portal.service.persistence.ListTypePersistence.impl")
513     protected com.liferay.portal.service.persistence.ListTypePersistence listTypePersistence;
514     @BeanReference(name = "com.liferay.portal.service.persistence.MembershipRequestPersistence.impl")
515     protected com.liferay.portal.service.persistence.MembershipRequestPersistence membershipRequestPersistence;
516     @BeanReference(name = "com.liferay.portal.service.persistence.OrganizationPersistence.impl")
517     protected com.liferay.portal.service.persistence.OrganizationPersistence organizationPersistence;
518     @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupPermissionPersistence.impl")
519     protected com.liferay.portal.service.persistence.OrgGroupPermissionPersistence orgGroupPermissionPersistence;
520     @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupRolePersistence.impl")
521     protected com.liferay.portal.service.persistence.OrgGroupRolePersistence orgGroupRolePersistence;
522     @BeanReference(name = "com.liferay.portal.service.persistence.OrgLaborPersistence.impl")
523     protected com.liferay.portal.service.persistence.OrgLaborPersistence orgLaborPersistence;
524     @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyPersistence.impl")
525     protected com.liferay.portal.service.persistence.PasswordPolicyPersistence passwordPolicyPersistence;
526     @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyRelPersistence.impl")
527     protected com.liferay.portal.service.persistence.PasswordPolicyRelPersistence passwordPolicyRelPersistence;
528     @BeanReference(name = "com.liferay.portal.service.persistence.PasswordTrackerPersistence.impl")
529     protected com.liferay.portal.service.persistence.PasswordTrackerPersistence passwordTrackerPersistence;
530     @BeanReference(name = "com.liferay.portal.service.persistence.PermissionPersistence.impl")
531     protected com.liferay.portal.service.persistence.PermissionPersistence permissionPersistence;
532     @BeanReference(name = "com.liferay.portal.service.persistence.PhonePersistence.impl")
533     protected com.liferay.portal.service.persistence.PhonePersistence phonePersistence;
534     @BeanReference(name = "com.liferay.portal.service.persistence.PluginSettingPersistence.impl")
535     protected com.liferay.portal.service.persistence.PluginSettingPersistence pluginSettingPersistence;
536     @BeanReference(name = "com.liferay.portal.service.persistence.PortletPersistence.impl")
537     protected com.liferay.portal.service.persistence.PortletPersistence portletPersistence;
538     @BeanReference(name = "com.liferay.portal.service.persistence.PortletItemPersistence.impl")
539     protected com.liferay.portal.service.persistence.PortletItemPersistence portletItemPersistence;
540     @BeanReference(name = "com.liferay.portal.service.persistence.PortletPreferencesPersistence.impl")
541     protected com.liferay.portal.service.persistence.PortletPreferencesPersistence portletPreferencesPersistence;
542     @BeanReference(name = "com.liferay.portal.service.persistence.RegionPersistence.impl")
543     protected com.liferay.portal.service.persistence.RegionPersistence regionPersistence;
544     @BeanReference(name = "com.liferay.portal.service.persistence.ReleasePersistence.impl")
545     protected com.liferay.portal.service.persistence.ReleasePersistence releasePersistence;
546     @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
547     protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
548     @BeanReference(name = "com.liferay.portal.service.persistence.ResourceActionPersistence.impl")
549     protected com.liferay.portal.service.persistence.ResourceActionPersistence resourceActionPersistence;
550     @BeanReference(name = "com.liferay.portal.service.persistence.ResourceCodePersistence.impl")
551     protected com.liferay.portal.service.persistence.ResourceCodePersistence resourceCodePersistence;
552     @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePermissionPersistence.impl")
553     protected com.liferay.portal.service.persistence.ResourcePermissionPersistence resourcePermissionPersistence;
554     @BeanReference(name = "com.liferay.portal.service.persistence.RolePersistence.impl")
555     protected com.liferay.portal.service.persistence.RolePersistence rolePersistence;
556     @BeanReference(name = "com.liferay.portal.service.persistence.ServiceComponentPersistence.impl")
557     protected com.liferay.portal.service.persistence.ServiceComponentPersistence serviceComponentPersistence;
558     @BeanReference(name = "com.liferay.portal.service.persistence.ShardPersistence.impl")
559     protected com.liferay.portal.service.persistence.ShardPersistence shardPersistence;
560     @BeanReference(name = "com.liferay.portal.service.persistence.SubscriptionPersistence.impl")
561     protected com.liferay.portal.service.persistence.SubscriptionPersistence subscriptionPersistence;
562     @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
563     protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
564     @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupPersistence.impl")
565     protected com.liferay.portal.service.persistence.UserGroupPersistence userGroupPersistence;
566     @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupRolePersistence.impl")
567     protected com.liferay.portal.service.persistence.UserGroupRolePersistence userGroupRolePersistence;
568     @BeanReference(name = "com.liferay.portal.service.persistence.UserIdMapperPersistence.impl")
569     protected com.liferay.portal.service.persistence.UserIdMapperPersistence userIdMapperPersistence;
570     @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPersistence.impl")
571     protected com.liferay.portal.service.persistence.UserTrackerPersistence userTrackerPersistence;
572     @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPathPersistence.impl")
573     protected com.liferay.portal.service.persistence.UserTrackerPathPersistence userTrackerPathPersistence;
574     @BeanReference(name = "com.liferay.portal.service.persistence.WebDAVPropsPersistence.impl")
575     protected com.liferay.portal.service.persistence.WebDAVPropsPersistence webDAVPropsPersistence;
576     @BeanReference(name = "com.liferay.portal.service.persistence.WebsitePersistence.impl")
577     protected com.liferay.portal.service.persistence.WebsitePersistence websitePersistence;
578     private static Log _log = LogFactoryUtil.getLog(ReleasePersistenceImpl.class);
579 }