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