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