001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.announcements.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.kernel.annotation.BeanReference;
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.exception.SystemException;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstanceFactory;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.model.ModelListener;
038    import com.liferay.portal.service.persistence.BatchSessionUtil;
039    import com.liferay.portal.service.persistence.ResourcePersistence;
040    import com.liferay.portal.service.persistence.UserPersistence;
041    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
042    
043    import com.liferay.portlet.announcements.NoSuchDeliveryException;
044    import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
045    import com.liferay.portlet.announcements.model.impl.AnnouncementsDeliveryImpl;
046    import com.liferay.portlet.announcements.model.impl.AnnouncementsDeliveryModelImpl;
047    
048    import java.io.Serializable;
049    
050    import java.util.ArrayList;
051    import java.util.Collections;
052    import java.util.List;
053    
054    /**
055     * @author    Brian Wing Shun Chan
056     * @see       AnnouncementsDeliveryPersistence
057     * @see       AnnouncementsDeliveryUtil
058     * @generated
059     */
060    public class AnnouncementsDeliveryPersistenceImpl extends BasePersistenceImpl<AnnouncementsDelivery>
061            implements AnnouncementsDeliveryPersistence {
062            public static final String FINDER_CLASS_NAME_ENTITY = AnnouncementsDeliveryImpl.class.getName();
063            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
064                    ".List";
065            public static final FinderPath FINDER_PATH_FIND_BY_USERID = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
066                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
067                            FINDER_CLASS_NAME_LIST, "findByUserId",
068                            new String[] {
069                                    Long.class.getName(),
070                                    
071                            "java.lang.Integer", "java.lang.Integer",
072                                    "com.liferay.portal.kernel.util.OrderByComparator"
073                            });
074            public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
075                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
076                            FINDER_CLASS_NAME_LIST, "countByUserId",
077                            new String[] { Long.class.getName() });
078            public static final FinderPath FINDER_PATH_FETCH_BY_U_T = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
079                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
080                            FINDER_CLASS_NAME_ENTITY, "fetchByU_T",
081                            new String[] { Long.class.getName(), String.class.getName() });
082            public static final FinderPath FINDER_PATH_COUNT_BY_U_T = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
083                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
084                            FINDER_CLASS_NAME_LIST, "countByU_T",
085                            new String[] { Long.class.getName(), String.class.getName() });
086            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
087                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
088                            FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
089            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
090                            AnnouncementsDeliveryModelImpl.FINDER_CACHE_ENABLED,
091                            FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
092    
093            public void cacheResult(AnnouncementsDelivery announcementsDelivery) {
094                    EntityCacheUtil.putResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
095                            AnnouncementsDeliveryImpl.class,
096                            announcementsDelivery.getPrimaryKey(), announcementsDelivery);
097    
098                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
099                            new Object[] {
100                                    new Long(announcementsDelivery.getUserId()),
101                                    
102                            announcementsDelivery.getType()
103                            }, announcementsDelivery);
104            }
105    
106            public void cacheResult(List<AnnouncementsDelivery> announcementsDeliveries) {
107                    for (AnnouncementsDelivery announcementsDelivery : announcementsDeliveries) {
108                            if (EntityCacheUtil.getResult(
109                                                    AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
110                                                    AnnouncementsDeliveryImpl.class,
111                                                    announcementsDelivery.getPrimaryKey(), this) == null) {
112                                    cacheResult(announcementsDelivery);
113                            }
114                    }
115            }
116    
117            public void clearCache() {
118                    CacheRegistryUtil.clear(AnnouncementsDeliveryImpl.class.getName());
119                    EntityCacheUtil.clearCache(AnnouncementsDeliveryImpl.class.getName());
120                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
121                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
122            }
123    
124            public void clearCache(AnnouncementsDelivery announcementsDelivery) {
125                    EntityCacheUtil.removeResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
126                            AnnouncementsDeliveryImpl.class,
127                            announcementsDelivery.getPrimaryKey());
128    
129                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_T,
130                            new Object[] {
131                                    new Long(announcementsDelivery.getUserId()),
132                                    
133                            announcementsDelivery.getType()
134                            });
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(Serializable primaryKey)
147                    throws NoSuchModelException, SystemException {
148                    return remove(((Long)primaryKey).longValue());
149            }
150    
151            public AnnouncementsDelivery remove(long deliveryId)
152                    throws NoSuchDeliveryException, SystemException {
153                    Session session = null;
154    
155                    try {
156                            session = openSession();
157    
158                            AnnouncementsDelivery announcementsDelivery = (AnnouncementsDelivery)session.get(AnnouncementsDeliveryImpl.class,
159                                            new Long(deliveryId));
160    
161                            if (announcementsDelivery == null) {
162                                    if (_log.isWarnEnabled()) {
163                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + deliveryId);
164                                    }
165    
166                                    throw new NoSuchDeliveryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
167                                            deliveryId);
168                            }
169    
170                            return remove(announcementsDelivery);
171                    }
172                    catch (NoSuchDeliveryException nsee) {
173                            throw nsee;
174                    }
175                    catch (Exception e) {
176                            throw processException(e);
177                    }
178                    finally {
179                            closeSession(session);
180                    }
181            }
182    
183            protected AnnouncementsDelivery removeImpl(
184                    AnnouncementsDelivery announcementsDelivery) throws SystemException {
185                    announcementsDelivery = toUnwrappedModel(announcementsDelivery);
186    
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            public AnnouncementsDelivery updateImpl(
232                    com.liferay.portlet.announcements.model.AnnouncementsDelivery announcementsDelivery,
233                    boolean merge) throws SystemException {
234                    announcementsDelivery = toUnwrappedModel(announcementsDelivery);
235    
236                    boolean isNew = announcementsDelivery.isNew();
237    
238                    AnnouncementsDeliveryModelImpl announcementsDeliveryModelImpl = (AnnouncementsDeliveryModelImpl)announcementsDelivery;
239    
240                    Session session = null;
241    
242                    try {
243                            session = openSession();
244    
245                            BatchSessionUtil.update(session, announcementsDelivery, merge);
246    
247                            announcementsDelivery.setNew(false);
248                    }
249                    catch (Exception e) {
250                            throw processException(e);
251                    }
252                    finally {
253                            closeSession(session);
254                    }
255    
256                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
257    
258                    EntityCacheUtil.putResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
259                            AnnouncementsDeliveryImpl.class,
260                            announcementsDelivery.getPrimaryKey(), announcementsDelivery);
261    
262                    if (!isNew &&
263                                    ((announcementsDelivery.getUserId() != announcementsDeliveryModelImpl.getOriginalUserId()) ||
264                                    !Validator.equals(announcementsDelivery.getType(),
265                                            announcementsDeliveryModelImpl.getOriginalType()))) {
266                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_T,
267                                    new Object[] {
268                                            new Long(announcementsDeliveryModelImpl.getOriginalUserId()),
269                                            
270                                    announcementsDeliveryModelImpl.getOriginalType()
271                                    });
272                    }
273    
274                    if (isNew ||
275                                    ((announcementsDelivery.getUserId() != announcementsDeliveryModelImpl.getOriginalUserId()) ||
276                                    !Validator.equals(announcementsDelivery.getType(),
277                                            announcementsDeliveryModelImpl.getOriginalType()))) {
278                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
279                                    new Object[] {
280                                            new Long(announcementsDelivery.getUserId()),
281                                            
282                                    announcementsDelivery.getType()
283                                    }, announcementsDelivery);
284                    }
285    
286                    return announcementsDelivery;
287            }
288    
289            protected AnnouncementsDelivery toUnwrappedModel(
290                    AnnouncementsDelivery announcementsDelivery) {
291                    if (announcementsDelivery instanceof AnnouncementsDeliveryImpl) {
292                            return announcementsDelivery;
293                    }
294    
295                    AnnouncementsDeliveryImpl announcementsDeliveryImpl = new AnnouncementsDeliveryImpl();
296    
297                    announcementsDeliveryImpl.setNew(announcementsDelivery.isNew());
298                    announcementsDeliveryImpl.setPrimaryKey(announcementsDelivery.getPrimaryKey());
299    
300                    announcementsDeliveryImpl.setDeliveryId(announcementsDelivery.getDeliveryId());
301                    announcementsDeliveryImpl.setCompanyId(announcementsDelivery.getCompanyId());
302                    announcementsDeliveryImpl.setUserId(announcementsDelivery.getUserId());
303                    announcementsDeliveryImpl.setType(announcementsDelivery.getType());
304                    announcementsDeliveryImpl.setEmail(announcementsDelivery.isEmail());
305                    announcementsDeliveryImpl.setSms(announcementsDelivery.isSms());
306                    announcementsDeliveryImpl.setWebsite(announcementsDelivery.isWebsite());
307    
308                    return announcementsDeliveryImpl;
309            }
310    
311            public AnnouncementsDelivery findByPrimaryKey(Serializable primaryKey)
312                    throws NoSuchModelException, SystemException {
313                    return findByPrimaryKey(((Long)primaryKey).longValue());
314            }
315    
316            public AnnouncementsDelivery findByPrimaryKey(long deliveryId)
317                    throws NoSuchDeliveryException, SystemException {
318                    AnnouncementsDelivery announcementsDelivery = fetchByPrimaryKey(deliveryId);
319    
320                    if (announcementsDelivery == null) {
321                            if (_log.isWarnEnabled()) {
322                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + deliveryId);
323                            }
324    
325                            throw new NoSuchDeliveryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
326                                    deliveryId);
327                    }
328    
329                    return announcementsDelivery;
330            }
331    
332            public AnnouncementsDelivery fetchByPrimaryKey(Serializable primaryKey)
333                    throws SystemException {
334                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
335            }
336    
337            public AnnouncementsDelivery fetchByPrimaryKey(long deliveryId)
338                    throws SystemException {
339                    AnnouncementsDelivery announcementsDelivery = (AnnouncementsDelivery)EntityCacheUtil.getResult(AnnouncementsDeliveryModelImpl.ENTITY_CACHE_ENABLED,
340                                    AnnouncementsDeliveryImpl.class, deliveryId, this);
341    
342                    if (announcementsDelivery == null) {
343                            Session session = null;
344    
345                            try {
346                                    session = openSession();
347    
348                                    announcementsDelivery = (AnnouncementsDelivery)session.get(AnnouncementsDeliveryImpl.class,
349                                                    new Long(deliveryId));
350                            }
351                            catch (Exception e) {
352                                    throw processException(e);
353                            }
354                            finally {
355                                    if (announcementsDelivery != null) {
356                                            cacheResult(announcementsDelivery);
357                                    }
358    
359                                    closeSession(session);
360                            }
361                    }
362    
363                    return announcementsDelivery;
364            }
365    
366            public List<AnnouncementsDelivery> findByUserId(long userId)
367                    throws SystemException {
368                    return findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
369            }
370    
371            public List<AnnouncementsDelivery> findByUserId(long userId, int start,
372                    int end) throws SystemException {
373                    return findByUserId(userId, start, end, null);
374            }
375    
376            public List<AnnouncementsDelivery> findByUserId(long userId, int start,
377                    int end, OrderByComparator orderByComparator) throws SystemException {
378                    Object[] finderArgs = new Object[] {
379                                    userId,
380                                    
381                                    String.valueOf(start), String.valueOf(end),
382                                    String.valueOf(orderByComparator)
383                            };
384    
385                    List<AnnouncementsDelivery> list = (List<AnnouncementsDelivery>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_USERID,
386                                    finderArgs, this);
387    
388                    if (list == null) {
389                            Session session = null;
390    
391                            try {
392                                    session = openSession();
393    
394                                    StringBundler query = null;
395    
396                                    if (orderByComparator != null) {
397                                            query = new StringBundler(3 +
398                                                            (orderByComparator.getOrderByFields().length * 3));
399                                    }
400                                    else {
401                                            query = new StringBundler(2);
402                                    }
403    
404                                    query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE);
405    
406                                    query.append(_FINDER_COLUMN_USERID_USERID_2);
407    
408                                    if (orderByComparator != null) {
409                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
410                                                    orderByComparator);
411                                    }
412    
413                                    String sql = query.toString();
414    
415                                    Query q = session.createQuery(sql);
416    
417                                    QueryPos qPos = QueryPos.getInstance(q);
418    
419                                    qPos.add(userId);
420    
421                                    list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
422                                                    getDialect(), start, end);
423                            }
424                            catch (Exception e) {
425                                    throw processException(e);
426                            }
427                            finally {
428                                    if (list == null) {
429                                            list = new ArrayList<AnnouncementsDelivery>();
430                                    }
431    
432                                    cacheResult(list);
433    
434                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_USERID,
435                                            finderArgs, list);
436    
437                                    closeSession(session);
438                            }
439                    }
440    
441                    return list;
442            }
443    
444            public AnnouncementsDelivery findByUserId_First(long userId,
445                    OrderByComparator orderByComparator)
446                    throws NoSuchDeliveryException, SystemException {
447                    List<AnnouncementsDelivery> list = findByUserId(userId, 0, 1,
448                                    orderByComparator);
449    
450                    if (list.isEmpty()) {
451                            StringBundler msg = new StringBundler(4);
452    
453                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
454    
455                            msg.append("userId=");
456                            msg.append(userId);
457    
458                            msg.append(StringPool.CLOSE_CURLY_BRACE);
459    
460                            throw new NoSuchDeliveryException(msg.toString());
461                    }
462                    else {
463                            return list.get(0);
464                    }
465            }
466    
467            public AnnouncementsDelivery findByUserId_Last(long userId,
468                    OrderByComparator orderByComparator)
469                    throws NoSuchDeliveryException, SystemException {
470                    int count = countByUserId(userId);
471    
472                    List<AnnouncementsDelivery> list = findByUserId(userId, count - 1,
473                                    count, orderByComparator);
474    
475                    if (list.isEmpty()) {
476                            StringBundler msg = new StringBundler(4);
477    
478                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
479    
480                            msg.append("userId=");
481                            msg.append(userId);
482    
483                            msg.append(StringPool.CLOSE_CURLY_BRACE);
484    
485                            throw new NoSuchDeliveryException(msg.toString());
486                    }
487                    else {
488                            return list.get(0);
489                    }
490            }
491    
492            public AnnouncementsDelivery[] findByUserId_PrevAndNext(long deliveryId,
493                    long userId, OrderByComparator orderByComparator)
494                    throws NoSuchDeliveryException, SystemException {
495                    AnnouncementsDelivery announcementsDelivery = findByPrimaryKey(deliveryId);
496    
497                    Session session = null;
498    
499                    try {
500                            session = openSession();
501    
502                            AnnouncementsDelivery[] array = new AnnouncementsDeliveryImpl[3];
503    
504                            array[0] = getByUserId_PrevAndNext(session, announcementsDelivery,
505                                            userId, orderByComparator, true);
506    
507                            array[1] = announcementsDelivery;
508    
509                            array[2] = getByUserId_PrevAndNext(session, announcementsDelivery,
510                                            userId, orderByComparator, false);
511    
512                            return array;
513                    }
514                    catch (Exception e) {
515                            throw processException(e);
516                    }
517                    finally {
518                            closeSession(session);
519                    }
520            }
521    
522            protected AnnouncementsDelivery getByUserId_PrevAndNext(Session session,
523                    AnnouncementsDelivery announcementsDelivery, long userId,
524                    OrderByComparator orderByComparator, boolean previous) {
525                    StringBundler query = null;
526    
527                    if (orderByComparator != null) {
528                            query = new StringBundler(6 +
529                                            (orderByComparator.getOrderByFields().length * 6));
530                    }
531                    else {
532                            query = new StringBundler(3);
533                    }
534    
535                    query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE);
536    
537                    query.append(_FINDER_COLUMN_USERID_USERID_2);
538    
539                    if (orderByComparator != null) {
540                            String[] orderByFields = orderByComparator.getOrderByFields();
541    
542                            if (orderByFields.length > 0) {
543                                    query.append(WHERE_AND);
544                            }
545    
546                            for (int i = 0; i < orderByFields.length; i++) {
547                                    query.append(_ORDER_BY_ENTITY_ALIAS);
548                                    query.append(orderByFields[i]);
549    
550                                    if ((i + 1) < orderByFields.length) {
551                                            if (orderByComparator.isAscending() ^ previous) {
552                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
553                                            }
554                                            else {
555                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
556                                            }
557                                    }
558                                    else {
559                                            if (orderByComparator.isAscending() ^ previous) {
560                                                    query.append(WHERE_GREATER_THAN);
561                                            }
562                                            else {
563                                                    query.append(WHERE_LESSER_THAN);
564                                            }
565                                    }
566                            }
567    
568                            query.append(ORDER_BY_CLAUSE);
569    
570                            for (int i = 0; i < orderByFields.length; i++) {
571                                    query.append(_ORDER_BY_ENTITY_ALIAS);
572                                    query.append(orderByFields[i]);
573    
574                                    if ((i + 1) < orderByFields.length) {
575                                            if (orderByComparator.isAscending() ^ previous) {
576                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
577                                            }
578                                            else {
579                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
580                                            }
581                                    }
582                                    else {
583                                            if (orderByComparator.isAscending() ^ previous) {
584                                                    query.append(ORDER_BY_ASC);
585                                            }
586                                            else {
587                                                    query.append(ORDER_BY_DESC);
588                                            }
589                                    }
590                            }
591                    }
592    
593                    String sql = query.toString();
594    
595                    Query q = session.createQuery(sql);
596    
597                    q.setFirstResult(0);
598                    q.setMaxResults(2);
599    
600                    QueryPos qPos = QueryPos.getInstance(q);
601    
602                    qPos.add(userId);
603    
604                    if (orderByComparator != null) {
605                            Object[] values = orderByComparator.getOrderByValues(announcementsDelivery);
606    
607                            for (Object value : values) {
608                                    qPos.add(value);
609                            }
610                    }
611    
612                    List<AnnouncementsDelivery> list = q.list();
613    
614                    if (list.size() == 2) {
615                            return list.get(1);
616                    }
617                    else {
618                            return null;
619                    }
620            }
621    
622            public AnnouncementsDelivery findByU_T(long userId, String type)
623                    throws NoSuchDeliveryException, SystemException {
624                    AnnouncementsDelivery announcementsDelivery = fetchByU_T(userId, type);
625    
626                    if (announcementsDelivery == null) {
627                            StringBundler msg = new StringBundler(6);
628    
629                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
630    
631                            msg.append("userId=");
632                            msg.append(userId);
633    
634                            msg.append(", type=");
635                            msg.append(type);
636    
637                            msg.append(StringPool.CLOSE_CURLY_BRACE);
638    
639                            if (_log.isWarnEnabled()) {
640                                    _log.warn(msg.toString());
641                            }
642    
643                            throw new NoSuchDeliveryException(msg.toString());
644                    }
645    
646                    return announcementsDelivery;
647            }
648    
649            public AnnouncementsDelivery fetchByU_T(long userId, String type)
650                    throws SystemException {
651                    return fetchByU_T(userId, type, true);
652            }
653    
654            public AnnouncementsDelivery fetchByU_T(long userId, String type,
655                    boolean retrieveFromCache) throws SystemException {
656                    Object[] finderArgs = new Object[] { userId, type };
657    
658                    Object result = null;
659    
660                    if (retrieveFromCache) {
661                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_U_T,
662                                            finderArgs, this);
663                    }
664    
665                    if (result == null) {
666                            Session session = null;
667    
668                            try {
669                                    session = openSession();
670    
671                                    StringBundler query = new StringBundler(3);
672    
673                                    query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE);
674    
675                                    query.append(_FINDER_COLUMN_U_T_USERID_2);
676    
677                                    if (type == null) {
678                                            query.append(_FINDER_COLUMN_U_T_TYPE_1);
679                                    }
680                                    else {
681                                            if (type.equals(StringPool.BLANK)) {
682                                                    query.append(_FINDER_COLUMN_U_T_TYPE_3);
683                                            }
684                                            else {
685                                                    query.append(_FINDER_COLUMN_U_T_TYPE_2);
686                                            }
687                                    }
688    
689                                    String sql = query.toString();
690    
691                                    Query q = session.createQuery(sql);
692    
693                                    QueryPos qPos = QueryPos.getInstance(q);
694    
695                                    qPos.add(userId);
696    
697                                    if (type != null) {
698                                            qPos.add(type);
699                                    }
700    
701                                    List<AnnouncementsDelivery> list = q.list();
702    
703                                    result = list;
704    
705                                    AnnouncementsDelivery announcementsDelivery = null;
706    
707                                    if (list.isEmpty()) {
708                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
709                                                    finderArgs, list);
710                                    }
711                                    else {
712                                            announcementsDelivery = list.get(0);
713    
714                                            cacheResult(announcementsDelivery);
715    
716                                            if ((announcementsDelivery.getUserId() != userId) ||
717                                                            (announcementsDelivery.getType() == null) ||
718                                                            !announcementsDelivery.getType().equals(type)) {
719                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
720                                                            finderArgs, announcementsDelivery);
721                                            }
722                                    }
723    
724                                    return announcementsDelivery;
725                            }
726                            catch (Exception e) {
727                                    throw processException(e);
728                            }
729                            finally {
730                                    if (result == null) {
731                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_T,
732                                                    finderArgs, new ArrayList<AnnouncementsDelivery>());
733                                    }
734    
735                                    closeSession(session);
736                            }
737                    }
738                    else {
739                            if (result instanceof List<?>) {
740                                    return null;
741                            }
742                            else {
743                                    return (AnnouncementsDelivery)result;
744                            }
745                    }
746            }
747    
748            public List<AnnouncementsDelivery> findAll() throws SystemException {
749                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
750            }
751    
752            public List<AnnouncementsDelivery> findAll(int start, int end)
753                    throws SystemException {
754                    return findAll(start, end, null);
755            }
756    
757            public List<AnnouncementsDelivery> findAll(int start, int end,
758                    OrderByComparator orderByComparator) throws SystemException {
759                    Object[] finderArgs = new Object[] {
760                                    String.valueOf(start), String.valueOf(end),
761                                    String.valueOf(orderByComparator)
762                            };
763    
764                    List<AnnouncementsDelivery> list = (List<AnnouncementsDelivery>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
765                                    finderArgs, this);
766    
767                    if (list == null) {
768                            Session session = null;
769    
770                            try {
771                                    session = openSession();
772    
773                                    StringBundler query = null;
774                                    String sql = null;
775    
776                                    if (orderByComparator != null) {
777                                            query = new StringBundler(2 +
778                                                            (orderByComparator.getOrderByFields().length * 3));
779    
780                                            query.append(_SQL_SELECT_ANNOUNCEMENTSDELIVERY);
781    
782                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
783                                                    orderByComparator);
784    
785                                            sql = query.toString();
786                                    }
787                                    else {
788                                            sql = _SQL_SELECT_ANNOUNCEMENTSDELIVERY;
789                                    }
790    
791                                    Query q = session.createQuery(sql);
792    
793                                    if (orderByComparator == null) {
794                                            list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
795                                                            getDialect(), start, end, false);
796    
797                                            Collections.sort(list);
798                                    }
799                                    else {
800                                            list = (List<AnnouncementsDelivery>)QueryUtil.list(q,
801                                                            getDialect(), start, end);
802                                    }
803                            }
804                            catch (Exception e) {
805                                    throw processException(e);
806                            }
807                            finally {
808                                    if (list == null) {
809                                            list = new ArrayList<AnnouncementsDelivery>();
810                                    }
811    
812                                    cacheResult(list);
813    
814                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
815    
816                                    closeSession(session);
817                            }
818                    }
819    
820                    return list;
821            }
822    
823            public void removeByUserId(long userId) throws SystemException {
824                    for (AnnouncementsDelivery announcementsDelivery : findByUserId(userId)) {
825                            remove(announcementsDelivery);
826                    }
827            }
828    
829            public void removeByU_T(long userId, String type)
830                    throws NoSuchDeliveryException, SystemException {
831                    AnnouncementsDelivery announcementsDelivery = findByU_T(userId, type);
832    
833                    remove(announcementsDelivery);
834            }
835    
836            public void removeAll() throws SystemException {
837                    for (AnnouncementsDelivery announcementsDelivery : findAll()) {
838                            remove(announcementsDelivery);
839                    }
840            }
841    
842            public int countByUserId(long userId) throws SystemException {
843                    Object[] finderArgs = new Object[] { userId };
844    
845                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_USERID,
846                                    finderArgs, this);
847    
848                    if (count == null) {
849                            Session session = null;
850    
851                            try {
852                                    session = openSession();
853    
854                                    StringBundler query = new StringBundler(2);
855    
856                                    query.append(_SQL_COUNT_ANNOUNCEMENTSDELIVERY_WHERE);
857    
858                                    query.append(_FINDER_COLUMN_USERID_USERID_2);
859    
860                                    String sql = query.toString();
861    
862                                    Query q = session.createQuery(sql);
863    
864                                    QueryPos qPos = QueryPos.getInstance(q);
865    
866                                    qPos.add(userId);
867    
868                                    count = (Long)q.uniqueResult();
869                            }
870                            catch (Exception e) {
871                                    throw processException(e);
872                            }
873                            finally {
874                                    if (count == null) {
875                                            count = Long.valueOf(0);
876                                    }
877    
878                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_USERID,
879                                            finderArgs, count);
880    
881                                    closeSession(session);
882                            }
883                    }
884    
885                    return count.intValue();
886            }
887    
888            public int countByU_T(long userId, String type) throws SystemException {
889                    Object[] finderArgs = new Object[] { userId, type };
890    
891                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_U_T,
892                                    finderArgs, this);
893    
894                    if (count == null) {
895                            Session session = null;
896    
897                            try {
898                                    session = openSession();
899    
900                                    StringBundler query = new StringBundler(3);
901    
902                                    query.append(_SQL_COUNT_ANNOUNCEMENTSDELIVERY_WHERE);
903    
904                                    query.append(_FINDER_COLUMN_U_T_USERID_2);
905    
906                                    if (type == null) {
907                                            query.append(_FINDER_COLUMN_U_T_TYPE_1);
908                                    }
909                                    else {
910                                            if (type.equals(StringPool.BLANK)) {
911                                                    query.append(_FINDER_COLUMN_U_T_TYPE_3);
912                                            }
913                                            else {
914                                                    query.append(_FINDER_COLUMN_U_T_TYPE_2);
915                                            }
916                                    }
917    
918                                    String sql = query.toString();
919    
920                                    Query q = session.createQuery(sql);
921    
922                                    QueryPos qPos = QueryPos.getInstance(q);
923    
924                                    qPos.add(userId);
925    
926                                    if (type != null) {
927                                            qPos.add(type);
928                                    }
929    
930                                    count = (Long)q.uniqueResult();
931                            }
932                            catch (Exception e) {
933                                    throw processException(e);
934                            }
935                            finally {
936                                    if (count == null) {
937                                            count = Long.valueOf(0);
938                                    }
939    
940                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_U_T, finderArgs,
941                                            count);
942    
943                                    closeSession(session);
944                            }
945                    }
946    
947                    return count.intValue();
948            }
949    
950            public int countAll() throws SystemException {
951                    Object[] finderArgs = new Object[0];
952    
953                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
954                                    finderArgs, this);
955    
956                    if (count == null) {
957                            Session session = null;
958    
959                            try {
960                                    session = openSession();
961    
962                                    Query q = session.createQuery(_SQL_COUNT_ANNOUNCEMENTSDELIVERY);
963    
964                                    count = (Long)q.uniqueResult();
965                            }
966                            catch (Exception e) {
967                                    throw processException(e);
968                            }
969                            finally {
970                                    if (count == null) {
971                                            count = Long.valueOf(0);
972                                    }
973    
974                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
975                                            count);
976    
977                                    closeSession(session);
978                            }
979                    }
980    
981                    return count.intValue();
982            }
983    
984            public void afterPropertiesSet() {
985                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
986                                            com.liferay.portal.util.PropsUtil.get(
987                                                    "value.object.listener.com.liferay.portlet.announcements.model.AnnouncementsDelivery")));
988    
989                    if (listenerClassNames.length > 0) {
990                            try {
991                                    List<ModelListener<AnnouncementsDelivery>> listenersList = new ArrayList<ModelListener<AnnouncementsDelivery>>();
992    
993                                    for (String listenerClassName : listenerClassNames) {
994                                            listenersList.add((ModelListener<AnnouncementsDelivery>)InstanceFactory.newInstance(
995                                                            listenerClassName));
996                                    }
997    
998                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
999                            }
1000                            catch (Exception e) {
1001                                    _log.error(e);
1002                            }
1003                    }
1004            }
1005    
1006            @BeanReference(type = AnnouncementsDeliveryPersistence.class)
1007            protected AnnouncementsDeliveryPersistence announcementsDeliveryPersistence;
1008            @BeanReference(type = AnnouncementsEntryPersistence.class)
1009            protected AnnouncementsEntryPersistence announcementsEntryPersistence;
1010            @BeanReference(type = AnnouncementsFlagPersistence.class)
1011            protected AnnouncementsFlagPersistence announcementsFlagPersistence;
1012            @BeanReference(type = ResourcePersistence.class)
1013            protected ResourcePersistence resourcePersistence;
1014            @BeanReference(type = UserPersistence.class)
1015            protected UserPersistence userPersistence;
1016            private static final String _SQL_SELECT_ANNOUNCEMENTSDELIVERY = "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery";
1017            private static final String _SQL_SELECT_ANNOUNCEMENTSDELIVERY_WHERE = "SELECT announcementsDelivery FROM AnnouncementsDelivery announcementsDelivery WHERE ";
1018            private static final String _SQL_COUNT_ANNOUNCEMENTSDELIVERY = "SELECT COUNT(announcementsDelivery) FROM AnnouncementsDelivery announcementsDelivery";
1019            private static final String _SQL_COUNT_ANNOUNCEMENTSDELIVERY_WHERE = "SELECT COUNT(announcementsDelivery) FROM AnnouncementsDelivery announcementsDelivery WHERE ";
1020            private static final String _FINDER_COLUMN_USERID_USERID_2 = "announcementsDelivery.userId = ?";
1021            private static final String _FINDER_COLUMN_U_T_USERID_2 = "announcementsDelivery.userId = ? AND ";
1022            private static final String _FINDER_COLUMN_U_T_TYPE_1 = "announcementsDelivery.type IS NULL";
1023            private static final String _FINDER_COLUMN_U_T_TYPE_2 = "announcementsDelivery.type = ?";
1024            private static final String _FINDER_COLUMN_U_T_TYPE_3 = "(announcementsDelivery.type IS NULL OR announcementsDelivery.type = ?)";
1025            private static final String _ORDER_BY_ENTITY_ALIAS = "announcementsDelivery.";
1026            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No AnnouncementsDelivery exists with the primary key ";
1027            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No AnnouncementsDelivery exists with the key {";
1028            private static Log _log = LogFactoryUtil.getLog(AnnouncementsDeliveryPersistenceImpl.class);
1029    }