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