001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.exception.NoSuchContactException;
020    import com.liferay.portal.kernel.bean.BeanReference;
021    import com.liferay.portal.kernel.dao.orm.EntityCache;
022    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
023    import com.liferay.portal.kernel.dao.orm.FinderCache;
024    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
025    import com.liferay.portal.kernel.dao.orm.FinderPath;
026    import com.liferay.portal.kernel.dao.orm.Query;
027    import com.liferay.portal.kernel.dao.orm.QueryPos;
028    import com.liferay.portal.kernel.dao.orm.QueryUtil;
029    import com.liferay.portal.kernel.dao.orm.Session;
030    import com.liferay.portal.kernel.log.Log;
031    import com.liferay.portal.kernel.log.LogFactoryUtil;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.model.CacheModel;
036    import com.liferay.portal.model.Contact;
037    import com.liferay.portal.model.MVCCModel;
038    import com.liferay.portal.model.impl.ContactImpl;
039    import com.liferay.portal.model.impl.ContactModelImpl;
040    import com.liferay.portal.service.ServiceContext;
041    import com.liferay.portal.service.ServiceContextThreadLocal;
042    import com.liferay.portal.service.persistence.CompanyProvider;
043    import com.liferay.portal.service.persistence.CompanyProviderWrapper;
044    import com.liferay.portal.service.persistence.ContactPersistence;
045    
046    import java.io.Serializable;
047    
048    import java.util.Collections;
049    import java.util.Date;
050    import java.util.HashMap;
051    import java.util.HashSet;
052    import java.util.Iterator;
053    import java.util.List;
054    import java.util.Map;
055    import java.util.Set;
056    
057    /**
058     * The persistence implementation for the contact service.
059     *
060     * <p>
061     * Caching information and settings can be found in <code>portal.properties</code>
062     * </p>
063     *
064     * @author Brian Wing Shun Chan
065     * @see ContactPersistence
066     * @see com.liferay.portal.service.persistence.ContactUtil
067     * @generated
068     */
069    @ProviderType
070    public class ContactPersistenceImpl extends BasePersistenceImpl<Contact>
071            implements ContactPersistence {
072            /*
073             * NOTE FOR DEVELOPERS:
074             *
075             * Never modify or reference this class directly. Always use {@link ContactUtil} to access the contact persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
076             */
077            public static final String FINDER_CLASS_NAME_ENTITY = ContactImpl.class.getName();
078            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
079                    ".List1";
080            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
081                    ".List2";
082            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
083                            ContactModelImpl.FINDER_CACHE_ENABLED, ContactImpl.class,
084                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
085            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
086                            ContactModelImpl.FINDER_CACHE_ENABLED, ContactImpl.class,
087                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
088            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
089                            ContactModelImpl.FINDER_CACHE_ENABLED, Long.class,
090                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
091            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID =
092                    new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
093                            ContactModelImpl.FINDER_CACHE_ENABLED, ContactImpl.class,
094                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByCompanyId",
095                            new String[] {
096                                    Long.class.getName(),
097                                    
098                            Integer.class.getName(), Integer.class.getName(),
099                                    OrderByComparator.class.getName()
100                            });
101            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID =
102                    new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
103                            ContactModelImpl.FINDER_CACHE_ENABLED, ContactImpl.class,
104                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByCompanyId",
105                            new String[] { Long.class.getName() },
106                            ContactModelImpl.COMPANYID_COLUMN_BITMASK);
107            public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
108                            ContactModelImpl.FINDER_CACHE_ENABLED, Long.class,
109                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCompanyId",
110                            new String[] { Long.class.getName() });
111    
112            /**
113             * Returns all the contacts where companyId = &#63;.
114             *
115             * @param companyId the company ID
116             * @return the matching contacts
117             */
118            @Override
119            public List<Contact> findByCompanyId(long companyId) {
120                    return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
121                            null);
122            }
123    
124            /**
125             * Returns a range of all the contacts where companyId = &#63;.
126             *
127             * <p>
128             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ContactModelImpl}. 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.
129             * </p>
130             *
131             * @param companyId the company ID
132             * @param start the lower bound of the range of contacts
133             * @param end the upper bound of the range of contacts (not inclusive)
134             * @return the range of matching contacts
135             */
136            @Override
137            public List<Contact> findByCompanyId(long companyId, int start, int end) {
138                    return findByCompanyId(companyId, start, end, null);
139            }
140    
141            /**
142             * Returns an ordered range of all the contacts where companyId = &#63;.
143             *
144             * <p>
145             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ContactModelImpl}. 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.
146             * </p>
147             *
148             * @param companyId the company ID
149             * @param start the lower bound of the range of contacts
150             * @param end the upper bound of the range of contacts (not inclusive)
151             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
152             * @return the ordered range of matching contacts
153             */
154            @Override
155            public List<Contact> findByCompanyId(long companyId, int start, int end,
156                    OrderByComparator<Contact> orderByComparator) {
157                    return findByCompanyId(companyId, start, end, orderByComparator, true);
158            }
159    
160            /**
161             * Returns an ordered range of all the contacts where companyId = &#63;.
162             *
163             * <p>
164             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ContactModelImpl}. 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.
165             * </p>
166             *
167             * @param companyId the company ID
168             * @param start the lower bound of the range of contacts
169             * @param end the upper bound of the range of contacts (not inclusive)
170             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
171             * @param retrieveFromCache whether to retrieve from the finder cache
172             * @return the ordered range of matching contacts
173             */
174            @Override
175            public List<Contact> findByCompanyId(long companyId, int start, int end,
176                    OrderByComparator<Contact> orderByComparator, boolean retrieveFromCache) {
177                    boolean pagination = true;
178                    FinderPath finderPath = null;
179                    Object[] finderArgs = null;
180    
181                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
182                                    (orderByComparator == null)) {
183                            pagination = false;
184                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID;
185                            finderArgs = new Object[] { companyId };
186                    }
187                    else {
188                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID;
189                            finderArgs = new Object[] { companyId, start, end, orderByComparator };
190                    }
191    
192                    List<Contact> list = null;
193    
194                    if (retrieveFromCache) {
195                            list = (List<Contact>)finderCache.getResult(finderPath, finderArgs,
196                                            this);
197    
198                            if ((list != null) && !list.isEmpty()) {
199                                    for (Contact contact : list) {
200                                            if ((companyId != contact.getCompanyId())) {
201                                                    list = null;
202    
203                                                    break;
204                                            }
205                                    }
206                            }
207                    }
208    
209                    if (list == null) {
210                            StringBundler query = null;
211    
212                            if (orderByComparator != null) {
213                                    query = new StringBundler(3 +
214                                                    (orderByComparator.getOrderByFields().length * 3));
215                            }
216                            else {
217                                    query = new StringBundler(3);
218                            }
219    
220                            query.append(_SQL_SELECT_CONTACT_WHERE);
221    
222                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
223    
224                            if (orderByComparator != null) {
225                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
226                                            orderByComparator);
227                            }
228                            else
229                             if (pagination) {
230                                    query.append(ContactModelImpl.ORDER_BY_JPQL);
231                            }
232    
233                            String sql = query.toString();
234    
235                            Session session = null;
236    
237                            try {
238                                    session = openSession();
239    
240                                    Query q = session.createQuery(sql);
241    
242                                    QueryPos qPos = QueryPos.getInstance(q);
243    
244                                    qPos.add(companyId);
245    
246                                    if (!pagination) {
247                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
248                                                            start, end, false);
249    
250                                            Collections.sort(list);
251    
252                                            list = Collections.unmodifiableList(list);
253                                    }
254                                    else {
255                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
256                                                            start, end);
257                                    }
258    
259                                    cacheResult(list);
260    
261                                    finderCache.putResult(finderPath, finderArgs, list);
262                            }
263                            catch (Exception e) {
264                                    finderCache.removeResult(finderPath, finderArgs);
265    
266                                    throw processException(e);
267                            }
268                            finally {
269                                    closeSession(session);
270                            }
271                    }
272    
273                    return list;
274            }
275    
276            /**
277             * Returns the first contact in the ordered set where companyId = &#63;.
278             *
279             * @param companyId the company ID
280             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
281             * @return the first matching contact
282             * @throws NoSuchContactException if a matching contact could not be found
283             */
284            @Override
285            public Contact findByCompanyId_First(long companyId,
286                    OrderByComparator<Contact> orderByComparator)
287                    throws NoSuchContactException {
288                    Contact contact = fetchByCompanyId_First(companyId, orderByComparator);
289    
290                    if (contact != null) {
291                            return contact;
292                    }
293    
294                    StringBundler msg = new StringBundler(4);
295    
296                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
297    
298                    msg.append("companyId=");
299                    msg.append(companyId);
300    
301                    msg.append(StringPool.CLOSE_CURLY_BRACE);
302    
303                    throw new NoSuchContactException(msg.toString());
304            }
305    
306            /**
307             * Returns the first contact in the ordered set where companyId = &#63;.
308             *
309             * @param companyId the company ID
310             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
311             * @return the first matching contact, or <code>null</code> if a matching contact could not be found
312             */
313            @Override
314            public Contact fetchByCompanyId_First(long companyId,
315                    OrderByComparator<Contact> orderByComparator) {
316                    List<Contact> list = findByCompanyId(companyId, 0, 1, orderByComparator);
317    
318                    if (!list.isEmpty()) {
319                            return list.get(0);
320                    }
321    
322                    return null;
323            }
324    
325            /**
326             * Returns the last contact in the ordered set where companyId = &#63;.
327             *
328             * @param companyId the company ID
329             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
330             * @return the last matching contact
331             * @throws NoSuchContactException if a matching contact could not be found
332             */
333            @Override
334            public Contact findByCompanyId_Last(long companyId,
335                    OrderByComparator<Contact> orderByComparator)
336                    throws NoSuchContactException {
337                    Contact contact = fetchByCompanyId_Last(companyId, orderByComparator);
338    
339                    if (contact != null) {
340                            return contact;
341                    }
342    
343                    StringBundler msg = new StringBundler(4);
344    
345                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
346    
347                    msg.append("companyId=");
348                    msg.append(companyId);
349    
350                    msg.append(StringPool.CLOSE_CURLY_BRACE);
351    
352                    throw new NoSuchContactException(msg.toString());
353            }
354    
355            /**
356             * Returns the last contact in the ordered set where companyId = &#63;.
357             *
358             * @param companyId the company ID
359             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
360             * @return the last matching contact, or <code>null</code> if a matching contact could not be found
361             */
362            @Override
363            public Contact fetchByCompanyId_Last(long companyId,
364                    OrderByComparator<Contact> orderByComparator) {
365                    int count = countByCompanyId(companyId);
366    
367                    if (count == 0) {
368                            return null;
369                    }
370    
371                    List<Contact> list = findByCompanyId(companyId, count - 1, count,
372                                    orderByComparator);
373    
374                    if (!list.isEmpty()) {
375                            return list.get(0);
376                    }
377    
378                    return null;
379            }
380    
381            /**
382             * Returns the contacts before and after the current contact in the ordered set where companyId = &#63;.
383             *
384             * @param contactId the primary key of the current contact
385             * @param companyId the company ID
386             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
387             * @return the previous, current, and next contact
388             * @throws NoSuchContactException if a contact with the primary key could not be found
389             */
390            @Override
391            public Contact[] findByCompanyId_PrevAndNext(long contactId,
392                    long companyId, OrderByComparator<Contact> orderByComparator)
393                    throws NoSuchContactException {
394                    Contact contact = findByPrimaryKey(contactId);
395    
396                    Session session = null;
397    
398                    try {
399                            session = openSession();
400    
401                            Contact[] array = new ContactImpl[3];
402    
403                            array[0] = getByCompanyId_PrevAndNext(session, contact, companyId,
404                                            orderByComparator, true);
405    
406                            array[1] = contact;
407    
408                            array[2] = getByCompanyId_PrevAndNext(session, contact, companyId,
409                                            orderByComparator, false);
410    
411                            return array;
412                    }
413                    catch (Exception e) {
414                            throw processException(e);
415                    }
416                    finally {
417                            closeSession(session);
418                    }
419            }
420    
421            protected Contact getByCompanyId_PrevAndNext(Session session,
422                    Contact contact, long companyId,
423                    OrderByComparator<Contact> orderByComparator, boolean previous) {
424                    StringBundler query = null;
425    
426                    if (orderByComparator != null) {
427                            query = new StringBundler(6 +
428                                            (orderByComparator.getOrderByFields().length * 6));
429                    }
430                    else {
431                            query = new StringBundler(3);
432                    }
433    
434                    query.append(_SQL_SELECT_CONTACT_WHERE);
435    
436                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
437    
438                    if (orderByComparator != null) {
439                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
440    
441                            if (orderByConditionFields.length > 0) {
442                                    query.append(WHERE_AND);
443                            }
444    
445                            for (int i = 0; i < orderByConditionFields.length; i++) {
446                                    query.append(_ORDER_BY_ENTITY_ALIAS);
447                                    query.append(orderByConditionFields[i]);
448    
449                                    if ((i + 1) < orderByConditionFields.length) {
450                                            if (orderByComparator.isAscending() ^ previous) {
451                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
452                                            }
453                                            else {
454                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
455                                            }
456                                    }
457                                    else {
458                                            if (orderByComparator.isAscending() ^ previous) {
459                                                    query.append(WHERE_GREATER_THAN);
460                                            }
461                                            else {
462                                                    query.append(WHERE_LESSER_THAN);
463                                            }
464                                    }
465                            }
466    
467                            query.append(ORDER_BY_CLAUSE);
468    
469                            String[] orderByFields = orderByComparator.getOrderByFields();
470    
471                            for (int i = 0; i < orderByFields.length; i++) {
472                                    query.append(_ORDER_BY_ENTITY_ALIAS);
473                                    query.append(orderByFields[i]);
474    
475                                    if ((i + 1) < orderByFields.length) {
476                                            if (orderByComparator.isAscending() ^ previous) {
477                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
478                                            }
479                                            else {
480                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
481                                            }
482                                    }
483                                    else {
484                                            if (orderByComparator.isAscending() ^ previous) {
485                                                    query.append(ORDER_BY_ASC);
486                                            }
487                                            else {
488                                                    query.append(ORDER_BY_DESC);
489                                            }
490                                    }
491                            }
492                    }
493                    else {
494                            query.append(ContactModelImpl.ORDER_BY_JPQL);
495                    }
496    
497                    String sql = query.toString();
498    
499                    Query q = session.createQuery(sql);
500    
501                    q.setFirstResult(0);
502                    q.setMaxResults(2);
503    
504                    QueryPos qPos = QueryPos.getInstance(q);
505    
506                    qPos.add(companyId);
507    
508                    if (orderByComparator != null) {
509                            Object[] values = orderByComparator.getOrderByConditionValues(contact);
510    
511                            for (Object value : values) {
512                                    qPos.add(value);
513                            }
514                    }
515    
516                    List<Contact> list = q.list();
517    
518                    if (list.size() == 2) {
519                            return list.get(1);
520                    }
521                    else {
522                            return null;
523                    }
524            }
525    
526            /**
527             * Removes all the contacts where companyId = &#63; from the database.
528             *
529             * @param companyId the company ID
530             */
531            @Override
532            public void removeByCompanyId(long companyId) {
533                    for (Contact contact : findByCompanyId(companyId, QueryUtil.ALL_POS,
534                                    QueryUtil.ALL_POS, null)) {
535                            remove(contact);
536                    }
537            }
538    
539            /**
540             * Returns the number of contacts where companyId = &#63;.
541             *
542             * @param companyId the company ID
543             * @return the number of matching contacts
544             */
545            @Override
546            public int countByCompanyId(long companyId) {
547                    FinderPath finderPath = FINDER_PATH_COUNT_BY_COMPANYID;
548    
549                    Object[] finderArgs = new Object[] { companyId };
550    
551                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
552    
553                    if (count == null) {
554                            StringBundler query = new StringBundler(2);
555    
556                            query.append(_SQL_COUNT_CONTACT_WHERE);
557    
558                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
559    
560                            String sql = query.toString();
561    
562                            Session session = null;
563    
564                            try {
565                                    session = openSession();
566    
567                                    Query q = session.createQuery(sql);
568    
569                                    QueryPos qPos = QueryPos.getInstance(q);
570    
571                                    qPos.add(companyId);
572    
573                                    count = (Long)q.uniqueResult();
574    
575                                    finderCache.putResult(finderPath, finderArgs, count);
576                            }
577                            catch (Exception e) {
578                                    finderCache.removeResult(finderPath, finderArgs);
579    
580                                    throw processException(e);
581                            }
582                            finally {
583                                    closeSession(session);
584                            }
585                    }
586    
587                    return count.intValue();
588            }
589    
590            private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "contact.companyId = ?";
591            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_ACCOUNTID =
592                    new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
593                            ContactModelImpl.FINDER_CACHE_ENABLED, ContactImpl.class,
594                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByAccountId",
595                            new String[] {
596                                    Long.class.getName(),
597                                    
598                            Integer.class.getName(), Integer.class.getName(),
599                                    OrderByComparator.class.getName()
600                            });
601            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ACCOUNTID =
602                    new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
603                            ContactModelImpl.FINDER_CACHE_ENABLED, ContactImpl.class,
604                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByAccountId",
605                            new String[] { Long.class.getName() },
606                            ContactModelImpl.ACCOUNTID_COLUMN_BITMASK);
607            public static final FinderPath FINDER_PATH_COUNT_BY_ACCOUNTID = new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
608                            ContactModelImpl.FINDER_CACHE_ENABLED, Long.class,
609                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByAccountId",
610                            new String[] { Long.class.getName() });
611    
612            /**
613             * Returns all the contacts where accountId = &#63;.
614             *
615             * @param accountId the account ID
616             * @return the matching contacts
617             */
618            @Override
619            public List<Contact> findByAccountId(long accountId) {
620                    return findByAccountId(accountId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
621                            null);
622            }
623    
624            /**
625             * Returns a range of all the contacts where accountId = &#63;.
626             *
627             * <p>
628             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ContactModelImpl}. 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.
629             * </p>
630             *
631             * @param accountId the account ID
632             * @param start the lower bound of the range of contacts
633             * @param end the upper bound of the range of contacts (not inclusive)
634             * @return the range of matching contacts
635             */
636            @Override
637            public List<Contact> findByAccountId(long accountId, int start, int end) {
638                    return findByAccountId(accountId, start, end, null);
639            }
640    
641            /**
642             * Returns an ordered range of all the contacts where accountId = &#63;.
643             *
644             * <p>
645             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ContactModelImpl}. 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.
646             * </p>
647             *
648             * @param accountId the account ID
649             * @param start the lower bound of the range of contacts
650             * @param end the upper bound of the range of contacts (not inclusive)
651             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
652             * @return the ordered range of matching contacts
653             */
654            @Override
655            public List<Contact> findByAccountId(long accountId, int start, int end,
656                    OrderByComparator<Contact> orderByComparator) {
657                    return findByAccountId(accountId, start, end, orderByComparator, true);
658            }
659    
660            /**
661             * Returns an ordered range of all the contacts where accountId = &#63;.
662             *
663             * <p>
664             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ContactModelImpl}. 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.
665             * </p>
666             *
667             * @param accountId the account ID
668             * @param start the lower bound of the range of contacts
669             * @param end the upper bound of the range of contacts (not inclusive)
670             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
671             * @param retrieveFromCache whether to retrieve from the finder cache
672             * @return the ordered range of matching contacts
673             */
674            @Override
675            public List<Contact> findByAccountId(long accountId, int start, int end,
676                    OrderByComparator<Contact> orderByComparator, boolean retrieveFromCache) {
677                    boolean pagination = true;
678                    FinderPath finderPath = null;
679                    Object[] finderArgs = null;
680    
681                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
682                                    (orderByComparator == null)) {
683                            pagination = false;
684                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ACCOUNTID;
685                            finderArgs = new Object[] { accountId };
686                    }
687                    else {
688                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_ACCOUNTID;
689                            finderArgs = new Object[] { accountId, start, end, orderByComparator };
690                    }
691    
692                    List<Contact> list = null;
693    
694                    if (retrieveFromCache) {
695                            list = (List<Contact>)finderCache.getResult(finderPath, finderArgs,
696                                            this);
697    
698                            if ((list != null) && !list.isEmpty()) {
699                                    for (Contact contact : list) {
700                                            if ((accountId != contact.getAccountId())) {
701                                                    list = null;
702    
703                                                    break;
704                                            }
705                                    }
706                            }
707                    }
708    
709                    if (list == null) {
710                            StringBundler query = null;
711    
712                            if (orderByComparator != null) {
713                                    query = new StringBundler(3 +
714                                                    (orderByComparator.getOrderByFields().length * 3));
715                            }
716                            else {
717                                    query = new StringBundler(3);
718                            }
719    
720                            query.append(_SQL_SELECT_CONTACT_WHERE);
721    
722                            query.append(_FINDER_COLUMN_ACCOUNTID_ACCOUNTID_2);
723    
724                            if (orderByComparator != null) {
725                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
726                                            orderByComparator);
727                            }
728                            else
729                             if (pagination) {
730                                    query.append(ContactModelImpl.ORDER_BY_JPQL);
731                            }
732    
733                            String sql = query.toString();
734    
735                            Session session = null;
736    
737                            try {
738                                    session = openSession();
739    
740                                    Query q = session.createQuery(sql);
741    
742                                    QueryPos qPos = QueryPos.getInstance(q);
743    
744                                    qPos.add(accountId);
745    
746                                    if (!pagination) {
747                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
748                                                            start, end, false);
749    
750                                            Collections.sort(list);
751    
752                                            list = Collections.unmodifiableList(list);
753                                    }
754                                    else {
755                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
756                                                            start, end);
757                                    }
758    
759                                    cacheResult(list);
760    
761                                    finderCache.putResult(finderPath, finderArgs, list);
762                            }
763                            catch (Exception e) {
764                                    finderCache.removeResult(finderPath, finderArgs);
765    
766                                    throw processException(e);
767                            }
768                            finally {
769                                    closeSession(session);
770                            }
771                    }
772    
773                    return list;
774            }
775    
776            /**
777             * Returns the first contact in the ordered set where accountId = &#63;.
778             *
779             * @param accountId the account ID
780             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
781             * @return the first matching contact
782             * @throws NoSuchContactException if a matching contact could not be found
783             */
784            @Override
785            public Contact findByAccountId_First(long accountId,
786                    OrderByComparator<Contact> orderByComparator)
787                    throws NoSuchContactException {
788                    Contact contact = fetchByAccountId_First(accountId, orderByComparator);
789    
790                    if (contact != null) {
791                            return contact;
792                    }
793    
794                    StringBundler msg = new StringBundler(4);
795    
796                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
797    
798                    msg.append("accountId=");
799                    msg.append(accountId);
800    
801                    msg.append(StringPool.CLOSE_CURLY_BRACE);
802    
803                    throw new NoSuchContactException(msg.toString());
804            }
805    
806            /**
807             * Returns the first contact in the ordered set where accountId = &#63;.
808             *
809             * @param accountId the account ID
810             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
811             * @return the first matching contact, or <code>null</code> if a matching contact could not be found
812             */
813            @Override
814            public Contact fetchByAccountId_First(long accountId,
815                    OrderByComparator<Contact> orderByComparator) {
816                    List<Contact> list = findByAccountId(accountId, 0, 1, orderByComparator);
817    
818                    if (!list.isEmpty()) {
819                            return list.get(0);
820                    }
821    
822                    return null;
823            }
824    
825            /**
826             * Returns the last contact in the ordered set where accountId = &#63;.
827             *
828             * @param accountId the account ID
829             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
830             * @return the last matching contact
831             * @throws NoSuchContactException if a matching contact could not be found
832             */
833            @Override
834            public Contact findByAccountId_Last(long accountId,
835                    OrderByComparator<Contact> orderByComparator)
836                    throws NoSuchContactException {
837                    Contact contact = fetchByAccountId_Last(accountId, orderByComparator);
838    
839                    if (contact != null) {
840                            return contact;
841                    }
842    
843                    StringBundler msg = new StringBundler(4);
844    
845                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
846    
847                    msg.append("accountId=");
848                    msg.append(accountId);
849    
850                    msg.append(StringPool.CLOSE_CURLY_BRACE);
851    
852                    throw new NoSuchContactException(msg.toString());
853            }
854    
855            /**
856             * Returns the last contact in the ordered set where accountId = &#63;.
857             *
858             * @param accountId the account ID
859             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
860             * @return the last matching contact, or <code>null</code> if a matching contact could not be found
861             */
862            @Override
863            public Contact fetchByAccountId_Last(long accountId,
864                    OrderByComparator<Contact> orderByComparator) {
865                    int count = countByAccountId(accountId);
866    
867                    if (count == 0) {
868                            return null;
869                    }
870    
871                    List<Contact> list = findByAccountId(accountId, count - 1, count,
872                                    orderByComparator);
873    
874                    if (!list.isEmpty()) {
875                            return list.get(0);
876                    }
877    
878                    return null;
879            }
880    
881            /**
882             * Returns the contacts before and after the current contact in the ordered set where accountId = &#63;.
883             *
884             * @param contactId the primary key of the current contact
885             * @param accountId the account ID
886             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
887             * @return the previous, current, and next contact
888             * @throws NoSuchContactException if a contact with the primary key could not be found
889             */
890            @Override
891            public Contact[] findByAccountId_PrevAndNext(long contactId,
892                    long accountId, OrderByComparator<Contact> orderByComparator)
893                    throws NoSuchContactException {
894                    Contact contact = findByPrimaryKey(contactId);
895    
896                    Session session = null;
897    
898                    try {
899                            session = openSession();
900    
901                            Contact[] array = new ContactImpl[3];
902    
903                            array[0] = getByAccountId_PrevAndNext(session, contact, accountId,
904                                            orderByComparator, true);
905    
906                            array[1] = contact;
907    
908                            array[2] = getByAccountId_PrevAndNext(session, contact, accountId,
909                                            orderByComparator, false);
910    
911                            return array;
912                    }
913                    catch (Exception e) {
914                            throw processException(e);
915                    }
916                    finally {
917                            closeSession(session);
918                    }
919            }
920    
921            protected Contact getByAccountId_PrevAndNext(Session session,
922                    Contact contact, long accountId,
923                    OrderByComparator<Contact> orderByComparator, boolean previous) {
924                    StringBundler query = null;
925    
926                    if (orderByComparator != null) {
927                            query = new StringBundler(6 +
928                                            (orderByComparator.getOrderByFields().length * 6));
929                    }
930                    else {
931                            query = new StringBundler(3);
932                    }
933    
934                    query.append(_SQL_SELECT_CONTACT_WHERE);
935    
936                    query.append(_FINDER_COLUMN_ACCOUNTID_ACCOUNTID_2);
937    
938                    if (orderByComparator != null) {
939                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
940    
941                            if (orderByConditionFields.length > 0) {
942                                    query.append(WHERE_AND);
943                            }
944    
945                            for (int i = 0; i < orderByConditionFields.length; i++) {
946                                    query.append(_ORDER_BY_ENTITY_ALIAS);
947                                    query.append(orderByConditionFields[i]);
948    
949                                    if ((i + 1) < orderByConditionFields.length) {
950                                            if (orderByComparator.isAscending() ^ previous) {
951                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
952                                            }
953                                            else {
954                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
955                                            }
956                                    }
957                                    else {
958                                            if (orderByComparator.isAscending() ^ previous) {
959                                                    query.append(WHERE_GREATER_THAN);
960                                            }
961                                            else {
962                                                    query.append(WHERE_LESSER_THAN);
963                                            }
964                                    }
965                            }
966    
967                            query.append(ORDER_BY_CLAUSE);
968    
969                            String[] orderByFields = orderByComparator.getOrderByFields();
970    
971                            for (int i = 0; i < orderByFields.length; i++) {
972                                    query.append(_ORDER_BY_ENTITY_ALIAS);
973                                    query.append(orderByFields[i]);
974    
975                                    if ((i + 1) < orderByFields.length) {
976                                            if (orderByComparator.isAscending() ^ previous) {
977                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
978                                            }
979                                            else {
980                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
981                                            }
982                                    }
983                                    else {
984                                            if (orderByComparator.isAscending() ^ previous) {
985                                                    query.append(ORDER_BY_ASC);
986                                            }
987                                            else {
988                                                    query.append(ORDER_BY_DESC);
989                                            }
990                                    }
991                            }
992                    }
993                    else {
994                            query.append(ContactModelImpl.ORDER_BY_JPQL);
995                    }
996    
997                    String sql = query.toString();
998    
999                    Query q = session.createQuery(sql);
1000    
1001                    q.setFirstResult(0);
1002                    q.setMaxResults(2);
1003    
1004                    QueryPos qPos = QueryPos.getInstance(q);
1005    
1006                    qPos.add(accountId);
1007    
1008                    if (orderByComparator != null) {
1009                            Object[] values = orderByComparator.getOrderByConditionValues(contact);
1010    
1011                            for (Object value : values) {
1012                                    qPos.add(value);
1013                            }
1014                    }
1015    
1016                    List<Contact> list = q.list();
1017    
1018                    if (list.size() == 2) {
1019                            return list.get(1);
1020                    }
1021                    else {
1022                            return null;
1023                    }
1024            }
1025    
1026            /**
1027             * Removes all the contacts where accountId = &#63; from the database.
1028             *
1029             * @param accountId the account ID
1030             */
1031            @Override
1032            public void removeByAccountId(long accountId) {
1033                    for (Contact contact : findByAccountId(accountId, QueryUtil.ALL_POS,
1034                                    QueryUtil.ALL_POS, null)) {
1035                            remove(contact);
1036                    }
1037            }
1038    
1039            /**
1040             * Returns the number of contacts where accountId = &#63;.
1041             *
1042             * @param accountId the account ID
1043             * @return the number of matching contacts
1044             */
1045            @Override
1046            public int countByAccountId(long accountId) {
1047                    FinderPath finderPath = FINDER_PATH_COUNT_BY_ACCOUNTID;
1048    
1049                    Object[] finderArgs = new Object[] { accountId };
1050    
1051                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
1052    
1053                    if (count == null) {
1054                            StringBundler query = new StringBundler(2);
1055    
1056                            query.append(_SQL_COUNT_CONTACT_WHERE);
1057    
1058                            query.append(_FINDER_COLUMN_ACCOUNTID_ACCOUNTID_2);
1059    
1060                            String sql = query.toString();
1061    
1062                            Session session = null;
1063    
1064                            try {
1065                                    session = openSession();
1066    
1067                                    Query q = session.createQuery(sql);
1068    
1069                                    QueryPos qPos = QueryPos.getInstance(q);
1070    
1071                                    qPos.add(accountId);
1072    
1073                                    count = (Long)q.uniqueResult();
1074    
1075                                    finderCache.putResult(finderPath, finderArgs, count);
1076                            }
1077                            catch (Exception e) {
1078                                    finderCache.removeResult(finderPath, finderArgs);
1079    
1080                                    throw processException(e);
1081                            }
1082                            finally {
1083                                    closeSession(session);
1084                            }
1085                    }
1086    
1087                    return count.intValue();
1088            }
1089    
1090            private static final String _FINDER_COLUMN_ACCOUNTID_ACCOUNTID_2 = "contact.accountId = ?";
1091            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C = new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
1092                            ContactModelImpl.FINDER_CACHE_ENABLED, ContactImpl.class,
1093                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C",
1094                            new String[] {
1095                                    Long.class.getName(), Long.class.getName(),
1096                                    
1097                            Integer.class.getName(), Integer.class.getName(),
1098                                    OrderByComparator.class.getName()
1099                            });
1100            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C = new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
1101                            ContactModelImpl.FINDER_CACHE_ENABLED, ContactImpl.class,
1102                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C",
1103                            new String[] { Long.class.getName(), Long.class.getName() },
1104                            ContactModelImpl.CLASSNAMEID_COLUMN_BITMASK |
1105                            ContactModelImpl.CLASSPK_COLUMN_BITMASK);
1106            public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
1107                            ContactModelImpl.FINDER_CACHE_ENABLED, Long.class,
1108                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C",
1109                            new String[] { Long.class.getName(), Long.class.getName() });
1110    
1111            /**
1112             * Returns all the contacts where classNameId = &#63; and classPK = &#63;.
1113             *
1114             * @param classNameId the class name ID
1115             * @param classPK the class p k
1116             * @return the matching contacts
1117             */
1118            @Override
1119            public List<Contact> findByC_C(long classNameId, long classPK) {
1120                    return findByC_C(classNameId, classPK, QueryUtil.ALL_POS,
1121                            QueryUtil.ALL_POS, null);
1122            }
1123    
1124            /**
1125             * Returns a range of all the contacts where classNameId = &#63; and classPK = &#63;.
1126             *
1127             * <p>
1128             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ContactModelImpl}. 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.
1129             * </p>
1130             *
1131             * @param classNameId the class name ID
1132             * @param classPK the class p k
1133             * @param start the lower bound of the range of contacts
1134             * @param end the upper bound of the range of contacts (not inclusive)
1135             * @return the range of matching contacts
1136             */
1137            @Override
1138            public List<Contact> findByC_C(long classNameId, long classPK, int start,
1139                    int end) {
1140                    return findByC_C(classNameId, classPK, start, end, null);
1141            }
1142    
1143            /**
1144             * Returns an ordered range of all the contacts where classNameId = &#63; and classPK = &#63;.
1145             *
1146             * <p>
1147             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ContactModelImpl}. 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.
1148             * </p>
1149             *
1150             * @param classNameId the class name ID
1151             * @param classPK the class p k
1152             * @param start the lower bound of the range of contacts
1153             * @param end the upper bound of the range of contacts (not inclusive)
1154             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1155             * @return the ordered range of matching contacts
1156             */
1157            @Override
1158            public List<Contact> findByC_C(long classNameId, long classPK, int start,
1159                    int end, OrderByComparator<Contact> orderByComparator) {
1160                    return findByC_C(classNameId, classPK, start, end, orderByComparator,
1161                            true);
1162            }
1163    
1164            /**
1165             * Returns an ordered range of all the contacts where classNameId = &#63; and classPK = &#63;.
1166             *
1167             * <p>
1168             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ContactModelImpl}. 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.
1169             * </p>
1170             *
1171             * @param classNameId the class name ID
1172             * @param classPK the class p k
1173             * @param start the lower bound of the range of contacts
1174             * @param end the upper bound of the range of contacts (not inclusive)
1175             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1176             * @param retrieveFromCache whether to retrieve from the finder cache
1177             * @return the ordered range of matching contacts
1178             */
1179            @Override
1180            public List<Contact> findByC_C(long classNameId, long classPK, int start,
1181                    int end, OrderByComparator<Contact> orderByComparator,
1182                    boolean retrieveFromCache) {
1183                    boolean pagination = true;
1184                    FinderPath finderPath = null;
1185                    Object[] finderArgs = null;
1186    
1187                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1188                                    (orderByComparator == null)) {
1189                            pagination = false;
1190                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C;
1191                            finderArgs = new Object[] { classNameId, classPK };
1192                    }
1193                    else {
1194                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C;
1195                            finderArgs = new Object[] {
1196                                            classNameId, classPK,
1197                                            
1198                                            start, end, orderByComparator
1199                                    };
1200                    }
1201    
1202                    List<Contact> list = null;
1203    
1204                    if (retrieveFromCache) {
1205                            list = (List<Contact>)finderCache.getResult(finderPath, finderArgs,
1206                                            this);
1207    
1208                            if ((list != null) && !list.isEmpty()) {
1209                                    for (Contact contact : list) {
1210                                            if ((classNameId != contact.getClassNameId()) ||
1211                                                            (classPK != contact.getClassPK())) {
1212                                                    list = null;
1213    
1214                                                    break;
1215                                            }
1216                                    }
1217                            }
1218                    }
1219    
1220                    if (list == null) {
1221                            StringBundler query = null;
1222    
1223                            if (orderByComparator != null) {
1224                                    query = new StringBundler(4 +
1225                                                    (orderByComparator.getOrderByFields().length * 3));
1226                            }
1227                            else {
1228                                    query = new StringBundler(4);
1229                            }
1230    
1231                            query.append(_SQL_SELECT_CONTACT_WHERE);
1232    
1233                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
1234    
1235                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
1236    
1237                            if (orderByComparator != null) {
1238                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1239                                            orderByComparator);
1240                            }
1241                            else
1242                             if (pagination) {
1243                                    query.append(ContactModelImpl.ORDER_BY_JPQL);
1244                            }
1245    
1246                            String sql = query.toString();
1247    
1248                            Session session = null;
1249    
1250                            try {
1251                                    session = openSession();
1252    
1253                                    Query q = session.createQuery(sql);
1254    
1255                                    QueryPos qPos = QueryPos.getInstance(q);
1256    
1257                                    qPos.add(classNameId);
1258    
1259                                    qPos.add(classPK);
1260    
1261                                    if (!pagination) {
1262                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
1263                                                            start, end, false);
1264    
1265                                            Collections.sort(list);
1266    
1267                                            list = Collections.unmodifiableList(list);
1268                                    }
1269                                    else {
1270                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
1271                                                            start, end);
1272                                    }
1273    
1274                                    cacheResult(list);
1275    
1276                                    finderCache.putResult(finderPath, finderArgs, list);
1277                            }
1278                            catch (Exception e) {
1279                                    finderCache.removeResult(finderPath, finderArgs);
1280    
1281                                    throw processException(e);
1282                            }
1283                            finally {
1284                                    closeSession(session);
1285                            }
1286                    }
1287    
1288                    return list;
1289            }
1290    
1291            /**
1292             * Returns the first contact in the ordered set where classNameId = &#63; and classPK = &#63;.
1293             *
1294             * @param classNameId the class name ID
1295             * @param classPK the class p k
1296             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1297             * @return the first matching contact
1298             * @throws NoSuchContactException if a matching contact could not be found
1299             */
1300            @Override
1301            public Contact findByC_C_First(long classNameId, long classPK,
1302                    OrderByComparator<Contact> orderByComparator)
1303                    throws NoSuchContactException {
1304                    Contact contact = fetchByC_C_First(classNameId, classPK,
1305                                    orderByComparator);
1306    
1307                    if (contact != null) {
1308                            return contact;
1309                    }
1310    
1311                    StringBundler msg = new StringBundler(6);
1312    
1313                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1314    
1315                    msg.append("classNameId=");
1316                    msg.append(classNameId);
1317    
1318                    msg.append(", classPK=");
1319                    msg.append(classPK);
1320    
1321                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1322    
1323                    throw new NoSuchContactException(msg.toString());
1324            }
1325    
1326            /**
1327             * Returns the first contact in the ordered set where classNameId = &#63; and classPK = &#63;.
1328             *
1329             * @param classNameId the class name ID
1330             * @param classPK the class p k
1331             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1332             * @return the first matching contact, or <code>null</code> if a matching contact could not be found
1333             */
1334            @Override
1335            public Contact fetchByC_C_First(long classNameId, long classPK,
1336                    OrderByComparator<Contact> orderByComparator) {
1337                    List<Contact> list = findByC_C(classNameId, classPK, 0, 1,
1338                                    orderByComparator);
1339    
1340                    if (!list.isEmpty()) {
1341                            return list.get(0);
1342                    }
1343    
1344                    return null;
1345            }
1346    
1347            /**
1348             * Returns the last contact in the ordered set where classNameId = &#63; and classPK = &#63;.
1349             *
1350             * @param classNameId the class name ID
1351             * @param classPK the class p k
1352             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1353             * @return the last matching contact
1354             * @throws NoSuchContactException if a matching contact could not be found
1355             */
1356            @Override
1357            public Contact findByC_C_Last(long classNameId, long classPK,
1358                    OrderByComparator<Contact> orderByComparator)
1359                    throws NoSuchContactException {
1360                    Contact contact = fetchByC_C_Last(classNameId, classPK,
1361                                    orderByComparator);
1362    
1363                    if (contact != null) {
1364                            return contact;
1365                    }
1366    
1367                    StringBundler msg = new StringBundler(6);
1368    
1369                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1370    
1371                    msg.append("classNameId=");
1372                    msg.append(classNameId);
1373    
1374                    msg.append(", classPK=");
1375                    msg.append(classPK);
1376    
1377                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1378    
1379                    throw new NoSuchContactException(msg.toString());
1380            }
1381    
1382            /**
1383             * Returns the last contact in the ordered set where classNameId = &#63; and classPK = &#63;.
1384             *
1385             * @param classNameId the class name ID
1386             * @param classPK the class p k
1387             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1388             * @return the last matching contact, or <code>null</code> if a matching contact could not be found
1389             */
1390            @Override
1391            public Contact fetchByC_C_Last(long classNameId, long classPK,
1392                    OrderByComparator<Contact> orderByComparator) {
1393                    int count = countByC_C(classNameId, classPK);
1394    
1395                    if (count == 0) {
1396                            return null;
1397                    }
1398    
1399                    List<Contact> list = findByC_C(classNameId, classPK, count - 1, count,
1400                                    orderByComparator);
1401    
1402                    if (!list.isEmpty()) {
1403                            return list.get(0);
1404                    }
1405    
1406                    return null;
1407            }
1408    
1409            /**
1410             * Returns the contacts before and after the current contact in the ordered set where classNameId = &#63; and classPK = &#63;.
1411             *
1412             * @param contactId the primary key of the current contact
1413             * @param classNameId the class name ID
1414             * @param classPK the class p k
1415             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1416             * @return the previous, current, and next contact
1417             * @throws NoSuchContactException if a contact with the primary key could not be found
1418             */
1419            @Override
1420            public Contact[] findByC_C_PrevAndNext(long contactId, long classNameId,
1421                    long classPK, OrderByComparator<Contact> orderByComparator)
1422                    throws NoSuchContactException {
1423                    Contact contact = findByPrimaryKey(contactId);
1424    
1425                    Session session = null;
1426    
1427                    try {
1428                            session = openSession();
1429    
1430                            Contact[] array = new ContactImpl[3];
1431    
1432                            array[0] = getByC_C_PrevAndNext(session, contact, classNameId,
1433                                            classPK, orderByComparator, true);
1434    
1435                            array[1] = contact;
1436    
1437                            array[2] = getByC_C_PrevAndNext(session, contact, classNameId,
1438                                            classPK, orderByComparator, false);
1439    
1440                            return array;
1441                    }
1442                    catch (Exception e) {
1443                            throw processException(e);
1444                    }
1445                    finally {
1446                            closeSession(session);
1447                    }
1448            }
1449    
1450            protected Contact getByC_C_PrevAndNext(Session session, Contact contact,
1451                    long classNameId, long classPK,
1452                    OrderByComparator<Contact> orderByComparator, boolean previous) {
1453                    StringBundler query = null;
1454    
1455                    if (orderByComparator != null) {
1456                            query = new StringBundler(6 +
1457                                            (orderByComparator.getOrderByFields().length * 6));
1458                    }
1459                    else {
1460                            query = new StringBundler(3);
1461                    }
1462    
1463                    query.append(_SQL_SELECT_CONTACT_WHERE);
1464    
1465                    query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
1466    
1467                    query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
1468    
1469                    if (orderByComparator != null) {
1470                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1471    
1472                            if (orderByConditionFields.length > 0) {
1473                                    query.append(WHERE_AND);
1474                            }
1475    
1476                            for (int i = 0; i < orderByConditionFields.length; i++) {
1477                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1478                                    query.append(orderByConditionFields[i]);
1479    
1480                                    if ((i + 1) < orderByConditionFields.length) {
1481                                            if (orderByComparator.isAscending() ^ previous) {
1482                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1483                                            }
1484                                            else {
1485                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1486                                            }
1487                                    }
1488                                    else {
1489                                            if (orderByComparator.isAscending() ^ previous) {
1490                                                    query.append(WHERE_GREATER_THAN);
1491                                            }
1492                                            else {
1493                                                    query.append(WHERE_LESSER_THAN);
1494                                            }
1495                                    }
1496                            }
1497    
1498                            query.append(ORDER_BY_CLAUSE);
1499    
1500                            String[] orderByFields = orderByComparator.getOrderByFields();
1501    
1502                            for (int i = 0; i < orderByFields.length; i++) {
1503                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1504                                    query.append(orderByFields[i]);
1505    
1506                                    if ((i + 1) < orderByFields.length) {
1507                                            if (orderByComparator.isAscending() ^ previous) {
1508                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1509                                            }
1510                                            else {
1511                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1512                                            }
1513                                    }
1514                                    else {
1515                                            if (orderByComparator.isAscending() ^ previous) {
1516                                                    query.append(ORDER_BY_ASC);
1517                                            }
1518                                            else {
1519                                                    query.append(ORDER_BY_DESC);
1520                                            }
1521                                    }
1522                            }
1523                    }
1524                    else {
1525                            query.append(ContactModelImpl.ORDER_BY_JPQL);
1526                    }
1527    
1528                    String sql = query.toString();
1529    
1530                    Query q = session.createQuery(sql);
1531    
1532                    q.setFirstResult(0);
1533                    q.setMaxResults(2);
1534    
1535                    QueryPos qPos = QueryPos.getInstance(q);
1536    
1537                    qPos.add(classNameId);
1538    
1539                    qPos.add(classPK);
1540    
1541                    if (orderByComparator != null) {
1542                            Object[] values = orderByComparator.getOrderByConditionValues(contact);
1543    
1544                            for (Object value : values) {
1545                                    qPos.add(value);
1546                            }
1547                    }
1548    
1549                    List<Contact> list = q.list();
1550    
1551                    if (list.size() == 2) {
1552                            return list.get(1);
1553                    }
1554                    else {
1555                            return null;
1556                    }
1557            }
1558    
1559            /**
1560             * Removes all the contacts where classNameId = &#63; and classPK = &#63; from the database.
1561             *
1562             * @param classNameId the class name ID
1563             * @param classPK the class p k
1564             */
1565            @Override
1566            public void removeByC_C(long classNameId, long classPK) {
1567                    for (Contact contact : findByC_C(classNameId, classPK,
1568                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1569                            remove(contact);
1570                    }
1571            }
1572    
1573            /**
1574             * Returns the number of contacts where classNameId = &#63; and classPK = &#63;.
1575             *
1576             * @param classNameId the class name ID
1577             * @param classPK the class p k
1578             * @return the number of matching contacts
1579             */
1580            @Override
1581            public int countByC_C(long classNameId, long classPK) {
1582                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C;
1583    
1584                    Object[] finderArgs = new Object[] { classNameId, classPK };
1585    
1586                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
1587    
1588                    if (count == null) {
1589                            StringBundler query = new StringBundler(3);
1590    
1591                            query.append(_SQL_COUNT_CONTACT_WHERE);
1592    
1593                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
1594    
1595                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
1596    
1597                            String sql = query.toString();
1598    
1599                            Session session = null;
1600    
1601                            try {
1602                                    session = openSession();
1603    
1604                                    Query q = session.createQuery(sql);
1605    
1606                                    QueryPos qPos = QueryPos.getInstance(q);
1607    
1608                                    qPos.add(classNameId);
1609    
1610                                    qPos.add(classPK);
1611    
1612                                    count = (Long)q.uniqueResult();
1613    
1614                                    finderCache.putResult(finderPath, finderArgs, count);
1615                            }
1616                            catch (Exception e) {
1617                                    finderCache.removeResult(finderPath, finderArgs);
1618    
1619                                    throw processException(e);
1620                            }
1621                            finally {
1622                                    closeSession(session);
1623                            }
1624                    }
1625    
1626                    return count.intValue();
1627            }
1628    
1629            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "contact.classNameId = ? AND ";
1630            private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "contact.classPK = ?";
1631    
1632            public ContactPersistenceImpl() {
1633                    setModelClass(Contact.class);
1634            }
1635    
1636            /**
1637             * Caches the contact in the entity cache if it is enabled.
1638             *
1639             * @param contact the contact
1640             */
1641            @Override
1642            public void cacheResult(Contact contact) {
1643                    entityCache.putResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
1644                            ContactImpl.class, contact.getPrimaryKey(), contact);
1645    
1646                    contact.resetOriginalValues();
1647            }
1648    
1649            /**
1650             * Caches the contacts in the entity cache if it is enabled.
1651             *
1652             * @param contacts the contacts
1653             */
1654            @Override
1655            public void cacheResult(List<Contact> contacts) {
1656                    for (Contact contact : contacts) {
1657                            if (entityCache.getResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
1658                                                    ContactImpl.class, contact.getPrimaryKey()) == null) {
1659                                    cacheResult(contact);
1660                            }
1661                            else {
1662                                    contact.resetOriginalValues();
1663                            }
1664                    }
1665            }
1666    
1667            /**
1668             * Clears the cache for all contacts.
1669             *
1670             * <p>
1671             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
1672             * </p>
1673             */
1674            @Override
1675            public void clearCache() {
1676                    entityCache.clearCache(ContactImpl.class);
1677    
1678                    finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
1679                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1680                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1681            }
1682    
1683            /**
1684             * Clears the cache for the contact.
1685             *
1686             * <p>
1687             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
1688             * </p>
1689             */
1690            @Override
1691            public void clearCache(Contact contact) {
1692                    entityCache.removeResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
1693                            ContactImpl.class, contact.getPrimaryKey());
1694    
1695                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1696                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1697            }
1698    
1699            @Override
1700            public void clearCache(List<Contact> contacts) {
1701                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1702                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1703    
1704                    for (Contact contact : contacts) {
1705                            entityCache.removeResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
1706                                    ContactImpl.class, contact.getPrimaryKey());
1707                    }
1708            }
1709    
1710            /**
1711             * Creates a new contact with the primary key. Does not add the contact to the database.
1712             *
1713             * @param contactId the primary key for the new contact
1714             * @return the new contact
1715             */
1716            @Override
1717            public Contact create(long contactId) {
1718                    Contact contact = new ContactImpl();
1719    
1720                    contact.setNew(true);
1721                    contact.setPrimaryKey(contactId);
1722    
1723                    contact.setCompanyId(companyProvider.getCompanyId());
1724    
1725                    return contact;
1726            }
1727    
1728            /**
1729             * Removes the contact with the primary key from the database. Also notifies the appropriate model listeners.
1730             *
1731             * @param contactId the primary key of the contact
1732             * @return the contact that was removed
1733             * @throws NoSuchContactException if a contact with the primary key could not be found
1734             */
1735            @Override
1736            public Contact remove(long contactId) throws NoSuchContactException {
1737                    return remove((Serializable)contactId);
1738            }
1739    
1740            /**
1741             * Removes the contact with the primary key from the database. Also notifies the appropriate model listeners.
1742             *
1743             * @param primaryKey the primary key of the contact
1744             * @return the contact that was removed
1745             * @throws NoSuchContactException if a contact with the primary key could not be found
1746             */
1747            @Override
1748            public Contact remove(Serializable primaryKey)
1749                    throws NoSuchContactException {
1750                    Session session = null;
1751    
1752                    try {
1753                            session = openSession();
1754    
1755                            Contact contact = (Contact)session.get(ContactImpl.class, primaryKey);
1756    
1757                            if (contact == null) {
1758                                    if (_log.isWarnEnabled()) {
1759                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1760                                    }
1761    
1762                                    throw new NoSuchContactException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1763                                            primaryKey);
1764                            }
1765    
1766                            return remove(contact);
1767                    }
1768                    catch (NoSuchContactException nsee) {
1769                            throw nsee;
1770                    }
1771                    catch (Exception e) {
1772                            throw processException(e);
1773                    }
1774                    finally {
1775                            closeSession(session);
1776                    }
1777            }
1778    
1779            @Override
1780            protected Contact removeImpl(Contact contact) {
1781                    contact = toUnwrappedModel(contact);
1782    
1783                    Session session = null;
1784    
1785                    try {
1786                            session = openSession();
1787    
1788                            if (!session.contains(contact)) {
1789                                    contact = (Contact)session.get(ContactImpl.class,
1790                                                    contact.getPrimaryKeyObj());
1791                            }
1792    
1793                            if (contact != null) {
1794                                    session.delete(contact);
1795                            }
1796                    }
1797                    catch (Exception e) {
1798                            throw processException(e);
1799                    }
1800                    finally {
1801                            closeSession(session);
1802                    }
1803    
1804                    if (contact != null) {
1805                            clearCache(contact);
1806                    }
1807    
1808                    return contact;
1809            }
1810    
1811            @Override
1812            public Contact updateImpl(Contact contact) {
1813                    contact = toUnwrappedModel(contact);
1814    
1815                    boolean isNew = contact.isNew();
1816    
1817                    ContactModelImpl contactModelImpl = (ContactModelImpl)contact;
1818    
1819                    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
1820    
1821                    Date now = new Date();
1822    
1823                    if (isNew && (contact.getCreateDate() == null)) {
1824                            if (serviceContext == null) {
1825                                    contact.setCreateDate(now);
1826                            }
1827                            else {
1828                                    contact.setCreateDate(serviceContext.getCreateDate(now));
1829                            }
1830                    }
1831    
1832                    if (!contactModelImpl.hasSetModifiedDate()) {
1833                            if (serviceContext == null) {
1834                                    contact.setModifiedDate(now);
1835                            }
1836                            else {
1837                                    contact.setModifiedDate(serviceContext.getModifiedDate(now));
1838                            }
1839                    }
1840    
1841                    Session session = null;
1842    
1843                    try {
1844                            session = openSession();
1845    
1846                            if (contact.isNew()) {
1847                                    session.save(contact);
1848    
1849                                    contact.setNew(false);
1850                            }
1851                            else {
1852                                    contact = (Contact)session.merge(contact);
1853                            }
1854                    }
1855                    catch (Exception e) {
1856                            throw processException(e);
1857                    }
1858                    finally {
1859                            closeSession(session);
1860                    }
1861    
1862                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1863    
1864                    if (isNew || !ContactModelImpl.COLUMN_BITMASK_ENABLED) {
1865                            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1866                    }
1867    
1868                    else {
1869                            if ((contactModelImpl.getColumnBitmask() &
1870                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID.getColumnBitmask()) != 0) {
1871                                    Object[] args = new Object[] {
1872                                                    contactModelImpl.getOriginalCompanyId()
1873                                            };
1874    
1875                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_COMPANYID, args);
1876                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
1877                                            args);
1878    
1879                                    args = new Object[] { contactModelImpl.getCompanyId() };
1880    
1881                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_COMPANYID, args);
1882                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
1883                                            args);
1884                            }
1885    
1886                            if ((contactModelImpl.getColumnBitmask() &
1887                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ACCOUNTID.getColumnBitmask()) != 0) {
1888                                    Object[] args = new Object[] {
1889                                                    contactModelImpl.getOriginalAccountId()
1890                                            };
1891    
1892                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_ACCOUNTID, args);
1893                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ACCOUNTID,
1894                                            args);
1895    
1896                                    args = new Object[] { contactModelImpl.getAccountId() };
1897    
1898                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_ACCOUNTID, args);
1899                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ACCOUNTID,
1900                                            args);
1901                            }
1902    
1903                            if ((contactModelImpl.getColumnBitmask() &
1904                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C.getColumnBitmask()) != 0) {
1905                                    Object[] args = new Object[] {
1906                                                    contactModelImpl.getOriginalClassNameId(),
1907                                                    contactModelImpl.getOriginalClassPK()
1908                                            };
1909    
1910                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
1911                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C,
1912                                            args);
1913    
1914                                    args = new Object[] {
1915                                                    contactModelImpl.getClassNameId(),
1916                                                    contactModelImpl.getClassPK()
1917                                            };
1918    
1919                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
1920                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C,
1921                                            args);
1922                            }
1923                    }
1924    
1925                    entityCache.putResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
1926                            ContactImpl.class, contact.getPrimaryKey(), contact, false);
1927    
1928                    contact.resetOriginalValues();
1929    
1930                    return contact;
1931            }
1932    
1933            protected Contact toUnwrappedModel(Contact contact) {
1934                    if (contact instanceof ContactImpl) {
1935                            return contact;
1936                    }
1937    
1938                    ContactImpl contactImpl = new ContactImpl();
1939    
1940                    contactImpl.setNew(contact.isNew());
1941                    contactImpl.setPrimaryKey(contact.getPrimaryKey());
1942    
1943                    contactImpl.setMvccVersion(contact.getMvccVersion());
1944                    contactImpl.setContactId(contact.getContactId());
1945                    contactImpl.setCompanyId(contact.getCompanyId());
1946                    contactImpl.setUserId(contact.getUserId());
1947                    contactImpl.setUserName(contact.getUserName());
1948                    contactImpl.setCreateDate(contact.getCreateDate());
1949                    contactImpl.setModifiedDate(contact.getModifiedDate());
1950                    contactImpl.setClassNameId(contact.getClassNameId());
1951                    contactImpl.setClassPK(contact.getClassPK());
1952                    contactImpl.setAccountId(contact.getAccountId());
1953                    contactImpl.setParentContactId(contact.getParentContactId());
1954                    contactImpl.setEmailAddress(contact.getEmailAddress());
1955                    contactImpl.setFirstName(contact.getFirstName());
1956                    contactImpl.setMiddleName(contact.getMiddleName());
1957                    contactImpl.setLastName(contact.getLastName());
1958                    contactImpl.setPrefixId(contact.getPrefixId());
1959                    contactImpl.setSuffixId(contact.getSuffixId());
1960                    contactImpl.setMale(contact.isMale());
1961                    contactImpl.setBirthday(contact.getBirthday());
1962                    contactImpl.setSmsSn(contact.getSmsSn());
1963                    contactImpl.setFacebookSn(contact.getFacebookSn());
1964                    contactImpl.setJabberSn(contact.getJabberSn());
1965                    contactImpl.setSkypeSn(contact.getSkypeSn());
1966                    contactImpl.setTwitterSn(contact.getTwitterSn());
1967                    contactImpl.setEmployeeStatusId(contact.getEmployeeStatusId());
1968                    contactImpl.setEmployeeNumber(contact.getEmployeeNumber());
1969                    contactImpl.setJobTitle(contact.getJobTitle());
1970                    contactImpl.setJobClass(contact.getJobClass());
1971                    contactImpl.setHoursOfOperation(contact.getHoursOfOperation());
1972    
1973                    return contactImpl;
1974            }
1975    
1976            /**
1977             * Returns the contact with the primary key or throws a {@link com.liferay.portal.exception.NoSuchModelException} if it could not be found.
1978             *
1979             * @param primaryKey the primary key of the contact
1980             * @return the contact
1981             * @throws NoSuchContactException if a contact with the primary key could not be found
1982             */
1983            @Override
1984            public Contact findByPrimaryKey(Serializable primaryKey)
1985                    throws NoSuchContactException {
1986                    Contact contact = fetchByPrimaryKey(primaryKey);
1987    
1988                    if (contact == null) {
1989                            if (_log.isWarnEnabled()) {
1990                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1991                            }
1992    
1993                            throw new NoSuchContactException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1994                                    primaryKey);
1995                    }
1996    
1997                    return contact;
1998            }
1999    
2000            /**
2001             * Returns the contact with the primary key or throws a {@link NoSuchContactException} if it could not be found.
2002             *
2003             * @param contactId the primary key of the contact
2004             * @return the contact
2005             * @throws NoSuchContactException if a contact with the primary key could not be found
2006             */
2007            @Override
2008            public Contact findByPrimaryKey(long contactId)
2009                    throws NoSuchContactException {
2010                    return findByPrimaryKey((Serializable)contactId);
2011            }
2012    
2013            /**
2014             * Returns the contact with the primary key or returns <code>null</code> if it could not be found.
2015             *
2016             * @param primaryKey the primary key of the contact
2017             * @return the contact, or <code>null</code> if a contact with the primary key could not be found
2018             */
2019            @Override
2020            public Contact fetchByPrimaryKey(Serializable primaryKey) {
2021                    Contact contact = (Contact)entityCache.getResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
2022                                    ContactImpl.class, primaryKey);
2023    
2024                    if (contact == _nullContact) {
2025                            return null;
2026                    }
2027    
2028                    if (contact == null) {
2029                            Session session = null;
2030    
2031                            try {
2032                                    session = openSession();
2033    
2034                                    contact = (Contact)session.get(ContactImpl.class, primaryKey);
2035    
2036                                    if (contact != null) {
2037                                            cacheResult(contact);
2038                                    }
2039                                    else {
2040                                            entityCache.putResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
2041                                                    ContactImpl.class, primaryKey, _nullContact);
2042                                    }
2043                            }
2044                            catch (Exception e) {
2045                                    entityCache.removeResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
2046                                            ContactImpl.class, primaryKey);
2047    
2048                                    throw processException(e);
2049                            }
2050                            finally {
2051                                    closeSession(session);
2052                            }
2053                    }
2054    
2055                    return contact;
2056            }
2057    
2058            /**
2059             * Returns the contact with the primary key or returns <code>null</code> if it could not be found.
2060             *
2061             * @param contactId the primary key of the contact
2062             * @return the contact, or <code>null</code> if a contact with the primary key could not be found
2063             */
2064            @Override
2065            public Contact fetchByPrimaryKey(long contactId) {
2066                    return fetchByPrimaryKey((Serializable)contactId);
2067            }
2068    
2069            @Override
2070            public Map<Serializable, Contact> fetchByPrimaryKeys(
2071                    Set<Serializable> primaryKeys) {
2072                    if (primaryKeys.isEmpty()) {
2073                            return Collections.emptyMap();
2074                    }
2075    
2076                    Map<Serializable, Contact> map = new HashMap<Serializable, Contact>();
2077    
2078                    if (primaryKeys.size() == 1) {
2079                            Iterator<Serializable> iterator = primaryKeys.iterator();
2080    
2081                            Serializable primaryKey = iterator.next();
2082    
2083                            Contact contact = fetchByPrimaryKey(primaryKey);
2084    
2085                            if (contact != null) {
2086                                    map.put(primaryKey, contact);
2087                            }
2088    
2089                            return map;
2090                    }
2091    
2092                    Set<Serializable> uncachedPrimaryKeys = null;
2093    
2094                    for (Serializable primaryKey : primaryKeys) {
2095                            Contact contact = (Contact)entityCache.getResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
2096                                            ContactImpl.class, primaryKey);
2097    
2098                            if (contact == null) {
2099                                    if (uncachedPrimaryKeys == null) {
2100                                            uncachedPrimaryKeys = new HashSet<Serializable>();
2101                                    }
2102    
2103                                    uncachedPrimaryKeys.add(primaryKey);
2104                            }
2105                            else {
2106                                    map.put(primaryKey, contact);
2107                            }
2108                    }
2109    
2110                    if (uncachedPrimaryKeys == null) {
2111                            return map;
2112                    }
2113    
2114                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
2115                                    1);
2116    
2117                    query.append(_SQL_SELECT_CONTACT_WHERE_PKS_IN);
2118    
2119                    for (Serializable primaryKey : uncachedPrimaryKeys) {
2120                            query.append(String.valueOf(primaryKey));
2121    
2122                            query.append(StringPool.COMMA);
2123                    }
2124    
2125                    query.setIndex(query.index() - 1);
2126    
2127                    query.append(StringPool.CLOSE_PARENTHESIS);
2128    
2129                    String sql = query.toString();
2130    
2131                    Session session = null;
2132    
2133                    try {
2134                            session = openSession();
2135    
2136                            Query q = session.createQuery(sql);
2137    
2138                            for (Contact contact : (List<Contact>)q.list()) {
2139                                    map.put(contact.getPrimaryKeyObj(), contact);
2140    
2141                                    cacheResult(contact);
2142    
2143                                    uncachedPrimaryKeys.remove(contact.getPrimaryKeyObj());
2144                            }
2145    
2146                            for (Serializable primaryKey : uncachedPrimaryKeys) {
2147                                    entityCache.putResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
2148                                            ContactImpl.class, primaryKey, _nullContact);
2149                            }
2150                    }
2151                    catch (Exception e) {
2152                            throw processException(e);
2153                    }
2154                    finally {
2155                            closeSession(session);
2156                    }
2157    
2158                    return map;
2159            }
2160    
2161            /**
2162             * Returns all the contacts.
2163             *
2164             * @return the contacts
2165             */
2166            @Override
2167            public List<Contact> findAll() {
2168                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
2169            }
2170    
2171            /**
2172             * Returns a range of all the contacts.
2173             *
2174             * <p>
2175             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ContactModelImpl}. 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.
2176             * </p>
2177             *
2178             * @param start the lower bound of the range of contacts
2179             * @param end the upper bound of the range of contacts (not inclusive)
2180             * @return the range of contacts
2181             */
2182            @Override
2183            public List<Contact> findAll(int start, int end) {
2184                    return findAll(start, end, null);
2185            }
2186    
2187            /**
2188             * Returns an ordered range of all the contacts.
2189             *
2190             * <p>
2191             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ContactModelImpl}. 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.
2192             * </p>
2193             *
2194             * @param start the lower bound of the range of contacts
2195             * @param end the upper bound of the range of contacts (not inclusive)
2196             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2197             * @return the ordered range of contacts
2198             */
2199            @Override
2200            public List<Contact> findAll(int start, int end,
2201                    OrderByComparator<Contact> orderByComparator) {
2202                    return findAll(start, end, orderByComparator, true);
2203            }
2204    
2205            /**
2206             * Returns an ordered range of all the contacts.
2207             *
2208             * <p>
2209             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ContactModelImpl}. 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.
2210             * </p>
2211             *
2212             * @param start the lower bound of the range of contacts
2213             * @param end the upper bound of the range of contacts (not inclusive)
2214             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2215             * @param retrieveFromCache whether to retrieve from the finder cache
2216             * @return the ordered range of contacts
2217             */
2218            @Override
2219            public List<Contact> findAll(int start, int end,
2220                    OrderByComparator<Contact> orderByComparator, boolean retrieveFromCache) {
2221                    boolean pagination = true;
2222                    FinderPath finderPath = null;
2223                    Object[] finderArgs = null;
2224    
2225                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2226                                    (orderByComparator == null)) {
2227                            pagination = false;
2228                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
2229                            finderArgs = FINDER_ARGS_EMPTY;
2230                    }
2231                    else {
2232                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
2233                            finderArgs = new Object[] { start, end, orderByComparator };
2234                    }
2235    
2236                    List<Contact> list = null;
2237    
2238                    if (retrieveFromCache) {
2239                            list = (List<Contact>)finderCache.getResult(finderPath, finderArgs,
2240                                            this);
2241                    }
2242    
2243                    if (list == null) {
2244                            StringBundler query = null;
2245                            String sql = null;
2246    
2247                            if (orderByComparator != null) {
2248                                    query = new StringBundler(2 +
2249                                                    (orderByComparator.getOrderByFields().length * 3));
2250    
2251                                    query.append(_SQL_SELECT_CONTACT);
2252    
2253                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2254                                            orderByComparator);
2255    
2256                                    sql = query.toString();
2257                            }
2258                            else {
2259                                    sql = _SQL_SELECT_CONTACT;
2260    
2261                                    if (pagination) {
2262                                            sql = sql.concat(ContactModelImpl.ORDER_BY_JPQL);
2263                                    }
2264                            }
2265    
2266                            Session session = null;
2267    
2268                            try {
2269                                    session = openSession();
2270    
2271                                    Query q = session.createQuery(sql);
2272    
2273                                    if (!pagination) {
2274                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
2275                                                            start, end, false);
2276    
2277                                            Collections.sort(list);
2278    
2279                                            list = Collections.unmodifiableList(list);
2280                                    }
2281                                    else {
2282                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
2283                                                            start, end);
2284                                    }
2285    
2286                                    cacheResult(list);
2287    
2288                                    finderCache.putResult(finderPath, finderArgs, list);
2289                            }
2290                            catch (Exception e) {
2291                                    finderCache.removeResult(finderPath, finderArgs);
2292    
2293                                    throw processException(e);
2294                            }
2295                            finally {
2296                                    closeSession(session);
2297                            }
2298                    }
2299    
2300                    return list;
2301            }
2302    
2303            /**
2304             * Removes all the contacts from the database.
2305             *
2306             */
2307            @Override
2308            public void removeAll() {
2309                    for (Contact contact : findAll()) {
2310                            remove(contact);
2311                    }
2312            }
2313    
2314            /**
2315             * Returns the number of contacts.
2316             *
2317             * @return the number of contacts
2318             */
2319            @Override
2320            public int countAll() {
2321                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
2322                                    FINDER_ARGS_EMPTY, this);
2323    
2324                    if (count == null) {
2325                            Session session = null;
2326    
2327                            try {
2328                                    session = openSession();
2329    
2330                                    Query q = session.createQuery(_SQL_COUNT_CONTACT);
2331    
2332                                    count = (Long)q.uniqueResult();
2333    
2334                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
2335                                            count);
2336                            }
2337                            catch (Exception e) {
2338                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
2339                                            FINDER_ARGS_EMPTY);
2340    
2341                                    throw processException(e);
2342                            }
2343                            finally {
2344                                    closeSession(session);
2345                            }
2346                    }
2347    
2348                    return count.intValue();
2349            }
2350    
2351            @Override
2352            protected Map<String, Integer> getTableColumnsMap() {
2353                    return ContactModelImpl.TABLE_COLUMNS_MAP;
2354            }
2355    
2356            /**
2357             * Initializes the contact persistence.
2358             */
2359            public void afterPropertiesSet() {
2360            }
2361    
2362            public void destroy() {
2363                    entityCache.removeCache(ContactImpl.class.getName());
2364                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
2365                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2366                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2367            }
2368    
2369            @BeanReference(type = CompanyProviderWrapper.class)
2370            protected CompanyProvider companyProvider;
2371            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
2372            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
2373            private static final String _SQL_SELECT_CONTACT = "SELECT contact FROM Contact contact";
2374            private static final String _SQL_SELECT_CONTACT_WHERE_PKS_IN = "SELECT contact FROM Contact contact WHERE contactId IN (";
2375            private static final String _SQL_SELECT_CONTACT_WHERE = "SELECT contact FROM Contact contact WHERE ";
2376            private static final String _SQL_COUNT_CONTACT = "SELECT COUNT(contact) FROM Contact contact";
2377            private static final String _SQL_COUNT_CONTACT_WHERE = "SELECT COUNT(contact) FROM Contact contact WHERE ";
2378            private static final String _ORDER_BY_ENTITY_ALIAS = "contact.";
2379            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Contact exists with the primary key ";
2380            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Contact exists with the key {";
2381            private static final Log _log = LogFactoryUtil.getLog(ContactPersistenceImpl.class);
2382            private static final Contact _nullContact = new ContactImpl() {
2383                            @Override
2384                            public Object clone() {
2385                                    return this;
2386                            }
2387    
2388                            @Override
2389                            public CacheModel<Contact> toCacheModel() {
2390                                    return _nullContactCacheModel;
2391                            }
2392                    };
2393    
2394            private static final CacheModel<Contact> _nullContactCacheModel = new NullCacheModel();
2395    
2396            private static class NullCacheModel implements CacheModel<Contact>,
2397                    MVCCModel {
2398                    @Override
2399                    public long getMvccVersion() {
2400                            return -1;
2401                    }
2402    
2403                    @Override
2404                    public void setMvccVersion(long mvccVersion) {
2405                    }
2406    
2407                    @Override
2408                    public Contact toEntityModel() {
2409                            return _nullContact;
2410                    }
2411            }
2412    }