001    /**
002     * Copyright (c) 2000-2011 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.annotation.BeanReference;
020    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
023    import com.liferay.portal.kernel.dao.orm.FinderPath;
024    import com.liferay.portal.kernel.dao.orm.Query;
025    import com.liferay.portal.kernel.dao.orm.QueryPos;
026    import com.liferay.portal.kernel.dao.orm.QueryUtil;
027    import com.liferay.portal.kernel.dao.orm.Session;
028    import com.liferay.portal.kernel.exception.SystemException;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.InstanceFactory;
033    import com.liferay.portal.kernel.util.OrderByComparator;
034    import com.liferay.portal.kernel.util.StringBundler;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.model.Contact;
038    import com.liferay.portal.model.ModelListener;
039    import com.liferay.portal.model.impl.ContactImpl;
040    import com.liferay.portal.model.impl.ContactModelImpl;
041    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
042    
043    import java.io.Serializable;
044    
045    import java.util.ArrayList;
046    import java.util.Collections;
047    import java.util.List;
048    
049    /**
050     * The persistence implementation for the contact service.
051     *
052     * <p>
053     * 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.
054     * </p>
055     *
056     * <p>
057     * Caching information and settings can be found in <code>portal.properties</code>
058     * </p>
059     *
060     * @author Brian Wing Shun Chan
061     * @see ContactPersistence
062     * @see ContactUtil
063     * @generated
064     */
065    public class ContactPersistenceImpl extends BasePersistenceImpl<Contact>
066            implements ContactPersistence {
067            public static final String FINDER_CLASS_NAME_ENTITY = ContactImpl.class.getName();
068            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
069                    ".List";
070            public static final FinderPath FINDER_PATH_FIND_BY_COMPANYID = new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
071                            ContactModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
072                            "findByCompanyId",
073                            new String[] {
074                                    Long.class.getName(),
075                                    
076                            "java.lang.Integer", "java.lang.Integer",
077                                    "com.liferay.portal.kernel.util.OrderByComparator"
078                            });
079            public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
080                            ContactModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
081                            "countByCompanyId", new String[] { Long.class.getName() });
082            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
083                            ContactModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
084                            "findAll", new String[0]);
085            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ContactModelImpl.ENTITY_CACHE_ENABLED,
086                            ContactModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
087                            "countAll", new String[0]);
088    
089            /**
090             * Caches the contact in the entity cache if it is enabled.
091             *
092             * @param contact the contact to cache
093             */
094            public void cacheResult(Contact contact) {
095                    EntityCacheUtil.putResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
096                            ContactImpl.class, contact.getPrimaryKey(), contact);
097            }
098    
099            /**
100             * Caches the contacts in the entity cache if it is enabled.
101             *
102             * @param contacts the contacts to cache
103             */
104            public void cacheResult(List<Contact> contacts) {
105                    for (Contact contact : contacts) {
106                            if (EntityCacheUtil.getResult(
107                                                    ContactModelImpl.ENTITY_CACHE_ENABLED,
108                                                    ContactImpl.class, contact.getPrimaryKey(), this) == null) {
109                                    cacheResult(contact);
110                            }
111                    }
112            }
113    
114            /**
115             * Clears the cache for all contacts.
116             *
117             * <p>
118             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
119             * </p>
120             */
121            public void clearCache() {
122                    CacheRegistryUtil.clear(ContactImpl.class.getName());
123                    EntityCacheUtil.clearCache(ContactImpl.class.getName());
124                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
125                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
126            }
127    
128            /**
129             * Clears the cache for the contact.
130             *
131             * <p>
132             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
133             * </p>
134             */
135            public void clearCache(Contact contact) {
136                    EntityCacheUtil.removeResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
137                            ContactImpl.class, contact.getPrimaryKey());
138            }
139    
140            /**
141             * Creates a new contact with the primary key. Does not add the contact to the database.
142             *
143             * @param contactId the primary key for the new contact
144             * @return the new contact
145             */
146            public Contact create(long contactId) {
147                    Contact contact = new ContactImpl();
148    
149                    contact.setNew(true);
150                    contact.setPrimaryKey(contactId);
151    
152                    return contact;
153            }
154    
155            /**
156             * Removes the contact with the primary key from the database. Also notifies the appropriate model listeners.
157             *
158             * @param primaryKey the primary key of the contact to remove
159             * @return the contact that was removed
160             * @throws com.liferay.portal.NoSuchModelException if a contact with the primary key could not be found
161             * @throws SystemException if a system exception occurred
162             */
163            public Contact remove(Serializable primaryKey)
164                    throws NoSuchModelException, SystemException {
165                    return remove(((Long)primaryKey).longValue());
166            }
167    
168            /**
169             * Removes the contact with the primary key from the database. Also notifies the appropriate model listeners.
170             *
171             * @param contactId the primary key of the contact to remove
172             * @return the contact that was removed
173             * @throws com.liferay.portal.NoSuchContactException if a contact with the primary key could not be found
174             * @throws SystemException if a system exception occurred
175             */
176            public Contact remove(long contactId)
177                    throws NoSuchContactException, SystemException {
178                    Session session = null;
179    
180                    try {
181                            session = openSession();
182    
183                            Contact contact = (Contact)session.get(ContactImpl.class,
184                                            new Long(contactId));
185    
186                            if (contact == null) {
187                                    if (_log.isWarnEnabled()) {
188                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + contactId);
189                                    }
190    
191                                    throw new NoSuchContactException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
192                                            contactId);
193                            }
194    
195                            return remove(contact);
196                    }
197                    catch (NoSuchContactException nsee) {
198                            throw nsee;
199                    }
200                    catch (Exception e) {
201                            throw processException(e);
202                    }
203                    finally {
204                            closeSession(session);
205                    }
206            }
207    
208            protected Contact removeImpl(Contact contact) throws SystemException {
209                    contact = toUnwrappedModel(contact);
210    
211                    Session session = null;
212    
213                    try {
214                            session = openSession();
215    
216                            BatchSessionUtil.delete(session, contact);
217                    }
218                    catch (Exception e) {
219                            throw processException(e);
220                    }
221                    finally {
222                            closeSession(session);
223                    }
224    
225                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
226    
227                    EntityCacheUtil.removeResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
228                            ContactImpl.class, contact.getPrimaryKey());
229    
230                    return contact;
231            }
232    
233            public Contact updateImpl(com.liferay.portal.model.Contact contact,
234                    boolean merge) throws SystemException {
235                    contact = toUnwrappedModel(contact);
236    
237                    Session session = null;
238    
239                    try {
240                            session = openSession();
241    
242                            BatchSessionUtil.update(session, contact, merge);
243    
244                            contact.setNew(false);
245                    }
246                    catch (Exception e) {
247                            throw processException(e);
248                    }
249                    finally {
250                            closeSession(session);
251                    }
252    
253                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
254    
255                    EntityCacheUtil.putResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
256                            ContactImpl.class, contact.getPrimaryKey(), contact);
257    
258                    return contact;
259            }
260    
261            protected Contact toUnwrappedModel(Contact contact) {
262                    if (contact instanceof ContactImpl) {
263                            return contact;
264                    }
265    
266                    ContactImpl contactImpl = new ContactImpl();
267    
268                    contactImpl.setNew(contact.isNew());
269                    contactImpl.setPrimaryKey(contact.getPrimaryKey());
270    
271                    contactImpl.setContactId(contact.getContactId());
272                    contactImpl.setCompanyId(contact.getCompanyId());
273                    contactImpl.setUserId(contact.getUserId());
274                    contactImpl.setUserName(contact.getUserName());
275                    contactImpl.setCreateDate(contact.getCreateDate());
276                    contactImpl.setModifiedDate(contact.getModifiedDate());
277                    contactImpl.setAccountId(contact.getAccountId());
278                    contactImpl.setParentContactId(contact.getParentContactId());
279                    contactImpl.setFirstName(contact.getFirstName());
280                    contactImpl.setMiddleName(contact.getMiddleName());
281                    contactImpl.setLastName(contact.getLastName());
282                    contactImpl.setPrefixId(contact.getPrefixId());
283                    contactImpl.setSuffixId(contact.getSuffixId());
284                    contactImpl.setMale(contact.isMale());
285                    contactImpl.setBirthday(contact.getBirthday());
286                    contactImpl.setSmsSn(contact.getSmsSn());
287                    contactImpl.setAimSn(contact.getAimSn());
288                    contactImpl.setFacebookSn(contact.getFacebookSn());
289                    contactImpl.setIcqSn(contact.getIcqSn());
290                    contactImpl.setJabberSn(contact.getJabberSn());
291                    contactImpl.setMsnSn(contact.getMsnSn());
292                    contactImpl.setMySpaceSn(contact.getMySpaceSn());
293                    contactImpl.setSkypeSn(contact.getSkypeSn());
294                    contactImpl.setTwitterSn(contact.getTwitterSn());
295                    contactImpl.setYmSn(contact.getYmSn());
296                    contactImpl.setEmployeeStatusId(contact.getEmployeeStatusId());
297                    contactImpl.setEmployeeNumber(contact.getEmployeeNumber());
298                    contactImpl.setJobTitle(contact.getJobTitle());
299                    contactImpl.setJobClass(contact.getJobClass());
300                    contactImpl.setHoursOfOperation(contact.getHoursOfOperation());
301    
302                    return contactImpl;
303            }
304    
305            /**
306             * Finds the contact with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
307             *
308             * @param primaryKey the primary key of the contact to find
309             * @return the contact
310             * @throws com.liferay.portal.NoSuchModelException if a contact with the primary key could not be found
311             * @throws SystemException if a system exception occurred
312             */
313            public Contact findByPrimaryKey(Serializable primaryKey)
314                    throws NoSuchModelException, SystemException {
315                    return findByPrimaryKey(((Long)primaryKey).longValue());
316            }
317    
318            /**
319             * Finds the contact with the primary key or throws a {@link com.liferay.portal.NoSuchContactException} if it could not be found.
320             *
321             * @param contactId the primary key of the contact to find
322             * @return the contact
323             * @throws com.liferay.portal.NoSuchContactException if a contact with the primary key could not be found
324             * @throws SystemException if a system exception occurred
325             */
326            public Contact findByPrimaryKey(long contactId)
327                    throws NoSuchContactException, SystemException {
328                    Contact contact = fetchByPrimaryKey(contactId);
329    
330                    if (contact == null) {
331                            if (_log.isWarnEnabled()) {
332                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + contactId);
333                            }
334    
335                            throw new NoSuchContactException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
336                                    contactId);
337                    }
338    
339                    return contact;
340            }
341    
342            /**
343             * Finds the contact with the primary key or returns <code>null</code> if it could not be found.
344             *
345             * @param primaryKey the primary key of the contact to find
346             * @return the contact, or <code>null</code> if a contact with the primary key could not be found
347             * @throws SystemException if a system exception occurred
348             */
349            public Contact fetchByPrimaryKey(Serializable primaryKey)
350                    throws SystemException {
351                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
352            }
353    
354            /**
355             * Finds the contact with the primary key or returns <code>null</code> if it could not be found.
356             *
357             * @param contactId the primary key of the contact to find
358             * @return the contact, or <code>null</code> if a contact with the primary key could not be found
359             * @throws SystemException if a system exception occurred
360             */
361            public Contact fetchByPrimaryKey(long contactId) throws SystemException {
362                    Contact contact = (Contact)EntityCacheUtil.getResult(ContactModelImpl.ENTITY_CACHE_ENABLED,
363                                    ContactImpl.class, contactId, this);
364    
365                    if (contact == null) {
366                            Session session = null;
367    
368                            try {
369                                    session = openSession();
370    
371                                    contact = (Contact)session.get(ContactImpl.class,
372                                                    new Long(contactId));
373                            }
374                            catch (Exception e) {
375                                    throw processException(e);
376                            }
377                            finally {
378                                    if (contact != null) {
379                                            cacheResult(contact);
380                                    }
381    
382                                    closeSession(session);
383                            }
384                    }
385    
386                    return contact;
387            }
388    
389            /**
390             * Finds all the contacts where companyId = &#63;.
391             *
392             * @param companyId the company id to search with
393             * @return the matching contacts
394             * @throws SystemException if a system exception occurred
395             */
396            public List<Contact> findByCompanyId(long companyId)
397                    throws SystemException {
398                    return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
399                            null);
400            }
401    
402            /**
403             * Finds a range of all the contacts where companyId = &#63;.
404             *
405             * <p>
406             * 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.
407             * </p>
408             *
409             * @param companyId the company id to search with
410             * @param start the lower bound of the range of contacts to return
411             * @param end the upper bound of the range of contacts to return (not inclusive)
412             * @return the range of matching contacts
413             * @throws SystemException if a system exception occurred
414             */
415            public List<Contact> findByCompanyId(long companyId, int start, int end)
416                    throws SystemException {
417                    return findByCompanyId(companyId, start, end, null);
418            }
419    
420            /**
421             * Finds an ordered range of all the contacts where companyId = &#63;.
422             *
423             * <p>
424             * 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.
425             * </p>
426             *
427             * @param companyId the company id to search with
428             * @param start the lower bound of the range of contacts to return
429             * @param end the upper bound of the range of contacts to return (not inclusive)
430             * @param orderByComparator the comparator to order the results by
431             * @return the ordered range of matching contacts
432             * @throws SystemException if a system exception occurred
433             */
434            public List<Contact> findByCompanyId(long companyId, int start, int end,
435                    OrderByComparator orderByComparator) throws SystemException {
436                    Object[] finderArgs = new Object[] {
437                                    companyId,
438                                    
439                                    String.valueOf(start), String.valueOf(end),
440                                    String.valueOf(orderByComparator)
441                            };
442    
443                    List<Contact> list = (List<Contact>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_COMPANYID,
444                                    finderArgs, this);
445    
446                    if (list == null) {
447                            StringBundler query = null;
448    
449                            if (orderByComparator != null) {
450                                    query = new StringBundler(3 +
451                                                    (orderByComparator.getOrderByFields().length * 3));
452                            }
453                            else {
454                                    query = new StringBundler(2);
455                            }
456    
457                            query.append(_SQL_SELECT_CONTACT_WHERE);
458    
459                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
460    
461                            if (orderByComparator != null) {
462                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
463                                            orderByComparator);
464                            }
465    
466                            String sql = query.toString();
467    
468                            Session session = null;
469    
470                            try {
471                                    session = openSession();
472    
473                                    Query q = session.createQuery(sql);
474    
475                                    QueryPos qPos = QueryPos.getInstance(q);
476    
477                                    qPos.add(companyId);
478    
479                                    list = (List<Contact>)QueryUtil.list(q, getDialect(), start, end);
480                            }
481                            catch (Exception e) {
482                                    throw processException(e);
483                            }
484                            finally {
485                                    if (list == null) {
486                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_COMPANYID,
487                                                    finderArgs);
488                                    }
489                                    else {
490                                            cacheResult(list);
491    
492                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COMPANYID,
493                                                    finderArgs, list);
494                                    }
495    
496                                    closeSession(session);
497                            }
498                    }
499    
500                    return list;
501            }
502    
503            /**
504             * Finds the first contact in the ordered set where companyId = &#63;.
505             *
506             * <p>
507             * 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.
508             * </p>
509             *
510             * @param companyId the company id to search with
511             * @param orderByComparator the comparator to order the set by
512             * @return the first matching contact
513             * @throws com.liferay.portal.NoSuchContactException if a matching contact could not be found
514             * @throws SystemException if a system exception occurred
515             */
516            public Contact findByCompanyId_First(long companyId,
517                    OrderByComparator orderByComparator)
518                    throws NoSuchContactException, SystemException {
519                    List<Contact> list = findByCompanyId(companyId, 0, 1, orderByComparator);
520    
521                    if (list.isEmpty()) {
522                            StringBundler msg = new StringBundler(4);
523    
524                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
525    
526                            msg.append("companyId=");
527                            msg.append(companyId);
528    
529                            msg.append(StringPool.CLOSE_CURLY_BRACE);
530    
531                            throw new NoSuchContactException(msg.toString());
532                    }
533                    else {
534                            return list.get(0);
535                    }
536            }
537    
538            /**
539             * Finds the last contact in the ordered set where companyId = &#63;.
540             *
541             * <p>
542             * 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.
543             * </p>
544             *
545             * @param companyId the company id to search with
546             * @param orderByComparator the comparator to order the set by
547             * @return the last matching contact
548             * @throws com.liferay.portal.NoSuchContactException if a matching contact could not be found
549             * @throws SystemException if a system exception occurred
550             */
551            public Contact findByCompanyId_Last(long companyId,
552                    OrderByComparator orderByComparator)
553                    throws NoSuchContactException, SystemException {
554                    int count = countByCompanyId(companyId);
555    
556                    List<Contact> list = findByCompanyId(companyId, count - 1, count,
557                                    orderByComparator);
558    
559                    if (list.isEmpty()) {
560                            StringBundler msg = new StringBundler(4);
561    
562                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
563    
564                            msg.append("companyId=");
565                            msg.append(companyId);
566    
567                            msg.append(StringPool.CLOSE_CURLY_BRACE);
568    
569                            throw new NoSuchContactException(msg.toString());
570                    }
571                    else {
572                            return list.get(0);
573                    }
574            }
575    
576            /**
577             * Finds the contacts before and after the current contact in the ordered set where companyId = &#63;.
578             *
579             * <p>
580             * 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.
581             * </p>
582             *
583             * @param contactId the primary key of the current contact
584             * @param companyId the company id to search with
585             * @param orderByComparator the comparator to order the set by
586             * @return the previous, current, and next contact
587             * @throws com.liferay.portal.NoSuchContactException if a contact with the primary key could not be found
588             * @throws SystemException if a system exception occurred
589             */
590            public Contact[] findByCompanyId_PrevAndNext(long contactId,
591                    long companyId, OrderByComparator orderByComparator)
592                    throws NoSuchContactException, SystemException {
593                    Contact contact = findByPrimaryKey(contactId);
594    
595                    Session session = null;
596    
597                    try {
598                            session = openSession();
599    
600                            Contact[] array = new ContactImpl[3];
601    
602                            array[0] = getByCompanyId_PrevAndNext(session, contact, companyId,
603                                            orderByComparator, true);
604    
605                            array[1] = contact;
606    
607                            array[2] = getByCompanyId_PrevAndNext(session, contact, companyId,
608                                            orderByComparator, false);
609    
610                            return array;
611                    }
612                    catch (Exception e) {
613                            throw processException(e);
614                    }
615                    finally {
616                            closeSession(session);
617                    }
618            }
619    
620            protected Contact getByCompanyId_PrevAndNext(Session session,
621                    Contact contact, long companyId, OrderByComparator orderByComparator,
622                    boolean previous) {
623                    StringBundler query = null;
624    
625                    if (orderByComparator != null) {
626                            query = new StringBundler(6 +
627                                            (orderByComparator.getOrderByFields().length * 6));
628                    }
629                    else {
630                            query = new StringBundler(3);
631                    }
632    
633                    query.append(_SQL_SELECT_CONTACT_WHERE);
634    
635                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
636    
637                    if (orderByComparator != null) {
638                            String[] orderByFields = orderByComparator.getOrderByFields();
639    
640                            if (orderByFields.length > 0) {
641                                    query.append(WHERE_AND);
642                            }
643    
644                            for (int i = 0; i < orderByFields.length; i++) {
645                                    query.append(_ORDER_BY_ENTITY_ALIAS);
646                                    query.append(orderByFields[i]);
647    
648                                    if ((i + 1) < orderByFields.length) {
649                                            if (orderByComparator.isAscending() ^ previous) {
650                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
651                                            }
652                                            else {
653                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
654                                            }
655                                    }
656                                    else {
657                                            if (orderByComparator.isAscending() ^ previous) {
658                                                    query.append(WHERE_GREATER_THAN);
659                                            }
660                                            else {
661                                                    query.append(WHERE_LESSER_THAN);
662                                            }
663                                    }
664                            }
665    
666                            query.append(ORDER_BY_CLAUSE);
667    
668                            for (int i = 0; i < orderByFields.length; i++) {
669                                    query.append(_ORDER_BY_ENTITY_ALIAS);
670                                    query.append(orderByFields[i]);
671    
672                                    if ((i + 1) < orderByFields.length) {
673                                            if (orderByComparator.isAscending() ^ previous) {
674                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
675                                            }
676                                            else {
677                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
678                                            }
679                                    }
680                                    else {
681                                            if (orderByComparator.isAscending() ^ previous) {
682                                                    query.append(ORDER_BY_ASC);
683                                            }
684                                            else {
685                                                    query.append(ORDER_BY_DESC);
686                                            }
687                                    }
688                            }
689                    }
690    
691                    String sql = query.toString();
692    
693                    Query q = session.createQuery(sql);
694    
695                    q.setFirstResult(0);
696                    q.setMaxResults(2);
697    
698                    QueryPos qPos = QueryPos.getInstance(q);
699    
700                    qPos.add(companyId);
701    
702                    if (orderByComparator != null) {
703                            Object[] values = orderByComparator.getOrderByValues(contact);
704    
705                            for (Object value : values) {
706                                    qPos.add(value);
707                            }
708                    }
709    
710                    List<Contact> list = q.list();
711    
712                    if (list.size() == 2) {
713                            return list.get(1);
714                    }
715                    else {
716                            return null;
717                    }
718            }
719    
720            /**
721             * Finds all the contacts.
722             *
723             * @return the contacts
724             * @throws SystemException if a system exception occurred
725             */
726            public List<Contact> findAll() throws SystemException {
727                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
728            }
729    
730            /**
731             * Finds a range of all the contacts.
732             *
733             * <p>
734             * 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.
735             * </p>
736             *
737             * @param start the lower bound of the range of contacts to return
738             * @param end the upper bound of the range of contacts to return (not inclusive)
739             * @return the range of contacts
740             * @throws SystemException if a system exception occurred
741             */
742            public List<Contact> findAll(int start, int end) throws SystemException {
743                    return findAll(start, end, null);
744            }
745    
746            /**
747             * Finds an ordered range of all the contacts.
748             *
749             * <p>
750             * 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.
751             * </p>
752             *
753             * @param start the lower bound of the range of contacts to return
754             * @param end the upper bound of the range of contacts to return (not inclusive)
755             * @param orderByComparator the comparator to order the results by
756             * @return the ordered range of contacts
757             * @throws SystemException if a system exception occurred
758             */
759            public List<Contact> findAll(int start, int end,
760                    OrderByComparator orderByComparator) throws SystemException {
761                    Object[] finderArgs = new Object[] {
762                                    String.valueOf(start), String.valueOf(end),
763                                    String.valueOf(orderByComparator)
764                            };
765    
766                    List<Contact> list = (List<Contact>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
767                                    finderArgs, this);
768    
769                    if (list == null) {
770                            StringBundler query = null;
771                            String sql = null;
772    
773                            if (orderByComparator != null) {
774                                    query = new StringBundler(2 +
775                                                    (orderByComparator.getOrderByFields().length * 3));
776    
777                                    query.append(_SQL_SELECT_CONTACT);
778    
779                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
780                                            orderByComparator);
781    
782                                    sql = query.toString();
783                            }
784                            else {
785                                    sql = _SQL_SELECT_CONTACT;
786                            }
787    
788                            Session session = null;
789    
790                            try {
791                                    session = openSession();
792    
793                                    Query q = session.createQuery(sql);
794    
795                                    if (orderByComparator == null) {
796                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
797                                                            start, end, false);
798    
799                                            Collections.sort(list);
800                                    }
801                                    else {
802                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
803                                                            start, end);
804                                    }
805                            }
806                            catch (Exception e) {
807                                    throw processException(e);
808                            }
809                            finally {
810                                    if (list == null) {
811                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
812                                                    finderArgs);
813                                    }
814                                    else {
815                                            cacheResult(list);
816    
817                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
818                                                    list);
819                                    }
820    
821                                    closeSession(session);
822                            }
823                    }
824    
825                    return list;
826            }
827    
828            /**
829             * Removes all the contacts where companyId = &#63; from the database.
830             *
831             * @param companyId the company id to search with
832             * @throws SystemException if a system exception occurred
833             */
834            public void removeByCompanyId(long companyId) throws SystemException {
835                    for (Contact contact : findByCompanyId(companyId)) {
836                            remove(contact);
837                    }
838            }
839    
840            /**
841             * Removes all the contacts from the database.
842             *
843             * @throws SystemException if a system exception occurred
844             */
845            public void removeAll() throws SystemException {
846                    for (Contact contact : findAll()) {
847                            remove(contact);
848                    }
849            }
850    
851            /**
852             * Counts all the contacts where companyId = &#63;.
853             *
854             * @param companyId the company id to search with
855             * @return the number of matching contacts
856             * @throws SystemException if a system exception occurred
857             */
858            public int countByCompanyId(long companyId) throws SystemException {
859                    Object[] finderArgs = new Object[] { companyId };
860    
861                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
862                                    finderArgs, this);
863    
864                    if (count == null) {
865                            StringBundler query = new StringBundler(2);
866    
867                            query.append(_SQL_COUNT_CONTACT_WHERE);
868    
869                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
870    
871                            String sql = query.toString();
872    
873                            Session session = null;
874    
875                            try {
876                                    session = openSession();
877    
878                                    Query q = session.createQuery(sql);
879    
880                                    QueryPos qPos = QueryPos.getInstance(q);
881    
882                                    qPos.add(companyId);
883    
884                                    count = (Long)q.uniqueResult();
885                            }
886                            catch (Exception e) {
887                                    throw processException(e);
888                            }
889                            finally {
890                                    if (count == null) {
891                                            count = Long.valueOf(0);
892                                    }
893    
894                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
895                                            finderArgs, count);
896    
897                                    closeSession(session);
898                            }
899                    }
900    
901                    return count.intValue();
902            }
903    
904            /**
905             * Counts all the contacts.
906             *
907             * @return the number of contacts
908             * @throws SystemException if a system exception occurred
909             */
910            public int countAll() throws SystemException {
911                    Object[] finderArgs = new Object[0];
912    
913                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
914                                    finderArgs, this);
915    
916                    if (count == null) {
917                            Session session = null;
918    
919                            try {
920                                    session = openSession();
921    
922                                    Query q = session.createQuery(_SQL_COUNT_CONTACT);
923    
924                                    count = (Long)q.uniqueResult();
925                            }
926                            catch (Exception e) {
927                                    throw processException(e);
928                            }
929                            finally {
930                                    if (count == null) {
931                                            count = Long.valueOf(0);
932                                    }
933    
934                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
935                                            count);
936    
937                                    closeSession(session);
938                            }
939                    }
940    
941                    return count.intValue();
942            }
943    
944            /**
945             * Initializes the contact persistence.
946             */
947            public void afterPropertiesSet() {
948                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
949                                            com.liferay.portal.util.PropsUtil.get(
950                                                    "value.object.listener.com.liferay.portal.model.Contact")));
951    
952                    if (listenerClassNames.length > 0) {
953                            try {
954                                    List<ModelListener<Contact>> listenersList = new ArrayList<ModelListener<Contact>>();
955    
956                                    for (String listenerClassName : listenerClassNames) {
957                                            listenersList.add((ModelListener<Contact>)InstanceFactory.newInstance(
958                                                            listenerClassName));
959                                    }
960    
961                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
962                            }
963                            catch (Exception e) {
964                                    _log.error(e);
965                            }
966                    }
967            }
968    
969            public void destroy() {
970                    EntityCacheUtil.removeCache(ContactImpl.class.getName());
971                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
972                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
973            }
974    
975            @BeanReference(type = AccountPersistence.class)
976            protected AccountPersistence accountPersistence;
977            @BeanReference(type = AddressPersistence.class)
978            protected AddressPersistence addressPersistence;
979            @BeanReference(type = BrowserTrackerPersistence.class)
980            protected BrowserTrackerPersistence browserTrackerPersistence;
981            @BeanReference(type = ClassNamePersistence.class)
982            protected ClassNamePersistence classNamePersistence;
983            @BeanReference(type = ClusterGroupPersistence.class)
984            protected ClusterGroupPersistence clusterGroupPersistence;
985            @BeanReference(type = CompanyPersistence.class)
986            protected CompanyPersistence companyPersistence;
987            @BeanReference(type = ContactPersistence.class)
988            protected ContactPersistence contactPersistence;
989            @BeanReference(type = CountryPersistence.class)
990            protected CountryPersistence countryPersistence;
991            @BeanReference(type = EmailAddressPersistence.class)
992            protected EmailAddressPersistence emailAddressPersistence;
993            @BeanReference(type = GroupPersistence.class)
994            protected GroupPersistence groupPersistence;
995            @BeanReference(type = ImagePersistence.class)
996            protected ImagePersistence imagePersistence;
997            @BeanReference(type = LayoutPersistence.class)
998            protected LayoutPersistence layoutPersistence;
999            @BeanReference(type = LayoutPrototypePersistence.class)
1000            protected LayoutPrototypePersistence layoutPrototypePersistence;
1001            @BeanReference(type = LayoutSetPersistence.class)
1002            protected LayoutSetPersistence layoutSetPersistence;
1003            @BeanReference(type = LayoutSetPrototypePersistence.class)
1004            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1005            @BeanReference(type = ListTypePersistence.class)
1006            protected ListTypePersistence listTypePersistence;
1007            @BeanReference(type = LockPersistence.class)
1008            protected LockPersistence lockPersistence;
1009            @BeanReference(type = MembershipRequestPersistence.class)
1010            protected MembershipRequestPersistence membershipRequestPersistence;
1011            @BeanReference(type = OrganizationPersistence.class)
1012            protected OrganizationPersistence organizationPersistence;
1013            @BeanReference(type = OrgGroupPermissionPersistence.class)
1014            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1015            @BeanReference(type = OrgGroupRolePersistence.class)
1016            protected OrgGroupRolePersistence orgGroupRolePersistence;
1017            @BeanReference(type = OrgLaborPersistence.class)
1018            protected OrgLaborPersistence orgLaborPersistence;
1019            @BeanReference(type = PasswordPolicyPersistence.class)
1020            protected PasswordPolicyPersistence passwordPolicyPersistence;
1021            @BeanReference(type = PasswordPolicyRelPersistence.class)
1022            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1023            @BeanReference(type = PasswordTrackerPersistence.class)
1024            protected PasswordTrackerPersistence passwordTrackerPersistence;
1025            @BeanReference(type = PermissionPersistence.class)
1026            protected PermissionPersistence permissionPersistence;
1027            @BeanReference(type = PhonePersistence.class)
1028            protected PhonePersistence phonePersistence;
1029            @BeanReference(type = PluginSettingPersistence.class)
1030            protected PluginSettingPersistence pluginSettingPersistence;
1031            @BeanReference(type = PortletPersistence.class)
1032            protected PortletPersistence portletPersistence;
1033            @BeanReference(type = PortletItemPersistence.class)
1034            protected PortletItemPersistence portletItemPersistence;
1035            @BeanReference(type = PortletPreferencesPersistence.class)
1036            protected PortletPreferencesPersistence portletPreferencesPersistence;
1037            @BeanReference(type = RegionPersistence.class)
1038            protected RegionPersistence regionPersistence;
1039            @BeanReference(type = ReleasePersistence.class)
1040            protected ReleasePersistence releasePersistence;
1041            @BeanReference(type = ResourcePersistence.class)
1042            protected ResourcePersistence resourcePersistence;
1043            @BeanReference(type = ResourceActionPersistence.class)
1044            protected ResourceActionPersistence resourceActionPersistence;
1045            @BeanReference(type = ResourceCodePersistence.class)
1046            protected ResourceCodePersistence resourceCodePersistence;
1047            @BeanReference(type = ResourcePermissionPersistence.class)
1048            protected ResourcePermissionPersistence resourcePermissionPersistence;
1049            @BeanReference(type = RolePersistence.class)
1050            protected RolePersistence rolePersistence;
1051            @BeanReference(type = ServiceComponentPersistence.class)
1052            protected ServiceComponentPersistence serviceComponentPersistence;
1053            @BeanReference(type = ShardPersistence.class)
1054            protected ShardPersistence shardPersistence;
1055            @BeanReference(type = SubscriptionPersistence.class)
1056            protected SubscriptionPersistence subscriptionPersistence;
1057            @BeanReference(type = TicketPersistence.class)
1058            protected TicketPersistence ticketPersistence;
1059            @BeanReference(type = TeamPersistence.class)
1060            protected TeamPersistence teamPersistence;
1061            @BeanReference(type = UserPersistence.class)
1062            protected UserPersistence userPersistence;
1063            @BeanReference(type = UserGroupPersistence.class)
1064            protected UserGroupPersistence userGroupPersistence;
1065            @BeanReference(type = UserGroupGroupRolePersistence.class)
1066            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1067            @BeanReference(type = UserGroupRolePersistence.class)
1068            protected UserGroupRolePersistence userGroupRolePersistence;
1069            @BeanReference(type = UserIdMapperPersistence.class)
1070            protected UserIdMapperPersistence userIdMapperPersistence;
1071            @BeanReference(type = UserTrackerPersistence.class)
1072            protected UserTrackerPersistence userTrackerPersistence;
1073            @BeanReference(type = UserTrackerPathPersistence.class)
1074            protected UserTrackerPathPersistence userTrackerPathPersistence;
1075            @BeanReference(type = WebDAVPropsPersistence.class)
1076            protected WebDAVPropsPersistence webDAVPropsPersistence;
1077            @BeanReference(type = WebsitePersistence.class)
1078            protected WebsitePersistence websitePersistence;
1079            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1080            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1081            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1082            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1083            private static final String _SQL_SELECT_CONTACT = "SELECT contact FROM Contact contact";
1084            private static final String _SQL_SELECT_CONTACT_WHERE = "SELECT contact FROM Contact contact WHERE ";
1085            private static final String _SQL_COUNT_CONTACT = "SELECT COUNT(contact) FROM Contact contact";
1086            private static final String _SQL_COUNT_CONTACT_WHERE = "SELECT COUNT(contact) FROM Contact contact WHERE ";
1087            private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "contact.companyId = ?";
1088            private static final String _ORDER_BY_ENTITY_ALIAS = "contact.";
1089            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Contact exists with the primary key ";
1090            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Contact exists with the key {";
1091            private static Log _log = LogFactoryUtil.getLog(ContactPersistenceImpl.class);
1092    }