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