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