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