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.announcements.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.QueryPos;
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.StringPool;
41  import com.liferay.portal.kernel.util.StringUtil;
42  import com.liferay.portal.kernel.util.Validator;
43  import com.liferay.portal.model.ModelListener;
44  import com.liferay.portal.service.persistence.BatchSessionUtil;
45  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
46  
47  import com.liferay.portlet.announcements.NoSuchDeliveryException;
48  import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
49  import com.liferay.portlet.announcements.model.impl.AnnouncementsDeliveryImpl;
50  import com.liferay.portlet.announcements.model.impl.AnnouncementsDeliveryModelImpl;
51  
52  import java.util.ArrayList;
53  import java.util.Collections;
54  import java.util.List;
55  
56  /**
57   * <a href="AnnouncementsDeliveryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * <p>
60   * ServiceBuilder generated this class. Modifications in this class will be
61   * overwritten the next time is generated.
62   * </p>
63   *
64   * @author    Brian Wing Shun Chan
65   * @see       AnnouncementsDeliveryPersistence
66   * @see       AnnouncementsDeliveryUtil
67   * @generated
68   */
69  public class AnnouncementsDeliveryPersistenceImpl extends BasePersistenceImpl
70      implements AnnouncementsDeliveryPersistence {
71      public static final String FINDER_CLASS_NAME_ENTITY = AnnouncementsDeliveryImpl.class.getName();
72      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
73          ".List";
74      public static final FinderPath FINDER_PATH_FIND_BY_USERID = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
75              AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
76              FINDER_CLASS_NAME_LIST, "findByUserId",
77              new String[] { Long.class.getName() });
78      public static final FinderPath FINDER_PATH_FIND_BY_OBC_USERID = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
79              AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
80              FINDER_CLASS_NAME_LIST, "findByUserId",
81              new String[] {
82                  Long.class.getName(),
83                  
84              "java.lang.Integer", "java.lang.Integer",
85                  "com.liferay.portal.kernel.util.OrderByComparator"
86              });
87      public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
88              AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
89              FINDER_CLASS_NAME_LIST, "countByUserId",
90              new String[] { Long.class.getName() });
91      public static final FinderPath FINDER_PATH_FETCH_BY_U_T = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
92              AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
93              FINDER_CLASS_NAME_ENTITY, "fetchByU_T",
94              new String[] { Long.class.getName(), String.class.getName() });
95      public static final FinderPath FINDER_PATH_COUNT_BY_U_T = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
96              AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
97              FINDER_CLASS_NAME_LIST, "countByU_T",
98              new String[] { Long.class.getName(), String.class.getName() });
99      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
100             AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
101             FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
102     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
103             AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
104             FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
105 
106     public void cacheResult(AnnouncementsDelivery announcementsDelivery) {
107         EntityCacheUtil.putResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
108             AnnouncementsDeliveryImpl.class,
109             announcementsDelivery.getPrimaryKey(), announcementsDelivery);
110 
111         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
112             new Object[] {
113                 new Long(announcementsDelivery.getUserId()),
114                 
115             announcementsDelivery.getType()
116             }, announcementsDelivery);
117     }
118 
119     public void cacheResult(List<AnnouncementsDelivery> announcementsDeliveries) {
120         for (AnnouncementsDelivery announcementsDelivery : announcementsDeliveries) {
121             if (EntityCacheUtil.getResult(
122                         AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
123                         AnnouncementsDeliveryImpl.class,
124                         announcementsDelivery.getPrimaryKey(), this) == null) {
125                 cacheResult(announcementsDelivery);
126             }
127         }
128     }
129 
130     public void clearCache() {
131         CacheRegistry.clear(AnnouncementsDeliveryImpl.class.getName());
132         EntityCacheUtil.clearCache(AnnouncementsDeliveryImpl.class.getName());
133         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
134         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
135     }
136 
137     public AnnouncementsDelivery create(long deliveryId) {
138         AnnouncementsDelivery announcementsDelivery = new AnnouncementsDeliveryImpl();
139 
140         announcementsDelivery.setNew(true);
141         announcementsDelivery.setPrimaryKey(deliveryId);
142 
143         return announcementsDelivery;
144     }
145 
146     public AnnouncementsDelivery remove(long deliveryId)
147         throws NoSuchDeliveryException, SystemException {
148         Session session = null;
149 
150         try {
151             session = openSession();
152 
153             AnnouncementsDelivery announcementsDelivery = (AnnouncementsDelivery)session.get(AnnouncementsDeliveryImpl.class,
154                     new Long(deliveryId));
155 
156             if (announcementsDelivery == null) {
157                 if (_log.isWarnEnabled()) {
158                     _log.warn(
159                         "No AnnouncementsDelivery exists with the primary key " +
160                         deliveryId);
161                 }
162 
163                 throw new NoSuchDeliveryException(
164                     "No AnnouncementsDelivery exists with the primary key " +
165                     deliveryId);
166             }
167 
168             return remove(announcementsDelivery);
169         }
170         catch (NoSuchDeliveryException nsee) {
171             throw nsee;
172         }
173         catch (Exception e) {
174             throw processException(e);
175         }
176         finally {
177             closeSession(session);
178         }
179     }
180 
181     public AnnouncementsDelivery remove(
182         AnnouncementsDelivery announcementsDelivery) throws SystemException {
183         for (ModelListener<AnnouncementsDelivery> listener : listeners) {
184             listener.onBeforeRemove(announcementsDelivery);
185         }
186 
187         announcementsDelivery = removeImpl(announcementsDelivery);
188 
189         for (ModelListener<AnnouncementsDelivery> listener : listeners) {
190             listener.onAfterRemove(announcementsDelivery);
191         }
192 
193         return announcementsDelivery;
194     }
195 
196     protected AnnouncementsDelivery removeImpl(
197         AnnouncementsDelivery announcementsDelivery) throws SystemException {
198         Session session = null;
199 
200         try {
201             session = openSession();
202 
203             if (announcementsDelivery.isCachedModel() ||
204                     BatchSessionUtil.isEnabled()) {
205                 Object staleObject = session.get(AnnouncementsDeliveryImpl.class,
206                         announcementsDelivery.getPrimaryKeyObj());
207 
208                 if (staleObject != null) {
209                     session.evict(staleObject);
210                 }
211             }
212 
213             session.delete(announcementsDelivery);
214 
215             session.flush();
216         }
217         catch (Exception e) {
218             throw processException(e);
219         }
220         finally {
221             closeSession(session);
222         }
223 
224         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
225 
226         AnnouncementsDeliveryModelImpl announcementsDeliveryModelImpl = (AnnouncementsDeliveryModelImpl)announcementsDelivery;
227 
228         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_T,
229             new Object[] {
230                 new Long(announcementsDeliveryModelImpl.getOriginalUserId()),
231                 
232             announcementsDeliveryModelImpl.getOriginalType()
233             });
234 
235         EntityCacheUtil.removeResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
236             AnnouncementsDeliveryImpl.class,
237             announcementsDelivery.getPrimaryKey());
238 
239         return announcementsDelivery;
240     }
241 
242     /**
243      * @deprecated Use {@link #update(AnnouncementsDelivery, boolean merge)}.
244      */
245     public AnnouncementsDelivery update(
246         AnnouncementsDelivery announcementsDelivery) throws SystemException {
247         if (_log.isWarnEnabled()) {
248             _log.warn(
249                 "Using the deprecated update(AnnouncementsDelivery announcementsDelivery) method. Use update(AnnouncementsDelivery announcementsDelivery, boolean merge) instead.");
250         }
251 
252         return update(announcementsDelivery, false);
253     }
254 
255     /**
256      * Add, update, or merge, the entity. This method also calls the model
257      * listeners to trigger the proper events associated with adding, deleting,
258      * or updating an entity.
259      *
260      * @param  announcementsDelivery the entity to add, update, or merge
261      * @param  merge boolean value for whether to merge the entity. The default
262      *         value is false. Setting merge to true is more expensive and
263      *         should only be true when announcementsDelivery is transient. See
264      *         LEP-5473 for a detailed discussion of this method.
265      * @return the entity that was added, updated, or merged
266      */
267     public AnnouncementsDelivery update(
268         AnnouncementsDelivery announcementsDelivery, boolean merge)
269         throws SystemException {
270         boolean isNew = announcementsDelivery.isNew();
271 
272         for (ModelListener<AnnouncementsDelivery> listener : listeners) {
273             if (isNew) {
274                 listener.onBeforeCreate(announcementsDelivery);
275             }
276             else {
277                 listener.onBeforeUpdate(announcementsDelivery);
278             }
279         }
280 
281         announcementsDelivery = updateImpl(announcementsDelivery, merge);
282 
283         for (ModelListener<AnnouncementsDelivery> listener : listeners) {
284             if (isNew) {
285                 listener.onAfterCreate(announcementsDelivery);
286             }
287             else {
288                 listener.onAfterUpdate(announcementsDelivery);
289             }
290         }
291 
292         return announcementsDelivery;
293     }
294 
295     public AnnouncementsDelivery updateImpl(
296         com.liferay.portlet.announcements.model.AnnouncementsDelivery announcementsDelivery,
297         boolean merge) throws SystemException {
298         boolean isNew = announcementsDelivery.isNew();
299 
300         AnnouncementsDeliveryModelImpl announcementsDeliveryModelImpl = (AnnouncementsDeliveryModelImpl)announcementsDelivery;
301 
302         Session session = null;
303 
304         try {
305             session = openSession();
306 
307             BatchSessionUtil.update(session, announcementsDelivery, merge);
308 
309             announcementsDelivery.setNew(false);
310         }
311         catch (Exception e) {
312             throw processException(e);
313         }
314         finally {
315             closeSession(session);
316         }
317 
318         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
319 
320         EntityCacheUtil.putResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
321             AnnouncementsDeliveryImpl.class,
322             announcementsDelivery.getPrimaryKey(), announcementsDelivery);
323 
324         if (!isNew &&
325                 ((announcementsDelivery.getUserId() != announcementsDeliveryModelImpl.getOriginalUserId()) ||
326                 !Validator.equals(announcementsDelivery.getType(),
327                     announcementsDeliveryModelImpl.getOriginalType()))) {
328             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_T,
329                 new Object[] {
330                     new Long(announcementsDeliveryModelImpl.getOriginalUserId()),
331                     
332                 announcementsDeliveryModelImpl.getOriginalType()
333                 });
334         }
335 
336         if (isNew ||
337                 ((announcementsDelivery.getUserId() != announcementsDeliveryModelImpl.getOriginalUserId()) ||
338                 !Validator.equals(announcementsDelivery.getType(),
339                     announcementsDeliveryModelImpl.getOriginalType()))) {
340             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
341                 new Object[] {
342                     new Long(announcementsDelivery.getUserId()),
343                     
344                 announcementsDelivery.getType()
345                 }, announcementsDelivery);
346         }
347 
348         return announcementsDelivery;
349     }
350 
351     public AnnouncementsDelivery findByPrimaryKey(long deliveryId)
352         throws NoSuchDeliveryException, SystemException {
353         AnnouncementsDelivery announcementsDelivery = fetchByPrimaryKey(deliveryId);
354 
355         if (announcementsDelivery == null) {
356             if (_log.isWarnEnabled()) {
357                 _log.warn(
358                     "No AnnouncementsDelivery exists with the primary key " +
359                     deliveryId);
360             }
361 
362             throw new NoSuchDeliveryException(
363                 "No AnnouncementsDelivery exists with the primary key " +
364                 deliveryId);
365         }
366 
367         return announcementsDelivery;
368     }
369 
370     public AnnouncementsDelivery fetchByPrimaryKey(long deliveryId)
371         throws SystemException {
372         AnnouncementsDelivery announcementsDelivery = (AnnouncementsDelivery)EntityCacheUtil.getResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
373                 AnnouncementsDeliveryImpl.class, deliveryId, this);
374 
375         if (announcementsDelivery == null) {
376             Session session = null;
377 
378             try {
379                 session = openSession();
380 
381                 announcementsDelivery = (AnnouncementsDelivery)session.get(AnnouncementsDeliveryImpl.class,
382                         new Long(deliveryId));
383             }
384             catch (Exception e) {
385                 throw processException(e);
386             }
387             finally {
388                 if (announcementsDelivery != null) {
389                     cacheResult(announcementsDelivery);
390                 }
391 
392                 closeSession(session);
393             }
394         }
395 
396         return announcementsDelivery;
397     }
398 
399     public List<AnnouncementsDelivery> findByUserId(long userId)
400         throws SystemException {
401         Object[] finderArgs = new Object[] { new Long(userId) };
402 
403         List<AnnouncementsDelivery> list = (List<AnnouncementsDelivery>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_USERID,
404                 finderArgs, this);
405 
406         if (list == null) {
407             Session session = null;
408 
409             try {
410                 session = openSession();
411 
412                 StringBuilder query = new StringBuilder();
413 
414                 query.append(
415                     "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery WHERE ");
416 
417                 query.append("announcementsDelivery.userId = ?");
418 
419                 query.append(" ");
420 
421                 Query q = session.createQuery(query.toString());
422 
423                 QueryPos qPos = QueryPos.getInstance(q);
424 
425                 qPos.add(userId);
426 
427                 list = q.list();
428             }
429             catch (Exception e) {
430                 throw processException(e);
431             }
432             finally {
433                 if (list == null) {
434                     list = new ArrayList<AnnouncementsDelivery>();
435                 }
436 
437                 cacheResult(list);
438 
439                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_USERID,
440                     finderArgs, list);
441 
442                 closeSession(session);
443             }
444         }
445 
446         return list;
447     }
448 
449     public List<AnnouncementsDelivery> findByUserId(long userId, int start,
450         int end) throws SystemException {
451         return findByUserId(userId, start, end, null);
452     }
453 
454     public List<AnnouncementsDelivery> findByUserId(long userId, int start,
455         int end, OrderByComparator obc) throws SystemException {
456         Object[] finderArgs = new Object[] {
457                 new Long(userId),
458                 
459                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
460             };
461 
462         List<AnnouncementsDelivery> list = (List<AnnouncementsDelivery>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_USERID,
463                 finderArgs, this);
464 
465         if (list == null) {
466             Session session = null;
467 
468             try {
469                 session = openSession();
470 
471                 StringBuilder query = new StringBuilder();
472 
473                 query.append(
474                     "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery WHERE ");
475 
476                 query.append("announcementsDelivery.userId = ?");
477 
478                 query.append(" ");
479 
480                 if (obc != null) {
481                     query.append("ORDER BY ");
482 
483                     String[] orderByFields = obc.getOrderByFields();
484 
485                     for (int i = 0; i < orderByFields.length; i++) {
486                         query.append("announcementsDelivery.");
487                         query.append(orderByFields[i]);
488 
489                         if (obc.isAscending()) {
490                             query.append(" ASC");
491                         }
492                         else {
493                             query.append(" DESC");
494                         }
495 
496                         if ((i + 1) < orderByFields.length) {
497                             query.append(", ");
498                         }
499                     }
500                 }
501 
502                 Query q = session.createQuery(query.toString());
503 
504                 QueryPos qPos = QueryPos.getInstance(q);
505 
506                 qPos.add(userId);
507 
508                 list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
509                         getDialect(), start, end);
510             }
511             catch (Exception e) {
512                 throw processException(e);
513             }
514             finally {
515                 if (list == null) {
516                     list = new ArrayList<AnnouncementsDelivery>();
517                 }
518 
519                 cacheResult(list);
520 
521                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_USERID,
522                     finderArgs, list);
523 
524                 closeSession(session);
525             }
526         }
527 
528         return list;
529     }
530 
531     public AnnouncementsDelivery findByUserId_First(long userId,
532         OrderByComparator obc) throws NoSuchDeliveryException, SystemException {
533         List<AnnouncementsDelivery> list = findByUserId(userId, 0, 1, obc);
534 
535         if (list.isEmpty()) {
536             StringBuilder msg = new StringBuilder();
537 
538             msg.append("No AnnouncementsDelivery exists with the key {");
539 
540             msg.append("userId=" + userId);
541 
542             msg.append(StringPool.CLOSE_CURLY_BRACE);
543 
544             throw new NoSuchDeliveryException(msg.toString());
545         }
546         else {
547             return list.get(0);
548         }
549     }
550 
551     public AnnouncementsDelivery findByUserId_Last(long userId,
552         OrderByComparator obc) throws NoSuchDeliveryException, SystemException {
553         int count = countByUserId(userId);
554 
555         List<AnnouncementsDelivery> list = findByUserId(userId, count - 1,
556                 count, obc);
557 
558         if (list.isEmpty()) {
559             StringBuilder msg = new StringBuilder();
560 
561             msg.append("No AnnouncementsDelivery exists with the key {");
562 
563             msg.append("userId=" + userId);
564 
565             msg.append(StringPool.CLOSE_CURLY_BRACE);
566 
567             throw new NoSuchDeliveryException(msg.toString());
568         }
569         else {
570             return list.get(0);
571         }
572     }
573 
574     public AnnouncementsDelivery[] findByUserId_PrevAndNext(long deliveryId,
575         long userId, OrderByComparator obc)
576         throws NoSuchDeliveryException, SystemException {
577         AnnouncementsDelivery announcementsDelivery = findByPrimaryKey(deliveryId);
578 
579         int count = countByUserId(userId);
580 
581         Session session = null;
582 
583         try {
584             session = openSession();
585 
586             StringBuilder query = new StringBuilder();
587 
588             query.append(
589                 "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery WHERE ");
590 
591             query.append("announcementsDelivery.userId = ?");
592 
593             query.append(" ");
594 
595             if (obc != null) {
596                 query.append("ORDER BY ");
597 
598                 String[] orderByFields = obc.getOrderByFields();
599 
600                 for (int i = 0; i < orderByFields.length; i++) {
601                     query.append("announcementsDelivery.");
602                     query.append(orderByFields[i]);
603 
604                     if (obc.isAscending()) {
605                         query.append(" ASC");
606                     }
607                     else {
608                         query.append(" DESC");
609                     }
610 
611                     if ((i + 1) < orderByFields.length) {
612                         query.append(", ");
613                     }
614                 }
615             }
616 
617             Query q = session.createQuery(query.toString());
618 
619             QueryPos qPos = QueryPos.getInstance(q);
620 
621             qPos.add(userId);
622 
623             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
624                     announcementsDelivery);
625 
626             AnnouncementsDelivery[] array = new AnnouncementsDeliveryImpl[3];
627 
628             array[0] = (AnnouncementsDelivery)objArray[0];
629             array[1] = (AnnouncementsDelivery)objArray[1];
630             array[2] = (AnnouncementsDelivery)objArray[2];
631 
632             return array;
633         }
634         catch (Exception e) {
635             throw processException(e);
636         }
637         finally {
638             closeSession(session);
639         }
640     }
641 
642     public AnnouncementsDelivery findByU_T(long userId, String type)
643         throws NoSuchDeliveryException, SystemException {
644         AnnouncementsDelivery announcementsDelivery = fetchByU_T(userId, type);
645 
646         if (announcementsDelivery == null) {
647             StringBuilder msg = new StringBuilder();
648 
649             msg.append("No AnnouncementsDelivery exists with the key {");
650 
651             msg.append("userId=" + userId);
652 
653             msg.append(", ");
654             msg.append("type=" + type);
655 
656             msg.append(StringPool.CLOSE_CURLY_BRACE);
657 
658             if (_log.isWarnEnabled()) {
659                 _log.warn(msg.toString());
660             }
661 
662             throw new NoSuchDeliveryException(msg.toString());
663         }
664 
665         return announcementsDelivery;
666     }
667 
668     public AnnouncementsDelivery fetchByU_T(long userId, String type)
669         throws SystemException {
670         return fetchByU_T(userId, type, true);
671     }
672 
673     public AnnouncementsDelivery fetchByU_T(long userId, String type,
674         boolean retrieveFromCache) throws SystemException {
675         Object[] finderArgs = new Object[] { new Long(userId), type };
676 
677         Object result = null;
678 
679         if (retrieveFromCache) {
680             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_U_T,
681                     finderArgs, this);
682         }
683 
684         if (result == null) {
685             Session session = null;
686 
687             try {
688                 session = openSession();
689 
690                 StringBuilder query = new StringBuilder();
691 
692                 query.append(
693                     "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery WHERE ");
694 
695                 query.append("announcementsDelivery.userId = ?");
696 
697                 query.append(" AND ");
698 
699                 if (type == null) {
700                     query.append("announcementsDelivery.type IS NULL");
701                 }
702                 else {
703                     query.append("announcementsDelivery.type = ?");
704                 }
705 
706                 query.append(" ");
707 
708                 Query q = session.createQuery(query.toString());
709 
710                 QueryPos qPos = QueryPos.getInstance(q);
711 
712                 qPos.add(userId);
713 
714                 if (type != null) {
715                     qPos.add(type);
716                 }
717 
718                 List<AnnouncementsDelivery> list = q.list();
719 
720                 result = list;
721 
722                 AnnouncementsDelivery announcementsDelivery = null;
723 
724                 if (list.isEmpty()) {
725                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
726                         finderArgs, list);
727                 }
728                 else {
729                     announcementsDelivery = list.get(0);
730 
731                     cacheResult(announcementsDelivery);
732 
733                     if ((announcementsDelivery.getUserId() != userId) ||
734                             (announcementsDelivery.getType() == null) ||
735                             !announcementsDelivery.getType().equals(type)) {
736                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
737                             finderArgs, announcementsDelivery);
738                     }
739                 }
740 
741                 return announcementsDelivery;
742             }
743             catch (Exception e) {
744                 throw processException(e);
745             }
746             finally {
747                 if (result == null) {
748                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
749                         finderArgs, new ArrayList<AnnouncementsDelivery>());
750                 }
751 
752                 closeSession(session);
753             }
754         }
755         else {
756             if (result instanceof List<?>) {
757                 return null;
758             }
759             else {
760                 return (AnnouncementsDelivery)result;
761             }
762         }
763     }
764 
765     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
766         throws SystemException {
767         Session session = null;
768 
769         try {
770             session = openSession();
771 
772             dynamicQuery.compile(session);
773 
774             return dynamicQuery.list();
775         }
776         catch (Exception e) {
777             throw processException(e);
778         }
779         finally {
780             closeSession(session);
781         }
782     }
783 
784     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
785         int start, int end) throws SystemException {
786         Session session = null;
787 
788         try {
789             session = openSession();
790 
791             dynamicQuery.setLimit(start, end);
792 
793             dynamicQuery.compile(session);
794 
795             return dynamicQuery.list();
796         }
797         catch (Exception e) {
798             throw processException(e);
799         }
800         finally {
801             closeSession(session);
802         }
803     }
804 
805     public List<AnnouncementsDelivery> findAll() throws SystemException {
806         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
807     }
808 
809     public List<AnnouncementsDelivery> findAll(int start, int end)
810         throws SystemException {
811         return findAll(start, end, null);
812     }
813 
814     public List<AnnouncementsDelivery> findAll(int start, int end,
815         OrderByComparator obc) throws SystemException {
816         Object[] finderArgs = new Object[] {
817                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
818             };
819 
820         List<AnnouncementsDelivery> list = (List<AnnouncementsDelivery>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
821                 finderArgs, this);
822 
823         if (list == null) {
824             Session session = null;
825 
826             try {
827                 session = openSession();
828 
829                 StringBuilder query = new StringBuilder();
830 
831                 query.append(
832                     "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery ");
833 
834                 if (obc != null) {
835                     query.append("ORDER BY ");
836 
837                     String[] orderByFields = obc.getOrderByFields();
838 
839                     for (int i = 0; i < orderByFields.length; i++) {
840                         query.append("announcementsDelivery.");
841                         query.append(orderByFields[i]);
842 
843                         if (obc.isAscending()) {
844                             query.append(" ASC");
845                         }
846                         else {
847                             query.append(" DESC");
848                         }
849 
850                         if ((i + 1) < orderByFields.length) {
851                             query.append(", ");
852                         }
853                     }
854                 }
855 
856                 Query q = session.createQuery(query.toString());
857 
858                 if (obc == null) {
859                     list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
860                             getDialect(), start, end, false);
861 
862                     Collections.sort(list);
863                 }
864                 else {
865                     list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
866                             getDialect(), start, end);
867                 }
868             }
869             catch (Exception e) {
870                 throw processException(e);
871             }
872             finally {
873                 if (list == null) {
874                     list = new ArrayList<AnnouncementsDelivery>();
875                 }
876 
877                 cacheResult(list);
878 
879                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
880 
881                 closeSession(session);
882             }
883         }
884 
885         return list;
886     }
887 
888     public void removeByUserId(long userId) throws SystemException {
889         for (AnnouncementsDelivery announcementsDelivery : findByUserId(userId)) {
890             remove(announcementsDelivery);
891         }
892     }
893 
894     public void removeByU_T(long userId, String type)
895         throws NoSuchDeliveryException, SystemException {
896         AnnouncementsDelivery announcementsDelivery = findByU_T(userId, type);
897 
898         remove(announcementsDelivery);
899     }
900 
901     public void removeAll() throws SystemException {
902         for (AnnouncementsDelivery announcementsDelivery : findAll()) {
903             remove(announcementsDelivery);
904         }
905     }
906 
907     public int countByUserId(long userId) throws SystemException {
908         Object[] finderArgs = new Object[] { new Long(userId) };
909 
910         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_USERID,
911                 finderArgs, this);
912 
913         if (count == null) {
914             Session session = null;
915 
916             try {
917                 session = openSession();
918 
919                 StringBuilder query = new StringBuilder();
920 
921                 query.append("SELECT COUNT(announcementsDelivery) ");
922                 query.append(
923                     "FROM AnnouncementsDelivery announcementsDelivery WHERE ");
924 
925                 query.append("announcementsDelivery.userId = ?");
926 
927                 query.append(" ");
928 
929                 Query q = session.createQuery(query.toString());
930 
931                 QueryPos qPos = QueryPos.getInstance(q);
932 
933                 qPos.add(userId);
934 
935                 count = (Long)q.uniqueResult();
936             }
937             catch (Exception e) {
938                 throw processException(e);
939             }
940             finally {
941                 if (count == null) {
942                     count = Long.valueOf(0);
943                 }
944 
945                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_USERID,
946                     finderArgs, count);
947 
948                 closeSession(session);
949             }
950         }
951 
952         return count.intValue();
953     }
954 
955     public int countByU_T(long userId, String type) throws SystemException {
956         Object[] finderArgs = new Object[] { new Long(userId), type };
957 
958         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_U_T,
959                 finderArgs, this);
960 
961         if (count == null) {
962             Session session = null;
963 
964             try {
965                 session = openSession();
966 
967                 StringBuilder query = new StringBuilder();
968 
969                 query.append("SELECT COUNT(announcementsDelivery) ");
970                 query.append(
971                     "FROM AnnouncementsDelivery announcementsDelivery WHERE ");
972 
973                 query.append("announcementsDelivery.userId = ?");
974 
975                 query.append(" AND ");
976 
977                 if (type == null) {
978                     query.append("announcementsDelivery.type IS NULL");
979                 }
980                 else {
981                     query.append("announcementsDelivery.type = ?");
982                 }
983 
984                 query.append(" ");
985 
986                 Query q = session.createQuery(query.toString());
987 
988                 QueryPos qPos = QueryPos.getInstance(q);
989 
990                 qPos.add(userId);
991 
992                 if (type != null) {
993                     qPos.add(type);
994                 }
995 
996                 count = (Long)q.uniqueResult();
997             }
998             catch (Exception e) {
999                 throw processException(e);
1000            }
1001            finally {
1002                if (count == null) {
1003                    count = Long.valueOf(0);
1004                }
1005
1006                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_U_T, finderArgs,
1007                    count);
1008
1009                closeSession(session);
1010            }
1011        }
1012
1013        return count.intValue();
1014    }
1015
1016    public int countAll() throws SystemException {
1017        Object[] finderArgs = new Object[0];
1018
1019        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1020                finderArgs, this);
1021
1022        if (count == null) {
1023            Session session = null;
1024
1025            try {
1026                session = openSession();
1027
1028                Query q = session.createQuery(
1029                        "SELECT COUNT(announcementsDelivery) FROM AnnouncementsDelivery announcementsDelivery");
1030
1031                count = (Long)q.uniqueResult();
1032            }
1033            catch (Exception e) {
1034                throw processException(e);
1035            }
1036            finally {
1037                if (count == null) {
1038                    count = Long.valueOf(0);
1039                }
1040
1041                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1042                    count);
1043
1044                closeSession(session);
1045            }
1046        }
1047
1048        return count.intValue();
1049    }
1050
1051    public void afterPropertiesSet() {
1052        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1053                    com.liferay.portal.util.PropsUtil.get(
1054                        "value.object.listener.com.liferay.portlet.announcements.model.AnnouncementsDelivery")));
1055
1056        if (listenerClassNames.length > 0) {
1057            try {
1058                List<ModelListener<AnnouncementsDelivery>> listenersList = new ArrayList<ModelListener<AnnouncementsDelivery>>();
1059
1060                for (String listenerClassName : listenerClassNames) {
1061                    listenersList.add((ModelListener<AnnouncementsDelivery>)Class.forName(
1062                            listenerClassName).newInstance());
1063                }
1064
1065                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1066            }
1067            catch (Exception e) {
1068                _log.error(e);
1069            }
1070        }
1071    }
1072
1073    @BeanReference(name = "com.liferay.portlet.announcements.service.persistence.AnnouncementsDeliveryPersistence.impl")
1074    protected com.liferay.portlet.announcements.service.persistence.AnnouncementsDeliveryPersistence announcementsDeliveryPersistence;
1075    @BeanReference(name = "com.liferay.portlet.announcements.service.persistence.AnnouncementsEntryPersistence.impl")
1076    protected com.liferay.portlet.announcements.service.persistence.AnnouncementsEntryPersistence announcementsEntryPersistence;
1077    @BeanReference(name = "com.liferay.portlet.announcements.service.persistence.AnnouncementsFlagPersistence.impl")
1078    protected com.liferay.portlet.announcements.service.persistence.AnnouncementsFlagPersistence announcementsFlagPersistence;
1079    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1080    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1081    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1082    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1083    private static Log _log = LogFactoryUtil.getLog(AnnouncementsDeliveryPersistenceImpl.class);
1084}