001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                            Session session = null;
448    
449                            try {
450                                    session = openSession();
451    
452                                    StringBundler query = null;
453    
454                                    if (orderByComparator != null) {
455                                            query = new StringBundler(3 +
456                                                            (orderByComparator.getOrderByFields().length * 3));
457                                    }
458                                    else {
459                                            query = new StringBundler(2);
460                                    }
461    
462                                    query.append(_SQL_SELECT_CONTACT_WHERE);
463    
464                                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
465    
466                                    if (orderByComparator != null) {
467                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
468                                                    orderByComparator);
469                                    }
470    
471                                    String sql = query.toString();
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                                            list = new ArrayList<Contact>();
487                                    }
488    
489                                    cacheResult(list);
490    
491                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COMPANYID,
492                                            finderArgs, list);
493    
494                                    closeSession(session);
495                            }
496                    }
497    
498                    return list;
499            }
500    
501            /**
502             * Finds the first contact in the ordered set where companyId = &#63;.
503             *
504             * <p>
505             * 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.
506             * </p>
507             *
508             * @param companyId the company id to search with
509             * @param orderByComparator the comparator to order the set by
510             * @return the first matching contact
511             * @throws com.liferay.portal.NoSuchContactException if a matching contact could not be found
512             * @throws SystemException if a system exception occurred
513             */
514            public Contact findByCompanyId_First(long companyId,
515                    OrderByComparator orderByComparator)
516                    throws NoSuchContactException, SystemException {
517                    List<Contact> list = findByCompanyId(companyId, 0, 1, orderByComparator);
518    
519                    if (list.isEmpty()) {
520                            StringBundler msg = new StringBundler(4);
521    
522                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
523    
524                            msg.append("companyId=");
525                            msg.append(companyId);
526    
527                            msg.append(StringPool.CLOSE_CURLY_BRACE);
528    
529                            throw new NoSuchContactException(msg.toString());
530                    }
531                    else {
532                            return list.get(0);
533                    }
534            }
535    
536            /**
537             * Finds the last contact in the ordered set where companyId = &#63;.
538             *
539             * <p>
540             * 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.
541             * </p>
542             *
543             * @param companyId the company id to search with
544             * @param orderByComparator the comparator to order the set by
545             * @return the last matching contact
546             * @throws com.liferay.portal.NoSuchContactException if a matching contact could not be found
547             * @throws SystemException if a system exception occurred
548             */
549            public Contact findByCompanyId_Last(long companyId,
550                    OrderByComparator orderByComparator)
551                    throws NoSuchContactException, SystemException {
552                    int count = countByCompanyId(companyId);
553    
554                    List<Contact> list = findByCompanyId(companyId, count - 1, count,
555                                    orderByComparator);
556    
557                    if (list.isEmpty()) {
558                            StringBundler msg = new StringBundler(4);
559    
560                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
561    
562                            msg.append("companyId=");
563                            msg.append(companyId);
564    
565                            msg.append(StringPool.CLOSE_CURLY_BRACE);
566    
567                            throw new NoSuchContactException(msg.toString());
568                    }
569                    else {
570                            return list.get(0);
571                    }
572            }
573    
574            /**
575             * Finds the contacts before and after the current contact in the ordered set where companyId = &#63;.
576             *
577             * <p>
578             * 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.
579             * </p>
580             *
581             * @param contactId the primary key of the current contact
582             * @param companyId the company id to search with
583             * @param orderByComparator the comparator to order the set by
584             * @return the previous, current, and next contact
585             * @throws com.liferay.portal.NoSuchContactException if a contact with the primary key could not be found
586             * @throws SystemException if a system exception occurred
587             */
588            public Contact[] findByCompanyId_PrevAndNext(long contactId,
589                    long companyId, OrderByComparator orderByComparator)
590                    throws NoSuchContactException, SystemException {
591                    Contact contact = findByPrimaryKey(contactId);
592    
593                    Session session = null;
594    
595                    try {
596                            session = openSession();
597    
598                            Contact[] array = new ContactImpl[3];
599    
600                            array[0] = getByCompanyId_PrevAndNext(session, contact, companyId,
601                                            orderByComparator, true);
602    
603                            array[1] = contact;
604    
605                            array[2] = getByCompanyId_PrevAndNext(session, contact, companyId,
606                                            orderByComparator, false);
607    
608                            return array;
609                    }
610                    catch (Exception e) {
611                            throw processException(e);
612                    }
613                    finally {
614                            closeSession(session);
615                    }
616            }
617    
618            protected Contact getByCompanyId_PrevAndNext(Session session,
619                    Contact contact, long companyId, OrderByComparator orderByComparator,
620                    boolean previous) {
621                    StringBundler query = null;
622    
623                    if (orderByComparator != null) {
624                            query = new StringBundler(6 +
625                                            (orderByComparator.getOrderByFields().length * 6));
626                    }
627                    else {
628                            query = new StringBundler(3);
629                    }
630    
631                    query.append(_SQL_SELECT_CONTACT_WHERE);
632    
633                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
634    
635                    if (orderByComparator != null) {
636                            String[] orderByFields = orderByComparator.getOrderByFields();
637    
638                            if (orderByFields.length > 0) {
639                                    query.append(WHERE_AND);
640                            }
641    
642                            for (int i = 0; i < orderByFields.length; i++) {
643                                    query.append(_ORDER_BY_ENTITY_ALIAS);
644                                    query.append(orderByFields[i]);
645    
646                                    if ((i + 1) < orderByFields.length) {
647                                            if (orderByComparator.isAscending() ^ previous) {
648                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
649                                            }
650                                            else {
651                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
652                                            }
653                                    }
654                                    else {
655                                            if (orderByComparator.isAscending() ^ previous) {
656                                                    query.append(WHERE_GREATER_THAN);
657                                            }
658                                            else {
659                                                    query.append(WHERE_LESSER_THAN);
660                                            }
661                                    }
662                            }
663    
664                            query.append(ORDER_BY_CLAUSE);
665    
666                            for (int i = 0; i < orderByFields.length; i++) {
667                                    query.append(_ORDER_BY_ENTITY_ALIAS);
668                                    query.append(orderByFields[i]);
669    
670                                    if ((i + 1) < orderByFields.length) {
671                                            if (orderByComparator.isAscending() ^ previous) {
672                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
673                                            }
674                                            else {
675                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
676                                            }
677                                    }
678                                    else {
679                                            if (orderByComparator.isAscending() ^ previous) {
680                                                    query.append(ORDER_BY_ASC);
681                                            }
682                                            else {
683                                                    query.append(ORDER_BY_DESC);
684                                            }
685                                    }
686                            }
687                    }
688    
689                    String sql = query.toString();
690    
691                    Query q = session.createQuery(sql);
692    
693                    q.setFirstResult(0);
694                    q.setMaxResults(2);
695    
696                    QueryPos qPos = QueryPos.getInstance(q);
697    
698                    qPos.add(companyId);
699    
700                    if (orderByComparator != null) {
701                            Object[] values = orderByComparator.getOrderByValues(contact);
702    
703                            for (Object value : values) {
704                                    qPos.add(value);
705                            }
706                    }
707    
708                    List<Contact> list = q.list();
709    
710                    if (list.size() == 2) {
711                            return list.get(1);
712                    }
713                    else {
714                            return null;
715                    }
716            }
717    
718            /**
719             * Finds all the contacts.
720             *
721             * @return the contacts
722             * @throws SystemException if a system exception occurred
723             */
724            public List<Contact> findAll() throws SystemException {
725                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
726            }
727    
728            /**
729             * Finds a range of all the contacts.
730             *
731             * <p>
732             * 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.
733             * </p>
734             *
735             * @param start the lower bound of the range of contacts to return
736             * @param end the upper bound of the range of contacts to return (not inclusive)
737             * @return the range of contacts
738             * @throws SystemException if a system exception occurred
739             */
740            public List<Contact> findAll(int start, int end) throws SystemException {
741                    return findAll(start, end, null);
742            }
743    
744            /**
745             * Finds an ordered range of all the contacts.
746             *
747             * <p>
748             * 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.
749             * </p>
750             *
751             * @param start the lower bound of the range of contacts to return
752             * @param end the upper bound of the range of contacts to return (not inclusive)
753             * @param orderByComparator the comparator to order the results by
754             * @return the ordered range of contacts
755             * @throws SystemException if a system exception occurred
756             */
757            public List<Contact> findAll(int start, int end,
758                    OrderByComparator orderByComparator) throws SystemException {
759                    Object[] finderArgs = new Object[] {
760                                    String.valueOf(start), String.valueOf(end),
761                                    String.valueOf(orderByComparator)
762                            };
763    
764                    List<Contact> list = (List<Contact>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
765                                    finderArgs, this);
766    
767                    if (list == null) {
768                            Session session = null;
769    
770                            try {
771                                    session = openSession();
772    
773                                    StringBundler query = null;
774                                    String sql = null;
775    
776                                    if (orderByComparator != null) {
777                                            query = new StringBundler(2 +
778                                                            (orderByComparator.getOrderByFields().length * 3));
779    
780                                            query.append(_SQL_SELECT_CONTACT);
781    
782                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
783                                                    orderByComparator);
784    
785                                            sql = query.toString();
786                                    }
787                                    else {
788                                            sql = _SQL_SELECT_CONTACT;
789                                    }
790    
791                                    Query q = session.createQuery(sql);
792    
793                                    if (orderByComparator == null) {
794                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
795                                                            start, end, false);
796    
797                                            Collections.sort(list);
798                                    }
799                                    else {
800                                            list = (List<Contact>)QueryUtil.list(q, getDialect(),
801                                                            start, end);
802                                    }
803                            }
804                            catch (Exception e) {
805                                    throw processException(e);
806                            }
807                            finally {
808                                    if (list == null) {
809                                            list = new ArrayList<Contact>();
810                                    }
811    
812                                    cacheResult(list);
813    
814                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
815    
816                                    closeSession(session);
817                            }
818                    }
819    
820                    return list;
821            }
822    
823            /**
824             * Removes all the contacts where companyId = &#63; from the database.
825             *
826             * @param companyId the company id to search with
827             * @throws SystemException if a system exception occurred
828             */
829            public void removeByCompanyId(long companyId) throws SystemException {
830                    for (Contact contact : findByCompanyId(companyId)) {
831                            remove(contact);
832                    }
833            }
834    
835            /**
836             * Removes all the contacts from the database.
837             *
838             * @throws SystemException if a system exception occurred
839             */
840            public void removeAll() throws SystemException {
841                    for (Contact contact : findAll()) {
842                            remove(contact);
843                    }
844            }
845    
846            /**
847             * Counts all the contacts where companyId = &#63;.
848             *
849             * @param companyId the company id to search with
850             * @return the number of matching contacts
851             * @throws SystemException if a system exception occurred
852             */
853            public int countByCompanyId(long companyId) throws SystemException {
854                    Object[] finderArgs = new Object[] { companyId };
855    
856                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
857                                    finderArgs, this);
858    
859                    if (count == null) {
860                            Session session = null;
861    
862                            try {
863                                    session = openSession();
864    
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                                    Query q = session.createQuery(sql);
874    
875                                    QueryPos qPos = QueryPos.getInstance(q);
876    
877                                    qPos.add(companyId);
878    
879                                    count = (Long)q.uniqueResult();
880                            }
881                            catch (Exception e) {
882                                    throw processException(e);
883                            }
884                            finally {
885                                    if (count == null) {
886                                            count = Long.valueOf(0);
887                                    }
888    
889                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
890                                            finderArgs, count);
891    
892                                    closeSession(session);
893                            }
894                    }
895    
896                    return count.intValue();
897            }
898    
899            /**
900             * Counts all the contacts.
901             *
902             * @return the number of contacts
903             * @throws SystemException if a system exception occurred
904             */
905            public int countAll() throws SystemException {
906                    Object[] finderArgs = new Object[0];
907    
908                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
909                                    finderArgs, this);
910    
911                    if (count == null) {
912                            Session session = null;
913    
914                            try {
915                                    session = openSession();
916    
917                                    Query q = session.createQuery(_SQL_COUNT_CONTACT);
918    
919                                    count = (Long)q.uniqueResult();
920                            }
921                            catch (Exception e) {
922                                    throw processException(e);
923                            }
924                            finally {
925                                    if (count == null) {
926                                            count = Long.valueOf(0);
927                                    }
928    
929                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
930                                            count);
931    
932                                    closeSession(session);
933                            }
934                    }
935    
936                    return count.intValue();
937            }
938    
939            /**
940             * Initializes the contact persistence.
941             */
942            public void afterPropertiesSet() {
943                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
944                                            com.liferay.portal.util.PropsUtil.get(
945                                                    "value.object.listener.com.liferay.portal.model.Contact")));
946    
947                    if (listenerClassNames.length > 0) {
948                            try {
949                                    List<ModelListener<Contact>> listenersList = new ArrayList<ModelListener<Contact>>();
950    
951                                    for (String listenerClassName : listenerClassNames) {
952                                            listenersList.add((ModelListener<Contact>)InstanceFactory.newInstance(
953                                                            listenerClassName));
954                                    }
955    
956                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
957                            }
958                            catch (Exception e) {
959                                    _log.error(e);
960                            }
961                    }
962            }
963    
964            public void destroy() {
965                    EntityCacheUtil.removeCache(ContactImpl.class.getName());
966                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
967                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
968            }
969    
970            @BeanReference(type = AccountPersistence.class)
971            protected AccountPersistence accountPersistence;
972            @BeanReference(type = AddressPersistence.class)
973            protected AddressPersistence addressPersistence;
974            @BeanReference(type = BrowserTrackerPersistence.class)
975            protected BrowserTrackerPersistence browserTrackerPersistence;
976            @BeanReference(type = ClassNamePersistence.class)
977            protected ClassNamePersistence classNamePersistence;
978            @BeanReference(type = ClusterGroupPersistence.class)
979            protected ClusterGroupPersistence clusterGroupPersistence;
980            @BeanReference(type = CompanyPersistence.class)
981            protected CompanyPersistence companyPersistence;
982            @BeanReference(type = ContactPersistence.class)
983            protected ContactPersistence contactPersistence;
984            @BeanReference(type = CountryPersistence.class)
985            protected CountryPersistence countryPersistence;
986            @BeanReference(type = EmailAddressPersistence.class)
987            protected EmailAddressPersistence emailAddressPersistence;
988            @BeanReference(type = GroupPersistence.class)
989            protected GroupPersistence groupPersistence;
990            @BeanReference(type = ImagePersistence.class)
991            protected ImagePersistence imagePersistence;
992            @BeanReference(type = LayoutPersistence.class)
993            protected LayoutPersistence layoutPersistence;
994            @BeanReference(type = LayoutPrototypePersistence.class)
995            protected LayoutPrototypePersistence layoutPrototypePersistence;
996            @BeanReference(type = LayoutSetPersistence.class)
997            protected LayoutSetPersistence layoutSetPersistence;
998            @BeanReference(type = LayoutSetPrototypePersistence.class)
999            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1000            @BeanReference(type = ListTypePersistence.class)
1001            protected ListTypePersistence listTypePersistence;
1002            @BeanReference(type = LockPersistence.class)
1003            protected LockPersistence lockPersistence;
1004            @BeanReference(type = MembershipRequestPersistence.class)
1005            protected MembershipRequestPersistence membershipRequestPersistence;
1006            @BeanReference(type = OrganizationPersistence.class)
1007            protected OrganizationPersistence organizationPersistence;
1008            @BeanReference(type = OrgGroupPermissionPersistence.class)
1009            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1010            @BeanReference(type = OrgGroupRolePersistence.class)
1011            protected OrgGroupRolePersistence orgGroupRolePersistence;
1012            @BeanReference(type = OrgLaborPersistence.class)
1013            protected OrgLaborPersistence orgLaborPersistence;
1014            @BeanReference(type = PasswordPolicyPersistence.class)
1015            protected PasswordPolicyPersistence passwordPolicyPersistence;
1016            @BeanReference(type = PasswordPolicyRelPersistence.class)
1017            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1018            @BeanReference(type = PasswordTrackerPersistence.class)
1019            protected PasswordTrackerPersistence passwordTrackerPersistence;
1020            @BeanReference(type = PermissionPersistence.class)
1021            protected PermissionPersistence permissionPersistence;
1022            @BeanReference(type = PhonePersistence.class)
1023            protected PhonePersistence phonePersistence;
1024            @BeanReference(type = PluginSettingPersistence.class)
1025            protected PluginSettingPersistence pluginSettingPersistence;
1026            @BeanReference(type = PortletPersistence.class)
1027            protected PortletPersistence portletPersistence;
1028            @BeanReference(type = PortletItemPersistence.class)
1029            protected PortletItemPersistence portletItemPersistence;
1030            @BeanReference(type = PortletPreferencesPersistence.class)
1031            protected PortletPreferencesPersistence portletPreferencesPersistence;
1032            @BeanReference(type = RegionPersistence.class)
1033            protected RegionPersistence regionPersistence;
1034            @BeanReference(type = ReleasePersistence.class)
1035            protected ReleasePersistence releasePersistence;
1036            @BeanReference(type = ResourcePersistence.class)
1037            protected ResourcePersistence resourcePersistence;
1038            @BeanReference(type = ResourceActionPersistence.class)
1039            protected ResourceActionPersistence resourceActionPersistence;
1040            @BeanReference(type = ResourceCodePersistence.class)
1041            protected ResourceCodePersistence resourceCodePersistence;
1042            @BeanReference(type = ResourcePermissionPersistence.class)
1043            protected ResourcePermissionPersistence resourcePermissionPersistence;
1044            @BeanReference(type = RolePersistence.class)
1045            protected RolePersistence rolePersistence;
1046            @BeanReference(type = ServiceComponentPersistence.class)
1047            protected ServiceComponentPersistence serviceComponentPersistence;
1048            @BeanReference(type = ShardPersistence.class)
1049            protected ShardPersistence shardPersistence;
1050            @BeanReference(type = SubscriptionPersistence.class)
1051            protected SubscriptionPersistence subscriptionPersistence;
1052            @BeanReference(type = TicketPersistence.class)
1053            protected TicketPersistence ticketPersistence;
1054            @BeanReference(type = TeamPersistence.class)
1055            protected TeamPersistence teamPersistence;
1056            @BeanReference(type = UserPersistence.class)
1057            protected UserPersistence userPersistence;
1058            @BeanReference(type = UserGroupPersistence.class)
1059            protected UserGroupPersistence userGroupPersistence;
1060            @BeanReference(type = UserGroupGroupRolePersistence.class)
1061            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1062            @BeanReference(type = UserGroupRolePersistence.class)
1063            protected UserGroupRolePersistence userGroupRolePersistence;
1064            @BeanReference(type = UserIdMapperPersistence.class)
1065            protected UserIdMapperPersistence userIdMapperPersistence;
1066            @BeanReference(type = UserTrackerPersistence.class)
1067            protected UserTrackerPersistence userTrackerPersistence;
1068            @BeanReference(type = UserTrackerPathPersistence.class)
1069            protected UserTrackerPathPersistence userTrackerPathPersistence;
1070            @BeanReference(type = WebDAVPropsPersistence.class)
1071            protected WebDAVPropsPersistence webDAVPropsPersistence;
1072            @BeanReference(type = WebsitePersistence.class)
1073            protected WebsitePersistence websitePersistence;
1074            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1075            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1076            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1077            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1078            private static final String _SQL_SELECT_CONTACT = "SELECT contact FROM Contact contact";
1079            private static final String _SQL_SELECT_CONTACT_WHERE = "SELECT contact FROM Contact contact WHERE ";
1080            private static final String _SQL_COUNT_CONTACT = "SELECT COUNT(contact) FROM Contact contact";
1081            private static final String _SQL_COUNT_CONTACT_WHERE = "SELECT COUNT(contact) FROM Contact contact WHERE ";
1082            private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "contact.companyId = ?";
1083            private static final String _ORDER_BY_ENTITY_ALIAS = "contact.";
1084            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Contact exists with the primary key ";
1085            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Contact exists with the key {";
1086            private static Log _log = LogFactoryUtil.getLog(ContactPersistenceImpl.class);
1087    }