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