001    /**
002     * Copyright (c) 2000-present 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.portal.service.persistence.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.NoSuchEmailAddressException;
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.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.OrderByComparator;
030    import com.liferay.portal.kernel.util.SetUtil;
031    import com.liferay.portal.kernel.util.StringBundler;
032    import com.liferay.portal.kernel.util.StringPool;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
035    import com.liferay.portal.model.CacheModel;
036    import com.liferay.portal.model.EmailAddress;
037    import com.liferay.portal.model.MVCCModel;
038    import com.liferay.portal.model.impl.EmailAddressImpl;
039    import com.liferay.portal.model.impl.EmailAddressModelImpl;
040    import com.liferay.portal.service.ServiceContext;
041    import com.liferay.portal.service.ServiceContextThreadLocal;
042    import com.liferay.portal.service.persistence.EmailAddressPersistence;
043    
044    import java.io.Serializable;
045    
046    import java.util.Collections;
047    import java.util.Date;
048    import java.util.HashMap;
049    import java.util.HashSet;
050    import java.util.Iterator;
051    import java.util.List;
052    import java.util.Map;
053    import java.util.Set;
054    
055    /**
056     * The persistence implementation for the email address service.
057     *
058     * <p>
059     * Caching information and settings can be found in <code>portal.properties</code>
060     * </p>
061     *
062     * @author Brian Wing Shun Chan
063     * @see EmailAddressPersistence
064     * @see com.liferay.portal.service.persistence.EmailAddressUtil
065     * @generated
066     */
067    @ProviderType
068    public class EmailAddressPersistenceImpl extends BasePersistenceImpl<EmailAddress>
069            implements EmailAddressPersistence {
070            /*
071             * NOTE FOR DEVELOPERS:
072             *
073             * Never modify or reference this class directly. Always use {@link EmailAddressUtil} to access the email address persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
074             */
075            public static final String FINDER_CLASS_NAME_ENTITY = EmailAddressImpl.class.getName();
076            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
077                    ".List1";
078            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
079                    ".List2";
080            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
081                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
082                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
083            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
084                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
085                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
086            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
087                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
088                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
089            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
090                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
091                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid",
092                            new String[] {
093                                    String.class.getName(),
094                                    
095                            Integer.class.getName(), Integer.class.getName(),
096                                    OrderByComparator.class.getName()
097                            });
098            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
099                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
100                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid",
101                            new String[] { String.class.getName() },
102                            EmailAddressModelImpl.UUID_COLUMN_BITMASK |
103                            EmailAddressModelImpl.CREATEDATE_COLUMN_BITMASK);
104            public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
105                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
106                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid",
107                            new String[] { String.class.getName() });
108    
109            /**
110             * Returns all the email addresses where uuid = &#63;.
111             *
112             * @param uuid the uuid
113             * @return the matching email addresses
114             */
115            @Override
116            public List<EmailAddress> findByUuid(String uuid) {
117                    return findByUuid(uuid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
118            }
119    
120            /**
121             * Returns a range of all the email addresses where uuid = &#63;.
122             *
123             * <p>
124             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
125             * </p>
126             *
127             * @param uuid the uuid
128             * @param start the lower bound of the range of email addresses
129             * @param end the upper bound of the range of email addresses (not inclusive)
130             * @return the range of matching email addresses
131             */
132            @Override
133            public List<EmailAddress> findByUuid(String uuid, int start, int end) {
134                    return findByUuid(uuid, start, end, null);
135            }
136    
137            /**
138             * Returns an ordered range of all the email addresses where uuid = &#63;.
139             *
140             * <p>
141             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
142             * </p>
143             *
144             * @param uuid the uuid
145             * @param start the lower bound of the range of email addresses
146             * @param end the upper bound of the range of email addresses (not inclusive)
147             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
148             * @return the ordered range of matching email addresses
149             */
150            @Override
151            public List<EmailAddress> findByUuid(String uuid, int start, int end,
152                    OrderByComparator<EmailAddress> orderByComparator) {
153                    boolean pagination = true;
154                    FinderPath finderPath = null;
155                    Object[] finderArgs = null;
156    
157                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
158                                    (orderByComparator == null)) {
159                            pagination = false;
160                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID;
161                            finderArgs = new Object[] { uuid };
162                    }
163                    else {
164                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID;
165                            finderArgs = new Object[] { uuid, start, end, orderByComparator };
166                    }
167    
168                    List<EmailAddress> list = (List<EmailAddress>)FinderCacheUtil.getResult(finderPath,
169                                    finderArgs, this);
170    
171                    if ((list != null) && !list.isEmpty()) {
172                            for (EmailAddress emailAddress : list) {
173                                    if (!Validator.equals(uuid, emailAddress.getUuid())) {
174                                            list = null;
175    
176                                            break;
177                                    }
178                            }
179                    }
180    
181                    if (list == null) {
182                            StringBundler query = null;
183    
184                            if (orderByComparator != null) {
185                                    query = new StringBundler(3 +
186                                                    (orderByComparator.getOrderByFields().length * 3));
187                            }
188                            else {
189                                    query = new StringBundler(3);
190                            }
191    
192                            query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
193    
194                            boolean bindUuid = false;
195    
196                            if (uuid == null) {
197                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
198                            }
199                            else if (uuid.equals(StringPool.BLANK)) {
200                                    query.append(_FINDER_COLUMN_UUID_UUID_3);
201                            }
202                            else {
203                                    bindUuid = true;
204    
205                                    query.append(_FINDER_COLUMN_UUID_UUID_2);
206                            }
207    
208                            if (orderByComparator != null) {
209                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
210                                            orderByComparator);
211                            }
212                            else
213                             if (pagination) {
214                                    query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
215                            }
216    
217                            String sql = query.toString();
218    
219                            Session session = null;
220    
221                            try {
222                                    session = openSession();
223    
224                                    Query q = session.createQuery(sql);
225    
226                                    QueryPos qPos = QueryPos.getInstance(q);
227    
228                                    if (bindUuid) {
229                                            qPos.add(uuid);
230                                    }
231    
232                                    if (!pagination) {
233                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
234                                                            start, end, false);
235    
236                                            Collections.sort(list);
237    
238                                            list = Collections.unmodifiableList(list);
239                                    }
240                                    else {
241                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
242                                                            start, end);
243                                    }
244    
245                                    cacheResult(list);
246    
247                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
248                            }
249                            catch (Exception e) {
250                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
251    
252                                    throw processException(e);
253                            }
254                            finally {
255                                    closeSession(session);
256                            }
257                    }
258    
259                    return list;
260            }
261    
262            /**
263             * Returns the first email address in the ordered set where uuid = &#63;.
264             *
265             * @param uuid the uuid
266             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
267             * @return the first matching email address
268             * @throws NoSuchEmailAddressException if a matching email address could not be found
269             */
270            @Override
271            public EmailAddress findByUuid_First(String uuid,
272                    OrderByComparator<EmailAddress> orderByComparator)
273                    throws NoSuchEmailAddressException {
274                    EmailAddress emailAddress = fetchByUuid_First(uuid, orderByComparator);
275    
276                    if (emailAddress != null) {
277                            return emailAddress;
278                    }
279    
280                    StringBundler msg = new StringBundler(4);
281    
282                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
283    
284                    msg.append("uuid=");
285                    msg.append(uuid);
286    
287                    msg.append(StringPool.CLOSE_CURLY_BRACE);
288    
289                    throw new NoSuchEmailAddressException(msg.toString());
290            }
291    
292            /**
293             * Returns the first email address in the ordered set where uuid = &#63;.
294             *
295             * @param uuid the uuid
296             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
297             * @return the first matching email address, or <code>null</code> if a matching email address could not be found
298             */
299            @Override
300            public EmailAddress fetchByUuid_First(String uuid,
301                    OrderByComparator<EmailAddress> orderByComparator) {
302                    List<EmailAddress> list = findByUuid(uuid, 0, 1, orderByComparator);
303    
304                    if (!list.isEmpty()) {
305                            return list.get(0);
306                    }
307    
308                    return null;
309            }
310    
311            /**
312             * Returns the last email address in the ordered set where uuid = &#63;.
313             *
314             * @param uuid the uuid
315             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
316             * @return the last matching email address
317             * @throws NoSuchEmailAddressException if a matching email address could not be found
318             */
319            @Override
320            public EmailAddress findByUuid_Last(String uuid,
321                    OrderByComparator<EmailAddress> orderByComparator)
322                    throws NoSuchEmailAddressException {
323                    EmailAddress emailAddress = fetchByUuid_Last(uuid, orderByComparator);
324    
325                    if (emailAddress != null) {
326                            return emailAddress;
327                    }
328    
329                    StringBundler msg = new StringBundler(4);
330    
331                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
332    
333                    msg.append("uuid=");
334                    msg.append(uuid);
335    
336                    msg.append(StringPool.CLOSE_CURLY_BRACE);
337    
338                    throw new NoSuchEmailAddressException(msg.toString());
339            }
340    
341            /**
342             * Returns the last email address in the ordered set where uuid = &#63;.
343             *
344             * @param uuid the uuid
345             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
346             * @return the last matching email address, or <code>null</code> if a matching email address could not be found
347             */
348            @Override
349            public EmailAddress fetchByUuid_Last(String uuid,
350                    OrderByComparator<EmailAddress> orderByComparator) {
351                    int count = countByUuid(uuid);
352    
353                    if (count == 0) {
354                            return null;
355                    }
356    
357                    List<EmailAddress> list = findByUuid(uuid, count - 1, count,
358                                    orderByComparator);
359    
360                    if (!list.isEmpty()) {
361                            return list.get(0);
362                    }
363    
364                    return null;
365            }
366    
367            /**
368             * Returns the email addresses before and after the current email address in the ordered set where uuid = &#63;.
369             *
370             * @param emailAddressId the primary key of the current email address
371             * @param uuid the uuid
372             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
373             * @return the previous, current, and next email address
374             * @throws NoSuchEmailAddressException if a email address with the primary key could not be found
375             */
376            @Override
377            public EmailAddress[] findByUuid_PrevAndNext(long emailAddressId,
378                    String uuid, OrderByComparator<EmailAddress> orderByComparator)
379                    throws NoSuchEmailAddressException {
380                    EmailAddress emailAddress = findByPrimaryKey(emailAddressId);
381    
382                    Session session = null;
383    
384                    try {
385                            session = openSession();
386    
387                            EmailAddress[] array = new EmailAddressImpl[3];
388    
389                            array[0] = getByUuid_PrevAndNext(session, emailAddress, uuid,
390                                            orderByComparator, true);
391    
392                            array[1] = emailAddress;
393    
394                            array[2] = getByUuid_PrevAndNext(session, emailAddress, uuid,
395                                            orderByComparator, false);
396    
397                            return array;
398                    }
399                    catch (Exception e) {
400                            throw processException(e);
401                    }
402                    finally {
403                            closeSession(session);
404                    }
405            }
406    
407            protected EmailAddress getByUuid_PrevAndNext(Session session,
408                    EmailAddress emailAddress, String uuid,
409                    OrderByComparator<EmailAddress> orderByComparator, boolean previous) {
410                    StringBundler query = null;
411    
412                    if (orderByComparator != null) {
413                            query = new StringBundler(6 +
414                                            (orderByComparator.getOrderByFields().length * 6));
415                    }
416                    else {
417                            query = new StringBundler(3);
418                    }
419    
420                    query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
421    
422                    boolean bindUuid = false;
423    
424                    if (uuid == null) {
425                            query.append(_FINDER_COLUMN_UUID_UUID_1);
426                    }
427                    else if (uuid.equals(StringPool.BLANK)) {
428                            query.append(_FINDER_COLUMN_UUID_UUID_3);
429                    }
430                    else {
431                            bindUuid = true;
432    
433                            query.append(_FINDER_COLUMN_UUID_UUID_2);
434                    }
435    
436                    if (orderByComparator != null) {
437                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
438    
439                            if (orderByConditionFields.length > 0) {
440                                    query.append(WHERE_AND);
441                            }
442    
443                            for (int i = 0; i < orderByConditionFields.length; i++) {
444                                    query.append(_ORDER_BY_ENTITY_ALIAS);
445                                    query.append(orderByConditionFields[i]);
446    
447                                    if ((i + 1) < orderByConditionFields.length) {
448                                            if (orderByComparator.isAscending() ^ previous) {
449                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
450                                            }
451                                            else {
452                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
453                                            }
454                                    }
455                                    else {
456                                            if (orderByComparator.isAscending() ^ previous) {
457                                                    query.append(WHERE_GREATER_THAN);
458                                            }
459                                            else {
460                                                    query.append(WHERE_LESSER_THAN);
461                                            }
462                                    }
463                            }
464    
465                            query.append(ORDER_BY_CLAUSE);
466    
467                            String[] orderByFields = orderByComparator.getOrderByFields();
468    
469                            for (int i = 0; i < orderByFields.length; i++) {
470                                    query.append(_ORDER_BY_ENTITY_ALIAS);
471                                    query.append(orderByFields[i]);
472    
473                                    if ((i + 1) < orderByFields.length) {
474                                            if (orderByComparator.isAscending() ^ previous) {
475                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
476                                            }
477                                            else {
478                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
479                                            }
480                                    }
481                                    else {
482                                            if (orderByComparator.isAscending() ^ previous) {
483                                                    query.append(ORDER_BY_ASC);
484                                            }
485                                            else {
486                                                    query.append(ORDER_BY_DESC);
487                                            }
488                                    }
489                            }
490                    }
491                    else {
492                            query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
493                    }
494    
495                    String sql = query.toString();
496    
497                    Query q = session.createQuery(sql);
498    
499                    q.setFirstResult(0);
500                    q.setMaxResults(2);
501    
502                    QueryPos qPos = QueryPos.getInstance(q);
503    
504                    if (bindUuid) {
505                            qPos.add(uuid);
506                    }
507    
508                    if (orderByComparator != null) {
509                            Object[] values = orderByComparator.getOrderByConditionValues(emailAddress);
510    
511                            for (Object value : values) {
512                                    qPos.add(value);
513                            }
514                    }
515    
516                    List<EmailAddress> list = q.list();
517    
518                    if (list.size() == 2) {
519                            return list.get(1);
520                    }
521                    else {
522                            return null;
523                    }
524            }
525    
526            /**
527             * Removes all the email addresses where uuid = &#63; from the database.
528             *
529             * @param uuid the uuid
530             */
531            @Override
532            public void removeByUuid(String uuid) {
533                    for (EmailAddress emailAddress : findByUuid(uuid, QueryUtil.ALL_POS,
534                                    QueryUtil.ALL_POS, null)) {
535                            remove(emailAddress);
536                    }
537            }
538    
539            /**
540             * Returns the number of email addresses where uuid = &#63;.
541             *
542             * @param uuid the uuid
543             * @return the number of matching email addresses
544             */
545            @Override
546            public int countByUuid(String uuid) {
547                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID;
548    
549                    Object[] finderArgs = new Object[] { uuid };
550    
551                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
552                                    this);
553    
554                    if (count == null) {
555                            StringBundler query = new StringBundler(2);
556    
557                            query.append(_SQL_COUNT_EMAILADDRESS_WHERE);
558    
559                            boolean bindUuid = false;
560    
561                            if (uuid == null) {
562                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
563                            }
564                            else if (uuid.equals(StringPool.BLANK)) {
565                                    query.append(_FINDER_COLUMN_UUID_UUID_3);
566                            }
567                            else {
568                                    bindUuid = true;
569    
570                                    query.append(_FINDER_COLUMN_UUID_UUID_2);
571                            }
572    
573                            String sql = query.toString();
574    
575                            Session session = null;
576    
577                            try {
578                                    session = openSession();
579    
580                                    Query q = session.createQuery(sql);
581    
582                                    QueryPos qPos = QueryPos.getInstance(q);
583    
584                                    if (bindUuid) {
585                                            qPos.add(uuid);
586                                    }
587    
588                                    count = (Long)q.uniqueResult();
589    
590                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
591                            }
592                            catch (Exception e) {
593                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
594    
595                                    throw processException(e);
596                            }
597                            finally {
598                                    closeSession(session);
599                            }
600                    }
601    
602                    return count.intValue();
603            }
604    
605            private static final String _FINDER_COLUMN_UUID_UUID_1 = "emailAddress.uuid IS NULL";
606            private static final String _FINDER_COLUMN_UUID_UUID_2 = "emailAddress.uuid = ?";
607            private static final String _FINDER_COLUMN_UUID_UUID_3 = "(emailAddress.uuid IS NULL OR emailAddress.uuid = '')";
608            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
609                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
610                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid_C",
611                            new String[] {
612                                    String.class.getName(), Long.class.getName(),
613                                    
614                            Integer.class.getName(), Integer.class.getName(),
615                                    OrderByComparator.class.getName()
616                            });
617            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C =
618                    new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
619                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
620                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid_C",
621                            new String[] { String.class.getName(), Long.class.getName() },
622                            EmailAddressModelImpl.UUID_COLUMN_BITMASK |
623                            EmailAddressModelImpl.COMPANYID_COLUMN_BITMASK |
624                            EmailAddressModelImpl.CREATEDATE_COLUMN_BITMASK);
625            public static final FinderPath FINDER_PATH_COUNT_BY_UUID_C = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
626                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
627                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid_C",
628                            new String[] { String.class.getName(), Long.class.getName() });
629    
630            /**
631             * Returns all the email addresses where uuid = &#63; and companyId = &#63;.
632             *
633             * @param uuid the uuid
634             * @param companyId the company ID
635             * @return the matching email addresses
636             */
637            @Override
638            public List<EmailAddress> findByUuid_C(String uuid, long companyId) {
639                    return findByUuid_C(uuid, companyId, QueryUtil.ALL_POS,
640                            QueryUtil.ALL_POS, null);
641            }
642    
643            /**
644             * Returns a range of all the email addresses where uuid = &#63; and companyId = &#63;.
645             *
646             * <p>
647             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
648             * </p>
649             *
650             * @param uuid the uuid
651             * @param companyId the company ID
652             * @param start the lower bound of the range of email addresses
653             * @param end the upper bound of the range of email addresses (not inclusive)
654             * @return the range of matching email addresses
655             */
656            @Override
657            public List<EmailAddress> findByUuid_C(String uuid, long companyId,
658                    int start, int end) {
659                    return findByUuid_C(uuid, companyId, start, end, null);
660            }
661    
662            /**
663             * Returns an ordered range of all the email addresses where uuid = &#63; and companyId = &#63;.
664             *
665             * <p>
666             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
667             * </p>
668             *
669             * @param uuid the uuid
670             * @param companyId the company ID
671             * @param start the lower bound of the range of email addresses
672             * @param end the upper bound of the range of email addresses (not inclusive)
673             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
674             * @return the ordered range of matching email addresses
675             */
676            @Override
677            public List<EmailAddress> findByUuid_C(String uuid, long companyId,
678                    int start, int end, OrderByComparator<EmailAddress> orderByComparator) {
679                    boolean pagination = true;
680                    FinderPath finderPath = null;
681                    Object[] finderArgs = null;
682    
683                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
684                                    (orderByComparator == null)) {
685                            pagination = false;
686                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C;
687                            finderArgs = new Object[] { uuid, companyId };
688                    }
689                    else {
690                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C;
691                            finderArgs = new Object[] {
692                                            uuid, companyId,
693                                            
694                                            start, end, orderByComparator
695                                    };
696                    }
697    
698                    List<EmailAddress> list = (List<EmailAddress>)FinderCacheUtil.getResult(finderPath,
699                                    finderArgs, this);
700    
701                    if ((list != null) && !list.isEmpty()) {
702                            for (EmailAddress emailAddress : list) {
703                                    if (!Validator.equals(uuid, emailAddress.getUuid()) ||
704                                                    (companyId != emailAddress.getCompanyId())) {
705                                            list = null;
706    
707                                            break;
708                                    }
709                            }
710                    }
711    
712                    if (list == null) {
713                            StringBundler query = null;
714    
715                            if (orderByComparator != null) {
716                                    query = new StringBundler(4 +
717                                                    (orderByComparator.getOrderByFields().length * 3));
718                            }
719                            else {
720                                    query = new StringBundler(4);
721                            }
722    
723                            query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
724    
725                            boolean bindUuid = false;
726    
727                            if (uuid == null) {
728                                    query.append(_FINDER_COLUMN_UUID_C_UUID_1);
729                            }
730                            else if (uuid.equals(StringPool.BLANK)) {
731                                    query.append(_FINDER_COLUMN_UUID_C_UUID_3);
732                            }
733                            else {
734                                    bindUuid = true;
735    
736                                    query.append(_FINDER_COLUMN_UUID_C_UUID_2);
737                            }
738    
739                            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
740    
741                            if (orderByComparator != null) {
742                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
743                                            orderByComparator);
744                            }
745                            else
746                             if (pagination) {
747                                    query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
748                            }
749    
750                            String sql = query.toString();
751    
752                            Session session = null;
753    
754                            try {
755                                    session = openSession();
756    
757                                    Query q = session.createQuery(sql);
758    
759                                    QueryPos qPos = QueryPos.getInstance(q);
760    
761                                    if (bindUuid) {
762                                            qPos.add(uuid);
763                                    }
764    
765                                    qPos.add(companyId);
766    
767                                    if (!pagination) {
768                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
769                                                            start, end, false);
770    
771                                            Collections.sort(list);
772    
773                                            list = Collections.unmodifiableList(list);
774                                    }
775                                    else {
776                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
777                                                            start, end);
778                                    }
779    
780                                    cacheResult(list);
781    
782                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
783                            }
784                            catch (Exception e) {
785                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
786    
787                                    throw processException(e);
788                            }
789                            finally {
790                                    closeSession(session);
791                            }
792                    }
793    
794                    return list;
795            }
796    
797            /**
798             * Returns the first email address in the ordered set where uuid = &#63; and companyId = &#63;.
799             *
800             * @param uuid the uuid
801             * @param companyId the company ID
802             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
803             * @return the first matching email address
804             * @throws NoSuchEmailAddressException if a matching email address could not be found
805             */
806            @Override
807            public EmailAddress findByUuid_C_First(String uuid, long companyId,
808                    OrderByComparator<EmailAddress> orderByComparator)
809                    throws NoSuchEmailAddressException {
810                    EmailAddress emailAddress = fetchByUuid_C_First(uuid, companyId,
811                                    orderByComparator);
812    
813                    if (emailAddress != null) {
814                            return emailAddress;
815                    }
816    
817                    StringBundler msg = new StringBundler(6);
818    
819                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
820    
821                    msg.append("uuid=");
822                    msg.append(uuid);
823    
824                    msg.append(", companyId=");
825                    msg.append(companyId);
826    
827                    msg.append(StringPool.CLOSE_CURLY_BRACE);
828    
829                    throw new NoSuchEmailAddressException(msg.toString());
830            }
831    
832            /**
833             * Returns the first email address in the ordered set where uuid = &#63; and companyId = &#63;.
834             *
835             * @param uuid the uuid
836             * @param companyId the company ID
837             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
838             * @return the first matching email address, or <code>null</code> if a matching email address could not be found
839             */
840            @Override
841            public EmailAddress fetchByUuid_C_First(String uuid, long companyId,
842                    OrderByComparator<EmailAddress> orderByComparator) {
843                    List<EmailAddress> list = findByUuid_C(uuid, companyId, 0, 1,
844                                    orderByComparator);
845    
846                    if (!list.isEmpty()) {
847                            return list.get(0);
848                    }
849    
850                    return null;
851            }
852    
853            /**
854             * Returns the last email address in the ordered set where uuid = &#63; and companyId = &#63;.
855             *
856             * @param uuid the uuid
857             * @param companyId the company ID
858             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
859             * @return the last matching email address
860             * @throws NoSuchEmailAddressException if a matching email address could not be found
861             */
862            @Override
863            public EmailAddress findByUuid_C_Last(String uuid, long companyId,
864                    OrderByComparator<EmailAddress> orderByComparator)
865                    throws NoSuchEmailAddressException {
866                    EmailAddress emailAddress = fetchByUuid_C_Last(uuid, companyId,
867                                    orderByComparator);
868    
869                    if (emailAddress != null) {
870                            return emailAddress;
871                    }
872    
873                    StringBundler msg = new StringBundler(6);
874    
875                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
876    
877                    msg.append("uuid=");
878                    msg.append(uuid);
879    
880                    msg.append(", companyId=");
881                    msg.append(companyId);
882    
883                    msg.append(StringPool.CLOSE_CURLY_BRACE);
884    
885                    throw new NoSuchEmailAddressException(msg.toString());
886            }
887    
888            /**
889             * Returns the last email address in the ordered set where uuid = &#63; and companyId = &#63;.
890             *
891             * @param uuid the uuid
892             * @param companyId the company ID
893             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
894             * @return the last matching email address, or <code>null</code> if a matching email address could not be found
895             */
896            @Override
897            public EmailAddress fetchByUuid_C_Last(String uuid, long companyId,
898                    OrderByComparator<EmailAddress> orderByComparator) {
899                    int count = countByUuid_C(uuid, companyId);
900    
901                    if (count == 0) {
902                            return null;
903                    }
904    
905                    List<EmailAddress> list = findByUuid_C(uuid, companyId, count - 1,
906                                    count, orderByComparator);
907    
908                    if (!list.isEmpty()) {
909                            return list.get(0);
910                    }
911    
912                    return null;
913            }
914    
915            /**
916             * Returns the email addresses before and after the current email address in the ordered set where uuid = &#63; and companyId = &#63;.
917             *
918             * @param emailAddressId the primary key of the current email address
919             * @param uuid the uuid
920             * @param companyId the company ID
921             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
922             * @return the previous, current, and next email address
923             * @throws NoSuchEmailAddressException if a email address with the primary key could not be found
924             */
925            @Override
926            public EmailAddress[] findByUuid_C_PrevAndNext(long emailAddressId,
927                    String uuid, long companyId,
928                    OrderByComparator<EmailAddress> orderByComparator)
929                    throws NoSuchEmailAddressException {
930                    EmailAddress emailAddress = findByPrimaryKey(emailAddressId);
931    
932                    Session session = null;
933    
934                    try {
935                            session = openSession();
936    
937                            EmailAddress[] array = new EmailAddressImpl[3];
938    
939                            array[0] = getByUuid_C_PrevAndNext(session, emailAddress, uuid,
940                                            companyId, orderByComparator, true);
941    
942                            array[1] = emailAddress;
943    
944                            array[2] = getByUuid_C_PrevAndNext(session, emailAddress, uuid,
945                                            companyId, orderByComparator, false);
946    
947                            return array;
948                    }
949                    catch (Exception e) {
950                            throw processException(e);
951                    }
952                    finally {
953                            closeSession(session);
954                    }
955            }
956    
957            protected EmailAddress getByUuid_C_PrevAndNext(Session session,
958                    EmailAddress emailAddress, String uuid, long companyId,
959                    OrderByComparator<EmailAddress> orderByComparator, boolean previous) {
960                    StringBundler query = null;
961    
962                    if (orderByComparator != null) {
963                            query = new StringBundler(6 +
964                                            (orderByComparator.getOrderByFields().length * 6));
965                    }
966                    else {
967                            query = new StringBundler(3);
968                    }
969    
970                    query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
971    
972                    boolean bindUuid = false;
973    
974                    if (uuid == null) {
975                            query.append(_FINDER_COLUMN_UUID_C_UUID_1);
976                    }
977                    else if (uuid.equals(StringPool.BLANK)) {
978                            query.append(_FINDER_COLUMN_UUID_C_UUID_3);
979                    }
980                    else {
981                            bindUuid = true;
982    
983                            query.append(_FINDER_COLUMN_UUID_C_UUID_2);
984                    }
985    
986                    query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
987    
988                    if (orderByComparator != null) {
989                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
990    
991                            if (orderByConditionFields.length > 0) {
992                                    query.append(WHERE_AND);
993                            }
994    
995                            for (int i = 0; i < orderByConditionFields.length; i++) {
996                                    query.append(_ORDER_BY_ENTITY_ALIAS);
997                                    query.append(orderByConditionFields[i]);
998    
999                                    if ((i + 1) < orderByConditionFields.length) {
1000                                            if (orderByComparator.isAscending() ^ previous) {
1001                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1002                                            }
1003                                            else {
1004                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1005                                            }
1006                                    }
1007                                    else {
1008                                            if (orderByComparator.isAscending() ^ previous) {
1009                                                    query.append(WHERE_GREATER_THAN);
1010                                            }
1011                                            else {
1012                                                    query.append(WHERE_LESSER_THAN);
1013                                            }
1014                                    }
1015                            }
1016    
1017                            query.append(ORDER_BY_CLAUSE);
1018    
1019                            String[] orderByFields = orderByComparator.getOrderByFields();
1020    
1021                            for (int i = 0; i < orderByFields.length; i++) {
1022                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1023                                    query.append(orderByFields[i]);
1024    
1025                                    if ((i + 1) < orderByFields.length) {
1026                                            if (orderByComparator.isAscending() ^ previous) {
1027                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1028                                            }
1029                                            else {
1030                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1031                                            }
1032                                    }
1033                                    else {
1034                                            if (orderByComparator.isAscending() ^ previous) {
1035                                                    query.append(ORDER_BY_ASC);
1036                                            }
1037                                            else {
1038                                                    query.append(ORDER_BY_DESC);
1039                                            }
1040                                    }
1041                            }
1042                    }
1043                    else {
1044                            query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
1045                    }
1046    
1047                    String sql = query.toString();
1048    
1049                    Query q = session.createQuery(sql);
1050    
1051                    q.setFirstResult(0);
1052                    q.setMaxResults(2);
1053    
1054                    QueryPos qPos = QueryPos.getInstance(q);
1055    
1056                    if (bindUuid) {
1057                            qPos.add(uuid);
1058                    }
1059    
1060                    qPos.add(companyId);
1061    
1062                    if (orderByComparator != null) {
1063                            Object[] values = orderByComparator.getOrderByConditionValues(emailAddress);
1064    
1065                            for (Object value : values) {
1066                                    qPos.add(value);
1067                            }
1068                    }
1069    
1070                    List<EmailAddress> list = q.list();
1071    
1072                    if (list.size() == 2) {
1073                            return list.get(1);
1074                    }
1075                    else {
1076                            return null;
1077                    }
1078            }
1079    
1080            /**
1081             * Removes all the email addresses where uuid = &#63; and companyId = &#63; from the database.
1082             *
1083             * @param uuid the uuid
1084             * @param companyId the company ID
1085             */
1086            @Override
1087            public void removeByUuid_C(String uuid, long companyId) {
1088                    for (EmailAddress emailAddress : findByUuid_C(uuid, companyId,
1089                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1090                            remove(emailAddress);
1091                    }
1092            }
1093    
1094            /**
1095             * Returns the number of email addresses where uuid = &#63; and companyId = &#63;.
1096             *
1097             * @param uuid the uuid
1098             * @param companyId the company ID
1099             * @return the number of matching email addresses
1100             */
1101            @Override
1102            public int countByUuid_C(String uuid, long companyId) {
1103                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID_C;
1104    
1105                    Object[] finderArgs = new Object[] { uuid, companyId };
1106    
1107                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1108                                    this);
1109    
1110                    if (count == null) {
1111                            StringBundler query = new StringBundler(3);
1112    
1113                            query.append(_SQL_COUNT_EMAILADDRESS_WHERE);
1114    
1115                            boolean bindUuid = false;
1116    
1117                            if (uuid == null) {
1118                                    query.append(_FINDER_COLUMN_UUID_C_UUID_1);
1119                            }
1120                            else if (uuid.equals(StringPool.BLANK)) {
1121                                    query.append(_FINDER_COLUMN_UUID_C_UUID_3);
1122                            }
1123                            else {
1124                                    bindUuid = true;
1125    
1126                                    query.append(_FINDER_COLUMN_UUID_C_UUID_2);
1127                            }
1128    
1129                            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
1130    
1131                            String sql = query.toString();
1132    
1133                            Session session = null;
1134    
1135                            try {
1136                                    session = openSession();
1137    
1138                                    Query q = session.createQuery(sql);
1139    
1140                                    QueryPos qPos = QueryPos.getInstance(q);
1141    
1142                                    if (bindUuid) {
1143                                            qPos.add(uuid);
1144                                    }
1145    
1146                                    qPos.add(companyId);
1147    
1148                                    count = (Long)q.uniqueResult();
1149    
1150                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1151                            }
1152                            catch (Exception e) {
1153                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1154    
1155                                    throw processException(e);
1156                            }
1157                            finally {
1158                                    closeSession(session);
1159                            }
1160                    }
1161    
1162                    return count.intValue();
1163            }
1164    
1165            private static final String _FINDER_COLUMN_UUID_C_UUID_1 = "emailAddress.uuid IS NULL AND ";
1166            private static final String _FINDER_COLUMN_UUID_C_UUID_2 = "emailAddress.uuid = ? AND ";
1167            private static final String _FINDER_COLUMN_UUID_C_UUID_3 = "(emailAddress.uuid IS NULL OR emailAddress.uuid = '') AND ";
1168            private static final String _FINDER_COLUMN_UUID_C_COMPANYID_2 = "emailAddress.companyId = ?";
1169            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID =
1170                    new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
1171                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
1172                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByCompanyId",
1173                            new String[] {
1174                                    Long.class.getName(),
1175                                    
1176                            Integer.class.getName(), Integer.class.getName(),
1177                                    OrderByComparator.class.getName()
1178                            });
1179            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID =
1180                    new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
1181                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
1182                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByCompanyId",
1183                            new String[] { Long.class.getName() },
1184                            EmailAddressModelImpl.COMPANYID_COLUMN_BITMASK |
1185                            EmailAddressModelImpl.CREATEDATE_COLUMN_BITMASK);
1186            public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
1187                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
1188                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCompanyId",
1189                            new String[] { Long.class.getName() });
1190    
1191            /**
1192             * Returns all the email addresses where companyId = &#63;.
1193             *
1194             * @param companyId the company ID
1195             * @return the matching email addresses
1196             */
1197            @Override
1198            public List<EmailAddress> findByCompanyId(long companyId) {
1199                    return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
1200                            null);
1201            }
1202    
1203            /**
1204             * Returns a range of all the email addresses where companyId = &#63;.
1205             *
1206             * <p>
1207             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1208             * </p>
1209             *
1210             * @param companyId the company ID
1211             * @param start the lower bound of the range of email addresses
1212             * @param end the upper bound of the range of email addresses (not inclusive)
1213             * @return the range of matching email addresses
1214             */
1215            @Override
1216            public List<EmailAddress> findByCompanyId(long companyId, int start, int end) {
1217                    return findByCompanyId(companyId, start, end, null);
1218            }
1219    
1220            /**
1221             * Returns an ordered range of all the email addresses where companyId = &#63;.
1222             *
1223             * <p>
1224             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1225             * </p>
1226             *
1227             * @param companyId the company ID
1228             * @param start the lower bound of the range of email addresses
1229             * @param end the upper bound of the range of email addresses (not inclusive)
1230             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1231             * @return the ordered range of matching email addresses
1232             */
1233            @Override
1234            public List<EmailAddress> findByCompanyId(long companyId, int start,
1235                    int end, OrderByComparator<EmailAddress> orderByComparator) {
1236                    boolean pagination = true;
1237                    FinderPath finderPath = null;
1238                    Object[] finderArgs = null;
1239    
1240                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1241                                    (orderByComparator == null)) {
1242                            pagination = false;
1243                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID;
1244                            finderArgs = new Object[] { companyId };
1245                    }
1246                    else {
1247                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID;
1248                            finderArgs = new Object[] { companyId, start, end, orderByComparator };
1249                    }
1250    
1251                    List<EmailAddress> list = (List<EmailAddress>)FinderCacheUtil.getResult(finderPath,
1252                                    finderArgs, this);
1253    
1254                    if ((list != null) && !list.isEmpty()) {
1255                            for (EmailAddress emailAddress : list) {
1256                                    if ((companyId != emailAddress.getCompanyId())) {
1257                                            list = null;
1258    
1259                                            break;
1260                                    }
1261                            }
1262                    }
1263    
1264                    if (list == null) {
1265                            StringBundler query = null;
1266    
1267                            if (orderByComparator != null) {
1268                                    query = new StringBundler(3 +
1269                                                    (orderByComparator.getOrderByFields().length * 3));
1270                            }
1271                            else {
1272                                    query = new StringBundler(3);
1273                            }
1274    
1275                            query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
1276    
1277                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1278    
1279                            if (orderByComparator != null) {
1280                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1281                                            orderByComparator);
1282                            }
1283                            else
1284                             if (pagination) {
1285                                    query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
1286                            }
1287    
1288                            String sql = query.toString();
1289    
1290                            Session session = null;
1291    
1292                            try {
1293                                    session = openSession();
1294    
1295                                    Query q = session.createQuery(sql);
1296    
1297                                    QueryPos qPos = QueryPos.getInstance(q);
1298    
1299                                    qPos.add(companyId);
1300    
1301                                    if (!pagination) {
1302                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
1303                                                            start, end, false);
1304    
1305                                            Collections.sort(list);
1306    
1307                                            list = Collections.unmodifiableList(list);
1308                                    }
1309                                    else {
1310                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
1311                                                            start, end);
1312                                    }
1313    
1314                                    cacheResult(list);
1315    
1316                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1317                            }
1318                            catch (Exception e) {
1319                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1320    
1321                                    throw processException(e);
1322                            }
1323                            finally {
1324                                    closeSession(session);
1325                            }
1326                    }
1327    
1328                    return list;
1329            }
1330    
1331            /**
1332             * Returns the first email address in the ordered set where companyId = &#63;.
1333             *
1334             * @param companyId the company ID
1335             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1336             * @return the first matching email address
1337             * @throws NoSuchEmailAddressException if a matching email address could not be found
1338             */
1339            @Override
1340            public EmailAddress findByCompanyId_First(long companyId,
1341                    OrderByComparator<EmailAddress> orderByComparator)
1342                    throws NoSuchEmailAddressException {
1343                    EmailAddress emailAddress = fetchByCompanyId_First(companyId,
1344                                    orderByComparator);
1345    
1346                    if (emailAddress != null) {
1347                            return emailAddress;
1348                    }
1349    
1350                    StringBundler msg = new StringBundler(4);
1351    
1352                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1353    
1354                    msg.append("companyId=");
1355                    msg.append(companyId);
1356    
1357                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1358    
1359                    throw new NoSuchEmailAddressException(msg.toString());
1360            }
1361    
1362            /**
1363             * Returns the first email address in the ordered set where companyId = &#63;.
1364             *
1365             * @param companyId the company ID
1366             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1367             * @return the first matching email address, or <code>null</code> if a matching email address could not be found
1368             */
1369            @Override
1370            public EmailAddress fetchByCompanyId_First(long companyId,
1371                    OrderByComparator<EmailAddress> orderByComparator) {
1372                    List<EmailAddress> list = findByCompanyId(companyId, 0, 1,
1373                                    orderByComparator);
1374    
1375                    if (!list.isEmpty()) {
1376                            return list.get(0);
1377                    }
1378    
1379                    return null;
1380            }
1381    
1382            /**
1383             * Returns the last email address in the ordered set where companyId = &#63;.
1384             *
1385             * @param companyId the company ID
1386             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1387             * @return the last matching email address
1388             * @throws NoSuchEmailAddressException if a matching email address could not be found
1389             */
1390            @Override
1391            public EmailAddress findByCompanyId_Last(long companyId,
1392                    OrderByComparator<EmailAddress> orderByComparator)
1393                    throws NoSuchEmailAddressException {
1394                    EmailAddress emailAddress = fetchByCompanyId_Last(companyId,
1395                                    orderByComparator);
1396    
1397                    if (emailAddress != null) {
1398                            return emailAddress;
1399                    }
1400    
1401                    StringBundler msg = new StringBundler(4);
1402    
1403                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1404    
1405                    msg.append("companyId=");
1406                    msg.append(companyId);
1407    
1408                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1409    
1410                    throw new NoSuchEmailAddressException(msg.toString());
1411            }
1412    
1413            /**
1414             * Returns the last email address in the ordered set where companyId = &#63;.
1415             *
1416             * @param companyId the company ID
1417             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1418             * @return the last matching email address, or <code>null</code> if a matching email address could not be found
1419             */
1420            @Override
1421            public EmailAddress fetchByCompanyId_Last(long companyId,
1422                    OrderByComparator<EmailAddress> orderByComparator) {
1423                    int count = countByCompanyId(companyId);
1424    
1425                    if (count == 0) {
1426                            return null;
1427                    }
1428    
1429                    List<EmailAddress> list = findByCompanyId(companyId, count - 1, count,
1430                                    orderByComparator);
1431    
1432                    if (!list.isEmpty()) {
1433                            return list.get(0);
1434                    }
1435    
1436                    return null;
1437            }
1438    
1439            /**
1440             * Returns the email addresses before and after the current email address in the ordered set where companyId = &#63;.
1441             *
1442             * @param emailAddressId the primary key of the current email address
1443             * @param companyId the company ID
1444             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1445             * @return the previous, current, and next email address
1446             * @throws NoSuchEmailAddressException if a email address with the primary key could not be found
1447             */
1448            @Override
1449            public EmailAddress[] findByCompanyId_PrevAndNext(long emailAddressId,
1450                    long companyId, OrderByComparator<EmailAddress> orderByComparator)
1451                    throws NoSuchEmailAddressException {
1452                    EmailAddress emailAddress = findByPrimaryKey(emailAddressId);
1453    
1454                    Session session = null;
1455    
1456                    try {
1457                            session = openSession();
1458    
1459                            EmailAddress[] array = new EmailAddressImpl[3];
1460    
1461                            array[0] = getByCompanyId_PrevAndNext(session, emailAddress,
1462                                            companyId, orderByComparator, true);
1463    
1464                            array[1] = emailAddress;
1465    
1466                            array[2] = getByCompanyId_PrevAndNext(session, emailAddress,
1467                                            companyId, orderByComparator, false);
1468    
1469                            return array;
1470                    }
1471                    catch (Exception e) {
1472                            throw processException(e);
1473                    }
1474                    finally {
1475                            closeSession(session);
1476                    }
1477            }
1478    
1479            protected EmailAddress getByCompanyId_PrevAndNext(Session session,
1480                    EmailAddress emailAddress, long companyId,
1481                    OrderByComparator<EmailAddress> orderByComparator, boolean previous) {
1482                    StringBundler query = null;
1483    
1484                    if (orderByComparator != null) {
1485                            query = new StringBundler(6 +
1486                                            (orderByComparator.getOrderByFields().length * 6));
1487                    }
1488                    else {
1489                            query = new StringBundler(3);
1490                    }
1491    
1492                    query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
1493    
1494                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1495    
1496                    if (orderByComparator != null) {
1497                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1498    
1499                            if (orderByConditionFields.length > 0) {
1500                                    query.append(WHERE_AND);
1501                            }
1502    
1503                            for (int i = 0; i < orderByConditionFields.length; i++) {
1504                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1505                                    query.append(orderByConditionFields[i]);
1506    
1507                                    if ((i + 1) < orderByConditionFields.length) {
1508                                            if (orderByComparator.isAscending() ^ previous) {
1509                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1510                                            }
1511                                            else {
1512                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1513                                            }
1514                                    }
1515                                    else {
1516                                            if (orderByComparator.isAscending() ^ previous) {
1517                                                    query.append(WHERE_GREATER_THAN);
1518                                            }
1519                                            else {
1520                                                    query.append(WHERE_LESSER_THAN);
1521                                            }
1522                                    }
1523                            }
1524    
1525                            query.append(ORDER_BY_CLAUSE);
1526    
1527                            String[] orderByFields = orderByComparator.getOrderByFields();
1528    
1529                            for (int i = 0; i < orderByFields.length; i++) {
1530                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1531                                    query.append(orderByFields[i]);
1532    
1533                                    if ((i + 1) < orderByFields.length) {
1534                                            if (orderByComparator.isAscending() ^ previous) {
1535                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1536                                            }
1537                                            else {
1538                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1539                                            }
1540                                    }
1541                                    else {
1542                                            if (orderByComparator.isAscending() ^ previous) {
1543                                                    query.append(ORDER_BY_ASC);
1544                                            }
1545                                            else {
1546                                                    query.append(ORDER_BY_DESC);
1547                                            }
1548                                    }
1549                            }
1550                    }
1551                    else {
1552                            query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
1553                    }
1554    
1555                    String sql = query.toString();
1556    
1557                    Query q = session.createQuery(sql);
1558    
1559                    q.setFirstResult(0);
1560                    q.setMaxResults(2);
1561    
1562                    QueryPos qPos = QueryPos.getInstance(q);
1563    
1564                    qPos.add(companyId);
1565    
1566                    if (orderByComparator != null) {
1567                            Object[] values = orderByComparator.getOrderByConditionValues(emailAddress);
1568    
1569                            for (Object value : values) {
1570                                    qPos.add(value);
1571                            }
1572                    }
1573    
1574                    List<EmailAddress> list = q.list();
1575    
1576                    if (list.size() == 2) {
1577                            return list.get(1);
1578                    }
1579                    else {
1580                            return null;
1581                    }
1582            }
1583    
1584            /**
1585             * Removes all the email addresses where companyId = &#63; from the database.
1586             *
1587             * @param companyId the company ID
1588             */
1589            @Override
1590            public void removeByCompanyId(long companyId) {
1591                    for (EmailAddress emailAddress : findByCompanyId(companyId,
1592                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1593                            remove(emailAddress);
1594                    }
1595            }
1596    
1597            /**
1598             * Returns the number of email addresses where companyId = &#63;.
1599             *
1600             * @param companyId the company ID
1601             * @return the number of matching email addresses
1602             */
1603            @Override
1604            public int countByCompanyId(long companyId) {
1605                    FinderPath finderPath = FINDER_PATH_COUNT_BY_COMPANYID;
1606    
1607                    Object[] finderArgs = new Object[] { companyId };
1608    
1609                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1610                                    this);
1611    
1612                    if (count == null) {
1613                            StringBundler query = new StringBundler(2);
1614    
1615                            query.append(_SQL_COUNT_EMAILADDRESS_WHERE);
1616    
1617                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1618    
1619                            String sql = query.toString();
1620    
1621                            Session session = null;
1622    
1623                            try {
1624                                    session = openSession();
1625    
1626                                    Query q = session.createQuery(sql);
1627    
1628                                    QueryPos qPos = QueryPos.getInstance(q);
1629    
1630                                    qPos.add(companyId);
1631    
1632                                    count = (Long)q.uniqueResult();
1633    
1634                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1635                            }
1636                            catch (Exception e) {
1637                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1638    
1639                                    throw processException(e);
1640                            }
1641                            finally {
1642                                    closeSession(session);
1643                            }
1644                    }
1645    
1646                    return count.intValue();
1647            }
1648    
1649            private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "emailAddress.companyId = ?";
1650            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
1651                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
1652                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUserId",
1653                            new String[] {
1654                                    Long.class.getName(),
1655                                    
1656                            Integer.class.getName(), Integer.class.getName(),
1657                                    OrderByComparator.class.getName()
1658                            });
1659            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID =
1660                    new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
1661                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
1662                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUserId",
1663                            new String[] { Long.class.getName() },
1664                            EmailAddressModelImpl.USERID_COLUMN_BITMASK |
1665                            EmailAddressModelImpl.CREATEDATE_COLUMN_BITMASK);
1666            public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
1667                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
1668                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUserId",
1669                            new String[] { Long.class.getName() });
1670    
1671            /**
1672             * Returns all the email addresses where userId = &#63;.
1673             *
1674             * @param userId the user ID
1675             * @return the matching email addresses
1676             */
1677            @Override
1678            public List<EmailAddress> findByUserId(long userId) {
1679                    return findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1680            }
1681    
1682            /**
1683             * Returns a range of all the email addresses where userId = &#63;.
1684             *
1685             * <p>
1686             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1687             * </p>
1688             *
1689             * @param userId the user ID
1690             * @param start the lower bound of the range of email addresses
1691             * @param end the upper bound of the range of email addresses (not inclusive)
1692             * @return the range of matching email addresses
1693             */
1694            @Override
1695            public List<EmailAddress> findByUserId(long userId, int start, int end) {
1696                    return findByUserId(userId, start, end, null);
1697            }
1698    
1699            /**
1700             * Returns an ordered range of all the email addresses where userId = &#63;.
1701             *
1702             * <p>
1703             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1704             * </p>
1705             *
1706             * @param userId the user ID
1707             * @param start the lower bound of the range of email addresses
1708             * @param end the upper bound of the range of email addresses (not inclusive)
1709             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1710             * @return the ordered range of matching email addresses
1711             */
1712            @Override
1713            public List<EmailAddress> findByUserId(long userId, int start, int end,
1714                    OrderByComparator<EmailAddress> orderByComparator) {
1715                    boolean pagination = true;
1716                    FinderPath finderPath = null;
1717                    Object[] finderArgs = null;
1718    
1719                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1720                                    (orderByComparator == null)) {
1721                            pagination = false;
1722                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID;
1723                            finderArgs = new Object[] { userId };
1724                    }
1725                    else {
1726                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID;
1727                            finderArgs = new Object[] { userId, start, end, orderByComparator };
1728                    }
1729    
1730                    List<EmailAddress> list = (List<EmailAddress>)FinderCacheUtil.getResult(finderPath,
1731                                    finderArgs, this);
1732    
1733                    if ((list != null) && !list.isEmpty()) {
1734                            for (EmailAddress emailAddress : list) {
1735                                    if ((userId != emailAddress.getUserId())) {
1736                                            list = null;
1737    
1738                                            break;
1739                                    }
1740                            }
1741                    }
1742    
1743                    if (list == null) {
1744                            StringBundler query = null;
1745    
1746                            if (orderByComparator != null) {
1747                                    query = new StringBundler(3 +
1748                                                    (orderByComparator.getOrderByFields().length * 3));
1749                            }
1750                            else {
1751                                    query = new StringBundler(3);
1752                            }
1753    
1754                            query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
1755    
1756                            query.append(_FINDER_COLUMN_USERID_USERID_2);
1757    
1758                            if (orderByComparator != null) {
1759                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1760                                            orderByComparator);
1761                            }
1762                            else
1763                             if (pagination) {
1764                                    query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
1765                            }
1766    
1767                            String sql = query.toString();
1768    
1769                            Session session = null;
1770    
1771                            try {
1772                                    session = openSession();
1773    
1774                                    Query q = session.createQuery(sql);
1775    
1776                                    QueryPos qPos = QueryPos.getInstance(q);
1777    
1778                                    qPos.add(userId);
1779    
1780                                    if (!pagination) {
1781                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
1782                                                            start, end, false);
1783    
1784                                            Collections.sort(list);
1785    
1786                                            list = Collections.unmodifiableList(list);
1787                                    }
1788                                    else {
1789                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
1790                                                            start, end);
1791                                    }
1792    
1793                                    cacheResult(list);
1794    
1795                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1796                            }
1797                            catch (Exception e) {
1798                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1799    
1800                                    throw processException(e);
1801                            }
1802                            finally {
1803                                    closeSession(session);
1804                            }
1805                    }
1806    
1807                    return list;
1808            }
1809    
1810            /**
1811             * Returns the first email address in the ordered set where userId = &#63;.
1812             *
1813             * @param userId the user ID
1814             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1815             * @return the first matching email address
1816             * @throws NoSuchEmailAddressException if a matching email address could not be found
1817             */
1818            @Override
1819            public EmailAddress findByUserId_First(long userId,
1820                    OrderByComparator<EmailAddress> orderByComparator)
1821                    throws NoSuchEmailAddressException {
1822                    EmailAddress emailAddress = fetchByUserId_First(userId,
1823                                    orderByComparator);
1824    
1825                    if (emailAddress != null) {
1826                            return emailAddress;
1827                    }
1828    
1829                    StringBundler msg = new StringBundler(4);
1830    
1831                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1832    
1833                    msg.append("userId=");
1834                    msg.append(userId);
1835    
1836                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1837    
1838                    throw new NoSuchEmailAddressException(msg.toString());
1839            }
1840    
1841            /**
1842             * Returns the first email address in the ordered set where userId = &#63;.
1843             *
1844             * @param userId the user ID
1845             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1846             * @return the first matching email address, or <code>null</code> if a matching email address could not be found
1847             */
1848            @Override
1849            public EmailAddress fetchByUserId_First(long userId,
1850                    OrderByComparator<EmailAddress> orderByComparator) {
1851                    List<EmailAddress> list = findByUserId(userId, 0, 1, orderByComparator);
1852    
1853                    if (!list.isEmpty()) {
1854                            return list.get(0);
1855                    }
1856    
1857                    return null;
1858            }
1859    
1860            /**
1861             * Returns the last email address in the ordered set where userId = &#63;.
1862             *
1863             * @param userId the user ID
1864             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1865             * @return the last matching email address
1866             * @throws NoSuchEmailAddressException if a matching email address could not be found
1867             */
1868            @Override
1869            public EmailAddress findByUserId_Last(long userId,
1870                    OrderByComparator<EmailAddress> orderByComparator)
1871                    throws NoSuchEmailAddressException {
1872                    EmailAddress emailAddress = fetchByUserId_Last(userId, orderByComparator);
1873    
1874                    if (emailAddress != null) {
1875                            return emailAddress;
1876                    }
1877    
1878                    StringBundler msg = new StringBundler(4);
1879    
1880                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1881    
1882                    msg.append("userId=");
1883                    msg.append(userId);
1884    
1885                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1886    
1887                    throw new NoSuchEmailAddressException(msg.toString());
1888            }
1889    
1890            /**
1891             * Returns the last email address in the ordered set where userId = &#63;.
1892             *
1893             * @param userId the user ID
1894             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1895             * @return the last matching email address, or <code>null</code> if a matching email address could not be found
1896             */
1897            @Override
1898            public EmailAddress fetchByUserId_Last(long userId,
1899                    OrderByComparator<EmailAddress> orderByComparator) {
1900                    int count = countByUserId(userId);
1901    
1902                    if (count == 0) {
1903                            return null;
1904                    }
1905    
1906                    List<EmailAddress> list = findByUserId(userId, count - 1, count,
1907                                    orderByComparator);
1908    
1909                    if (!list.isEmpty()) {
1910                            return list.get(0);
1911                    }
1912    
1913                    return null;
1914            }
1915    
1916            /**
1917             * Returns the email addresses before and after the current email address in the ordered set where userId = &#63;.
1918             *
1919             * @param emailAddressId the primary key of the current email address
1920             * @param userId the user ID
1921             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1922             * @return the previous, current, and next email address
1923             * @throws NoSuchEmailAddressException if a email address with the primary key could not be found
1924             */
1925            @Override
1926            public EmailAddress[] findByUserId_PrevAndNext(long emailAddressId,
1927                    long userId, OrderByComparator<EmailAddress> orderByComparator)
1928                    throws NoSuchEmailAddressException {
1929                    EmailAddress emailAddress = findByPrimaryKey(emailAddressId);
1930    
1931                    Session session = null;
1932    
1933                    try {
1934                            session = openSession();
1935    
1936                            EmailAddress[] array = new EmailAddressImpl[3];
1937    
1938                            array[0] = getByUserId_PrevAndNext(session, emailAddress, userId,
1939                                            orderByComparator, true);
1940    
1941                            array[1] = emailAddress;
1942    
1943                            array[2] = getByUserId_PrevAndNext(session, emailAddress, userId,
1944                                            orderByComparator, false);
1945    
1946                            return array;
1947                    }
1948                    catch (Exception e) {
1949                            throw processException(e);
1950                    }
1951                    finally {
1952                            closeSession(session);
1953                    }
1954            }
1955    
1956            protected EmailAddress getByUserId_PrevAndNext(Session session,
1957                    EmailAddress emailAddress, long userId,
1958                    OrderByComparator<EmailAddress> orderByComparator, boolean previous) {
1959                    StringBundler query = null;
1960    
1961                    if (orderByComparator != null) {
1962                            query = new StringBundler(6 +
1963                                            (orderByComparator.getOrderByFields().length * 6));
1964                    }
1965                    else {
1966                            query = new StringBundler(3);
1967                    }
1968    
1969                    query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
1970    
1971                    query.append(_FINDER_COLUMN_USERID_USERID_2);
1972    
1973                    if (orderByComparator != null) {
1974                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1975    
1976                            if (orderByConditionFields.length > 0) {
1977                                    query.append(WHERE_AND);
1978                            }
1979    
1980                            for (int i = 0; i < orderByConditionFields.length; i++) {
1981                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1982                                    query.append(orderByConditionFields[i]);
1983    
1984                                    if ((i + 1) < orderByConditionFields.length) {
1985                                            if (orderByComparator.isAscending() ^ previous) {
1986                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1987                                            }
1988                                            else {
1989                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1990                                            }
1991                                    }
1992                                    else {
1993                                            if (orderByComparator.isAscending() ^ previous) {
1994                                                    query.append(WHERE_GREATER_THAN);
1995                                            }
1996                                            else {
1997                                                    query.append(WHERE_LESSER_THAN);
1998                                            }
1999                                    }
2000                            }
2001    
2002                            query.append(ORDER_BY_CLAUSE);
2003    
2004                            String[] orderByFields = orderByComparator.getOrderByFields();
2005    
2006                            for (int i = 0; i < orderByFields.length; i++) {
2007                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2008                                    query.append(orderByFields[i]);
2009    
2010                                    if ((i + 1) < orderByFields.length) {
2011                                            if (orderByComparator.isAscending() ^ previous) {
2012                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2013                                            }
2014                                            else {
2015                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2016                                            }
2017                                    }
2018                                    else {
2019                                            if (orderByComparator.isAscending() ^ previous) {
2020                                                    query.append(ORDER_BY_ASC);
2021                                            }
2022                                            else {
2023                                                    query.append(ORDER_BY_DESC);
2024                                            }
2025                                    }
2026                            }
2027                    }
2028                    else {
2029                            query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
2030                    }
2031    
2032                    String sql = query.toString();
2033    
2034                    Query q = session.createQuery(sql);
2035    
2036                    q.setFirstResult(0);
2037                    q.setMaxResults(2);
2038    
2039                    QueryPos qPos = QueryPos.getInstance(q);
2040    
2041                    qPos.add(userId);
2042    
2043                    if (orderByComparator != null) {
2044                            Object[] values = orderByComparator.getOrderByConditionValues(emailAddress);
2045    
2046                            for (Object value : values) {
2047                                    qPos.add(value);
2048                            }
2049                    }
2050    
2051                    List<EmailAddress> list = q.list();
2052    
2053                    if (list.size() == 2) {
2054                            return list.get(1);
2055                    }
2056                    else {
2057                            return null;
2058                    }
2059            }
2060    
2061            /**
2062             * Removes all the email addresses where userId = &#63; from the database.
2063             *
2064             * @param userId the user ID
2065             */
2066            @Override
2067            public void removeByUserId(long userId) {
2068                    for (EmailAddress emailAddress : findByUserId(userId,
2069                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
2070                            remove(emailAddress);
2071                    }
2072            }
2073    
2074            /**
2075             * Returns the number of email addresses where userId = &#63;.
2076             *
2077             * @param userId the user ID
2078             * @return the number of matching email addresses
2079             */
2080            @Override
2081            public int countByUserId(long userId) {
2082                    FinderPath finderPath = FINDER_PATH_COUNT_BY_USERID;
2083    
2084                    Object[] finderArgs = new Object[] { userId };
2085    
2086                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
2087                                    this);
2088    
2089                    if (count == null) {
2090                            StringBundler query = new StringBundler(2);
2091    
2092                            query.append(_SQL_COUNT_EMAILADDRESS_WHERE);
2093    
2094                            query.append(_FINDER_COLUMN_USERID_USERID_2);
2095    
2096                            String sql = query.toString();
2097    
2098                            Session session = null;
2099    
2100                            try {
2101                                    session = openSession();
2102    
2103                                    Query q = session.createQuery(sql);
2104    
2105                                    QueryPos qPos = QueryPos.getInstance(q);
2106    
2107                                    qPos.add(userId);
2108    
2109                                    count = (Long)q.uniqueResult();
2110    
2111                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2112                            }
2113                            catch (Exception e) {
2114                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2115    
2116                                    throw processException(e);
2117                            }
2118                            finally {
2119                                    closeSession(session);
2120                            }
2121                    }
2122    
2123                    return count.intValue();
2124            }
2125    
2126            private static final String _FINDER_COLUMN_USERID_USERID_2 = "emailAddress.userId = ?";
2127            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
2128                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
2129                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C",
2130                            new String[] {
2131                                    Long.class.getName(), Long.class.getName(),
2132                                    
2133                            Integer.class.getName(), Integer.class.getName(),
2134                                    OrderByComparator.class.getName()
2135                            });
2136            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
2137                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
2138                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C",
2139                            new String[] { Long.class.getName(), Long.class.getName() },
2140                            EmailAddressModelImpl.COMPANYID_COLUMN_BITMASK |
2141                            EmailAddressModelImpl.CLASSNAMEID_COLUMN_BITMASK |
2142                            EmailAddressModelImpl.CREATEDATE_COLUMN_BITMASK);
2143            public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
2144                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
2145                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C",
2146                            new String[] { Long.class.getName(), Long.class.getName() });
2147    
2148            /**
2149             * Returns all the email addresses where companyId = &#63; and classNameId = &#63;.
2150             *
2151             * @param companyId the company ID
2152             * @param classNameId the class name ID
2153             * @return the matching email addresses
2154             */
2155            @Override
2156            public List<EmailAddress> findByC_C(long companyId, long classNameId) {
2157                    return findByC_C(companyId, classNameId, QueryUtil.ALL_POS,
2158                            QueryUtil.ALL_POS, null);
2159            }
2160    
2161            /**
2162             * Returns a range of all the email addresses where companyId = &#63; and classNameId = &#63;.
2163             *
2164             * <p>
2165             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
2166             * </p>
2167             *
2168             * @param companyId the company ID
2169             * @param classNameId the class name ID
2170             * @param start the lower bound of the range of email addresses
2171             * @param end the upper bound of the range of email addresses (not inclusive)
2172             * @return the range of matching email addresses
2173             */
2174            @Override
2175            public List<EmailAddress> findByC_C(long companyId, long classNameId,
2176                    int start, int end) {
2177                    return findByC_C(companyId, classNameId, start, end, null);
2178            }
2179    
2180            /**
2181             * Returns an ordered range of all the email addresses where companyId = &#63; and classNameId = &#63;.
2182             *
2183             * <p>
2184             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
2185             * </p>
2186             *
2187             * @param companyId the company ID
2188             * @param classNameId the class name ID
2189             * @param start the lower bound of the range of email addresses
2190             * @param end the upper bound of the range of email addresses (not inclusive)
2191             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2192             * @return the ordered range of matching email addresses
2193             */
2194            @Override
2195            public List<EmailAddress> findByC_C(long companyId, long classNameId,
2196                    int start, int end, OrderByComparator<EmailAddress> orderByComparator) {
2197                    boolean pagination = true;
2198                    FinderPath finderPath = null;
2199                    Object[] finderArgs = null;
2200    
2201                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2202                                    (orderByComparator == null)) {
2203                            pagination = false;
2204                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C;
2205                            finderArgs = new Object[] { companyId, classNameId };
2206                    }
2207                    else {
2208                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C;
2209                            finderArgs = new Object[] {
2210                                            companyId, classNameId,
2211                                            
2212                                            start, end, orderByComparator
2213                                    };
2214                    }
2215    
2216                    List<EmailAddress> list = (List<EmailAddress>)FinderCacheUtil.getResult(finderPath,
2217                                    finderArgs, this);
2218    
2219                    if ((list != null) && !list.isEmpty()) {
2220                            for (EmailAddress emailAddress : list) {
2221                                    if ((companyId != emailAddress.getCompanyId()) ||
2222                                                    (classNameId != emailAddress.getClassNameId())) {
2223                                            list = null;
2224    
2225                                            break;
2226                                    }
2227                            }
2228                    }
2229    
2230                    if (list == null) {
2231                            StringBundler query = null;
2232    
2233                            if (orderByComparator != null) {
2234                                    query = new StringBundler(4 +
2235                                                    (orderByComparator.getOrderByFields().length * 3));
2236                            }
2237                            else {
2238                                    query = new StringBundler(4);
2239                            }
2240    
2241                            query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
2242    
2243                            query.append(_FINDER_COLUMN_C_C_COMPANYID_2);
2244    
2245                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
2246    
2247                            if (orderByComparator != null) {
2248                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2249                                            orderByComparator);
2250                            }
2251                            else
2252                             if (pagination) {
2253                                    query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
2254                            }
2255    
2256                            String sql = query.toString();
2257    
2258                            Session session = null;
2259    
2260                            try {
2261                                    session = openSession();
2262    
2263                                    Query q = session.createQuery(sql);
2264    
2265                                    QueryPos qPos = QueryPos.getInstance(q);
2266    
2267                                    qPos.add(companyId);
2268    
2269                                    qPos.add(classNameId);
2270    
2271                                    if (!pagination) {
2272                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
2273                                                            start, end, false);
2274    
2275                                            Collections.sort(list);
2276    
2277                                            list = Collections.unmodifiableList(list);
2278                                    }
2279                                    else {
2280                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
2281                                                            start, end);
2282                                    }
2283    
2284                                    cacheResult(list);
2285    
2286                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2287                            }
2288                            catch (Exception e) {
2289                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2290    
2291                                    throw processException(e);
2292                            }
2293                            finally {
2294                                    closeSession(session);
2295                            }
2296                    }
2297    
2298                    return list;
2299            }
2300    
2301            /**
2302             * Returns the first email address in the ordered set where companyId = &#63; and classNameId = &#63;.
2303             *
2304             * @param companyId the company ID
2305             * @param classNameId the class name ID
2306             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2307             * @return the first matching email address
2308             * @throws NoSuchEmailAddressException if a matching email address could not be found
2309             */
2310            @Override
2311            public EmailAddress findByC_C_First(long companyId, long classNameId,
2312                    OrderByComparator<EmailAddress> orderByComparator)
2313                    throws NoSuchEmailAddressException {
2314                    EmailAddress emailAddress = fetchByC_C_First(companyId, classNameId,
2315                                    orderByComparator);
2316    
2317                    if (emailAddress != null) {
2318                            return emailAddress;
2319                    }
2320    
2321                    StringBundler msg = new StringBundler(6);
2322    
2323                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2324    
2325                    msg.append("companyId=");
2326                    msg.append(companyId);
2327    
2328                    msg.append(", classNameId=");
2329                    msg.append(classNameId);
2330    
2331                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2332    
2333                    throw new NoSuchEmailAddressException(msg.toString());
2334            }
2335    
2336            /**
2337             * Returns the first email address in the ordered set where companyId = &#63; and classNameId = &#63;.
2338             *
2339             * @param companyId the company ID
2340             * @param classNameId the class name ID
2341             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2342             * @return the first matching email address, or <code>null</code> if a matching email address could not be found
2343             */
2344            @Override
2345            public EmailAddress fetchByC_C_First(long companyId, long classNameId,
2346                    OrderByComparator<EmailAddress> orderByComparator) {
2347                    List<EmailAddress> list = findByC_C(companyId, classNameId, 0, 1,
2348                                    orderByComparator);
2349    
2350                    if (!list.isEmpty()) {
2351                            return list.get(0);
2352                    }
2353    
2354                    return null;
2355            }
2356    
2357            /**
2358             * Returns the last email address in the ordered set where companyId = &#63; and classNameId = &#63;.
2359             *
2360             * @param companyId the company ID
2361             * @param classNameId the class name ID
2362             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2363             * @return the last matching email address
2364             * @throws NoSuchEmailAddressException if a matching email address could not be found
2365             */
2366            @Override
2367            public EmailAddress findByC_C_Last(long companyId, long classNameId,
2368                    OrderByComparator<EmailAddress> orderByComparator)
2369                    throws NoSuchEmailAddressException {
2370                    EmailAddress emailAddress = fetchByC_C_Last(companyId, classNameId,
2371                                    orderByComparator);
2372    
2373                    if (emailAddress != null) {
2374                            return emailAddress;
2375                    }
2376    
2377                    StringBundler msg = new StringBundler(6);
2378    
2379                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2380    
2381                    msg.append("companyId=");
2382                    msg.append(companyId);
2383    
2384                    msg.append(", classNameId=");
2385                    msg.append(classNameId);
2386    
2387                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2388    
2389                    throw new NoSuchEmailAddressException(msg.toString());
2390            }
2391    
2392            /**
2393             * Returns the last email address in the ordered set where companyId = &#63; and classNameId = &#63;.
2394             *
2395             * @param companyId the company ID
2396             * @param classNameId the class name ID
2397             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2398             * @return the last matching email address, or <code>null</code> if a matching email address could not be found
2399             */
2400            @Override
2401            public EmailAddress fetchByC_C_Last(long companyId, long classNameId,
2402                    OrderByComparator<EmailAddress> orderByComparator) {
2403                    int count = countByC_C(companyId, classNameId);
2404    
2405                    if (count == 0) {
2406                            return null;
2407                    }
2408    
2409                    List<EmailAddress> list = findByC_C(companyId, classNameId, count - 1,
2410                                    count, orderByComparator);
2411    
2412                    if (!list.isEmpty()) {
2413                            return list.get(0);
2414                    }
2415    
2416                    return null;
2417            }
2418    
2419            /**
2420             * Returns the email addresses before and after the current email address in the ordered set where companyId = &#63; and classNameId = &#63;.
2421             *
2422             * @param emailAddressId the primary key of the current email address
2423             * @param companyId the company ID
2424             * @param classNameId the class name ID
2425             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2426             * @return the previous, current, and next email address
2427             * @throws NoSuchEmailAddressException if a email address with the primary key could not be found
2428             */
2429            @Override
2430            public EmailAddress[] findByC_C_PrevAndNext(long emailAddressId,
2431                    long companyId, long classNameId,
2432                    OrderByComparator<EmailAddress> orderByComparator)
2433                    throws NoSuchEmailAddressException {
2434                    EmailAddress emailAddress = findByPrimaryKey(emailAddressId);
2435    
2436                    Session session = null;
2437    
2438                    try {
2439                            session = openSession();
2440    
2441                            EmailAddress[] array = new EmailAddressImpl[3];
2442    
2443                            array[0] = getByC_C_PrevAndNext(session, emailAddress, companyId,
2444                                            classNameId, orderByComparator, true);
2445    
2446                            array[1] = emailAddress;
2447    
2448                            array[2] = getByC_C_PrevAndNext(session, emailAddress, companyId,
2449                                            classNameId, orderByComparator, false);
2450    
2451                            return array;
2452                    }
2453                    catch (Exception e) {
2454                            throw processException(e);
2455                    }
2456                    finally {
2457                            closeSession(session);
2458                    }
2459            }
2460    
2461            protected EmailAddress getByC_C_PrevAndNext(Session session,
2462                    EmailAddress emailAddress, long companyId, long classNameId,
2463                    OrderByComparator<EmailAddress> orderByComparator, boolean previous) {
2464                    StringBundler query = null;
2465    
2466                    if (orderByComparator != null) {
2467                            query = new StringBundler(6 +
2468                                            (orderByComparator.getOrderByFields().length * 6));
2469                    }
2470                    else {
2471                            query = new StringBundler(3);
2472                    }
2473    
2474                    query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
2475    
2476                    query.append(_FINDER_COLUMN_C_C_COMPANYID_2);
2477    
2478                    query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
2479    
2480                    if (orderByComparator != null) {
2481                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2482    
2483                            if (orderByConditionFields.length > 0) {
2484                                    query.append(WHERE_AND);
2485                            }
2486    
2487                            for (int i = 0; i < orderByConditionFields.length; i++) {
2488                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2489                                    query.append(orderByConditionFields[i]);
2490    
2491                                    if ((i + 1) < orderByConditionFields.length) {
2492                                            if (orderByComparator.isAscending() ^ previous) {
2493                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2494                                            }
2495                                            else {
2496                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2497                                            }
2498                                    }
2499                                    else {
2500                                            if (orderByComparator.isAscending() ^ previous) {
2501                                                    query.append(WHERE_GREATER_THAN);
2502                                            }
2503                                            else {
2504                                                    query.append(WHERE_LESSER_THAN);
2505                                            }
2506                                    }
2507                            }
2508    
2509                            query.append(ORDER_BY_CLAUSE);
2510    
2511                            String[] orderByFields = orderByComparator.getOrderByFields();
2512    
2513                            for (int i = 0; i < orderByFields.length; i++) {
2514                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2515                                    query.append(orderByFields[i]);
2516    
2517                                    if ((i + 1) < orderByFields.length) {
2518                                            if (orderByComparator.isAscending() ^ previous) {
2519                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2520                                            }
2521                                            else {
2522                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2523                                            }
2524                                    }
2525                                    else {
2526                                            if (orderByComparator.isAscending() ^ previous) {
2527                                                    query.append(ORDER_BY_ASC);
2528                                            }
2529                                            else {
2530                                                    query.append(ORDER_BY_DESC);
2531                                            }
2532                                    }
2533                            }
2534                    }
2535                    else {
2536                            query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
2537                    }
2538    
2539                    String sql = query.toString();
2540    
2541                    Query q = session.createQuery(sql);
2542    
2543                    q.setFirstResult(0);
2544                    q.setMaxResults(2);
2545    
2546                    QueryPos qPos = QueryPos.getInstance(q);
2547    
2548                    qPos.add(companyId);
2549    
2550                    qPos.add(classNameId);
2551    
2552                    if (orderByComparator != null) {
2553                            Object[] values = orderByComparator.getOrderByConditionValues(emailAddress);
2554    
2555                            for (Object value : values) {
2556                                    qPos.add(value);
2557                            }
2558                    }
2559    
2560                    List<EmailAddress> list = q.list();
2561    
2562                    if (list.size() == 2) {
2563                            return list.get(1);
2564                    }
2565                    else {
2566                            return null;
2567                    }
2568            }
2569    
2570            /**
2571             * Removes all the email addresses where companyId = &#63; and classNameId = &#63; from the database.
2572             *
2573             * @param companyId the company ID
2574             * @param classNameId the class name ID
2575             */
2576            @Override
2577            public void removeByC_C(long companyId, long classNameId) {
2578                    for (EmailAddress emailAddress : findByC_C(companyId, classNameId,
2579                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
2580                            remove(emailAddress);
2581                    }
2582            }
2583    
2584            /**
2585             * Returns the number of email addresses where companyId = &#63; and classNameId = &#63;.
2586             *
2587             * @param companyId the company ID
2588             * @param classNameId the class name ID
2589             * @return the number of matching email addresses
2590             */
2591            @Override
2592            public int countByC_C(long companyId, long classNameId) {
2593                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C;
2594    
2595                    Object[] finderArgs = new Object[] { companyId, classNameId };
2596    
2597                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
2598                                    this);
2599    
2600                    if (count == null) {
2601                            StringBundler query = new StringBundler(3);
2602    
2603                            query.append(_SQL_COUNT_EMAILADDRESS_WHERE);
2604    
2605                            query.append(_FINDER_COLUMN_C_C_COMPANYID_2);
2606    
2607                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
2608    
2609                            String sql = query.toString();
2610    
2611                            Session session = null;
2612    
2613                            try {
2614                                    session = openSession();
2615    
2616                                    Query q = session.createQuery(sql);
2617    
2618                                    QueryPos qPos = QueryPos.getInstance(q);
2619    
2620                                    qPos.add(companyId);
2621    
2622                                    qPos.add(classNameId);
2623    
2624                                    count = (Long)q.uniqueResult();
2625    
2626                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2627                            }
2628                            catch (Exception e) {
2629                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2630    
2631                                    throw processException(e);
2632                            }
2633                            finally {
2634                                    closeSession(session);
2635                            }
2636                    }
2637    
2638                    return count.intValue();
2639            }
2640    
2641            private static final String _FINDER_COLUMN_C_C_COMPANYID_2 = "emailAddress.companyId = ? AND ";
2642            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "emailAddress.classNameId = ?";
2643            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_C = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
2644                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
2645                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C_C",
2646                            new String[] {
2647                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
2648                                    
2649                            Integer.class.getName(), Integer.class.getName(),
2650                                    OrderByComparator.class.getName()
2651                            });
2652            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
2653                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
2654                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C_C",
2655                            new String[] {
2656                                    Long.class.getName(), Long.class.getName(), Long.class.getName()
2657                            },
2658                            EmailAddressModelImpl.COMPANYID_COLUMN_BITMASK |
2659                            EmailAddressModelImpl.CLASSNAMEID_COLUMN_BITMASK |
2660                            EmailAddressModelImpl.CLASSPK_COLUMN_BITMASK |
2661                            EmailAddressModelImpl.CREATEDATE_COLUMN_BITMASK);
2662            public static final FinderPath FINDER_PATH_COUNT_BY_C_C_C = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
2663                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
2664                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C_C",
2665                            new String[] {
2666                                    Long.class.getName(), Long.class.getName(), Long.class.getName()
2667                            });
2668    
2669            /**
2670             * Returns all the email addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
2671             *
2672             * @param companyId the company ID
2673             * @param classNameId the class name ID
2674             * @param classPK the class p k
2675             * @return the matching email addresses
2676             */
2677            @Override
2678            public List<EmailAddress> findByC_C_C(long companyId, long classNameId,
2679                    long classPK) {
2680                    return findByC_C_C(companyId, classNameId, classPK, QueryUtil.ALL_POS,
2681                            QueryUtil.ALL_POS, null);
2682            }
2683    
2684            /**
2685             * Returns a range of all the email addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
2686             *
2687             * <p>
2688             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
2689             * </p>
2690             *
2691             * @param companyId the company ID
2692             * @param classNameId the class name ID
2693             * @param classPK the class p k
2694             * @param start the lower bound of the range of email addresses
2695             * @param end the upper bound of the range of email addresses (not inclusive)
2696             * @return the range of matching email addresses
2697             */
2698            @Override
2699            public List<EmailAddress> findByC_C_C(long companyId, long classNameId,
2700                    long classPK, int start, int end) {
2701                    return findByC_C_C(companyId, classNameId, classPK, start, end, null);
2702            }
2703    
2704            /**
2705             * Returns an ordered range of all the email addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
2706             *
2707             * <p>
2708             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
2709             * </p>
2710             *
2711             * @param companyId the company ID
2712             * @param classNameId the class name ID
2713             * @param classPK the class p k
2714             * @param start the lower bound of the range of email addresses
2715             * @param end the upper bound of the range of email addresses (not inclusive)
2716             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2717             * @return the ordered range of matching email addresses
2718             */
2719            @Override
2720            public List<EmailAddress> findByC_C_C(long companyId, long classNameId,
2721                    long classPK, int start, int end,
2722                    OrderByComparator<EmailAddress> orderByComparator) {
2723                    boolean pagination = true;
2724                    FinderPath finderPath = null;
2725                    Object[] finderArgs = null;
2726    
2727                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2728                                    (orderByComparator == null)) {
2729                            pagination = false;
2730                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C;
2731                            finderArgs = new Object[] { companyId, classNameId, classPK };
2732                    }
2733                    else {
2734                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_C;
2735                            finderArgs = new Object[] {
2736                                            companyId, classNameId, classPK,
2737                                            
2738                                            start, end, orderByComparator
2739                                    };
2740                    }
2741    
2742                    List<EmailAddress> list = (List<EmailAddress>)FinderCacheUtil.getResult(finderPath,
2743                                    finderArgs, this);
2744    
2745                    if ((list != null) && !list.isEmpty()) {
2746                            for (EmailAddress emailAddress : list) {
2747                                    if ((companyId != emailAddress.getCompanyId()) ||
2748                                                    (classNameId != emailAddress.getClassNameId()) ||
2749                                                    (classPK != emailAddress.getClassPK())) {
2750                                            list = null;
2751    
2752                                            break;
2753                                    }
2754                            }
2755                    }
2756    
2757                    if (list == null) {
2758                            StringBundler query = null;
2759    
2760                            if (orderByComparator != null) {
2761                                    query = new StringBundler(5 +
2762                                                    (orderByComparator.getOrderByFields().length * 3));
2763                            }
2764                            else {
2765                                    query = new StringBundler(5);
2766                            }
2767    
2768                            query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
2769    
2770                            query.append(_FINDER_COLUMN_C_C_C_COMPANYID_2);
2771    
2772                            query.append(_FINDER_COLUMN_C_C_C_CLASSNAMEID_2);
2773    
2774                            query.append(_FINDER_COLUMN_C_C_C_CLASSPK_2);
2775    
2776                            if (orderByComparator != null) {
2777                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2778                                            orderByComparator);
2779                            }
2780                            else
2781                             if (pagination) {
2782                                    query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
2783                            }
2784    
2785                            String sql = query.toString();
2786    
2787                            Session session = null;
2788    
2789                            try {
2790                                    session = openSession();
2791    
2792                                    Query q = session.createQuery(sql);
2793    
2794                                    QueryPos qPos = QueryPos.getInstance(q);
2795    
2796                                    qPos.add(companyId);
2797    
2798                                    qPos.add(classNameId);
2799    
2800                                    qPos.add(classPK);
2801    
2802                                    if (!pagination) {
2803                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
2804                                                            start, end, false);
2805    
2806                                            Collections.sort(list);
2807    
2808                                            list = Collections.unmodifiableList(list);
2809                                    }
2810                                    else {
2811                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
2812                                                            start, end);
2813                                    }
2814    
2815                                    cacheResult(list);
2816    
2817                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2818                            }
2819                            catch (Exception e) {
2820                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2821    
2822                                    throw processException(e);
2823                            }
2824                            finally {
2825                                    closeSession(session);
2826                            }
2827                    }
2828    
2829                    return list;
2830            }
2831    
2832            /**
2833             * Returns the first email address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
2834             *
2835             * @param companyId the company ID
2836             * @param classNameId the class name ID
2837             * @param classPK the class p k
2838             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2839             * @return the first matching email address
2840             * @throws NoSuchEmailAddressException if a matching email address could not be found
2841             */
2842            @Override
2843            public EmailAddress findByC_C_C_First(long companyId, long classNameId,
2844                    long classPK, OrderByComparator<EmailAddress> orderByComparator)
2845                    throws NoSuchEmailAddressException {
2846                    EmailAddress emailAddress = fetchByC_C_C_First(companyId, classNameId,
2847                                    classPK, orderByComparator);
2848    
2849                    if (emailAddress != null) {
2850                            return emailAddress;
2851                    }
2852    
2853                    StringBundler msg = new StringBundler(8);
2854    
2855                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2856    
2857                    msg.append("companyId=");
2858                    msg.append(companyId);
2859    
2860                    msg.append(", classNameId=");
2861                    msg.append(classNameId);
2862    
2863                    msg.append(", classPK=");
2864                    msg.append(classPK);
2865    
2866                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2867    
2868                    throw new NoSuchEmailAddressException(msg.toString());
2869            }
2870    
2871            /**
2872             * Returns the first email address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
2873             *
2874             * @param companyId the company ID
2875             * @param classNameId the class name ID
2876             * @param classPK the class p k
2877             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2878             * @return the first matching email address, or <code>null</code> if a matching email address could not be found
2879             */
2880            @Override
2881            public EmailAddress fetchByC_C_C_First(long companyId, long classNameId,
2882                    long classPK, OrderByComparator<EmailAddress> orderByComparator) {
2883                    List<EmailAddress> list = findByC_C_C(companyId, classNameId, classPK,
2884                                    0, 1, orderByComparator);
2885    
2886                    if (!list.isEmpty()) {
2887                            return list.get(0);
2888                    }
2889    
2890                    return null;
2891            }
2892    
2893            /**
2894             * Returns the last email address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
2895             *
2896             * @param companyId the company ID
2897             * @param classNameId the class name ID
2898             * @param classPK the class p k
2899             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2900             * @return the last matching email address
2901             * @throws NoSuchEmailAddressException if a matching email address could not be found
2902             */
2903            @Override
2904            public EmailAddress findByC_C_C_Last(long companyId, long classNameId,
2905                    long classPK, OrderByComparator<EmailAddress> orderByComparator)
2906                    throws NoSuchEmailAddressException {
2907                    EmailAddress emailAddress = fetchByC_C_C_Last(companyId, classNameId,
2908                                    classPK, orderByComparator);
2909    
2910                    if (emailAddress != null) {
2911                            return emailAddress;
2912                    }
2913    
2914                    StringBundler msg = new StringBundler(8);
2915    
2916                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2917    
2918                    msg.append("companyId=");
2919                    msg.append(companyId);
2920    
2921                    msg.append(", classNameId=");
2922                    msg.append(classNameId);
2923    
2924                    msg.append(", classPK=");
2925                    msg.append(classPK);
2926    
2927                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2928    
2929                    throw new NoSuchEmailAddressException(msg.toString());
2930            }
2931    
2932            /**
2933             * Returns the last email address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
2934             *
2935             * @param companyId the company ID
2936             * @param classNameId the class name ID
2937             * @param classPK the class p k
2938             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2939             * @return the last matching email address, or <code>null</code> if a matching email address could not be found
2940             */
2941            @Override
2942            public EmailAddress fetchByC_C_C_Last(long companyId, long classNameId,
2943                    long classPK, OrderByComparator<EmailAddress> orderByComparator) {
2944                    int count = countByC_C_C(companyId, classNameId, classPK);
2945    
2946                    if (count == 0) {
2947                            return null;
2948                    }
2949    
2950                    List<EmailAddress> list = findByC_C_C(companyId, classNameId, classPK,
2951                                    count - 1, count, orderByComparator);
2952    
2953                    if (!list.isEmpty()) {
2954                            return list.get(0);
2955                    }
2956    
2957                    return null;
2958            }
2959    
2960            /**
2961             * Returns the email addresses before and after the current email address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
2962             *
2963             * @param emailAddressId the primary key of the current email address
2964             * @param companyId the company ID
2965             * @param classNameId the class name ID
2966             * @param classPK the class p k
2967             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2968             * @return the previous, current, and next email address
2969             * @throws NoSuchEmailAddressException if a email address with the primary key could not be found
2970             */
2971            @Override
2972            public EmailAddress[] findByC_C_C_PrevAndNext(long emailAddressId,
2973                    long companyId, long classNameId, long classPK,
2974                    OrderByComparator<EmailAddress> orderByComparator)
2975                    throws NoSuchEmailAddressException {
2976                    EmailAddress emailAddress = findByPrimaryKey(emailAddressId);
2977    
2978                    Session session = null;
2979    
2980                    try {
2981                            session = openSession();
2982    
2983                            EmailAddress[] array = new EmailAddressImpl[3];
2984    
2985                            array[0] = getByC_C_C_PrevAndNext(session, emailAddress, companyId,
2986                                            classNameId, classPK, orderByComparator, true);
2987    
2988                            array[1] = emailAddress;
2989    
2990                            array[2] = getByC_C_C_PrevAndNext(session, emailAddress, companyId,
2991                                            classNameId, classPK, orderByComparator, false);
2992    
2993                            return array;
2994                    }
2995                    catch (Exception e) {
2996                            throw processException(e);
2997                    }
2998                    finally {
2999                            closeSession(session);
3000                    }
3001            }
3002    
3003            protected EmailAddress getByC_C_C_PrevAndNext(Session session,
3004                    EmailAddress emailAddress, long companyId, long classNameId,
3005                    long classPK, OrderByComparator<EmailAddress> orderByComparator,
3006                    boolean previous) {
3007                    StringBundler query = null;
3008    
3009                    if (orderByComparator != null) {
3010                            query = new StringBundler(6 +
3011                                            (orderByComparator.getOrderByFields().length * 6));
3012                    }
3013                    else {
3014                            query = new StringBundler(3);
3015                    }
3016    
3017                    query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
3018    
3019                    query.append(_FINDER_COLUMN_C_C_C_COMPANYID_2);
3020    
3021                    query.append(_FINDER_COLUMN_C_C_C_CLASSNAMEID_2);
3022    
3023                    query.append(_FINDER_COLUMN_C_C_C_CLASSPK_2);
3024    
3025                    if (orderByComparator != null) {
3026                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
3027    
3028                            if (orderByConditionFields.length > 0) {
3029                                    query.append(WHERE_AND);
3030                            }
3031    
3032                            for (int i = 0; i < orderByConditionFields.length; i++) {
3033                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3034                                    query.append(orderByConditionFields[i]);
3035    
3036                                    if ((i + 1) < orderByConditionFields.length) {
3037                                            if (orderByComparator.isAscending() ^ previous) {
3038                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
3039                                            }
3040                                            else {
3041                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
3042                                            }
3043                                    }
3044                                    else {
3045                                            if (orderByComparator.isAscending() ^ previous) {
3046                                                    query.append(WHERE_GREATER_THAN);
3047                                            }
3048                                            else {
3049                                                    query.append(WHERE_LESSER_THAN);
3050                                            }
3051                                    }
3052                            }
3053    
3054                            query.append(ORDER_BY_CLAUSE);
3055    
3056                            String[] orderByFields = orderByComparator.getOrderByFields();
3057    
3058                            for (int i = 0; i < orderByFields.length; i++) {
3059                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3060                                    query.append(orderByFields[i]);
3061    
3062                                    if ((i + 1) < orderByFields.length) {
3063                                            if (orderByComparator.isAscending() ^ previous) {
3064                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
3065                                            }
3066                                            else {
3067                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
3068                                            }
3069                                    }
3070                                    else {
3071                                            if (orderByComparator.isAscending() ^ previous) {
3072                                                    query.append(ORDER_BY_ASC);
3073                                            }
3074                                            else {
3075                                                    query.append(ORDER_BY_DESC);
3076                                            }
3077                                    }
3078                            }
3079                    }
3080                    else {
3081                            query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
3082                    }
3083    
3084                    String sql = query.toString();
3085    
3086                    Query q = session.createQuery(sql);
3087    
3088                    q.setFirstResult(0);
3089                    q.setMaxResults(2);
3090    
3091                    QueryPos qPos = QueryPos.getInstance(q);
3092    
3093                    qPos.add(companyId);
3094    
3095                    qPos.add(classNameId);
3096    
3097                    qPos.add(classPK);
3098    
3099                    if (orderByComparator != null) {
3100                            Object[] values = orderByComparator.getOrderByConditionValues(emailAddress);
3101    
3102                            for (Object value : values) {
3103                                    qPos.add(value);
3104                            }
3105                    }
3106    
3107                    List<EmailAddress> list = q.list();
3108    
3109                    if (list.size() == 2) {
3110                            return list.get(1);
3111                    }
3112                    else {
3113                            return null;
3114                    }
3115            }
3116    
3117            /**
3118             * Removes all the email addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; from the database.
3119             *
3120             * @param companyId the company ID
3121             * @param classNameId the class name ID
3122             * @param classPK the class p k
3123             */
3124            @Override
3125            public void removeByC_C_C(long companyId, long classNameId, long classPK) {
3126                    for (EmailAddress emailAddress : findByC_C_C(companyId, classNameId,
3127                                    classPK, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
3128                            remove(emailAddress);
3129                    }
3130            }
3131    
3132            /**
3133             * Returns the number of email addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
3134             *
3135             * @param companyId the company ID
3136             * @param classNameId the class name ID
3137             * @param classPK the class p k
3138             * @return the number of matching email addresses
3139             */
3140            @Override
3141            public int countByC_C_C(long companyId, long classNameId, long classPK) {
3142                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C_C;
3143    
3144                    Object[] finderArgs = new Object[] { companyId, classNameId, classPK };
3145    
3146                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
3147                                    this);
3148    
3149                    if (count == null) {
3150                            StringBundler query = new StringBundler(4);
3151    
3152                            query.append(_SQL_COUNT_EMAILADDRESS_WHERE);
3153    
3154                            query.append(_FINDER_COLUMN_C_C_C_COMPANYID_2);
3155    
3156                            query.append(_FINDER_COLUMN_C_C_C_CLASSNAMEID_2);
3157    
3158                            query.append(_FINDER_COLUMN_C_C_C_CLASSPK_2);
3159    
3160                            String sql = query.toString();
3161    
3162                            Session session = null;
3163    
3164                            try {
3165                                    session = openSession();
3166    
3167                                    Query q = session.createQuery(sql);
3168    
3169                                    QueryPos qPos = QueryPos.getInstance(q);
3170    
3171                                    qPos.add(companyId);
3172    
3173                                    qPos.add(classNameId);
3174    
3175                                    qPos.add(classPK);
3176    
3177                                    count = (Long)q.uniqueResult();
3178    
3179                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
3180                            }
3181                            catch (Exception e) {
3182                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3183    
3184                                    throw processException(e);
3185                            }
3186                            finally {
3187                                    closeSession(session);
3188                            }
3189                    }
3190    
3191                    return count.intValue();
3192            }
3193    
3194            private static final String _FINDER_COLUMN_C_C_C_COMPANYID_2 = "emailAddress.companyId = ? AND ";
3195            private static final String _FINDER_COLUMN_C_C_C_CLASSNAMEID_2 = "emailAddress.classNameId = ? AND ";
3196            private static final String _FINDER_COLUMN_C_C_C_CLASSPK_2 = "emailAddress.classPK = ?";
3197            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_C_P = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
3198                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
3199                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C_C_P",
3200                            new String[] {
3201                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
3202                                    Boolean.class.getName(),
3203                                    
3204                            Integer.class.getName(), Integer.class.getName(),
3205                                    OrderByComparator.class.getName()
3206                            });
3207            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_P =
3208                    new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
3209                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, EmailAddressImpl.class,
3210                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C_C_P",
3211                            new String[] {
3212                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
3213                                    Boolean.class.getName()
3214                            },
3215                            EmailAddressModelImpl.COMPANYID_COLUMN_BITMASK |
3216                            EmailAddressModelImpl.CLASSNAMEID_COLUMN_BITMASK |
3217                            EmailAddressModelImpl.CLASSPK_COLUMN_BITMASK |
3218                            EmailAddressModelImpl.PRIMARY_COLUMN_BITMASK |
3219                            EmailAddressModelImpl.CREATEDATE_COLUMN_BITMASK);
3220            public static final FinderPath FINDER_PATH_COUNT_BY_C_C_C_P = new FinderPath(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
3221                            EmailAddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
3222                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C_C_P",
3223                            new String[] {
3224                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
3225                                    Boolean.class.getName()
3226                            });
3227    
3228            /**
3229             * Returns all the email addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
3230             *
3231             * @param companyId the company ID
3232             * @param classNameId the class name ID
3233             * @param classPK the class p k
3234             * @param primary the primary
3235             * @return the matching email addresses
3236             */
3237            @Override
3238            public List<EmailAddress> findByC_C_C_P(long companyId, long classNameId,
3239                    long classPK, boolean primary) {
3240                    return findByC_C_C_P(companyId, classNameId, classPK, primary,
3241                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
3242            }
3243    
3244            /**
3245             * Returns a range of all the email addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
3246             *
3247             * <p>
3248             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
3249             * </p>
3250             *
3251             * @param companyId the company ID
3252             * @param classNameId the class name ID
3253             * @param classPK the class p k
3254             * @param primary the primary
3255             * @param start the lower bound of the range of email addresses
3256             * @param end the upper bound of the range of email addresses (not inclusive)
3257             * @return the range of matching email addresses
3258             */
3259            @Override
3260            public List<EmailAddress> findByC_C_C_P(long companyId, long classNameId,
3261                    long classPK, boolean primary, int start, int end) {
3262                    return findByC_C_C_P(companyId, classNameId, classPK, primary, start,
3263                            end, null);
3264            }
3265    
3266            /**
3267             * Returns an ordered range of all the email addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
3268             *
3269             * <p>
3270             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
3271             * </p>
3272             *
3273             * @param companyId the company ID
3274             * @param classNameId the class name ID
3275             * @param classPK the class p k
3276             * @param primary the primary
3277             * @param start the lower bound of the range of email addresses
3278             * @param end the upper bound of the range of email addresses (not inclusive)
3279             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3280             * @return the ordered range of matching email addresses
3281             */
3282            @Override
3283            public List<EmailAddress> findByC_C_C_P(long companyId, long classNameId,
3284                    long classPK, boolean primary, int start, int end,
3285                    OrderByComparator<EmailAddress> orderByComparator) {
3286                    boolean pagination = true;
3287                    FinderPath finderPath = null;
3288                    Object[] finderArgs = null;
3289    
3290                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3291                                    (orderByComparator == null)) {
3292                            pagination = false;
3293                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_P;
3294                            finderArgs = new Object[] { companyId, classNameId, classPK, primary };
3295                    }
3296                    else {
3297                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_C_P;
3298                            finderArgs = new Object[] {
3299                                            companyId, classNameId, classPK, primary,
3300                                            
3301                                            start, end, orderByComparator
3302                                    };
3303                    }
3304    
3305                    List<EmailAddress> list = (List<EmailAddress>)FinderCacheUtil.getResult(finderPath,
3306                                    finderArgs, this);
3307    
3308                    if ((list != null) && !list.isEmpty()) {
3309                            for (EmailAddress emailAddress : list) {
3310                                    if ((companyId != emailAddress.getCompanyId()) ||
3311                                                    (classNameId != emailAddress.getClassNameId()) ||
3312                                                    (classPK != emailAddress.getClassPK()) ||
3313                                                    (primary != emailAddress.getPrimary())) {
3314                                            list = null;
3315    
3316                                            break;
3317                                    }
3318                            }
3319                    }
3320    
3321                    if (list == null) {
3322                            StringBundler query = null;
3323    
3324                            if (orderByComparator != null) {
3325                                    query = new StringBundler(6 +
3326                                                    (orderByComparator.getOrderByFields().length * 3));
3327                            }
3328                            else {
3329                                    query = new StringBundler(6);
3330                            }
3331    
3332                            query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
3333    
3334                            query.append(_FINDER_COLUMN_C_C_C_P_COMPANYID_2);
3335    
3336                            query.append(_FINDER_COLUMN_C_C_C_P_CLASSNAMEID_2);
3337    
3338                            query.append(_FINDER_COLUMN_C_C_C_P_CLASSPK_2);
3339    
3340                            query.append(_FINDER_COLUMN_C_C_C_P_PRIMARY_2);
3341    
3342                            if (orderByComparator != null) {
3343                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3344                                            orderByComparator);
3345                            }
3346                            else
3347                             if (pagination) {
3348                                    query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
3349                            }
3350    
3351                            String sql = query.toString();
3352    
3353                            Session session = null;
3354    
3355                            try {
3356                                    session = openSession();
3357    
3358                                    Query q = session.createQuery(sql);
3359    
3360                                    QueryPos qPos = QueryPos.getInstance(q);
3361    
3362                                    qPos.add(companyId);
3363    
3364                                    qPos.add(classNameId);
3365    
3366                                    qPos.add(classPK);
3367    
3368                                    qPos.add(primary);
3369    
3370                                    if (!pagination) {
3371                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
3372                                                            start, end, false);
3373    
3374                                            Collections.sort(list);
3375    
3376                                            list = Collections.unmodifiableList(list);
3377                                    }
3378                                    else {
3379                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
3380                                                            start, end);
3381                                    }
3382    
3383                                    cacheResult(list);
3384    
3385                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
3386                            }
3387                            catch (Exception e) {
3388                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3389    
3390                                    throw processException(e);
3391                            }
3392                            finally {
3393                                    closeSession(session);
3394                            }
3395                    }
3396    
3397                    return list;
3398            }
3399    
3400            /**
3401             * Returns the first email address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
3402             *
3403             * @param companyId the company ID
3404             * @param classNameId the class name ID
3405             * @param classPK the class p k
3406             * @param primary the primary
3407             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3408             * @return the first matching email address
3409             * @throws NoSuchEmailAddressException if a matching email address could not be found
3410             */
3411            @Override
3412            public EmailAddress findByC_C_C_P_First(long companyId, long classNameId,
3413                    long classPK, boolean primary,
3414                    OrderByComparator<EmailAddress> orderByComparator)
3415                    throws NoSuchEmailAddressException {
3416                    EmailAddress emailAddress = fetchByC_C_C_P_First(companyId,
3417                                    classNameId, classPK, primary, orderByComparator);
3418    
3419                    if (emailAddress != null) {
3420                            return emailAddress;
3421                    }
3422    
3423                    StringBundler msg = new StringBundler(10);
3424    
3425                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3426    
3427                    msg.append("companyId=");
3428                    msg.append(companyId);
3429    
3430                    msg.append(", classNameId=");
3431                    msg.append(classNameId);
3432    
3433                    msg.append(", classPK=");
3434                    msg.append(classPK);
3435    
3436                    msg.append(", primary=");
3437                    msg.append(primary);
3438    
3439                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3440    
3441                    throw new NoSuchEmailAddressException(msg.toString());
3442            }
3443    
3444            /**
3445             * Returns the first email address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
3446             *
3447             * @param companyId the company ID
3448             * @param classNameId the class name ID
3449             * @param classPK the class p k
3450             * @param primary the primary
3451             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3452             * @return the first matching email address, or <code>null</code> if a matching email address could not be found
3453             */
3454            @Override
3455            public EmailAddress fetchByC_C_C_P_First(long companyId, long classNameId,
3456                    long classPK, boolean primary,
3457                    OrderByComparator<EmailAddress> orderByComparator) {
3458                    List<EmailAddress> list = findByC_C_C_P(companyId, classNameId,
3459                                    classPK, primary, 0, 1, orderByComparator);
3460    
3461                    if (!list.isEmpty()) {
3462                            return list.get(0);
3463                    }
3464    
3465                    return null;
3466            }
3467    
3468            /**
3469             * Returns the last email address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
3470             *
3471             * @param companyId the company ID
3472             * @param classNameId the class name ID
3473             * @param classPK the class p k
3474             * @param primary the primary
3475             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3476             * @return the last matching email address
3477             * @throws NoSuchEmailAddressException if a matching email address could not be found
3478             */
3479            @Override
3480            public EmailAddress findByC_C_C_P_Last(long companyId, long classNameId,
3481                    long classPK, boolean primary,
3482                    OrderByComparator<EmailAddress> orderByComparator)
3483                    throws NoSuchEmailAddressException {
3484                    EmailAddress emailAddress = fetchByC_C_C_P_Last(companyId, classNameId,
3485                                    classPK, primary, orderByComparator);
3486    
3487                    if (emailAddress != null) {
3488                            return emailAddress;
3489                    }
3490    
3491                    StringBundler msg = new StringBundler(10);
3492    
3493                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3494    
3495                    msg.append("companyId=");
3496                    msg.append(companyId);
3497    
3498                    msg.append(", classNameId=");
3499                    msg.append(classNameId);
3500    
3501                    msg.append(", classPK=");
3502                    msg.append(classPK);
3503    
3504                    msg.append(", primary=");
3505                    msg.append(primary);
3506    
3507                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3508    
3509                    throw new NoSuchEmailAddressException(msg.toString());
3510            }
3511    
3512            /**
3513             * Returns the last email address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
3514             *
3515             * @param companyId the company ID
3516             * @param classNameId the class name ID
3517             * @param classPK the class p k
3518             * @param primary the primary
3519             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3520             * @return the last matching email address, or <code>null</code> if a matching email address could not be found
3521             */
3522            @Override
3523            public EmailAddress fetchByC_C_C_P_Last(long companyId, long classNameId,
3524                    long classPK, boolean primary,
3525                    OrderByComparator<EmailAddress> orderByComparator) {
3526                    int count = countByC_C_C_P(companyId, classNameId, classPK, primary);
3527    
3528                    if (count == 0) {
3529                            return null;
3530                    }
3531    
3532                    List<EmailAddress> list = findByC_C_C_P(companyId, classNameId,
3533                                    classPK, primary, count - 1, count, orderByComparator);
3534    
3535                    if (!list.isEmpty()) {
3536                            return list.get(0);
3537                    }
3538    
3539                    return null;
3540            }
3541    
3542            /**
3543             * Returns the email addresses before and after the current email address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
3544             *
3545             * @param emailAddressId the primary key of the current email address
3546             * @param companyId the company ID
3547             * @param classNameId the class name ID
3548             * @param classPK the class p k
3549             * @param primary the primary
3550             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3551             * @return the previous, current, and next email address
3552             * @throws NoSuchEmailAddressException if a email address with the primary key could not be found
3553             */
3554            @Override
3555            public EmailAddress[] findByC_C_C_P_PrevAndNext(long emailAddressId,
3556                    long companyId, long classNameId, long classPK, boolean primary,
3557                    OrderByComparator<EmailAddress> orderByComparator)
3558                    throws NoSuchEmailAddressException {
3559                    EmailAddress emailAddress = findByPrimaryKey(emailAddressId);
3560    
3561                    Session session = null;
3562    
3563                    try {
3564                            session = openSession();
3565    
3566                            EmailAddress[] array = new EmailAddressImpl[3];
3567    
3568                            array[0] = getByC_C_C_P_PrevAndNext(session, emailAddress,
3569                                            companyId, classNameId, classPK, primary,
3570                                            orderByComparator, true);
3571    
3572                            array[1] = emailAddress;
3573    
3574                            array[2] = getByC_C_C_P_PrevAndNext(session, emailAddress,
3575                                            companyId, classNameId, classPK, primary,
3576                                            orderByComparator, false);
3577    
3578                            return array;
3579                    }
3580                    catch (Exception e) {
3581                            throw processException(e);
3582                    }
3583                    finally {
3584                            closeSession(session);
3585                    }
3586            }
3587    
3588            protected EmailAddress getByC_C_C_P_PrevAndNext(Session session,
3589                    EmailAddress emailAddress, long companyId, long classNameId,
3590                    long classPK, boolean primary,
3591                    OrderByComparator<EmailAddress> orderByComparator, boolean previous) {
3592                    StringBundler query = null;
3593    
3594                    if (orderByComparator != null) {
3595                            query = new StringBundler(6 +
3596                                            (orderByComparator.getOrderByFields().length * 6));
3597                    }
3598                    else {
3599                            query = new StringBundler(3);
3600                    }
3601    
3602                    query.append(_SQL_SELECT_EMAILADDRESS_WHERE);
3603    
3604                    query.append(_FINDER_COLUMN_C_C_C_P_COMPANYID_2);
3605    
3606                    query.append(_FINDER_COLUMN_C_C_C_P_CLASSNAMEID_2);
3607    
3608                    query.append(_FINDER_COLUMN_C_C_C_P_CLASSPK_2);
3609    
3610                    query.append(_FINDER_COLUMN_C_C_C_P_PRIMARY_2);
3611    
3612                    if (orderByComparator != null) {
3613                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
3614    
3615                            if (orderByConditionFields.length > 0) {
3616                                    query.append(WHERE_AND);
3617                            }
3618    
3619                            for (int i = 0; i < orderByConditionFields.length; i++) {
3620                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3621                                    query.append(orderByConditionFields[i]);
3622    
3623                                    if ((i + 1) < orderByConditionFields.length) {
3624                                            if (orderByComparator.isAscending() ^ previous) {
3625                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
3626                                            }
3627                                            else {
3628                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
3629                                            }
3630                                    }
3631                                    else {
3632                                            if (orderByComparator.isAscending() ^ previous) {
3633                                                    query.append(WHERE_GREATER_THAN);
3634                                            }
3635                                            else {
3636                                                    query.append(WHERE_LESSER_THAN);
3637                                            }
3638                                    }
3639                            }
3640    
3641                            query.append(ORDER_BY_CLAUSE);
3642    
3643                            String[] orderByFields = orderByComparator.getOrderByFields();
3644    
3645                            for (int i = 0; i < orderByFields.length; i++) {
3646                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3647                                    query.append(orderByFields[i]);
3648    
3649                                    if ((i + 1) < orderByFields.length) {
3650                                            if (orderByComparator.isAscending() ^ previous) {
3651                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
3652                                            }
3653                                            else {
3654                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
3655                                            }
3656                                    }
3657                                    else {
3658                                            if (orderByComparator.isAscending() ^ previous) {
3659                                                    query.append(ORDER_BY_ASC);
3660                                            }
3661                                            else {
3662                                                    query.append(ORDER_BY_DESC);
3663                                            }
3664                                    }
3665                            }
3666                    }
3667                    else {
3668                            query.append(EmailAddressModelImpl.ORDER_BY_JPQL);
3669                    }
3670    
3671                    String sql = query.toString();
3672    
3673                    Query q = session.createQuery(sql);
3674    
3675                    q.setFirstResult(0);
3676                    q.setMaxResults(2);
3677    
3678                    QueryPos qPos = QueryPos.getInstance(q);
3679    
3680                    qPos.add(companyId);
3681    
3682                    qPos.add(classNameId);
3683    
3684                    qPos.add(classPK);
3685    
3686                    qPos.add(primary);
3687    
3688                    if (orderByComparator != null) {
3689                            Object[] values = orderByComparator.getOrderByConditionValues(emailAddress);
3690    
3691                            for (Object value : values) {
3692                                    qPos.add(value);
3693                            }
3694                    }
3695    
3696                    List<EmailAddress> list = q.list();
3697    
3698                    if (list.size() == 2) {
3699                            return list.get(1);
3700                    }
3701                    else {
3702                            return null;
3703                    }
3704            }
3705    
3706            /**
3707             * Removes all the email addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63; from the database.
3708             *
3709             * @param companyId the company ID
3710             * @param classNameId the class name ID
3711             * @param classPK the class p k
3712             * @param primary the primary
3713             */
3714            @Override
3715            public void removeByC_C_C_P(long companyId, long classNameId, long classPK,
3716                    boolean primary) {
3717                    for (EmailAddress emailAddress : findByC_C_C_P(companyId, classNameId,
3718                                    classPK, primary, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
3719                            remove(emailAddress);
3720                    }
3721            }
3722    
3723            /**
3724             * Returns the number of email addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
3725             *
3726             * @param companyId the company ID
3727             * @param classNameId the class name ID
3728             * @param classPK the class p k
3729             * @param primary the primary
3730             * @return the number of matching email addresses
3731             */
3732            @Override
3733            public int countByC_C_C_P(long companyId, long classNameId, long classPK,
3734                    boolean primary) {
3735                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C_C_P;
3736    
3737                    Object[] finderArgs = new Object[] {
3738                                    companyId, classNameId, classPK, primary
3739                            };
3740    
3741                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
3742                                    this);
3743    
3744                    if (count == null) {
3745                            StringBundler query = new StringBundler(5);
3746    
3747                            query.append(_SQL_COUNT_EMAILADDRESS_WHERE);
3748    
3749                            query.append(_FINDER_COLUMN_C_C_C_P_COMPANYID_2);
3750    
3751                            query.append(_FINDER_COLUMN_C_C_C_P_CLASSNAMEID_2);
3752    
3753                            query.append(_FINDER_COLUMN_C_C_C_P_CLASSPK_2);
3754    
3755                            query.append(_FINDER_COLUMN_C_C_C_P_PRIMARY_2);
3756    
3757                            String sql = query.toString();
3758    
3759                            Session session = null;
3760    
3761                            try {
3762                                    session = openSession();
3763    
3764                                    Query q = session.createQuery(sql);
3765    
3766                                    QueryPos qPos = QueryPos.getInstance(q);
3767    
3768                                    qPos.add(companyId);
3769    
3770                                    qPos.add(classNameId);
3771    
3772                                    qPos.add(classPK);
3773    
3774                                    qPos.add(primary);
3775    
3776                                    count = (Long)q.uniqueResult();
3777    
3778                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
3779                            }
3780                            catch (Exception e) {
3781                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3782    
3783                                    throw processException(e);
3784                            }
3785                            finally {
3786                                    closeSession(session);
3787                            }
3788                    }
3789    
3790                    return count.intValue();
3791            }
3792    
3793            private static final String _FINDER_COLUMN_C_C_C_P_COMPANYID_2 = "emailAddress.companyId = ? AND ";
3794            private static final String _FINDER_COLUMN_C_C_C_P_CLASSNAMEID_2 = "emailAddress.classNameId = ? AND ";
3795            private static final String _FINDER_COLUMN_C_C_C_P_CLASSPK_2 = "emailAddress.classPK = ? AND ";
3796            private static final String _FINDER_COLUMN_C_C_C_P_PRIMARY_2 = "emailAddress.primary = ?";
3797    
3798            public EmailAddressPersistenceImpl() {
3799                    setModelClass(EmailAddress.class);
3800            }
3801    
3802            /**
3803             * Caches the email address in the entity cache if it is enabled.
3804             *
3805             * @param emailAddress the email address
3806             */
3807            @Override
3808            public void cacheResult(EmailAddress emailAddress) {
3809                    EntityCacheUtil.putResult(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
3810                            EmailAddressImpl.class, emailAddress.getPrimaryKey(), emailAddress);
3811    
3812                    emailAddress.resetOriginalValues();
3813            }
3814    
3815            /**
3816             * Caches the email addresses in the entity cache if it is enabled.
3817             *
3818             * @param emailAddresses the email addresses
3819             */
3820            @Override
3821            public void cacheResult(List<EmailAddress> emailAddresses) {
3822                    for (EmailAddress emailAddress : emailAddresses) {
3823                            if (EntityCacheUtil.getResult(
3824                                                    EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
3825                                                    EmailAddressImpl.class, emailAddress.getPrimaryKey()) == null) {
3826                                    cacheResult(emailAddress);
3827                            }
3828                            else {
3829                                    emailAddress.resetOriginalValues();
3830                            }
3831                    }
3832            }
3833    
3834            /**
3835             * Clears the cache for all email addresses.
3836             *
3837             * <p>
3838             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
3839             * </p>
3840             */
3841            @Override
3842            public void clearCache() {
3843                    EntityCacheUtil.clearCache(EmailAddressImpl.class);
3844    
3845                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
3846                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
3847                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
3848            }
3849    
3850            /**
3851             * Clears the cache for the email address.
3852             *
3853             * <p>
3854             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
3855             * </p>
3856             */
3857            @Override
3858            public void clearCache(EmailAddress emailAddress) {
3859                    EntityCacheUtil.removeResult(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
3860                            EmailAddressImpl.class, emailAddress.getPrimaryKey());
3861    
3862                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
3863                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
3864            }
3865    
3866            @Override
3867            public void clearCache(List<EmailAddress> emailAddresses) {
3868                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
3869                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
3870    
3871                    for (EmailAddress emailAddress : emailAddresses) {
3872                            EntityCacheUtil.removeResult(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
3873                                    EmailAddressImpl.class, emailAddress.getPrimaryKey());
3874                    }
3875            }
3876    
3877            /**
3878             * Creates a new email address with the primary key. Does not add the email address to the database.
3879             *
3880             * @param emailAddressId the primary key for the new email address
3881             * @return the new email address
3882             */
3883            @Override
3884            public EmailAddress create(long emailAddressId) {
3885                    EmailAddress emailAddress = new EmailAddressImpl();
3886    
3887                    emailAddress.setNew(true);
3888                    emailAddress.setPrimaryKey(emailAddressId);
3889    
3890                    String uuid = PortalUUIDUtil.generate();
3891    
3892                    emailAddress.setUuid(uuid);
3893    
3894                    return emailAddress;
3895            }
3896    
3897            /**
3898             * Removes the email address with the primary key from the database. Also notifies the appropriate model listeners.
3899             *
3900             * @param emailAddressId the primary key of the email address
3901             * @return the email address that was removed
3902             * @throws NoSuchEmailAddressException if a email address with the primary key could not be found
3903             */
3904            @Override
3905            public EmailAddress remove(long emailAddressId)
3906                    throws NoSuchEmailAddressException {
3907                    return remove((Serializable)emailAddressId);
3908            }
3909    
3910            /**
3911             * Removes the email address with the primary key from the database. Also notifies the appropriate model listeners.
3912             *
3913             * @param primaryKey the primary key of the email address
3914             * @return the email address that was removed
3915             * @throws NoSuchEmailAddressException if a email address with the primary key could not be found
3916             */
3917            @Override
3918            public EmailAddress remove(Serializable primaryKey)
3919                    throws NoSuchEmailAddressException {
3920                    Session session = null;
3921    
3922                    try {
3923                            session = openSession();
3924    
3925                            EmailAddress emailAddress = (EmailAddress)session.get(EmailAddressImpl.class,
3926                                            primaryKey);
3927    
3928                            if (emailAddress == null) {
3929                                    if (_log.isWarnEnabled()) {
3930                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
3931                                    }
3932    
3933                                    throw new NoSuchEmailAddressException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
3934                                            primaryKey);
3935                            }
3936    
3937                            return remove(emailAddress);
3938                    }
3939                    catch (NoSuchEmailAddressException nsee) {
3940                            throw nsee;
3941                    }
3942                    catch (Exception e) {
3943                            throw processException(e);
3944                    }
3945                    finally {
3946                            closeSession(session);
3947                    }
3948            }
3949    
3950            @Override
3951            protected EmailAddress removeImpl(EmailAddress emailAddress) {
3952                    emailAddress = toUnwrappedModel(emailAddress);
3953    
3954                    Session session = null;
3955    
3956                    try {
3957                            session = openSession();
3958    
3959                            if (!session.contains(emailAddress)) {
3960                                    emailAddress = (EmailAddress)session.get(EmailAddressImpl.class,
3961                                                    emailAddress.getPrimaryKeyObj());
3962                            }
3963    
3964                            if (emailAddress != null) {
3965                                    session.delete(emailAddress);
3966                            }
3967                    }
3968                    catch (Exception e) {
3969                            throw processException(e);
3970                    }
3971                    finally {
3972                            closeSession(session);
3973                    }
3974    
3975                    if (emailAddress != null) {
3976                            clearCache(emailAddress);
3977                    }
3978    
3979                    return emailAddress;
3980            }
3981    
3982            @Override
3983            public EmailAddress updateImpl(EmailAddress emailAddress) {
3984                    emailAddress = toUnwrappedModel(emailAddress);
3985    
3986                    boolean isNew = emailAddress.isNew();
3987    
3988                    EmailAddressModelImpl emailAddressModelImpl = (EmailAddressModelImpl)emailAddress;
3989    
3990                    if (Validator.isNull(emailAddress.getUuid())) {
3991                            String uuid = PortalUUIDUtil.generate();
3992    
3993                            emailAddress.setUuid(uuid);
3994                    }
3995    
3996                    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
3997    
3998                    Date now = new Date();
3999    
4000                    if (isNew && (emailAddress.getCreateDate() == null)) {
4001                            if (serviceContext == null) {
4002                                    emailAddress.setCreateDate(now);
4003                            }
4004                            else {
4005                                    emailAddress.setCreateDate(serviceContext.getCreateDate(now));
4006                            }
4007                    }
4008    
4009                    if (!emailAddressModelImpl.hasSetModifiedDate()) {
4010                            if (serviceContext == null) {
4011                                    emailAddress.setModifiedDate(now);
4012                            }
4013                            else {
4014                                    emailAddress.setModifiedDate(serviceContext.getModifiedDate(now));
4015                            }
4016                    }
4017    
4018                    Session session = null;
4019    
4020                    try {
4021                            session = openSession();
4022    
4023                            if (emailAddress.isNew()) {
4024                                    session.save(emailAddress);
4025    
4026                                    emailAddress.setNew(false);
4027                            }
4028                            else {
4029                                    session.merge(emailAddress);
4030                            }
4031                    }
4032                    catch (Exception e) {
4033                            throw processException(e);
4034                    }
4035                    finally {
4036                            closeSession(session);
4037                    }
4038    
4039                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4040    
4041                    if (isNew || !EmailAddressModelImpl.COLUMN_BITMASK_ENABLED) {
4042                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4043                    }
4044    
4045                    else {
4046                            if ((emailAddressModelImpl.getColumnBitmask() &
4047                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID.getColumnBitmask()) != 0) {
4048                                    Object[] args = new Object[] {
4049                                                    emailAddressModelImpl.getOriginalUuid()
4050                                            };
4051    
4052                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
4053                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
4054                                            args);
4055    
4056                                    args = new Object[] { emailAddressModelImpl.getUuid() };
4057    
4058                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
4059                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
4060                                            args);
4061                            }
4062    
4063                            if ((emailAddressModelImpl.getColumnBitmask() &
4064                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C.getColumnBitmask()) != 0) {
4065                                    Object[] args = new Object[] {
4066                                                    emailAddressModelImpl.getOriginalUuid(),
4067                                                    emailAddressModelImpl.getOriginalCompanyId()
4068                                            };
4069    
4070                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
4071                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
4072                                            args);
4073    
4074                                    args = new Object[] {
4075                                                    emailAddressModelImpl.getUuid(),
4076                                                    emailAddressModelImpl.getCompanyId()
4077                                            };
4078    
4079                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
4080                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
4081                                            args);
4082                            }
4083    
4084                            if ((emailAddressModelImpl.getColumnBitmask() &
4085                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID.getColumnBitmask()) != 0) {
4086                                    Object[] args = new Object[] {
4087                                                    emailAddressModelImpl.getOriginalCompanyId()
4088                                            };
4089    
4090                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPANYID,
4091                                            args);
4092                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
4093                                            args);
4094    
4095                                    args = new Object[] { emailAddressModelImpl.getCompanyId() };
4096    
4097                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPANYID,
4098                                            args);
4099                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
4100                                            args);
4101                            }
4102    
4103                            if ((emailAddressModelImpl.getColumnBitmask() &
4104                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID.getColumnBitmask()) != 0) {
4105                                    Object[] args = new Object[] {
4106                                                    emailAddressModelImpl.getOriginalUserId()
4107                                            };
4108    
4109                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
4110                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID,
4111                                            args);
4112    
4113                                    args = new Object[] { emailAddressModelImpl.getUserId() };
4114    
4115                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
4116                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID,
4117                                            args);
4118                            }
4119    
4120                            if ((emailAddressModelImpl.getColumnBitmask() &
4121                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C.getColumnBitmask()) != 0) {
4122                                    Object[] args = new Object[] {
4123                                                    emailAddressModelImpl.getOriginalCompanyId(),
4124                                                    emailAddressModelImpl.getOriginalClassNameId()
4125                                            };
4126    
4127                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
4128                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C,
4129                                            args);
4130    
4131                                    args = new Object[] {
4132                                                    emailAddressModelImpl.getCompanyId(),
4133                                                    emailAddressModelImpl.getClassNameId()
4134                                            };
4135    
4136                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
4137                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C,
4138                                            args);
4139                            }
4140    
4141                            if ((emailAddressModelImpl.getColumnBitmask() &
4142                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C.getColumnBitmask()) != 0) {
4143                                    Object[] args = new Object[] {
4144                                                    emailAddressModelImpl.getOriginalCompanyId(),
4145                                                    emailAddressModelImpl.getOriginalClassNameId(),
4146                                                    emailAddressModelImpl.getOriginalClassPK()
4147                                            };
4148    
4149                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C_C, args);
4150                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C,
4151                                            args);
4152    
4153                                    args = new Object[] {
4154                                                    emailAddressModelImpl.getCompanyId(),
4155                                                    emailAddressModelImpl.getClassNameId(),
4156                                                    emailAddressModelImpl.getClassPK()
4157                                            };
4158    
4159                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C_C, args);
4160                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C,
4161                                            args);
4162                            }
4163    
4164                            if ((emailAddressModelImpl.getColumnBitmask() &
4165                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_P.getColumnBitmask()) != 0) {
4166                                    Object[] args = new Object[] {
4167                                                    emailAddressModelImpl.getOriginalCompanyId(),
4168                                                    emailAddressModelImpl.getOriginalClassNameId(),
4169                                                    emailAddressModelImpl.getOriginalClassPK(),
4170                                                    emailAddressModelImpl.getOriginalPrimary()
4171                                            };
4172    
4173                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C_C_P, args);
4174                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_P,
4175                                            args);
4176    
4177                                    args = new Object[] {
4178                                                    emailAddressModelImpl.getCompanyId(),
4179                                                    emailAddressModelImpl.getClassNameId(),
4180                                                    emailAddressModelImpl.getClassPK(),
4181                                                    emailAddressModelImpl.getPrimary()
4182                                            };
4183    
4184                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C_C_P, args);
4185                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_P,
4186                                            args);
4187                            }
4188                    }
4189    
4190                    EntityCacheUtil.putResult(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
4191                            EmailAddressImpl.class, emailAddress.getPrimaryKey(), emailAddress,
4192                            false);
4193    
4194                    emailAddress.resetOriginalValues();
4195    
4196                    return emailAddress;
4197            }
4198    
4199            protected EmailAddress toUnwrappedModel(EmailAddress emailAddress) {
4200                    if (emailAddress instanceof EmailAddressImpl) {
4201                            return emailAddress;
4202                    }
4203    
4204                    EmailAddressImpl emailAddressImpl = new EmailAddressImpl();
4205    
4206                    emailAddressImpl.setNew(emailAddress.isNew());
4207                    emailAddressImpl.setPrimaryKey(emailAddress.getPrimaryKey());
4208    
4209                    emailAddressImpl.setMvccVersion(emailAddress.getMvccVersion());
4210                    emailAddressImpl.setUuid(emailAddress.getUuid());
4211                    emailAddressImpl.setEmailAddressId(emailAddress.getEmailAddressId());
4212                    emailAddressImpl.setCompanyId(emailAddress.getCompanyId());
4213                    emailAddressImpl.setUserId(emailAddress.getUserId());
4214                    emailAddressImpl.setUserName(emailAddress.getUserName());
4215                    emailAddressImpl.setCreateDate(emailAddress.getCreateDate());
4216                    emailAddressImpl.setModifiedDate(emailAddress.getModifiedDate());
4217                    emailAddressImpl.setClassNameId(emailAddress.getClassNameId());
4218                    emailAddressImpl.setClassPK(emailAddress.getClassPK());
4219                    emailAddressImpl.setAddress(emailAddress.getAddress());
4220                    emailAddressImpl.setTypeId(emailAddress.getTypeId());
4221                    emailAddressImpl.setPrimary(emailAddress.isPrimary());
4222    
4223                    return emailAddressImpl;
4224            }
4225    
4226            /**
4227             * Returns the email address with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
4228             *
4229             * @param primaryKey the primary key of the email address
4230             * @return the email address
4231             * @throws NoSuchEmailAddressException if a email address with the primary key could not be found
4232             */
4233            @Override
4234            public EmailAddress findByPrimaryKey(Serializable primaryKey)
4235                    throws NoSuchEmailAddressException {
4236                    EmailAddress emailAddress = fetchByPrimaryKey(primaryKey);
4237    
4238                    if (emailAddress == null) {
4239                            if (_log.isWarnEnabled()) {
4240                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
4241                            }
4242    
4243                            throw new NoSuchEmailAddressException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
4244                                    primaryKey);
4245                    }
4246    
4247                    return emailAddress;
4248            }
4249    
4250            /**
4251             * Returns the email address with the primary key or throws a {@link NoSuchEmailAddressException} if it could not be found.
4252             *
4253             * @param emailAddressId the primary key of the email address
4254             * @return the email address
4255             * @throws NoSuchEmailAddressException if a email address with the primary key could not be found
4256             */
4257            @Override
4258            public EmailAddress findByPrimaryKey(long emailAddressId)
4259                    throws NoSuchEmailAddressException {
4260                    return findByPrimaryKey((Serializable)emailAddressId);
4261            }
4262    
4263            /**
4264             * Returns the email address with the primary key or returns <code>null</code> if it could not be found.
4265             *
4266             * @param primaryKey the primary key of the email address
4267             * @return the email address, or <code>null</code> if a email address with the primary key could not be found
4268             */
4269            @Override
4270            public EmailAddress fetchByPrimaryKey(Serializable primaryKey) {
4271                    EmailAddress emailAddress = (EmailAddress)EntityCacheUtil.getResult(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
4272                                    EmailAddressImpl.class, primaryKey);
4273    
4274                    if (emailAddress == _nullEmailAddress) {
4275                            return null;
4276                    }
4277    
4278                    if (emailAddress == null) {
4279                            Session session = null;
4280    
4281                            try {
4282                                    session = openSession();
4283    
4284                                    emailAddress = (EmailAddress)session.get(EmailAddressImpl.class,
4285                                                    primaryKey);
4286    
4287                                    if (emailAddress != null) {
4288                                            cacheResult(emailAddress);
4289                                    }
4290                                    else {
4291                                            EntityCacheUtil.putResult(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
4292                                                    EmailAddressImpl.class, primaryKey, _nullEmailAddress);
4293                                    }
4294                            }
4295                            catch (Exception e) {
4296                                    EntityCacheUtil.removeResult(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
4297                                            EmailAddressImpl.class, primaryKey);
4298    
4299                                    throw processException(e);
4300                            }
4301                            finally {
4302                                    closeSession(session);
4303                            }
4304                    }
4305    
4306                    return emailAddress;
4307            }
4308    
4309            /**
4310             * Returns the email address with the primary key or returns <code>null</code> if it could not be found.
4311             *
4312             * @param emailAddressId the primary key of the email address
4313             * @return the email address, or <code>null</code> if a email address with the primary key could not be found
4314             */
4315            @Override
4316            public EmailAddress fetchByPrimaryKey(long emailAddressId) {
4317                    return fetchByPrimaryKey((Serializable)emailAddressId);
4318            }
4319    
4320            @Override
4321            public Map<Serializable, EmailAddress> fetchByPrimaryKeys(
4322                    Set<Serializable> primaryKeys) {
4323                    if (primaryKeys.isEmpty()) {
4324                            return Collections.emptyMap();
4325                    }
4326    
4327                    Map<Serializable, EmailAddress> map = new HashMap<Serializable, EmailAddress>();
4328    
4329                    if (primaryKeys.size() == 1) {
4330                            Iterator<Serializable> iterator = primaryKeys.iterator();
4331    
4332                            Serializable primaryKey = iterator.next();
4333    
4334                            EmailAddress emailAddress = fetchByPrimaryKey(primaryKey);
4335    
4336                            if (emailAddress != null) {
4337                                    map.put(primaryKey, emailAddress);
4338                            }
4339    
4340                            return map;
4341                    }
4342    
4343                    Set<Serializable> uncachedPrimaryKeys = null;
4344    
4345                    for (Serializable primaryKey : primaryKeys) {
4346                            EmailAddress emailAddress = (EmailAddress)EntityCacheUtil.getResult(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
4347                                            EmailAddressImpl.class, primaryKey);
4348    
4349                            if (emailAddress == null) {
4350                                    if (uncachedPrimaryKeys == null) {
4351                                            uncachedPrimaryKeys = new HashSet<Serializable>();
4352                                    }
4353    
4354                                    uncachedPrimaryKeys.add(primaryKey);
4355                            }
4356                            else {
4357                                    map.put(primaryKey, emailAddress);
4358                            }
4359                    }
4360    
4361                    if (uncachedPrimaryKeys == null) {
4362                            return map;
4363                    }
4364    
4365                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
4366                                    1);
4367    
4368                    query.append(_SQL_SELECT_EMAILADDRESS_WHERE_PKS_IN);
4369    
4370                    for (Serializable primaryKey : uncachedPrimaryKeys) {
4371                            query.append(String.valueOf(primaryKey));
4372    
4373                            query.append(StringPool.COMMA);
4374                    }
4375    
4376                    query.setIndex(query.index() - 1);
4377    
4378                    query.append(StringPool.CLOSE_PARENTHESIS);
4379    
4380                    String sql = query.toString();
4381    
4382                    Session session = null;
4383    
4384                    try {
4385                            session = openSession();
4386    
4387                            Query q = session.createQuery(sql);
4388    
4389                            for (EmailAddress emailAddress : (List<EmailAddress>)q.list()) {
4390                                    map.put(emailAddress.getPrimaryKeyObj(), emailAddress);
4391    
4392                                    cacheResult(emailAddress);
4393    
4394                                    uncachedPrimaryKeys.remove(emailAddress.getPrimaryKeyObj());
4395                            }
4396    
4397                            for (Serializable primaryKey : uncachedPrimaryKeys) {
4398                                    EntityCacheUtil.putResult(EmailAddressModelImpl.ENTITY_CACHE_ENABLED,
4399                                            EmailAddressImpl.class, primaryKey, _nullEmailAddress);
4400                            }
4401                    }
4402                    catch (Exception e) {
4403                            throw processException(e);
4404                    }
4405                    finally {
4406                            closeSession(session);
4407                    }
4408    
4409                    return map;
4410            }
4411    
4412            /**
4413             * Returns all the email addresses.
4414             *
4415             * @return the email addresses
4416             */
4417            @Override
4418            public List<EmailAddress> findAll() {
4419                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
4420            }
4421    
4422            /**
4423             * Returns a range of all the email addresses.
4424             *
4425             * <p>
4426             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
4427             * </p>
4428             *
4429             * @param start the lower bound of the range of email addresses
4430             * @param end the upper bound of the range of email addresses (not inclusive)
4431             * @return the range of email addresses
4432             */
4433            @Override
4434            public List<EmailAddress> findAll(int start, int end) {
4435                    return findAll(start, end, null);
4436            }
4437    
4438            /**
4439             * Returns an ordered range of all the email addresses.
4440             *
4441             * <p>
4442             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link EmailAddressModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
4443             * </p>
4444             *
4445             * @param start the lower bound of the range of email addresses
4446             * @param end the upper bound of the range of email addresses (not inclusive)
4447             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
4448             * @return the ordered range of email addresses
4449             */
4450            @Override
4451            public List<EmailAddress> findAll(int start, int end,
4452                    OrderByComparator<EmailAddress> orderByComparator) {
4453                    boolean pagination = true;
4454                    FinderPath finderPath = null;
4455                    Object[] finderArgs = null;
4456    
4457                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
4458                                    (orderByComparator == null)) {
4459                            pagination = false;
4460                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
4461                            finderArgs = FINDER_ARGS_EMPTY;
4462                    }
4463                    else {
4464                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
4465                            finderArgs = new Object[] { start, end, orderByComparator };
4466                    }
4467    
4468                    List<EmailAddress> list = (List<EmailAddress>)FinderCacheUtil.getResult(finderPath,
4469                                    finderArgs, this);
4470    
4471                    if (list == null) {
4472                            StringBundler query = null;
4473                            String sql = null;
4474    
4475                            if (orderByComparator != null) {
4476                                    query = new StringBundler(2 +
4477                                                    (orderByComparator.getOrderByFields().length * 3));
4478    
4479                                    query.append(_SQL_SELECT_EMAILADDRESS);
4480    
4481                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
4482                                            orderByComparator);
4483    
4484                                    sql = query.toString();
4485                            }
4486                            else {
4487                                    sql = _SQL_SELECT_EMAILADDRESS;
4488    
4489                                    if (pagination) {
4490                                            sql = sql.concat(EmailAddressModelImpl.ORDER_BY_JPQL);
4491                                    }
4492                            }
4493    
4494                            Session session = null;
4495    
4496                            try {
4497                                    session = openSession();
4498    
4499                                    Query q = session.createQuery(sql);
4500    
4501                                    if (!pagination) {
4502                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
4503                                                            start, end, false);
4504    
4505                                            Collections.sort(list);
4506    
4507                                            list = Collections.unmodifiableList(list);
4508                                    }
4509                                    else {
4510                                            list = (List<EmailAddress>)QueryUtil.list(q, getDialect(),
4511                                                            start, end);
4512                                    }
4513    
4514                                    cacheResult(list);
4515    
4516                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
4517                            }
4518                            catch (Exception e) {
4519                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
4520    
4521                                    throw processException(e);
4522                            }
4523                            finally {
4524                                    closeSession(session);
4525                            }
4526                    }
4527    
4528                    return list;
4529            }
4530    
4531            /**
4532             * Removes all the email addresses from the database.
4533             *
4534             */
4535            @Override
4536            public void removeAll() {
4537                    for (EmailAddress emailAddress : findAll()) {
4538                            remove(emailAddress);
4539                    }
4540            }
4541    
4542            /**
4543             * Returns the number of email addresses.
4544             *
4545             * @return the number of email addresses
4546             */
4547            @Override
4548            public int countAll() {
4549                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
4550                                    FINDER_ARGS_EMPTY, this);
4551    
4552                    if (count == null) {
4553                            Session session = null;
4554    
4555                            try {
4556                                    session = openSession();
4557    
4558                                    Query q = session.createQuery(_SQL_COUNT_EMAILADDRESS);
4559    
4560                                    count = (Long)q.uniqueResult();
4561    
4562                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
4563                                            FINDER_ARGS_EMPTY, count);
4564                            }
4565                            catch (Exception e) {
4566                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
4567                                            FINDER_ARGS_EMPTY);
4568    
4569                                    throw processException(e);
4570                            }
4571                            finally {
4572                                    closeSession(session);
4573                            }
4574                    }
4575    
4576                    return count.intValue();
4577            }
4578    
4579            @Override
4580            protected Set<String> getBadColumnNames() {
4581                    return _badColumnNames;
4582            }
4583    
4584            /**
4585             * Initializes the email address persistence.
4586             */
4587            public void afterPropertiesSet() {
4588            }
4589    
4590            public void destroy() {
4591                    EntityCacheUtil.removeCache(EmailAddressImpl.class.getName());
4592                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
4593                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4594                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4595            }
4596    
4597            private static final String _SQL_SELECT_EMAILADDRESS = "SELECT emailAddress FROM EmailAddress emailAddress";
4598            private static final String _SQL_SELECT_EMAILADDRESS_WHERE_PKS_IN = "SELECT emailAddress FROM EmailAddress emailAddress WHERE emailAddressId IN (";
4599            private static final String _SQL_SELECT_EMAILADDRESS_WHERE = "SELECT emailAddress FROM EmailAddress emailAddress WHERE ";
4600            private static final String _SQL_COUNT_EMAILADDRESS = "SELECT COUNT(emailAddress) FROM EmailAddress emailAddress";
4601            private static final String _SQL_COUNT_EMAILADDRESS_WHERE = "SELECT COUNT(emailAddress) FROM EmailAddress emailAddress WHERE ";
4602            private static final String _ORDER_BY_ENTITY_ALIAS = "emailAddress.";
4603            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No EmailAddress exists with the primary key ";
4604            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No EmailAddress exists with the key {";
4605            private static final Log _log = LogFactoryUtil.getLog(EmailAddressPersistenceImpl.class);
4606            private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
4607                                    "uuid", "primary"
4608                            });
4609            private static final EmailAddress _nullEmailAddress = new EmailAddressImpl() {
4610                            @Override
4611                            public Object clone() {
4612                                    return this;
4613                            }
4614    
4615                            @Override
4616                            public CacheModel<EmailAddress> toCacheModel() {
4617                                    return _nullEmailAddressCacheModel;
4618                            }
4619                    };
4620    
4621            private static final CacheModel<EmailAddress> _nullEmailAddressCacheModel = new NullCacheModel();
4622    
4623            private static class NullCacheModel implements CacheModel<EmailAddress>,
4624                    MVCCModel {
4625                    @Override
4626                    public long getMvccVersion() {
4627                            return -1;
4628                    }
4629    
4630                    @Override
4631                    public void setMvccVersion(long mvccVersion) {
4632                    }
4633    
4634                    @Override
4635                    public EmailAddress toEntityModel() {
4636                            return _nullEmailAddress;
4637                    }
4638            }
4639    }