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