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