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