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