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