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