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            public ContactPersistenceImpl() {
1589                    setModelClass(Contact.class);
1590            }
1591    
1592            /**
1593             * Caches the contact in the entity cache if it is enabled.
1594             *
1595             * @param contact the contact
1596             */
1597            @Override
1598            public void cacheResult(Contact contact) {
1599                    EntityCacheUtil.putResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
1600                            ContactImpl.class, contact.getPrimaryKey(), contact);
1601    
1602                    contact.resetOriginalValues();
1603            }
1604    
1605            /**
1606             * Caches the contacts in the entity cache if it is enabled.
1607             *
1608             * @param contacts the contacts
1609             */
1610            @Override
1611            public void cacheResult(List<Contact> contacts) {
1612                    for (Contact contact : contacts) {
1613                            if (EntityCacheUtil.getResult(
1614                                                    ContactModelImpl.ENTITY_CACHE_ENABLED,
1615                                                    ContactImpl.class, contact.getPrimaryKey()) == null) {
1616                                    cacheResult(contact);
1617                            }
1618                            else {
1619                                    contact.resetOriginalValues();
1620                            }
1621                    }
1622            }
1623    
1624            /**
1625             * Clears the cache for all contacts.
1626             *
1627             * <p>
1628             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1629             * </p>
1630             */
1631            @Override
1632            public void clearCache() {
1633                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
1634                            CacheRegistryUtil.clear(ContactImpl.class.getName());
1635                    }
1636    
1637                    EntityCacheUtil.clearCache(ContactImpl.class.getName());
1638    
1639                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
1640                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1641                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1642            }
1643    
1644            /**
1645             * Clears the cache for the contact.
1646             *
1647             * <p>
1648             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1649             * </p>
1650             */
1651            @Override
1652            public void clearCache(Contact contact) {
1653                    EntityCacheUtil.removeResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
1654                            ContactImpl.class, contact.getPrimaryKey());
1655    
1656                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1657                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1658            }
1659    
1660            @Override
1661            public void clearCache(List<Contact> contacts) {
1662                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1663                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1664    
1665                    for (Contact contact : contacts) {
1666                            EntityCacheUtil.removeResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
1667                                    ContactImpl.class, contact.getPrimaryKey());
1668                    }
1669            }
1670    
1671            /**
1672             * Creates a new contact with the primary key. Does not add the contact to the database.
1673             *
1674             * @param contactId the primary key for the new contact
1675             * @return the new contact
1676             */
1677            @Override
1678            public Contact create(long contactId) {
1679                    Contact contact = new ContactImpl();
1680    
1681                    contact.setNew(true);
1682                    contact.setPrimaryKey(contactId);
1683    
1684                    return contact;
1685            }
1686    
1687            /**
1688             * Removes the contact with the primary key from the database. Also notifies the appropriate model listeners.
1689             *
1690             * @param contactId the primary key of the contact
1691             * @return the contact that was removed
1692             * @throws com.liferay.portal.NoSuchContactException if a contact with the primary key could not be found
1693             * @throws SystemException if a system exception occurred
1694             */
1695            @Override
1696            public Contact remove(long contactId)
1697                    throws NoSuchContactException, SystemException {
1698                    return remove((Serializable)contactId);
1699            }
1700    
1701            /**
1702             * Removes the contact with the primary key from the database. Also notifies the appropriate model listeners.
1703             *
1704             * @param primaryKey the primary key of the contact
1705             * @return the contact that was removed
1706             * @throws com.liferay.portal.NoSuchContactException if a contact with the primary key could not be found
1707             * @throws SystemException if a system exception occurred
1708             */
1709            @Override
1710            public Contact remove(Serializable primaryKey)
1711                    throws NoSuchContactException, SystemException {
1712                    Session session = null;
1713    
1714                    try {
1715                            session = openSession();
1716    
1717                            Contact contact = (Contact)session.get(ContactImpl.class, primaryKey);
1718    
1719                            if (contact == null) {
1720                                    if (_log.isWarnEnabled()) {
1721                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1722                                    }
1723    
1724                                    throw new NoSuchContactException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1725                                            primaryKey);
1726                            }
1727    
1728                            return remove(contact);
1729                    }
1730                    catch (NoSuchContactException nsee) {
1731                            throw nsee;
1732                    }
1733                    catch (Exception e) {
1734                            throw processException(e);
1735                    }
1736                    finally {
1737                            closeSession(session);
1738                    }
1739            }
1740    
1741            @Override
1742            protected Contact removeImpl(Contact contact) throws SystemException {
1743                    contact = toUnwrappedModel(contact);
1744    
1745                    Session session = null;
1746    
1747                    try {
1748                            session = openSession();
1749    
1750                            if (!session.contains(contact)) {
1751                                    contact = (Contact)session.get(ContactImpl.class,
1752                                                    contact.getPrimaryKeyObj());
1753                            }
1754    
1755                            if (contact != null) {
1756                                    session.delete(contact);
1757                            }
1758                    }
1759                    catch (Exception e) {
1760                            throw processException(e);
1761                    }
1762                    finally {
1763                            closeSession(session);
1764                    }
1765    
1766                    if (contact != null) {
1767                            clearCache(contact);
1768                    }
1769    
1770                    return contact;
1771            }
1772    
1773            @Override
1774            public Contact updateImpl(com.liferay.portal.model.Contact contact)
1775                    throws SystemException {
1776                    contact = toUnwrappedModel(contact);
1777    
1778                    boolean isNew = contact.isNew();
1779    
1780                    ContactModelImpl contactModelImpl = (ContactModelImpl)contact;
1781    
1782                    Session session = null;
1783    
1784                    try {
1785                            session = openSession();
1786    
1787                            if (contact.isNew()) {
1788                                    session.save(contact);
1789    
1790                                    contact.setNew(false);
1791                            }
1792                            else {
1793                                    session.merge(contact);
1794                            }
1795                    }
1796                    catch (Exception e) {
1797                            throw processException(e);
1798                    }
1799                    finally {
1800                            closeSession(session);
1801                    }
1802    
1803                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1804    
1805                    if (isNew || !ContactModelImpl.COLUMN_BITMASK_ENABLED) {
1806                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1807                    }
1808    
1809                    else {
1810                            if ((contactModelImpl.getColumnBitmask() &
1811                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID.getColumnBitmask()) != 0) {
1812                                    Object[] args = new Object[] {
1813                                                    contactModelImpl.getOriginalCompanyId()
1814                                            };
1815    
1816                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPANYID,
1817                                            args);
1818                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
1819                                            args);
1820    
1821                                    args = new Object[] { contactModelImpl.getCompanyId() };
1822    
1823                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPANYID,
1824                                            args);
1825                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
1826                                            args);
1827                            }
1828    
1829                            if ((contactModelImpl.getColumnBitmask() &
1830                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ACCOUNTID.getColumnBitmask()) != 0) {
1831                                    Object[] args = new Object[] {
1832                                                    contactModelImpl.getOriginalAccountId()
1833                                            };
1834    
1835                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ACCOUNTID,
1836                                            args);
1837                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ACCOUNTID,
1838                                            args);
1839    
1840                                    args = new Object[] { contactModelImpl.getAccountId() };
1841    
1842                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ACCOUNTID,
1843                                            args);
1844                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ACCOUNTID,
1845                                            args);
1846                            }
1847    
1848                            if ((contactModelImpl.getColumnBitmask() &
1849                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C.getColumnBitmask()) != 0) {
1850                                    Object[] args = new Object[] {
1851                                                    contactModelImpl.getOriginalClassNameId(),
1852                                                    contactModelImpl.getOriginalClassPK()
1853                                            };
1854    
1855                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
1856                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C,
1857                                            args);
1858    
1859                                    args = new Object[] {
1860                                                    contactModelImpl.getClassNameId(),
1861                                                    contactModelImpl.getClassPK()
1862                                            };
1863    
1864                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
1865                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C,
1866                                            args);
1867                            }
1868                    }
1869    
1870                    EntityCacheUtil.putResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
1871                            ContactImpl.class, contact.getPrimaryKey(), contact);
1872    
1873                    return contact;
1874            }
1875    
1876            protected Contact toUnwrappedModel(Contact contact) {
1877                    if (contact instanceof ContactImpl) {
1878                            return contact;
1879                    }
1880    
1881                    ContactImpl contactImpl = new ContactImpl();
1882    
1883                    contactImpl.setNew(contact.isNew());
1884                    contactImpl.setPrimaryKey(contact.getPrimaryKey());
1885    
1886                    contactImpl.setContactId(contact.getContactId());
1887                    contactImpl.setCompanyId(contact.getCompanyId());
1888                    contactImpl.setUserId(contact.getUserId());
1889                    contactImpl.setUserName(contact.getUserName());
1890                    contactImpl.setCreateDate(contact.getCreateDate());
1891                    contactImpl.setModifiedDate(contact.getModifiedDate());
1892                    contactImpl.setClassNameId(contact.getClassNameId());
1893                    contactImpl.setClassPK(contact.getClassPK());
1894                    contactImpl.setAccountId(contact.getAccountId());
1895                    contactImpl.setParentContactId(contact.getParentContactId());
1896                    contactImpl.setEmailAddress(contact.getEmailAddress());
1897                    contactImpl.setFirstName(contact.getFirstName());
1898                    contactImpl.setMiddleName(contact.getMiddleName());
1899                    contactImpl.setLastName(contact.getLastName());
1900                    contactImpl.setPrefixId(contact.getPrefixId());
1901                    contactImpl.setSuffixId(contact.getSuffixId());
1902                    contactImpl.setMale(contact.isMale());
1903                    contactImpl.setBirthday(contact.getBirthday());
1904                    contactImpl.setSmsSn(contact.getSmsSn());
1905                    contactImpl.setAimSn(contact.getAimSn());
1906                    contactImpl.setFacebookSn(contact.getFacebookSn());
1907                    contactImpl.setIcqSn(contact.getIcqSn());
1908                    contactImpl.setJabberSn(contact.getJabberSn());
1909                    contactImpl.setMsnSn(contact.getMsnSn());
1910                    contactImpl.setMySpaceSn(contact.getMySpaceSn());
1911                    contactImpl.setSkypeSn(contact.getSkypeSn());
1912                    contactImpl.setTwitterSn(contact.getTwitterSn());
1913                    contactImpl.setYmSn(contact.getYmSn());
1914                    contactImpl.setEmployeeStatusId(contact.getEmployeeStatusId());
1915                    contactImpl.setEmployeeNumber(contact.getEmployeeNumber());
1916                    contactImpl.setJobTitle(contact.getJobTitle());
1917                    contactImpl.setJobClass(contact.getJobClass());
1918                    contactImpl.setHoursOfOperation(contact.getHoursOfOperation());
1919    
1920                    return contactImpl;
1921            }
1922    
1923            /**
1924             * Returns the contact with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1925             *
1926             * @param primaryKey the primary key of the contact
1927             * @return the contact
1928             * @throws com.liferay.portal.NoSuchContactException if a contact with the primary key could not be found
1929             * @throws SystemException if a system exception occurred
1930             */
1931            @Override
1932            public Contact findByPrimaryKey(Serializable primaryKey)
1933                    throws NoSuchContactException, SystemException {
1934                    Contact contact = fetchByPrimaryKey(primaryKey);
1935    
1936                    if (contact == null) {
1937                            if (_log.isWarnEnabled()) {
1938                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1939                            }
1940    
1941                            throw new NoSuchContactException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1942                                    primaryKey);
1943                    }
1944    
1945                    return contact;
1946            }
1947    
1948            /**
1949             * Returns the contact with the primary key or throws a {@link com.liferay.portal.NoSuchContactException} if it could not be found.
1950             *
1951             * @param contactId the primary key of the contact
1952             * @return the contact
1953             * @throws com.liferay.portal.NoSuchContactException if a contact with the primary key could not be found
1954             * @throws SystemException if a system exception occurred
1955             */
1956            @Override
1957            public Contact findByPrimaryKey(long contactId)
1958                    throws NoSuchContactException, SystemException {
1959                    return findByPrimaryKey((Serializable)contactId);
1960            }
1961    
1962            /**
1963             * Returns the contact with the primary key or returns <code>null</code> if it could not be found.
1964             *
1965             * @param primaryKey the primary key of the contact
1966             * @return the contact, or <code>null</code> if a contact with the primary key could not be found
1967             * @throws SystemException if a system exception occurred
1968             */
1969            @Override
1970            public Contact fetchByPrimaryKey(Serializable primaryKey)
1971                    throws SystemException {
1972                    Contact contact = (Contact)EntityCacheUtil.getResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
1973                                    ContactImpl.class, primaryKey);
1974    
1975                    if (contact == _nullContact) {
1976                            return null;
1977                    }
1978    
1979                    if (contact == null) {
1980                            Session session = null;
1981    
1982                            try {
1983                                    session = openSession();
1984    
1985                                    contact = (Contact)session.get(ContactImpl.class, primaryKey);
1986    
1987                                    if (contact != null) {
1988                                            cacheResult(contact);
1989                                    }
1990                                    else {
1991                                            EntityCacheUtil.putResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
1992                                                    ContactImpl.class, primaryKey, _nullContact);
1993                                    }
1994                            }
1995                            catch (Exception e) {
1996                                    EntityCacheUtil.removeResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
1997                                            ContactImpl.class, primaryKey);
1998    
1999                                    throw processException(e);
2000                            }
2001                            finally {
2002                                    closeSession(session);
2003                            }
2004                    }
2005    
2006                    return contact;
2007            }
2008    
2009            /**
2010             * Returns the contact with the primary key or returns <code>null</code> if it could not be found.
2011             *
2012             * @param contactId the primary key of the contact
2013             * @return the contact, or <code>null</code> if a contact with the primary key could not be found
2014             * @throws SystemException if a system exception occurred
2015             */
2016            @Override
2017            public Contact fetchByPrimaryKey(long contactId) throws SystemException {
2018                    return fetchByPrimaryKey((Serializable)contactId);
2019            }
2020    
2021            /**
2022             * Returns all the contacts.
2023             *
2024             * @return the contacts
2025             * @throws SystemException if a system exception occurred
2026             */
2027            @Override
2028            public List<Contact> findAll() throws SystemException {
2029                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
2030            }
2031    
2032            /**
2033             * Returns a range of all the contacts.
2034             *
2035             * <p>
2036             * 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.
2037             * </p>
2038             *
2039             * @param start the lower bound of the range of contacts
2040             * @param end the upper bound of the range of contacts (not inclusive)
2041             * @return the range of contacts
2042             * @throws SystemException if a system exception occurred
2043             */
2044            @Override
2045            public List<Contact> findAll(int start, int end) throws SystemException {
2046                    return findAll(start, end, null);
2047            }
2048    
2049            /**
2050             * Returns an ordered range of all the contacts.
2051             *
2052             * <p>
2053             * 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.
2054             * </p>
2055             *
2056             * @param start the lower bound of the range of contacts
2057             * @param end the upper bound of the range of contacts (not inclusive)
2058             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2059             * @return the ordered range of contacts
2060             * @throws SystemException if a system exception occurred
2061             */
2062            @Override
2063            public List<Contact> findAll(int start, int end,
2064                    OrderByComparator orderByComparator) throws SystemException {
2065                    boolean pagination = true;
2066                    FinderPath finderPath = null;
2067                    Object[] finderArgs = null;
2068    
2069                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2070                                    (orderByComparator == null)) {
2071                            pagination = false;
2072                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
2073                            finderArgs = FINDER_ARGS_EMPTY;
2074                    }
2075                    else {
2076                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
2077                            finderArgs = new Object[] { start, end, orderByComparator };
2078                    }
2079    
2080                    List<Contact> list = (List<Contact>)FinderCacheUtil.getResult(finderPath,
2081                                    finderArgs, this);
2082    
2083                    if (list == null) {
2084                            StringBundler query = null;
2085                            String sql = null;
2086    
2087                            if (orderByComparator != null) {
2088                                    query = new StringBundler(2 +
2089                                                    (orderByComparator.getOrderByFields().length * 3));
2090    
2091                                    query.append(_SQL_SELECT_CONTACT);
2092    
2093                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2094                                            orderByComparator);
2095    
2096                                    sql = query.toString();
2097                            }
2098                            else {
2099                                    sql = _SQL_SELECT_CONTACT;
2100    
2101                                    if (pagination) {
2102                                            sql = sql.concat(ContactModelImpl.ORDER_BY_JPQL);
2103                                    }
2104                            }
2105    
2106                            Session session = null;
2107    
2108                            try {
2109                                    session = openSession();
2110    
2111                                    Query q = session.createQuery(sql);
2112    
2113                                    if (!pagination) {
2114                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
2115                                                            start, end, false);
2116    
2117                                            Collections.sort(list);
2118    
2119                                            list = new UnmodifiableList<Contact>(list);
2120                                    }
2121                                    else {
2122                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
2123                                                            start, end);
2124                                    }
2125    
2126                                    cacheResult(list);
2127    
2128                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2129                            }
2130                            catch (Exception e) {
2131                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2132    
2133                                    throw processException(e);
2134                            }
2135                            finally {
2136                                    closeSession(session);
2137                            }
2138                    }
2139    
2140                    return list;
2141            }
2142    
2143            /**
2144             * Removes all the contacts from the database.
2145             *
2146             * @throws SystemException if a system exception occurred
2147             */
2148            @Override
2149            public void removeAll() throws SystemException {
2150                    for (Contact contact : findAll()) {
2151                            remove(contact);
2152                    }
2153            }
2154    
2155            /**
2156             * Returns the number of contacts.
2157             *
2158             * @return the number of contacts
2159             * @throws SystemException if a system exception occurred
2160             */
2161            @Override
2162            public int countAll() throws SystemException {
2163                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
2164                                    FINDER_ARGS_EMPTY, this);
2165    
2166                    if (count == null) {
2167                            Session session = null;
2168    
2169                            try {
2170                                    session = openSession();
2171    
2172                                    Query q = session.createQuery(_SQL_COUNT_CONTACT);
2173    
2174                                    count = (Long)q.uniqueResult();
2175    
2176                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
2177                                            FINDER_ARGS_EMPTY, count);
2178                            }
2179                            catch (Exception e) {
2180                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
2181                                            FINDER_ARGS_EMPTY);
2182    
2183                                    throw processException(e);
2184                            }
2185                            finally {
2186                                    closeSession(session);
2187                            }
2188                    }
2189    
2190                    return count.intValue();
2191            }
2192    
2193            /**
2194             * Initializes the contact persistence.
2195             */
2196            public void afterPropertiesSet() {
2197                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2198                                            com.liferay.portal.util.PropsUtil.get(
2199                                                    "value.object.listener.com.liferay.portal.model.Contact")));
2200    
2201                    if (listenerClassNames.length > 0) {
2202                            try {
2203                                    List<ModelListener<Contact>> listenersList = new ArrayList<ModelListener<Contact>>();
2204    
2205                                    for (String listenerClassName : listenerClassNames) {
2206                                            listenersList.add((ModelListener<Contact>)InstanceFactory.newInstance(
2207                                                            getClassLoader(), listenerClassName));
2208                                    }
2209    
2210                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
2211                            }
2212                            catch (Exception e) {
2213                                    _log.error(e);
2214                            }
2215                    }
2216            }
2217    
2218            public void destroy() {
2219                    EntityCacheUtil.removeCache(ContactImpl.class.getName());
2220                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
2221                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2222                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2223            }
2224    
2225            private static final String _SQL_SELECT_CONTACT = "SELECT contact FROM Contact contact";
2226            private static final String _SQL_SELECT_CONTACT_WHERE = "SELECT contact FROM Contact contact WHERE ";
2227            private static final String _SQL_COUNT_CONTACT = "SELECT COUNT(contact) FROM Contact contact";
2228            private static final String _SQL_COUNT_CONTACT_WHERE = "SELECT COUNT(contact) FROM Contact contact WHERE ";
2229            private static final String _ORDER_BY_ENTITY_ALIAS = "contact.";
2230            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Contact exists with the primary key ";
2231            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Contact exists with the key {";
2232            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
2233            private static Log _log = LogFactoryUtil.getLog(ContactPersistenceImpl.class);
2234            private static Contact _nullContact = new ContactImpl() {
2235                            @Override
2236                            public Object clone() {
2237                                    return this;
2238                            }
2239    
2240                            @Override
2241                            public CacheModel<Contact> toCacheModel() {
2242                                    return _nullContactCacheModel;
2243                            }
2244                    };
2245    
2246            private static CacheModel<Contact> _nullContactCacheModel = new CacheModel<Contact>() {
2247                            @Override
2248                            public Contact toEntityModel() {
2249                                    return _nullContact;
2250                            }
2251                    };
2252    }