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