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