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