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