001    /**
002     * Copyright (c) 2000-present 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.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.bean.BeanReference;
020    import com.liferay.portal.kernel.dao.orm.EntityCache;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCache;
023    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
024    import com.liferay.portal.kernel.dao.orm.FinderPath;
025    import com.liferay.portal.kernel.dao.orm.Query;
026    import com.liferay.portal.kernel.dao.orm.QueryPos;
027    import com.liferay.portal.kernel.dao.orm.QueryUtil;
028    import com.liferay.portal.kernel.dao.orm.Session;
029    import com.liferay.portal.kernel.exception.NoSuchUserException;
030    import com.liferay.portal.kernel.log.Log;
031    import com.liferay.portal.kernel.log.LogFactoryUtil;
032    import com.liferay.portal.kernel.model.CacheModel;
033    import com.liferay.portal.kernel.model.MVCCModel;
034    import com.liferay.portal.kernel.model.User;
035    import com.liferay.portal.kernel.service.ServiceContext;
036    import com.liferay.portal.kernel.service.ServiceContextThreadLocal;
037    import com.liferay.portal.kernel.service.persistence.CompanyProvider;
038    import com.liferay.portal.kernel.service.persistence.CompanyProviderWrapper;
039    import com.liferay.portal.kernel.service.persistence.GroupPersistence;
040    import com.liferay.portal.kernel.service.persistence.OrganizationPersistence;
041    import com.liferay.portal.kernel.service.persistence.RolePersistence;
042    import com.liferay.portal.kernel.service.persistence.TeamPersistence;
043    import com.liferay.portal.kernel.service.persistence.UserGroupPersistence;
044    import com.liferay.portal.kernel.service.persistence.UserPersistence;
045    import com.liferay.portal.kernel.service.persistence.impl.BasePersistenceImpl;
046    import com.liferay.portal.kernel.service.persistence.impl.TableMapper;
047    import com.liferay.portal.kernel.service.persistence.impl.TableMapperFactory;
048    import com.liferay.portal.kernel.util.OrderByComparator;
049    import com.liferay.portal.kernel.util.SetUtil;
050    import com.liferay.portal.kernel.util.StringBundler;
051    import com.liferay.portal.kernel.util.StringPool;
052    import com.liferay.portal.kernel.util.StringUtil;
053    import com.liferay.portal.kernel.util.Validator;
054    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
055    import com.liferay.portal.model.impl.UserImpl;
056    import com.liferay.portal.model.impl.UserModelImpl;
057    
058    import java.io.Serializable;
059    
060    import java.sql.Timestamp;
061    
062    import java.util.Collections;
063    import java.util.Date;
064    import java.util.HashMap;
065    import java.util.HashSet;
066    import java.util.Iterator;
067    import java.util.List;
068    import java.util.Map;
069    import java.util.Set;
070    
071    /**
072     * The persistence implementation for the user service.
073     *
074     * <p>
075     * Caching information and settings can be found in <code>portal.properties</code>
076     * </p>
077     *
078     * @author Brian Wing Shun Chan
079     * @see UserPersistence
080     * @see com.liferay.portal.kernel.service.persistence.UserUtil
081     * @generated
082     */
083    @ProviderType
084    public class UserPersistenceImpl extends BasePersistenceImpl<User>
085            implements UserPersistence {
086            /*
087             * NOTE FOR DEVELOPERS:
088             *
089             * Never modify or reference this class directly. Always use {@link UserUtil} to access the user persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
090             */
091            public static final String FINDER_CLASS_NAME_ENTITY = UserImpl.class.getName();
092            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
093                    ".List1";
094            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
095                    ".List2";
096            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
097                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
098                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
099            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
100                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
101                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
102            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
103                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
104                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
105            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
106                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
107                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid",
108                            new String[] {
109                                    String.class.getName(),
110                                    
111                            Integer.class.getName(), Integer.class.getName(),
112                                    OrderByComparator.class.getName()
113                            });
114            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
115                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
116                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid",
117                            new String[] { String.class.getName() },
118                            UserModelImpl.UUID_COLUMN_BITMASK);
119            public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
120                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
121                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid",
122                            new String[] { String.class.getName() });
123    
124            /**
125             * Returns all the users where uuid = &#63;.
126             *
127             * @param uuid the uuid
128             * @return the matching users
129             */
130            @Override
131            public List<User> findByUuid(String uuid) {
132                    return findByUuid(uuid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
133            }
134    
135            /**
136             * Returns a range of all the users where uuid = &#63;.
137             *
138             * <p>
139             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
140             * </p>
141             *
142             * @param uuid the uuid
143             * @param start the lower bound of the range of users
144             * @param end the upper bound of the range of users (not inclusive)
145             * @return the range of matching users
146             */
147            @Override
148            public List<User> findByUuid(String uuid, int start, int end) {
149                    return findByUuid(uuid, start, end, null);
150            }
151    
152            /**
153             * Returns an ordered range of all the users where uuid = &#63;.
154             *
155             * <p>
156             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
157             * </p>
158             *
159             * @param uuid the uuid
160             * @param start the lower bound of the range of users
161             * @param end the upper bound of the range of users (not inclusive)
162             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
163             * @return the ordered range of matching users
164             */
165            @Override
166            public List<User> findByUuid(String uuid, int start, int end,
167                    OrderByComparator<User> orderByComparator) {
168                    return findByUuid(uuid, start, end, orderByComparator, true);
169            }
170    
171            /**
172             * Returns an ordered range of all the users where uuid = &#63;.
173             *
174             * <p>
175             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
176             * </p>
177             *
178             * @param uuid the uuid
179             * @param start the lower bound of the range of users
180             * @param end the upper bound of the range of users (not inclusive)
181             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
182             * @param retrieveFromCache whether to retrieve from the finder cache
183             * @return the ordered range of matching users
184             */
185            @Override
186            public List<User> findByUuid(String uuid, int start, int end,
187                    OrderByComparator<User> orderByComparator, boolean retrieveFromCache) {
188                    boolean pagination = true;
189                    FinderPath finderPath = null;
190                    Object[] finderArgs = null;
191    
192                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
193                                    (orderByComparator == null)) {
194                            pagination = false;
195                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID;
196                            finderArgs = new Object[] { uuid };
197                    }
198                    else {
199                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID;
200                            finderArgs = new Object[] { uuid, start, end, orderByComparator };
201                    }
202    
203                    List<User> list = null;
204    
205                    if (retrieveFromCache) {
206                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
207                                            this);
208    
209                            if ((list != null) && !list.isEmpty()) {
210                                    for (User user : list) {
211                                            if (!Validator.equals(uuid, user.getUuid())) {
212                                                    list = null;
213    
214                                                    break;
215                                            }
216                                    }
217                            }
218                    }
219    
220                    if (list == null) {
221                            StringBundler query = null;
222    
223                            if (orderByComparator != null) {
224                                    query = new StringBundler(3 +
225                                                    (orderByComparator.getOrderByFields().length * 2));
226                            }
227                            else {
228                                    query = new StringBundler(3);
229                            }
230    
231                            query.append(_SQL_SELECT_USER_WHERE);
232    
233                            boolean bindUuid = false;
234    
235                            if (uuid == null) {
236                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
237                            }
238                            else if (uuid.equals(StringPool.BLANK)) {
239                                    query.append(_FINDER_COLUMN_UUID_UUID_3);
240                            }
241                            else {
242                                    bindUuid = true;
243    
244                                    query.append(_FINDER_COLUMN_UUID_UUID_2);
245                            }
246    
247                            if (orderByComparator != null) {
248                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
249                                            orderByComparator);
250                            }
251                            else
252                             if (pagination) {
253                                    query.append(UserModelImpl.ORDER_BY_JPQL);
254                            }
255    
256                            String sql = query.toString();
257    
258                            Session session = null;
259    
260                            try {
261                                    session = openSession();
262    
263                                    Query q = session.createQuery(sql);
264    
265                                    QueryPos qPos = QueryPos.getInstance(q);
266    
267                                    if (bindUuid) {
268                                            qPos.add(uuid);
269                                    }
270    
271                                    if (!pagination) {
272                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
273                                                            end, false);
274    
275                                            Collections.sort(list);
276    
277                                            list = Collections.unmodifiableList(list);
278                                    }
279                                    else {
280                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
281                                                            end);
282                                    }
283    
284                                    cacheResult(list);
285    
286                                    finderCache.putResult(finderPath, finderArgs, list);
287                            }
288                            catch (Exception e) {
289                                    finderCache.removeResult(finderPath, finderArgs);
290    
291                                    throw processException(e);
292                            }
293                            finally {
294                                    closeSession(session);
295                            }
296                    }
297    
298                    return list;
299            }
300    
301            /**
302             * Returns the first user in the ordered set where uuid = &#63;.
303             *
304             * @param uuid the uuid
305             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
306             * @return the first matching user
307             * @throws NoSuchUserException if a matching user could not be found
308             */
309            @Override
310            public User findByUuid_First(String uuid,
311                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
312                    User user = fetchByUuid_First(uuid, orderByComparator);
313    
314                    if (user != null) {
315                            return user;
316                    }
317    
318                    StringBundler msg = new StringBundler(4);
319    
320                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
321    
322                    msg.append("uuid=");
323                    msg.append(uuid);
324    
325                    msg.append(StringPool.CLOSE_CURLY_BRACE);
326    
327                    throw new NoSuchUserException(msg.toString());
328            }
329    
330            /**
331             * Returns the first user in the ordered set where uuid = &#63;.
332             *
333             * @param uuid the uuid
334             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
335             * @return the first matching user, or <code>null</code> if a matching user could not be found
336             */
337            @Override
338            public User fetchByUuid_First(String uuid,
339                    OrderByComparator<User> orderByComparator) {
340                    List<User> list = findByUuid(uuid, 0, 1, orderByComparator);
341    
342                    if (!list.isEmpty()) {
343                            return list.get(0);
344                    }
345    
346                    return null;
347            }
348    
349            /**
350             * Returns the last user in the ordered set where uuid = &#63;.
351             *
352             * @param uuid the uuid
353             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
354             * @return the last matching user
355             * @throws NoSuchUserException if a matching user could not be found
356             */
357            @Override
358            public User findByUuid_Last(String uuid,
359                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
360                    User user = fetchByUuid_Last(uuid, orderByComparator);
361    
362                    if (user != null) {
363                            return user;
364                    }
365    
366                    StringBundler msg = new StringBundler(4);
367    
368                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
369    
370                    msg.append("uuid=");
371                    msg.append(uuid);
372    
373                    msg.append(StringPool.CLOSE_CURLY_BRACE);
374    
375                    throw new NoSuchUserException(msg.toString());
376            }
377    
378            /**
379             * Returns the last user in the ordered set where uuid = &#63;.
380             *
381             * @param uuid the uuid
382             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
383             * @return the last matching user, or <code>null</code> if a matching user could not be found
384             */
385            @Override
386            public User fetchByUuid_Last(String uuid,
387                    OrderByComparator<User> orderByComparator) {
388                    int count = countByUuid(uuid);
389    
390                    if (count == 0) {
391                            return null;
392                    }
393    
394                    List<User> list = findByUuid(uuid, count - 1, count, orderByComparator);
395    
396                    if (!list.isEmpty()) {
397                            return list.get(0);
398                    }
399    
400                    return null;
401            }
402    
403            /**
404             * Returns the users before and after the current user in the ordered set where uuid = &#63;.
405             *
406             * @param userId the primary key of the current user
407             * @param uuid the uuid
408             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
409             * @return the previous, current, and next user
410             * @throws NoSuchUserException if a user with the primary key could not be found
411             */
412            @Override
413            public User[] findByUuid_PrevAndNext(long userId, String uuid,
414                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
415                    User user = findByPrimaryKey(userId);
416    
417                    Session session = null;
418    
419                    try {
420                            session = openSession();
421    
422                            User[] array = new UserImpl[3];
423    
424                            array[0] = getByUuid_PrevAndNext(session, user, uuid,
425                                            orderByComparator, true);
426    
427                            array[1] = user;
428    
429                            array[2] = getByUuid_PrevAndNext(session, user, uuid,
430                                            orderByComparator, false);
431    
432                            return array;
433                    }
434                    catch (Exception e) {
435                            throw processException(e);
436                    }
437                    finally {
438                            closeSession(session);
439                    }
440            }
441    
442            protected User getByUuid_PrevAndNext(Session session, User user,
443                    String uuid, OrderByComparator<User> orderByComparator, boolean previous) {
444                    StringBundler query = null;
445    
446                    if (orderByComparator != null) {
447                            query = new StringBundler(4 +
448                                            (orderByComparator.getOrderByConditionFields().length * 3) +
449                                            (orderByComparator.getOrderByFields().length * 3));
450                    }
451                    else {
452                            query = new StringBundler(3);
453                    }
454    
455                    query.append(_SQL_SELECT_USER_WHERE);
456    
457                    boolean bindUuid = false;
458    
459                    if (uuid == null) {
460                            query.append(_FINDER_COLUMN_UUID_UUID_1);
461                    }
462                    else if (uuid.equals(StringPool.BLANK)) {
463                            query.append(_FINDER_COLUMN_UUID_UUID_3);
464                    }
465                    else {
466                            bindUuid = true;
467    
468                            query.append(_FINDER_COLUMN_UUID_UUID_2);
469                    }
470    
471                    if (orderByComparator != null) {
472                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
473    
474                            if (orderByConditionFields.length > 0) {
475                                    query.append(WHERE_AND);
476                            }
477    
478                            for (int i = 0; i < orderByConditionFields.length; i++) {
479                                    query.append(_ORDER_BY_ENTITY_ALIAS);
480                                    query.append(orderByConditionFields[i]);
481    
482                                    if ((i + 1) < orderByConditionFields.length) {
483                                            if (orderByComparator.isAscending() ^ previous) {
484                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
485                                            }
486                                            else {
487                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
488                                            }
489                                    }
490                                    else {
491                                            if (orderByComparator.isAscending() ^ previous) {
492                                                    query.append(WHERE_GREATER_THAN);
493                                            }
494                                            else {
495                                                    query.append(WHERE_LESSER_THAN);
496                                            }
497                                    }
498                            }
499    
500                            query.append(ORDER_BY_CLAUSE);
501    
502                            String[] orderByFields = orderByComparator.getOrderByFields();
503    
504                            for (int i = 0; i < orderByFields.length; i++) {
505                                    query.append(_ORDER_BY_ENTITY_ALIAS);
506                                    query.append(orderByFields[i]);
507    
508                                    if ((i + 1) < orderByFields.length) {
509                                            if (orderByComparator.isAscending() ^ previous) {
510                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
511                                            }
512                                            else {
513                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
514                                            }
515                                    }
516                                    else {
517                                            if (orderByComparator.isAscending() ^ previous) {
518                                                    query.append(ORDER_BY_ASC);
519                                            }
520                                            else {
521                                                    query.append(ORDER_BY_DESC);
522                                            }
523                                    }
524                            }
525                    }
526                    else {
527                            query.append(UserModelImpl.ORDER_BY_JPQL);
528                    }
529    
530                    String sql = query.toString();
531    
532                    Query q = session.createQuery(sql);
533    
534                    q.setFirstResult(0);
535                    q.setMaxResults(2);
536    
537                    QueryPos qPos = QueryPos.getInstance(q);
538    
539                    if (bindUuid) {
540                            qPos.add(uuid);
541                    }
542    
543                    if (orderByComparator != null) {
544                            Object[] values = orderByComparator.getOrderByConditionValues(user);
545    
546                            for (Object value : values) {
547                                    qPos.add(value);
548                            }
549                    }
550    
551                    List<User> list = q.list();
552    
553                    if (list.size() == 2) {
554                            return list.get(1);
555                    }
556                    else {
557                            return null;
558                    }
559            }
560    
561            /**
562             * Removes all the users where uuid = &#63; from the database.
563             *
564             * @param uuid the uuid
565             */
566            @Override
567            public void removeByUuid(String uuid) {
568                    for (User user : findByUuid(uuid, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
569                                    null)) {
570                            remove(user);
571                    }
572            }
573    
574            /**
575             * Returns the number of users where uuid = &#63;.
576             *
577             * @param uuid the uuid
578             * @return the number of matching users
579             */
580            @Override
581            public int countByUuid(String uuid) {
582                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID;
583    
584                    Object[] finderArgs = new Object[] { uuid };
585    
586                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
587    
588                    if (count == null) {
589                            StringBundler query = new StringBundler(2);
590    
591                            query.append(_SQL_COUNT_USER_WHERE);
592    
593                            boolean bindUuid = false;
594    
595                            if (uuid == null) {
596                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
597                            }
598                            else if (uuid.equals(StringPool.BLANK)) {
599                                    query.append(_FINDER_COLUMN_UUID_UUID_3);
600                            }
601                            else {
602                                    bindUuid = true;
603    
604                                    query.append(_FINDER_COLUMN_UUID_UUID_2);
605                            }
606    
607                            String sql = query.toString();
608    
609                            Session session = null;
610    
611                            try {
612                                    session = openSession();
613    
614                                    Query q = session.createQuery(sql);
615    
616                                    QueryPos qPos = QueryPos.getInstance(q);
617    
618                                    if (bindUuid) {
619                                            qPos.add(uuid);
620                                    }
621    
622                                    count = (Long)q.uniqueResult();
623    
624                                    finderCache.putResult(finderPath, finderArgs, count);
625                            }
626                            catch (Exception e) {
627                                    finderCache.removeResult(finderPath, finderArgs);
628    
629                                    throw processException(e);
630                            }
631                            finally {
632                                    closeSession(session);
633                            }
634                    }
635    
636                    return count.intValue();
637            }
638    
639            private static final String _FINDER_COLUMN_UUID_UUID_1 = "user.uuid IS NULL";
640            private static final String _FINDER_COLUMN_UUID_UUID_2 = "user.uuid = ?";
641            private static final String _FINDER_COLUMN_UUID_UUID_3 = "(user.uuid IS NULL OR user.uuid = '')";
642            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
643                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
644                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid_C",
645                            new String[] {
646                                    String.class.getName(), Long.class.getName(),
647                                    
648                            Integer.class.getName(), Integer.class.getName(),
649                                    OrderByComparator.class.getName()
650                            });
651            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C =
652                    new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
653                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
654                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid_C",
655                            new String[] { String.class.getName(), Long.class.getName() },
656                            UserModelImpl.UUID_COLUMN_BITMASK |
657                            UserModelImpl.COMPANYID_COLUMN_BITMASK);
658            public static final FinderPath FINDER_PATH_COUNT_BY_UUID_C = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
659                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
660                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid_C",
661                            new String[] { String.class.getName(), Long.class.getName() });
662    
663            /**
664             * Returns all the users where uuid = &#63; and companyId = &#63;.
665             *
666             * @param uuid the uuid
667             * @param companyId the company ID
668             * @return the matching users
669             */
670            @Override
671            public List<User> findByUuid_C(String uuid, long companyId) {
672                    return findByUuid_C(uuid, companyId, QueryUtil.ALL_POS,
673                            QueryUtil.ALL_POS, null);
674            }
675    
676            /**
677             * Returns a range of all the users where uuid = &#63; and companyId = &#63;.
678             *
679             * <p>
680             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
681             * </p>
682             *
683             * @param uuid the uuid
684             * @param companyId the company ID
685             * @param start the lower bound of the range of users
686             * @param end the upper bound of the range of users (not inclusive)
687             * @return the range of matching users
688             */
689            @Override
690            public List<User> findByUuid_C(String uuid, long companyId, int start,
691                    int end) {
692                    return findByUuid_C(uuid, companyId, start, end, null);
693            }
694    
695            /**
696             * Returns an ordered range of all the users where uuid = &#63; and companyId = &#63;.
697             *
698             * <p>
699             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
700             * </p>
701             *
702             * @param uuid the uuid
703             * @param companyId the company ID
704             * @param start the lower bound of the range of users
705             * @param end the upper bound of the range of users (not inclusive)
706             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
707             * @return the ordered range of matching users
708             */
709            @Override
710            public List<User> findByUuid_C(String uuid, long companyId, int start,
711                    int end, OrderByComparator<User> orderByComparator) {
712                    return findByUuid_C(uuid, companyId, start, end, orderByComparator, true);
713            }
714    
715            /**
716             * Returns an ordered range of all the users where uuid = &#63; and companyId = &#63;.
717             *
718             * <p>
719             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
720             * </p>
721             *
722             * @param uuid the uuid
723             * @param companyId the company ID
724             * @param start the lower bound of the range of users
725             * @param end the upper bound of the range of users (not inclusive)
726             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
727             * @param retrieveFromCache whether to retrieve from the finder cache
728             * @return the ordered range of matching users
729             */
730            @Override
731            public List<User> findByUuid_C(String uuid, long companyId, int start,
732                    int end, OrderByComparator<User> orderByComparator,
733                    boolean retrieveFromCache) {
734                    boolean pagination = true;
735                    FinderPath finderPath = null;
736                    Object[] finderArgs = null;
737    
738                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
739                                    (orderByComparator == null)) {
740                            pagination = false;
741                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C;
742                            finderArgs = new Object[] { uuid, companyId };
743                    }
744                    else {
745                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C;
746                            finderArgs = new Object[] {
747                                            uuid, companyId,
748                                            
749                                            start, end, orderByComparator
750                                    };
751                    }
752    
753                    List<User> list = null;
754    
755                    if (retrieveFromCache) {
756                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
757                                            this);
758    
759                            if ((list != null) && !list.isEmpty()) {
760                                    for (User user : list) {
761                                            if (!Validator.equals(uuid, user.getUuid()) ||
762                                                            (companyId != user.getCompanyId())) {
763                                                    list = null;
764    
765                                                    break;
766                                            }
767                                    }
768                            }
769                    }
770    
771                    if (list == null) {
772                            StringBundler query = null;
773    
774                            if (orderByComparator != null) {
775                                    query = new StringBundler(4 +
776                                                    (orderByComparator.getOrderByFields().length * 2));
777                            }
778                            else {
779                                    query = new StringBundler(4);
780                            }
781    
782                            query.append(_SQL_SELECT_USER_WHERE);
783    
784                            boolean bindUuid = false;
785    
786                            if (uuid == null) {
787                                    query.append(_FINDER_COLUMN_UUID_C_UUID_1);
788                            }
789                            else if (uuid.equals(StringPool.BLANK)) {
790                                    query.append(_FINDER_COLUMN_UUID_C_UUID_3);
791                            }
792                            else {
793                                    bindUuid = true;
794    
795                                    query.append(_FINDER_COLUMN_UUID_C_UUID_2);
796                            }
797    
798                            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
799    
800                            if (orderByComparator != null) {
801                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
802                                            orderByComparator);
803                            }
804                            else
805                             if (pagination) {
806                                    query.append(UserModelImpl.ORDER_BY_JPQL);
807                            }
808    
809                            String sql = query.toString();
810    
811                            Session session = null;
812    
813                            try {
814                                    session = openSession();
815    
816                                    Query q = session.createQuery(sql);
817    
818                                    QueryPos qPos = QueryPos.getInstance(q);
819    
820                                    if (bindUuid) {
821                                            qPos.add(uuid);
822                                    }
823    
824                                    qPos.add(companyId);
825    
826                                    if (!pagination) {
827                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
828                                                            end, false);
829    
830                                            Collections.sort(list);
831    
832                                            list = Collections.unmodifiableList(list);
833                                    }
834                                    else {
835                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
836                                                            end);
837                                    }
838    
839                                    cacheResult(list);
840    
841                                    finderCache.putResult(finderPath, finderArgs, list);
842                            }
843                            catch (Exception e) {
844                                    finderCache.removeResult(finderPath, finderArgs);
845    
846                                    throw processException(e);
847                            }
848                            finally {
849                                    closeSession(session);
850                            }
851                    }
852    
853                    return list;
854            }
855    
856            /**
857             * Returns the first user in the ordered set where uuid = &#63; and companyId = &#63;.
858             *
859             * @param uuid the uuid
860             * @param companyId the company ID
861             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
862             * @return the first matching user
863             * @throws NoSuchUserException if a matching user could not be found
864             */
865            @Override
866            public User findByUuid_C_First(String uuid, long companyId,
867                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
868                    User user = fetchByUuid_C_First(uuid, companyId, orderByComparator);
869    
870                    if (user != null) {
871                            return user;
872                    }
873    
874                    StringBundler msg = new StringBundler(6);
875    
876                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
877    
878                    msg.append("uuid=");
879                    msg.append(uuid);
880    
881                    msg.append(", companyId=");
882                    msg.append(companyId);
883    
884                    msg.append(StringPool.CLOSE_CURLY_BRACE);
885    
886                    throw new NoSuchUserException(msg.toString());
887            }
888    
889            /**
890             * Returns the first user in the ordered set where uuid = &#63; and companyId = &#63;.
891             *
892             * @param uuid the uuid
893             * @param companyId the company ID
894             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
895             * @return the first matching user, or <code>null</code> if a matching user could not be found
896             */
897            @Override
898            public User fetchByUuid_C_First(String uuid, long companyId,
899                    OrderByComparator<User> orderByComparator) {
900                    List<User> list = findByUuid_C(uuid, companyId, 0, 1, orderByComparator);
901    
902                    if (!list.isEmpty()) {
903                            return list.get(0);
904                    }
905    
906                    return null;
907            }
908    
909            /**
910             * Returns the last user in the ordered set where uuid = &#63; and companyId = &#63;.
911             *
912             * @param uuid the uuid
913             * @param companyId the company ID
914             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
915             * @return the last matching user
916             * @throws NoSuchUserException if a matching user could not be found
917             */
918            @Override
919            public User findByUuid_C_Last(String uuid, long companyId,
920                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
921                    User user = fetchByUuid_C_Last(uuid, companyId, orderByComparator);
922    
923                    if (user != null) {
924                            return user;
925                    }
926    
927                    StringBundler msg = new StringBundler(6);
928    
929                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
930    
931                    msg.append("uuid=");
932                    msg.append(uuid);
933    
934                    msg.append(", companyId=");
935                    msg.append(companyId);
936    
937                    msg.append(StringPool.CLOSE_CURLY_BRACE);
938    
939                    throw new NoSuchUserException(msg.toString());
940            }
941    
942            /**
943             * Returns the last user in the ordered set where uuid = &#63; and companyId = &#63;.
944             *
945             * @param uuid the uuid
946             * @param companyId the company ID
947             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
948             * @return the last matching user, or <code>null</code> if a matching user could not be found
949             */
950            @Override
951            public User fetchByUuid_C_Last(String uuid, long companyId,
952                    OrderByComparator<User> orderByComparator) {
953                    int count = countByUuid_C(uuid, companyId);
954    
955                    if (count == 0) {
956                            return null;
957                    }
958    
959                    List<User> list = findByUuid_C(uuid, companyId, count - 1, count,
960                                    orderByComparator);
961    
962                    if (!list.isEmpty()) {
963                            return list.get(0);
964                    }
965    
966                    return null;
967            }
968    
969            /**
970             * Returns the users before and after the current user in the ordered set where uuid = &#63; and companyId = &#63;.
971             *
972             * @param userId the primary key of the current user
973             * @param uuid the uuid
974             * @param companyId the company ID
975             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
976             * @return the previous, current, and next user
977             * @throws NoSuchUserException if a user with the primary key could not be found
978             */
979            @Override
980            public User[] findByUuid_C_PrevAndNext(long userId, String uuid,
981                    long companyId, OrderByComparator<User> orderByComparator)
982                    throws NoSuchUserException {
983                    User user = findByPrimaryKey(userId);
984    
985                    Session session = null;
986    
987                    try {
988                            session = openSession();
989    
990                            User[] array = new UserImpl[3];
991    
992                            array[0] = getByUuid_C_PrevAndNext(session, user, uuid, companyId,
993                                            orderByComparator, true);
994    
995                            array[1] = user;
996    
997                            array[2] = getByUuid_C_PrevAndNext(session, user, uuid, companyId,
998                                            orderByComparator, false);
999    
1000                            return array;
1001                    }
1002                    catch (Exception e) {
1003                            throw processException(e);
1004                    }
1005                    finally {
1006                            closeSession(session);
1007                    }
1008            }
1009    
1010            protected User getByUuid_C_PrevAndNext(Session session, User user,
1011                    String uuid, long companyId, OrderByComparator<User> orderByComparator,
1012                    boolean previous) {
1013                    StringBundler query = null;
1014    
1015                    if (orderByComparator != null) {
1016                            query = new StringBundler(5 +
1017                                            (orderByComparator.getOrderByConditionFields().length * 3) +
1018                                            (orderByComparator.getOrderByFields().length * 3));
1019                    }
1020                    else {
1021                            query = new StringBundler(4);
1022                    }
1023    
1024                    query.append(_SQL_SELECT_USER_WHERE);
1025    
1026                    boolean bindUuid = false;
1027    
1028                    if (uuid == null) {
1029                            query.append(_FINDER_COLUMN_UUID_C_UUID_1);
1030                    }
1031                    else if (uuid.equals(StringPool.BLANK)) {
1032                            query.append(_FINDER_COLUMN_UUID_C_UUID_3);
1033                    }
1034                    else {
1035                            bindUuid = true;
1036    
1037                            query.append(_FINDER_COLUMN_UUID_C_UUID_2);
1038                    }
1039    
1040                    query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
1041    
1042                    if (orderByComparator != null) {
1043                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1044    
1045                            if (orderByConditionFields.length > 0) {
1046                                    query.append(WHERE_AND);
1047                            }
1048    
1049                            for (int i = 0; i < orderByConditionFields.length; i++) {
1050                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1051                                    query.append(orderByConditionFields[i]);
1052    
1053                                    if ((i + 1) < orderByConditionFields.length) {
1054                                            if (orderByComparator.isAscending() ^ previous) {
1055                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1056                                            }
1057                                            else {
1058                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1059                                            }
1060                                    }
1061                                    else {
1062                                            if (orderByComparator.isAscending() ^ previous) {
1063                                                    query.append(WHERE_GREATER_THAN);
1064                                            }
1065                                            else {
1066                                                    query.append(WHERE_LESSER_THAN);
1067                                            }
1068                                    }
1069                            }
1070    
1071                            query.append(ORDER_BY_CLAUSE);
1072    
1073                            String[] orderByFields = orderByComparator.getOrderByFields();
1074    
1075                            for (int i = 0; i < orderByFields.length; i++) {
1076                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1077                                    query.append(orderByFields[i]);
1078    
1079                                    if ((i + 1) < orderByFields.length) {
1080                                            if (orderByComparator.isAscending() ^ previous) {
1081                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1082                                            }
1083                                            else {
1084                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1085                                            }
1086                                    }
1087                                    else {
1088                                            if (orderByComparator.isAscending() ^ previous) {
1089                                                    query.append(ORDER_BY_ASC);
1090                                            }
1091                                            else {
1092                                                    query.append(ORDER_BY_DESC);
1093                                            }
1094                                    }
1095                            }
1096                    }
1097                    else {
1098                            query.append(UserModelImpl.ORDER_BY_JPQL);
1099                    }
1100    
1101                    String sql = query.toString();
1102    
1103                    Query q = session.createQuery(sql);
1104    
1105                    q.setFirstResult(0);
1106                    q.setMaxResults(2);
1107    
1108                    QueryPos qPos = QueryPos.getInstance(q);
1109    
1110                    if (bindUuid) {
1111                            qPos.add(uuid);
1112                    }
1113    
1114                    qPos.add(companyId);
1115    
1116                    if (orderByComparator != null) {
1117                            Object[] values = orderByComparator.getOrderByConditionValues(user);
1118    
1119                            for (Object value : values) {
1120                                    qPos.add(value);
1121                            }
1122                    }
1123    
1124                    List<User> list = q.list();
1125    
1126                    if (list.size() == 2) {
1127                            return list.get(1);
1128                    }
1129                    else {
1130                            return null;
1131                    }
1132            }
1133    
1134            /**
1135             * Removes all the users where uuid = &#63; and companyId = &#63; from the database.
1136             *
1137             * @param uuid the uuid
1138             * @param companyId the company ID
1139             */
1140            @Override
1141            public void removeByUuid_C(String uuid, long companyId) {
1142                    for (User user : findByUuid_C(uuid, companyId, QueryUtil.ALL_POS,
1143                                    QueryUtil.ALL_POS, null)) {
1144                            remove(user);
1145                    }
1146            }
1147    
1148            /**
1149             * Returns the number of users where uuid = &#63; and companyId = &#63;.
1150             *
1151             * @param uuid the uuid
1152             * @param companyId the company ID
1153             * @return the number of matching users
1154             */
1155            @Override
1156            public int countByUuid_C(String uuid, long companyId) {
1157                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID_C;
1158    
1159                    Object[] finderArgs = new Object[] { uuid, companyId };
1160    
1161                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
1162    
1163                    if (count == null) {
1164                            StringBundler query = new StringBundler(3);
1165    
1166                            query.append(_SQL_COUNT_USER_WHERE);
1167    
1168                            boolean bindUuid = false;
1169    
1170                            if (uuid == null) {
1171                                    query.append(_FINDER_COLUMN_UUID_C_UUID_1);
1172                            }
1173                            else if (uuid.equals(StringPool.BLANK)) {
1174                                    query.append(_FINDER_COLUMN_UUID_C_UUID_3);
1175                            }
1176                            else {
1177                                    bindUuid = true;
1178    
1179                                    query.append(_FINDER_COLUMN_UUID_C_UUID_2);
1180                            }
1181    
1182                            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
1183    
1184                            String sql = query.toString();
1185    
1186                            Session session = null;
1187    
1188                            try {
1189                                    session = openSession();
1190    
1191                                    Query q = session.createQuery(sql);
1192    
1193                                    QueryPos qPos = QueryPos.getInstance(q);
1194    
1195                                    if (bindUuid) {
1196                                            qPos.add(uuid);
1197                                    }
1198    
1199                                    qPos.add(companyId);
1200    
1201                                    count = (Long)q.uniqueResult();
1202    
1203                                    finderCache.putResult(finderPath, finderArgs, count);
1204                            }
1205                            catch (Exception e) {
1206                                    finderCache.removeResult(finderPath, finderArgs);
1207    
1208                                    throw processException(e);
1209                            }
1210                            finally {
1211                                    closeSession(session);
1212                            }
1213                    }
1214    
1215                    return count.intValue();
1216            }
1217    
1218            private static final String _FINDER_COLUMN_UUID_C_UUID_1 = "user.uuid IS NULL AND ";
1219            private static final String _FINDER_COLUMN_UUID_C_UUID_2 = "user.uuid = ? AND ";
1220            private static final String _FINDER_COLUMN_UUID_C_UUID_3 = "(user.uuid IS NULL OR user.uuid = '') AND ";
1221            private static final String _FINDER_COLUMN_UUID_C_COMPANYID_2 = "user.companyId = ?";
1222            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID =
1223                    new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1224                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
1225                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByCompanyId",
1226                            new String[] {
1227                                    Long.class.getName(),
1228                                    
1229                            Integer.class.getName(), Integer.class.getName(),
1230                                    OrderByComparator.class.getName()
1231                            });
1232            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID =
1233                    new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1234                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
1235                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByCompanyId",
1236                            new String[] { Long.class.getName() },
1237                            UserModelImpl.COMPANYID_COLUMN_BITMASK);
1238            public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1239                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
1240                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCompanyId",
1241                            new String[] { Long.class.getName() });
1242    
1243            /**
1244             * Returns all the users where companyId = &#63;.
1245             *
1246             * @param companyId the company ID
1247             * @return the matching users
1248             */
1249            @Override
1250            public List<User> findByCompanyId(long companyId) {
1251                    return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
1252                            null);
1253            }
1254    
1255            /**
1256             * Returns a range of all the users where companyId = &#63;.
1257             *
1258             * <p>
1259             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1260             * </p>
1261             *
1262             * @param companyId the company ID
1263             * @param start the lower bound of the range of users
1264             * @param end the upper bound of the range of users (not inclusive)
1265             * @return the range of matching users
1266             */
1267            @Override
1268            public List<User> findByCompanyId(long companyId, int start, int end) {
1269                    return findByCompanyId(companyId, start, end, null);
1270            }
1271    
1272            /**
1273             * Returns an ordered range of all the users where companyId = &#63;.
1274             *
1275             * <p>
1276             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1277             * </p>
1278             *
1279             * @param companyId the company ID
1280             * @param start the lower bound of the range of users
1281             * @param end the upper bound of the range of users (not inclusive)
1282             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1283             * @return the ordered range of matching users
1284             */
1285            @Override
1286            public List<User> findByCompanyId(long companyId, int start, int end,
1287                    OrderByComparator<User> orderByComparator) {
1288                    return findByCompanyId(companyId, start, end, orderByComparator, true);
1289            }
1290    
1291            /**
1292             * Returns an ordered range of all the users where companyId = &#63;.
1293             *
1294             * <p>
1295             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1296             * </p>
1297             *
1298             * @param companyId the company ID
1299             * @param start the lower bound of the range of users
1300             * @param end the upper bound of the range of users (not inclusive)
1301             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1302             * @param retrieveFromCache whether to retrieve from the finder cache
1303             * @return the ordered range of matching users
1304             */
1305            @Override
1306            public List<User> findByCompanyId(long companyId, int start, int end,
1307                    OrderByComparator<User> orderByComparator, boolean retrieveFromCache) {
1308                    boolean pagination = true;
1309                    FinderPath finderPath = null;
1310                    Object[] finderArgs = null;
1311    
1312                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1313                                    (orderByComparator == null)) {
1314                            pagination = false;
1315                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID;
1316                            finderArgs = new Object[] { companyId };
1317                    }
1318                    else {
1319                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID;
1320                            finderArgs = new Object[] { companyId, start, end, orderByComparator };
1321                    }
1322    
1323                    List<User> list = null;
1324    
1325                    if (retrieveFromCache) {
1326                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
1327                                            this);
1328    
1329                            if ((list != null) && !list.isEmpty()) {
1330                                    for (User user : list) {
1331                                            if ((companyId != user.getCompanyId())) {
1332                                                    list = null;
1333    
1334                                                    break;
1335                                            }
1336                                    }
1337                            }
1338                    }
1339    
1340                    if (list == null) {
1341                            StringBundler query = null;
1342    
1343                            if (orderByComparator != null) {
1344                                    query = new StringBundler(3 +
1345                                                    (orderByComparator.getOrderByFields().length * 2));
1346                            }
1347                            else {
1348                                    query = new StringBundler(3);
1349                            }
1350    
1351                            query.append(_SQL_SELECT_USER_WHERE);
1352    
1353                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1354    
1355                            if (orderByComparator != null) {
1356                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1357                                            orderByComparator);
1358                            }
1359                            else
1360                             if (pagination) {
1361                                    query.append(UserModelImpl.ORDER_BY_JPQL);
1362                            }
1363    
1364                            String sql = query.toString();
1365    
1366                            Session session = null;
1367    
1368                            try {
1369                                    session = openSession();
1370    
1371                                    Query q = session.createQuery(sql);
1372    
1373                                    QueryPos qPos = QueryPos.getInstance(q);
1374    
1375                                    qPos.add(companyId);
1376    
1377                                    if (!pagination) {
1378                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
1379                                                            end, false);
1380    
1381                                            Collections.sort(list);
1382    
1383                                            list = Collections.unmodifiableList(list);
1384                                    }
1385                                    else {
1386                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
1387                                                            end);
1388                                    }
1389    
1390                                    cacheResult(list);
1391    
1392                                    finderCache.putResult(finderPath, finderArgs, list);
1393                            }
1394                            catch (Exception e) {
1395                                    finderCache.removeResult(finderPath, finderArgs);
1396    
1397                                    throw processException(e);
1398                            }
1399                            finally {
1400                                    closeSession(session);
1401                            }
1402                    }
1403    
1404                    return list;
1405            }
1406    
1407            /**
1408             * Returns the first user in the ordered set where companyId = &#63;.
1409             *
1410             * @param companyId the company ID
1411             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1412             * @return the first matching user
1413             * @throws NoSuchUserException if a matching user could not be found
1414             */
1415            @Override
1416            public User findByCompanyId_First(long companyId,
1417                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
1418                    User user = fetchByCompanyId_First(companyId, orderByComparator);
1419    
1420                    if (user != null) {
1421                            return user;
1422                    }
1423    
1424                    StringBundler msg = new StringBundler(4);
1425    
1426                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1427    
1428                    msg.append("companyId=");
1429                    msg.append(companyId);
1430    
1431                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1432    
1433                    throw new NoSuchUserException(msg.toString());
1434            }
1435    
1436            /**
1437             * Returns the first user in the ordered set where companyId = &#63;.
1438             *
1439             * @param companyId the company ID
1440             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1441             * @return the first matching user, or <code>null</code> if a matching user could not be found
1442             */
1443            @Override
1444            public User fetchByCompanyId_First(long companyId,
1445                    OrderByComparator<User> orderByComparator) {
1446                    List<User> list = findByCompanyId(companyId, 0, 1, orderByComparator);
1447    
1448                    if (!list.isEmpty()) {
1449                            return list.get(0);
1450                    }
1451    
1452                    return null;
1453            }
1454    
1455            /**
1456             * Returns the last user in the ordered set where companyId = &#63;.
1457             *
1458             * @param companyId the company ID
1459             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1460             * @return the last matching user
1461             * @throws NoSuchUserException if a matching user could not be found
1462             */
1463            @Override
1464            public User findByCompanyId_Last(long companyId,
1465                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
1466                    User user = fetchByCompanyId_Last(companyId, orderByComparator);
1467    
1468                    if (user != null) {
1469                            return user;
1470                    }
1471    
1472                    StringBundler msg = new StringBundler(4);
1473    
1474                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1475    
1476                    msg.append("companyId=");
1477                    msg.append(companyId);
1478    
1479                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1480    
1481                    throw new NoSuchUserException(msg.toString());
1482            }
1483    
1484            /**
1485             * Returns the last user in the ordered set where companyId = &#63;.
1486             *
1487             * @param companyId the company ID
1488             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1489             * @return the last matching user, or <code>null</code> if a matching user could not be found
1490             */
1491            @Override
1492            public User fetchByCompanyId_Last(long companyId,
1493                    OrderByComparator<User> orderByComparator) {
1494                    int count = countByCompanyId(companyId);
1495    
1496                    if (count == 0) {
1497                            return null;
1498                    }
1499    
1500                    List<User> list = findByCompanyId(companyId, count - 1, count,
1501                                    orderByComparator);
1502    
1503                    if (!list.isEmpty()) {
1504                            return list.get(0);
1505                    }
1506    
1507                    return null;
1508            }
1509    
1510            /**
1511             * Returns the users before and after the current user in the ordered set where companyId = &#63;.
1512             *
1513             * @param userId the primary key of the current user
1514             * @param companyId the company ID
1515             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1516             * @return the previous, current, and next user
1517             * @throws NoSuchUserException if a user with the primary key could not be found
1518             */
1519            @Override
1520            public User[] findByCompanyId_PrevAndNext(long userId, long companyId,
1521                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
1522                    User user = findByPrimaryKey(userId);
1523    
1524                    Session session = null;
1525    
1526                    try {
1527                            session = openSession();
1528    
1529                            User[] array = new UserImpl[3];
1530    
1531                            array[0] = getByCompanyId_PrevAndNext(session, user, companyId,
1532                                            orderByComparator, true);
1533    
1534                            array[1] = user;
1535    
1536                            array[2] = getByCompanyId_PrevAndNext(session, user, companyId,
1537                                            orderByComparator, false);
1538    
1539                            return array;
1540                    }
1541                    catch (Exception e) {
1542                            throw processException(e);
1543                    }
1544                    finally {
1545                            closeSession(session);
1546                    }
1547            }
1548    
1549            protected User getByCompanyId_PrevAndNext(Session session, User user,
1550                    long companyId, OrderByComparator<User> orderByComparator,
1551                    boolean previous) {
1552                    StringBundler query = null;
1553    
1554                    if (orderByComparator != null) {
1555                            query = new StringBundler(4 +
1556                                            (orderByComparator.getOrderByConditionFields().length * 3) +
1557                                            (orderByComparator.getOrderByFields().length * 3));
1558                    }
1559                    else {
1560                            query = new StringBundler(3);
1561                    }
1562    
1563                    query.append(_SQL_SELECT_USER_WHERE);
1564    
1565                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1566    
1567                    if (orderByComparator != null) {
1568                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1569    
1570                            if (orderByConditionFields.length > 0) {
1571                                    query.append(WHERE_AND);
1572                            }
1573    
1574                            for (int i = 0; i < orderByConditionFields.length; i++) {
1575                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1576                                    query.append(orderByConditionFields[i]);
1577    
1578                                    if ((i + 1) < orderByConditionFields.length) {
1579                                            if (orderByComparator.isAscending() ^ previous) {
1580                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1581                                            }
1582                                            else {
1583                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1584                                            }
1585                                    }
1586                                    else {
1587                                            if (orderByComparator.isAscending() ^ previous) {
1588                                                    query.append(WHERE_GREATER_THAN);
1589                                            }
1590                                            else {
1591                                                    query.append(WHERE_LESSER_THAN);
1592                                            }
1593                                    }
1594                            }
1595    
1596                            query.append(ORDER_BY_CLAUSE);
1597    
1598                            String[] orderByFields = orderByComparator.getOrderByFields();
1599    
1600                            for (int i = 0; i < orderByFields.length; i++) {
1601                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1602                                    query.append(orderByFields[i]);
1603    
1604                                    if ((i + 1) < orderByFields.length) {
1605                                            if (orderByComparator.isAscending() ^ previous) {
1606                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1607                                            }
1608                                            else {
1609                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1610                                            }
1611                                    }
1612                                    else {
1613                                            if (orderByComparator.isAscending() ^ previous) {
1614                                                    query.append(ORDER_BY_ASC);
1615                                            }
1616                                            else {
1617                                                    query.append(ORDER_BY_DESC);
1618                                            }
1619                                    }
1620                            }
1621                    }
1622                    else {
1623                            query.append(UserModelImpl.ORDER_BY_JPQL);
1624                    }
1625    
1626                    String sql = query.toString();
1627    
1628                    Query q = session.createQuery(sql);
1629    
1630                    q.setFirstResult(0);
1631                    q.setMaxResults(2);
1632    
1633                    QueryPos qPos = QueryPos.getInstance(q);
1634    
1635                    qPos.add(companyId);
1636    
1637                    if (orderByComparator != null) {
1638                            Object[] values = orderByComparator.getOrderByConditionValues(user);
1639    
1640                            for (Object value : values) {
1641                                    qPos.add(value);
1642                            }
1643                    }
1644    
1645                    List<User> list = q.list();
1646    
1647                    if (list.size() == 2) {
1648                            return list.get(1);
1649                    }
1650                    else {
1651                            return null;
1652                    }
1653            }
1654    
1655            /**
1656             * Removes all the users where companyId = &#63; from the database.
1657             *
1658             * @param companyId the company ID
1659             */
1660            @Override
1661            public void removeByCompanyId(long companyId) {
1662                    for (User user : findByCompanyId(companyId, QueryUtil.ALL_POS,
1663                                    QueryUtil.ALL_POS, null)) {
1664                            remove(user);
1665                    }
1666            }
1667    
1668            /**
1669             * Returns the number of users where companyId = &#63;.
1670             *
1671             * @param companyId the company ID
1672             * @return the number of matching users
1673             */
1674            @Override
1675            public int countByCompanyId(long companyId) {
1676                    FinderPath finderPath = FINDER_PATH_COUNT_BY_COMPANYID;
1677    
1678                    Object[] finderArgs = new Object[] { companyId };
1679    
1680                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
1681    
1682                    if (count == null) {
1683                            StringBundler query = new StringBundler(2);
1684    
1685                            query.append(_SQL_COUNT_USER_WHERE);
1686    
1687                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1688    
1689                            String sql = query.toString();
1690    
1691                            Session session = null;
1692    
1693                            try {
1694                                    session = openSession();
1695    
1696                                    Query q = session.createQuery(sql);
1697    
1698                                    QueryPos qPos = QueryPos.getInstance(q);
1699    
1700                                    qPos.add(companyId);
1701    
1702                                    count = (Long)q.uniqueResult();
1703    
1704                                    finderCache.putResult(finderPath, finderArgs, count);
1705                            }
1706                            catch (Exception e) {
1707                                    finderCache.removeResult(finderPath, finderArgs);
1708    
1709                                    throw processException(e);
1710                            }
1711                            finally {
1712                                    closeSession(session);
1713                            }
1714                    }
1715    
1716                    return count.intValue();
1717            }
1718    
1719            private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "user.companyId = ?";
1720            public static final FinderPath FINDER_PATH_FETCH_BY_CONTACTID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1721                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
1722                            FINDER_CLASS_NAME_ENTITY, "fetchByContactId",
1723                            new String[] { Long.class.getName() },
1724                            UserModelImpl.CONTACTID_COLUMN_BITMASK);
1725            public static final FinderPath FINDER_PATH_COUNT_BY_CONTACTID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1726                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
1727                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByContactId",
1728                            new String[] { Long.class.getName() });
1729    
1730            /**
1731             * Returns the user where contactId = &#63; or throws a {@link NoSuchUserException} if it could not be found.
1732             *
1733             * @param contactId the contact ID
1734             * @return the matching user
1735             * @throws NoSuchUserException if a matching user could not be found
1736             */
1737            @Override
1738            public User findByContactId(long contactId) throws NoSuchUserException {
1739                    User user = fetchByContactId(contactId);
1740    
1741                    if (user == null) {
1742                            StringBundler msg = new StringBundler(4);
1743    
1744                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1745    
1746                            msg.append("contactId=");
1747                            msg.append(contactId);
1748    
1749                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1750    
1751                            if (_log.isWarnEnabled()) {
1752                                    _log.warn(msg.toString());
1753                            }
1754    
1755                            throw new NoSuchUserException(msg.toString());
1756                    }
1757    
1758                    return user;
1759            }
1760    
1761            /**
1762             * Returns the user where contactId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
1763             *
1764             * @param contactId the contact ID
1765             * @return the matching user, or <code>null</code> if a matching user could not be found
1766             */
1767            @Override
1768            public User fetchByContactId(long contactId) {
1769                    return fetchByContactId(contactId, true);
1770            }
1771    
1772            /**
1773             * Returns the user where contactId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
1774             *
1775             * @param contactId the contact ID
1776             * @param retrieveFromCache whether to retrieve from the finder cache
1777             * @return the matching user, or <code>null</code> if a matching user could not be found
1778             */
1779            @Override
1780            public User fetchByContactId(long contactId, boolean retrieveFromCache) {
1781                    Object[] finderArgs = new Object[] { contactId };
1782    
1783                    Object result = null;
1784    
1785                    if (retrieveFromCache) {
1786                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_CONTACTID,
1787                                            finderArgs, this);
1788                    }
1789    
1790                    if (result instanceof User) {
1791                            User user = (User)result;
1792    
1793                            if ((contactId != user.getContactId())) {
1794                                    result = null;
1795                            }
1796                    }
1797    
1798                    if (result == null) {
1799                            StringBundler query = new StringBundler(3);
1800    
1801                            query.append(_SQL_SELECT_USER_WHERE);
1802    
1803                            query.append(_FINDER_COLUMN_CONTACTID_CONTACTID_2);
1804    
1805                            String sql = query.toString();
1806    
1807                            Session session = null;
1808    
1809                            try {
1810                                    session = openSession();
1811    
1812                                    Query q = session.createQuery(sql);
1813    
1814                                    QueryPos qPos = QueryPos.getInstance(q);
1815    
1816                                    qPos.add(contactId);
1817    
1818                                    List<User> list = q.list();
1819    
1820                                    if (list.isEmpty()) {
1821                                            finderCache.putResult(FINDER_PATH_FETCH_BY_CONTACTID,
1822                                                    finderArgs, list);
1823                                    }
1824                                    else {
1825                                            User user = list.get(0);
1826    
1827                                            result = user;
1828    
1829                                            cacheResult(user);
1830    
1831                                            if ((user.getContactId() != contactId)) {
1832                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_CONTACTID,
1833                                                            finderArgs, user);
1834                                            }
1835                                    }
1836                            }
1837                            catch (Exception e) {
1838                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_CONTACTID,
1839                                            finderArgs);
1840    
1841                                    throw processException(e);
1842                            }
1843                            finally {
1844                                    closeSession(session);
1845                            }
1846                    }
1847    
1848                    if (result instanceof List<?>) {
1849                            return null;
1850                    }
1851                    else {
1852                            return (User)result;
1853                    }
1854            }
1855    
1856            /**
1857             * Removes the user where contactId = &#63; from the database.
1858             *
1859             * @param contactId the contact ID
1860             * @return the user that was removed
1861             */
1862            @Override
1863            public User removeByContactId(long contactId) throws NoSuchUserException {
1864                    User user = findByContactId(contactId);
1865    
1866                    return remove(user);
1867            }
1868    
1869            /**
1870             * Returns the number of users where contactId = &#63;.
1871             *
1872             * @param contactId the contact ID
1873             * @return the number of matching users
1874             */
1875            @Override
1876            public int countByContactId(long contactId) {
1877                    FinderPath finderPath = FINDER_PATH_COUNT_BY_CONTACTID;
1878    
1879                    Object[] finderArgs = new Object[] { contactId };
1880    
1881                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
1882    
1883                    if (count == null) {
1884                            StringBundler query = new StringBundler(2);
1885    
1886                            query.append(_SQL_COUNT_USER_WHERE);
1887    
1888                            query.append(_FINDER_COLUMN_CONTACTID_CONTACTID_2);
1889    
1890                            String sql = query.toString();
1891    
1892                            Session session = null;
1893    
1894                            try {
1895                                    session = openSession();
1896    
1897                                    Query q = session.createQuery(sql);
1898    
1899                                    QueryPos qPos = QueryPos.getInstance(q);
1900    
1901                                    qPos.add(contactId);
1902    
1903                                    count = (Long)q.uniqueResult();
1904    
1905                                    finderCache.putResult(finderPath, finderArgs, count);
1906                            }
1907                            catch (Exception e) {
1908                                    finderCache.removeResult(finderPath, finderArgs);
1909    
1910                                    throw processException(e);
1911                            }
1912                            finally {
1913                                    closeSession(session);
1914                            }
1915                    }
1916    
1917                    return count.intValue();
1918            }
1919    
1920            private static final String _FINDER_COLUMN_CONTACTID_CONTACTID_2 = "user.contactId = ?";
1921            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_EMAILADDRESS =
1922                    new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1923                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
1924                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByEmailAddress",
1925                            new String[] {
1926                                    String.class.getName(),
1927                                    
1928                            Integer.class.getName(), Integer.class.getName(),
1929                                    OrderByComparator.class.getName()
1930                            });
1931            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_EMAILADDRESS =
1932                    new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1933                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
1934                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByEmailAddress",
1935                            new String[] { String.class.getName() },
1936                            UserModelImpl.EMAILADDRESS_COLUMN_BITMASK);
1937            public static final FinderPath FINDER_PATH_COUNT_BY_EMAILADDRESS = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1938                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
1939                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByEmailAddress",
1940                            new String[] { String.class.getName() });
1941    
1942            /**
1943             * Returns all the users where emailAddress = &#63;.
1944             *
1945             * @param emailAddress the email address
1946             * @return the matching users
1947             */
1948            @Override
1949            public List<User> findByEmailAddress(String emailAddress) {
1950                    return findByEmailAddress(emailAddress, QueryUtil.ALL_POS,
1951                            QueryUtil.ALL_POS, null);
1952            }
1953    
1954            /**
1955             * Returns a range of all the users where emailAddress = &#63;.
1956             *
1957             * <p>
1958             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1959             * </p>
1960             *
1961             * @param emailAddress the email address
1962             * @param start the lower bound of the range of users
1963             * @param end the upper bound of the range of users (not inclusive)
1964             * @return the range of matching users
1965             */
1966            @Override
1967            public List<User> findByEmailAddress(String emailAddress, int start, int end) {
1968                    return findByEmailAddress(emailAddress, start, end, null);
1969            }
1970    
1971            /**
1972             * Returns an ordered range of all the users where emailAddress = &#63;.
1973             *
1974             * <p>
1975             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1976             * </p>
1977             *
1978             * @param emailAddress the email address
1979             * @param start the lower bound of the range of users
1980             * @param end the upper bound of the range of users (not inclusive)
1981             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1982             * @return the ordered range of matching users
1983             */
1984            @Override
1985            public List<User> findByEmailAddress(String emailAddress, int start,
1986                    int end, OrderByComparator<User> orderByComparator) {
1987                    return findByEmailAddress(emailAddress, start, end, orderByComparator,
1988                            true);
1989            }
1990    
1991            /**
1992             * Returns an ordered range of all the users where emailAddress = &#63;.
1993             *
1994             * <p>
1995             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1996             * </p>
1997             *
1998             * @param emailAddress the email address
1999             * @param start the lower bound of the range of users
2000             * @param end the upper bound of the range of users (not inclusive)
2001             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2002             * @param retrieveFromCache whether to retrieve from the finder cache
2003             * @return the ordered range of matching users
2004             */
2005            @Override
2006            public List<User> findByEmailAddress(String emailAddress, int start,
2007                    int end, OrderByComparator<User> orderByComparator,
2008                    boolean retrieveFromCache) {
2009                    boolean pagination = true;
2010                    FinderPath finderPath = null;
2011                    Object[] finderArgs = null;
2012    
2013                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2014                                    (orderByComparator == null)) {
2015                            pagination = false;
2016                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_EMAILADDRESS;
2017                            finderArgs = new Object[] { emailAddress };
2018                    }
2019                    else {
2020                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_EMAILADDRESS;
2021                            finderArgs = new Object[] {
2022                                            emailAddress,
2023                                            
2024                                            start, end, orderByComparator
2025                                    };
2026                    }
2027    
2028                    List<User> list = null;
2029    
2030                    if (retrieveFromCache) {
2031                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
2032                                            this);
2033    
2034                            if ((list != null) && !list.isEmpty()) {
2035                                    for (User user : list) {
2036                                            if (!Validator.equals(emailAddress, user.getEmailAddress())) {
2037                                                    list = null;
2038    
2039                                                    break;
2040                                            }
2041                                    }
2042                            }
2043                    }
2044    
2045                    if (list == null) {
2046                            StringBundler query = null;
2047    
2048                            if (orderByComparator != null) {
2049                                    query = new StringBundler(3 +
2050                                                    (orderByComparator.getOrderByFields().length * 2));
2051                            }
2052                            else {
2053                                    query = new StringBundler(3);
2054                            }
2055    
2056                            query.append(_SQL_SELECT_USER_WHERE);
2057    
2058                            boolean bindEmailAddress = false;
2059    
2060                            if (emailAddress == null) {
2061                                    query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_1);
2062                            }
2063                            else if (emailAddress.equals(StringPool.BLANK)) {
2064                                    query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_3);
2065                            }
2066                            else {
2067                                    bindEmailAddress = true;
2068    
2069                                    query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_2);
2070                            }
2071    
2072                            if (orderByComparator != null) {
2073                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2074                                            orderByComparator);
2075                            }
2076                            else
2077                             if (pagination) {
2078                                    query.append(UserModelImpl.ORDER_BY_JPQL);
2079                            }
2080    
2081                            String sql = query.toString();
2082    
2083                            Session session = null;
2084    
2085                            try {
2086                                    session = openSession();
2087    
2088                                    Query q = session.createQuery(sql);
2089    
2090                                    QueryPos qPos = QueryPos.getInstance(q);
2091    
2092                                    if (bindEmailAddress) {
2093                                            qPos.add(emailAddress);
2094                                    }
2095    
2096                                    if (!pagination) {
2097                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
2098                                                            end, false);
2099    
2100                                            Collections.sort(list);
2101    
2102                                            list = Collections.unmodifiableList(list);
2103                                    }
2104                                    else {
2105                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
2106                                                            end);
2107                                    }
2108    
2109                                    cacheResult(list);
2110    
2111                                    finderCache.putResult(finderPath, finderArgs, list);
2112                            }
2113                            catch (Exception e) {
2114                                    finderCache.removeResult(finderPath, finderArgs);
2115    
2116                                    throw processException(e);
2117                            }
2118                            finally {
2119                                    closeSession(session);
2120                            }
2121                    }
2122    
2123                    return list;
2124            }
2125    
2126            /**
2127             * Returns the first user in the ordered set where emailAddress = &#63;.
2128             *
2129             * @param emailAddress the email address
2130             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2131             * @return the first matching user
2132             * @throws NoSuchUserException if a matching user could not be found
2133             */
2134            @Override
2135            public User findByEmailAddress_First(String emailAddress,
2136                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
2137                    User user = fetchByEmailAddress_First(emailAddress, orderByComparator);
2138    
2139                    if (user != null) {
2140                            return user;
2141                    }
2142    
2143                    StringBundler msg = new StringBundler(4);
2144    
2145                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2146    
2147                    msg.append("emailAddress=");
2148                    msg.append(emailAddress);
2149    
2150                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2151    
2152                    throw new NoSuchUserException(msg.toString());
2153            }
2154    
2155            /**
2156             * Returns the first user in the ordered set where emailAddress = &#63;.
2157             *
2158             * @param emailAddress the email address
2159             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2160             * @return the first matching user, or <code>null</code> if a matching user could not be found
2161             */
2162            @Override
2163            public User fetchByEmailAddress_First(String emailAddress,
2164                    OrderByComparator<User> orderByComparator) {
2165                    List<User> list = findByEmailAddress(emailAddress, 0, 1,
2166                                    orderByComparator);
2167    
2168                    if (!list.isEmpty()) {
2169                            return list.get(0);
2170                    }
2171    
2172                    return null;
2173            }
2174    
2175            /**
2176             * Returns the last user in the ordered set where emailAddress = &#63;.
2177             *
2178             * @param emailAddress the email address
2179             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2180             * @return the last matching user
2181             * @throws NoSuchUserException if a matching user could not be found
2182             */
2183            @Override
2184            public User findByEmailAddress_Last(String emailAddress,
2185                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
2186                    User user = fetchByEmailAddress_Last(emailAddress, orderByComparator);
2187    
2188                    if (user != null) {
2189                            return user;
2190                    }
2191    
2192                    StringBundler msg = new StringBundler(4);
2193    
2194                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2195    
2196                    msg.append("emailAddress=");
2197                    msg.append(emailAddress);
2198    
2199                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2200    
2201                    throw new NoSuchUserException(msg.toString());
2202            }
2203    
2204            /**
2205             * Returns the last user in the ordered set where emailAddress = &#63;.
2206             *
2207             * @param emailAddress the email address
2208             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2209             * @return the last matching user, or <code>null</code> if a matching user could not be found
2210             */
2211            @Override
2212            public User fetchByEmailAddress_Last(String emailAddress,
2213                    OrderByComparator<User> orderByComparator) {
2214                    int count = countByEmailAddress(emailAddress);
2215    
2216                    if (count == 0) {
2217                            return null;
2218                    }
2219    
2220                    List<User> list = findByEmailAddress(emailAddress, count - 1, count,
2221                                    orderByComparator);
2222    
2223                    if (!list.isEmpty()) {
2224                            return list.get(0);
2225                    }
2226    
2227                    return null;
2228            }
2229    
2230            /**
2231             * Returns the users before and after the current user in the ordered set where emailAddress = &#63;.
2232             *
2233             * @param userId the primary key of the current user
2234             * @param emailAddress the email address
2235             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2236             * @return the previous, current, and next user
2237             * @throws NoSuchUserException if a user with the primary key could not be found
2238             */
2239            @Override
2240            public User[] findByEmailAddress_PrevAndNext(long userId,
2241                    String emailAddress, OrderByComparator<User> orderByComparator)
2242                    throws NoSuchUserException {
2243                    User user = findByPrimaryKey(userId);
2244    
2245                    Session session = null;
2246    
2247                    try {
2248                            session = openSession();
2249    
2250                            User[] array = new UserImpl[3];
2251    
2252                            array[0] = getByEmailAddress_PrevAndNext(session, user,
2253                                            emailAddress, orderByComparator, true);
2254    
2255                            array[1] = user;
2256    
2257                            array[2] = getByEmailAddress_PrevAndNext(session, user,
2258                                            emailAddress, orderByComparator, false);
2259    
2260                            return array;
2261                    }
2262                    catch (Exception e) {
2263                            throw processException(e);
2264                    }
2265                    finally {
2266                            closeSession(session);
2267                    }
2268            }
2269    
2270            protected User getByEmailAddress_PrevAndNext(Session session, User user,
2271                    String emailAddress, OrderByComparator<User> orderByComparator,
2272                    boolean previous) {
2273                    StringBundler query = null;
2274    
2275                    if (orderByComparator != null) {
2276                            query = new StringBundler(4 +
2277                                            (orderByComparator.getOrderByConditionFields().length * 3) +
2278                                            (orderByComparator.getOrderByFields().length * 3));
2279                    }
2280                    else {
2281                            query = new StringBundler(3);
2282                    }
2283    
2284                    query.append(_SQL_SELECT_USER_WHERE);
2285    
2286                    boolean bindEmailAddress = false;
2287    
2288                    if (emailAddress == null) {
2289                            query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_1);
2290                    }
2291                    else if (emailAddress.equals(StringPool.BLANK)) {
2292                            query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_3);
2293                    }
2294                    else {
2295                            bindEmailAddress = true;
2296    
2297                            query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_2);
2298                    }
2299    
2300                    if (orderByComparator != null) {
2301                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2302    
2303                            if (orderByConditionFields.length > 0) {
2304                                    query.append(WHERE_AND);
2305                            }
2306    
2307                            for (int i = 0; i < orderByConditionFields.length; i++) {
2308                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2309                                    query.append(orderByConditionFields[i]);
2310    
2311                                    if ((i + 1) < orderByConditionFields.length) {
2312                                            if (orderByComparator.isAscending() ^ previous) {
2313                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2314                                            }
2315                                            else {
2316                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2317                                            }
2318                                    }
2319                                    else {
2320                                            if (orderByComparator.isAscending() ^ previous) {
2321                                                    query.append(WHERE_GREATER_THAN);
2322                                            }
2323                                            else {
2324                                                    query.append(WHERE_LESSER_THAN);
2325                                            }
2326                                    }
2327                            }
2328    
2329                            query.append(ORDER_BY_CLAUSE);
2330    
2331                            String[] orderByFields = orderByComparator.getOrderByFields();
2332    
2333                            for (int i = 0; i < orderByFields.length; i++) {
2334                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2335                                    query.append(orderByFields[i]);
2336    
2337                                    if ((i + 1) < orderByFields.length) {
2338                                            if (orderByComparator.isAscending() ^ previous) {
2339                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2340                                            }
2341                                            else {
2342                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2343                                            }
2344                                    }
2345                                    else {
2346                                            if (orderByComparator.isAscending() ^ previous) {
2347                                                    query.append(ORDER_BY_ASC);
2348                                            }
2349                                            else {
2350                                                    query.append(ORDER_BY_DESC);
2351                                            }
2352                                    }
2353                            }
2354                    }
2355                    else {
2356                            query.append(UserModelImpl.ORDER_BY_JPQL);
2357                    }
2358    
2359                    String sql = query.toString();
2360    
2361                    Query q = session.createQuery(sql);
2362    
2363                    q.setFirstResult(0);
2364                    q.setMaxResults(2);
2365    
2366                    QueryPos qPos = QueryPos.getInstance(q);
2367    
2368                    if (bindEmailAddress) {
2369                            qPos.add(emailAddress);
2370                    }
2371    
2372                    if (orderByComparator != null) {
2373                            Object[] values = orderByComparator.getOrderByConditionValues(user);
2374    
2375                            for (Object value : values) {
2376                                    qPos.add(value);
2377                            }
2378                    }
2379    
2380                    List<User> list = q.list();
2381    
2382                    if (list.size() == 2) {
2383                            return list.get(1);
2384                    }
2385                    else {
2386                            return null;
2387                    }
2388            }
2389    
2390            /**
2391             * Removes all the users where emailAddress = &#63; from the database.
2392             *
2393             * @param emailAddress the email address
2394             */
2395            @Override
2396            public void removeByEmailAddress(String emailAddress) {
2397                    for (User user : findByEmailAddress(emailAddress, QueryUtil.ALL_POS,
2398                                    QueryUtil.ALL_POS, null)) {
2399                            remove(user);
2400                    }
2401            }
2402    
2403            /**
2404             * Returns the number of users where emailAddress = &#63;.
2405             *
2406             * @param emailAddress the email address
2407             * @return the number of matching users
2408             */
2409            @Override
2410            public int countByEmailAddress(String emailAddress) {
2411                    FinderPath finderPath = FINDER_PATH_COUNT_BY_EMAILADDRESS;
2412    
2413                    Object[] finderArgs = new Object[] { emailAddress };
2414    
2415                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
2416    
2417                    if (count == null) {
2418                            StringBundler query = new StringBundler(2);
2419    
2420                            query.append(_SQL_COUNT_USER_WHERE);
2421    
2422                            boolean bindEmailAddress = false;
2423    
2424                            if (emailAddress == null) {
2425                                    query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_1);
2426                            }
2427                            else if (emailAddress.equals(StringPool.BLANK)) {
2428                                    query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_3);
2429                            }
2430                            else {
2431                                    bindEmailAddress = true;
2432    
2433                                    query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_2);
2434                            }
2435    
2436                            String sql = query.toString();
2437    
2438                            Session session = null;
2439    
2440                            try {
2441                                    session = openSession();
2442    
2443                                    Query q = session.createQuery(sql);
2444    
2445                                    QueryPos qPos = QueryPos.getInstance(q);
2446    
2447                                    if (bindEmailAddress) {
2448                                            qPos.add(emailAddress);
2449                                    }
2450    
2451                                    count = (Long)q.uniqueResult();
2452    
2453                                    finderCache.putResult(finderPath, finderArgs, count);
2454                            }
2455                            catch (Exception e) {
2456                                    finderCache.removeResult(finderPath, finderArgs);
2457    
2458                                    throw processException(e);
2459                            }
2460                            finally {
2461                                    closeSession(session);
2462                            }
2463                    }
2464    
2465                    return count.intValue();
2466            }
2467    
2468            private static final String _FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_1 = "user.emailAddress IS NULL";
2469            private static final String _FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_2 = "user.emailAddress = ?";
2470            private static final String _FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_3 = "(user.emailAddress IS NULL OR user.emailAddress = '')";
2471            public static final FinderPath FINDER_PATH_FETCH_BY_PORTRAITID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2472                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
2473                            FINDER_CLASS_NAME_ENTITY, "fetchByPortraitId",
2474                            new String[] { Long.class.getName() },
2475                            UserModelImpl.PORTRAITID_COLUMN_BITMASK);
2476            public static final FinderPath FINDER_PATH_COUNT_BY_PORTRAITID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2477                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
2478                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByPortraitId",
2479                            new String[] { Long.class.getName() });
2480    
2481            /**
2482             * Returns the user where portraitId = &#63; or throws a {@link NoSuchUserException} if it could not be found.
2483             *
2484             * @param portraitId the portrait ID
2485             * @return the matching user
2486             * @throws NoSuchUserException if a matching user could not be found
2487             */
2488            @Override
2489            public User findByPortraitId(long portraitId) throws NoSuchUserException {
2490                    User user = fetchByPortraitId(portraitId);
2491    
2492                    if (user == null) {
2493                            StringBundler msg = new StringBundler(4);
2494    
2495                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2496    
2497                            msg.append("portraitId=");
2498                            msg.append(portraitId);
2499    
2500                            msg.append(StringPool.CLOSE_CURLY_BRACE);
2501    
2502                            if (_log.isWarnEnabled()) {
2503                                    _log.warn(msg.toString());
2504                            }
2505    
2506                            throw new NoSuchUserException(msg.toString());
2507                    }
2508    
2509                    return user;
2510            }
2511    
2512            /**
2513             * Returns the user where portraitId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
2514             *
2515             * @param portraitId the portrait ID
2516             * @return the matching user, or <code>null</code> if a matching user could not be found
2517             */
2518            @Override
2519            public User fetchByPortraitId(long portraitId) {
2520                    return fetchByPortraitId(portraitId, true);
2521            }
2522    
2523            /**
2524             * Returns the user where portraitId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
2525             *
2526             * @param portraitId the portrait ID
2527             * @param retrieveFromCache whether to retrieve from the finder cache
2528             * @return the matching user, or <code>null</code> if a matching user could not be found
2529             */
2530            @Override
2531            public User fetchByPortraitId(long portraitId, boolean retrieveFromCache) {
2532                    Object[] finderArgs = new Object[] { portraitId };
2533    
2534                    Object result = null;
2535    
2536                    if (retrieveFromCache) {
2537                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_PORTRAITID,
2538                                            finderArgs, this);
2539                    }
2540    
2541                    if (result instanceof User) {
2542                            User user = (User)result;
2543    
2544                            if ((portraitId != user.getPortraitId())) {
2545                                    result = null;
2546                            }
2547                    }
2548    
2549                    if (result == null) {
2550                            StringBundler query = new StringBundler(3);
2551    
2552                            query.append(_SQL_SELECT_USER_WHERE);
2553    
2554                            query.append(_FINDER_COLUMN_PORTRAITID_PORTRAITID_2);
2555    
2556                            String sql = query.toString();
2557    
2558                            Session session = null;
2559    
2560                            try {
2561                                    session = openSession();
2562    
2563                                    Query q = session.createQuery(sql);
2564    
2565                                    QueryPos qPos = QueryPos.getInstance(q);
2566    
2567                                    qPos.add(portraitId);
2568    
2569                                    List<User> list = q.list();
2570    
2571                                    if (list.isEmpty()) {
2572                                            finderCache.putResult(FINDER_PATH_FETCH_BY_PORTRAITID,
2573                                                    finderArgs, list);
2574                                    }
2575                                    else {
2576                                            if ((list.size() > 1) && _log.isWarnEnabled()) {
2577                                                    _log.warn(
2578                                                            "UserPersistenceImpl.fetchByPortraitId(long, boolean) with parameters (" +
2579                                                            StringUtil.merge(finderArgs) +
2580                                                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
2581                                            }
2582    
2583                                            User user = list.get(0);
2584    
2585                                            result = user;
2586    
2587                                            cacheResult(user);
2588    
2589                                            if ((user.getPortraitId() != portraitId)) {
2590                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_PORTRAITID,
2591                                                            finderArgs, user);
2592                                            }
2593                                    }
2594                            }
2595                            catch (Exception e) {
2596                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_PORTRAITID,
2597                                            finderArgs);
2598    
2599                                    throw processException(e);
2600                            }
2601                            finally {
2602                                    closeSession(session);
2603                            }
2604                    }
2605    
2606                    if (result instanceof List<?>) {
2607                            return null;
2608                    }
2609                    else {
2610                            return (User)result;
2611                    }
2612            }
2613    
2614            /**
2615             * Removes the user where portraitId = &#63; from the database.
2616             *
2617             * @param portraitId the portrait ID
2618             * @return the user that was removed
2619             */
2620            @Override
2621            public User removeByPortraitId(long portraitId) throws NoSuchUserException {
2622                    User user = findByPortraitId(portraitId);
2623    
2624                    return remove(user);
2625            }
2626    
2627            /**
2628             * Returns the number of users where portraitId = &#63;.
2629             *
2630             * @param portraitId the portrait ID
2631             * @return the number of matching users
2632             */
2633            @Override
2634            public int countByPortraitId(long portraitId) {
2635                    FinderPath finderPath = FINDER_PATH_COUNT_BY_PORTRAITID;
2636    
2637                    Object[] finderArgs = new Object[] { portraitId };
2638    
2639                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
2640    
2641                    if (count == null) {
2642                            StringBundler query = new StringBundler(2);
2643    
2644                            query.append(_SQL_COUNT_USER_WHERE);
2645    
2646                            query.append(_FINDER_COLUMN_PORTRAITID_PORTRAITID_2);
2647    
2648                            String sql = query.toString();
2649    
2650                            Session session = null;
2651    
2652                            try {
2653                                    session = openSession();
2654    
2655                                    Query q = session.createQuery(sql);
2656    
2657                                    QueryPos qPos = QueryPos.getInstance(q);
2658    
2659                                    qPos.add(portraitId);
2660    
2661                                    count = (Long)q.uniqueResult();
2662    
2663                                    finderCache.putResult(finderPath, finderArgs, count);
2664                            }
2665                            catch (Exception e) {
2666                                    finderCache.removeResult(finderPath, finderArgs);
2667    
2668                                    throw processException(e);
2669                            }
2670                            finally {
2671                                    closeSession(session);
2672                            }
2673                    }
2674    
2675                    return count.intValue();
2676            }
2677    
2678            private static final String _FINDER_COLUMN_PORTRAITID_PORTRAITID_2 = "user.portraitId = ?";
2679            public static final FinderPath FINDER_PATH_FETCH_BY_C_U = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2680                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
2681                            FINDER_CLASS_NAME_ENTITY, "fetchByC_U",
2682                            new String[] { Long.class.getName(), Long.class.getName() },
2683                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
2684                            UserModelImpl.USERID_COLUMN_BITMASK);
2685            public static final FinderPath FINDER_PATH_COUNT_BY_C_U = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2686                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
2687                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_U",
2688                            new String[] { Long.class.getName(), Long.class.getName() });
2689    
2690            /**
2691             * Returns the user where companyId = &#63; and userId = &#63; or throws a {@link NoSuchUserException} if it could not be found.
2692             *
2693             * @param companyId the company ID
2694             * @param userId the user ID
2695             * @return the matching user
2696             * @throws NoSuchUserException if a matching user could not be found
2697             */
2698            @Override
2699            public User findByC_U(long companyId, long userId)
2700                    throws NoSuchUserException {
2701                    User user = fetchByC_U(companyId, userId);
2702    
2703                    if (user == null) {
2704                            StringBundler msg = new StringBundler(6);
2705    
2706                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2707    
2708                            msg.append("companyId=");
2709                            msg.append(companyId);
2710    
2711                            msg.append(", userId=");
2712                            msg.append(userId);
2713    
2714                            msg.append(StringPool.CLOSE_CURLY_BRACE);
2715    
2716                            if (_log.isWarnEnabled()) {
2717                                    _log.warn(msg.toString());
2718                            }
2719    
2720                            throw new NoSuchUserException(msg.toString());
2721                    }
2722    
2723                    return user;
2724            }
2725    
2726            /**
2727             * Returns the user where companyId = &#63; and userId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
2728             *
2729             * @param companyId the company ID
2730             * @param userId the user ID
2731             * @return the matching user, or <code>null</code> if a matching user could not be found
2732             */
2733            @Override
2734            public User fetchByC_U(long companyId, long userId) {
2735                    return fetchByC_U(companyId, userId, true);
2736            }
2737    
2738            /**
2739             * Returns the user where companyId = &#63; and userId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
2740             *
2741             * @param companyId the company ID
2742             * @param userId the user ID
2743             * @param retrieveFromCache whether to retrieve from the finder cache
2744             * @return the matching user, or <code>null</code> if a matching user could not be found
2745             */
2746            @Override
2747            public User fetchByC_U(long companyId, long userId,
2748                    boolean retrieveFromCache) {
2749                    Object[] finderArgs = new Object[] { companyId, userId };
2750    
2751                    Object result = null;
2752    
2753                    if (retrieveFromCache) {
2754                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_C_U,
2755                                            finderArgs, this);
2756                    }
2757    
2758                    if (result instanceof User) {
2759                            User user = (User)result;
2760    
2761                            if ((companyId != user.getCompanyId()) ||
2762                                            (userId != user.getUserId())) {
2763                                    result = null;
2764                            }
2765                    }
2766    
2767                    if (result == null) {
2768                            StringBundler query = new StringBundler(4);
2769    
2770                            query.append(_SQL_SELECT_USER_WHERE);
2771    
2772                            query.append(_FINDER_COLUMN_C_U_COMPANYID_2);
2773    
2774                            query.append(_FINDER_COLUMN_C_U_USERID_2);
2775    
2776                            String sql = query.toString();
2777    
2778                            Session session = null;
2779    
2780                            try {
2781                                    session = openSession();
2782    
2783                                    Query q = session.createQuery(sql);
2784    
2785                                    QueryPos qPos = QueryPos.getInstance(q);
2786    
2787                                    qPos.add(companyId);
2788    
2789                                    qPos.add(userId);
2790    
2791                                    List<User> list = q.list();
2792    
2793                                    if (list.isEmpty()) {
2794                                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_U, finderArgs,
2795                                                    list);
2796                                    }
2797                                    else {
2798                                            User user = list.get(0);
2799    
2800                                            result = user;
2801    
2802                                            cacheResult(user);
2803    
2804                                            if ((user.getCompanyId() != companyId) ||
2805                                                            (user.getUserId() != userId)) {
2806                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_U,
2807                                                            finderArgs, user);
2808                                            }
2809                                    }
2810                            }
2811                            catch (Exception e) {
2812                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_U, finderArgs);
2813    
2814                                    throw processException(e);
2815                            }
2816                            finally {
2817                                    closeSession(session);
2818                            }
2819                    }
2820    
2821                    if (result instanceof List<?>) {
2822                            return null;
2823                    }
2824                    else {
2825                            return (User)result;
2826                    }
2827            }
2828    
2829            /**
2830             * Removes the user where companyId = &#63; and userId = &#63; from the database.
2831             *
2832             * @param companyId the company ID
2833             * @param userId the user ID
2834             * @return the user that was removed
2835             */
2836            @Override
2837            public User removeByC_U(long companyId, long userId)
2838                    throws NoSuchUserException {
2839                    User user = findByC_U(companyId, userId);
2840    
2841                    return remove(user);
2842            }
2843    
2844            /**
2845             * Returns the number of users where companyId = &#63; and userId = &#63;.
2846             *
2847             * @param companyId the company ID
2848             * @param userId the user ID
2849             * @return the number of matching users
2850             */
2851            @Override
2852            public int countByC_U(long companyId, long userId) {
2853                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_U;
2854    
2855                    Object[] finderArgs = new Object[] { companyId, userId };
2856    
2857                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
2858    
2859                    if (count == null) {
2860                            StringBundler query = new StringBundler(3);
2861    
2862                            query.append(_SQL_COUNT_USER_WHERE);
2863    
2864                            query.append(_FINDER_COLUMN_C_U_COMPANYID_2);
2865    
2866                            query.append(_FINDER_COLUMN_C_U_USERID_2);
2867    
2868                            String sql = query.toString();
2869    
2870                            Session session = null;
2871    
2872                            try {
2873                                    session = openSession();
2874    
2875                                    Query q = session.createQuery(sql);
2876    
2877                                    QueryPos qPos = QueryPos.getInstance(q);
2878    
2879                                    qPos.add(companyId);
2880    
2881                                    qPos.add(userId);
2882    
2883                                    count = (Long)q.uniqueResult();
2884    
2885                                    finderCache.putResult(finderPath, finderArgs, count);
2886                            }
2887                            catch (Exception e) {
2888                                    finderCache.removeResult(finderPath, finderArgs);
2889    
2890                                    throw processException(e);
2891                            }
2892                            finally {
2893                                    closeSession(session);
2894                            }
2895                    }
2896    
2897                    return count.intValue();
2898            }
2899    
2900            private static final String _FINDER_COLUMN_C_U_COMPANYID_2 = "user.companyId = ? AND ";
2901            private static final String _FINDER_COLUMN_C_U_USERID_2 = "user.userId = ?";
2902            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_CD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2903                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
2904                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_CD",
2905                            new String[] {
2906                                    Long.class.getName(), Date.class.getName(),
2907                                    
2908                            Integer.class.getName(), Integer.class.getName(),
2909                                    OrderByComparator.class.getName()
2910                            });
2911            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2912                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
2913                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_CD",
2914                            new String[] { Long.class.getName(), Date.class.getName() },
2915                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
2916                            UserModelImpl.CREATEDATE_COLUMN_BITMASK);
2917            public static final FinderPath FINDER_PATH_COUNT_BY_C_CD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2918                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
2919                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_CD",
2920                            new String[] { Long.class.getName(), Date.class.getName() });
2921    
2922            /**
2923             * Returns all the users where companyId = &#63; and createDate = &#63;.
2924             *
2925             * @param companyId the company ID
2926             * @param createDate the create date
2927             * @return the matching users
2928             */
2929            @Override
2930            public List<User> findByC_CD(long companyId, Date createDate) {
2931                    return findByC_CD(companyId, createDate, QueryUtil.ALL_POS,
2932                            QueryUtil.ALL_POS, null);
2933            }
2934    
2935            /**
2936             * Returns a range of all the users where companyId = &#63; and createDate = &#63;.
2937             *
2938             * <p>
2939             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
2940             * </p>
2941             *
2942             * @param companyId the company ID
2943             * @param createDate the create date
2944             * @param start the lower bound of the range of users
2945             * @param end the upper bound of the range of users (not inclusive)
2946             * @return the range of matching users
2947             */
2948            @Override
2949            public List<User> findByC_CD(long companyId, Date createDate, int start,
2950                    int end) {
2951                    return findByC_CD(companyId, createDate, start, end, null);
2952            }
2953    
2954            /**
2955             * Returns an ordered range of all the users where companyId = &#63; and createDate = &#63;.
2956             *
2957             * <p>
2958             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
2959             * </p>
2960             *
2961             * @param companyId the company ID
2962             * @param createDate the create date
2963             * @param start the lower bound of the range of users
2964             * @param end the upper bound of the range of users (not inclusive)
2965             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2966             * @return the ordered range of matching users
2967             */
2968            @Override
2969            public List<User> findByC_CD(long companyId, Date createDate, int start,
2970                    int end, OrderByComparator<User> orderByComparator) {
2971                    return findByC_CD(companyId, createDate, start, end, orderByComparator,
2972                            true);
2973            }
2974    
2975            /**
2976             * Returns an ordered range of all the users where companyId = &#63; and createDate = &#63;.
2977             *
2978             * <p>
2979             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
2980             * </p>
2981             *
2982             * @param companyId the company ID
2983             * @param createDate the create date
2984             * @param start the lower bound of the range of users
2985             * @param end the upper bound of the range of users (not inclusive)
2986             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2987             * @param retrieveFromCache whether to retrieve from the finder cache
2988             * @return the ordered range of matching users
2989             */
2990            @Override
2991            public List<User> findByC_CD(long companyId, Date createDate, int start,
2992                    int end, OrderByComparator<User> orderByComparator,
2993                    boolean retrieveFromCache) {
2994                    boolean pagination = true;
2995                    FinderPath finderPath = null;
2996                    Object[] finderArgs = null;
2997    
2998                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2999                                    (orderByComparator == null)) {
3000                            pagination = false;
3001                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD;
3002                            finderArgs = new Object[] { companyId, createDate };
3003                    }
3004                    else {
3005                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_CD;
3006                            finderArgs = new Object[] {
3007                                            companyId, createDate,
3008                                            
3009                                            start, end, orderByComparator
3010                                    };
3011                    }
3012    
3013                    List<User> list = null;
3014    
3015                    if (retrieveFromCache) {
3016                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
3017                                            this);
3018    
3019                            if ((list != null) && !list.isEmpty()) {
3020                                    for (User user : list) {
3021                                            if ((companyId != user.getCompanyId()) ||
3022                                                            !Validator.equals(createDate, user.getCreateDate())) {
3023                                                    list = null;
3024    
3025                                                    break;
3026                                            }
3027                                    }
3028                            }
3029                    }
3030    
3031                    if (list == null) {
3032                            StringBundler query = null;
3033    
3034                            if (orderByComparator != null) {
3035                                    query = new StringBundler(4 +
3036                                                    (orderByComparator.getOrderByFields().length * 2));
3037                            }
3038                            else {
3039                                    query = new StringBundler(4);
3040                            }
3041    
3042                            query.append(_SQL_SELECT_USER_WHERE);
3043    
3044                            query.append(_FINDER_COLUMN_C_CD_COMPANYID_2);
3045    
3046                            boolean bindCreateDate = false;
3047    
3048                            if (createDate == null) {
3049                                    query.append(_FINDER_COLUMN_C_CD_CREATEDATE_1);
3050                            }
3051                            else {
3052                                    bindCreateDate = true;
3053    
3054                                    query.append(_FINDER_COLUMN_C_CD_CREATEDATE_2);
3055                            }
3056    
3057                            if (orderByComparator != null) {
3058                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3059                                            orderByComparator);
3060                            }
3061                            else
3062                             if (pagination) {
3063                                    query.append(UserModelImpl.ORDER_BY_JPQL);
3064                            }
3065    
3066                            String sql = query.toString();
3067    
3068                            Session session = null;
3069    
3070                            try {
3071                                    session = openSession();
3072    
3073                                    Query q = session.createQuery(sql);
3074    
3075                                    QueryPos qPos = QueryPos.getInstance(q);
3076    
3077                                    qPos.add(companyId);
3078    
3079                                    if (bindCreateDate) {
3080                                            qPos.add(new Timestamp(createDate.getTime()));
3081                                    }
3082    
3083                                    if (!pagination) {
3084                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
3085                                                            end, false);
3086    
3087                                            Collections.sort(list);
3088    
3089                                            list = Collections.unmodifiableList(list);
3090                                    }
3091                                    else {
3092                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
3093                                                            end);
3094                                    }
3095    
3096                                    cacheResult(list);
3097    
3098                                    finderCache.putResult(finderPath, finderArgs, list);
3099                            }
3100                            catch (Exception e) {
3101                                    finderCache.removeResult(finderPath, finderArgs);
3102    
3103                                    throw processException(e);
3104                            }
3105                            finally {
3106                                    closeSession(session);
3107                            }
3108                    }
3109    
3110                    return list;
3111            }
3112    
3113            /**
3114             * Returns the first user in the ordered set where companyId = &#63; and createDate = &#63;.
3115             *
3116             * @param companyId the company ID
3117             * @param createDate the create date
3118             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3119             * @return the first matching user
3120             * @throws NoSuchUserException if a matching user could not be found
3121             */
3122            @Override
3123            public User findByC_CD_First(long companyId, Date createDate,
3124                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
3125                    User user = fetchByC_CD_First(companyId, createDate, orderByComparator);
3126    
3127                    if (user != null) {
3128                            return user;
3129                    }
3130    
3131                    StringBundler msg = new StringBundler(6);
3132    
3133                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3134    
3135                    msg.append("companyId=");
3136                    msg.append(companyId);
3137    
3138                    msg.append(", createDate=");
3139                    msg.append(createDate);
3140    
3141                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3142    
3143                    throw new NoSuchUserException(msg.toString());
3144            }
3145    
3146            /**
3147             * Returns the first user in the ordered set where companyId = &#63; and createDate = &#63;.
3148             *
3149             * @param companyId the company ID
3150             * @param createDate the create date
3151             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3152             * @return the first matching user, or <code>null</code> if a matching user could not be found
3153             */
3154            @Override
3155            public User fetchByC_CD_First(long companyId, Date createDate,
3156                    OrderByComparator<User> orderByComparator) {
3157                    List<User> list = findByC_CD(companyId, createDate, 0, 1,
3158                                    orderByComparator);
3159    
3160                    if (!list.isEmpty()) {
3161                            return list.get(0);
3162                    }
3163    
3164                    return null;
3165            }
3166    
3167            /**
3168             * Returns the last user in the ordered set where companyId = &#63; and createDate = &#63;.
3169             *
3170             * @param companyId the company ID
3171             * @param createDate the create date
3172             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3173             * @return the last matching user
3174             * @throws NoSuchUserException if a matching user could not be found
3175             */
3176            @Override
3177            public User findByC_CD_Last(long companyId, Date createDate,
3178                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
3179                    User user = fetchByC_CD_Last(companyId, createDate, orderByComparator);
3180    
3181                    if (user != null) {
3182                            return user;
3183                    }
3184    
3185                    StringBundler msg = new StringBundler(6);
3186    
3187                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3188    
3189                    msg.append("companyId=");
3190                    msg.append(companyId);
3191    
3192                    msg.append(", createDate=");
3193                    msg.append(createDate);
3194    
3195                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3196    
3197                    throw new NoSuchUserException(msg.toString());
3198            }
3199    
3200            /**
3201             * Returns the last user in the ordered set where companyId = &#63; and createDate = &#63;.
3202             *
3203             * @param companyId the company ID
3204             * @param createDate the create date
3205             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3206             * @return the last matching user, or <code>null</code> if a matching user could not be found
3207             */
3208            @Override
3209            public User fetchByC_CD_Last(long companyId, Date createDate,
3210                    OrderByComparator<User> orderByComparator) {
3211                    int count = countByC_CD(companyId, createDate);
3212    
3213                    if (count == 0) {
3214                            return null;
3215                    }
3216    
3217                    List<User> list = findByC_CD(companyId, createDate, count - 1, count,
3218                                    orderByComparator);
3219    
3220                    if (!list.isEmpty()) {
3221                            return list.get(0);
3222                    }
3223    
3224                    return null;
3225            }
3226    
3227            /**
3228             * Returns the users before and after the current user in the ordered set where companyId = &#63; and createDate = &#63;.
3229             *
3230             * @param userId the primary key of the current user
3231             * @param companyId the company ID
3232             * @param createDate the create date
3233             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3234             * @return the previous, current, and next user
3235             * @throws NoSuchUserException if a user with the primary key could not be found
3236             */
3237            @Override
3238            public User[] findByC_CD_PrevAndNext(long userId, long companyId,
3239                    Date createDate, OrderByComparator<User> orderByComparator)
3240                    throws NoSuchUserException {
3241                    User user = findByPrimaryKey(userId);
3242    
3243                    Session session = null;
3244    
3245                    try {
3246                            session = openSession();
3247    
3248                            User[] array = new UserImpl[3];
3249    
3250                            array[0] = getByC_CD_PrevAndNext(session, user, companyId,
3251                                            createDate, orderByComparator, true);
3252    
3253                            array[1] = user;
3254    
3255                            array[2] = getByC_CD_PrevAndNext(session, user, companyId,
3256                                            createDate, orderByComparator, false);
3257    
3258                            return array;
3259                    }
3260                    catch (Exception e) {
3261                            throw processException(e);
3262                    }
3263                    finally {
3264                            closeSession(session);
3265                    }
3266            }
3267    
3268            protected User getByC_CD_PrevAndNext(Session session, User user,
3269                    long companyId, Date createDate,
3270                    OrderByComparator<User> orderByComparator, boolean previous) {
3271                    StringBundler query = null;
3272    
3273                    if (orderByComparator != null) {
3274                            query = new StringBundler(5 +
3275                                            (orderByComparator.getOrderByConditionFields().length * 3) +
3276                                            (orderByComparator.getOrderByFields().length * 3));
3277                    }
3278                    else {
3279                            query = new StringBundler(4);
3280                    }
3281    
3282                    query.append(_SQL_SELECT_USER_WHERE);
3283    
3284                    query.append(_FINDER_COLUMN_C_CD_COMPANYID_2);
3285    
3286                    boolean bindCreateDate = false;
3287    
3288                    if (createDate == null) {
3289                            query.append(_FINDER_COLUMN_C_CD_CREATEDATE_1);
3290                    }
3291                    else {
3292                            bindCreateDate = true;
3293    
3294                            query.append(_FINDER_COLUMN_C_CD_CREATEDATE_2);
3295                    }
3296    
3297                    if (orderByComparator != null) {
3298                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
3299    
3300                            if (orderByConditionFields.length > 0) {
3301                                    query.append(WHERE_AND);
3302                            }
3303    
3304                            for (int i = 0; i < orderByConditionFields.length; i++) {
3305                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3306                                    query.append(orderByConditionFields[i]);
3307    
3308                                    if ((i + 1) < orderByConditionFields.length) {
3309                                            if (orderByComparator.isAscending() ^ previous) {
3310                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
3311                                            }
3312                                            else {
3313                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
3314                                            }
3315                                    }
3316                                    else {
3317                                            if (orderByComparator.isAscending() ^ previous) {
3318                                                    query.append(WHERE_GREATER_THAN);
3319                                            }
3320                                            else {
3321                                                    query.append(WHERE_LESSER_THAN);
3322                                            }
3323                                    }
3324                            }
3325    
3326                            query.append(ORDER_BY_CLAUSE);
3327    
3328                            String[] orderByFields = orderByComparator.getOrderByFields();
3329    
3330                            for (int i = 0; i < orderByFields.length; i++) {
3331                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3332                                    query.append(orderByFields[i]);
3333    
3334                                    if ((i + 1) < orderByFields.length) {
3335                                            if (orderByComparator.isAscending() ^ previous) {
3336                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
3337                                            }
3338                                            else {
3339                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
3340                                            }
3341                                    }
3342                                    else {
3343                                            if (orderByComparator.isAscending() ^ previous) {
3344                                                    query.append(ORDER_BY_ASC);
3345                                            }
3346                                            else {
3347                                                    query.append(ORDER_BY_DESC);
3348                                            }
3349                                    }
3350                            }
3351                    }
3352                    else {
3353                            query.append(UserModelImpl.ORDER_BY_JPQL);
3354                    }
3355    
3356                    String sql = query.toString();
3357    
3358                    Query q = session.createQuery(sql);
3359    
3360                    q.setFirstResult(0);
3361                    q.setMaxResults(2);
3362    
3363                    QueryPos qPos = QueryPos.getInstance(q);
3364    
3365                    qPos.add(companyId);
3366    
3367                    if (bindCreateDate) {
3368                            qPos.add(new Timestamp(createDate.getTime()));
3369                    }
3370    
3371                    if (orderByComparator != null) {
3372                            Object[] values = orderByComparator.getOrderByConditionValues(user);
3373    
3374                            for (Object value : values) {
3375                                    qPos.add(value);
3376                            }
3377                    }
3378    
3379                    List<User> list = q.list();
3380    
3381                    if (list.size() == 2) {
3382                            return list.get(1);
3383                    }
3384                    else {
3385                            return null;
3386                    }
3387            }
3388    
3389            /**
3390             * Removes all the users where companyId = &#63; and createDate = &#63; from the database.
3391             *
3392             * @param companyId the company ID
3393             * @param createDate the create date
3394             */
3395            @Override
3396            public void removeByC_CD(long companyId, Date createDate) {
3397                    for (User user : findByC_CD(companyId, createDate, QueryUtil.ALL_POS,
3398                                    QueryUtil.ALL_POS, null)) {
3399                            remove(user);
3400                    }
3401            }
3402    
3403            /**
3404             * Returns the number of users where companyId = &#63; and createDate = &#63;.
3405             *
3406             * @param companyId the company ID
3407             * @param createDate the create date
3408             * @return the number of matching users
3409             */
3410            @Override
3411            public int countByC_CD(long companyId, Date createDate) {
3412                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_CD;
3413    
3414                    Object[] finderArgs = new Object[] { companyId, createDate };
3415    
3416                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
3417    
3418                    if (count == null) {
3419                            StringBundler query = new StringBundler(3);
3420    
3421                            query.append(_SQL_COUNT_USER_WHERE);
3422    
3423                            query.append(_FINDER_COLUMN_C_CD_COMPANYID_2);
3424    
3425                            boolean bindCreateDate = false;
3426    
3427                            if (createDate == null) {
3428                                    query.append(_FINDER_COLUMN_C_CD_CREATEDATE_1);
3429                            }
3430                            else {
3431                                    bindCreateDate = true;
3432    
3433                                    query.append(_FINDER_COLUMN_C_CD_CREATEDATE_2);
3434                            }
3435    
3436                            String sql = query.toString();
3437    
3438                            Session session = null;
3439    
3440                            try {
3441                                    session = openSession();
3442    
3443                                    Query q = session.createQuery(sql);
3444    
3445                                    QueryPos qPos = QueryPos.getInstance(q);
3446    
3447                                    qPos.add(companyId);
3448    
3449                                    if (bindCreateDate) {
3450                                            qPos.add(new Timestamp(createDate.getTime()));
3451                                    }
3452    
3453                                    count = (Long)q.uniqueResult();
3454    
3455                                    finderCache.putResult(finderPath, finderArgs, count);
3456                            }
3457                            catch (Exception e) {
3458                                    finderCache.removeResult(finderPath, finderArgs);
3459    
3460                                    throw processException(e);
3461                            }
3462                            finally {
3463                                    closeSession(session);
3464                            }
3465                    }
3466    
3467                    return count.intValue();
3468            }
3469    
3470            private static final String _FINDER_COLUMN_C_CD_COMPANYID_2 = "user.companyId = ? AND ";
3471            private static final String _FINDER_COLUMN_C_CD_CREATEDATE_1 = "user.createDate IS NULL";
3472            private static final String _FINDER_COLUMN_C_CD_CREATEDATE_2 = "user.createDate = ?";
3473            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_MD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
3474                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
3475                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_MD",
3476                            new String[] {
3477                                    Long.class.getName(), Date.class.getName(),
3478                                    
3479                            Integer.class.getName(), Integer.class.getName(),
3480                                    OrderByComparator.class.getName()
3481                            });
3482            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_MD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
3483                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
3484                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_MD",
3485                            new String[] { Long.class.getName(), Date.class.getName() },
3486                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
3487                            UserModelImpl.MODIFIEDDATE_COLUMN_BITMASK);
3488            public static final FinderPath FINDER_PATH_COUNT_BY_C_MD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
3489                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
3490                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_MD",
3491                            new String[] { Long.class.getName(), Date.class.getName() });
3492    
3493            /**
3494             * Returns all the users where companyId = &#63; and modifiedDate = &#63;.
3495             *
3496             * @param companyId the company ID
3497             * @param modifiedDate the modified date
3498             * @return the matching users
3499             */
3500            @Override
3501            public List<User> findByC_MD(long companyId, Date modifiedDate) {
3502                    return findByC_MD(companyId, modifiedDate, QueryUtil.ALL_POS,
3503                            QueryUtil.ALL_POS, null);
3504            }
3505    
3506            /**
3507             * Returns a range of all the users where companyId = &#63; and modifiedDate = &#63;.
3508             *
3509             * <p>
3510             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
3511             * </p>
3512             *
3513             * @param companyId the company ID
3514             * @param modifiedDate the modified date
3515             * @param start the lower bound of the range of users
3516             * @param end the upper bound of the range of users (not inclusive)
3517             * @return the range of matching users
3518             */
3519            @Override
3520            public List<User> findByC_MD(long companyId, Date modifiedDate, int start,
3521                    int end) {
3522                    return findByC_MD(companyId, modifiedDate, start, end, null);
3523            }
3524    
3525            /**
3526             * Returns an ordered range of all the users where companyId = &#63; and modifiedDate = &#63;.
3527             *
3528             * <p>
3529             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
3530             * </p>
3531             *
3532             * @param companyId the company ID
3533             * @param modifiedDate the modified date
3534             * @param start the lower bound of the range of users
3535             * @param end the upper bound of the range of users (not inclusive)
3536             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3537             * @return the ordered range of matching users
3538             */
3539            @Override
3540            public List<User> findByC_MD(long companyId, Date modifiedDate, int start,
3541                    int end, OrderByComparator<User> orderByComparator) {
3542                    return findByC_MD(companyId, modifiedDate, start, end,
3543                            orderByComparator, true);
3544            }
3545    
3546            /**
3547             * Returns an ordered range of all the users where companyId = &#63; and modifiedDate = &#63;.
3548             *
3549             * <p>
3550             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
3551             * </p>
3552             *
3553             * @param companyId the company ID
3554             * @param modifiedDate the modified date
3555             * @param start the lower bound of the range of users
3556             * @param end the upper bound of the range of users (not inclusive)
3557             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3558             * @param retrieveFromCache whether to retrieve from the finder cache
3559             * @return the ordered range of matching users
3560             */
3561            @Override
3562            public List<User> findByC_MD(long companyId, Date modifiedDate, int start,
3563                    int end, OrderByComparator<User> orderByComparator,
3564                    boolean retrieveFromCache) {
3565                    boolean pagination = true;
3566                    FinderPath finderPath = null;
3567                    Object[] finderArgs = null;
3568    
3569                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3570                                    (orderByComparator == null)) {
3571                            pagination = false;
3572                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_MD;
3573                            finderArgs = new Object[] { companyId, modifiedDate };
3574                    }
3575                    else {
3576                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_MD;
3577                            finderArgs = new Object[] {
3578                                            companyId, modifiedDate,
3579                                            
3580                                            start, end, orderByComparator
3581                                    };
3582                    }
3583    
3584                    List<User> list = null;
3585    
3586                    if (retrieveFromCache) {
3587                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
3588                                            this);
3589    
3590                            if ((list != null) && !list.isEmpty()) {
3591                                    for (User user : list) {
3592                                            if ((companyId != user.getCompanyId()) ||
3593                                                            !Validator.equals(modifiedDate,
3594                                                                    user.getModifiedDate())) {
3595                                                    list = null;
3596    
3597                                                    break;
3598                                            }
3599                                    }
3600                            }
3601                    }
3602    
3603                    if (list == null) {
3604                            StringBundler query = null;
3605    
3606                            if (orderByComparator != null) {
3607                                    query = new StringBundler(4 +
3608                                                    (orderByComparator.getOrderByFields().length * 2));
3609                            }
3610                            else {
3611                                    query = new StringBundler(4);
3612                            }
3613    
3614                            query.append(_SQL_SELECT_USER_WHERE);
3615    
3616                            query.append(_FINDER_COLUMN_C_MD_COMPANYID_2);
3617    
3618                            boolean bindModifiedDate = false;
3619    
3620                            if (modifiedDate == null) {
3621                                    query.append(_FINDER_COLUMN_C_MD_MODIFIEDDATE_1);
3622                            }
3623                            else {
3624                                    bindModifiedDate = true;
3625    
3626                                    query.append(_FINDER_COLUMN_C_MD_MODIFIEDDATE_2);
3627                            }
3628    
3629                            if (orderByComparator != null) {
3630                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3631                                            orderByComparator);
3632                            }
3633                            else
3634                             if (pagination) {
3635                                    query.append(UserModelImpl.ORDER_BY_JPQL);
3636                            }
3637    
3638                            String sql = query.toString();
3639    
3640                            Session session = null;
3641    
3642                            try {
3643                                    session = openSession();
3644    
3645                                    Query q = session.createQuery(sql);
3646    
3647                                    QueryPos qPos = QueryPos.getInstance(q);
3648    
3649                                    qPos.add(companyId);
3650    
3651                                    if (bindModifiedDate) {
3652                                            qPos.add(new Timestamp(modifiedDate.getTime()));
3653                                    }
3654    
3655                                    if (!pagination) {
3656                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
3657                                                            end, false);
3658    
3659                                            Collections.sort(list);
3660    
3661                                            list = Collections.unmodifiableList(list);
3662                                    }
3663                                    else {
3664                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
3665                                                            end);
3666                                    }
3667    
3668                                    cacheResult(list);
3669    
3670                                    finderCache.putResult(finderPath, finderArgs, list);
3671                            }
3672                            catch (Exception e) {
3673                                    finderCache.removeResult(finderPath, finderArgs);
3674    
3675                                    throw processException(e);
3676                            }
3677                            finally {
3678                                    closeSession(session);
3679                            }
3680                    }
3681    
3682                    return list;
3683            }
3684    
3685            /**
3686             * Returns the first user in the ordered set where companyId = &#63; and modifiedDate = &#63;.
3687             *
3688             * @param companyId the company ID
3689             * @param modifiedDate the modified date
3690             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3691             * @return the first matching user
3692             * @throws NoSuchUserException if a matching user could not be found
3693             */
3694            @Override
3695            public User findByC_MD_First(long companyId, Date modifiedDate,
3696                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
3697                    User user = fetchByC_MD_First(companyId, modifiedDate, orderByComparator);
3698    
3699                    if (user != null) {
3700                            return user;
3701                    }
3702    
3703                    StringBundler msg = new StringBundler(6);
3704    
3705                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3706    
3707                    msg.append("companyId=");
3708                    msg.append(companyId);
3709    
3710                    msg.append(", modifiedDate=");
3711                    msg.append(modifiedDate);
3712    
3713                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3714    
3715                    throw new NoSuchUserException(msg.toString());
3716            }
3717    
3718            /**
3719             * Returns the first user in the ordered set where companyId = &#63; and modifiedDate = &#63;.
3720             *
3721             * @param companyId the company ID
3722             * @param modifiedDate the modified date
3723             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3724             * @return the first matching user, or <code>null</code> if a matching user could not be found
3725             */
3726            @Override
3727            public User fetchByC_MD_First(long companyId, Date modifiedDate,
3728                    OrderByComparator<User> orderByComparator) {
3729                    List<User> list = findByC_MD(companyId, modifiedDate, 0, 1,
3730                                    orderByComparator);
3731    
3732                    if (!list.isEmpty()) {
3733                            return list.get(0);
3734                    }
3735    
3736                    return null;
3737            }
3738    
3739            /**
3740             * Returns the last user in the ordered set where companyId = &#63; and modifiedDate = &#63;.
3741             *
3742             * @param companyId the company ID
3743             * @param modifiedDate the modified date
3744             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3745             * @return the last matching user
3746             * @throws NoSuchUserException if a matching user could not be found
3747             */
3748            @Override
3749            public User findByC_MD_Last(long companyId, Date modifiedDate,
3750                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
3751                    User user = fetchByC_MD_Last(companyId, modifiedDate, orderByComparator);
3752    
3753                    if (user != null) {
3754                            return user;
3755                    }
3756    
3757                    StringBundler msg = new StringBundler(6);
3758    
3759                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3760    
3761                    msg.append("companyId=");
3762                    msg.append(companyId);
3763    
3764                    msg.append(", modifiedDate=");
3765                    msg.append(modifiedDate);
3766    
3767                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3768    
3769                    throw new NoSuchUserException(msg.toString());
3770            }
3771    
3772            /**
3773             * Returns the last user in the ordered set where companyId = &#63; and modifiedDate = &#63;.
3774             *
3775             * @param companyId the company ID
3776             * @param modifiedDate the modified date
3777             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3778             * @return the last matching user, or <code>null</code> if a matching user could not be found
3779             */
3780            @Override
3781            public User fetchByC_MD_Last(long companyId, Date modifiedDate,
3782                    OrderByComparator<User> orderByComparator) {
3783                    int count = countByC_MD(companyId, modifiedDate);
3784    
3785                    if (count == 0) {
3786                            return null;
3787                    }
3788    
3789                    List<User> list = findByC_MD(companyId, modifiedDate, count - 1, count,
3790                                    orderByComparator);
3791    
3792                    if (!list.isEmpty()) {
3793                            return list.get(0);
3794                    }
3795    
3796                    return null;
3797            }
3798    
3799            /**
3800             * Returns the users before and after the current user in the ordered set where companyId = &#63; and modifiedDate = &#63;.
3801             *
3802             * @param userId the primary key of the current user
3803             * @param companyId the company ID
3804             * @param modifiedDate the modified date
3805             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3806             * @return the previous, current, and next user
3807             * @throws NoSuchUserException if a user with the primary key could not be found
3808             */
3809            @Override
3810            public User[] findByC_MD_PrevAndNext(long userId, long companyId,
3811                    Date modifiedDate, OrderByComparator<User> orderByComparator)
3812                    throws NoSuchUserException {
3813                    User user = findByPrimaryKey(userId);
3814    
3815                    Session session = null;
3816    
3817                    try {
3818                            session = openSession();
3819    
3820                            User[] array = new UserImpl[3];
3821    
3822                            array[0] = getByC_MD_PrevAndNext(session, user, companyId,
3823                                            modifiedDate, orderByComparator, true);
3824    
3825                            array[1] = user;
3826    
3827                            array[2] = getByC_MD_PrevAndNext(session, user, companyId,
3828                                            modifiedDate, orderByComparator, false);
3829    
3830                            return array;
3831                    }
3832                    catch (Exception e) {
3833                            throw processException(e);
3834                    }
3835                    finally {
3836                            closeSession(session);
3837                    }
3838            }
3839    
3840            protected User getByC_MD_PrevAndNext(Session session, User user,
3841                    long companyId, Date modifiedDate,
3842                    OrderByComparator<User> orderByComparator, boolean previous) {
3843                    StringBundler query = null;
3844    
3845                    if (orderByComparator != null) {
3846                            query = new StringBundler(5 +
3847                                            (orderByComparator.getOrderByConditionFields().length * 3) +
3848                                            (orderByComparator.getOrderByFields().length * 3));
3849                    }
3850                    else {
3851                            query = new StringBundler(4);
3852                    }
3853    
3854                    query.append(_SQL_SELECT_USER_WHERE);
3855    
3856                    query.append(_FINDER_COLUMN_C_MD_COMPANYID_2);
3857    
3858                    boolean bindModifiedDate = false;
3859    
3860                    if (modifiedDate == null) {
3861                            query.append(_FINDER_COLUMN_C_MD_MODIFIEDDATE_1);
3862                    }
3863                    else {
3864                            bindModifiedDate = true;
3865    
3866                            query.append(_FINDER_COLUMN_C_MD_MODIFIEDDATE_2);
3867                    }
3868    
3869                    if (orderByComparator != null) {
3870                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
3871    
3872                            if (orderByConditionFields.length > 0) {
3873                                    query.append(WHERE_AND);
3874                            }
3875    
3876                            for (int i = 0; i < orderByConditionFields.length; i++) {
3877                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3878                                    query.append(orderByConditionFields[i]);
3879    
3880                                    if ((i + 1) < orderByConditionFields.length) {
3881                                            if (orderByComparator.isAscending() ^ previous) {
3882                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
3883                                            }
3884                                            else {
3885                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
3886                                            }
3887                                    }
3888                                    else {
3889                                            if (orderByComparator.isAscending() ^ previous) {
3890                                                    query.append(WHERE_GREATER_THAN);
3891                                            }
3892                                            else {
3893                                                    query.append(WHERE_LESSER_THAN);
3894                                            }
3895                                    }
3896                            }
3897    
3898                            query.append(ORDER_BY_CLAUSE);
3899    
3900                            String[] orderByFields = orderByComparator.getOrderByFields();
3901    
3902                            for (int i = 0; i < orderByFields.length; i++) {
3903                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3904                                    query.append(orderByFields[i]);
3905    
3906                                    if ((i + 1) < orderByFields.length) {
3907                                            if (orderByComparator.isAscending() ^ previous) {
3908                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
3909                                            }
3910                                            else {
3911                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
3912                                            }
3913                                    }
3914                                    else {
3915                                            if (orderByComparator.isAscending() ^ previous) {
3916                                                    query.append(ORDER_BY_ASC);
3917                                            }
3918                                            else {
3919                                                    query.append(ORDER_BY_DESC);
3920                                            }
3921                                    }
3922                            }
3923                    }
3924                    else {
3925                            query.append(UserModelImpl.ORDER_BY_JPQL);
3926                    }
3927    
3928                    String sql = query.toString();
3929    
3930                    Query q = session.createQuery(sql);
3931    
3932                    q.setFirstResult(0);
3933                    q.setMaxResults(2);
3934    
3935                    QueryPos qPos = QueryPos.getInstance(q);
3936    
3937                    qPos.add(companyId);
3938    
3939                    if (bindModifiedDate) {
3940                            qPos.add(new Timestamp(modifiedDate.getTime()));
3941                    }
3942    
3943                    if (orderByComparator != null) {
3944                            Object[] values = orderByComparator.getOrderByConditionValues(user);
3945    
3946                            for (Object value : values) {
3947                                    qPos.add(value);
3948                            }
3949                    }
3950    
3951                    List<User> list = q.list();
3952    
3953                    if (list.size() == 2) {
3954                            return list.get(1);
3955                    }
3956                    else {
3957                            return null;
3958                    }
3959            }
3960    
3961            /**
3962             * Removes all the users where companyId = &#63; and modifiedDate = &#63; from the database.
3963             *
3964             * @param companyId the company ID
3965             * @param modifiedDate the modified date
3966             */
3967            @Override
3968            public void removeByC_MD(long companyId, Date modifiedDate) {
3969                    for (User user : findByC_MD(companyId, modifiedDate, QueryUtil.ALL_POS,
3970                                    QueryUtil.ALL_POS, null)) {
3971                            remove(user);
3972                    }
3973            }
3974    
3975            /**
3976             * Returns the number of users where companyId = &#63; and modifiedDate = &#63;.
3977             *
3978             * @param companyId the company ID
3979             * @param modifiedDate the modified date
3980             * @return the number of matching users
3981             */
3982            @Override
3983            public int countByC_MD(long companyId, Date modifiedDate) {
3984                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_MD;
3985    
3986                    Object[] finderArgs = new Object[] { companyId, modifiedDate };
3987    
3988                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
3989    
3990                    if (count == null) {
3991                            StringBundler query = new StringBundler(3);
3992    
3993                            query.append(_SQL_COUNT_USER_WHERE);
3994    
3995                            query.append(_FINDER_COLUMN_C_MD_COMPANYID_2);
3996    
3997                            boolean bindModifiedDate = false;
3998    
3999                            if (modifiedDate == null) {
4000                                    query.append(_FINDER_COLUMN_C_MD_MODIFIEDDATE_1);
4001                            }
4002                            else {
4003                                    bindModifiedDate = true;
4004    
4005                                    query.append(_FINDER_COLUMN_C_MD_MODIFIEDDATE_2);
4006                            }
4007    
4008                            String sql = query.toString();
4009    
4010                            Session session = null;
4011    
4012                            try {
4013                                    session = openSession();
4014    
4015                                    Query q = session.createQuery(sql);
4016    
4017                                    QueryPos qPos = QueryPos.getInstance(q);
4018    
4019                                    qPos.add(companyId);
4020    
4021                                    if (bindModifiedDate) {
4022                                            qPos.add(new Timestamp(modifiedDate.getTime()));
4023                                    }
4024    
4025                                    count = (Long)q.uniqueResult();
4026    
4027                                    finderCache.putResult(finderPath, finderArgs, count);
4028                            }
4029                            catch (Exception e) {
4030                                    finderCache.removeResult(finderPath, finderArgs);
4031    
4032                                    throw processException(e);
4033                            }
4034                            finally {
4035                                    closeSession(session);
4036                            }
4037                    }
4038    
4039                    return count.intValue();
4040            }
4041    
4042            private static final String _FINDER_COLUMN_C_MD_COMPANYID_2 = "user.companyId = ? AND ";
4043            private static final String _FINDER_COLUMN_C_MD_MODIFIEDDATE_1 = "user.modifiedDate IS NULL";
4044            private static final String _FINDER_COLUMN_C_MD_MODIFIEDDATE_2 = "user.modifiedDate = ?";
4045            public static final FinderPath FINDER_PATH_FETCH_BY_C_DU = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
4046                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
4047                            FINDER_CLASS_NAME_ENTITY, "fetchByC_DU",
4048                            new String[] { Long.class.getName(), Boolean.class.getName() },
4049                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
4050                            UserModelImpl.DEFAULTUSER_COLUMN_BITMASK);
4051            public static final FinderPath FINDER_PATH_COUNT_BY_C_DU = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
4052                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
4053                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_DU",
4054                            new String[] { Long.class.getName(), Boolean.class.getName() });
4055    
4056            /**
4057             * Returns the user where companyId = &#63; and defaultUser = &#63; or throws a {@link NoSuchUserException} if it could not be found.
4058             *
4059             * @param companyId the company ID
4060             * @param defaultUser the default user
4061             * @return the matching user
4062             * @throws NoSuchUserException if a matching user could not be found
4063             */
4064            @Override
4065            public User findByC_DU(long companyId, boolean defaultUser)
4066                    throws NoSuchUserException {
4067                    User user = fetchByC_DU(companyId, defaultUser);
4068    
4069                    if (user == null) {
4070                            StringBundler msg = new StringBundler(6);
4071    
4072                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
4073    
4074                            msg.append("companyId=");
4075                            msg.append(companyId);
4076    
4077                            msg.append(", defaultUser=");
4078                            msg.append(defaultUser);
4079    
4080                            msg.append(StringPool.CLOSE_CURLY_BRACE);
4081    
4082                            if (_log.isWarnEnabled()) {
4083                                    _log.warn(msg.toString());
4084                            }
4085    
4086                            throw new NoSuchUserException(msg.toString());
4087                    }
4088    
4089                    return user;
4090            }
4091    
4092            /**
4093             * Returns the user where companyId = &#63; and defaultUser = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
4094             *
4095             * @param companyId the company ID
4096             * @param defaultUser the default user
4097             * @return the matching user, or <code>null</code> if a matching user could not be found
4098             */
4099            @Override
4100            public User fetchByC_DU(long companyId, boolean defaultUser) {
4101                    return fetchByC_DU(companyId, defaultUser, true);
4102            }
4103    
4104            /**
4105             * Returns the user where companyId = &#63; and defaultUser = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
4106             *
4107             * @param companyId the company ID
4108             * @param defaultUser the default user
4109             * @param retrieveFromCache whether to retrieve from the finder cache
4110             * @return the matching user, or <code>null</code> if a matching user could not be found
4111             */
4112            @Override
4113            public User fetchByC_DU(long companyId, boolean defaultUser,
4114                    boolean retrieveFromCache) {
4115                    Object[] finderArgs = new Object[] { companyId, defaultUser };
4116    
4117                    Object result = null;
4118    
4119                    if (retrieveFromCache) {
4120                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_C_DU,
4121                                            finderArgs, this);
4122                    }
4123    
4124                    if (result instanceof User) {
4125                            User user = (User)result;
4126    
4127                            if ((companyId != user.getCompanyId()) ||
4128                                            (defaultUser != user.getDefaultUser())) {
4129                                    result = null;
4130                            }
4131                    }
4132    
4133                    if (result == null) {
4134                            StringBundler query = new StringBundler(4);
4135    
4136                            query.append(_SQL_SELECT_USER_WHERE);
4137    
4138                            query.append(_FINDER_COLUMN_C_DU_COMPANYID_2);
4139    
4140                            query.append(_FINDER_COLUMN_C_DU_DEFAULTUSER_2);
4141    
4142                            String sql = query.toString();
4143    
4144                            Session session = null;
4145    
4146                            try {
4147                                    session = openSession();
4148    
4149                                    Query q = session.createQuery(sql);
4150    
4151                                    QueryPos qPos = QueryPos.getInstance(q);
4152    
4153                                    qPos.add(companyId);
4154    
4155                                    qPos.add(defaultUser);
4156    
4157                                    List<User> list = q.list();
4158    
4159                                    if (list.isEmpty()) {
4160                                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_DU,
4161                                                    finderArgs, list);
4162                                    }
4163                                    else {
4164                                            if ((list.size() > 1) && _log.isWarnEnabled()) {
4165                                                    _log.warn(
4166                                                            "UserPersistenceImpl.fetchByC_DU(long, boolean, boolean) with parameters (" +
4167                                                            StringUtil.merge(finderArgs) +
4168                                                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
4169                                            }
4170    
4171                                            User user = list.get(0);
4172    
4173                                            result = user;
4174    
4175                                            cacheResult(user);
4176    
4177                                            if ((user.getCompanyId() != companyId) ||
4178                                                            (user.getDefaultUser() != defaultUser)) {
4179                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_DU,
4180                                                            finderArgs, user);
4181                                            }
4182                                    }
4183                            }
4184                            catch (Exception e) {
4185                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_DU, finderArgs);
4186    
4187                                    throw processException(e);
4188                            }
4189                            finally {
4190                                    closeSession(session);
4191                            }
4192                    }
4193    
4194                    if (result instanceof List<?>) {
4195                            return null;
4196                    }
4197                    else {
4198                            return (User)result;
4199                    }
4200            }
4201    
4202            /**
4203             * Removes the user where companyId = &#63; and defaultUser = &#63; from the database.
4204             *
4205             * @param companyId the company ID
4206             * @param defaultUser the default user
4207             * @return the user that was removed
4208             */
4209            @Override
4210            public User removeByC_DU(long companyId, boolean defaultUser)
4211                    throws NoSuchUserException {
4212                    User user = findByC_DU(companyId, defaultUser);
4213    
4214                    return remove(user);
4215            }
4216    
4217            /**
4218             * Returns the number of users where companyId = &#63; and defaultUser = &#63;.
4219             *
4220             * @param companyId the company ID
4221             * @param defaultUser the default user
4222             * @return the number of matching users
4223             */
4224            @Override
4225            public int countByC_DU(long companyId, boolean defaultUser) {
4226                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_DU;
4227    
4228                    Object[] finderArgs = new Object[] { companyId, defaultUser };
4229    
4230                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
4231    
4232                    if (count == null) {
4233                            StringBundler query = new StringBundler(3);
4234    
4235                            query.append(_SQL_COUNT_USER_WHERE);
4236    
4237                            query.append(_FINDER_COLUMN_C_DU_COMPANYID_2);
4238    
4239                            query.append(_FINDER_COLUMN_C_DU_DEFAULTUSER_2);
4240    
4241                            String sql = query.toString();
4242    
4243                            Session session = null;
4244    
4245                            try {
4246                                    session = openSession();
4247    
4248                                    Query q = session.createQuery(sql);
4249    
4250                                    QueryPos qPos = QueryPos.getInstance(q);
4251    
4252                                    qPos.add(companyId);
4253    
4254                                    qPos.add(defaultUser);
4255    
4256                                    count = (Long)q.uniqueResult();
4257    
4258                                    finderCache.putResult(finderPath, finderArgs, count);
4259                            }
4260                            catch (Exception e) {
4261                                    finderCache.removeResult(finderPath, finderArgs);
4262    
4263                                    throw processException(e);
4264                            }
4265                            finally {
4266                                    closeSession(session);
4267                            }
4268                    }
4269    
4270                    return count.intValue();
4271            }
4272    
4273            private static final String _FINDER_COLUMN_C_DU_COMPANYID_2 = "user.companyId = ? AND ";
4274            private static final String _FINDER_COLUMN_C_DU_DEFAULTUSER_2 = "user.defaultUser = ?";
4275            public static final FinderPath FINDER_PATH_FETCH_BY_C_SN = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
4276                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
4277                            FINDER_CLASS_NAME_ENTITY, "fetchByC_SN",
4278                            new String[] { Long.class.getName(), String.class.getName() },
4279                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
4280                            UserModelImpl.SCREENNAME_COLUMN_BITMASK);
4281            public static final FinderPath FINDER_PATH_COUNT_BY_C_SN = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
4282                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
4283                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_SN",
4284                            new String[] { Long.class.getName(), String.class.getName() });
4285    
4286            /**
4287             * Returns the user where companyId = &#63; and screenName = &#63; or throws a {@link NoSuchUserException} if it could not be found.
4288             *
4289             * @param companyId the company ID
4290             * @param screenName the screen name
4291             * @return the matching user
4292             * @throws NoSuchUserException if a matching user could not be found
4293             */
4294            @Override
4295            public User findByC_SN(long companyId, String screenName)
4296                    throws NoSuchUserException {
4297                    User user = fetchByC_SN(companyId, screenName);
4298    
4299                    if (user == null) {
4300                            StringBundler msg = new StringBundler(6);
4301    
4302                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
4303    
4304                            msg.append("companyId=");
4305                            msg.append(companyId);
4306    
4307                            msg.append(", screenName=");
4308                            msg.append(screenName);
4309    
4310                            msg.append(StringPool.CLOSE_CURLY_BRACE);
4311    
4312                            if (_log.isWarnEnabled()) {
4313                                    _log.warn(msg.toString());
4314                            }
4315    
4316                            throw new NoSuchUserException(msg.toString());
4317                    }
4318    
4319                    return user;
4320            }
4321    
4322            /**
4323             * Returns the user where companyId = &#63; and screenName = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
4324             *
4325             * @param companyId the company ID
4326             * @param screenName the screen name
4327             * @return the matching user, or <code>null</code> if a matching user could not be found
4328             */
4329            @Override
4330            public User fetchByC_SN(long companyId, String screenName) {
4331                    return fetchByC_SN(companyId, screenName, true);
4332            }
4333    
4334            /**
4335             * Returns the user where companyId = &#63; and screenName = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
4336             *
4337             * @param companyId the company ID
4338             * @param screenName the screen name
4339             * @param retrieveFromCache whether to retrieve from the finder cache
4340             * @return the matching user, or <code>null</code> if a matching user could not be found
4341             */
4342            @Override
4343            public User fetchByC_SN(long companyId, String screenName,
4344                    boolean retrieveFromCache) {
4345                    Object[] finderArgs = new Object[] { companyId, screenName };
4346    
4347                    Object result = null;
4348    
4349                    if (retrieveFromCache) {
4350                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_C_SN,
4351                                            finderArgs, this);
4352                    }
4353    
4354                    if (result instanceof User) {
4355                            User user = (User)result;
4356    
4357                            if ((companyId != user.getCompanyId()) ||
4358                                            !Validator.equals(screenName, user.getScreenName())) {
4359                                    result = null;
4360                            }
4361                    }
4362    
4363                    if (result == null) {
4364                            StringBundler query = new StringBundler(4);
4365    
4366                            query.append(_SQL_SELECT_USER_WHERE);
4367    
4368                            query.append(_FINDER_COLUMN_C_SN_COMPANYID_2);
4369    
4370                            boolean bindScreenName = false;
4371    
4372                            if (screenName == null) {
4373                                    query.append(_FINDER_COLUMN_C_SN_SCREENNAME_1);
4374                            }
4375                            else if (screenName.equals(StringPool.BLANK)) {
4376                                    query.append(_FINDER_COLUMN_C_SN_SCREENNAME_3);
4377                            }
4378                            else {
4379                                    bindScreenName = true;
4380    
4381                                    query.append(_FINDER_COLUMN_C_SN_SCREENNAME_2);
4382                            }
4383    
4384                            String sql = query.toString();
4385    
4386                            Session session = null;
4387    
4388                            try {
4389                                    session = openSession();
4390    
4391                                    Query q = session.createQuery(sql);
4392    
4393                                    QueryPos qPos = QueryPos.getInstance(q);
4394    
4395                                    qPos.add(companyId);
4396    
4397                                    if (bindScreenName) {
4398                                            qPos.add(screenName);
4399                                    }
4400    
4401                                    List<User> list = q.list();
4402    
4403                                    if (list.isEmpty()) {
4404                                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_SN,
4405                                                    finderArgs, list);
4406                                    }
4407                                    else {
4408                                            User user = list.get(0);
4409    
4410                                            result = user;
4411    
4412                                            cacheResult(user);
4413    
4414                                            if ((user.getCompanyId() != companyId) ||
4415                                                            (user.getScreenName() == null) ||
4416                                                            !user.getScreenName().equals(screenName)) {
4417                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_SN,
4418                                                            finderArgs, user);
4419                                            }
4420                                    }
4421                            }
4422                            catch (Exception e) {
4423                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_SN, finderArgs);
4424    
4425                                    throw processException(e);
4426                            }
4427                            finally {
4428                                    closeSession(session);
4429                            }
4430                    }
4431    
4432                    if (result instanceof List<?>) {
4433                            return null;
4434                    }
4435                    else {
4436                            return (User)result;
4437                    }
4438            }
4439    
4440            /**
4441             * Removes the user where companyId = &#63; and screenName = &#63; from the database.
4442             *
4443             * @param companyId the company ID
4444             * @param screenName the screen name
4445             * @return the user that was removed
4446             */
4447            @Override
4448            public User removeByC_SN(long companyId, String screenName)
4449                    throws NoSuchUserException {
4450                    User user = findByC_SN(companyId, screenName);
4451    
4452                    return remove(user);
4453            }
4454    
4455            /**
4456             * Returns the number of users where companyId = &#63; and screenName = &#63;.
4457             *
4458             * @param companyId the company ID
4459             * @param screenName the screen name
4460             * @return the number of matching users
4461             */
4462            @Override
4463            public int countByC_SN(long companyId, String screenName) {
4464                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_SN;
4465    
4466                    Object[] finderArgs = new Object[] { companyId, screenName };
4467    
4468                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
4469    
4470                    if (count == null) {
4471                            StringBundler query = new StringBundler(3);
4472    
4473                            query.append(_SQL_COUNT_USER_WHERE);
4474    
4475                            query.append(_FINDER_COLUMN_C_SN_COMPANYID_2);
4476    
4477                            boolean bindScreenName = false;
4478    
4479                            if (screenName == null) {
4480                                    query.append(_FINDER_COLUMN_C_SN_SCREENNAME_1);
4481                            }
4482                            else if (screenName.equals(StringPool.BLANK)) {
4483                                    query.append(_FINDER_COLUMN_C_SN_SCREENNAME_3);
4484                            }
4485                            else {
4486                                    bindScreenName = true;
4487    
4488                                    query.append(_FINDER_COLUMN_C_SN_SCREENNAME_2);
4489                            }
4490    
4491                            String sql = query.toString();
4492    
4493                            Session session = null;
4494    
4495                            try {
4496                                    session = openSession();
4497    
4498                                    Query q = session.createQuery(sql);
4499    
4500                                    QueryPos qPos = QueryPos.getInstance(q);
4501    
4502                                    qPos.add(companyId);
4503    
4504                                    if (bindScreenName) {
4505                                            qPos.add(screenName);
4506                                    }
4507    
4508                                    count = (Long)q.uniqueResult();
4509    
4510                                    finderCache.putResult(finderPath, finderArgs, count);
4511                            }
4512                            catch (Exception e) {
4513                                    finderCache.removeResult(finderPath, finderArgs);
4514    
4515                                    throw processException(e);
4516                            }
4517                            finally {
4518                                    closeSession(session);
4519                            }
4520                    }
4521    
4522                    return count.intValue();
4523            }
4524    
4525            private static final String _FINDER_COLUMN_C_SN_COMPANYID_2 = "user.companyId = ? AND ";
4526            private static final String _FINDER_COLUMN_C_SN_SCREENNAME_1 = "user.screenName IS NULL";
4527            private static final String _FINDER_COLUMN_C_SN_SCREENNAME_2 = "user.screenName = ?";
4528            private static final String _FINDER_COLUMN_C_SN_SCREENNAME_3 = "(user.screenName IS NULL OR user.screenName = '')";
4529            public static final FinderPath FINDER_PATH_FETCH_BY_C_EA = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
4530                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
4531                            FINDER_CLASS_NAME_ENTITY, "fetchByC_EA",
4532                            new String[] { Long.class.getName(), String.class.getName() },
4533                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
4534                            UserModelImpl.EMAILADDRESS_COLUMN_BITMASK);
4535            public static final FinderPath FINDER_PATH_COUNT_BY_C_EA = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
4536                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
4537                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_EA",
4538                            new String[] { Long.class.getName(), String.class.getName() });
4539    
4540            /**
4541             * Returns the user where companyId = &#63; and emailAddress = &#63; or throws a {@link NoSuchUserException} if it could not be found.
4542             *
4543             * @param companyId the company ID
4544             * @param emailAddress the email address
4545             * @return the matching user
4546             * @throws NoSuchUserException if a matching user could not be found
4547             */
4548            @Override
4549            public User findByC_EA(long companyId, String emailAddress)
4550                    throws NoSuchUserException {
4551                    User user = fetchByC_EA(companyId, emailAddress);
4552    
4553                    if (user == null) {
4554                            StringBundler msg = new StringBundler(6);
4555    
4556                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
4557    
4558                            msg.append("companyId=");
4559                            msg.append(companyId);
4560    
4561                            msg.append(", emailAddress=");
4562                            msg.append(emailAddress);
4563    
4564                            msg.append(StringPool.CLOSE_CURLY_BRACE);
4565    
4566                            if (_log.isWarnEnabled()) {
4567                                    _log.warn(msg.toString());
4568                            }
4569    
4570                            throw new NoSuchUserException(msg.toString());
4571                    }
4572    
4573                    return user;
4574            }
4575    
4576            /**
4577             * Returns the user where companyId = &#63; and emailAddress = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
4578             *
4579             * @param companyId the company ID
4580             * @param emailAddress the email address
4581             * @return the matching user, or <code>null</code> if a matching user could not be found
4582             */
4583            @Override
4584            public User fetchByC_EA(long companyId, String emailAddress) {
4585                    return fetchByC_EA(companyId, emailAddress, true);
4586            }
4587    
4588            /**
4589             * Returns the user where companyId = &#63; and emailAddress = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
4590             *
4591             * @param companyId the company ID
4592             * @param emailAddress the email address
4593             * @param retrieveFromCache whether to retrieve from the finder cache
4594             * @return the matching user, or <code>null</code> if a matching user could not be found
4595             */
4596            @Override
4597            public User fetchByC_EA(long companyId, String emailAddress,
4598                    boolean retrieveFromCache) {
4599                    Object[] finderArgs = new Object[] { companyId, emailAddress };
4600    
4601                    Object result = null;
4602    
4603                    if (retrieveFromCache) {
4604                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_C_EA,
4605                                            finderArgs, this);
4606                    }
4607    
4608                    if (result instanceof User) {
4609                            User user = (User)result;
4610    
4611                            if ((companyId != user.getCompanyId()) ||
4612                                            !Validator.equals(emailAddress, user.getEmailAddress())) {
4613                                    result = null;
4614                            }
4615                    }
4616    
4617                    if (result == null) {
4618                            StringBundler query = new StringBundler(4);
4619    
4620                            query.append(_SQL_SELECT_USER_WHERE);
4621    
4622                            query.append(_FINDER_COLUMN_C_EA_COMPANYID_2);
4623    
4624                            boolean bindEmailAddress = false;
4625    
4626                            if (emailAddress == null) {
4627                                    query.append(_FINDER_COLUMN_C_EA_EMAILADDRESS_1);
4628                            }
4629                            else if (emailAddress.equals(StringPool.BLANK)) {
4630                                    query.append(_FINDER_COLUMN_C_EA_EMAILADDRESS_3);
4631                            }
4632                            else {
4633                                    bindEmailAddress = true;
4634    
4635                                    query.append(_FINDER_COLUMN_C_EA_EMAILADDRESS_2);
4636                            }
4637    
4638                            String sql = query.toString();
4639    
4640                            Session session = null;
4641    
4642                            try {
4643                                    session = openSession();
4644    
4645                                    Query q = session.createQuery(sql);
4646    
4647                                    QueryPos qPos = QueryPos.getInstance(q);
4648    
4649                                    qPos.add(companyId);
4650    
4651                                    if (bindEmailAddress) {
4652                                            qPos.add(emailAddress);
4653                                    }
4654    
4655                                    List<User> list = q.list();
4656    
4657                                    if (list.isEmpty()) {
4658                                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_EA,
4659                                                    finderArgs, list);
4660                                    }
4661                                    else {
4662                                            User user = list.get(0);
4663    
4664                                            result = user;
4665    
4666                                            cacheResult(user);
4667    
4668                                            if ((user.getCompanyId() != companyId) ||
4669                                                            (user.getEmailAddress() == null) ||
4670                                                            !user.getEmailAddress().equals(emailAddress)) {
4671                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_EA,
4672                                                            finderArgs, user);
4673                                            }
4674                                    }
4675                            }
4676                            catch (Exception e) {
4677                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_EA, finderArgs);
4678    
4679                                    throw processException(e);
4680                            }
4681                            finally {
4682                                    closeSession(session);
4683                            }
4684                    }
4685    
4686                    if (result instanceof List<?>) {
4687                            return null;
4688                    }
4689                    else {
4690                            return (User)result;
4691                    }
4692            }
4693    
4694            /**
4695             * Removes the user where companyId = &#63; and emailAddress = &#63; from the database.
4696             *
4697             * @param companyId the company ID
4698             * @param emailAddress the email address
4699             * @return the user that was removed
4700             */
4701            @Override
4702            public User removeByC_EA(long companyId, String emailAddress)
4703                    throws NoSuchUserException {
4704                    User user = findByC_EA(companyId, emailAddress);
4705    
4706                    return remove(user);
4707            }
4708    
4709            /**
4710             * Returns the number of users where companyId = &#63; and emailAddress = &#63;.
4711             *
4712             * @param companyId the company ID
4713             * @param emailAddress the email address
4714             * @return the number of matching users
4715             */
4716            @Override
4717            public int countByC_EA(long companyId, String emailAddress) {
4718                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_EA;
4719    
4720                    Object[] finderArgs = new Object[] { companyId, emailAddress };
4721    
4722                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
4723    
4724                    if (count == null) {
4725                            StringBundler query = new StringBundler(3);
4726    
4727                            query.append(_SQL_COUNT_USER_WHERE);
4728    
4729                            query.append(_FINDER_COLUMN_C_EA_COMPANYID_2);
4730    
4731                            boolean bindEmailAddress = false;
4732    
4733                            if (emailAddress == null) {
4734                                    query.append(_FINDER_COLUMN_C_EA_EMAILADDRESS_1);
4735                            }
4736                            else if (emailAddress.equals(StringPool.BLANK)) {
4737                                    query.append(_FINDER_COLUMN_C_EA_EMAILADDRESS_3);
4738                            }
4739                            else {
4740                                    bindEmailAddress = true;
4741    
4742                                    query.append(_FINDER_COLUMN_C_EA_EMAILADDRESS_2);
4743                            }
4744    
4745                            String sql = query.toString();
4746    
4747                            Session session = null;
4748    
4749                            try {
4750                                    session = openSession();
4751    
4752                                    Query q = session.createQuery(sql);
4753    
4754                                    QueryPos qPos = QueryPos.getInstance(q);
4755    
4756                                    qPos.add(companyId);
4757    
4758                                    if (bindEmailAddress) {
4759                                            qPos.add(emailAddress);
4760                                    }
4761    
4762                                    count = (Long)q.uniqueResult();
4763    
4764                                    finderCache.putResult(finderPath, finderArgs, count);
4765                            }
4766                            catch (Exception e) {
4767                                    finderCache.removeResult(finderPath, finderArgs);
4768    
4769                                    throw processException(e);
4770                            }
4771                            finally {
4772                                    closeSession(session);
4773                            }
4774                    }
4775    
4776                    return count.intValue();
4777            }
4778    
4779            private static final String _FINDER_COLUMN_C_EA_COMPANYID_2 = "user.companyId = ? AND ";
4780            private static final String _FINDER_COLUMN_C_EA_EMAILADDRESS_1 = "user.emailAddress IS NULL";
4781            private static final String _FINDER_COLUMN_C_EA_EMAILADDRESS_2 = "user.emailAddress = ?";
4782            private static final String _FINDER_COLUMN_C_EA_EMAILADDRESS_3 = "(user.emailAddress IS NULL OR user.emailAddress = '')";
4783            public static final FinderPath FINDER_PATH_FETCH_BY_C_FID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
4784                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
4785                            FINDER_CLASS_NAME_ENTITY, "fetchByC_FID",
4786                            new String[] { Long.class.getName(), Long.class.getName() },
4787                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
4788                            UserModelImpl.FACEBOOKID_COLUMN_BITMASK);
4789            public static final FinderPath FINDER_PATH_COUNT_BY_C_FID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
4790                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
4791                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_FID",
4792                            new String[] { Long.class.getName(), Long.class.getName() });
4793    
4794            /**
4795             * Returns the user where companyId = &#63; and facebookId = &#63; or throws a {@link NoSuchUserException} if it could not be found.
4796             *
4797             * @param companyId the company ID
4798             * @param facebookId the facebook ID
4799             * @return the matching user
4800             * @throws NoSuchUserException if a matching user could not be found
4801             */
4802            @Override
4803            public User findByC_FID(long companyId, long facebookId)
4804                    throws NoSuchUserException {
4805                    User user = fetchByC_FID(companyId, facebookId);
4806    
4807                    if (user == null) {
4808                            StringBundler msg = new StringBundler(6);
4809    
4810                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
4811    
4812                            msg.append("companyId=");
4813                            msg.append(companyId);
4814    
4815                            msg.append(", facebookId=");
4816                            msg.append(facebookId);
4817    
4818                            msg.append(StringPool.CLOSE_CURLY_BRACE);
4819    
4820                            if (_log.isWarnEnabled()) {
4821                                    _log.warn(msg.toString());
4822                            }
4823    
4824                            throw new NoSuchUserException(msg.toString());
4825                    }
4826    
4827                    return user;
4828            }
4829    
4830            /**
4831             * Returns the user where companyId = &#63; and facebookId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
4832             *
4833             * @param companyId the company ID
4834             * @param facebookId the facebook ID
4835             * @return the matching user, or <code>null</code> if a matching user could not be found
4836             */
4837            @Override
4838            public User fetchByC_FID(long companyId, long facebookId) {
4839                    return fetchByC_FID(companyId, facebookId, true);
4840            }
4841    
4842            /**
4843             * Returns the user where companyId = &#63; and facebookId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
4844             *
4845             * @param companyId the company ID
4846             * @param facebookId the facebook ID
4847             * @param retrieveFromCache whether to retrieve from the finder cache
4848             * @return the matching user, or <code>null</code> if a matching user could not be found
4849             */
4850            @Override
4851            public User fetchByC_FID(long companyId, long facebookId,
4852                    boolean retrieveFromCache) {
4853                    Object[] finderArgs = new Object[] { companyId, facebookId };
4854    
4855                    Object result = null;
4856    
4857                    if (retrieveFromCache) {
4858                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_C_FID,
4859                                            finderArgs, this);
4860                    }
4861    
4862                    if (result instanceof User) {
4863                            User user = (User)result;
4864    
4865                            if ((companyId != user.getCompanyId()) ||
4866                                            (facebookId != user.getFacebookId())) {
4867                                    result = null;
4868                            }
4869                    }
4870    
4871                    if (result == null) {
4872                            StringBundler query = new StringBundler(4);
4873    
4874                            query.append(_SQL_SELECT_USER_WHERE);
4875    
4876                            query.append(_FINDER_COLUMN_C_FID_COMPANYID_2);
4877    
4878                            query.append(_FINDER_COLUMN_C_FID_FACEBOOKID_2);
4879    
4880                            String sql = query.toString();
4881    
4882                            Session session = null;
4883    
4884                            try {
4885                                    session = openSession();
4886    
4887                                    Query q = session.createQuery(sql);
4888    
4889                                    QueryPos qPos = QueryPos.getInstance(q);
4890    
4891                                    qPos.add(companyId);
4892    
4893                                    qPos.add(facebookId);
4894    
4895                                    List<User> list = q.list();
4896    
4897                                    if (list.isEmpty()) {
4898                                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_FID,
4899                                                    finderArgs, list);
4900                                    }
4901                                    else {
4902                                            if ((list.size() > 1) && _log.isWarnEnabled()) {
4903                                                    _log.warn(
4904                                                            "UserPersistenceImpl.fetchByC_FID(long, long, boolean) with parameters (" +
4905                                                            StringUtil.merge(finderArgs) +
4906                                                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
4907                                            }
4908    
4909                                            User user = list.get(0);
4910    
4911                                            result = user;
4912    
4913                                            cacheResult(user);
4914    
4915                                            if ((user.getCompanyId() != companyId) ||
4916                                                            (user.getFacebookId() != facebookId)) {
4917                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_FID,
4918                                                            finderArgs, user);
4919                                            }
4920                                    }
4921                            }
4922                            catch (Exception e) {
4923                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_FID, finderArgs);
4924    
4925                                    throw processException(e);
4926                            }
4927                            finally {
4928                                    closeSession(session);
4929                            }
4930                    }
4931    
4932                    if (result instanceof List<?>) {
4933                            return null;
4934                    }
4935                    else {
4936                            return (User)result;
4937                    }
4938            }
4939    
4940            /**
4941             * Removes the user where companyId = &#63; and facebookId = &#63; from the database.
4942             *
4943             * @param companyId the company ID
4944             * @param facebookId the facebook ID
4945             * @return the user that was removed
4946             */
4947            @Override
4948            public User removeByC_FID(long companyId, long facebookId)
4949                    throws NoSuchUserException {
4950                    User user = findByC_FID(companyId, facebookId);
4951    
4952                    return remove(user);
4953            }
4954    
4955            /**
4956             * Returns the number of users where companyId = &#63; and facebookId = &#63;.
4957             *
4958             * @param companyId the company ID
4959             * @param facebookId the facebook ID
4960             * @return the number of matching users
4961             */
4962            @Override
4963            public int countByC_FID(long companyId, long facebookId) {
4964                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_FID;
4965    
4966                    Object[] finderArgs = new Object[] { companyId, facebookId };
4967    
4968                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
4969    
4970                    if (count == null) {
4971                            StringBundler query = new StringBundler(3);
4972    
4973                            query.append(_SQL_COUNT_USER_WHERE);
4974    
4975                            query.append(_FINDER_COLUMN_C_FID_COMPANYID_2);
4976    
4977                            query.append(_FINDER_COLUMN_C_FID_FACEBOOKID_2);
4978    
4979                            String sql = query.toString();
4980    
4981                            Session session = null;
4982    
4983                            try {
4984                                    session = openSession();
4985    
4986                                    Query q = session.createQuery(sql);
4987    
4988                                    QueryPos qPos = QueryPos.getInstance(q);
4989    
4990                                    qPos.add(companyId);
4991    
4992                                    qPos.add(facebookId);
4993    
4994                                    count = (Long)q.uniqueResult();
4995    
4996                                    finderCache.putResult(finderPath, finderArgs, count);
4997                            }
4998                            catch (Exception e) {
4999                                    finderCache.removeResult(finderPath, finderArgs);
5000    
5001                                    throw processException(e);
5002                            }
5003                            finally {
5004                                    closeSession(session);
5005                            }
5006                    }
5007    
5008                    return count.intValue();
5009            }
5010    
5011            private static final String _FINDER_COLUMN_C_FID_COMPANYID_2 = "user.companyId = ? AND ";
5012            private static final String _FINDER_COLUMN_C_FID_FACEBOOKID_2 = "user.facebookId = ?";
5013            public static final FinderPath FINDER_PATH_FETCH_BY_C_GUID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
5014                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
5015                            FINDER_CLASS_NAME_ENTITY, "fetchByC_GUID",
5016                            new String[] { Long.class.getName(), String.class.getName() },
5017                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
5018                            UserModelImpl.GOOGLEUSERID_COLUMN_BITMASK);
5019            public static final FinderPath FINDER_PATH_COUNT_BY_C_GUID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
5020                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
5021                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_GUID",
5022                            new String[] { Long.class.getName(), String.class.getName() });
5023    
5024            /**
5025             * Returns the user where companyId = &#63; and googleUserId = &#63; or throws a {@link NoSuchUserException} if it could not be found.
5026             *
5027             * @param companyId the company ID
5028             * @param googleUserId the google user ID
5029             * @return the matching user
5030             * @throws NoSuchUserException if a matching user could not be found
5031             */
5032            @Override
5033            public User findByC_GUID(long companyId, String googleUserId)
5034                    throws NoSuchUserException {
5035                    User user = fetchByC_GUID(companyId, googleUserId);
5036    
5037                    if (user == null) {
5038                            StringBundler msg = new StringBundler(6);
5039    
5040                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
5041    
5042                            msg.append("companyId=");
5043                            msg.append(companyId);
5044    
5045                            msg.append(", googleUserId=");
5046                            msg.append(googleUserId);
5047    
5048                            msg.append(StringPool.CLOSE_CURLY_BRACE);
5049    
5050                            if (_log.isWarnEnabled()) {
5051                                    _log.warn(msg.toString());
5052                            }
5053    
5054                            throw new NoSuchUserException(msg.toString());
5055                    }
5056    
5057                    return user;
5058            }
5059    
5060            /**
5061             * Returns the user where companyId = &#63; and googleUserId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
5062             *
5063             * @param companyId the company ID
5064             * @param googleUserId the google user ID
5065             * @return the matching user, or <code>null</code> if a matching user could not be found
5066             */
5067            @Override
5068            public User fetchByC_GUID(long companyId, String googleUserId) {
5069                    return fetchByC_GUID(companyId, googleUserId, true);
5070            }
5071    
5072            /**
5073             * Returns the user where companyId = &#63; and googleUserId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
5074             *
5075             * @param companyId the company ID
5076             * @param googleUserId the google user ID
5077             * @param retrieveFromCache whether to retrieve from the finder cache
5078             * @return the matching user, or <code>null</code> if a matching user could not be found
5079             */
5080            @Override
5081            public User fetchByC_GUID(long companyId, String googleUserId,
5082                    boolean retrieveFromCache) {
5083                    Object[] finderArgs = new Object[] { companyId, googleUserId };
5084    
5085                    Object result = null;
5086    
5087                    if (retrieveFromCache) {
5088                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_C_GUID,
5089                                            finderArgs, this);
5090                    }
5091    
5092                    if (result instanceof User) {
5093                            User user = (User)result;
5094    
5095                            if ((companyId != user.getCompanyId()) ||
5096                                            !Validator.equals(googleUserId, user.getGoogleUserId())) {
5097                                    result = null;
5098                            }
5099                    }
5100    
5101                    if (result == null) {
5102                            StringBundler query = new StringBundler(4);
5103    
5104                            query.append(_SQL_SELECT_USER_WHERE);
5105    
5106                            query.append(_FINDER_COLUMN_C_GUID_COMPANYID_2);
5107    
5108                            boolean bindGoogleUserId = false;
5109    
5110                            if (googleUserId == null) {
5111                                    query.append(_FINDER_COLUMN_C_GUID_GOOGLEUSERID_1);
5112                            }
5113                            else if (googleUserId.equals(StringPool.BLANK)) {
5114                                    query.append(_FINDER_COLUMN_C_GUID_GOOGLEUSERID_3);
5115                            }
5116                            else {
5117                                    bindGoogleUserId = true;
5118    
5119                                    query.append(_FINDER_COLUMN_C_GUID_GOOGLEUSERID_2);
5120                            }
5121    
5122                            String sql = query.toString();
5123    
5124                            Session session = null;
5125    
5126                            try {
5127                                    session = openSession();
5128    
5129                                    Query q = session.createQuery(sql);
5130    
5131                                    QueryPos qPos = QueryPos.getInstance(q);
5132    
5133                                    qPos.add(companyId);
5134    
5135                                    if (bindGoogleUserId) {
5136                                            qPos.add(googleUserId);
5137                                    }
5138    
5139                                    List<User> list = q.list();
5140    
5141                                    if (list.isEmpty()) {
5142                                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_GUID,
5143                                                    finderArgs, list);
5144                                    }
5145                                    else {
5146                                            if ((list.size() > 1) && _log.isWarnEnabled()) {
5147                                                    _log.warn(
5148                                                            "UserPersistenceImpl.fetchByC_GUID(long, String, boolean) with parameters (" +
5149                                                            StringUtil.merge(finderArgs) +
5150                                                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
5151                                            }
5152    
5153                                            User user = list.get(0);
5154    
5155                                            result = user;
5156    
5157                                            cacheResult(user);
5158    
5159                                            if ((user.getCompanyId() != companyId) ||
5160                                                            (user.getGoogleUserId() == null) ||
5161                                                            !user.getGoogleUserId().equals(googleUserId)) {
5162                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_GUID,
5163                                                            finderArgs, user);
5164                                            }
5165                                    }
5166                            }
5167                            catch (Exception e) {
5168                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_GUID, finderArgs);
5169    
5170                                    throw processException(e);
5171                            }
5172                            finally {
5173                                    closeSession(session);
5174                            }
5175                    }
5176    
5177                    if (result instanceof List<?>) {
5178                            return null;
5179                    }
5180                    else {
5181                            return (User)result;
5182                    }
5183            }
5184    
5185            /**
5186             * Removes the user where companyId = &#63; and googleUserId = &#63; from the database.
5187             *
5188             * @param companyId the company ID
5189             * @param googleUserId the google user ID
5190             * @return the user that was removed
5191             */
5192            @Override
5193            public User removeByC_GUID(long companyId, String googleUserId)
5194                    throws NoSuchUserException {
5195                    User user = findByC_GUID(companyId, googleUserId);
5196    
5197                    return remove(user);
5198            }
5199    
5200            /**
5201             * Returns the number of users where companyId = &#63; and googleUserId = &#63;.
5202             *
5203             * @param companyId the company ID
5204             * @param googleUserId the google user ID
5205             * @return the number of matching users
5206             */
5207            @Override
5208            public int countByC_GUID(long companyId, String googleUserId) {
5209                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_GUID;
5210    
5211                    Object[] finderArgs = new Object[] { companyId, googleUserId };
5212    
5213                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
5214    
5215                    if (count == null) {
5216                            StringBundler query = new StringBundler(3);
5217    
5218                            query.append(_SQL_COUNT_USER_WHERE);
5219    
5220                            query.append(_FINDER_COLUMN_C_GUID_COMPANYID_2);
5221    
5222                            boolean bindGoogleUserId = false;
5223    
5224                            if (googleUserId == null) {
5225                                    query.append(_FINDER_COLUMN_C_GUID_GOOGLEUSERID_1);
5226                            }
5227                            else if (googleUserId.equals(StringPool.BLANK)) {
5228                                    query.append(_FINDER_COLUMN_C_GUID_GOOGLEUSERID_3);
5229                            }
5230                            else {
5231                                    bindGoogleUserId = true;
5232    
5233                                    query.append(_FINDER_COLUMN_C_GUID_GOOGLEUSERID_2);
5234                            }
5235    
5236                            String sql = query.toString();
5237    
5238                            Session session = null;
5239    
5240                            try {
5241                                    session = openSession();
5242    
5243                                    Query q = session.createQuery(sql);
5244    
5245                                    QueryPos qPos = QueryPos.getInstance(q);
5246    
5247                                    qPos.add(companyId);
5248    
5249                                    if (bindGoogleUserId) {
5250                                            qPos.add(googleUserId);
5251                                    }
5252    
5253                                    count = (Long)q.uniqueResult();
5254    
5255                                    finderCache.putResult(finderPath, finderArgs, count);
5256                            }
5257                            catch (Exception e) {
5258                                    finderCache.removeResult(finderPath, finderArgs);
5259    
5260                                    throw processException(e);
5261                            }
5262                            finally {
5263                                    closeSession(session);
5264                            }
5265                    }
5266    
5267                    return count.intValue();
5268            }
5269    
5270            private static final String _FINDER_COLUMN_C_GUID_COMPANYID_2 = "user.companyId = ? AND ";
5271            private static final String _FINDER_COLUMN_C_GUID_GOOGLEUSERID_1 = "user.googleUserId IS NULL";
5272            private static final String _FINDER_COLUMN_C_GUID_GOOGLEUSERID_2 = "user.googleUserId = ?";
5273            private static final String _FINDER_COLUMN_C_GUID_GOOGLEUSERID_3 = "(user.googleUserId IS NULL OR user.googleUserId = '')";
5274            public static final FinderPath FINDER_PATH_FETCH_BY_C_O = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
5275                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
5276                            FINDER_CLASS_NAME_ENTITY, "fetchByC_O",
5277                            new String[] { Long.class.getName(), String.class.getName() },
5278                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
5279                            UserModelImpl.OPENID_COLUMN_BITMASK);
5280            public static final FinderPath FINDER_PATH_COUNT_BY_C_O = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
5281                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
5282                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_O",
5283                            new String[] { Long.class.getName(), String.class.getName() });
5284    
5285            /**
5286             * Returns the user where companyId = &#63; and openId = &#63; or throws a {@link NoSuchUserException} if it could not be found.
5287             *
5288             * @param companyId the company ID
5289             * @param openId the open ID
5290             * @return the matching user
5291             * @throws NoSuchUserException if a matching user could not be found
5292             */
5293            @Override
5294            public User findByC_O(long companyId, String openId)
5295                    throws NoSuchUserException {
5296                    User user = fetchByC_O(companyId, openId);
5297    
5298                    if (user == null) {
5299                            StringBundler msg = new StringBundler(6);
5300    
5301                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
5302    
5303                            msg.append("companyId=");
5304                            msg.append(companyId);
5305    
5306                            msg.append(", openId=");
5307                            msg.append(openId);
5308    
5309                            msg.append(StringPool.CLOSE_CURLY_BRACE);
5310    
5311                            if (_log.isWarnEnabled()) {
5312                                    _log.warn(msg.toString());
5313                            }
5314    
5315                            throw new NoSuchUserException(msg.toString());
5316                    }
5317    
5318                    return user;
5319            }
5320    
5321            /**
5322             * Returns the user where companyId = &#63; and openId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
5323             *
5324             * @param companyId the company ID
5325             * @param openId the open ID
5326             * @return the matching user, or <code>null</code> if a matching user could not be found
5327             */
5328            @Override
5329            public User fetchByC_O(long companyId, String openId) {
5330                    return fetchByC_O(companyId, openId, true);
5331            }
5332    
5333            /**
5334             * Returns the user where companyId = &#63; and openId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
5335             *
5336             * @param companyId the company ID
5337             * @param openId the open ID
5338             * @param retrieveFromCache whether to retrieve from the finder cache
5339             * @return the matching user, or <code>null</code> if a matching user could not be found
5340             */
5341            @Override
5342            public User fetchByC_O(long companyId, String openId,
5343                    boolean retrieveFromCache) {
5344                    Object[] finderArgs = new Object[] { companyId, openId };
5345    
5346                    Object result = null;
5347    
5348                    if (retrieveFromCache) {
5349                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_C_O,
5350                                            finderArgs, this);
5351                    }
5352    
5353                    if (result instanceof User) {
5354                            User user = (User)result;
5355    
5356                            if ((companyId != user.getCompanyId()) ||
5357                                            !Validator.equals(openId, user.getOpenId())) {
5358                                    result = null;
5359                            }
5360                    }
5361    
5362                    if (result == null) {
5363                            StringBundler query = new StringBundler(4);
5364    
5365                            query.append(_SQL_SELECT_USER_WHERE);
5366    
5367                            query.append(_FINDER_COLUMN_C_O_COMPANYID_2);
5368    
5369                            boolean bindOpenId = false;
5370    
5371                            if (openId == null) {
5372                                    query.append(_FINDER_COLUMN_C_O_OPENID_1);
5373                            }
5374                            else if (openId.equals(StringPool.BLANK)) {
5375                                    query.append(_FINDER_COLUMN_C_O_OPENID_3);
5376                            }
5377                            else {
5378                                    bindOpenId = true;
5379    
5380                                    query.append(_FINDER_COLUMN_C_O_OPENID_2);
5381                            }
5382    
5383                            String sql = query.toString();
5384    
5385                            Session session = null;
5386    
5387                            try {
5388                                    session = openSession();
5389    
5390                                    Query q = session.createQuery(sql);
5391    
5392                                    QueryPos qPos = QueryPos.getInstance(q);
5393    
5394                                    qPos.add(companyId);
5395    
5396                                    if (bindOpenId) {
5397                                            qPos.add(openId);
5398                                    }
5399    
5400                                    List<User> list = q.list();
5401    
5402                                    if (list.isEmpty()) {
5403                                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_O, finderArgs,
5404                                                    list);
5405                                    }
5406                                    else {
5407                                            if ((list.size() > 1) && _log.isWarnEnabled()) {
5408                                                    _log.warn(
5409                                                            "UserPersistenceImpl.fetchByC_O(long, String, boolean) with parameters (" +
5410                                                            StringUtil.merge(finderArgs) +
5411                                                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
5412                                            }
5413    
5414                                            User user = list.get(0);
5415    
5416                                            result = user;
5417    
5418                                            cacheResult(user);
5419    
5420                                            if ((user.getCompanyId() != companyId) ||
5421                                                            (user.getOpenId() == null) ||
5422                                                            !user.getOpenId().equals(openId)) {
5423                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_O,
5424                                                            finderArgs, user);
5425                                            }
5426                                    }
5427                            }
5428                            catch (Exception e) {
5429                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_O, finderArgs);
5430    
5431                                    throw processException(e);
5432                            }
5433                            finally {
5434                                    closeSession(session);
5435                            }
5436                    }
5437    
5438                    if (result instanceof List<?>) {
5439                            return null;
5440                    }
5441                    else {
5442                            return (User)result;
5443                    }
5444            }
5445    
5446            /**
5447             * Removes the user where companyId = &#63; and openId = &#63; from the database.
5448             *
5449             * @param companyId the company ID
5450             * @param openId the open ID
5451             * @return the user that was removed
5452             */
5453            @Override
5454            public User removeByC_O(long companyId, String openId)
5455                    throws NoSuchUserException {
5456                    User user = findByC_O(companyId, openId);
5457    
5458                    return remove(user);
5459            }
5460    
5461            /**
5462             * Returns the number of users where companyId = &#63; and openId = &#63;.
5463             *
5464             * @param companyId the company ID
5465             * @param openId the open ID
5466             * @return the number of matching users
5467             */
5468            @Override
5469            public int countByC_O(long companyId, String openId) {
5470                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_O;
5471    
5472                    Object[] finderArgs = new Object[] { companyId, openId };
5473    
5474                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
5475    
5476                    if (count == null) {
5477                            StringBundler query = new StringBundler(3);
5478    
5479                            query.append(_SQL_COUNT_USER_WHERE);
5480    
5481                            query.append(_FINDER_COLUMN_C_O_COMPANYID_2);
5482    
5483                            boolean bindOpenId = false;
5484    
5485                            if (openId == null) {
5486                                    query.append(_FINDER_COLUMN_C_O_OPENID_1);
5487                            }
5488                            else if (openId.equals(StringPool.BLANK)) {
5489                                    query.append(_FINDER_COLUMN_C_O_OPENID_3);
5490                            }
5491                            else {
5492                                    bindOpenId = true;
5493    
5494                                    query.append(_FINDER_COLUMN_C_O_OPENID_2);
5495                            }
5496    
5497                            String sql = query.toString();
5498    
5499                            Session session = null;
5500    
5501                            try {
5502                                    session = openSession();
5503    
5504                                    Query q = session.createQuery(sql);
5505    
5506                                    QueryPos qPos = QueryPos.getInstance(q);
5507    
5508                                    qPos.add(companyId);
5509    
5510                                    if (bindOpenId) {
5511                                            qPos.add(openId);
5512                                    }
5513    
5514                                    count = (Long)q.uniqueResult();
5515    
5516                                    finderCache.putResult(finderPath, finderArgs, count);
5517                            }
5518                            catch (Exception e) {
5519                                    finderCache.removeResult(finderPath, finderArgs);
5520    
5521                                    throw processException(e);
5522                            }
5523                            finally {
5524                                    closeSession(session);
5525                            }
5526                    }
5527    
5528                    return count.intValue();
5529            }
5530    
5531            private static final String _FINDER_COLUMN_C_O_COMPANYID_2 = "user.companyId = ? AND ";
5532            private static final String _FINDER_COLUMN_C_O_OPENID_1 = "user.openId IS NULL";
5533            private static final String _FINDER_COLUMN_C_O_OPENID_2 = "user.openId = ?";
5534            private static final String _FINDER_COLUMN_C_O_OPENID_3 = "(user.openId IS NULL OR user.openId = '')";
5535            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_S = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
5536                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
5537                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_S",
5538                            new String[] {
5539                                    Long.class.getName(), Integer.class.getName(),
5540                                    
5541                            Integer.class.getName(), Integer.class.getName(),
5542                                    OrderByComparator.class.getName()
5543                            });
5544            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_S = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
5545                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
5546                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_S",
5547                            new String[] { Long.class.getName(), Integer.class.getName() },
5548                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
5549                            UserModelImpl.STATUS_COLUMN_BITMASK);
5550            public static final FinderPath FINDER_PATH_COUNT_BY_C_S = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
5551                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
5552                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_S",
5553                            new String[] { Long.class.getName(), Integer.class.getName() });
5554    
5555            /**
5556             * Returns all the users where companyId = &#63; and status = &#63;.
5557             *
5558             * @param companyId the company ID
5559             * @param status the status
5560             * @return the matching users
5561             */
5562            @Override
5563            public List<User> findByC_S(long companyId, int status) {
5564                    return findByC_S(companyId, status, QueryUtil.ALL_POS,
5565                            QueryUtil.ALL_POS, null);
5566            }
5567    
5568            /**
5569             * Returns a range of all the users where companyId = &#63; and status = &#63;.
5570             *
5571             * <p>
5572             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
5573             * </p>
5574             *
5575             * @param companyId the company ID
5576             * @param status the status
5577             * @param start the lower bound of the range of users
5578             * @param end the upper bound of the range of users (not inclusive)
5579             * @return the range of matching users
5580             */
5581            @Override
5582            public List<User> findByC_S(long companyId, int status, int start, int end) {
5583                    return findByC_S(companyId, status, start, end, null);
5584            }
5585    
5586            /**
5587             * Returns an ordered range of all the users where companyId = &#63; and status = &#63;.
5588             *
5589             * <p>
5590             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
5591             * </p>
5592             *
5593             * @param companyId the company ID
5594             * @param status the status
5595             * @param start the lower bound of the range of users
5596             * @param end the upper bound of the range of users (not inclusive)
5597             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
5598             * @return the ordered range of matching users
5599             */
5600            @Override
5601            public List<User> findByC_S(long companyId, int status, int start, int end,
5602                    OrderByComparator<User> orderByComparator) {
5603                    return findByC_S(companyId, status, start, end, orderByComparator, true);
5604            }
5605    
5606            /**
5607             * Returns an ordered range of all the users where companyId = &#63; and status = &#63;.
5608             *
5609             * <p>
5610             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
5611             * </p>
5612             *
5613             * @param companyId the company ID
5614             * @param status the status
5615             * @param start the lower bound of the range of users
5616             * @param end the upper bound of the range of users (not inclusive)
5617             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
5618             * @param retrieveFromCache whether to retrieve from the finder cache
5619             * @return the ordered range of matching users
5620             */
5621            @Override
5622            public List<User> findByC_S(long companyId, int status, int start, int end,
5623                    OrderByComparator<User> orderByComparator, boolean retrieveFromCache) {
5624                    boolean pagination = true;
5625                    FinderPath finderPath = null;
5626                    Object[] finderArgs = null;
5627    
5628                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
5629                                    (orderByComparator == null)) {
5630                            pagination = false;
5631                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_S;
5632                            finderArgs = new Object[] { companyId, status };
5633                    }
5634                    else {
5635                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_S;
5636                            finderArgs = new Object[] {
5637                                            companyId, status,
5638                                            
5639                                            start, end, orderByComparator
5640                                    };
5641                    }
5642    
5643                    List<User> list = null;
5644    
5645                    if (retrieveFromCache) {
5646                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
5647                                            this);
5648    
5649                            if ((list != null) && !list.isEmpty()) {
5650                                    for (User user : list) {
5651                                            if ((companyId != user.getCompanyId()) ||
5652                                                            (status != user.getStatus())) {
5653                                                    list = null;
5654    
5655                                                    break;
5656                                            }
5657                                    }
5658                            }
5659                    }
5660    
5661                    if (list == null) {
5662                            StringBundler query = null;
5663    
5664                            if (orderByComparator != null) {
5665                                    query = new StringBundler(4 +
5666                                                    (orderByComparator.getOrderByFields().length * 2));
5667                            }
5668                            else {
5669                                    query = new StringBundler(4);
5670                            }
5671    
5672                            query.append(_SQL_SELECT_USER_WHERE);
5673    
5674                            query.append(_FINDER_COLUMN_C_S_COMPANYID_2);
5675    
5676                            query.append(_FINDER_COLUMN_C_S_STATUS_2);
5677    
5678                            if (orderByComparator != null) {
5679                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
5680                                            orderByComparator);
5681                            }
5682                            else
5683                             if (pagination) {
5684                                    query.append(UserModelImpl.ORDER_BY_JPQL);
5685                            }
5686    
5687                            String sql = query.toString();
5688    
5689                            Session session = null;
5690    
5691                            try {
5692                                    session = openSession();
5693    
5694                                    Query q = session.createQuery(sql);
5695    
5696                                    QueryPos qPos = QueryPos.getInstance(q);
5697    
5698                                    qPos.add(companyId);
5699    
5700                                    qPos.add(status);
5701    
5702                                    if (!pagination) {
5703                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
5704                                                            end, false);
5705    
5706                                            Collections.sort(list);
5707    
5708                                            list = Collections.unmodifiableList(list);
5709                                    }
5710                                    else {
5711                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
5712                                                            end);
5713                                    }
5714    
5715                                    cacheResult(list);
5716    
5717                                    finderCache.putResult(finderPath, finderArgs, list);
5718                            }
5719                            catch (Exception e) {
5720                                    finderCache.removeResult(finderPath, finderArgs);
5721    
5722                                    throw processException(e);
5723                            }
5724                            finally {
5725                                    closeSession(session);
5726                            }
5727                    }
5728    
5729                    return list;
5730            }
5731    
5732            /**
5733             * Returns the first user in the ordered set where companyId = &#63; and status = &#63;.
5734             *
5735             * @param companyId the company ID
5736             * @param status the status
5737             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
5738             * @return the first matching user
5739             * @throws NoSuchUserException if a matching user could not be found
5740             */
5741            @Override
5742            public User findByC_S_First(long companyId, int status,
5743                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
5744                    User user = fetchByC_S_First(companyId, status, orderByComparator);
5745    
5746                    if (user != null) {
5747                            return user;
5748                    }
5749    
5750                    StringBundler msg = new StringBundler(6);
5751    
5752                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
5753    
5754                    msg.append("companyId=");
5755                    msg.append(companyId);
5756    
5757                    msg.append(", status=");
5758                    msg.append(status);
5759    
5760                    msg.append(StringPool.CLOSE_CURLY_BRACE);
5761    
5762                    throw new NoSuchUserException(msg.toString());
5763            }
5764    
5765            /**
5766             * Returns the first user in the ordered set where companyId = &#63; and status = &#63;.
5767             *
5768             * @param companyId the company ID
5769             * @param status the status
5770             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
5771             * @return the first matching user, or <code>null</code> if a matching user could not be found
5772             */
5773            @Override
5774            public User fetchByC_S_First(long companyId, int status,
5775                    OrderByComparator<User> orderByComparator) {
5776                    List<User> list = findByC_S(companyId, status, 0, 1, orderByComparator);
5777    
5778                    if (!list.isEmpty()) {
5779                            return list.get(0);
5780                    }
5781    
5782                    return null;
5783            }
5784    
5785            /**
5786             * Returns the last user in the ordered set where companyId = &#63; and status = &#63;.
5787             *
5788             * @param companyId the company ID
5789             * @param status the status
5790             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
5791             * @return the last matching user
5792             * @throws NoSuchUserException if a matching user could not be found
5793             */
5794            @Override
5795            public User findByC_S_Last(long companyId, int status,
5796                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
5797                    User user = fetchByC_S_Last(companyId, status, orderByComparator);
5798    
5799                    if (user != null) {
5800                            return user;
5801                    }
5802    
5803                    StringBundler msg = new StringBundler(6);
5804    
5805                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
5806    
5807                    msg.append("companyId=");
5808                    msg.append(companyId);
5809    
5810                    msg.append(", status=");
5811                    msg.append(status);
5812    
5813                    msg.append(StringPool.CLOSE_CURLY_BRACE);
5814    
5815                    throw new NoSuchUserException(msg.toString());
5816            }
5817    
5818            /**
5819             * Returns the last user in the ordered set where companyId = &#63; and status = &#63;.
5820             *
5821             * @param companyId the company ID
5822             * @param status the status
5823             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
5824             * @return the last matching user, or <code>null</code> if a matching user could not be found
5825             */
5826            @Override
5827            public User fetchByC_S_Last(long companyId, int status,
5828                    OrderByComparator<User> orderByComparator) {
5829                    int count = countByC_S(companyId, status);
5830    
5831                    if (count == 0) {
5832                            return null;
5833                    }
5834    
5835                    List<User> list = findByC_S(companyId, status, count - 1, count,
5836                                    orderByComparator);
5837    
5838                    if (!list.isEmpty()) {
5839                            return list.get(0);
5840                    }
5841    
5842                    return null;
5843            }
5844    
5845            /**
5846             * Returns the users before and after the current user in the ordered set where companyId = &#63; and status = &#63;.
5847             *
5848             * @param userId the primary key of the current user
5849             * @param companyId the company ID
5850             * @param status the status
5851             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
5852             * @return the previous, current, and next user
5853             * @throws NoSuchUserException if a user with the primary key could not be found
5854             */
5855            @Override
5856            public User[] findByC_S_PrevAndNext(long userId, long companyId,
5857                    int status, OrderByComparator<User> orderByComparator)
5858                    throws NoSuchUserException {
5859                    User user = findByPrimaryKey(userId);
5860    
5861                    Session session = null;
5862    
5863                    try {
5864                            session = openSession();
5865    
5866                            User[] array = new UserImpl[3];
5867    
5868                            array[0] = getByC_S_PrevAndNext(session, user, companyId, status,
5869                                            orderByComparator, true);
5870    
5871                            array[1] = user;
5872    
5873                            array[2] = getByC_S_PrevAndNext(session, user, companyId, status,
5874                                            orderByComparator, false);
5875    
5876                            return array;
5877                    }
5878                    catch (Exception e) {
5879                            throw processException(e);
5880                    }
5881                    finally {
5882                            closeSession(session);
5883                    }
5884            }
5885    
5886            protected User getByC_S_PrevAndNext(Session session, User user,
5887                    long companyId, int status, OrderByComparator<User> orderByComparator,
5888                    boolean previous) {
5889                    StringBundler query = null;
5890    
5891                    if (orderByComparator != null) {
5892                            query = new StringBundler(5 +
5893                                            (orderByComparator.getOrderByConditionFields().length * 3) +
5894                                            (orderByComparator.getOrderByFields().length * 3));
5895                    }
5896                    else {
5897                            query = new StringBundler(4);
5898                    }
5899    
5900                    query.append(_SQL_SELECT_USER_WHERE);
5901    
5902                    query.append(_FINDER_COLUMN_C_S_COMPANYID_2);
5903    
5904                    query.append(_FINDER_COLUMN_C_S_STATUS_2);
5905    
5906                    if (orderByComparator != null) {
5907                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
5908    
5909                            if (orderByConditionFields.length > 0) {
5910                                    query.append(WHERE_AND);
5911                            }
5912    
5913                            for (int i = 0; i < orderByConditionFields.length; i++) {
5914                                    query.append(_ORDER_BY_ENTITY_ALIAS);
5915                                    query.append(orderByConditionFields[i]);
5916    
5917                                    if ((i + 1) < orderByConditionFields.length) {
5918                                            if (orderByComparator.isAscending() ^ previous) {
5919                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
5920                                            }
5921                                            else {
5922                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
5923                                            }
5924                                    }
5925                                    else {
5926                                            if (orderByComparator.isAscending() ^ previous) {
5927                                                    query.append(WHERE_GREATER_THAN);
5928                                            }
5929                                            else {
5930                                                    query.append(WHERE_LESSER_THAN);
5931                                            }
5932                                    }
5933                            }
5934    
5935                            query.append(ORDER_BY_CLAUSE);
5936    
5937                            String[] orderByFields = orderByComparator.getOrderByFields();
5938    
5939                            for (int i = 0; i < orderByFields.length; i++) {
5940                                    query.append(_ORDER_BY_ENTITY_ALIAS);
5941                                    query.append(orderByFields[i]);
5942    
5943                                    if ((i + 1) < orderByFields.length) {
5944                                            if (orderByComparator.isAscending() ^ previous) {
5945                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
5946                                            }
5947                                            else {
5948                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
5949                                            }
5950                                    }
5951                                    else {
5952                                            if (orderByComparator.isAscending() ^ previous) {
5953                                                    query.append(ORDER_BY_ASC);
5954                                            }
5955                                            else {
5956                                                    query.append(ORDER_BY_DESC);
5957                                            }
5958                                    }
5959                            }
5960                    }
5961                    else {
5962                            query.append(UserModelImpl.ORDER_BY_JPQL);
5963                    }
5964    
5965                    String sql = query.toString();
5966    
5967                    Query q = session.createQuery(sql);
5968    
5969                    q.setFirstResult(0);
5970                    q.setMaxResults(2);
5971    
5972                    QueryPos qPos = QueryPos.getInstance(q);
5973    
5974                    qPos.add(companyId);
5975    
5976                    qPos.add(status);
5977    
5978                    if (orderByComparator != null) {
5979                            Object[] values = orderByComparator.getOrderByConditionValues(user);
5980    
5981                            for (Object value : values) {
5982                                    qPos.add(value);
5983                            }
5984                    }
5985    
5986                    List<User> list = q.list();
5987    
5988                    if (list.size() == 2) {
5989                            return list.get(1);
5990                    }
5991                    else {
5992                            return null;
5993                    }
5994            }
5995    
5996            /**
5997             * Removes all the users where companyId = &#63; and status = &#63; from the database.
5998             *
5999             * @param companyId the company ID
6000             * @param status the status
6001             */
6002            @Override
6003            public void removeByC_S(long companyId, int status) {
6004                    for (User user : findByC_S(companyId, status, QueryUtil.ALL_POS,
6005                                    QueryUtil.ALL_POS, null)) {
6006                            remove(user);
6007                    }
6008            }
6009    
6010            /**
6011             * Returns the number of users where companyId = &#63; and status = &#63;.
6012             *
6013             * @param companyId the company ID
6014             * @param status the status
6015             * @return the number of matching users
6016             */
6017            @Override
6018            public int countByC_S(long companyId, int status) {
6019                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_S;
6020    
6021                    Object[] finderArgs = new Object[] { companyId, status };
6022    
6023                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
6024    
6025                    if (count == null) {
6026                            StringBundler query = new StringBundler(3);
6027    
6028                            query.append(_SQL_COUNT_USER_WHERE);
6029    
6030                            query.append(_FINDER_COLUMN_C_S_COMPANYID_2);
6031    
6032                            query.append(_FINDER_COLUMN_C_S_STATUS_2);
6033    
6034                            String sql = query.toString();
6035    
6036                            Session session = null;
6037    
6038                            try {
6039                                    session = openSession();
6040    
6041                                    Query q = session.createQuery(sql);
6042    
6043                                    QueryPos qPos = QueryPos.getInstance(q);
6044    
6045                                    qPos.add(companyId);
6046    
6047                                    qPos.add(status);
6048    
6049                                    count = (Long)q.uniqueResult();
6050    
6051                                    finderCache.putResult(finderPath, finderArgs, count);
6052                            }
6053                            catch (Exception e) {
6054                                    finderCache.removeResult(finderPath, finderArgs);
6055    
6056                                    throw processException(e);
6057                            }
6058                            finally {
6059                                    closeSession(session);
6060                            }
6061                    }
6062    
6063                    return count.intValue();
6064            }
6065    
6066            private static final String _FINDER_COLUMN_C_S_COMPANYID_2 = "user.companyId = ? AND ";
6067            private static final String _FINDER_COLUMN_C_S_STATUS_2 = "user.status = ?";
6068            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_CD_MD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
6069                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
6070                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_CD_MD",
6071                            new String[] {
6072                                    Long.class.getName(), Date.class.getName(), Date.class.getName(),
6073                                    
6074                            Integer.class.getName(), Integer.class.getName(),
6075                                    OrderByComparator.class.getName()
6076                            });
6077            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD_MD =
6078                    new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
6079                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
6080                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_CD_MD",
6081                            new String[] {
6082                                    Long.class.getName(), Date.class.getName(), Date.class.getName()
6083                            },
6084                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
6085                            UserModelImpl.CREATEDATE_COLUMN_BITMASK |
6086                            UserModelImpl.MODIFIEDDATE_COLUMN_BITMASK);
6087            public static final FinderPath FINDER_PATH_COUNT_BY_C_CD_MD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
6088                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
6089                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_CD_MD",
6090                            new String[] {
6091                                    Long.class.getName(), Date.class.getName(), Date.class.getName()
6092                            });
6093    
6094            /**
6095             * Returns all the users where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6096             *
6097             * @param companyId the company ID
6098             * @param createDate the create date
6099             * @param modifiedDate the modified date
6100             * @return the matching users
6101             */
6102            @Override
6103            public List<User> findByC_CD_MD(long companyId, Date createDate,
6104                    Date modifiedDate) {
6105                    return findByC_CD_MD(companyId, createDate, modifiedDate,
6106                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
6107            }
6108    
6109            /**
6110             * Returns a range of all the users where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6111             *
6112             * <p>
6113             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
6114             * </p>
6115             *
6116             * @param companyId the company ID
6117             * @param createDate the create date
6118             * @param modifiedDate the modified date
6119             * @param start the lower bound of the range of users
6120             * @param end the upper bound of the range of users (not inclusive)
6121             * @return the range of matching users
6122             */
6123            @Override
6124            public List<User> findByC_CD_MD(long companyId, Date createDate,
6125                    Date modifiedDate, int start, int end) {
6126                    return findByC_CD_MD(companyId, createDate, modifiedDate, start, end,
6127                            null);
6128            }
6129    
6130            /**
6131             * Returns an ordered range of all the users where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6132             *
6133             * <p>
6134             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
6135             * </p>
6136             *
6137             * @param companyId the company ID
6138             * @param createDate the create date
6139             * @param modifiedDate the modified date
6140             * @param start the lower bound of the range of users
6141             * @param end the upper bound of the range of users (not inclusive)
6142             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
6143             * @return the ordered range of matching users
6144             */
6145            @Override
6146            public List<User> findByC_CD_MD(long companyId, Date createDate,
6147                    Date modifiedDate, int start, int end,
6148                    OrderByComparator<User> orderByComparator) {
6149                    return findByC_CD_MD(companyId, createDate, modifiedDate, start, end,
6150                            orderByComparator, true);
6151            }
6152    
6153            /**
6154             * Returns an ordered range of all the users where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6155             *
6156             * <p>
6157             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
6158             * </p>
6159             *
6160             * @param companyId the company ID
6161             * @param createDate the create date
6162             * @param modifiedDate the modified date
6163             * @param start the lower bound of the range of users
6164             * @param end the upper bound of the range of users (not inclusive)
6165             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
6166             * @param retrieveFromCache whether to retrieve from the finder cache
6167             * @return the ordered range of matching users
6168             */
6169            @Override
6170            public List<User> findByC_CD_MD(long companyId, Date createDate,
6171                    Date modifiedDate, int start, int end,
6172                    OrderByComparator<User> orderByComparator, boolean retrieveFromCache) {
6173                    boolean pagination = true;
6174                    FinderPath finderPath = null;
6175                    Object[] finderArgs = null;
6176    
6177                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
6178                                    (orderByComparator == null)) {
6179                            pagination = false;
6180                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD_MD;
6181                            finderArgs = new Object[] { companyId, createDate, modifiedDate };
6182                    }
6183                    else {
6184                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_CD_MD;
6185                            finderArgs = new Object[] {
6186                                            companyId, createDate, modifiedDate,
6187                                            
6188                                            start, end, orderByComparator
6189                                    };
6190                    }
6191    
6192                    List<User> list = null;
6193    
6194                    if (retrieveFromCache) {
6195                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
6196                                            this);
6197    
6198                            if ((list != null) && !list.isEmpty()) {
6199                                    for (User user : list) {
6200                                            if ((companyId != user.getCompanyId()) ||
6201                                                            !Validator.equals(createDate, user.getCreateDate()) ||
6202                                                            !Validator.equals(modifiedDate,
6203                                                                    user.getModifiedDate())) {
6204                                                    list = null;
6205    
6206                                                    break;
6207                                            }
6208                                    }
6209                            }
6210                    }
6211    
6212                    if (list == null) {
6213                            StringBundler query = null;
6214    
6215                            if (orderByComparator != null) {
6216                                    query = new StringBundler(5 +
6217                                                    (orderByComparator.getOrderByFields().length * 2));
6218                            }
6219                            else {
6220                                    query = new StringBundler(5);
6221                            }
6222    
6223                            query.append(_SQL_SELECT_USER_WHERE);
6224    
6225                            query.append(_FINDER_COLUMN_C_CD_MD_COMPANYID_2);
6226    
6227                            boolean bindCreateDate = false;
6228    
6229                            if (createDate == null) {
6230                                    query.append(_FINDER_COLUMN_C_CD_MD_CREATEDATE_1);
6231                            }
6232                            else {
6233                                    bindCreateDate = true;
6234    
6235                                    query.append(_FINDER_COLUMN_C_CD_MD_CREATEDATE_2);
6236                            }
6237    
6238                            boolean bindModifiedDate = false;
6239    
6240                            if (modifiedDate == null) {
6241                                    query.append(_FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_1);
6242                            }
6243                            else {
6244                                    bindModifiedDate = true;
6245    
6246                                    query.append(_FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_2);
6247                            }
6248    
6249                            if (orderByComparator != null) {
6250                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
6251                                            orderByComparator);
6252                            }
6253                            else
6254                             if (pagination) {
6255                                    query.append(UserModelImpl.ORDER_BY_JPQL);
6256                            }
6257    
6258                            String sql = query.toString();
6259    
6260                            Session session = null;
6261    
6262                            try {
6263                                    session = openSession();
6264    
6265                                    Query q = session.createQuery(sql);
6266    
6267                                    QueryPos qPos = QueryPos.getInstance(q);
6268    
6269                                    qPos.add(companyId);
6270    
6271                                    if (bindCreateDate) {
6272                                            qPos.add(new Timestamp(createDate.getTime()));
6273                                    }
6274    
6275                                    if (bindModifiedDate) {
6276                                            qPos.add(new Timestamp(modifiedDate.getTime()));
6277                                    }
6278    
6279                                    if (!pagination) {
6280                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
6281                                                            end, false);
6282    
6283                                            Collections.sort(list);
6284    
6285                                            list = Collections.unmodifiableList(list);
6286                                    }
6287                                    else {
6288                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
6289                                                            end);
6290                                    }
6291    
6292                                    cacheResult(list);
6293    
6294                                    finderCache.putResult(finderPath, finderArgs, list);
6295                            }
6296                            catch (Exception e) {
6297                                    finderCache.removeResult(finderPath, finderArgs);
6298    
6299                                    throw processException(e);
6300                            }
6301                            finally {
6302                                    closeSession(session);
6303                            }
6304                    }
6305    
6306                    return list;
6307            }
6308    
6309            /**
6310             * Returns the first user in the ordered set where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6311             *
6312             * @param companyId the company ID
6313             * @param createDate the create date
6314             * @param modifiedDate the modified date
6315             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6316             * @return the first matching user
6317             * @throws NoSuchUserException if a matching user could not be found
6318             */
6319            @Override
6320            public User findByC_CD_MD_First(long companyId, Date createDate,
6321                    Date modifiedDate, OrderByComparator<User> orderByComparator)
6322                    throws NoSuchUserException {
6323                    User user = fetchByC_CD_MD_First(companyId, createDate, modifiedDate,
6324                                    orderByComparator);
6325    
6326                    if (user != null) {
6327                            return user;
6328                    }
6329    
6330                    StringBundler msg = new StringBundler(8);
6331    
6332                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
6333    
6334                    msg.append("companyId=");
6335                    msg.append(companyId);
6336    
6337                    msg.append(", createDate=");
6338                    msg.append(createDate);
6339    
6340                    msg.append(", modifiedDate=");
6341                    msg.append(modifiedDate);
6342    
6343                    msg.append(StringPool.CLOSE_CURLY_BRACE);
6344    
6345                    throw new NoSuchUserException(msg.toString());
6346            }
6347    
6348            /**
6349             * Returns the first user in the ordered set where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6350             *
6351             * @param companyId the company ID
6352             * @param createDate the create date
6353             * @param modifiedDate the modified date
6354             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6355             * @return the first matching user, or <code>null</code> if a matching user could not be found
6356             */
6357            @Override
6358            public User fetchByC_CD_MD_First(long companyId, Date createDate,
6359                    Date modifiedDate, OrderByComparator<User> orderByComparator) {
6360                    List<User> list = findByC_CD_MD(companyId, createDate, modifiedDate, 0,
6361                                    1, orderByComparator);
6362    
6363                    if (!list.isEmpty()) {
6364                            return list.get(0);
6365                    }
6366    
6367                    return null;
6368            }
6369    
6370            /**
6371             * Returns the last user in the ordered set where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6372             *
6373             * @param companyId the company ID
6374             * @param createDate the create date
6375             * @param modifiedDate the modified date
6376             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6377             * @return the last matching user
6378             * @throws NoSuchUserException if a matching user could not be found
6379             */
6380            @Override
6381            public User findByC_CD_MD_Last(long companyId, Date createDate,
6382                    Date modifiedDate, OrderByComparator<User> orderByComparator)
6383                    throws NoSuchUserException {
6384                    User user = fetchByC_CD_MD_Last(companyId, createDate, modifiedDate,
6385                                    orderByComparator);
6386    
6387                    if (user != null) {
6388                            return user;
6389                    }
6390    
6391                    StringBundler msg = new StringBundler(8);
6392    
6393                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
6394    
6395                    msg.append("companyId=");
6396                    msg.append(companyId);
6397    
6398                    msg.append(", createDate=");
6399                    msg.append(createDate);
6400    
6401                    msg.append(", modifiedDate=");
6402                    msg.append(modifiedDate);
6403    
6404                    msg.append(StringPool.CLOSE_CURLY_BRACE);
6405    
6406                    throw new NoSuchUserException(msg.toString());
6407            }
6408    
6409            /**
6410             * Returns the last user in the ordered set where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6411             *
6412             * @param companyId the company ID
6413             * @param createDate the create date
6414             * @param modifiedDate the modified date
6415             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6416             * @return the last matching user, or <code>null</code> if a matching user could not be found
6417             */
6418            @Override
6419            public User fetchByC_CD_MD_Last(long companyId, Date createDate,
6420                    Date modifiedDate, OrderByComparator<User> orderByComparator) {
6421                    int count = countByC_CD_MD(companyId, createDate, modifiedDate);
6422    
6423                    if (count == 0) {
6424                            return null;
6425                    }
6426    
6427                    List<User> list = findByC_CD_MD(companyId, createDate, modifiedDate,
6428                                    count - 1, count, orderByComparator);
6429    
6430                    if (!list.isEmpty()) {
6431                            return list.get(0);
6432                    }
6433    
6434                    return null;
6435            }
6436    
6437            /**
6438             * Returns the users before and after the current user in the ordered set where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6439             *
6440             * @param userId the primary key of the current user
6441             * @param companyId the company ID
6442             * @param createDate the create date
6443             * @param modifiedDate the modified date
6444             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6445             * @return the previous, current, and next user
6446             * @throws NoSuchUserException if a user with the primary key could not be found
6447             */
6448            @Override
6449            public User[] findByC_CD_MD_PrevAndNext(long userId, long companyId,
6450                    Date createDate, Date modifiedDate,
6451                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
6452                    User user = findByPrimaryKey(userId);
6453    
6454                    Session session = null;
6455    
6456                    try {
6457                            session = openSession();
6458    
6459                            User[] array = new UserImpl[3];
6460    
6461                            array[0] = getByC_CD_MD_PrevAndNext(session, user, companyId,
6462                                            createDate, modifiedDate, orderByComparator, true);
6463    
6464                            array[1] = user;
6465    
6466                            array[2] = getByC_CD_MD_PrevAndNext(session, user, companyId,
6467                                            createDate, modifiedDate, orderByComparator, false);
6468    
6469                            return array;
6470                    }
6471                    catch (Exception e) {
6472                            throw processException(e);
6473                    }
6474                    finally {
6475                            closeSession(session);
6476                    }
6477            }
6478    
6479            protected User getByC_CD_MD_PrevAndNext(Session session, User user,
6480                    long companyId, Date createDate, Date modifiedDate,
6481                    OrderByComparator<User> orderByComparator, boolean previous) {
6482                    StringBundler query = null;
6483    
6484                    if (orderByComparator != null) {
6485                            query = new StringBundler(6 +
6486                                            (orderByComparator.getOrderByConditionFields().length * 3) +
6487                                            (orderByComparator.getOrderByFields().length * 3));
6488                    }
6489                    else {
6490                            query = new StringBundler(5);
6491                    }
6492    
6493                    query.append(_SQL_SELECT_USER_WHERE);
6494    
6495                    query.append(_FINDER_COLUMN_C_CD_MD_COMPANYID_2);
6496    
6497                    boolean bindCreateDate = false;
6498    
6499                    if (createDate == null) {
6500                            query.append(_FINDER_COLUMN_C_CD_MD_CREATEDATE_1);
6501                    }
6502                    else {
6503                            bindCreateDate = true;
6504    
6505                            query.append(_FINDER_COLUMN_C_CD_MD_CREATEDATE_2);
6506                    }
6507    
6508                    boolean bindModifiedDate = false;
6509    
6510                    if (modifiedDate == null) {
6511                            query.append(_FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_1);
6512                    }
6513                    else {
6514                            bindModifiedDate = true;
6515    
6516                            query.append(_FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_2);
6517                    }
6518    
6519                    if (orderByComparator != null) {
6520                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
6521    
6522                            if (orderByConditionFields.length > 0) {
6523                                    query.append(WHERE_AND);
6524                            }
6525    
6526                            for (int i = 0; i < orderByConditionFields.length; i++) {
6527                                    query.append(_ORDER_BY_ENTITY_ALIAS);
6528                                    query.append(orderByConditionFields[i]);
6529    
6530                                    if ((i + 1) < orderByConditionFields.length) {
6531                                            if (orderByComparator.isAscending() ^ previous) {
6532                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
6533                                            }
6534                                            else {
6535                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
6536                                            }
6537                                    }
6538                                    else {
6539                                            if (orderByComparator.isAscending() ^ previous) {
6540                                                    query.append(WHERE_GREATER_THAN);
6541                                            }
6542                                            else {
6543                                                    query.append(WHERE_LESSER_THAN);
6544                                            }
6545                                    }
6546                            }
6547    
6548                            query.append(ORDER_BY_CLAUSE);
6549    
6550                            String[] orderByFields = orderByComparator.getOrderByFields();
6551    
6552                            for (int i = 0; i < orderByFields.length; i++) {
6553                                    query.append(_ORDER_BY_ENTITY_ALIAS);
6554                                    query.append(orderByFields[i]);
6555    
6556                                    if ((i + 1) < orderByFields.length) {
6557                                            if (orderByComparator.isAscending() ^ previous) {
6558                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
6559                                            }
6560                                            else {
6561                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
6562                                            }
6563                                    }
6564                                    else {
6565                                            if (orderByComparator.isAscending() ^ previous) {
6566                                                    query.append(ORDER_BY_ASC);
6567                                            }
6568                                            else {
6569                                                    query.append(ORDER_BY_DESC);
6570                                            }
6571                                    }
6572                            }
6573                    }
6574                    else {
6575                            query.append(UserModelImpl.ORDER_BY_JPQL);
6576                    }
6577    
6578                    String sql = query.toString();
6579    
6580                    Query q = session.createQuery(sql);
6581    
6582                    q.setFirstResult(0);
6583                    q.setMaxResults(2);
6584    
6585                    QueryPos qPos = QueryPos.getInstance(q);
6586    
6587                    qPos.add(companyId);
6588    
6589                    if (bindCreateDate) {
6590                            qPos.add(new Timestamp(createDate.getTime()));
6591                    }
6592    
6593                    if (bindModifiedDate) {
6594                            qPos.add(new Timestamp(modifiedDate.getTime()));
6595                    }
6596    
6597                    if (orderByComparator != null) {
6598                            Object[] values = orderByComparator.getOrderByConditionValues(user);
6599    
6600                            for (Object value : values) {
6601                                    qPos.add(value);
6602                            }
6603                    }
6604    
6605                    List<User> list = q.list();
6606    
6607                    if (list.size() == 2) {
6608                            return list.get(1);
6609                    }
6610                    else {
6611                            return null;
6612                    }
6613            }
6614    
6615            /**
6616             * Removes all the users where companyId = &#63; and createDate = &#63; and modifiedDate = &#63; from the database.
6617             *
6618             * @param companyId the company ID
6619             * @param createDate the create date
6620             * @param modifiedDate the modified date
6621             */
6622            @Override
6623            public void removeByC_CD_MD(long companyId, Date createDate,
6624                    Date modifiedDate) {
6625                    for (User user : findByC_CD_MD(companyId, createDate, modifiedDate,
6626                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
6627                            remove(user);
6628                    }
6629            }
6630    
6631            /**
6632             * Returns the number of users where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6633             *
6634             * @param companyId the company ID
6635             * @param createDate the create date
6636             * @param modifiedDate the modified date
6637             * @return the number of matching users
6638             */
6639            @Override
6640            public int countByC_CD_MD(long companyId, Date createDate, Date modifiedDate) {
6641                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_CD_MD;
6642    
6643                    Object[] finderArgs = new Object[] { companyId, createDate, modifiedDate };
6644    
6645                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
6646    
6647                    if (count == null) {
6648                            StringBundler query = new StringBundler(4);
6649    
6650                            query.append(_SQL_COUNT_USER_WHERE);
6651    
6652                            query.append(_FINDER_COLUMN_C_CD_MD_COMPANYID_2);
6653    
6654                            boolean bindCreateDate = false;
6655    
6656                            if (createDate == null) {
6657                                    query.append(_FINDER_COLUMN_C_CD_MD_CREATEDATE_1);
6658                            }
6659                            else {
6660                                    bindCreateDate = true;
6661    
6662                                    query.append(_FINDER_COLUMN_C_CD_MD_CREATEDATE_2);
6663                            }
6664    
6665                            boolean bindModifiedDate = false;
6666    
6667                            if (modifiedDate == null) {
6668                                    query.append(_FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_1);
6669                            }
6670                            else {
6671                                    bindModifiedDate = true;
6672    
6673                                    query.append(_FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_2);
6674                            }
6675    
6676                            String sql = query.toString();
6677    
6678                            Session session = null;
6679    
6680                            try {
6681                                    session = openSession();
6682    
6683                                    Query q = session.createQuery(sql);
6684    
6685                                    QueryPos qPos = QueryPos.getInstance(q);
6686    
6687                                    qPos.add(companyId);
6688    
6689                                    if (bindCreateDate) {
6690                                            qPos.add(new Timestamp(createDate.getTime()));
6691                                    }
6692    
6693                                    if (bindModifiedDate) {
6694                                            qPos.add(new Timestamp(modifiedDate.getTime()));
6695                                    }
6696    
6697                                    count = (Long)q.uniqueResult();
6698    
6699                                    finderCache.putResult(finderPath, finderArgs, count);
6700                            }
6701                            catch (Exception e) {
6702                                    finderCache.removeResult(finderPath, finderArgs);
6703    
6704                                    throw processException(e);
6705                            }
6706                            finally {
6707                                    closeSession(session);
6708                            }
6709                    }
6710    
6711                    return count.intValue();
6712            }
6713    
6714            private static final String _FINDER_COLUMN_C_CD_MD_COMPANYID_2 = "user.companyId = ? AND ";
6715            private static final String _FINDER_COLUMN_C_CD_MD_CREATEDATE_1 = "user.createDate IS NULL AND ";
6716            private static final String _FINDER_COLUMN_C_CD_MD_CREATEDATE_2 = "user.createDate = ? AND ";
6717            private static final String _FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_1 = "user.modifiedDate IS NULL";
6718            private static final String _FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_2 = "user.modifiedDate = ?";
6719            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_DU_S = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
6720                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
6721                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_DU_S",
6722                            new String[] {
6723                                    Long.class.getName(), Boolean.class.getName(),
6724                                    Integer.class.getName(),
6725                                    
6726                            Integer.class.getName(), Integer.class.getName(),
6727                                    OrderByComparator.class.getName()
6728                            });
6729            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_DU_S =
6730                    new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
6731                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
6732                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_DU_S",
6733                            new String[] {
6734                                    Long.class.getName(), Boolean.class.getName(),
6735                                    Integer.class.getName()
6736                            },
6737                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
6738                            UserModelImpl.DEFAULTUSER_COLUMN_BITMASK |
6739                            UserModelImpl.STATUS_COLUMN_BITMASK);
6740            public static final FinderPath FINDER_PATH_COUNT_BY_C_DU_S = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
6741                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
6742                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_DU_S",
6743                            new String[] {
6744                                    Long.class.getName(), Boolean.class.getName(),
6745                                    Integer.class.getName()
6746                            });
6747    
6748            /**
6749             * Returns all the users where companyId = &#63; and defaultUser = &#63; and status = &#63;.
6750             *
6751             * @param companyId the company ID
6752             * @param defaultUser the default user
6753             * @param status the status
6754             * @return the matching users
6755             */
6756            @Override
6757            public List<User> findByC_DU_S(long companyId, boolean defaultUser,
6758                    int status) {
6759                    return findByC_DU_S(companyId, defaultUser, status, QueryUtil.ALL_POS,
6760                            QueryUtil.ALL_POS, null);
6761            }
6762    
6763            /**
6764             * Returns a range of all the users where companyId = &#63; and defaultUser = &#63; and status = &#63;.
6765             *
6766             * <p>
6767             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
6768             * </p>
6769             *
6770             * @param companyId the company ID
6771             * @param defaultUser the default user
6772             * @param status the status
6773             * @param start the lower bound of the range of users
6774             * @param end the upper bound of the range of users (not inclusive)
6775             * @return the range of matching users
6776             */
6777            @Override
6778            public List<User> findByC_DU_S(long companyId, boolean defaultUser,
6779                    int status, int start, int end) {
6780                    return findByC_DU_S(companyId, defaultUser, status, start, end, null);
6781            }
6782    
6783            /**
6784             * Returns an ordered range of all the users where companyId = &#63; and defaultUser = &#63; and status = &#63;.
6785             *
6786             * <p>
6787             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
6788             * </p>
6789             *
6790             * @param companyId the company ID
6791             * @param defaultUser the default user
6792             * @param status the status
6793             * @param start the lower bound of the range of users
6794             * @param end the upper bound of the range of users (not inclusive)
6795             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
6796             * @return the ordered range of matching users
6797             */
6798            @Override
6799            public List<User> findByC_DU_S(long companyId, boolean defaultUser,
6800                    int status, int start, int end,
6801                    OrderByComparator<User> orderByComparator) {
6802                    return findByC_DU_S(companyId, defaultUser, status, start, end,
6803                            orderByComparator, true);
6804            }
6805    
6806            /**
6807             * Returns an ordered range of all the users where companyId = &#63; and defaultUser = &#63; and status = &#63;.
6808             *
6809             * <p>
6810             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
6811             * </p>
6812             *
6813             * @param companyId the company ID
6814             * @param defaultUser the default user
6815             * @param status the status
6816             * @param start the lower bound of the range of users
6817             * @param end the upper bound of the range of users (not inclusive)
6818             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
6819             * @param retrieveFromCache whether to retrieve from the finder cache
6820             * @return the ordered range of matching users
6821             */
6822            @Override
6823            public List<User> findByC_DU_S(long companyId, boolean defaultUser,
6824                    int status, int start, int end,
6825                    OrderByComparator<User> orderByComparator, boolean retrieveFromCache) {
6826                    boolean pagination = true;
6827                    FinderPath finderPath = null;
6828                    Object[] finderArgs = null;
6829    
6830                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
6831                                    (orderByComparator == null)) {
6832                            pagination = false;
6833                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_DU_S;
6834                            finderArgs = new Object[] { companyId, defaultUser, status };
6835                    }
6836                    else {
6837                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_DU_S;
6838                            finderArgs = new Object[] {
6839                                            companyId, defaultUser, status,
6840                                            
6841                                            start, end, orderByComparator
6842                                    };
6843                    }
6844    
6845                    List<User> list = null;
6846    
6847                    if (retrieveFromCache) {
6848                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
6849                                            this);
6850    
6851                            if ((list != null) && !list.isEmpty()) {
6852                                    for (User user : list) {
6853                                            if ((companyId != user.getCompanyId()) ||
6854                                                            (defaultUser != user.getDefaultUser()) ||
6855                                                            (status != user.getStatus())) {
6856                                                    list = null;
6857    
6858                                                    break;
6859                                            }
6860                                    }
6861                            }
6862                    }
6863    
6864                    if (list == null) {
6865                            StringBundler query = null;
6866    
6867                            if (orderByComparator != null) {
6868                                    query = new StringBundler(5 +
6869                                                    (orderByComparator.getOrderByFields().length * 2));
6870                            }
6871                            else {
6872                                    query = new StringBundler(5);
6873                            }
6874    
6875                            query.append(_SQL_SELECT_USER_WHERE);
6876    
6877                            query.append(_FINDER_COLUMN_C_DU_S_COMPANYID_2);
6878    
6879                            query.append(_FINDER_COLUMN_C_DU_S_DEFAULTUSER_2);
6880    
6881                            query.append(_FINDER_COLUMN_C_DU_S_STATUS_2);
6882    
6883                            if (orderByComparator != null) {
6884                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
6885                                            orderByComparator);
6886                            }
6887                            else
6888                             if (pagination) {
6889                                    query.append(UserModelImpl.ORDER_BY_JPQL);
6890                            }
6891    
6892                            String sql = query.toString();
6893    
6894                            Session session = null;
6895    
6896                            try {
6897                                    session = openSession();
6898    
6899                                    Query q = session.createQuery(sql);
6900    
6901                                    QueryPos qPos = QueryPos.getInstance(q);
6902    
6903                                    qPos.add(companyId);
6904    
6905                                    qPos.add(defaultUser);
6906    
6907                                    qPos.add(status);
6908    
6909                                    if (!pagination) {
6910                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
6911                                                            end, false);
6912    
6913                                            Collections.sort(list);
6914    
6915                                            list = Collections.unmodifiableList(list);
6916                                    }
6917                                    else {
6918                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
6919                                                            end);
6920                                    }
6921    
6922                                    cacheResult(list);
6923    
6924                                    finderCache.putResult(finderPath, finderArgs, list);
6925                            }
6926                            catch (Exception e) {
6927                                    finderCache.removeResult(finderPath, finderArgs);
6928    
6929                                    throw processException(e);
6930                            }
6931                            finally {
6932                                    closeSession(session);
6933                            }
6934                    }
6935    
6936                    return list;
6937            }
6938    
6939            /**
6940             * Returns the first user in the ordered set where companyId = &#63; and defaultUser = &#63; and status = &#63;.
6941             *
6942             * @param companyId the company ID
6943             * @param defaultUser the default user
6944             * @param status the status
6945             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6946             * @return the first matching user
6947             * @throws NoSuchUserException if a matching user could not be found
6948             */
6949            @Override
6950            public User findByC_DU_S_First(long companyId, boolean defaultUser,
6951                    int status, OrderByComparator<User> orderByComparator)
6952                    throws NoSuchUserException {
6953                    User user = fetchByC_DU_S_First(companyId, defaultUser, status,
6954                                    orderByComparator);
6955    
6956                    if (user != null) {
6957                            return user;
6958                    }
6959    
6960                    StringBundler msg = new StringBundler(8);
6961    
6962                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
6963    
6964                    msg.append("companyId=");
6965                    msg.append(companyId);
6966    
6967                    msg.append(", defaultUser=");
6968                    msg.append(defaultUser);
6969    
6970                    msg.append(", status=");
6971                    msg.append(status);
6972    
6973                    msg.append(StringPool.CLOSE_CURLY_BRACE);
6974    
6975                    throw new NoSuchUserException(msg.toString());
6976            }
6977    
6978            /**
6979             * Returns the first user in the ordered set where companyId = &#63; and defaultUser = &#63; and status = &#63;.
6980             *
6981             * @param companyId the company ID
6982             * @param defaultUser the default user
6983             * @param status the status
6984             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6985             * @return the first matching user, or <code>null</code> if a matching user could not be found
6986             */
6987            @Override
6988            public User fetchByC_DU_S_First(long companyId, boolean defaultUser,
6989                    int status, OrderByComparator<User> orderByComparator) {
6990                    List<User> list = findByC_DU_S(companyId, defaultUser, status, 0, 1,
6991                                    orderByComparator);
6992    
6993                    if (!list.isEmpty()) {
6994                            return list.get(0);
6995                    }
6996    
6997                    return null;
6998            }
6999    
7000            /**
7001             * Returns the last user in the ordered set where companyId = &#63; and defaultUser = &#63; and status = &#63;.
7002             *
7003             * @param companyId the company ID
7004             * @param defaultUser the default user
7005             * @param status the status
7006             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
7007             * @return the last matching user
7008             * @throws NoSuchUserException if a matching user could not be found
7009             */
7010            @Override
7011            public User findByC_DU_S_Last(long companyId, boolean defaultUser,
7012                    int status, OrderByComparator<User> orderByComparator)
7013                    throws NoSuchUserException {
7014                    User user = fetchByC_DU_S_Last(companyId, defaultUser, status,
7015                                    orderByComparator);
7016    
7017                    if (user != null) {
7018                            return user;
7019                    }
7020    
7021                    StringBundler msg = new StringBundler(8);
7022    
7023                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
7024    
7025                    msg.append("companyId=");
7026                    msg.append(companyId);
7027    
7028                    msg.append(", defaultUser=");
7029                    msg.append(defaultUser);
7030    
7031                    msg.append(", status=");
7032                    msg.append(status);
7033    
7034                    msg.append(StringPool.CLOSE_CURLY_BRACE);
7035    
7036                    throw new NoSuchUserException(msg.toString());
7037            }
7038    
7039            /**
7040             * Returns the last user in the ordered set where companyId = &#63; and defaultUser = &#63; and status = &#63;.
7041             *
7042             * @param companyId the company ID
7043             * @param defaultUser the default user
7044             * @param status the status
7045             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
7046             * @return the last matching user, or <code>null</code> if a matching user could not be found
7047             */
7048            @Override
7049            public User fetchByC_DU_S_Last(long companyId, boolean defaultUser,
7050                    int status, OrderByComparator<User> orderByComparator) {
7051                    int count = countByC_DU_S(companyId, defaultUser, status);
7052    
7053                    if (count == 0) {
7054                            return null;
7055                    }
7056    
7057                    List<User> list = findByC_DU_S(companyId, defaultUser, status,
7058                                    count - 1, count, orderByComparator);
7059    
7060                    if (!list.isEmpty()) {
7061                            return list.get(0);
7062                    }
7063    
7064                    return null;
7065            }
7066    
7067            /**
7068             * Returns the users before and after the current user in the ordered set where companyId = &#63; and defaultUser = &#63; and status = &#63;.
7069             *
7070             * @param userId the primary key of the current user
7071             * @param companyId the company ID
7072             * @param defaultUser the default user
7073             * @param status the status
7074             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
7075             * @return the previous, current, and next user
7076             * @throws NoSuchUserException if a user with the primary key could not be found
7077             */
7078            @Override
7079            public User[] findByC_DU_S_PrevAndNext(long userId, long companyId,
7080                    boolean defaultUser, int status,
7081                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
7082                    User user = findByPrimaryKey(userId);
7083    
7084                    Session session = null;
7085    
7086                    try {
7087                            session = openSession();
7088    
7089                            User[] array = new UserImpl[3];
7090    
7091                            array[0] = getByC_DU_S_PrevAndNext(session, user, companyId,
7092                                            defaultUser, status, orderByComparator, true);
7093    
7094                            array[1] = user;
7095    
7096                            array[2] = getByC_DU_S_PrevAndNext(session, user, companyId,
7097                                            defaultUser, status, orderByComparator, false);
7098    
7099                            return array;
7100                    }
7101                    catch (Exception e) {
7102                            throw processException(e);
7103                    }
7104                    finally {
7105                            closeSession(session);
7106                    }
7107            }
7108    
7109            protected User getByC_DU_S_PrevAndNext(Session session, User user,
7110                    long companyId, boolean defaultUser, int status,
7111                    OrderByComparator<User> orderByComparator, boolean previous) {
7112                    StringBundler query = null;
7113    
7114                    if (orderByComparator != null) {
7115                            query = new StringBundler(6 +
7116                                            (orderByComparator.getOrderByConditionFields().length * 3) +
7117                                            (orderByComparator.getOrderByFields().length * 3));
7118                    }
7119                    else {
7120                            query = new StringBundler(5);
7121                    }
7122    
7123                    query.append(_SQL_SELECT_USER_WHERE);
7124    
7125                    query.append(_FINDER_COLUMN_C_DU_S_COMPANYID_2);
7126    
7127                    query.append(_FINDER_COLUMN_C_DU_S_DEFAULTUSER_2);
7128    
7129                    query.append(_FINDER_COLUMN_C_DU_S_STATUS_2);
7130    
7131                    if (orderByComparator != null) {
7132                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
7133    
7134                            if (orderByConditionFields.length > 0) {
7135                                    query.append(WHERE_AND);
7136                            }
7137    
7138                            for (int i = 0; i < orderByConditionFields.length; i++) {
7139                                    query.append(_ORDER_BY_ENTITY_ALIAS);
7140                                    query.append(orderByConditionFields[i]);
7141    
7142                                    if ((i + 1) < orderByConditionFields.length) {
7143                                            if (orderByComparator.isAscending() ^ previous) {
7144                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
7145                                            }
7146                                            else {
7147                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
7148                                            }
7149                                    }
7150                                    else {
7151                                            if (orderByComparator.isAscending() ^ previous) {
7152                                                    query.append(WHERE_GREATER_THAN);
7153                                            }
7154                                            else {
7155                                                    query.append(WHERE_LESSER_THAN);
7156                                            }
7157                                    }
7158                            }
7159    
7160                            query.append(ORDER_BY_CLAUSE);
7161    
7162                            String[] orderByFields = orderByComparator.getOrderByFields();
7163    
7164                            for (int i = 0; i < orderByFields.length; i++) {
7165                                    query.append(_ORDER_BY_ENTITY_ALIAS);
7166                                    query.append(orderByFields[i]);
7167    
7168                                    if ((i + 1) < orderByFields.length) {
7169                                            if (orderByComparator.isAscending() ^ previous) {
7170                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
7171                                            }
7172                                            else {
7173                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
7174                                            }
7175                                    }
7176                                    else {
7177                                            if (orderByComparator.isAscending() ^ previous) {
7178                                                    query.append(ORDER_BY_ASC);
7179                                            }
7180                                            else {
7181                                                    query.append(ORDER_BY_DESC);
7182                                            }
7183                                    }
7184                            }
7185                    }
7186                    else {
7187                            query.append(UserModelImpl.ORDER_BY_JPQL);
7188                    }
7189    
7190                    String sql = query.toString();
7191    
7192                    Query q = session.createQuery(sql);
7193    
7194                    q.setFirstResult(0);
7195                    q.setMaxResults(2);
7196    
7197                    QueryPos qPos = QueryPos.getInstance(q);
7198    
7199                    qPos.add(companyId);
7200    
7201                    qPos.add(defaultUser);
7202    
7203                    qPos.add(status);
7204    
7205                    if (orderByComparator != null) {
7206                            Object[] values = orderByComparator.getOrderByConditionValues(user);
7207    
7208                            for (Object value : values) {
7209                                    qPos.add(value);
7210                            }
7211                    }
7212    
7213                    List<User> list = q.list();
7214    
7215                    if (list.size() == 2) {
7216                            return list.get(1);
7217                    }
7218                    else {
7219                            return null;
7220                    }
7221            }
7222    
7223            /**
7224             * Removes all the users where companyId = &#63; and defaultUser = &#63; and status = &#63; from the database.
7225             *
7226             * @param companyId the company ID
7227             * @param defaultUser the default user
7228             * @param status the status
7229             */
7230            @Override
7231            public void removeByC_DU_S(long companyId, boolean defaultUser, int status) {
7232                    for (User user : findByC_DU_S(companyId, defaultUser, status,
7233                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
7234                            remove(user);
7235                    }
7236            }
7237    
7238            /**
7239             * Returns the number of users where companyId = &#63; and defaultUser = &#63; and status = &#63;.
7240             *
7241             * @param companyId the company ID
7242             * @param defaultUser the default user
7243             * @param status the status
7244             * @return the number of matching users
7245             */
7246            @Override
7247            public int countByC_DU_S(long companyId, boolean defaultUser, int status) {
7248                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_DU_S;
7249    
7250                    Object[] finderArgs = new Object[] { companyId, defaultUser, status };
7251    
7252                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
7253    
7254                    if (count == null) {
7255                            StringBundler query = new StringBundler(4);
7256    
7257                            query.append(_SQL_COUNT_USER_WHERE);
7258    
7259                            query.append(_FINDER_COLUMN_C_DU_S_COMPANYID_2);
7260    
7261                            query.append(_FINDER_COLUMN_C_DU_S_DEFAULTUSER_2);
7262    
7263                            query.append(_FINDER_COLUMN_C_DU_S_STATUS_2);
7264    
7265                            String sql = query.toString();
7266    
7267                            Session session = null;
7268    
7269                            try {
7270                                    session = openSession();
7271    
7272                                    Query q = session.createQuery(sql);
7273    
7274                                    QueryPos qPos = QueryPos.getInstance(q);
7275    
7276                                    qPos.add(companyId);
7277    
7278                                    qPos.add(defaultUser);
7279    
7280                                    qPos.add(status);
7281    
7282                                    count = (Long)q.uniqueResult();
7283    
7284                                    finderCache.putResult(finderPath, finderArgs, count);
7285                            }
7286                            catch (Exception e) {
7287                                    finderCache.removeResult(finderPath, finderArgs);
7288    
7289                                    throw processException(e);
7290                            }
7291                            finally {
7292                                    closeSession(session);
7293                            }
7294                    }
7295    
7296                    return count.intValue();
7297            }
7298    
7299            private static final String _FINDER_COLUMN_C_DU_S_COMPANYID_2 = "user.companyId = ? AND ";
7300            private static final String _FINDER_COLUMN_C_DU_S_DEFAULTUSER_2 = "user.defaultUser = ? AND ";
7301            private static final String _FINDER_COLUMN_C_DU_S_STATUS_2 = "user.status = ?";
7302    
7303            public UserPersistenceImpl() {
7304                    setModelClass(User.class);
7305            }
7306    
7307            /**
7308             * Caches the user in the entity cache if it is enabled.
7309             *
7310             * @param user the user
7311             */
7312            @Override
7313            public void cacheResult(User user) {
7314                    entityCache.putResult(UserModelImpl.ENTITY_CACHE_ENABLED,
7315                            UserImpl.class, user.getPrimaryKey(), user);
7316    
7317                    finderCache.putResult(FINDER_PATH_FETCH_BY_CONTACTID,
7318                            new Object[] { user.getContactId() }, user);
7319    
7320                    finderCache.putResult(FINDER_PATH_FETCH_BY_PORTRAITID,
7321                            new Object[] { user.getPortraitId() }, user);
7322    
7323                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_U,
7324                            new Object[] { user.getCompanyId(), user.getUserId() }, user);
7325    
7326                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_DU,
7327                            new Object[] { user.getCompanyId(), user.getDefaultUser() }, user);
7328    
7329                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_SN,
7330                            new Object[] { user.getCompanyId(), user.getScreenName() }, user);
7331    
7332                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_EA,
7333                            new Object[] { user.getCompanyId(), user.getEmailAddress() }, user);
7334    
7335                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_FID,
7336                            new Object[] { user.getCompanyId(), user.getFacebookId() }, user);
7337    
7338                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_GUID,
7339                            new Object[] { user.getCompanyId(), user.getGoogleUserId() }, user);
7340    
7341                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_O,
7342                            new Object[] { user.getCompanyId(), user.getOpenId() }, user);
7343    
7344                    user.resetOriginalValues();
7345            }
7346    
7347            /**
7348             * Caches the users in the entity cache if it is enabled.
7349             *
7350             * @param users the users
7351             */
7352            @Override
7353            public void cacheResult(List<User> users) {
7354                    for (User user : users) {
7355                            if (entityCache.getResult(UserModelImpl.ENTITY_CACHE_ENABLED,
7356                                                    UserImpl.class, user.getPrimaryKey()) == null) {
7357                                    cacheResult(user);
7358                            }
7359                            else {
7360                                    user.resetOriginalValues();
7361                            }
7362                    }
7363            }
7364    
7365            /**
7366             * Clears the cache for all users.
7367             *
7368             * <p>
7369             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
7370             * </p>
7371             */
7372            @Override
7373            public void clearCache() {
7374                    entityCache.clearCache(UserImpl.class);
7375    
7376                    finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
7377                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
7378                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
7379            }
7380    
7381            /**
7382             * Clears the cache for the user.
7383             *
7384             * <p>
7385             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
7386             * </p>
7387             */
7388            @Override
7389            public void clearCache(User user) {
7390                    entityCache.removeResult(UserModelImpl.ENTITY_CACHE_ENABLED,
7391                            UserImpl.class, user.getPrimaryKey());
7392    
7393                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
7394                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
7395    
7396                    clearUniqueFindersCache((UserModelImpl)user);
7397            }
7398    
7399            @Override
7400            public void clearCache(List<User> users) {
7401                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
7402                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
7403    
7404                    for (User user : users) {
7405                            entityCache.removeResult(UserModelImpl.ENTITY_CACHE_ENABLED,
7406                                    UserImpl.class, user.getPrimaryKey());
7407    
7408                            clearUniqueFindersCache((UserModelImpl)user);
7409                    }
7410            }
7411    
7412            protected void cacheUniqueFindersCache(UserModelImpl userModelImpl,
7413                    boolean isNew) {
7414                    if (isNew) {
7415                            Object[] args = new Object[] { userModelImpl.getContactId() };
7416    
7417                            finderCache.putResult(FINDER_PATH_COUNT_BY_CONTACTID, args,
7418                                    Long.valueOf(1));
7419                            finderCache.putResult(FINDER_PATH_FETCH_BY_CONTACTID, args,
7420                                    userModelImpl);
7421    
7422                            args = new Object[] { userModelImpl.getPortraitId() };
7423    
7424                            finderCache.putResult(FINDER_PATH_COUNT_BY_PORTRAITID, args,
7425                                    Long.valueOf(1));
7426                            finderCache.putResult(FINDER_PATH_FETCH_BY_PORTRAITID, args,
7427                                    userModelImpl);
7428    
7429                            args = new Object[] {
7430                                            userModelImpl.getCompanyId(), userModelImpl.getUserId()
7431                                    };
7432    
7433                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_U, args,
7434                                    Long.valueOf(1));
7435                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_U, args, userModelImpl);
7436    
7437                            args = new Object[] {
7438                                            userModelImpl.getCompanyId(), userModelImpl.getDefaultUser()
7439                                    };
7440    
7441                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_DU, args,
7442                                    Long.valueOf(1));
7443                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_DU, args, userModelImpl);
7444    
7445                            args = new Object[] {
7446                                            userModelImpl.getCompanyId(), userModelImpl.getScreenName()
7447                                    };
7448    
7449                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_SN, args,
7450                                    Long.valueOf(1));
7451                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_SN, args, userModelImpl);
7452    
7453                            args = new Object[] {
7454                                            userModelImpl.getCompanyId(),
7455                                            userModelImpl.getEmailAddress()
7456                                    };
7457    
7458                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_EA, args,
7459                                    Long.valueOf(1));
7460                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_EA, args, userModelImpl);
7461    
7462                            args = new Object[] {
7463                                            userModelImpl.getCompanyId(), userModelImpl.getFacebookId()
7464                                    };
7465    
7466                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_FID, args,
7467                                    Long.valueOf(1));
7468                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_FID, args,
7469                                    userModelImpl);
7470    
7471                            args = new Object[] {
7472                                            userModelImpl.getCompanyId(),
7473                                            userModelImpl.getGoogleUserId()
7474                                    };
7475    
7476                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_GUID, args,
7477                                    Long.valueOf(1));
7478                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_GUID, args,
7479                                    userModelImpl);
7480    
7481                            args = new Object[] {
7482                                            userModelImpl.getCompanyId(), userModelImpl.getOpenId()
7483                                    };
7484    
7485                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_O, args,
7486                                    Long.valueOf(1));
7487                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_O, args, userModelImpl);
7488                    }
7489                    else {
7490                            if ((userModelImpl.getColumnBitmask() &
7491                                            FINDER_PATH_FETCH_BY_CONTACTID.getColumnBitmask()) != 0) {
7492                                    Object[] args = new Object[] { userModelImpl.getContactId() };
7493    
7494                                    finderCache.putResult(FINDER_PATH_COUNT_BY_CONTACTID, args,
7495                                            Long.valueOf(1));
7496                                    finderCache.putResult(FINDER_PATH_FETCH_BY_CONTACTID, args,
7497                                            userModelImpl);
7498                            }
7499    
7500                            if ((userModelImpl.getColumnBitmask() &
7501                                            FINDER_PATH_FETCH_BY_PORTRAITID.getColumnBitmask()) != 0) {
7502                                    Object[] args = new Object[] { userModelImpl.getPortraitId() };
7503    
7504                                    finderCache.putResult(FINDER_PATH_COUNT_BY_PORTRAITID, args,
7505                                            Long.valueOf(1));
7506                                    finderCache.putResult(FINDER_PATH_FETCH_BY_PORTRAITID, args,
7507                                            userModelImpl);
7508                            }
7509    
7510                            if ((userModelImpl.getColumnBitmask() &
7511                                            FINDER_PATH_FETCH_BY_C_U.getColumnBitmask()) != 0) {
7512                                    Object[] args = new Object[] {
7513                                                    userModelImpl.getCompanyId(), userModelImpl.getUserId()
7514                                            };
7515    
7516                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_U, args,
7517                                            Long.valueOf(1));
7518                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_U, args,
7519                                            userModelImpl);
7520                            }
7521    
7522                            if ((userModelImpl.getColumnBitmask() &
7523                                            FINDER_PATH_FETCH_BY_C_DU.getColumnBitmask()) != 0) {
7524                                    Object[] args = new Object[] {
7525                                                    userModelImpl.getCompanyId(),
7526                                                    userModelImpl.getDefaultUser()
7527                                            };
7528    
7529                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_DU, args,
7530                                            Long.valueOf(1));
7531                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_DU, args,
7532                                            userModelImpl);
7533                            }
7534    
7535                            if ((userModelImpl.getColumnBitmask() &
7536                                            FINDER_PATH_FETCH_BY_C_SN.getColumnBitmask()) != 0) {
7537                                    Object[] args = new Object[] {
7538                                                    userModelImpl.getCompanyId(),
7539                                                    userModelImpl.getScreenName()
7540                                            };
7541    
7542                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_SN, args,
7543                                            Long.valueOf(1));
7544                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_SN, args,
7545                                            userModelImpl);
7546                            }
7547    
7548                            if ((userModelImpl.getColumnBitmask() &
7549                                            FINDER_PATH_FETCH_BY_C_EA.getColumnBitmask()) != 0) {
7550                                    Object[] args = new Object[] {
7551                                                    userModelImpl.getCompanyId(),
7552                                                    userModelImpl.getEmailAddress()
7553                                            };
7554    
7555                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_EA, args,
7556                                            Long.valueOf(1));
7557                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_EA, args,
7558                                            userModelImpl);
7559                            }
7560    
7561                            if ((userModelImpl.getColumnBitmask() &
7562                                            FINDER_PATH_FETCH_BY_C_FID.getColumnBitmask()) != 0) {
7563                                    Object[] args = new Object[] {
7564                                                    userModelImpl.getCompanyId(),
7565                                                    userModelImpl.getFacebookId()
7566                                            };
7567    
7568                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_FID, args,
7569                                            Long.valueOf(1));
7570                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_FID, args,
7571                                            userModelImpl);
7572                            }
7573    
7574                            if ((userModelImpl.getColumnBitmask() &
7575                                            FINDER_PATH_FETCH_BY_C_GUID.getColumnBitmask()) != 0) {
7576                                    Object[] args = new Object[] {
7577                                                    userModelImpl.getCompanyId(),
7578                                                    userModelImpl.getGoogleUserId()
7579                                            };
7580    
7581                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_GUID, args,
7582                                            Long.valueOf(1));
7583                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_GUID, args,
7584                                            userModelImpl);
7585                            }
7586    
7587                            if ((userModelImpl.getColumnBitmask() &
7588                                            FINDER_PATH_FETCH_BY_C_O.getColumnBitmask()) != 0) {
7589                                    Object[] args = new Object[] {
7590                                                    userModelImpl.getCompanyId(), userModelImpl.getOpenId()
7591                                            };
7592    
7593                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_O, args,
7594                                            Long.valueOf(1));
7595                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_O, args,
7596                                            userModelImpl);
7597                            }
7598                    }
7599            }
7600    
7601            protected void clearUniqueFindersCache(UserModelImpl userModelImpl) {
7602                    Object[] args = new Object[] { userModelImpl.getContactId() };
7603    
7604                    finderCache.removeResult(FINDER_PATH_COUNT_BY_CONTACTID, args);
7605                    finderCache.removeResult(FINDER_PATH_FETCH_BY_CONTACTID, args);
7606    
7607                    if ((userModelImpl.getColumnBitmask() &
7608                                    FINDER_PATH_FETCH_BY_CONTACTID.getColumnBitmask()) != 0) {
7609                            args = new Object[] { userModelImpl.getOriginalContactId() };
7610    
7611                            finderCache.removeResult(FINDER_PATH_COUNT_BY_CONTACTID, args);
7612                            finderCache.removeResult(FINDER_PATH_FETCH_BY_CONTACTID, args);
7613                    }
7614    
7615                    args = new Object[] { userModelImpl.getPortraitId() };
7616    
7617                    finderCache.removeResult(FINDER_PATH_COUNT_BY_PORTRAITID, args);
7618                    finderCache.removeResult(FINDER_PATH_FETCH_BY_PORTRAITID, args);
7619    
7620                    if ((userModelImpl.getColumnBitmask() &
7621                                    FINDER_PATH_FETCH_BY_PORTRAITID.getColumnBitmask()) != 0) {
7622                            args = new Object[] { userModelImpl.getOriginalPortraitId() };
7623    
7624                            finderCache.removeResult(FINDER_PATH_COUNT_BY_PORTRAITID, args);
7625                            finderCache.removeResult(FINDER_PATH_FETCH_BY_PORTRAITID, args);
7626                    }
7627    
7628                    args = new Object[] {
7629                                    userModelImpl.getCompanyId(), userModelImpl.getUserId()
7630                            };
7631    
7632                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_U, args);
7633                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_U, args);
7634    
7635                    if ((userModelImpl.getColumnBitmask() &
7636                                    FINDER_PATH_FETCH_BY_C_U.getColumnBitmask()) != 0) {
7637                            args = new Object[] {
7638                                            userModelImpl.getOriginalCompanyId(),
7639                                            userModelImpl.getOriginalUserId()
7640                                    };
7641    
7642                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_U, args);
7643                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_U, args);
7644                    }
7645    
7646                    args = new Object[] {
7647                                    userModelImpl.getCompanyId(), userModelImpl.getDefaultUser()
7648                            };
7649    
7650                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_DU, args);
7651                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_DU, args);
7652    
7653                    if ((userModelImpl.getColumnBitmask() &
7654                                    FINDER_PATH_FETCH_BY_C_DU.getColumnBitmask()) != 0) {
7655                            args = new Object[] {
7656                                            userModelImpl.getOriginalCompanyId(),
7657                                            userModelImpl.getOriginalDefaultUser()
7658                                    };
7659    
7660                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_DU, args);
7661                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_DU, args);
7662                    }
7663    
7664                    args = new Object[] {
7665                                    userModelImpl.getCompanyId(), userModelImpl.getScreenName()
7666                            };
7667    
7668                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_SN, args);
7669                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_SN, args);
7670    
7671                    if ((userModelImpl.getColumnBitmask() &
7672                                    FINDER_PATH_FETCH_BY_C_SN.getColumnBitmask()) != 0) {
7673                            args = new Object[] {
7674                                            userModelImpl.getOriginalCompanyId(),
7675                                            userModelImpl.getOriginalScreenName()
7676                                    };
7677    
7678                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_SN, args);
7679                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_SN, args);
7680                    }
7681    
7682                    args = new Object[] {
7683                                    userModelImpl.getCompanyId(), userModelImpl.getEmailAddress()
7684                            };
7685    
7686                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_EA, args);
7687                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_EA, args);
7688    
7689                    if ((userModelImpl.getColumnBitmask() &
7690                                    FINDER_PATH_FETCH_BY_C_EA.getColumnBitmask()) != 0) {
7691                            args = new Object[] {
7692                                            userModelImpl.getOriginalCompanyId(),
7693                                            userModelImpl.getOriginalEmailAddress()
7694                                    };
7695    
7696                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_EA, args);
7697                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_EA, args);
7698                    }
7699    
7700                    args = new Object[] {
7701                                    userModelImpl.getCompanyId(), userModelImpl.getFacebookId()
7702                            };
7703    
7704                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_FID, args);
7705                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_FID, args);
7706    
7707                    if ((userModelImpl.getColumnBitmask() &
7708                                    FINDER_PATH_FETCH_BY_C_FID.getColumnBitmask()) != 0) {
7709                            args = new Object[] {
7710                                            userModelImpl.getOriginalCompanyId(),
7711                                            userModelImpl.getOriginalFacebookId()
7712                                    };
7713    
7714                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_FID, args);
7715                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_FID, args);
7716                    }
7717    
7718                    args = new Object[] {
7719                                    userModelImpl.getCompanyId(), userModelImpl.getGoogleUserId()
7720                            };
7721    
7722                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_GUID, args);
7723                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_GUID, args);
7724    
7725                    if ((userModelImpl.getColumnBitmask() &
7726                                    FINDER_PATH_FETCH_BY_C_GUID.getColumnBitmask()) != 0) {
7727                            args = new Object[] {
7728                                            userModelImpl.getOriginalCompanyId(),
7729                                            userModelImpl.getOriginalGoogleUserId()
7730                                    };
7731    
7732                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_GUID, args);
7733                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_GUID, args);
7734                    }
7735    
7736                    args = new Object[] {
7737                                    userModelImpl.getCompanyId(), userModelImpl.getOpenId()
7738                            };
7739    
7740                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_O, args);
7741                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_O, args);
7742    
7743                    if ((userModelImpl.getColumnBitmask() &
7744                                    FINDER_PATH_FETCH_BY_C_O.getColumnBitmask()) != 0) {
7745                            args = new Object[] {
7746                                            userModelImpl.getOriginalCompanyId(),
7747                                            userModelImpl.getOriginalOpenId()
7748                                    };
7749    
7750                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_O, args);
7751                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_O, args);
7752                    }
7753            }
7754    
7755            /**
7756             * Creates a new user with the primary key. Does not add the user to the database.
7757             *
7758             * @param userId the primary key for the new user
7759             * @return the new user
7760             */
7761            @Override
7762            public User create(long userId) {
7763                    User user = new UserImpl();
7764    
7765                    user.setNew(true);
7766                    user.setPrimaryKey(userId);
7767    
7768                    String uuid = PortalUUIDUtil.generate();
7769    
7770                    user.setUuid(uuid);
7771    
7772                    user.setCompanyId(companyProvider.getCompanyId());
7773    
7774                    return user;
7775            }
7776    
7777            /**
7778             * Removes the user with the primary key from the database. Also notifies the appropriate model listeners.
7779             *
7780             * @param userId the primary key of the user
7781             * @return the user that was removed
7782             * @throws NoSuchUserException if a user with the primary key could not be found
7783             */
7784            @Override
7785            public User remove(long userId) throws NoSuchUserException {
7786                    return remove((Serializable)userId);
7787            }
7788    
7789            /**
7790             * Removes the user with the primary key from the database. Also notifies the appropriate model listeners.
7791             *
7792             * @param primaryKey the primary key of the user
7793             * @return the user that was removed
7794             * @throws NoSuchUserException if a user with the primary key could not be found
7795             */
7796            @Override
7797            public User remove(Serializable primaryKey) throws NoSuchUserException {
7798                    Session session = null;
7799    
7800                    try {
7801                            session = openSession();
7802    
7803                            User user = (User)session.get(UserImpl.class, primaryKey);
7804    
7805                            if (user == null) {
7806                                    if (_log.isWarnEnabled()) {
7807                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
7808                                    }
7809    
7810                                    throw new NoSuchUserException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
7811                                            primaryKey);
7812                            }
7813    
7814                            return remove(user);
7815                    }
7816                    catch (NoSuchUserException nsee) {
7817                            throw nsee;
7818                    }
7819                    catch (Exception e) {
7820                            throw processException(e);
7821                    }
7822                    finally {
7823                            closeSession(session);
7824                    }
7825            }
7826    
7827            @Override
7828            protected User removeImpl(User user) {
7829                    user = toUnwrappedModel(user);
7830    
7831                    userToGroupTableMapper.deleteLeftPrimaryKeyTableMappings(user.getPrimaryKey());
7832    
7833                    userToOrganizationTableMapper.deleteLeftPrimaryKeyTableMappings(user.getPrimaryKey());
7834    
7835                    userToRoleTableMapper.deleteLeftPrimaryKeyTableMappings(user.getPrimaryKey());
7836    
7837                    userToTeamTableMapper.deleteLeftPrimaryKeyTableMappings(user.getPrimaryKey());
7838    
7839                    userToUserGroupTableMapper.deleteLeftPrimaryKeyTableMappings(user.getPrimaryKey());
7840    
7841                    Session session = null;
7842    
7843                    try {
7844                            session = openSession();
7845    
7846                            if (!session.contains(user)) {
7847                                    user = (User)session.get(UserImpl.class, user.getPrimaryKeyObj());
7848                            }
7849    
7850                            if (user != null) {
7851                                    session.delete(user);
7852                            }
7853                    }
7854                    catch (Exception e) {
7855                            throw processException(e);
7856                    }
7857                    finally {
7858                            closeSession(session);
7859                    }
7860    
7861                    if (user != null) {
7862                            clearCache(user);
7863                    }
7864    
7865                    return user;
7866            }
7867    
7868            @Override
7869            public User updateImpl(User user) {
7870                    user = toUnwrappedModel(user);
7871    
7872                    boolean isNew = user.isNew();
7873    
7874                    UserModelImpl userModelImpl = (UserModelImpl)user;
7875    
7876                    if (Validator.isNull(user.getUuid())) {
7877                            String uuid = PortalUUIDUtil.generate();
7878    
7879                            user.setUuid(uuid);
7880                    }
7881    
7882                    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
7883    
7884                    Date now = new Date();
7885    
7886                    if (isNew && (user.getCreateDate() == null)) {
7887                            if (serviceContext == null) {
7888                                    user.setCreateDate(now);
7889                            }
7890                            else {
7891                                    user.setCreateDate(serviceContext.getCreateDate(now));
7892                            }
7893                    }
7894    
7895                    if (!userModelImpl.hasSetModifiedDate()) {
7896                            if (serviceContext == null) {
7897                                    user.setModifiedDate(now);
7898                            }
7899                            else {
7900                                    user.setModifiedDate(serviceContext.getModifiedDate(now));
7901                            }
7902                    }
7903    
7904                    Session session = null;
7905    
7906                    try {
7907                            session = openSession();
7908    
7909                            if (user.isNew()) {
7910                                    session.save(user);
7911    
7912                                    user.setNew(false);
7913                            }
7914                            else {
7915                                    user = (User)session.merge(user);
7916                            }
7917                    }
7918                    catch (Exception e) {
7919                            throw processException(e);
7920                    }
7921                    finally {
7922                            closeSession(session);
7923                    }
7924    
7925                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
7926    
7927                    if (isNew || !UserModelImpl.COLUMN_BITMASK_ENABLED) {
7928                            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
7929                    }
7930    
7931                    else {
7932                            if ((userModelImpl.getColumnBitmask() &
7933                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID.getColumnBitmask()) != 0) {
7934                                    Object[] args = new Object[] { userModelImpl.getOriginalUuid() };
7935    
7936                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
7937                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
7938                                            args);
7939    
7940                                    args = new Object[] { userModelImpl.getUuid() };
7941    
7942                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
7943                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
7944                                            args);
7945                            }
7946    
7947                            if ((userModelImpl.getColumnBitmask() &
7948                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C.getColumnBitmask()) != 0) {
7949                                    Object[] args = new Object[] {
7950                                                    userModelImpl.getOriginalUuid(),
7951                                                    userModelImpl.getOriginalCompanyId()
7952                                            };
7953    
7954                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
7955                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
7956                                            args);
7957    
7958                                    args = new Object[] {
7959                                                    userModelImpl.getUuid(), userModelImpl.getCompanyId()
7960                                            };
7961    
7962                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
7963                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
7964                                            args);
7965                            }
7966    
7967                            if ((userModelImpl.getColumnBitmask() &
7968                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID.getColumnBitmask()) != 0) {
7969                                    Object[] args = new Object[] {
7970                                                    userModelImpl.getOriginalCompanyId()
7971                                            };
7972    
7973                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_COMPANYID, args);
7974                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
7975                                            args);
7976    
7977                                    args = new Object[] { userModelImpl.getCompanyId() };
7978    
7979                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_COMPANYID, args);
7980                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
7981                                            args);
7982                            }
7983    
7984                            if ((userModelImpl.getColumnBitmask() &
7985                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_EMAILADDRESS.getColumnBitmask()) != 0) {
7986                                    Object[] args = new Object[] {
7987                                                    userModelImpl.getOriginalEmailAddress()
7988                                            };
7989    
7990                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_EMAILADDRESS, args);
7991                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_EMAILADDRESS,
7992                                            args);
7993    
7994                                    args = new Object[] { userModelImpl.getEmailAddress() };
7995    
7996                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_EMAILADDRESS, args);
7997                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_EMAILADDRESS,
7998                                            args);
7999                            }
8000    
8001                            if ((userModelImpl.getColumnBitmask() &
8002                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD.getColumnBitmask()) != 0) {
8003                                    Object[] args = new Object[] {
8004                                                    userModelImpl.getOriginalCompanyId(),
8005                                                    userModelImpl.getOriginalCreateDate()
8006                                            };
8007    
8008                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_CD, args);
8009                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD,
8010                                            args);
8011    
8012                                    args = new Object[] {
8013                                                    userModelImpl.getCompanyId(),
8014                                                    userModelImpl.getCreateDate()
8015                                            };
8016    
8017                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_CD, args);
8018                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD,
8019                                            args);
8020                            }
8021    
8022                            if ((userModelImpl.getColumnBitmask() &
8023                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_MD.getColumnBitmask()) != 0) {
8024                                    Object[] args = new Object[] {
8025                                                    userModelImpl.getOriginalCompanyId(),
8026                                                    userModelImpl.getOriginalModifiedDate()
8027                                            };
8028    
8029                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_MD, args);
8030                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_MD,
8031                                            args);
8032    
8033                                    args = new Object[] {
8034                                                    userModelImpl.getCompanyId(),
8035                                                    userModelImpl.getModifiedDate()
8036                                            };
8037    
8038                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_MD, args);
8039                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_MD,
8040                                            args);
8041                            }
8042    
8043                            if ((userModelImpl.getColumnBitmask() &
8044                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_S.getColumnBitmask()) != 0) {
8045                                    Object[] args = new Object[] {
8046                                                    userModelImpl.getOriginalCompanyId(),
8047                                                    userModelImpl.getOriginalStatus()
8048                                            };
8049    
8050                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_S, args);
8051                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_S,
8052                                            args);
8053    
8054                                    args = new Object[] {
8055                                                    userModelImpl.getCompanyId(), userModelImpl.getStatus()
8056                                            };
8057    
8058                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_S, args);
8059                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_S,
8060                                            args);
8061                            }
8062    
8063                            if ((userModelImpl.getColumnBitmask() &
8064                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD_MD.getColumnBitmask()) != 0) {
8065                                    Object[] args = new Object[] {
8066                                                    userModelImpl.getOriginalCompanyId(),
8067                                                    userModelImpl.getOriginalCreateDate(),
8068                                                    userModelImpl.getOriginalModifiedDate()
8069                                            };
8070    
8071                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_CD_MD, args);
8072                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD_MD,
8073                                            args);
8074    
8075                                    args = new Object[] {
8076                                                    userModelImpl.getCompanyId(),
8077                                                    userModelImpl.getCreateDate(),
8078                                                    userModelImpl.getModifiedDate()
8079                                            };
8080    
8081                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_CD_MD, args);
8082                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD_MD,
8083                                            args);
8084                            }
8085    
8086                            if ((userModelImpl.getColumnBitmask() &
8087                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_DU_S.getColumnBitmask()) != 0) {
8088                                    Object[] args = new Object[] {
8089                                                    userModelImpl.getOriginalCompanyId(),
8090                                                    userModelImpl.getOriginalDefaultUser(),
8091                                                    userModelImpl.getOriginalStatus()
8092                                            };
8093    
8094                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_DU_S, args);
8095                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_DU_S,
8096                                            args);
8097    
8098                                    args = new Object[] {
8099                                                    userModelImpl.getCompanyId(),
8100                                                    userModelImpl.getDefaultUser(),
8101                                                    userModelImpl.getStatus()
8102                                            };
8103    
8104                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_DU_S, args);
8105                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_DU_S,
8106                                            args);
8107                            }
8108                    }
8109    
8110                    entityCache.putResult(UserModelImpl.ENTITY_CACHE_ENABLED,
8111                            UserImpl.class, user.getPrimaryKey(), user, false);
8112    
8113                    clearUniqueFindersCache(userModelImpl);
8114                    cacheUniqueFindersCache(userModelImpl, isNew);
8115    
8116                    user.resetOriginalValues();
8117    
8118                    return user;
8119            }
8120    
8121            protected User toUnwrappedModel(User user) {
8122                    if (user instanceof UserImpl) {
8123                            return user;
8124                    }
8125    
8126                    UserImpl userImpl = new UserImpl();
8127    
8128                    userImpl.setNew(user.isNew());
8129                    userImpl.setPrimaryKey(user.getPrimaryKey());
8130    
8131                    userImpl.setMvccVersion(user.getMvccVersion());
8132                    userImpl.setUuid(user.getUuid());
8133                    userImpl.setUserId(user.getUserId());
8134                    userImpl.setCompanyId(user.getCompanyId());
8135                    userImpl.setCreateDate(user.getCreateDate());
8136                    userImpl.setModifiedDate(user.getModifiedDate());
8137                    userImpl.setDefaultUser(user.isDefaultUser());
8138                    userImpl.setContactId(user.getContactId());
8139                    userImpl.setPassword(user.getPassword());
8140                    userImpl.setPasswordEncrypted(user.isPasswordEncrypted());
8141                    userImpl.setPasswordReset(user.isPasswordReset());
8142                    userImpl.setPasswordModifiedDate(user.getPasswordModifiedDate());
8143                    userImpl.setDigest(user.getDigest());
8144                    userImpl.setReminderQueryQuestion(user.getReminderQueryQuestion());
8145                    userImpl.setReminderQueryAnswer(user.getReminderQueryAnswer());
8146                    userImpl.setGraceLoginCount(user.getGraceLoginCount());
8147                    userImpl.setScreenName(user.getScreenName());
8148                    userImpl.setEmailAddress(user.getEmailAddress());
8149                    userImpl.setFacebookId(user.getFacebookId());
8150                    userImpl.setGoogleUserId(user.getGoogleUserId());
8151                    userImpl.setLdapServerId(user.getLdapServerId());
8152                    userImpl.setOpenId(user.getOpenId());
8153                    userImpl.setPortraitId(user.getPortraitId());
8154                    userImpl.setLanguageId(user.getLanguageId());
8155                    userImpl.setTimeZoneId(user.getTimeZoneId());
8156                    userImpl.setGreeting(user.getGreeting());
8157                    userImpl.setComments(user.getComments());
8158                    userImpl.setFirstName(user.getFirstName());
8159                    userImpl.setMiddleName(user.getMiddleName());
8160                    userImpl.setLastName(user.getLastName());
8161                    userImpl.setJobTitle(user.getJobTitle());
8162                    userImpl.setLoginDate(user.getLoginDate());
8163                    userImpl.setLoginIP(user.getLoginIP());
8164                    userImpl.setLastLoginDate(user.getLastLoginDate());
8165                    userImpl.setLastLoginIP(user.getLastLoginIP());
8166                    userImpl.setLastFailedLoginDate(user.getLastFailedLoginDate());
8167                    userImpl.setFailedLoginAttempts(user.getFailedLoginAttempts());
8168                    userImpl.setLockout(user.isLockout());
8169                    userImpl.setLockoutDate(user.getLockoutDate());
8170                    userImpl.setAgreedToTermsOfUse(user.isAgreedToTermsOfUse());
8171                    userImpl.setEmailAddressVerified(user.isEmailAddressVerified());
8172                    userImpl.setStatus(user.getStatus());
8173    
8174                    return userImpl;
8175            }
8176    
8177            /**
8178             * Returns the user with the primary key or throws a {@link com.liferay.portal.kernel.exception.NoSuchModelException} if it could not be found.
8179             *
8180             * @param primaryKey the primary key of the user
8181             * @return the user
8182             * @throws NoSuchUserException if a user with the primary key could not be found
8183             */
8184            @Override
8185            public User findByPrimaryKey(Serializable primaryKey)
8186                    throws NoSuchUserException {
8187                    User user = fetchByPrimaryKey(primaryKey);
8188    
8189                    if (user == null) {
8190                            if (_log.isWarnEnabled()) {
8191                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
8192                            }
8193    
8194                            throw new NoSuchUserException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
8195                                    primaryKey);
8196                    }
8197    
8198                    return user;
8199            }
8200    
8201            /**
8202             * Returns the user with the primary key or throws a {@link NoSuchUserException} if it could not be found.
8203             *
8204             * @param userId the primary key of the user
8205             * @return the user
8206             * @throws NoSuchUserException if a user with the primary key could not be found
8207             */
8208            @Override
8209            public User findByPrimaryKey(long userId) throws NoSuchUserException {
8210                    return findByPrimaryKey((Serializable)userId);
8211            }
8212    
8213            /**
8214             * Returns the user with the primary key or returns <code>null</code> if it could not be found.
8215             *
8216             * @param primaryKey the primary key of the user
8217             * @return the user, or <code>null</code> if a user with the primary key could not be found
8218             */
8219            @Override
8220            public User fetchByPrimaryKey(Serializable primaryKey) {
8221                    User user = (User)entityCache.getResult(UserModelImpl.ENTITY_CACHE_ENABLED,
8222                                    UserImpl.class, primaryKey);
8223    
8224                    if (user == _nullUser) {
8225                            return null;
8226                    }
8227    
8228                    if (user == null) {
8229                            Session session = null;
8230    
8231                            try {
8232                                    session = openSession();
8233    
8234                                    user = (User)session.get(UserImpl.class, primaryKey);
8235    
8236                                    if (user != null) {
8237                                            cacheResult(user);
8238                                    }
8239                                    else {
8240                                            entityCache.putResult(UserModelImpl.ENTITY_CACHE_ENABLED,
8241                                                    UserImpl.class, primaryKey, _nullUser);
8242                                    }
8243                            }
8244                            catch (Exception e) {
8245                                    entityCache.removeResult(UserModelImpl.ENTITY_CACHE_ENABLED,
8246                                            UserImpl.class, primaryKey);
8247    
8248                                    throw processException(e);
8249                            }
8250                            finally {
8251                                    closeSession(session);
8252                            }
8253                    }
8254    
8255                    return user;
8256            }
8257    
8258            /**
8259             * Returns the user with the primary key or returns <code>null</code> if it could not be found.
8260             *
8261             * @param userId the primary key of the user
8262             * @return the user, or <code>null</code> if a user with the primary key could not be found
8263             */
8264            @Override
8265            public User fetchByPrimaryKey(long userId) {
8266                    return fetchByPrimaryKey((Serializable)userId);
8267            }
8268    
8269            @Override
8270            public Map<Serializable, User> fetchByPrimaryKeys(
8271                    Set<Serializable> primaryKeys) {
8272                    if (primaryKeys.isEmpty()) {
8273                            return Collections.emptyMap();
8274                    }
8275    
8276                    Map<Serializable, User> map = new HashMap<Serializable, User>();
8277    
8278                    if (primaryKeys.size() == 1) {
8279                            Iterator<Serializable> iterator = primaryKeys.iterator();
8280    
8281                            Serializable primaryKey = iterator.next();
8282    
8283                            User user = fetchByPrimaryKey(primaryKey);
8284    
8285                            if (user != null) {
8286                                    map.put(primaryKey, user);
8287                            }
8288    
8289                            return map;
8290                    }
8291    
8292                    Set<Serializable> uncachedPrimaryKeys = null;
8293    
8294                    for (Serializable primaryKey : primaryKeys) {
8295                            User user = (User)entityCache.getResult(UserModelImpl.ENTITY_CACHE_ENABLED,
8296                                            UserImpl.class, primaryKey);
8297    
8298                            if (user == null) {
8299                                    if (uncachedPrimaryKeys == null) {
8300                                            uncachedPrimaryKeys = new HashSet<Serializable>();
8301                                    }
8302    
8303                                    uncachedPrimaryKeys.add(primaryKey);
8304                            }
8305                            else {
8306                                    map.put(primaryKey, user);
8307                            }
8308                    }
8309    
8310                    if (uncachedPrimaryKeys == null) {
8311                            return map;
8312                    }
8313    
8314                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
8315                                    1);
8316    
8317                    query.append(_SQL_SELECT_USER_WHERE_PKS_IN);
8318    
8319                    for (Serializable primaryKey : uncachedPrimaryKeys) {
8320                            query.append(String.valueOf(primaryKey));
8321    
8322                            query.append(StringPool.COMMA);
8323                    }
8324    
8325                    query.setIndex(query.index() - 1);
8326    
8327                    query.append(StringPool.CLOSE_PARENTHESIS);
8328    
8329                    String sql = query.toString();
8330    
8331                    Session session = null;
8332    
8333                    try {
8334                            session = openSession();
8335    
8336                            Query q = session.createQuery(sql);
8337    
8338                            for (User user : (List<User>)q.list()) {
8339                                    map.put(user.getPrimaryKeyObj(), user);
8340    
8341                                    cacheResult(user);
8342    
8343                                    uncachedPrimaryKeys.remove(user.getPrimaryKeyObj());
8344                            }
8345    
8346                            for (Serializable primaryKey : uncachedPrimaryKeys) {
8347                                    entityCache.putResult(UserModelImpl.ENTITY_CACHE_ENABLED,
8348                                            UserImpl.class, primaryKey, _nullUser);
8349                            }
8350                    }
8351                    catch (Exception e) {
8352                            throw processException(e);
8353                    }
8354                    finally {
8355                            closeSession(session);
8356                    }
8357    
8358                    return map;
8359            }
8360    
8361            /**
8362             * Returns all the users.
8363             *
8364             * @return the users
8365             */
8366            @Override
8367            public List<User> findAll() {
8368                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
8369            }
8370    
8371            /**
8372             * Returns a range of all the users.
8373             *
8374             * <p>
8375             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
8376             * </p>
8377             *
8378             * @param start the lower bound of the range of users
8379             * @param end the upper bound of the range of users (not inclusive)
8380             * @return the range of users
8381             */
8382            @Override
8383            public List<User> findAll(int start, int end) {
8384                    return findAll(start, end, null);
8385            }
8386    
8387            /**
8388             * Returns an ordered range of all the users.
8389             *
8390             * <p>
8391             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
8392             * </p>
8393             *
8394             * @param start the lower bound of the range of users
8395             * @param end the upper bound of the range of users (not inclusive)
8396             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
8397             * @return the ordered range of users
8398             */
8399            @Override
8400            public List<User> findAll(int start, int end,
8401                    OrderByComparator<User> orderByComparator) {
8402                    return findAll(start, end, orderByComparator, true);
8403            }
8404    
8405            /**
8406             * Returns an ordered range of all the users.
8407             *
8408             * <p>
8409             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
8410             * </p>
8411             *
8412             * @param start the lower bound of the range of users
8413             * @param end the upper bound of the range of users (not inclusive)
8414             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
8415             * @param retrieveFromCache whether to retrieve from the finder cache
8416             * @return the ordered range of users
8417             */
8418            @Override
8419            public List<User> findAll(int start, int end,
8420                    OrderByComparator<User> orderByComparator, boolean retrieveFromCache) {
8421                    boolean pagination = true;
8422                    FinderPath finderPath = null;
8423                    Object[] finderArgs = null;
8424    
8425                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
8426                                    (orderByComparator == null)) {
8427                            pagination = false;
8428                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
8429                            finderArgs = FINDER_ARGS_EMPTY;
8430                    }
8431                    else {
8432                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
8433                            finderArgs = new Object[] { start, end, orderByComparator };
8434                    }
8435    
8436                    List<User> list = null;
8437    
8438                    if (retrieveFromCache) {
8439                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
8440                                            this);
8441                    }
8442    
8443                    if (list == null) {
8444                            StringBundler query = null;
8445                            String sql = null;
8446    
8447                            if (orderByComparator != null) {
8448                                    query = new StringBundler(2 +
8449                                                    (orderByComparator.getOrderByFields().length * 2));
8450    
8451                                    query.append(_SQL_SELECT_USER);
8452    
8453                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
8454                                            orderByComparator);
8455    
8456                                    sql = query.toString();
8457                            }
8458                            else {
8459                                    sql = _SQL_SELECT_USER;
8460    
8461                                    if (pagination) {
8462                                            sql = sql.concat(UserModelImpl.ORDER_BY_JPQL);
8463                                    }
8464                            }
8465    
8466                            Session session = null;
8467    
8468                            try {
8469                                    session = openSession();
8470    
8471                                    Query q = session.createQuery(sql);
8472    
8473                                    if (!pagination) {
8474                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
8475                                                            end, false);
8476    
8477                                            Collections.sort(list);
8478    
8479                                            list = Collections.unmodifiableList(list);
8480                                    }
8481                                    else {
8482                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
8483                                                            end);
8484                                    }
8485    
8486                                    cacheResult(list);
8487    
8488                                    finderCache.putResult(finderPath, finderArgs, list);
8489                            }
8490                            catch (Exception e) {
8491                                    finderCache.removeResult(finderPath, finderArgs);
8492    
8493                                    throw processException(e);
8494                            }
8495                            finally {
8496                                    closeSession(session);
8497                            }
8498                    }
8499    
8500                    return list;
8501            }
8502    
8503            /**
8504             * Removes all the users from the database.
8505             *
8506             */
8507            @Override
8508            public void removeAll() {
8509                    for (User user : findAll()) {
8510                            remove(user);
8511                    }
8512            }
8513    
8514            /**
8515             * Returns the number of users.
8516             *
8517             * @return the number of users
8518             */
8519            @Override
8520            public int countAll() {
8521                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
8522                                    FINDER_ARGS_EMPTY, this);
8523    
8524                    if (count == null) {
8525                            Session session = null;
8526    
8527                            try {
8528                                    session = openSession();
8529    
8530                                    Query q = session.createQuery(_SQL_COUNT_USER);
8531    
8532                                    count = (Long)q.uniqueResult();
8533    
8534                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
8535                                            count);
8536                            }
8537                            catch (Exception e) {
8538                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
8539                                            FINDER_ARGS_EMPTY);
8540    
8541                                    throw processException(e);
8542                            }
8543                            finally {
8544                                    closeSession(session);
8545                            }
8546                    }
8547    
8548                    return count.intValue();
8549            }
8550    
8551            /**
8552             * Returns the primaryKeys of groups associated with the user.
8553             *
8554             * @param pk the primary key of the user
8555             * @return long[] of the primaryKeys of groups associated with the user
8556             */
8557            @Override
8558            public long[] getGroupPrimaryKeys(long pk) {
8559                    long[] pks = userToGroupTableMapper.getRightPrimaryKeys(pk);
8560    
8561                    return pks.clone();
8562            }
8563    
8564            /**
8565             * Returns all the groups associated with the user.
8566             *
8567             * @param pk the primary key of the user
8568             * @return the groups associated with the user
8569             */
8570            @Override
8571            public List<com.liferay.portal.kernel.model.Group> getGroups(long pk) {
8572                    return getGroups(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
8573            }
8574    
8575            /**
8576             * Returns a range of all the groups associated with the user.
8577             *
8578             * <p>
8579             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
8580             * </p>
8581             *
8582             * @param pk the primary key of the user
8583             * @param start the lower bound of the range of users
8584             * @param end the upper bound of the range of users (not inclusive)
8585             * @return the range of groups associated with the user
8586             */
8587            @Override
8588            public List<com.liferay.portal.kernel.model.Group> getGroups(long pk,
8589                    int start, int end) {
8590                    return getGroups(pk, start, end, null);
8591            }
8592    
8593            /**
8594             * Returns an ordered range of all the groups associated with the user.
8595             *
8596             * <p>
8597             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
8598             * </p>
8599             *
8600             * @param pk the primary key of the user
8601             * @param start the lower bound of the range of users
8602             * @param end the upper bound of the range of users (not inclusive)
8603             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
8604             * @return the ordered range of groups associated with the user
8605             */
8606            @Override
8607            public List<com.liferay.portal.kernel.model.Group> getGroups(long pk,
8608                    int start, int end,
8609                    OrderByComparator<com.liferay.portal.kernel.model.Group> orderByComparator) {
8610                    return userToGroupTableMapper.getRightBaseModels(pk, start, end,
8611                            orderByComparator);
8612            }
8613    
8614            /**
8615             * Returns the number of groups associated with the user.
8616             *
8617             * @param pk the primary key of the user
8618             * @return the number of groups associated with the user
8619             */
8620            @Override
8621            public int getGroupsSize(long pk) {
8622                    long[] pks = userToGroupTableMapper.getRightPrimaryKeys(pk);
8623    
8624                    return pks.length;
8625            }
8626    
8627            /**
8628             * Returns <code>true</code> if the group is associated with the user.
8629             *
8630             * @param pk the primary key of the user
8631             * @param groupPK the primary key of the group
8632             * @return <code>true</code> if the group is associated with the user; <code>false</code> otherwise
8633             */
8634            @Override
8635            public boolean containsGroup(long pk, long groupPK) {
8636                    return userToGroupTableMapper.containsTableMapping(pk, groupPK);
8637            }
8638    
8639            /**
8640             * Returns <code>true</code> if the user has any groups associated with it.
8641             *
8642             * @param pk the primary key of the user to check for associations with groups
8643             * @return <code>true</code> if the user has any groups associated with it; <code>false</code> otherwise
8644             */
8645            @Override
8646            public boolean containsGroups(long pk) {
8647                    if (getGroupsSize(pk) > 0) {
8648                            return true;
8649                    }
8650                    else {
8651                            return false;
8652                    }
8653            }
8654    
8655            /**
8656             * Adds an association between the user and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8657             *
8658             * @param pk the primary key of the user
8659             * @param groupPK the primary key of the group
8660             */
8661            @Override
8662            public void addGroup(long pk, long groupPK) {
8663                    User user = fetchByPrimaryKey(pk);
8664    
8665                    if (user == null) {
8666                            userToGroupTableMapper.addTableMapping(companyProvider.getCompanyId(),
8667                                    pk, groupPK);
8668                    }
8669                    else {
8670                            userToGroupTableMapper.addTableMapping(user.getCompanyId(), pk,
8671                                    groupPK);
8672                    }
8673            }
8674    
8675            /**
8676             * Adds an association between the user and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8677             *
8678             * @param pk the primary key of the user
8679             * @param group the group
8680             */
8681            @Override
8682            public void addGroup(long pk, com.liferay.portal.kernel.model.Group group) {
8683                    User user = fetchByPrimaryKey(pk);
8684    
8685                    if (user == null) {
8686                            userToGroupTableMapper.addTableMapping(companyProvider.getCompanyId(),
8687                                    pk, group.getPrimaryKey());
8688                    }
8689                    else {
8690                            userToGroupTableMapper.addTableMapping(user.getCompanyId(), pk,
8691                                    group.getPrimaryKey());
8692                    }
8693            }
8694    
8695            /**
8696             * Adds an association between the user and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8697             *
8698             * @param pk the primary key of the user
8699             * @param groupPKs the primary keys of the groups
8700             */
8701            @Override
8702            public void addGroups(long pk, long[] groupPKs) {
8703                    long companyId = 0;
8704    
8705                    User user = fetchByPrimaryKey(pk);
8706    
8707                    if (user == null) {
8708                            companyId = companyProvider.getCompanyId();
8709                    }
8710                    else {
8711                            companyId = user.getCompanyId();
8712                    }
8713    
8714                    for (long groupPK : groupPKs) {
8715                            userToGroupTableMapper.addTableMapping(companyId, pk, groupPK);
8716                    }
8717            }
8718    
8719            /**
8720             * Adds an association between the user and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8721             *
8722             * @param pk the primary key of the user
8723             * @param groups the groups
8724             */
8725            @Override
8726            public void addGroups(long pk,
8727                    List<com.liferay.portal.kernel.model.Group> groups) {
8728                    long companyId = 0;
8729    
8730                    User user = fetchByPrimaryKey(pk);
8731    
8732                    if (user == null) {
8733                            companyId = companyProvider.getCompanyId();
8734                    }
8735                    else {
8736                            companyId = user.getCompanyId();
8737                    }
8738    
8739                    for (com.liferay.portal.kernel.model.Group group : groups) {
8740                            userToGroupTableMapper.addTableMapping(companyId, pk,
8741                                    group.getPrimaryKey());
8742                    }
8743            }
8744    
8745            /**
8746             * Clears all associations between the user and its groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8747             *
8748             * @param pk the primary key of the user to clear the associated groups from
8749             */
8750            @Override
8751            public void clearGroups(long pk) {
8752                    userToGroupTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
8753            }
8754    
8755            /**
8756             * Removes the association between the user and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8757             *
8758             * @param pk the primary key of the user
8759             * @param groupPK the primary key of the group
8760             */
8761            @Override
8762            public void removeGroup(long pk, long groupPK) {
8763                    userToGroupTableMapper.deleteTableMapping(pk, groupPK);
8764            }
8765    
8766            /**
8767             * Removes the association between the user and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8768             *
8769             * @param pk the primary key of the user
8770             * @param group the group
8771             */
8772            @Override
8773            public void removeGroup(long pk, com.liferay.portal.kernel.model.Group group) {
8774                    userToGroupTableMapper.deleteTableMapping(pk, group.getPrimaryKey());
8775            }
8776    
8777            /**
8778             * Removes the association between the user and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8779             *
8780             * @param pk the primary key of the user
8781             * @param groupPKs the primary keys of the groups
8782             */
8783            @Override
8784            public void removeGroups(long pk, long[] groupPKs) {
8785                    for (long groupPK : groupPKs) {
8786                            userToGroupTableMapper.deleteTableMapping(pk, groupPK);
8787                    }
8788            }
8789    
8790            /**
8791             * Removes the association between the user and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8792             *
8793             * @param pk the primary key of the user
8794             * @param groups the groups
8795             */
8796            @Override
8797            public void removeGroups(long pk,
8798                    List<com.liferay.portal.kernel.model.Group> groups) {
8799                    for (com.liferay.portal.kernel.model.Group group : groups) {
8800                            userToGroupTableMapper.deleteTableMapping(pk, group.getPrimaryKey());
8801                    }
8802            }
8803    
8804            /**
8805             * Sets the groups associated with the user, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8806             *
8807             * @param pk the primary key of the user
8808             * @param groupPKs the primary keys of the groups to be associated with the user
8809             */
8810            @Override
8811            public void setGroups(long pk, long[] groupPKs) {
8812                    Set<Long> newGroupPKsSet = SetUtil.fromArray(groupPKs);
8813                    Set<Long> oldGroupPKsSet = SetUtil.fromArray(userToGroupTableMapper.getRightPrimaryKeys(
8814                                            pk));
8815    
8816                    Set<Long> removeGroupPKsSet = new HashSet<Long>(oldGroupPKsSet);
8817    
8818                    removeGroupPKsSet.removeAll(newGroupPKsSet);
8819    
8820                    for (long removeGroupPK : removeGroupPKsSet) {
8821                            userToGroupTableMapper.deleteTableMapping(pk, removeGroupPK);
8822                    }
8823    
8824                    newGroupPKsSet.removeAll(oldGroupPKsSet);
8825    
8826                    long companyId = 0;
8827    
8828                    User user = fetchByPrimaryKey(pk);
8829    
8830                    if (user == null) {
8831                            companyId = companyProvider.getCompanyId();
8832                    }
8833                    else {
8834                            companyId = user.getCompanyId();
8835                    }
8836    
8837                    for (long newGroupPK : newGroupPKsSet) {
8838                            userToGroupTableMapper.addTableMapping(companyId, pk, newGroupPK);
8839                    }
8840            }
8841    
8842            /**
8843             * Sets the groups associated with the user, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8844             *
8845             * @param pk the primary key of the user
8846             * @param groups the groups to be associated with the user
8847             */
8848            @Override
8849            public void setGroups(long pk,
8850                    List<com.liferay.portal.kernel.model.Group> groups) {
8851                    try {
8852                            long[] groupPKs = new long[groups.size()];
8853    
8854                            for (int i = 0; i < groups.size(); i++) {
8855                                    com.liferay.portal.kernel.model.Group group = groups.get(i);
8856    
8857                                    groupPKs[i] = group.getPrimaryKey();
8858                            }
8859    
8860                            setGroups(pk, groupPKs);
8861                    }
8862                    catch (Exception e) {
8863                            throw processException(e);
8864                    }
8865            }
8866    
8867            /**
8868             * Returns the primaryKeys of organizations associated with the user.
8869             *
8870             * @param pk the primary key of the user
8871             * @return long[] of the primaryKeys of organizations associated with the user
8872             */
8873            @Override
8874            public long[] getOrganizationPrimaryKeys(long pk) {
8875                    long[] pks = userToOrganizationTableMapper.getRightPrimaryKeys(pk);
8876    
8877                    return pks.clone();
8878            }
8879    
8880            /**
8881             * Returns all the organizations associated with the user.
8882             *
8883             * @param pk the primary key of the user
8884             * @return the organizations associated with the user
8885             */
8886            @Override
8887            public List<com.liferay.portal.kernel.model.Organization> getOrganizations(
8888                    long pk) {
8889                    return getOrganizations(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
8890            }
8891    
8892            /**
8893             * Returns a range of all the organizations associated with the user.
8894             *
8895             * <p>
8896             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
8897             * </p>
8898             *
8899             * @param pk the primary key of the user
8900             * @param start the lower bound of the range of users
8901             * @param end the upper bound of the range of users (not inclusive)
8902             * @return the range of organizations associated with the user
8903             */
8904            @Override
8905            public List<com.liferay.portal.kernel.model.Organization> getOrganizations(
8906                    long pk, int start, int end) {
8907                    return getOrganizations(pk, start, end, null);
8908            }
8909    
8910            /**
8911             * Returns an ordered range of all the organizations associated with the user.
8912             *
8913             * <p>
8914             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
8915             * </p>
8916             *
8917             * @param pk the primary key of the user
8918             * @param start the lower bound of the range of users
8919             * @param end the upper bound of the range of users (not inclusive)
8920             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
8921             * @return the ordered range of organizations associated with the user
8922             */
8923            @Override
8924            public List<com.liferay.portal.kernel.model.Organization> getOrganizations(
8925                    long pk, int start, int end,
8926                    OrderByComparator<com.liferay.portal.kernel.model.Organization> orderByComparator) {
8927                    return userToOrganizationTableMapper.getRightBaseModels(pk, start, end,
8928                            orderByComparator);
8929            }
8930    
8931            /**
8932             * Returns the number of organizations associated with the user.
8933             *
8934             * @param pk the primary key of the user
8935             * @return the number of organizations associated with the user
8936             */
8937            @Override
8938            public int getOrganizationsSize(long pk) {
8939                    long[] pks = userToOrganizationTableMapper.getRightPrimaryKeys(pk);
8940    
8941                    return pks.length;
8942            }
8943    
8944            /**
8945             * Returns <code>true</code> if the organization is associated with the user.
8946             *
8947             * @param pk the primary key of the user
8948             * @param organizationPK the primary key of the organization
8949             * @return <code>true</code> if the organization is associated with the user; <code>false</code> otherwise
8950             */
8951            @Override
8952            public boolean containsOrganization(long pk, long organizationPK) {
8953                    return userToOrganizationTableMapper.containsTableMapping(pk,
8954                            organizationPK);
8955            }
8956    
8957            /**
8958             * Returns <code>true</code> if the user has any organizations associated with it.
8959             *
8960             * @param pk the primary key of the user to check for associations with organizations
8961             * @return <code>true</code> if the user has any organizations associated with it; <code>false</code> otherwise
8962             */
8963            @Override
8964            public boolean containsOrganizations(long pk) {
8965                    if (getOrganizationsSize(pk) > 0) {
8966                            return true;
8967                    }
8968                    else {
8969                            return false;
8970                    }
8971            }
8972    
8973            /**
8974             * Adds an association between the user and the organization. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8975             *
8976             * @param pk the primary key of the user
8977             * @param organizationPK the primary key of the organization
8978             */
8979            @Override
8980            public void addOrganization(long pk, long organizationPK) {
8981                    User user = fetchByPrimaryKey(pk);
8982    
8983                    if (user == null) {
8984                            userToOrganizationTableMapper.addTableMapping(companyProvider.getCompanyId(),
8985                                    pk, organizationPK);
8986                    }
8987                    else {
8988                            userToOrganizationTableMapper.addTableMapping(user.getCompanyId(),
8989                                    pk, organizationPK);
8990                    }
8991            }
8992    
8993            /**
8994             * Adds an association between the user and the organization. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8995             *
8996             * @param pk the primary key of the user
8997             * @param organization the organization
8998             */
8999            @Override
9000            public void addOrganization(long pk,
9001                    com.liferay.portal.kernel.model.Organization organization) {
9002                    User user = fetchByPrimaryKey(pk);
9003    
9004                    if (user == null) {
9005                            userToOrganizationTableMapper.addTableMapping(companyProvider.getCompanyId(),
9006                                    pk, organization.getPrimaryKey());
9007                    }
9008                    else {
9009                            userToOrganizationTableMapper.addTableMapping(user.getCompanyId(),
9010                                    pk, organization.getPrimaryKey());
9011                    }
9012            }
9013    
9014            /**
9015             * Adds an association between the user and the organizations. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9016             *
9017             * @param pk the primary key of the user
9018             * @param organizationPKs the primary keys of the organizations
9019             */
9020            @Override
9021            public void addOrganizations(long pk, long[] organizationPKs) {
9022                    long companyId = 0;
9023    
9024                    User user = fetchByPrimaryKey(pk);
9025    
9026                    if (user == null) {
9027                            companyId = companyProvider.getCompanyId();
9028                    }
9029                    else {
9030                            companyId = user.getCompanyId();
9031                    }
9032    
9033                    for (long organizationPK : organizationPKs) {
9034                            userToOrganizationTableMapper.addTableMapping(companyId, pk,
9035                                    organizationPK);
9036                    }
9037            }
9038    
9039            /**
9040             * Adds an association between the user and the organizations. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9041             *
9042             * @param pk the primary key of the user
9043             * @param organizations the organizations
9044             */
9045            @Override
9046            public void addOrganizations(long pk,
9047                    List<com.liferay.portal.kernel.model.Organization> organizations) {
9048                    long companyId = 0;
9049    
9050                    User user = fetchByPrimaryKey(pk);
9051    
9052                    if (user == null) {
9053                            companyId = companyProvider.getCompanyId();
9054                    }
9055                    else {
9056                            companyId = user.getCompanyId();
9057                    }
9058    
9059                    for (com.liferay.portal.kernel.model.Organization organization : organizations) {
9060                            userToOrganizationTableMapper.addTableMapping(companyId, pk,
9061                                    organization.getPrimaryKey());
9062                    }
9063            }
9064    
9065            /**
9066             * Clears all associations between the user and its organizations. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9067             *
9068             * @param pk the primary key of the user to clear the associated organizations from
9069             */
9070            @Override
9071            public void clearOrganizations(long pk) {
9072                    userToOrganizationTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
9073            }
9074    
9075            /**
9076             * Removes the association between the user and the organization. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9077             *
9078             * @param pk the primary key of the user
9079             * @param organizationPK the primary key of the organization
9080             */
9081            @Override
9082            public void removeOrganization(long pk, long organizationPK) {
9083                    userToOrganizationTableMapper.deleteTableMapping(pk, organizationPK);
9084            }
9085    
9086            /**
9087             * Removes the association between the user and the organization. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9088             *
9089             * @param pk the primary key of the user
9090             * @param organization the organization
9091             */
9092            @Override
9093            public void removeOrganization(long pk,
9094                    com.liferay.portal.kernel.model.Organization organization) {
9095                    userToOrganizationTableMapper.deleteTableMapping(pk,
9096                            organization.getPrimaryKey());
9097            }
9098    
9099            /**
9100             * Removes the association between the user and the organizations. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9101             *
9102             * @param pk the primary key of the user
9103             * @param organizationPKs the primary keys of the organizations
9104             */
9105            @Override
9106            public void removeOrganizations(long pk, long[] organizationPKs) {
9107                    for (long organizationPK : organizationPKs) {
9108                            userToOrganizationTableMapper.deleteTableMapping(pk, organizationPK);
9109                    }
9110            }
9111    
9112            /**
9113             * Removes the association between the user and the organizations. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9114             *
9115             * @param pk the primary key of the user
9116             * @param organizations the organizations
9117             */
9118            @Override
9119            public void removeOrganizations(long pk,
9120                    List<com.liferay.portal.kernel.model.Organization> organizations) {
9121                    for (com.liferay.portal.kernel.model.Organization organization : organizations) {
9122                            userToOrganizationTableMapper.deleteTableMapping(pk,
9123                                    organization.getPrimaryKey());
9124                    }
9125            }
9126    
9127            /**
9128             * Sets the organizations associated with the user, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9129             *
9130             * @param pk the primary key of the user
9131             * @param organizationPKs the primary keys of the organizations to be associated with the user
9132             */
9133            @Override
9134            public void setOrganizations(long pk, long[] organizationPKs) {
9135                    Set<Long> newOrganizationPKsSet = SetUtil.fromArray(organizationPKs);
9136                    Set<Long> oldOrganizationPKsSet = SetUtil.fromArray(userToOrganizationTableMapper.getRightPrimaryKeys(
9137                                            pk));
9138    
9139                    Set<Long> removeOrganizationPKsSet = new HashSet<Long>(oldOrganizationPKsSet);
9140    
9141                    removeOrganizationPKsSet.removeAll(newOrganizationPKsSet);
9142    
9143                    for (long removeOrganizationPK : removeOrganizationPKsSet) {
9144                            userToOrganizationTableMapper.deleteTableMapping(pk,
9145                                    removeOrganizationPK);
9146                    }
9147    
9148                    newOrganizationPKsSet.removeAll(oldOrganizationPKsSet);
9149    
9150                    long companyId = 0;
9151    
9152                    User user = fetchByPrimaryKey(pk);
9153    
9154                    if (user == null) {
9155                            companyId = companyProvider.getCompanyId();
9156                    }
9157                    else {
9158                            companyId = user.getCompanyId();
9159                    }
9160    
9161                    for (long newOrganizationPK : newOrganizationPKsSet) {
9162                            userToOrganizationTableMapper.addTableMapping(companyId, pk,
9163                                    newOrganizationPK);
9164                    }
9165            }
9166    
9167            /**
9168             * Sets the organizations associated with the user, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9169             *
9170             * @param pk the primary key of the user
9171             * @param organizations the organizations to be associated with the user
9172             */
9173            @Override
9174            public void setOrganizations(long pk,
9175                    List<com.liferay.portal.kernel.model.Organization> organizations) {
9176                    try {
9177                            long[] organizationPKs = new long[organizations.size()];
9178    
9179                            for (int i = 0; i < organizations.size(); i++) {
9180                                    com.liferay.portal.kernel.model.Organization organization = organizations.get(i);
9181    
9182                                    organizationPKs[i] = organization.getPrimaryKey();
9183                            }
9184    
9185                            setOrganizations(pk, organizationPKs);
9186                    }
9187                    catch (Exception e) {
9188                            throw processException(e);
9189                    }
9190            }
9191    
9192            /**
9193             * Returns the primaryKeys of roles associated with the user.
9194             *
9195             * @param pk the primary key of the user
9196             * @return long[] of the primaryKeys of roles associated with the user
9197             */
9198            @Override
9199            public long[] getRolePrimaryKeys(long pk) {
9200                    long[] pks = userToRoleTableMapper.getRightPrimaryKeys(pk);
9201    
9202                    return pks.clone();
9203            }
9204    
9205            /**
9206             * Returns all the roles associated with the user.
9207             *
9208             * @param pk the primary key of the user
9209             * @return the roles associated with the user
9210             */
9211            @Override
9212            public List<com.liferay.portal.kernel.model.Role> getRoles(long pk) {
9213                    return getRoles(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
9214            }
9215    
9216            /**
9217             * Returns a range of all the roles associated with the user.
9218             *
9219             * <p>
9220             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
9221             * </p>
9222             *
9223             * @param pk the primary key of the user
9224             * @param start the lower bound of the range of users
9225             * @param end the upper bound of the range of users (not inclusive)
9226             * @return the range of roles associated with the user
9227             */
9228            @Override
9229            public List<com.liferay.portal.kernel.model.Role> getRoles(long pk,
9230                    int start, int end) {
9231                    return getRoles(pk, start, end, null);
9232            }
9233    
9234            /**
9235             * Returns an ordered range of all the roles associated with the user.
9236             *
9237             * <p>
9238             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
9239             * </p>
9240             *
9241             * @param pk the primary key of the user
9242             * @param start the lower bound of the range of users
9243             * @param end the upper bound of the range of users (not inclusive)
9244             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
9245             * @return the ordered range of roles associated with the user
9246             */
9247            @Override
9248            public List<com.liferay.portal.kernel.model.Role> getRoles(long pk,
9249                    int start, int end,
9250                    OrderByComparator<com.liferay.portal.kernel.model.Role> orderByComparator) {
9251                    return userToRoleTableMapper.getRightBaseModels(pk, start, end,
9252                            orderByComparator);
9253            }
9254    
9255            /**
9256             * Returns the number of roles associated with the user.
9257             *
9258             * @param pk the primary key of the user
9259             * @return the number of roles associated with the user
9260             */
9261            @Override
9262            public int getRolesSize(long pk) {
9263                    long[] pks = userToRoleTableMapper.getRightPrimaryKeys(pk);
9264    
9265                    return pks.length;
9266            }
9267    
9268            /**
9269             * Returns <code>true</code> if the role is associated with the user.
9270             *
9271             * @param pk the primary key of the user
9272             * @param rolePK the primary key of the role
9273             * @return <code>true</code> if the role is associated with the user; <code>false</code> otherwise
9274             */
9275            @Override
9276            public boolean containsRole(long pk, long rolePK) {
9277                    return userToRoleTableMapper.containsTableMapping(pk, rolePK);
9278            }
9279    
9280            /**
9281             * Returns <code>true</code> if the user has any roles associated with it.
9282             *
9283             * @param pk the primary key of the user to check for associations with roles
9284             * @return <code>true</code> if the user has any roles associated with it; <code>false</code> otherwise
9285             */
9286            @Override
9287            public boolean containsRoles(long pk) {
9288                    if (getRolesSize(pk) > 0) {
9289                            return true;
9290                    }
9291                    else {
9292                            return false;
9293                    }
9294            }
9295    
9296            /**
9297             * Adds an association between the user and the role. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9298             *
9299             * @param pk the primary key of the user
9300             * @param rolePK the primary key of the role
9301             */
9302            @Override
9303            public void addRole(long pk, long rolePK) {
9304                    User user = fetchByPrimaryKey(pk);
9305    
9306                    if (user == null) {
9307                            userToRoleTableMapper.addTableMapping(companyProvider.getCompanyId(),
9308                                    pk, rolePK);
9309                    }
9310                    else {
9311                            userToRoleTableMapper.addTableMapping(user.getCompanyId(), pk,
9312                                    rolePK);
9313                    }
9314            }
9315    
9316            /**
9317             * Adds an association between the user and the role. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9318             *
9319             * @param pk the primary key of the user
9320             * @param role the role
9321             */
9322            @Override
9323            public void addRole(long pk, com.liferay.portal.kernel.model.Role role) {
9324                    User user = fetchByPrimaryKey(pk);
9325    
9326                    if (user == null) {
9327                            userToRoleTableMapper.addTableMapping(companyProvider.getCompanyId(),
9328                                    pk, role.getPrimaryKey());
9329                    }
9330                    else {
9331                            userToRoleTableMapper.addTableMapping(user.getCompanyId(), pk,
9332                                    role.getPrimaryKey());
9333                    }
9334            }
9335    
9336            /**
9337             * Adds an association between the user and the roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9338             *
9339             * @param pk the primary key of the user
9340             * @param rolePKs the primary keys of the roles
9341             */
9342            @Override
9343            public void addRoles(long pk, long[] rolePKs) {
9344                    long companyId = 0;
9345    
9346                    User user = fetchByPrimaryKey(pk);
9347    
9348                    if (user == null) {
9349                            companyId = companyProvider.getCompanyId();
9350                    }
9351                    else {
9352                            companyId = user.getCompanyId();
9353                    }
9354    
9355                    for (long rolePK : rolePKs) {
9356                            userToRoleTableMapper.addTableMapping(companyId, pk, rolePK);
9357                    }
9358            }
9359    
9360            /**
9361             * Adds an association between the user and the roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9362             *
9363             * @param pk the primary key of the user
9364             * @param roles the roles
9365             */
9366            @Override
9367            public void addRoles(long pk,
9368                    List<com.liferay.portal.kernel.model.Role> roles) {
9369                    long companyId = 0;
9370    
9371                    User user = fetchByPrimaryKey(pk);
9372    
9373                    if (user == null) {
9374                            companyId = companyProvider.getCompanyId();
9375                    }
9376                    else {
9377                            companyId = user.getCompanyId();
9378                    }
9379    
9380                    for (com.liferay.portal.kernel.model.Role role : roles) {
9381                            userToRoleTableMapper.addTableMapping(companyId, pk,
9382                                    role.getPrimaryKey());
9383                    }
9384            }
9385    
9386            /**
9387             * Clears all associations between the user and its roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9388             *
9389             * @param pk the primary key of the user to clear the associated roles from
9390             */
9391            @Override
9392            public void clearRoles(long pk) {
9393                    userToRoleTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
9394            }
9395    
9396            /**
9397             * Removes the association between the user and the role. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9398             *
9399             * @param pk the primary key of the user
9400             * @param rolePK the primary key of the role
9401             */
9402            @Override
9403            public void removeRole(long pk, long rolePK) {
9404                    userToRoleTableMapper.deleteTableMapping(pk, rolePK);
9405            }
9406    
9407            /**
9408             * Removes the association between the user and the role. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9409             *
9410             * @param pk the primary key of the user
9411             * @param role the role
9412             */
9413            @Override
9414            public void removeRole(long pk, com.liferay.portal.kernel.model.Role role) {
9415                    userToRoleTableMapper.deleteTableMapping(pk, role.getPrimaryKey());
9416            }
9417    
9418            /**
9419             * Removes the association between the user and the roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9420             *
9421             * @param pk the primary key of the user
9422             * @param rolePKs the primary keys of the roles
9423             */
9424            @Override
9425            public void removeRoles(long pk, long[] rolePKs) {
9426                    for (long rolePK : rolePKs) {
9427                            userToRoleTableMapper.deleteTableMapping(pk, rolePK);
9428                    }
9429            }
9430    
9431            /**
9432             * Removes the association between the user and the roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9433             *
9434             * @param pk the primary key of the user
9435             * @param roles the roles
9436             */
9437            @Override
9438            public void removeRoles(long pk,
9439                    List<com.liferay.portal.kernel.model.Role> roles) {
9440                    for (com.liferay.portal.kernel.model.Role role : roles) {
9441                            userToRoleTableMapper.deleteTableMapping(pk, role.getPrimaryKey());
9442                    }
9443            }
9444    
9445            /**
9446             * Sets the roles associated with the user, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9447             *
9448             * @param pk the primary key of the user
9449             * @param rolePKs the primary keys of the roles to be associated with the user
9450             */
9451            @Override
9452            public void setRoles(long pk, long[] rolePKs) {
9453                    Set<Long> newRolePKsSet = SetUtil.fromArray(rolePKs);
9454                    Set<Long> oldRolePKsSet = SetUtil.fromArray(userToRoleTableMapper.getRightPrimaryKeys(
9455                                            pk));
9456    
9457                    Set<Long> removeRolePKsSet = new HashSet<Long>(oldRolePKsSet);
9458    
9459                    removeRolePKsSet.removeAll(newRolePKsSet);
9460    
9461                    for (long removeRolePK : removeRolePKsSet) {
9462                            userToRoleTableMapper.deleteTableMapping(pk, removeRolePK);
9463                    }
9464    
9465                    newRolePKsSet.removeAll(oldRolePKsSet);
9466    
9467                    long companyId = 0;
9468    
9469                    User user = fetchByPrimaryKey(pk);
9470    
9471                    if (user == null) {
9472                            companyId = companyProvider.getCompanyId();
9473                    }
9474                    else {
9475                            companyId = user.getCompanyId();
9476                    }
9477    
9478                    for (long newRolePK : newRolePKsSet) {
9479                            userToRoleTableMapper.addTableMapping(companyId, pk, newRolePK);
9480                    }
9481            }
9482    
9483            /**
9484             * Sets the roles associated with the user, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9485             *
9486             * @param pk the primary key of the user
9487             * @param roles the roles to be associated with the user
9488             */
9489            @Override
9490            public void setRoles(long pk,
9491                    List<com.liferay.portal.kernel.model.Role> roles) {
9492                    try {
9493                            long[] rolePKs = new long[roles.size()];
9494    
9495                            for (int i = 0; i < roles.size(); i++) {
9496                                    com.liferay.portal.kernel.model.Role role = roles.get(i);
9497    
9498                                    rolePKs[i] = role.getPrimaryKey();
9499                            }
9500    
9501                            setRoles(pk, rolePKs);
9502                    }
9503                    catch (Exception e) {
9504                            throw processException(e);
9505                    }
9506            }
9507    
9508            /**
9509             * Returns the primaryKeys of teams associated with the user.
9510             *
9511             * @param pk the primary key of the user
9512             * @return long[] of the primaryKeys of teams associated with the user
9513             */
9514            @Override
9515            public long[] getTeamPrimaryKeys(long pk) {
9516                    long[] pks = userToTeamTableMapper.getRightPrimaryKeys(pk);
9517    
9518                    return pks.clone();
9519            }
9520    
9521            /**
9522             * Returns all the teams associated with the user.
9523             *
9524             * @param pk the primary key of the user
9525             * @return the teams associated with the user
9526             */
9527            @Override
9528            public List<com.liferay.portal.kernel.model.Team> getTeams(long pk) {
9529                    return getTeams(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
9530            }
9531    
9532            /**
9533             * Returns a range of all the teams associated with the user.
9534             *
9535             * <p>
9536             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
9537             * </p>
9538             *
9539             * @param pk the primary key of the user
9540             * @param start the lower bound of the range of users
9541             * @param end the upper bound of the range of users (not inclusive)
9542             * @return the range of teams associated with the user
9543             */
9544            @Override
9545            public List<com.liferay.portal.kernel.model.Team> getTeams(long pk,
9546                    int start, int end) {
9547                    return getTeams(pk, start, end, null);
9548            }
9549    
9550            /**
9551             * Returns an ordered range of all the teams associated with the user.
9552             *
9553             * <p>
9554             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
9555             * </p>
9556             *
9557             * @param pk the primary key of the user
9558             * @param start the lower bound of the range of users
9559             * @param end the upper bound of the range of users (not inclusive)
9560             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
9561             * @return the ordered range of teams associated with the user
9562             */
9563            @Override
9564            public List<com.liferay.portal.kernel.model.Team> getTeams(long pk,
9565                    int start, int end,
9566                    OrderByComparator<com.liferay.portal.kernel.model.Team> orderByComparator) {
9567                    return userToTeamTableMapper.getRightBaseModels(pk, start, end,
9568                            orderByComparator);
9569            }
9570    
9571            /**
9572             * Returns the number of teams associated with the user.
9573             *
9574             * @param pk the primary key of the user
9575             * @return the number of teams associated with the user
9576             */
9577            @Override
9578            public int getTeamsSize(long pk) {
9579                    long[] pks = userToTeamTableMapper.getRightPrimaryKeys(pk);
9580    
9581                    return pks.length;
9582            }
9583    
9584            /**
9585             * Returns <code>true</code> if the team is associated with the user.
9586             *
9587             * @param pk the primary key of the user
9588             * @param teamPK the primary key of the team
9589             * @return <code>true</code> if the team is associated with the user; <code>false</code> otherwise
9590             */
9591            @Override
9592            public boolean containsTeam(long pk, long teamPK) {
9593                    return userToTeamTableMapper.containsTableMapping(pk, teamPK);
9594            }
9595    
9596            /**
9597             * Returns <code>true</code> if the user has any teams associated with it.
9598             *
9599             * @param pk the primary key of the user to check for associations with teams
9600             * @return <code>true</code> if the user has any teams associated with it; <code>false</code> otherwise
9601             */
9602            @Override
9603            public boolean containsTeams(long pk) {
9604                    if (getTeamsSize(pk) > 0) {
9605                            return true;
9606                    }
9607                    else {
9608                            return false;
9609                    }
9610            }
9611    
9612            /**
9613             * Adds an association between the user and the team. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9614             *
9615             * @param pk the primary key of the user
9616             * @param teamPK the primary key of the team
9617             */
9618            @Override
9619            public void addTeam(long pk, long teamPK) {
9620                    User user = fetchByPrimaryKey(pk);
9621    
9622                    if (user == null) {
9623                            userToTeamTableMapper.addTableMapping(companyProvider.getCompanyId(),
9624                                    pk, teamPK);
9625                    }
9626                    else {
9627                            userToTeamTableMapper.addTableMapping(user.getCompanyId(), pk,
9628                                    teamPK);
9629                    }
9630            }
9631    
9632            /**
9633             * Adds an association between the user and the team. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9634             *
9635             * @param pk the primary key of the user
9636             * @param team the team
9637             */
9638            @Override
9639            public void addTeam(long pk, com.liferay.portal.kernel.model.Team team) {
9640                    User user = fetchByPrimaryKey(pk);
9641    
9642                    if (user == null) {
9643                            userToTeamTableMapper.addTableMapping(companyProvider.getCompanyId(),
9644                                    pk, team.getPrimaryKey());
9645                    }
9646                    else {
9647                            userToTeamTableMapper.addTableMapping(user.getCompanyId(), pk,
9648                                    team.getPrimaryKey());
9649                    }
9650            }
9651    
9652            /**
9653             * Adds an association between the user and the teams. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9654             *
9655             * @param pk the primary key of the user
9656             * @param teamPKs the primary keys of the teams
9657             */
9658            @Override
9659            public void addTeams(long pk, long[] teamPKs) {
9660                    long companyId = 0;
9661    
9662                    User user = fetchByPrimaryKey(pk);
9663    
9664                    if (user == null) {
9665                            companyId = companyProvider.getCompanyId();
9666                    }
9667                    else {
9668                            companyId = user.getCompanyId();
9669                    }
9670    
9671                    for (long teamPK : teamPKs) {
9672                            userToTeamTableMapper.addTableMapping(companyId, pk, teamPK);
9673                    }
9674            }
9675    
9676            /**
9677             * Adds an association between the user and the teams. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9678             *
9679             * @param pk the primary key of the user
9680             * @param teams the teams
9681             */
9682            @Override
9683            public void addTeams(long pk,
9684                    List<com.liferay.portal.kernel.model.Team> teams) {
9685                    long companyId = 0;
9686    
9687                    User user = fetchByPrimaryKey(pk);
9688    
9689                    if (user == null) {
9690                            companyId = companyProvider.getCompanyId();
9691                    }
9692                    else {
9693                            companyId = user.getCompanyId();
9694                    }
9695    
9696                    for (com.liferay.portal.kernel.model.Team team : teams) {
9697                            userToTeamTableMapper.addTableMapping(companyId, pk,
9698                                    team.getPrimaryKey());
9699                    }
9700            }
9701    
9702            /**
9703             * Clears all associations between the user and its teams. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9704             *
9705             * @param pk the primary key of the user to clear the associated teams from
9706             */
9707            @Override
9708            public void clearTeams(long pk) {
9709                    userToTeamTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
9710            }
9711    
9712            /**
9713             * Removes the association between the user and the team. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9714             *
9715             * @param pk the primary key of the user
9716             * @param teamPK the primary key of the team
9717             */
9718            @Override
9719            public void removeTeam(long pk, long teamPK) {
9720                    userToTeamTableMapper.deleteTableMapping(pk, teamPK);
9721            }
9722    
9723            /**
9724             * Removes the association between the user and the team. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9725             *
9726             * @param pk the primary key of the user
9727             * @param team the team
9728             */
9729            @Override
9730            public void removeTeam(long pk, com.liferay.portal.kernel.model.Team team) {
9731                    userToTeamTableMapper.deleteTableMapping(pk, team.getPrimaryKey());
9732            }
9733    
9734            /**
9735             * Removes the association between the user and the teams. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9736             *
9737             * @param pk the primary key of the user
9738             * @param teamPKs the primary keys of the teams
9739             */
9740            @Override
9741            public void removeTeams(long pk, long[] teamPKs) {
9742                    for (long teamPK : teamPKs) {
9743                            userToTeamTableMapper.deleteTableMapping(pk, teamPK);
9744                    }
9745            }
9746    
9747            /**
9748             * Removes the association between the user and the teams. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9749             *
9750             * @param pk the primary key of the user
9751             * @param teams the teams
9752             */
9753            @Override
9754            public void removeTeams(long pk,
9755                    List<com.liferay.portal.kernel.model.Team> teams) {
9756                    for (com.liferay.portal.kernel.model.Team team : teams) {
9757                            userToTeamTableMapper.deleteTableMapping(pk, team.getPrimaryKey());
9758                    }
9759            }
9760    
9761            /**
9762             * Sets the teams associated with the user, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9763             *
9764             * @param pk the primary key of the user
9765             * @param teamPKs the primary keys of the teams to be associated with the user
9766             */
9767            @Override
9768            public void setTeams(long pk, long[] teamPKs) {
9769                    Set<Long> newTeamPKsSet = SetUtil.fromArray(teamPKs);
9770                    Set<Long> oldTeamPKsSet = SetUtil.fromArray(userToTeamTableMapper.getRightPrimaryKeys(
9771                                            pk));
9772    
9773                    Set<Long> removeTeamPKsSet = new HashSet<Long>(oldTeamPKsSet);
9774    
9775                    removeTeamPKsSet.removeAll(newTeamPKsSet);
9776    
9777                    for (long removeTeamPK : removeTeamPKsSet) {
9778                            userToTeamTableMapper.deleteTableMapping(pk, removeTeamPK);
9779                    }
9780    
9781                    newTeamPKsSet.removeAll(oldTeamPKsSet);
9782    
9783                    long companyId = 0;
9784    
9785                    User user = fetchByPrimaryKey(pk);
9786    
9787                    if (user == null) {
9788                            companyId = companyProvider.getCompanyId();
9789                    }
9790                    else {
9791                            companyId = user.getCompanyId();
9792                    }
9793    
9794                    for (long newTeamPK : newTeamPKsSet) {
9795                            userToTeamTableMapper.addTableMapping(companyId, pk, newTeamPK);
9796                    }
9797            }
9798    
9799            /**
9800             * Sets the teams associated with the user, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9801             *
9802             * @param pk the primary key of the user
9803             * @param teams the teams to be associated with the user
9804             */
9805            @Override
9806            public void setTeams(long pk,
9807                    List<com.liferay.portal.kernel.model.Team> teams) {
9808                    try {
9809                            long[] teamPKs = new long[teams.size()];
9810    
9811                            for (int i = 0; i < teams.size(); i++) {
9812                                    com.liferay.portal.kernel.model.Team team = teams.get(i);
9813    
9814                                    teamPKs[i] = team.getPrimaryKey();
9815                            }
9816    
9817                            setTeams(pk, teamPKs);
9818                    }
9819                    catch (Exception e) {
9820                            throw processException(e);
9821                    }
9822            }
9823    
9824            /**
9825             * Returns the primaryKeys of user groups associated with the user.
9826             *
9827             * @param pk the primary key of the user
9828             * @return long[] of the primaryKeys of user groups associated with the user
9829             */
9830            @Override
9831            public long[] getUserGroupPrimaryKeys(long pk) {
9832                    long[] pks = userToUserGroupTableMapper.getRightPrimaryKeys(pk);
9833    
9834                    return pks.clone();
9835            }
9836    
9837            /**
9838             * Returns all the user groups associated with the user.
9839             *
9840             * @param pk the primary key of the user
9841             * @return the user groups associated with the user
9842             */
9843            @Override
9844            public List<com.liferay.portal.kernel.model.UserGroup> getUserGroups(
9845                    long pk) {
9846                    return getUserGroups(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
9847            }
9848    
9849            /**
9850             * Returns a range of all the user groups associated with the user.
9851             *
9852             * <p>
9853             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
9854             * </p>
9855             *
9856             * @param pk the primary key of the user
9857             * @param start the lower bound of the range of users
9858             * @param end the upper bound of the range of users (not inclusive)
9859             * @return the range of user groups associated with the user
9860             */
9861            @Override
9862            public List<com.liferay.portal.kernel.model.UserGroup> getUserGroups(
9863                    long pk, int start, int end) {
9864                    return getUserGroups(pk, start, end, null);
9865            }
9866    
9867            /**
9868             * Returns an ordered range of all the user groups associated with the user.
9869             *
9870             * <p>
9871             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
9872             * </p>
9873             *
9874             * @param pk the primary key of the user
9875             * @param start the lower bound of the range of users
9876             * @param end the upper bound of the range of users (not inclusive)
9877             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
9878             * @return the ordered range of user groups associated with the user
9879             */
9880            @Override
9881            public List<com.liferay.portal.kernel.model.UserGroup> getUserGroups(
9882                    long pk, int start, int end,
9883                    OrderByComparator<com.liferay.portal.kernel.model.UserGroup> orderByComparator) {
9884                    return userToUserGroupTableMapper.getRightBaseModels(pk, start, end,
9885                            orderByComparator);
9886            }
9887    
9888            /**
9889             * Returns the number of user groups associated with the user.
9890             *
9891             * @param pk the primary key of the user
9892             * @return the number of user groups associated with the user
9893             */
9894            @Override
9895            public int getUserGroupsSize(long pk) {
9896                    long[] pks = userToUserGroupTableMapper.getRightPrimaryKeys(pk);
9897    
9898                    return pks.length;
9899            }
9900    
9901            /**
9902             * Returns <code>true</code> if the user group is associated with the user.
9903             *
9904             * @param pk the primary key of the user
9905             * @param userGroupPK the primary key of the user group
9906             * @return <code>true</code> if the user group is associated with the user; <code>false</code> otherwise
9907             */
9908            @Override
9909            public boolean containsUserGroup(long pk, long userGroupPK) {
9910                    return userToUserGroupTableMapper.containsTableMapping(pk, userGroupPK);
9911            }
9912    
9913            /**
9914             * Returns <code>true</code> if the user has any user groups associated with it.
9915             *
9916             * @param pk the primary key of the user to check for associations with user groups
9917             * @return <code>true</code> if the user has any user groups associated with it; <code>false</code> otherwise
9918             */
9919            @Override
9920            public boolean containsUserGroups(long pk) {
9921                    if (getUserGroupsSize(pk) > 0) {
9922                            return true;
9923                    }
9924                    else {
9925                            return false;
9926                    }
9927            }
9928    
9929            /**
9930             * Adds an association between the user and the user group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9931             *
9932             * @param pk the primary key of the user
9933             * @param userGroupPK the primary key of the user group
9934             */
9935            @Override
9936            public void addUserGroup(long pk, long userGroupPK) {
9937                    User user = fetchByPrimaryKey(pk);
9938    
9939                    if (user == null) {
9940                            userToUserGroupTableMapper.addTableMapping(companyProvider.getCompanyId(),
9941                                    pk, userGroupPK);
9942                    }
9943                    else {
9944                            userToUserGroupTableMapper.addTableMapping(user.getCompanyId(), pk,
9945                                    userGroupPK);
9946                    }
9947            }
9948    
9949            /**
9950             * Adds an association between the user and the user group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9951             *
9952             * @param pk the primary key of the user
9953             * @param userGroup the user group
9954             */
9955            @Override
9956            public void addUserGroup(long pk,
9957                    com.liferay.portal.kernel.model.UserGroup userGroup) {
9958                    User user = fetchByPrimaryKey(pk);
9959    
9960                    if (user == null) {
9961                            userToUserGroupTableMapper.addTableMapping(companyProvider.getCompanyId(),
9962                                    pk, userGroup.getPrimaryKey());
9963                    }
9964                    else {
9965                            userToUserGroupTableMapper.addTableMapping(user.getCompanyId(), pk,
9966                                    userGroup.getPrimaryKey());
9967                    }
9968            }
9969    
9970            /**
9971             * Adds an association between the user and the user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9972             *
9973             * @param pk the primary key of the user
9974             * @param userGroupPKs the primary keys of the user groups
9975             */
9976            @Override
9977            public void addUserGroups(long pk, long[] userGroupPKs) {
9978                    long companyId = 0;
9979    
9980                    User user = fetchByPrimaryKey(pk);
9981    
9982                    if (user == null) {
9983                            companyId = companyProvider.getCompanyId();
9984                    }
9985                    else {
9986                            companyId = user.getCompanyId();
9987                    }
9988    
9989                    for (long userGroupPK : userGroupPKs) {
9990                            userToUserGroupTableMapper.addTableMapping(companyId, pk,
9991                                    userGroupPK);
9992                    }
9993            }
9994    
9995            /**
9996             * Adds an association between the user and the user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9997             *
9998             * @param pk the primary key of the user
9999             * @param userGroups the user groups
10000             */
10001            @Override
10002            public void addUserGroups(long pk,
10003                    List<com.liferay.portal.kernel.model.UserGroup> userGroups) {
10004                    long companyId = 0;
10005    
10006                    User user = fetchByPrimaryKey(pk);
10007    
10008                    if (user == null) {
10009                            companyId = companyProvider.getCompanyId();
10010                    }
10011                    else {
10012                            companyId = user.getCompanyId();
10013                    }
10014    
10015                    for (com.liferay.portal.kernel.model.UserGroup userGroup : userGroups) {
10016                            userToUserGroupTableMapper.addTableMapping(companyId, pk,
10017                                    userGroup.getPrimaryKey());
10018                    }
10019            }
10020    
10021            /**
10022             * Clears all associations between the user and its user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
10023             *
10024             * @param pk the primary key of the user to clear the associated user groups from
10025             */
10026            @Override
10027            public void clearUserGroups(long pk) {
10028                    userToUserGroupTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
10029            }
10030    
10031            /**
10032             * Removes the association between the user and the user group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
10033             *
10034             * @param pk the primary key of the user
10035             * @param userGroupPK the primary key of the user group
10036             */
10037            @Override
10038            public void removeUserGroup(long pk, long userGroupPK) {
10039                    userToUserGroupTableMapper.deleteTableMapping(pk, userGroupPK);
10040            }
10041    
10042            /**
10043             * Removes the association between the user and the user group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
10044             *
10045             * @param pk the primary key of the user
10046             * @param userGroup the user group
10047             */
10048            @Override
10049            public void removeUserGroup(long pk,
10050                    com.liferay.portal.kernel.model.UserGroup userGroup) {
10051                    userToUserGroupTableMapper.deleteTableMapping(pk,
10052                            userGroup.getPrimaryKey());
10053            }
10054    
10055            /**
10056             * Removes the association between the user and the user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
10057             *
10058             * @param pk the primary key of the user
10059             * @param userGroupPKs the primary keys of the user groups
10060             */
10061            @Override
10062            public void removeUserGroups(long pk, long[] userGroupPKs) {
10063                    for (long userGroupPK : userGroupPKs) {
10064                            userToUserGroupTableMapper.deleteTableMapping(pk, userGroupPK);
10065                    }
10066            }
10067    
10068            /**
10069             * Removes the association between the user and the user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
10070             *
10071             * @param pk the primary key of the user
10072             * @param userGroups the user groups
10073             */
10074            @Override
10075            public void removeUserGroups(long pk,
10076                    List<com.liferay.portal.kernel.model.UserGroup> userGroups) {
10077                    for (com.liferay.portal.kernel.model.UserGroup userGroup : userGroups) {
10078                            userToUserGroupTableMapper.deleteTableMapping(pk,
10079                                    userGroup.getPrimaryKey());
10080                    }
10081            }
10082    
10083            /**
10084             * Sets the user groups associated with the user, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
10085             *
10086             * @param pk the primary key of the user
10087             * @param userGroupPKs the primary keys of the user groups to be associated with the user
10088             */
10089            @Override
10090            public void setUserGroups(long pk, long[] userGroupPKs) {
10091                    Set<Long> newUserGroupPKsSet = SetUtil.fromArray(userGroupPKs);
10092                    Set<Long> oldUserGroupPKsSet = SetUtil.fromArray(userToUserGroupTableMapper.getRightPrimaryKeys(
10093                                            pk));
10094    
10095                    Set<Long> removeUserGroupPKsSet = new HashSet<Long>(oldUserGroupPKsSet);
10096    
10097                    removeUserGroupPKsSet.removeAll(newUserGroupPKsSet);
10098    
10099                    for (long removeUserGroupPK : removeUserGroupPKsSet) {
10100                            userToUserGroupTableMapper.deleteTableMapping(pk, removeUserGroupPK);
10101                    }
10102    
10103                    newUserGroupPKsSet.removeAll(oldUserGroupPKsSet);
10104    
10105                    long companyId = 0;
10106    
10107                    User user = fetchByPrimaryKey(pk);
10108    
10109                    if (user == null) {
10110                            companyId = companyProvider.getCompanyId();
10111                    }
10112                    else {
10113                            companyId = user.getCompanyId();
10114                    }
10115    
10116                    for (long newUserGroupPK : newUserGroupPKsSet) {
10117                            userToUserGroupTableMapper.addTableMapping(companyId, pk,
10118                                    newUserGroupPK);
10119                    }
10120            }
10121    
10122            /**
10123             * Sets the user groups associated with the user, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
10124             *
10125             * @param pk the primary key of the user
10126             * @param userGroups the user groups to be associated with the user
10127             */
10128            @Override
10129            public void setUserGroups(long pk,
10130                    List<com.liferay.portal.kernel.model.UserGroup> userGroups) {
10131                    try {
10132                            long[] userGroupPKs = new long[userGroups.size()];
10133    
10134                            for (int i = 0; i < userGroups.size(); i++) {
10135                                    com.liferay.portal.kernel.model.UserGroup userGroup = userGroups.get(i);
10136    
10137                                    userGroupPKs[i] = userGroup.getPrimaryKey();
10138                            }
10139    
10140                            setUserGroups(pk, userGroupPKs);
10141                    }
10142                    catch (Exception e) {
10143                            throw processException(e);
10144                    }
10145            }
10146    
10147            @Override
10148            public Set<String> getBadColumnNames() {
10149                    return _badColumnNames;
10150            }
10151    
10152            @Override
10153            protected Map<String, Integer> getTableColumnsMap() {
10154                    return UserModelImpl.TABLE_COLUMNS_MAP;
10155            }
10156    
10157            /**
10158             * Initializes the user persistence.
10159             */
10160            public void afterPropertiesSet() {
10161                    userToGroupTableMapper = TableMapperFactory.getTableMapper("Users_Groups",
10162                                    "companyId", "userId", "groupId", this, groupPersistence);
10163    
10164                    userToOrganizationTableMapper = TableMapperFactory.getTableMapper("Users_Orgs",
10165                                    "companyId", "userId", "organizationId", this,
10166                                    organizationPersistence);
10167    
10168                    userToRoleTableMapper = TableMapperFactory.getTableMapper("Users_Roles",
10169                                    "companyId", "userId", "roleId", this, rolePersistence);
10170    
10171                    userToTeamTableMapper = TableMapperFactory.getTableMapper("Users_Teams",
10172                                    "companyId", "userId", "teamId", this, teamPersistence);
10173    
10174                    userToUserGroupTableMapper = TableMapperFactory.getTableMapper("Users_UserGroups",
10175                                    "companyId", "userId", "userGroupId", this, userGroupPersistence);
10176            }
10177    
10178            public void destroy() {
10179                    entityCache.removeCache(UserImpl.class.getName());
10180                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
10181                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
10182                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
10183    
10184                    TableMapperFactory.removeTableMapper("Users_Groups");
10185                    TableMapperFactory.removeTableMapper("Users_Orgs");
10186                    TableMapperFactory.removeTableMapper("Users_Roles");
10187                    TableMapperFactory.removeTableMapper("Users_Teams");
10188                    TableMapperFactory.removeTableMapper("Users_UserGroups");
10189            }
10190    
10191            @BeanReference(type = CompanyProviderWrapper.class)
10192            protected CompanyProvider companyProvider;
10193            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
10194            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
10195            @BeanReference(type = GroupPersistence.class)
10196            protected GroupPersistence groupPersistence;
10197            protected TableMapper<User, com.liferay.portal.kernel.model.Group> userToGroupTableMapper;
10198            @BeanReference(type = OrganizationPersistence.class)
10199            protected OrganizationPersistence organizationPersistence;
10200            protected TableMapper<User, com.liferay.portal.kernel.model.Organization> userToOrganizationTableMapper;
10201            @BeanReference(type = RolePersistence.class)
10202            protected RolePersistence rolePersistence;
10203            protected TableMapper<User, com.liferay.portal.kernel.model.Role> userToRoleTableMapper;
10204            @BeanReference(type = TeamPersistence.class)
10205            protected TeamPersistence teamPersistence;
10206            protected TableMapper<User, com.liferay.portal.kernel.model.Team> userToTeamTableMapper;
10207            @BeanReference(type = UserGroupPersistence.class)
10208            protected UserGroupPersistence userGroupPersistence;
10209            protected TableMapper<User, com.liferay.portal.kernel.model.UserGroup> userToUserGroupTableMapper;
10210            private static final String _SQL_SELECT_USER = "SELECT user FROM User user";
10211            private static final String _SQL_SELECT_USER_WHERE_PKS_IN = "SELECT user FROM User user WHERE userId IN (";
10212            private static final String _SQL_SELECT_USER_WHERE = "SELECT user FROM User user WHERE ";
10213            private static final String _SQL_COUNT_USER = "SELECT COUNT(user) FROM User user";
10214            private static final String _SQL_COUNT_USER_WHERE = "SELECT COUNT(user) FROM User user WHERE ";
10215            private static final String _ORDER_BY_ENTITY_ALIAS = "user.";
10216            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No User exists with the primary key ";
10217            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No User exists with the key {";
10218            private static final Log _log = LogFactoryUtil.getLog(UserPersistenceImpl.class);
10219            private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
10220                                    "uuid", "password"
10221                            });
10222            private static final User _nullUser = new UserImpl() {
10223                            @Override
10224                            public Object clone() {
10225                                    return this;
10226                            }
10227    
10228                            @Override
10229                            public CacheModel<User> toCacheModel() {
10230                                    return _nullUserCacheModel;
10231                            }
10232                    };
10233    
10234            private static final CacheModel<User> _nullUserCacheModel = new NullCacheModel();
10235    
10236            private static class NullCacheModel implements CacheModel<User>, MVCCModel {
10237                    @Override
10238                    public long getMvccVersion() {
10239                            return -1;
10240                    }
10241    
10242                    @Override
10243                    public void setMvccVersion(long mvccVersion) {
10244                    }
10245    
10246                    @Override
10247                    public User toEntityModel() {
10248                            return _nullUser;
10249                    }
10250            }
10251    }