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