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.Objects;
070    import java.util.Set;
071    
072    /**
073     * The persistence implementation for the user service.
074     *
075     * <p>
076     * Caching information and settings can be found in <code>portal.properties</code>
077     * </p>
078     *
079     * @author Brian Wing Shun Chan
080     * @see UserPersistence
081     * @see com.liferay.portal.kernel.service.persistence.UserUtil
082     * @generated
083     */
084    @ProviderType
085    public class UserPersistenceImpl extends BasePersistenceImpl<User>
086            implements UserPersistence {
087            /*
088             * NOTE FOR DEVELOPERS:
089             *
090             * 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.
091             */
092            public static final String FINDER_CLASS_NAME_ENTITY = UserImpl.class.getName();
093            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
094                    ".List1";
095            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
096                    ".List2";
097            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
098                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
099                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
100            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
101                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
102                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
103            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
104                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
105                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
106            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
107                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
108                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid",
109                            new String[] {
110                                    String.class.getName(),
111                                    
112                            Integer.class.getName(), Integer.class.getName(),
113                                    OrderByComparator.class.getName()
114                            });
115            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
116                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
117                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid",
118                            new String[] { String.class.getName() },
119                            UserModelImpl.UUID_COLUMN_BITMASK);
120            public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
121                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
122                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid",
123                            new String[] { String.class.getName() });
124    
125            /**
126             * Returns all the users where uuid = &#63;.
127             *
128             * @param uuid the uuid
129             * @return the matching users
130             */
131            @Override
132            public List<User> findByUuid(String uuid) {
133                    return findByUuid(uuid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
134            }
135    
136            /**
137             * Returns a range of all the users where uuid = &#63;.
138             *
139             * <p>
140             * 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.
141             * </p>
142             *
143             * @param uuid the uuid
144             * @param start the lower bound of the range of users
145             * @param end the upper bound of the range of users (not inclusive)
146             * @return the range of matching users
147             */
148            @Override
149            public List<User> findByUuid(String uuid, int start, int end) {
150                    return findByUuid(uuid, start, end, null);
151            }
152    
153            /**
154             * Returns an ordered range of all the users where uuid = &#63;.
155             *
156             * <p>
157             * 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.
158             * </p>
159             *
160             * @param uuid the uuid
161             * @param start the lower bound of the range of users
162             * @param end the upper bound of the range of users (not inclusive)
163             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
164             * @return the ordered range of matching users
165             */
166            @Override
167            public List<User> findByUuid(String uuid, int start, int end,
168                    OrderByComparator<User> orderByComparator) {
169                    return findByUuid(uuid, start, end, orderByComparator, true);
170            }
171    
172            /**
173             * Returns an ordered range of all the users where uuid = &#63;.
174             *
175             * <p>
176             * 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.
177             * </p>
178             *
179             * @param uuid the uuid
180             * @param start the lower bound of the range of users
181             * @param end the upper bound of the range of users (not inclusive)
182             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
183             * @param retrieveFromCache whether to retrieve from the finder cache
184             * @return the ordered range of matching users
185             */
186            @Override
187            public List<User> findByUuid(String uuid, int start, int end,
188                    OrderByComparator<User> orderByComparator, boolean retrieveFromCache) {
189                    boolean pagination = true;
190                    FinderPath finderPath = null;
191                    Object[] finderArgs = null;
192    
193                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
194                                    (orderByComparator == null)) {
195                            pagination = false;
196                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID;
197                            finderArgs = new Object[] { uuid };
198                    }
199                    else {
200                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID;
201                            finderArgs = new Object[] { uuid, start, end, orderByComparator };
202                    }
203    
204                    List<User> list = null;
205    
206                    if (retrieveFromCache) {
207                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
208                                            this);
209    
210                            if ((list != null) && !list.isEmpty()) {
211                                    for (User user : list) {
212                                            if (!Objects.equals(uuid, user.getUuid())) {
213                                                    list = null;
214    
215                                                    break;
216                                            }
217                                    }
218                            }
219                    }
220    
221                    if (list == null) {
222                            StringBundler query = null;
223    
224                            if (orderByComparator != null) {
225                                    query = new StringBundler(3 +
226                                                    (orderByComparator.getOrderByFields().length * 2));
227                            }
228                            else {
229                                    query = new StringBundler(3);
230                            }
231    
232                            query.append(_SQL_SELECT_USER_WHERE);
233    
234                            boolean bindUuid = false;
235    
236                            if (uuid == null) {
237                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
238                            }
239                            else if (uuid.equals(StringPool.BLANK)) {
240                                    query.append(_FINDER_COLUMN_UUID_UUID_3);
241                            }
242                            else {
243                                    bindUuid = true;
244    
245                                    query.append(_FINDER_COLUMN_UUID_UUID_2);
246                            }
247    
248                            if (orderByComparator != null) {
249                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
250                                            orderByComparator);
251                            }
252                            else
253                             if (pagination) {
254                                    query.append(UserModelImpl.ORDER_BY_JPQL);
255                            }
256    
257                            String sql = query.toString();
258    
259                            Session session = null;
260    
261                            try {
262                                    session = openSession();
263    
264                                    Query q = session.createQuery(sql);
265    
266                                    QueryPos qPos = QueryPos.getInstance(q);
267    
268                                    if (bindUuid) {
269                                            qPos.add(uuid);
270                                    }
271    
272                                    if (!pagination) {
273                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
274                                                            end, false);
275    
276                                            Collections.sort(list);
277    
278                                            list = Collections.unmodifiableList(list);
279                                    }
280                                    else {
281                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
282                                                            end);
283                                    }
284    
285                                    cacheResult(list);
286    
287                                    finderCache.putResult(finderPath, finderArgs, list);
288                            }
289                            catch (Exception e) {
290                                    finderCache.removeResult(finderPath, finderArgs);
291    
292                                    throw processException(e);
293                            }
294                            finally {
295                                    closeSession(session);
296                            }
297                    }
298    
299                    return list;
300            }
301    
302            /**
303             * Returns the first user in the ordered set where uuid = &#63;.
304             *
305             * @param uuid the uuid
306             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
307             * @return the first matching user
308             * @throws NoSuchUserException if a matching user could not be found
309             */
310            @Override
311            public User findByUuid_First(String uuid,
312                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
313                    User user = fetchByUuid_First(uuid, orderByComparator);
314    
315                    if (user != null) {
316                            return user;
317                    }
318    
319                    StringBundler msg = new StringBundler(4);
320    
321                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
322    
323                    msg.append("uuid=");
324                    msg.append(uuid);
325    
326                    msg.append(StringPool.CLOSE_CURLY_BRACE);
327    
328                    throw new NoSuchUserException(msg.toString());
329            }
330    
331            /**
332             * Returns the first user in the ordered set where uuid = &#63;.
333             *
334             * @param uuid the uuid
335             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
336             * @return the first matching user, or <code>null</code> if a matching user could not be found
337             */
338            @Override
339            public User fetchByUuid_First(String uuid,
340                    OrderByComparator<User> orderByComparator) {
341                    List<User> list = findByUuid(uuid, 0, 1, orderByComparator);
342    
343                    if (!list.isEmpty()) {
344                            return list.get(0);
345                    }
346    
347                    return null;
348            }
349    
350            /**
351             * Returns the last user in the ordered set where uuid = &#63;.
352             *
353             * @param uuid the uuid
354             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
355             * @return the last matching user
356             * @throws NoSuchUserException if a matching user could not be found
357             */
358            @Override
359            public User findByUuid_Last(String uuid,
360                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
361                    User user = fetchByUuid_Last(uuid, orderByComparator);
362    
363                    if (user != null) {
364                            return user;
365                    }
366    
367                    StringBundler msg = new StringBundler(4);
368    
369                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
370    
371                    msg.append("uuid=");
372                    msg.append(uuid);
373    
374                    msg.append(StringPool.CLOSE_CURLY_BRACE);
375    
376                    throw new NoSuchUserException(msg.toString());
377            }
378    
379            /**
380             * Returns the last user in the ordered set where uuid = &#63;.
381             *
382             * @param uuid the uuid
383             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
384             * @return the last matching user, or <code>null</code> if a matching user could not be found
385             */
386            @Override
387            public User fetchByUuid_Last(String uuid,
388                    OrderByComparator<User> orderByComparator) {
389                    int count = countByUuid(uuid);
390    
391                    if (count == 0) {
392                            return null;
393                    }
394    
395                    List<User> list = findByUuid(uuid, count - 1, count, orderByComparator);
396    
397                    if (!list.isEmpty()) {
398                            return list.get(0);
399                    }
400    
401                    return null;
402            }
403    
404            /**
405             * Returns the users before and after the current user in the ordered set where uuid = &#63;.
406             *
407             * @param userId the primary key of the current user
408             * @param uuid the uuid
409             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
410             * @return the previous, current, and next user
411             * @throws NoSuchUserException if a user with the primary key could not be found
412             */
413            @Override
414            public User[] findByUuid_PrevAndNext(long userId, String uuid,
415                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
416                    User user = findByPrimaryKey(userId);
417    
418                    Session session = null;
419    
420                    try {
421                            session = openSession();
422    
423                            User[] array = new UserImpl[3];
424    
425                            array[0] = getByUuid_PrevAndNext(session, user, uuid,
426                                            orderByComparator, true);
427    
428                            array[1] = user;
429    
430                            array[2] = getByUuid_PrevAndNext(session, user, uuid,
431                                            orderByComparator, false);
432    
433                            return array;
434                    }
435                    catch (Exception e) {
436                            throw processException(e);
437                    }
438                    finally {
439                            closeSession(session);
440                    }
441            }
442    
443            protected User getByUuid_PrevAndNext(Session session, User user,
444                    String uuid, OrderByComparator<User> orderByComparator, boolean previous) {
445                    StringBundler query = null;
446    
447                    if (orderByComparator != null) {
448                            query = new StringBundler(4 +
449                                            (orderByComparator.getOrderByConditionFields().length * 3) +
450                                            (orderByComparator.getOrderByFields().length * 3));
451                    }
452                    else {
453                            query = new StringBundler(3);
454                    }
455    
456                    query.append(_SQL_SELECT_USER_WHERE);
457    
458                    boolean bindUuid = false;
459    
460                    if (uuid == null) {
461                            query.append(_FINDER_COLUMN_UUID_UUID_1);
462                    }
463                    else if (uuid.equals(StringPool.BLANK)) {
464                            query.append(_FINDER_COLUMN_UUID_UUID_3);
465                    }
466                    else {
467                            bindUuid = true;
468    
469                            query.append(_FINDER_COLUMN_UUID_UUID_2);
470                    }
471    
472                    if (orderByComparator != null) {
473                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
474    
475                            if (orderByConditionFields.length > 0) {
476                                    query.append(WHERE_AND);
477                            }
478    
479                            for (int i = 0; i < orderByConditionFields.length; i++) {
480                                    query.append(_ORDER_BY_ENTITY_ALIAS);
481                                    query.append(orderByConditionFields[i]);
482    
483                                    if ((i + 1) < orderByConditionFields.length) {
484                                            if (orderByComparator.isAscending() ^ previous) {
485                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
486                                            }
487                                            else {
488                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
489                                            }
490                                    }
491                                    else {
492                                            if (orderByComparator.isAscending() ^ previous) {
493                                                    query.append(WHERE_GREATER_THAN);
494                                            }
495                                            else {
496                                                    query.append(WHERE_LESSER_THAN);
497                                            }
498                                    }
499                            }
500    
501                            query.append(ORDER_BY_CLAUSE);
502    
503                            String[] orderByFields = orderByComparator.getOrderByFields();
504    
505                            for (int i = 0; i < orderByFields.length; i++) {
506                                    query.append(_ORDER_BY_ENTITY_ALIAS);
507                                    query.append(orderByFields[i]);
508    
509                                    if ((i + 1) < orderByFields.length) {
510                                            if (orderByComparator.isAscending() ^ previous) {
511                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
512                                            }
513                                            else {
514                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
515                                            }
516                                    }
517                                    else {
518                                            if (orderByComparator.isAscending() ^ previous) {
519                                                    query.append(ORDER_BY_ASC);
520                                            }
521                                            else {
522                                                    query.append(ORDER_BY_DESC);
523                                            }
524                                    }
525                            }
526                    }
527                    else {
528                            query.append(UserModelImpl.ORDER_BY_JPQL);
529                    }
530    
531                    String sql = query.toString();
532    
533                    Query q = session.createQuery(sql);
534    
535                    q.setFirstResult(0);
536                    q.setMaxResults(2);
537    
538                    QueryPos qPos = QueryPos.getInstance(q);
539    
540                    if (bindUuid) {
541                            qPos.add(uuid);
542                    }
543    
544                    if (orderByComparator != null) {
545                            Object[] values = orderByComparator.getOrderByConditionValues(user);
546    
547                            for (Object value : values) {
548                                    qPos.add(value);
549                            }
550                    }
551    
552                    List<User> list = q.list();
553    
554                    if (list.size() == 2) {
555                            return list.get(1);
556                    }
557                    else {
558                            return null;
559                    }
560            }
561    
562            /**
563             * Removes all the users where uuid = &#63; from the database.
564             *
565             * @param uuid the uuid
566             */
567            @Override
568            public void removeByUuid(String uuid) {
569                    for (User user : findByUuid(uuid, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
570                                    null)) {
571                            remove(user);
572                    }
573            }
574    
575            /**
576             * Returns the number of users where uuid = &#63;.
577             *
578             * @param uuid the uuid
579             * @return the number of matching users
580             */
581            @Override
582            public int countByUuid(String uuid) {
583                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID;
584    
585                    Object[] finderArgs = new Object[] { uuid };
586    
587                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
588    
589                    if (count == null) {
590                            StringBundler query = new StringBundler(2);
591    
592                            query.append(_SQL_COUNT_USER_WHERE);
593    
594                            boolean bindUuid = false;
595    
596                            if (uuid == null) {
597                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
598                            }
599                            else if (uuid.equals(StringPool.BLANK)) {
600                                    query.append(_FINDER_COLUMN_UUID_UUID_3);
601                            }
602                            else {
603                                    bindUuid = true;
604    
605                                    query.append(_FINDER_COLUMN_UUID_UUID_2);
606                            }
607    
608                            String sql = query.toString();
609    
610                            Session session = null;
611    
612                            try {
613                                    session = openSession();
614    
615                                    Query q = session.createQuery(sql);
616    
617                                    QueryPos qPos = QueryPos.getInstance(q);
618    
619                                    if (bindUuid) {
620                                            qPos.add(uuid);
621                                    }
622    
623                                    count = (Long)q.uniqueResult();
624    
625                                    finderCache.putResult(finderPath, finderArgs, count);
626                            }
627                            catch (Exception e) {
628                                    finderCache.removeResult(finderPath, finderArgs);
629    
630                                    throw processException(e);
631                            }
632                            finally {
633                                    closeSession(session);
634                            }
635                    }
636    
637                    return count.intValue();
638            }
639    
640            private static final String _FINDER_COLUMN_UUID_UUID_1 = "user.uuid IS NULL";
641            private static final String _FINDER_COLUMN_UUID_UUID_2 = "user.uuid = ?";
642            private static final String _FINDER_COLUMN_UUID_UUID_3 = "(user.uuid IS NULL OR user.uuid = '')";
643            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
644                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
645                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid_C",
646                            new String[] {
647                                    String.class.getName(), Long.class.getName(),
648                                    
649                            Integer.class.getName(), Integer.class.getName(),
650                                    OrderByComparator.class.getName()
651                            });
652            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C =
653                    new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
654                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
655                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid_C",
656                            new String[] { String.class.getName(), Long.class.getName() },
657                            UserModelImpl.UUID_COLUMN_BITMASK |
658                            UserModelImpl.COMPANYID_COLUMN_BITMASK);
659            public static final FinderPath FINDER_PATH_COUNT_BY_UUID_C = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
660                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
661                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid_C",
662                            new String[] { String.class.getName(), Long.class.getName() });
663    
664            /**
665             * Returns all the users where uuid = &#63; and companyId = &#63;.
666             *
667             * @param uuid the uuid
668             * @param companyId the company ID
669             * @return the matching users
670             */
671            @Override
672            public List<User> findByUuid_C(String uuid, long companyId) {
673                    return findByUuid_C(uuid, companyId, QueryUtil.ALL_POS,
674                            QueryUtil.ALL_POS, null);
675            }
676    
677            /**
678             * Returns a range of all the users where uuid = &#63; and companyId = &#63;.
679             *
680             * <p>
681             * 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.
682             * </p>
683             *
684             * @param uuid the uuid
685             * @param companyId the company ID
686             * @param start the lower bound of the range of users
687             * @param end the upper bound of the range of users (not inclusive)
688             * @return the range of matching users
689             */
690            @Override
691            public List<User> findByUuid_C(String uuid, long companyId, int start,
692                    int end) {
693                    return findByUuid_C(uuid, companyId, start, end, null);
694            }
695    
696            /**
697             * Returns an ordered range of all the users where uuid = &#63; and companyId = &#63;.
698             *
699             * <p>
700             * 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.
701             * </p>
702             *
703             * @param uuid the uuid
704             * @param companyId the company ID
705             * @param start the lower bound of the range of users
706             * @param end the upper bound of the range of users (not inclusive)
707             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
708             * @return the ordered range of matching users
709             */
710            @Override
711            public List<User> findByUuid_C(String uuid, long companyId, int start,
712                    int end, OrderByComparator<User> orderByComparator) {
713                    return findByUuid_C(uuid, companyId, start, end, orderByComparator, true);
714            }
715    
716            /**
717             * Returns an ordered range of all the users where uuid = &#63; and companyId = &#63;.
718             *
719             * <p>
720             * 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.
721             * </p>
722             *
723             * @param uuid the uuid
724             * @param companyId the company ID
725             * @param start the lower bound of the range of users
726             * @param end the upper bound of the range of users (not inclusive)
727             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
728             * @param retrieveFromCache whether to retrieve from the finder cache
729             * @return the ordered range of matching users
730             */
731            @Override
732            public List<User> findByUuid_C(String uuid, long companyId, int start,
733                    int end, OrderByComparator<User> orderByComparator,
734                    boolean retrieveFromCache) {
735                    boolean pagination = true;
736                    FinderPath finderPath = null;
737                    Object[] finderArgs = null;
738    
739                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
740                                    (orderByComparator == null)) {
741                            pagination = false;
742                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C;
743                            finderArgs = new Object[] { uuid, companyId };
744                    }
745                    else {
746                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C;
747                            finderArgs = new Object[] {
748                                            uuid, companyId,
749                                            
750                                            start, end, orderByComparator
751                                    };
752                    }
753    
754                    List<User> list = null;
755    
756                    if (retrieveFromCache) {
757                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
758                                            this);
759    
760                            if ((list != null) && !list.isEmpty()) {
761                                    for (User user : list) {
762                                            if (!Objects.equals(uuid, user.getUuid()) ||
763                                                            (companyId != user.getCompanyId())) {
764                                                    list = null;
765    
766                                                    break;
767                                            }
768                                    }
769                            }
770                    }
771    
772                    if (list == null) {
773                            StringBundler query = null;
774    
775                            if (orderByComparator != null) {
776                                    query = new StringBundler(4 +
777                                                    (orderByComparator.getOrderByFields().length * 2));
778                            }
779                            else {
780                                    query = new StringBundler(4);
781                            }
782    
783                            query.append(_SQL_SELECT_USER_WHERE);
784    
785                            boolean bindUuid = false;
786    
787                            if (uuid == null) {
788                                    query.append(_FINDER_COLUMN_UUID_C_UUID_1);
789                            }
790                            else if (uuid.equals(StringPool.BLANK)) {
791                                    query.append(_FINDER_COLUMN_UUID_C_UUID_3);
792                            }
793                            else {
794                                    bindUuid = true;
795    
796                                    query.append(_FINDER_COLUMN_UUID_C_UUID_2);
797                            }
798    
799                            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
800    
801                            if (orderByComparator != null) {
802                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
803                                            orderByComparator);
804                            }
805                            else
806                             if (pagination) {
807                                    query.append(UserModelImpl.ORDER_BY_JPQL);
808                            }
809    
810                            String sql = query.toString();
811    
812                            Session session = null;
813    
814                            try {
815                                    session = openSession();
816    
817                                    Query q = session.createQuery(sql);
818    
819                                    QueryPos qPos = QueryPos.getInstance(q);
820    
821                                    if (bindUuid) {
822                                            qPos.add(uuid);
823                                    }
824    
825                                    qPos.add(companyId);
826    
827                                    if (!pagination) {
828                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
829                                                            end, false);
830    
831                                            Collections.sort(list);
832    
833                                            list = Collections.unmodifiableList(list);
834                                    }
835                                    else {
836                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
837                                                            end);
838                                    }
839    
840                                    cacheResult(list);
841    
842                                    finderCache.putResult(finderPath, finderArgs, list);
843                            }
844                            catch (Exception e) {
845                                    finderCache.removeResult(finderPath, finderArgs);
846    
847                                    throw processException(e);
848                            }
849                            finally {
850                                    closeSession(session);
851                            }
852                    }
853    
854                    return list;
855            }
856    
857            /**
858             * Returns the first user in the ordered set where uuid = &#63; and companyId = &#63;.
859             *
860             * @param uuid the uuid
861             * @param companyId the company ID
862             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
863             * @return the first matching user
864             * @throws NoSuchUserException if a matching user could not be found
865             */
866            @Override
867            public User findByUuid_C_First(String uuid, long companyId,
868                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
869                    User user = fetchByUuid_C_First(uuid, companyId, orderByComparator);
870    
871                    if (user != null) {
872                            return user;
873                    }
874    
875                    StringBundler msg = new StringBundler(6);
876    
877                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
878    
879                    msg.append("uuid=");
880                    msg.append(uuid);
881    
882                    msg.append(", companyId=");
883                    msg.append(companyId);
884    
885                    msg.append(StringPool.CLOSE_CURLY_BRACE);
886    
887                    throw new NoSuchUserException(msg.toString());
888            }
889    
890            /**
891             * Returns the first user in the ordered set where uuid = &#63; and companyId = &#63;.
892             *
893             * @param uuid the uuid
894             * @param companyId the company ID
895             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
896             * @return the first matching user, or <code>null</code> if a matching user could not be found
897             */
898            @Override
899            public User fetchByUuid_C_First(String uuid, long companyId,
900                    OrderByComparator<User> orderByComparator) {
901                    List<User> list = findByUuid_C(uuid, companyId, 0, 1, orderByComparator);
902    
903                    if (!list.isEmpty()) {
904                            return list.get(0);
905                    }
906    
907                    return null;
908            }
909    
910            /**
911             * Returns the last user in the ordered set where uuid = &#63; and companyId = &#63;.
912             *
913             * @param uuid the uuid
914             * @param companyId the company ID
915             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
916             * @return the last matching user
917             * @throws NoSuchUserException if a matching user could not be found
918             */
919            @Override
920            public User findByUuid_C_Last(String uuid, long companyId,
921                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
922                    User user = fetchByUuid_C_Last(uuid, companyId, orderByComparator);
923    
924                    if (user != null) {
925                            return user;
926                    }
927    
928                    StringBundler msg = new StringBundler(6);
929    
930                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
931    
932                    msg.append("uuid=");
933                    msg.append(uuid);
934    
935                    msg.append(", companyId=");
936                    msg.append(companyId);
937    
938                    msg.append(StringPool.CLOSE_CURLY_BRACE);
939    
940                    throw new NoSuchUserException(msg.toString());
941            }
942    
943            /**
944             * Returns the last user in the ordered set where uuid = &#63; and companyId = &#63;.
945             *
946             * @param uuid the uuid
947             * @param companyId the company ID
948             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
949             * @return the last matching user, or <code>null</code> if a matching user could not be found
950             */
951            @Override
952            public User fetchByUuid_C_Last(String uuid, long companyId,
953                    OrderByComparator<User> orderByComparator) {
954                    int count = countByUuid_C(uuid, companyId);
955    
956                    if (count == 0) {
957                            return null;
958                    }
959    
960                    List<User> list = findByUuid_C(uuid, companyId, count - 1, count,
961                                    orderByComparator);
962    
963                    if (!list.isEmpty()) {
964                            return list.get(0);
965                    }
966    
967                    return null;
968            }
969    
970            /**
971             * Returns the users before and after the current user in the ordered set where uuid = &#63; and companyId = &#63;.
972             *
973             * @param userId the primary key of the current user
974             * @param uuid the uuid
975             * @param companyId the company ID
976             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
977             * @return the previous, current, and next user
978             * @throws NoSuchUserException if a user with the primary key could not be found
979             */
980            @Override
981            public User[] findByUuid_C_PrevAndNext(long userId, String uuid,
982                    long companyId, OrderByComparator<User> orderByComparator)
983                    throws NoSuchUserException {
984                    User user = findByPrimaryKey(userId);
985    
986                    Session session = null;
987    
988                    try {
989                            session = openSession();
990    
991                            User[] array = new UserImpl[3];
992    
993                            array[0] = getByUuid_C_PrevAndNext(session, user, uuid, companyId,
994                                            orderByComparator, true);
995    
996                            array[1] = user;
997    
998                            array[2] = getByUuid_C_PrevAndNext(session, user, uuid, companyId,
999                                            orderByComparator, false);
1000    
1001                            return array;
1002                    }
1003                    catch (Exception e) {
1004                            throw processException(e);
1005                    }
1006                    finally {
1007                            closeSession(session);
1008                    }
1009            }
1010    
1011            protected User getByUuid_C_PrevAndNext(Session session, User user,
1012                    String uuid, long companyId, OrderByComparator<User> orderByComparator,
1013                    boolean previous) {
1014                    StringBundler query = null;
1015    
1016                    if (orderByComparator != null) {
1017                            query = new StringBundler(5 +
1018                                            (orderByComparator.getOrderByConditionFields().length * 3) +
1019                                            (orderByComparator.getOrderByFields().length * 3));
1020                    }
1021                    else {
1022                            query = new StringBundler(4);
1023                    }
1024    
1025                    query.append(_SQL_SELECT_USER_WHERE);
1026    
1027                    boolean bindUuid = false;
1028    
1029                    if (uuid == null) {
1030                            query.append(_FINDER_COLUMN_UUID_C_UUID_1);
1031                    }
1032                    else if (uuid.equals(StringPool.BLANK)) {
1033                            query.append(_FINDER_COLUMN_UUID_C_UUID_3);
1034                    }
1035                    else {
1036                            bindUuid = true;
1037    
1038                            query.append(_FINDER_COLUMN_UUID_C_UUID_2);
1039                    }
1040    
1041                    query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
1042    
1043                    if (orderByComparator != null) {
1044                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1045    
1046                            if (orderByConditionFields.length > 0) {
1047                                    query.append(WHERE_AND);
1048                            }
1049    
1050                            for (int i = 0; i < orderByConditionFields.length; i++) {
1051                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1052                                    query.append(orderByConditionFields[i]);
1053    
1054                                    if ((i + 1) < orderByConditionFields.length) {
1055                                            if (orderByComparator.isAscending() ^ previous) {
1056                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1057                                            }
1058                                            else {
1059                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1060                                            }
1061                                    }
1062                                    else {
1063                                            if (orderByComparator.isAscending() ^ previous) {
1064                                                    query.append(WHERE_GREATER_THAN);
1065                                            }
1066                                            else {
1067                                                    query.append(WHERE_LESSER_THAN);
1068                                            }
1069                                    }
1070                            }
1071    
1072                            query.append(ORDER_BY_CLAUSE);
1073    
1074                            String[] orderByFields = orderByComparator.getOrderByFields();
1075    
1076                            for (int i = 0; i < orderByFields.length; i++) {
1077                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1078                                    query.append(orderByFields[i]);
1079    
1080                                    if ((i + 1) < orderByFields.length) {
1081                                            if (orderByComparator.isAscending() ^ previous) {
1082                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1083                                            }
1084                                            else {
1085                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1086                                            }
1087                                    }
1088                                    else {
1089                                            if (orderByComparator.isAscending() ^ previous) {
1090                                                    query.append(ORDER_BY_ASC);
1091                                            }
1092                                            else {
1093                                                    query.append(ORDER_BY_DESC);
1094                                            }
1095                                    }
1096                            }
1097                    }
1098                    else {
1099                            query.append(UserModelImpl.ORDER_BY_JPQL);
1100                    }
1101    
1102                    String sql = query.toString();
1103    
1104                    Query q = session.createQuery(sql);
1105    
1106                    q.setFirstResult(0);
1107                    q.setMaxResults(2);
1108    
1109                    QueryPos qPos = QueryPos.getInstance(q);
1110    
1111                    if (bindUuid) {
1112                            qPos.add(uuid);
1113                    }
1114    
1115                    qPos.add(companyId);
1116    
1117                    if (orderByComparator != null) {
1118                            Object[] values = orderByComparator.getOrderByConditionValues(user);
1119    
1120                            for (Object value : values) {
1121                                    qPos.add(value);
1122                            }
1123                    }
1124    
1125                    List<User> list = q.list();
1126    
1127                    if (list.size() == 2) {
1128                            return list.get(1);
1129                    }
1130                    else {
1131                            return null;
1132                    }
1133            }
1134    
1135            /**
1136             * Removes all the users where uuid = &#63; and companyId = &#63; from the database.
1137             *
1138             * @param uuid the uuid
1139             * @param companyId the company ID
1140             */
1141            @Override
1142            public void removeByUuid_C(String uuid, long companyId) {
1143                    for (User user : findByUuid_C(uuid, companyId, QueryUtil.ALL_POS,
1144                                    QueryUtil.ALL_POS, null)) {
1145                            remove(user);
1146                    }
1147            }
1148    
1149            /**
1150             * Returns the number of users where uuid = &#63; and companyId = &#63;.
1151             *
1152             * @param uuid the uuid
1153             * @param companyId the company ID
1154             * @return the number of matching users
1155             */
1156            @Override
1157            public int countByUuid_C(String uuid, long companyId) {
1158                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID_C;
1159    
1160                    Object[] finderArgs = new Object[] { uuid, companyId };
1161    
1162                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
1163    
1164                    if (count == null) {
1165                            StringBundler query = new StringBundler(3);
1166    
1167                            query.append(_SQL_COUNT_USER_WHERE);
1168    
1169                            boolean bindUuid = false;
1170    
1171                            if (uuid == null) {
1172                                    query.append(_FINDER_COLUMN_UUID_C_UUID_1);
1173                            }
1174                            else if (uuid.equals(StringPool.BLANK)) {
1175                                    query.append(_FINDER_COLUMN_UUID_C_UUID_3);
1176                            }
1177                            else {
1178                                    bindUuid = true;
1179    
1180                                    query.append(_FINDER_COLUMN_UUID_C_UUID_2);
1181                            }
1182    
1183                            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
1184    
1185                            String sql = query.toString();
1186    
1187                            Session session = null;
1188    
1189                            try {
1190                                    session = openSession();
1191    
1192                                    Query q = session.createQuery(sql);
1193    
1194                                    QueryPos qPos = QueryPos.getInstance(q);
1195    
1196                                    if (bindUuid) {
1197                                            qPos.add(uuid);
1198                                    }
1199    
1200                                    qPos.add(companyId);
1201    
1202                                    count = (Long)q.uniqueResult();
1203    
1204                                    finderCache.putResult(finderPath, finderArgs, count);
1205                            }
1206                            catch (Exception e) {
1207                                    finderCache.removeResult(finderPath, finderArgs);
1208    
1209                                    throw processException(e);
1210                            }
1211                            finally {
1212                                    closeSession(session);
1213                            }
1214                    }
1215    
1216                    return count.intValue();
1217            }
1218    
1219            private static final String _FINDER_COLUMN_UUID_C_UUID_1 = "user.uuid IS NULL AND ";
1220            private static final String _FINDER_COLUMN_UUID_C_UUID_2 = "user.uuid = ? AND ";
1221            private static final String _FINDER_COLUMN_UUID_C_UUID_3 = "(user.uuid IS NULL OR user.uuid = '') AND ";
1222            private static final String _FINDER_COLUMN_UUID_C_COMPANYID_2 = "user.companyId = ?";
1223            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID =
1224                    new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1225                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
1226                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByCompanyId",
1227                            new String[] {
1228                                    Long.class.getName(),
1229                                    
1230                            Integer.class.getName(), Integer.class.getName(),
1231                                    OrderByComparator.class.getName()
1232                            });
1233            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID =
1234                    new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1235                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
1236                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByCompanyId",
1237                            new String[] { Long.class.getName() },
1238                            UserModelImpl.COMPANYID_COLUMN_BITMASK);
1239            public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1240                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
1241                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCompanyId",
1242                            new String[] { Long.class.getName() });
1243    
1244            /**
1245             * Returns all the users where companyId = &#63;.
1246             *
1247             * @param companyId the company ID
1248             * @return the matching users
1249             */
1250            @Override
1251            public List<User> findByCompanyId(long companyId) {
1252                    return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
1253                            null);
1254            }
1255    
1256            /**
1257             * Returns a range of all the users where companyId = &#63;.
1258             *
1259             * <p>
1260             * 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.
1261             * </p>
1262             *
1263             * @param companyId the company ID
1264             * @param start the lower bound of the range of users
1265             * @param end the upper bound of the range of users (not inclusive)
1266             * @return the range of matching users
1267             */
1268            @Override
1269            public List<User> findByCompanyId(long companyId, int start, int end) {
1270                    return findByCompanyId(companyId, start, end, null);
1271            }
1272    
1273            /**
1274             * Returns an ordered range of all the users where companyId = &#63;.
1275             *
1276             * <p>
1277             * 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.
1278             * </p>
1279             *
1280             * @param companyId the company ID
1281             * @param start the lower bound of the range of users
1282             * @param end the upper bound of the range of users (not inclusive)
1283             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1284             * @return the ordered range of matching users
1285             */
1286            @Override
1287            public List<User> findByCompanyId(long companyId, int start, int end,
1288                    OrderByComparator<User> orderByComparator) {
1289                    return findByCompanyId(companyId, start, end, orderByComparator, true);
1290            }
1291    
1292            /**
1293             * Returns an ordered range of all the users where companyId = &#63;.
1294             *
1295             * <p>
1296             * 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.
1297             * </p>
1298             *
1299             * @param companyId the company ID
1300             * @param start the lower bound of the range of users
1301             * @param end the upper bound of the range of users (not inclusive)
1302             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1303             * @param retrieveFromCache whether to retrieve from the finder cache
1304             * @return the ordered range of matching users
1305             */
1306            @Override
1307            public List<User> findByCompanyId(long companyId, int start, int end,
1308                    OrderByComparator<User> orderByComparator, boolean retrieveFromCache) {
1309                    boolean pagination = true;
1310                    FinderPath finderPath = null;
1311                    Object[] finderArgs = null;
1312    
1313                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1314                                    (orderByComparator == null)) {
1315                            pagination = false;
1316                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID;
1317                            finderArgs = new Object[] { companyId };
1318                    }
1319                    else {
1320                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID;
1321                            finderArgs = new Object[] { companyId, start, end, orderByComparator };
1322                    }
1323    
1324                    List<User> list = null;
1325    
1326                    if (retrieveFromCache) {
1327                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
1328                                            this);
1329    
1330                            if ((list != null) && !list.isEmpty()) {
1331                                    for (User user : list) {
1332                                            if ((companyId != user.getCompanyId())) {
1333                                                    list = null;
1334    
1335                                                    break;
1336                                            }
1337                                    }
1338                            }
1339                    }
1340    
1341                    if (list == null) {
1342                            StringBundler query = null;
1343    
1344                            if (orderByComparator != null) {
1345                                    query = new StringBundler(3 +
1346                                                    (orderByComparator.getOrderByFields().length * 2));
1347                            }
1348                            else {
1349                                    query = new StringBundler(3);
1350                            }
1351    
1352                            query.append(_SQL_SELECT_USER_WHERE);
1353    
1354                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1355    
1356                            if (orderByComparator != null) {
1357                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1358                                            orderByComparator);
1359                            }
1360                            else
1361                             if (pagination) {
1362                                    query.append(UserModelImpl.ORDER_BY_JPQL);
1363                            }
1364    
1365                            String sql = query.toString();
1366    
1367                            Session session = null;
1368    
1369                            try {
1370                                    session = openSession();
1371    
1372                                    Query q = session.createQuery(sql);
1373    
1374                                    QueryPos qPos = QueryPos.getInstance(q);
1375    
1376                                    qPos.add(companyId);
1377    
1378                                    if (!pagination) {
1379                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
1380                                                            end, false);
1381    
1382                                            Collections.sort(list);
1383    
1384                                            list = Collections.unmodifiableList(list);
1385                                    }
1386                                    else {
1387                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
1388                                                            end);
1389                                    }
1390    
1391                                    cacheResult(list);
1392    
1393                                    finderCache.putResult(finderPath, finderArgs, list);
1394                            }
1395                            catch (Exception e) {
1396                                    finderCache.removeResult(finderPath, finderArgs);
1397    
1398                                    throw processException(e);
1399                            }
1400                            finally {
1401                                    closeSession(session);
1402                            }
1403                    }
1404    
1405                    return list;
1406            }
1407    
1408            /**
1409             * Returns the first user in the ordered set where companyId = &#63;.
1410             *
1411             * @param companyId the company ID
1412             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1413             * @return the first matching user
1414             * @throws NoSuchUserException if a matching user could not be found
1415             */
1416            @Override
1417            public User findByCompanyId_First(long companyId,
1418                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
1419                    User user = fetchByCompanyId_First(companyId, orderByComparator);
1420    
1421                    if (user != null) {
1422                            return user;
1423                    }
1424    
1425                    StringBundler msg = new StringBundler(4);
1426    
1427                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1428    
1429                    msg.append("companyId=");
1430                    msg.append(companyId);
1431    
1432                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1433    
1434                    throw new NoSuchUserException(msg.toString());
1435            }
1436    
1437            /**
1438             * Returns the first user in the ordered set where companyId = &#63;.
1439             *
1440             * @param companyId the company ID
1441             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1442             * @return the first matching user, or <code>null</code> if a matching user could not be found
1443             */
1444            @Override
1445            public User fetchByCompanyId_First(long companyId,
1446                    OrderByComparator<User> orderByComparator) {
1447                    List<User> list = findByCompanyId(companyId, 0, 1, orderByComparator);
1448    
1449                    if (!list.isEmpty()) {
1450                            return list.get(0);
1451                    }
1452    
1453                    return null;
1454            }
1455    
1456            /**
1457             * Returns the last user in the ordered set where companyId = &#63;.
1458             *
1459             * @param companyId the company ID
1460             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1461             * @return the last matching user
1462             * @throws NoSuchUserException if a matching user could not be found
1463             */
1464            @Override
1465            public User findByCompanyId_Last(long companyId,
1466                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
1467                    User user = fetchByCompanyId_Last(companyId, orderByComparator);
1468    
1469                    if (user != null) {
1470                            return user;
1471                    }
1472    
1473                    StringBundler msg = new StringBundler(4);
1474    
1475                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1476    
1477                    msg.append("companyId=");
1478                    msg.append(companyId);
1479    
1480                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1481    
1482                    throw new NoSuchUserException(msg.toString());
1483            }
1484    
1485            /**
1486             * Returns the last user in the ordered set where companyId = &#63;.
1487             *
1488             * @param companyId the company ID
1489             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1490             * @return the last matching user, or <code>null</code> if a matching user could not be found
1491             */
1492            @Override
1493            public User fetchByCompanyId_Last(long companyId,
1494                    OrderByComparator<User> orderByComparator) {
1495                    int count = countByCompanyId(companyId);
1496    
1497                    if (count == 0) {
1498                            return null;
1499                    }
1500    
1501                    List<User> list = findByCompanyId(companyId, count - 1, count,
1502                                    orderByComparator);
1503    
1504                    if (!list.isEmpty()) {
1505                            return list.get(0);
1506                    }
1507    
1508                    return null;
1509            }
1510    
1511            /**
1512             * Returns the users before and after the current user in the ordered set where companyId = &#63;.
1513             *
1514             * @param userId the primary key of the current user
1515             * @param companyId the company ID
1516             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1517             * @return the previous, current, and next user
1518             * @throws NoSuchUserException if a user with the primary key could not be found
1519             */
1520            @Override
1521            public User[] findByCompanyId_PrevAndNext(long userId, long companyId,
1522                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
1523                    User user = findByPrimaryKey(userId);
1524    
1525                    Session session = null;
1526    
1527                    try {
1528                            session = openSession();
1529    
1530                            User[] array = new UserImpl[3];
1531    
1532                            array[0] = getByCompanyId_PrevAndNext(session, user, companyId,
1533                                            orderByComparator, true);
1534    
1535                            array[1] = user;
1536    
1537                            array[2] = getByCompanyId_PrevAndNext(session, user, companyId,
1538                                            orderByComparator, false);
1539    
1540                            return array;
1541                    }
1542                    catch (Exception e) {
1543                            throw processException(e);
1544                    }
1545                    finally {
1546                            closeSession(session);
1547                    }
1548            }
1549    
1550            protected User getByCompanyId_PrevAndNext(Session session, User user,
1551                    long companyId, OrderByComparator<User> orderByComparator,
1552                    boolean previous) {
1553                    StringBundler query = null;
1554    
1555                    if (orderByComparator != null) {
1556                            query = new StringBundler(4 +
1557                                            (orderByComparator.getOrderByConditionFields().length * 3) +
1558                                            (orderByComparator.getOrderByFields().length * 3));
1559                    }
1560                    else {
1561                            query = new StringBundler(3);
1562                    }
1563    
1564                    query.append(_SQL_SELECT_USER_WHERE);
1565    
1566                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1567    
1568                    if (orderByComparator != null) {
1569                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1570    
1571                            if (orderByConditionFields.length > 0) {
1572                                    query.append(WHERE_AND);
1573                            }
1574    
1575                            for (int i = 0; i < orderByConditionFields.length; i++) {
1576                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1577                                    query.append(orderByConditionFields[i]);
1578    
1579                                    if ((i + 1) < orderByConditionFields.length) {
1580                                            if (orderByComparator.isAscending() ^ previous) {
1581                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1582                                            }
1583                                            else {
1584                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1585                                            }
1586                                    }
1587                                    else {
1588                                            if (orderByComparator.isAscending() ^ previous) {
1589                                                    query.append(WHERE_GREATER_THAN);
1590                                            }
1591                                            else {
1592                                                    query.append(WHERE_LESSER_THAN);
1593                                            }
1594                                    }
1595                            }
1596    
1597                            query.append(ORDER_BY_CLAUSE);
1598    
1599                            String[] orderByFields = orderByComparator.getOrderByFields();
1600    
1601                            for (int i = 0; i < orderByFields.length; i++) {
1602                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1603                                    query.append(orderByFields[i]);
1604    
1605                                    if ((i + 1) < orderByFields.length) {
1606                                            if (orderByComparator.isAscending() ^ previous) {
1607                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1608                                            }
1609                                            else {
1610                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1611                                            }
1612                                    }
1613                                    else {
1614                                            if (orderByComparator.isAscending() ^ previous) {
1615                                                    query.append(ORDER_BY_ASC);
1616                                            }
1617                                            else {
1618                                                    query.append(ORDER_BY_DESC);
1619                                            }
1620                                    }
1621                            }
1622                    }
1623                    else {
1624                            query.append(UserModelImpl.ORDER_BY_JPQL);
1625                    }
1626    
1627                    String sql = query.toString();
1628    
1629                    Query q = session.createQuery(sql);
1630    
1631                    q.setFirstResult(0);
1632                    q.setMaxResults(2);
1633    
1634                    QueryPos qPos = QueryPos.getInstance(q);
1635    
1636                    qPos.add(companyId);
1637    
1638                    if (orderByComparator != null) {
1639                            Object[] values = orderByComparator.getOrderByConditionValues(user);
1640    
1641                            for (Object value : values) {
1642                                    qPos.add(value);
1643                            }
1644                    }
1645    
1646                    List<User> list = q.list();
1647    
1648                    if (list.size() == 2) {
1649                            return list.get(1);
1650                    }
1651                    else {
1652                            return null;
1653                    }
1654            }
1655    
1656            /**
1657             * Removes all the users where companyId = &#63; from the database.
1658             *
1659             * @param companyId the company ID
1660             */
1661            @Override
1662            public void removeByCompanyId(long companyId) {
1663                    for (User user : findByCompanyId(companyId, QueryUtil.ALL_POS,
1664                                    QueryUtil.ALL_POS, null)) {
1665                            remove(user);
1666                    }
1667            }
1668    
1669            /**
1670             * Returns the number of users where companyId = &#63;.
1671             *
1672             * @param companyId the company ID
1673             * @return the number of matching users
1674             */
1675            @Override
1676            public int countByCompanyId(long companyId) {
1677                    FinderPath finderPath = FINDER_PATH_COUNT_BY_COMPANYID;
1678    
1679                    Object[] finderArgs = new Object[] { companyId };
1680    
1681                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
1682    
1683                    if (count == null) {
1684                            StringBundler query = new StringBundler(2);
1685    
1686                            query.append(_SQL_COUNT_USER_WHERE);
1687    
1688                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1689    
1690                            String sql = query.toString();
1691    
1692                            Session session = null;
1693    
1694                            try {
1695                                    session = openSession();
1696    
1697                                    Query q = session.createQuery(sql);
1698    
1699                                    QueryPos qPos = QueryPos.getInstance(q);
1700    
1701                                    qPos.add(companyId);
1702    
1703                                    count = (Long)q.uniqueResult();
1704    
1705                                    finderCache.putResult(finderPath, finderArgs, count);
1706                            }
1707                            catch (Exception e) {
1708                                    finderCache.removeResult(finderPath, finderArgs);
1709    
1710                                    throw processException(e);
1711                            }
1712                            finally {
1713                                    closeSession(session);
1714                            }
1715                    }
1716    
1717                    return count.intValue();
1718            }
1719    
1720            private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "user.companyId = ?";
1721            public static final FinderPath FINDER_PATH_FETCH_BY_CONTACTID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1722                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
1723                            FINDER_CLASS_NAME_ENTITY, "fetchByContactId",
1724                            new String[] { Long.class.getName() },
1725                            UserModelImpl.CONTACTID_COLUMN_BITMASK);
1726            public static final FinderPath FINDER_PATH_COUNT_BY_CONTACTID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1727                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
1728                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByContactId",
1729                            new String[] { Long.class.getName() });
1730    
1731            /**
1732             * Returns the user where contactId = &#63; or throws a {@link NoSuchUserException} if it could not be found.
1733             *
1734             * @param contactId the contact ID
1735             * @return the matching user
1736             * @throws NoSuchUserException if a matching user could not be found
1737             */
1738            @Override
1739            public User findByContactId(long contactId) throws NoSuchUserException {
1740                    User user = fetchByContactId(contactId);
1741    
1742                    if (user == null) {
1743                            StringBundler msg = new StringBundler(4);
1744    
1745                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1746    
1747                            msg.append("contactId=");
1748                            msg.append(contactId);
1749    
1750                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1751    
1752                            if (_log.isDebugEnabled()) {
1753                                    _log.debug(msg.toString());
1754                            }
1755    
1756                            throw new NoSuchUserException(msg.toString());
1757                    }
1758    
1759                    return user;
1760            }
1761    
1762            /**
1763             * Returns the user where contactId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
1764             *
1765             * @param contactId the contact ID
1766             * @return the matching user, or <code>null</code> if a matching user could not be found
1767             */
1768            @Override
1769            public User fetchByContactId(long contactId) {
1770                    return fetchByContactId(contactId, true);
1771            }
1772    
1773            /**
1774             * Returns the user where contactId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
1775             *
1776             * @param contactId the contact ID
1777             * @param retrieveFromCache whether to retrieve from the finder cache
1778             * @return the matching user, or <code>null</code> if a matching user could not be found
1779             */
1780            @Override
1781            public User fetchByContactId(long contactId, boolean retrieveFromCache) {
1782                    Object[] finderArgs = new Object[] { contactId };
1783    
1784                    Object result = null;
1785    
1786                    if (retrieveFromCache) {
1787                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_CONTACTID,
1788                                            finderArgs, this);
1789                    }
1790    
1791                    if (result instanceof User) {
1792                            User user = (User)result;
1793    
1794                            if ((contactId != user.getContactId())) {
1795                                    result = null;
1796                            }
1797                    }
1798    
1799                    if (result == null) {
1800                            StringBundler query = new StringBundler(3);
1801    
1802                            query.append(_SQL_SELECT_USER_WHERE);
1803    
1804                            query.append(_FINDER_COLUMN_CONTACTID_CONTACTID_2);
1805    
1806                            String sql = query.toString();
1807    
1808                            Session session = null;
1809    
1810                            try {
1811                                    session = openSession();
1812    
1813                                    Query q = session.createQuery(sql);
1814    
1815                                    QueryPos qPos = QueryPos.getInstance(q);
1816    
1817                                    qPos.add(contactId);
1818    
1819                                    List<User> list = q.list();
1820    
1821                                    if (list.isEmpty()) {
1822                                            finderCache.putResult(FINDER_PATH_FETCH_BY_CONTACTID,
1823                                                    finderArgs, list);
1824                                    }
1825                                    else {
1826                                            User user = list.get(0);
1827    
1828                                            result = user;
1829    
1830                                            cacheResult(user);
1831    
1832                                            if ((user.getContactId() != contactId)) {
1833                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_CONTACTID,
1834                                                            finderArgs, user);
1835                                            }
1836                                    }
1837                            }
1838                            catch (Exception e) {
1839                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_CONTACTID,
1840                                            finderArgs);
1841    
1842                                    throw processException(e);
1843                            }
1844                            finally {
1845                                    closeSession(session);
1846                            }
1847                    }
1848    
1849                    if (result instanceof List<?>) {
1850                            return null;
1851                    }
1852                    else {
1853                            return (User)result;
1854                    }
1855            }
1856    
1857            /**
1858             * Removes the user where contactId = &#63; from the database.
1859             *
1860             * @param contactId the contact ID
1861             * @return the user that was removed
1862             */
1863            @Override
1864            public User removeByContactId(long contactId) throws NoSuchUserException {
1865                    User user = findByContactId(contactId);
1866    
1867                    return remove(user);
1868            }
1869    
1870            /**
1871             * Returns the number of users where contactId = &#63;.
1872             *
1873             * @param contactId the contact ID
1874             * @return the number of matching users
1875             */
1876            @Override
1877            public int countByContactId(long contactId) {
1878                    FinderPath finderPath = FINDER_PATH_COUNT_BY_CONTACTID;
1879    
1880                    Object[] finderArgs = new Object[] { contactId };
1881    
1882                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
1883    
1884                    if (count == null) {
1885                            StringBundler query = new StringBundler(2);
1886    
1887                            query.append(_SQL_COUNT_USER_WHERE);
1888    
1889                            query.append(_FINDER_COLUMN_CONTACTID_CONTACTID_2);
1890    
1891                            String sql = query.toString();
1892    
1893                            Session session = null;
1894    
1895                            try {
1896                                    session = openSession();
1897    
1898                                    Query q = session.createQuery(sql);
1899    
1900                                    QueryPos qPos = QueryPos.getInstance(q);
1901    
1902                                    qPos.add(contactId);
1903    
1904                                    count = (Long)q.uniqueResult();
1905    
1906                                    finderCache.putResult(finderPath, finderArgs, count);
1907                            }
1908                            catch (Exception e) {
1909                                    finderCache.removeResult(finderPath, finderArgs);
1910    
1911                                    throw processException(e);
1912                            }
1913                            finally {
1914                                    closeSession(session);
1915                            }
1916                    }
1917    
1918                    return count.intValue();
1919            }
1920    
1921            private static final String _FINDER_COLUMN_CONTACTID_CONTACTID_2 = "user.contactId = ?";
1922            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_EMAILADDRESS =
1923                    new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1924                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
1925                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByEmailAddress",
1926                            new String[] {
1927                                    String.class.getName(),
1928                                    
1929                            Integer.class.getName(), Integer.class.getName(),
1930                                    OrderByComparator.class.getName()
1931                            });
1932            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_EMAILADDRESS =
1933                    new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1934                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
1935                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByEmailAddress",
1936                            new String[] { String.class.getName() },
1937                            UserModelImpl.EMAILADDRESS_COLUMN_BITMASK);
1938            public static final FinderPath FINDER_PATH_COUNT_BY_EMAILADDRESS = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
1939                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
1940                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByEmailAddress",
1941                            new String[] { String.class.getName() });
1942    
1943            /**
1944             * Returns all the users where emailAddress = &#63;.
1945             *
1946             * @param emailAddress the email address
1947             * @return the matching users
1948             */
1949            @Override
1950            public List<User> findByEmailAddress(String emailAddress) {
1951                    return findByEmailAddress(emailAddress, QueryUtil.ALL_POS,
1952                            QueryUtil.ALL_POS, null);
1953            }
1954    
1955            /**
1956             * Returns a range of all the users where emailAddress = &#63;.
1957             *
1958             * <p>
1959             * 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.
1960             * </p>
1961             *
1962             * @param emailAddress the email address
1963             * @param start the lower bound of the range of users
1964             * @param end the upper bound of the range of users (not inclusive)
1965             * @return the range of matching users
1966             */
1967            @Override
1968            public List<User> findByEmailAddress(String emailAddress, int start, int end) {
1969                    return findByEmailAddress(emailAddress, start, end, null);
1970            }
1971    
1972            /**
1973             * Returns an ordered range of all the users where emailAddress = &#63;.
1974             *
1975             * <p>
1976             * 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.
1977             * </p>
1978             *
1979             * @param emailAddress the email address
1980             * @param start the lower bound of the range of users
1981             * @param end the upper bound of the range of users (not inclusive)
1982             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1983             * @return the ordered range of matching users
1984             */
1985            @Override
1986            public List<User> findByEmailAddress(String emailAddress, int start,
1987                    int end, OrderByComparator<User> orderByComparator) {
1988                    return findByEmailAddress(emailAddress, start, end, orderByComparator,
1989                            true);
1990            }
1991    
1992            /**
1993             * Returns an ordered range of all the users where emailAddress = &#63;.
1994             *
1995             * <p>
1996             * 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.
1997             * </p>
1998             *
1999             * @param emailAddress the email address
2000             * @param start the lower bound of the range of users
2001             * @param end the upper bound of the range of users (not inclusive)
2002             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2003             * @param retrieveFromCache whether to retrieve from the finder cache
2004             * @return the ordered range of matching users
2005             */
2006            @Override
2007            public List<User> findByEmailAddress(String emailAddress, int start,
2008                    int end, OrderByComparator<User> orderByComparator,
2009                    boolean retrieveFromCache) {
2010                    boolean pagination = true;
2011                    FinderPath finderPath = null;
2012                    Object[] finderArgs = null;
2013    
2014                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2015                                    (orderByComparator == null)) {
2016                            pagination = false;
2017                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_EMAILADDRESS;
2018                            finderArgs = new Object[] { emailAddress };
2019                    }
2020                    else {
2021                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_EMAILADDRESS;
2022                            finderArgs = new Object[] {
2023                                            emailAddress,
2024                                            
2025                                            start, end, orderByComparator
2026                                    };
2027                    }
2028    
2029                    List<User> list = null;
2030    
2031                    if (retrieveFromCache) {
2032                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
2033                                            this);
2034    
2035                            if ((list != null) && !list.isEmpty()) {
2036                                    for (User user : list) {
2037                                            if (!Objects.equals(emailAddress, user.getEmailAddress())) {
2038                                                    list = null;
2039    
2040                                                    break;
2041                                            }
2042                                    }
2043                            }
2044                    }
2045    
2046                    if (list == null) {
2047                            StringBundler query = null;
2048    
2049                            if (orderByComparator != null) {
2050                                    query = new StringBundler(3 +
2051                                                    (orderByComparator.getOrderByFields().length * 2));
2052                            }
2053                            else {
2054                                    query = new StringBundler(3);
2055                            }
2056    
2057                            query.append(_SQL_SELECT_USER_WHERE);
2058    
2059                            boolean bindEmailAddress = false;
2060    
2061                            if (emailAddress == null) {
2062                                    query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_1);
2063                            }
2064                            else if (emailAddress.equals(StringPool.BLANK)) {
2065                                    query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_3);
2066                            }
2067                            else {
2068                                    bindEmailAddress = true;
2069    
2070                                    query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_2);
2071                            }
2072    
2073                            if (orderByComparator != null) {
2074                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2075                                            orderByComparator);
2076                            }
2077                            else
2078                             if (pagination) {
2079                                    query.append(UserModelImpl.ORDER_BY_JPQL);
2080                            }
2081    
2082                            String sql = query.toString();
2083    
2084                            Session session = null;
2085    
2086                            try {
2087                                    session = openSession();
2088    
2089                                    Query q = session.createQuery(sql);
2090    
2091                                    QueryPos qPos = QueryPos.getInstance(q);
2092    
2093                                    if (bindEmailAddress) {
2094                                            qPos.add(emailAddress);
2095                                    }
2096    
2097                                    if (!pagination) {
2098                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
2099                                                            end, false);
2100    
2101                                            Collections.sort(list);
2102    
2103                                            list = Collections.unmodifiableList(list);
2104                                    }
2105                                    else {
2106                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
2107                                                            end);
2108                                    }
2109    
2110                                    cacheResult(list);
2111    
2112                                    finderCache.putResult(finderPath, finderArgs, list);
2113                            }
2114                            catch (Exception e) {
2115                                    finderCache.removeResult(finderPath, finderArgs);
2116    
2117                                    throw processException(e);
2118                            }
2119                            finally {
2120                                    closeSession(session);
2121                            }
2122                    }
2123    
2124                    return list;
2125            }
2126    
2127            /**
2128             * Returns the first user in the ordered set where emailAddress = &#63;.
2129             *
2130             * @param emailAddress the email address
2131             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2132             * @return the first matching user
2133             * @throws NoSuchUserException if a matching user could not be found
2134             */
2135            @Override
2136            public User findByEmailAddress_First(String emailAddress,
2137                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
2138                    User user = fetchByEmailAddress_First(emailAddress, orderByComparator);
2139    
2140                    if (user != null) {
2141                            return user;
2142                    }
2143    
2144                    StringBundler msg = new StringBundler(4);
2145    
2146                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2147    
2148                    msg.append("emailAddress=");
2149                    msg.append(emailAddress);
2150    
2151                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2152    
2153                    throw new NoSuchUserException(msg.toString());
2154            }
2155    
2156            /**
2157             * Returns the first user in the ordered set where emailAddress = &#63;.
2158             *
2159             * @param emailAddress the email address
2160             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2161             * @return the first matching user, or <code>null</code> if a matching user could not be found
2162             */
2163            @Override
2164            public User fetchByEmailAddress_First(String emailAddress,
2165                    OrderByComparator<User> orderByComparator) {
2166                    List<User> list = findByEmailAddress(emailAddress, 0, 1,
2167                                    orderByComparator);
2168    
2169                    if (!list.isEmpty()) {
2170                            return list.get(0);
2171                    }
2172    
2173                    return null;
2174            }
2175    
2176            /**
2177             * Returns the last user in the ordered set where emailAddress = &#63;.
2178             *
2179             * @param emailAddress the email address
2180             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2181             * @return the last matching user
2182             * @throws NoSuchUserException if a matching user could not be found
2183             */
2184            @Override
2185            public User findByEmailAddress_Last(String emailAddress,
2186                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
2187                    User user = fetchByEmailAddress_Last(emailAddress, orderByComparator);
2188    
2189                    if (user != null) {
2190                            return user;
2191                    }
2192    
2193                    StringBundler msg = new StringBundler(4);
2194    
2195                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2196    
2197                    msg.append("emailAddress=");
2198                    msg.append(emailAddress);
2199    
2200                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2201    
2202                    throw new NoSuchUserException(msg.toString());
2203            }
2204    
2205            /**
2206             * Returns the last user in the ordered set where emailAddress = &#63;.
2207             *
2208             * @param emailAddress the email address
2209             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2210             * @return the last matching user, or <code>null</code> if a matching user could not be found
2211             */
2212            @Override
2213            public User fetchByEmailAddress_Last(String emailAddress,
2214                    OrderByComparator<User> orderByComparator) {
2215                    int count = countByEmailAddress(emailAddress);
2216    
2217                    if (count == 0) {
2218                            return null;
2219                    }
2220    
2221                    List<User> list = findByEmailAddress(emailAddress, count - 1, count,
2222                                    orderByComparator);
2223    
2224                    if (!list.isEmpty()) {
2225                            return list.get(0);
2226                    }
2227    
2228                    return null;
2229            }
2230    
2231            /**
2232             * Returns the users before and after the current user in the ordered set where emailAddress = &#63;.
2233             *
2234             * @param userId the primary key of the current user
2235             * @param emailAddress the email address
2236             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2237             * @return the previous, current, and next user
2238             * @throws NoSuchUserException if a user with the primary key could not be found
2239             */
2240            @Override
2241            public User[] findByEmailAddress_PrevAndNext(long userId,
2242                    String emailAddress, OrderByComparator<User> orderByComparator)
2243                    throws NoSuchUserException {
2244                    User user = findByPrimaryKey(userId);
2245    
2246                    Session session = null;
2247    
2248                    try {
2249                            session = openSession();
2250    
2251                            User[] array = new UserImpl[3];
2252    
2253                            array[0] = getByEmailAddress_PrevAndNext(session, user,
2254                                            emailAddress, orderByComparator, true);
2255    
2256                            array[1] = user;
2257    
2258                            array[2] = getByEmailAddress_PrevAndNext(session, user,
2259                                            emailAddress, orderByComparator, false);
2260    
2261                            return array;
2262                    }
2263                    catch (Exception e) {
2264                            throw processException(e);
2265                    }
2266                    finally {
2267                            closeSession(session);
2268                    }
2269            }
2270    
2271            protected User getByEmailAddress_PrevAndNext(Session session, User user,
2272                    String emailAddress, OrderByComparator<User> orderByComparator,
2273                    boolean previous) {
2274                    StringBundler query = null;
2275    
2276                    if (orderByComparator != null) {
2277                            query = new StringBundler(4 +
2278                                            (orderByComparator.getOrderByConditionFields().length * 3) +
2279                                            (orderByComparator.getOrderByFields().length * 3));
2280                    }
2281                    else {
2282                            query = new StringBundler(3);
2283                    }
2284    
2285                    query.append(_SQL_SELECT_USER_WHERE);
2286    
2287                    boolean bindEmailAddress = false;
2288    
2289                    if (emailAddress == null) {
2290                            query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_1);
2291                    }
2292                    else if (emailAddress.equals(StringPool.BLANK)) {
2293                            query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_3);
2294                    }
2295                    else {
2296                            bindEmailAddress = true;
2297    
2298                            query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_2);
2299                    }
2300    
2301                    if (orderByComparator != null) {
2302                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2303    
2304                            if (orderByConditionFields.length > 0) {
2305                                    query.append(WHERE_AND);
2306                            }
2307    
2308                            for (int i = 0; i < orderByConditionFields.length; i++) {
2309                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2310                                    query.append(orderByConditionFields[i]);
2311    
2312                                    if ((i + 1) < orderByConditionFields.length) {
2313                                            if (orderByComparator.isAscending() ^ previous) {
2314                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2315                                            }
2316                                            else {
2317                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2318                                            }
2319                                    }
2320                                    else {
2321                                            if (orderByComparator.isAscending() ^ previous) {
2322                                                    query.append(WHERE_GREATER_THAN);
2323                                            }
2324                                            else {
2325                                                    query.append(WHERE_LESSER_THAN);
2326                                            }
2327                                    }
2328                            }
2329    
2330                            query.append(ORDER_BY_CLAUSE);
2331    
2332                            String[] orderByFields = orderByComparator.getOrderByFields();
2333    
2334                            for (int i = 0; i < orderByFields.length; i++) {
2335                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2336                                    query.append(orderByFields[i]);
2337    
2338                                    if ((i + 1) < orderByFields.length) {
2339                                            if (orderByComparator.isAscending() ^ previous) {
2340                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2341                                            }
2342                                            else {
2343                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2344                                            }
2345                                    }
2346                                    else {
2347                                            if (orderByComparator.isAscending() ^ previous) {
2348                                                    query.append(ORDER_BY_ASC);
2349                                            }
2350                                            else {
2351                                                    query.append(ORDER_BY_DESC);
2352                                            }
2353                                    }
2354                            }
2355                    }
2356                    else {
2357                            query.append(UserModelImpl.ORDER_BY_JPQL);
2358                    }
2359    
2360                    String sql = query.toString();
2361    
2362                    Query q = session.createQuery(sql);
2363    
2364                    q.setFirstResult(0);
2365                    q.setMaxResults(2);
2366    
2367                    QueryPos qPos = QueryPos.getInstance(q);
2368    
2369                    if (bindEmailAddress) {
2370                            qPos.add(emailAddress);
2371                    }
2372    
2373                    if (orderByComparator != null) {
2374                            Object[] values = orderByComparator.getOrderByConditionValues(user);
2375    
2376                            for (Object value : values) {
2377                                    qPos.add(value);
2378                            }
2379                    }
2380    
2381                    List<User> list = q.list();
2382    
2383                    if (list.size() == 2) {
2384                            return list.get(1);
2385                    }
2386                    else {
2387                            return null;
2388                    }
2389            }
2390    
2391            /**
2392             * Removes all the users where emailAddress = &#63; from the database.
2393             *
2394             * @param emailAddress the email address
2395             */
2396            @Override
2397            public void removeByEmailAddress(String emailAddress) {
2398                    for (User user : findByEmailAddress(emailAddress, QueryUtil.ALL_POS,
2399                                    QueryUtil.ALL_POS, null)) {
2400                            remove(user);
2401                    }
2402            }
2403    
2404            /**
2405             * Returns the number of users where emailAddress = &#63;.
2406             *
2407             * @param emailAddress the email address
2408             * @return the number of matching users
2409             */
2410            @Override
2411            public int countByEmailAddress(String emailAddress) {
2412                    FinderPath finderPath = FINDER_PATH_COUNT_BY_EMAILADDRESS;
2413    
2414                    Object[] finderArgs = new Object[] { emailAddress };
2415    
2416                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
2417    
2418                    if (count == null) {
2419                            StringBundler query = new StringBundler(2);
2420    
2421                            query.append(_SQL_COUNT_USER_WHERE);
2422    
2423                            boolean bindEmailAddress = false;
2424    
2425                            if (emailAddress == null) {
2426                                    query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_1);
2427                            }
2428                            else if (emailAddress.equals(StringPool.BLANK)) {
2429                                    query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_3);
2430                            }
2431                            else {
2432                                    bindEmailAddress = true;
2433    
2434                                    query.append(_FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_2);
2435                            }
2436    
2437                            String sql = query.toString();
2438    
2439                            Session session = null;
2440    
2441                            try {
2442                                    session = openSession();
2443    
2444                                    Query q = session.createQuery(sql);
2445    
2446                                    QueryPos qPos = QueryPos.getInstance(q);
2447    
2448                                    if (bindEmailAddress) {
2449                                            qPos.add(emailAddress);
2450                                    }
2451    
2452                                    count = (Long)q.uniqueResult();
2453    
2454                                    finderCache.putResult(finderPath, finderArgs, count);
2455                            }
2456                            catch (Exception e) {
2457                                    finderCache.removeResult(finderPath, finderArgs);
2458    
2459                                    throw processException(e);
2460                            }
2461                            finally {
2462                                    closeSession(session);
2463                            }
2464                    }
2465    
2466                    return count.intValue();
2467            }
2468    
2469            private static final String _FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_1 = "user.emailAddress IS NULL";
2470            private static final String _FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_2 = "user.emailAddress = ?";
2471            private static final String _FINDER_COLUMN_EMAILADDRESS_EMAILADDRESS_3 = "(user.emailAddress IS NULL OR user.emailAddress = '')";
2472            public static final FinderPath FINDER_PATH_FETCH_BY_PORTRAITID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2473                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
2474                            FINDER_CLASS_NAME_ENTITY, "fetchByPortraitId",
2475                            new String[] { Long.class.getName() },
2476                            UserModelImpl.PORTRAITID_COLUMN_BITMASK);
2477            public static final FinderPath FINDER_PATH_COUNT_BY_PORTRAITID = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2478                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
2479                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByPortraitId",
2480                            new String[] { Long.class.getName() });
2481    
2482            /**
2483             * Returns the user where portraitId = &#63; or throws a {@link NoSuchUserException} if it could not be found.
2484             *
2485             * @param portraitId the portrait ID
2486             * @return the matching user
2487             * @throws NoSuchUserException if a matching user could not be found
2488             */
2489            @Override
2490            public User findByPortraitId(long portraitId) throws NoSuchUserException {
2491                    User user = fetchByPortraitId(portraitId);
2492    
2493                    if (user == null) {
2494                            StringBundler msg = new StringBundler(4);
2495    
2496                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2497    
2498                            msg.append("portraitId=");
2499                            msg.append(portraitId);
2500    
2501                            msg.append(StringPool.CLOSE_CURLY_BRACE);
2502    
2503                            if (_log.isDebugEnabled()) {
2504                                    _log.debug(msg.toString());
2505                            }
2506    
2507                            throw new NoSuchUserException(msg.toString());
2508                    }
2509    
2510                    return user;
2511            }
2512    
2513            /**
2514             * Returns the user where portraitId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
2515             *
2516             * @param portraitId the portrait ID
2517             * @return the matching user, or <code>null</code> if a matching user could not be found
2518             */
2519            @Override
2520            public User fetchByPortraitId(long portraitId) {
2521                    return fetchByPortraitId(portraitId, true);
2522            }
2523    
2524            /**
2525             * Returns the user where portraitId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
2526             *
2527             * @param portraitId the portrait ID
2528             * @param retrieveFromCache whether to retrieve from the finder cache
2529             * @return the matching user, or <code>null</code> if a matching user could not be found
2530             */
2531            @Override
2532            public User fetchByPortraitId(long portraitId, boolean retrieveFromCache) {
2533                    Object[] finderArgs = new Object[] { portraitId };
2534    
2535                    Object result = null;
2536    
2537                    if (retrieveFromCache) {
2538                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_PORTRAITID,
2539                                            finderArgs, this);
2540                    }
2541    
2542                    if (result instanceof User) {
2543                            User user = (User)result;
2544    
2545                            if ((portraitId != user.getPortraitId())) {
2546                                    result = null;
2547                            }
2548                    }
2549    
2550                    if (result == null) {
2551                            StringBundler query = new StringBundler(3);
2552    
2553                            query.append(_SQL_SELECT_USER_WHERE);
2554    
2555                            query.append(_FINDER_COLUMN_PORTRAITID_PORTRAITID_2);
2556    
2557                            String sql = query.toString();
2558    
2559                            Session session = null;
2560    
2561                            try {
2562                                    session = openSession();
2563    
2564                                    Query q = session.createQuery(sql);
2565    
2566                                    QueryPos qPos = QueryPos.getInstance(q);
2567    
2568                                    qPos.add(portraitId);
2569    
2570                                    List<User> list = q.list();
2571    
2572                                    if (list.isEmpty()) {
2573                                            finderCache.putResult(FINDER_PATH_FETCH_BY_PORTRAITID,
2574                                                    finderArgs, list);
2575                                    }
2576                                    else {
2577                                            if ((list.size() > 1) && _log.isWarnEnabled()) {
2578                                                    _log.warn(
2579                                                            "UserPersistenceImpl.fetchByPortraitId(long, boolean) with parameters (" +
2580                                                            StringUtil.merge(finderArgs) +
2581                                                            ") 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.");
2582                                            }
2583    
2584                                            User user = list.get(0);
2585    
2586                                            result = user;
2587    
2588                                            cacheResult(user);
2589    
2590                                            if ((user.getPortraitId() != portraitId)) {
2591                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_PORTRAITID,
2592                                                            finderArgs, user);
2593                                            }
2594                                    }
2595                            }
2596                            catch (Exception e) {
2597                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_PORTRAITID,
2598                                            finderArgs);
2599    
2600                                    throw processException(e);
2601                            }
2602                            finally {
2603                                    closeSession(session);
2604                            }
2605                    }
2606    
2607                    if (result instanceof List<?>) {
2608                            return null;
2609                    }
2610                    else {
2611                            return (User)result;
2612                    }
2613            }
2614    
2615            /**
2616             * Removes the user where portraitId = &#63; from the database.
2617             *
2618             * @param portraitId the portrait ID
2619             * @return the user that was removed
2620             */
2621            @Override
2622            public User removeByPortraitId(long portraitId) throws NoSuchUserException {
2623                    User user = findByPortraitId(portraitId);
2624    
2625                    return remove(user);
2626            }
2627    
2628            /**
2629             * Returns the number of users where portraitId = &#63;.
2630             *
2631             * @param portraitId the portrait ID
2632             * @return the number of matching users
2633             */
2634            @Override
2635            public int countByPortraitId(long portraitId) {
2636                    FinderPath finderPath = FINDER_PATH_COUNT_BY_PORTRAITID;
2637    
2638                    Object[] finderArgs = new Object[] { portraitId };
2639    
2640                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
2641    
2642                    if (count == null) {
2643                            StringBundler query = new StringBundler(2);
2644    
2645                            query.append(_SQL_COUNT_USER_WHERE);
2646    
2647                            query.append(_FINDER_COLUMN_PORTRAITID_PORTRAITID_2);
2648    
2649                            String sql = query.toString();
2650    
2651                            Session session = null;
2652    
2653                            try {
2654                                    session = openSession();
2655    
2656                                    Query q = session.createQuery(sql);
2657    
2658                                    QueryPos qPos = QueryPos.getInstance(q);
2659    
2660                                    qPos.add(portraitId);
2661    
2662                                    count = (Long)q.uniqueResult();
2663    
2664                                    finderCache.putResult(finderPath, finderArgs, count);
2665                            }
2666                            catch (Exception e) {
2667                                    finderCache.removeResult(finderPath, finderArgs);
2668    
2669                                    throw processException(e);
2670                            }
2671                            finally {
2672                                    closeSession(session);
2673                            }
2674                    }
2675    
2676                    return count.intValue();
2677            }
2678    
2679            private static final String _FINDER_COLUMN_PORTRAITID_PORTRAITID_2 = "user.portraitId = ?";
2680            public static final FinderPath FINDER_PATH_FETCH_BY_C_U = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2681                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
2682                            FINDER_CLASS_NAME_ENTITY, "fetchByC_U",
2683                            new String[] { Long.class.getName(), Long.class.getName() },
2684                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
2685                            UserModelImpl.USERID_COLUMN_BITMASK);
2686            public static final FinderPath FINDER_PATH_COUNT_BY_C_U = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2687                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
2688                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_U",
2689                            new String[] { Long.class.getName(), Long.class.getName() });
2690    
2691            /**
2692             * Returns the user where companyId = &#63; and userId = &#63; or throws a {@link NoSuchUserException} if it could not be found.
2693             *
2694             * @param companyId the company ID
2695             * @param userId the user ID
2696             * @return the matching user
2697             * @throws NoSuchUserException if a matching user could not be found
2698             */
2699            @Override
2700            public User findByC_U(long companyId, long userId)
2701                    throws NoSuchUserException {
2702                    User user = fetchByC_U(companyId, userId);
2703    
2704                    if (user == null) {
2705                            StringBundler msg = new StringBundler(6);
2706    
2707                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2708    
2709                            msg.append("companyId=");
2710                            msg.append(companyId);
2711    
2712                            msg.append(", userId=");
2713                            msg.append(userId);
2714    
2715                            msg.append(StringPool.CLOSE_CURLY_BRACE);
2716    
2717                            if (_log.isDebugEnabled()) {
2718                                    _log.debug(msg.toString());
2719                            }
2720    
2721                            throw new NoSuchUserException(msg.toString());
2722                    }
2723    
2724                    return user;
2725            }
2726    
2727            /**
2728             * Returns the user where companyId = &#63; and userId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
2729             *
2730             * @param companyId the company ID
2731             * @param userId the user ID
2732             * @return the matching user, or <code>null</code> if a matching user could not be found
2733             */
2734            @Override
2735            public User fetchByC_U(long companyId, long userId) {
2736                    return fetchByC_U(companyId, userId, true);
2737            }
2738    
2739            /**
2740             * 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.
2741             *
2742             * @param companyId the company ID
2743             * @param userId the user ID
2744             * @param retrieveFromCache whether to retrieve from the finder cache
2745             * @return the matching user, or <code>null</code> if a matching user could not be found
2746             */
2747            @Override
2748            public User fetchByC_U(long companyId, long userId,
2749                    boolean retrieveFromCache) {
2750                    Object[] finderArgs = new Object[] { companyId, userId };
2751    
2752                    Object result = null;
2753    
2754                    if (retrieveFromCache) {
2755                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_C_U,
2756                                            finderArgs, this);
2757                    }
2758    
2759                    if (result instanceof User) {
2760                            User user = (User)result;
2761    
2762                            if ((companyId != user.getCompanyId()) ||
2763                                            (userId != user.getUserId())) {
2764                                    result = null;
2765                            }
2766                    }
2767    
2768                    if (result == null) {
2769                            StringBundler query = new StringBundler(4);
2770    
2771                            query.append(_SQL_SELECT_USER_WHERE);
2772    
2773                            query.append(_FINDER_COLUMN_C_U_COMPANYID_2);
2774    
2775                            query.append(_FINDER_COLUMN_C_U_USERID_2);
2776    
2777                            String sql = query.toString();
2778    
2779                            Session session = null;
2780    
2781                            try {
2782                                    session = openSession();
2783    
2784                                    Query q = session.createQuery(sql);
2785    
2786                                    QueryPos qPos = QueryPos.getInstance(q);
2787    
2788                                    qPos.add(companyId);
2789    
2790                                    qPos.add(userId);
2791    
2792                                    List<User> list = q.list();
2793    
2794                                    if (list.isEmpty()) {
2795                                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_U, finderArgs,
2796                                                    list);
2797                                    }
2798                                    else {
2799                                            User user = list.get(0);
2800    
2801                                            result = user;
2802    
2803                                            cacheResult(user);
2804    
2805                                            if ((user.getCompanyId() != companyId) ||
2806                                                            (user.getUserId() != userId)) {
2807                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_U,
2808                                                            finderArgs, user);
2809                                            }
2810                                    }
2811                            }
2812                            catch (Exception e) {
2813                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_U, finderArgs);
2814    
2815                                    throw processException(e);
2816                            }
2817                            finally {
2818                                    closeSession(session);
2819                            }
2820                    }
2821    
2822                    if (result instanceof List<?>) {
2823                            return null;
2824                    }
2825                    else {
2826                            return (User)result;
2827                    }
2828            }
2829    
2830            /**
2831             * Removes the user where companyId = &#63; and userId = &#63; from the database.
2832             *
2833             * @param companyId the company ID
2834             * @param userId the user ID
2835             * @return the user that was removed
2836             */
2837            @Override
2838            public User removeByC_U(long companyId, long userId)
2839                    throws NoSuchUserException {
2840                    User user = findByC_U(companyId, userId);
2841    
2842                    return remove(user);
2843            }
2844    
2845            /**
2846             * Returns the number of users where companyId = &#63; and userId = &#63;.
2847             *
2848             * @param companyId the company ID
2849             * @param userId the user ID
2850             * @return the number of matching users
2851             */
2852            @Override
2853            public int countByC_U(long companyId, long userId) {
2854                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_U;
2855    
2856                    Object[] finderArgs = new Object[] { companyId, userId };
2857    
2858                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
2859    
2860                    if (count == null) {
2861                            StringBundler query = new StringBundler(3);
2862    
2863                            query.append(_SQL_COUNT_USER_WHERE);
2864    
2865                            query.append(_FINDER_COLUMN_C_U_COMPANYID_2);
2866    
2867                            query.append(_FINDER_COLUMN_C_U_USERID_2);
2868    
2869                            String sql = query.toString();
2870    
2871                            Session session = null;
2872    
2873                            try {
2874                                    session = openSession();
2875    
2876                                    Query q = session.createQuery(sql);
2877    
2878                                    QueryPos qPos = QueryPos.getInstance(q);
2879    
2880                                    qPos.add(companyId);
2881    
2882                                    qPos.add(userId);
2883    
2884                                    count = (Long)q.uniqueResult();
2885    
2886                                    finderCache.putResult(finderPath, finderArgs, count);
2887                            }
2888                            catch (Exception e) {
2889                                    finderCache.removeResult(finderPath, finderArgs);
2890    
2891                                    throw processException(e);
2892                            }
2893                            finally {
2894                                    closeSession(session);
2895                            }
2896                    }
2897    
2898                    return count.intValue();
2899            }
2900    
2901            private static final String _FINDER_COLUMN_C_U_COMPANYID_2 = "user.companyId = ? AND ";
2902            private static final String _FINDER_COLUMN_C_U_USERID_2 = "user.userId = ?";
2903            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_CD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2904                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
2905                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_CD",
2906                            new String[] {
2907                                    Long.class.getName(), Date.class.getName(),
2908                                    
2909                            Integer.class.getName(), Integer.class.getName(),
2910                                    OrderByComparator.class.getName()
2911                            });
2912            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2913                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
2914                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_CD",
2915                            new String[] { Long.class.getName(), Date.class.getName() },
2916                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
2917                            UserModelImpl.CREATEDATE_COLUMN_BITMASK);
2918            public static final FinderPath FINDER_PATH_COUNT_BY_C_CD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
2919                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
2920                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_CD",
2921                            new String[] { Long.class.getName(), Date.class.getName() });
2922    
2923            /**
2924             * Returns all the users where companyId = &#63; and createDate = &#63;.
2925             *
2926             * @param companyId the company ID
2927             * @param createDate the create date
2928             * @return the matching users
2929             */
2930            @Override
2931            public List<User> findByC_CD(long companyId, Date createDate) {
2932                    return findByC_CD(companyId, createDate, QueryUtil.ALL_POS,
2933                            QueryUtil.ALL_POS, null);
2934            }
2935    
2936            /**
2937             * Returns a range of all the users where companyId = &#63; and createDate = &#63;.
2938             *
2939             * <p>
2940             * 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.
2941             * </p>
2942             *
2943             * @param companyId the company ID
2944             * @param createDate the create date
2945             * @param start the lower bound of the range of users
2946             * @param end the upper bound of the range of users (not inclusive)
2947             * @return the range of matching users
2948             */
2949            @Override
2950            public List<User> findByC_CD(long companyId, Date createDate, int start,
2951                    int end) {
2952                    return findByC_CD(companyId, createDate, start, end, null);
2953            }
2954    
2955            /**
2956             * Returns an ordered range of all the users where companyId = &#63; and createDate = &#63;.
2957             *
2958             * <p>
2959             * 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.
2960             * </p>
2961             *
2962             * @param companyId the company ID
2963             * @param createDate the create date
2964             * @param start the lower bound of the range of users
2965             * @param end the upper bound of the range of users (not inclusive)
2966             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2967             * @return the ordered range of matching users
2968             */
2969            @Override
2970            public List<User> findByC_CD(long companyId, Date createDate, int start,
2971                    int end, OrderByComparator<User> orderByComparator) {
2972                    return findByC_CD(companyId, createDate, start, end, orderByComparator,
2973                            true);
2974            }
2975    
2976            /**
2977             * Returns an ordered range of all the users where companyId = &#63; and createDate = &#63;.
2978             *
2979             * <p>
2980             * 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.
2981             * </p>
2982             *
2983             * @param companyId the company ID
2984             * @param createDate the create date
2985             * @param start the lower bound of the range of users
2986             * @param end the upper bound of the range of users (not inclusive)
2987             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2988             * @param retrieveFromCache whether to retrieve from the finder cache
2989             * @return the ordered range of matching users
2990             */
2991            @Override
2992            public List<User> findByC_CD(long companyId, Date createDate, int start,
2993                    int end, OrderByComparator<User> orderByComparator,
2994                    boolean retrieveFromCache) {
2995                    boolean pagination = true;
2996                    FinderPath finderPath = null;
2997                    Object[] finderArgs = null;
2998    
2999                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3000                                    (orderByComparator == null)) {
3001                            pagination = false;
3002                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD;
3003                            finderArgs = new Object[] { companyId, createDate };
3004                    }
3005                    else {
3006                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_CD;
3007                            finderArgs = new Object[] {
3008                                            companyId, createDate,
3009                                            
3010                                            start, end, orderByComparator
3011                                    };
3012                    }
3013    
3014                    List<User> list = null;
3015    
3016                    if (retrieveFromCache) {
3017                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
3018                                            this);
3019    
3020                            if ((list != null) && !list.isEmpty()) {
3021                                    for (User user : list) {
3022                                            if ((companyId != user.getCompanyId()) ||
3023                                                            !Objects.equals(createDate, user.getCreateDate())) {
3024                                                    list = null;
3025    
3026                                                    break;
3027                                            }
3028                                    }
3029                            }
3030                    }
3031    
3032                    if (list == null) {
3033                            StringBundler query = null;
3034    
3035                            if (orderByComparator != null) {
3036                                    query = new StringBundler(4 +
3037                                                    (orderByComparator.getOrderByFields().length * 2));
3038                            }
3039                            else {
3040                                    query = new StringBundler(4);
3041                            }
3042    
3043                            query.append(_SQL_SELECT_USER_WHERE);
3044    
3045                            query.append(_FINDER_COLUMN_C_CD_COMPANYID_2);
3046    
3047                            boolean bindCreateDate = false;
3048    
3049                            if (createDate == null) {
3050                                    query.append(_FINDER_COLUMN_C_CD_CREATEDATE_1);
3051                            }
3052                            else {
3053                                    bindCreateDate = true;
3054    
3055                                    query.append(_FINDER_COLUMN_C_CD_CREATEDATE_2);
3056                            }
3057    
3058                            if (orderByComparator != null) {
3059                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3060                                            orderByComparator);
3061                            }
3062                            else
3063                             if (pagination) {
3064                                    query.append(UserModelImpl.ORDER_BY_JPQL);
3065                            }
3066    
3067                            String sql = query.toString();
3068    
3069                            Session session = null;
3070    
3071                            try {
3072                                    session = openSession();
3073    
3074                                    Query q = session.createQuery(sql);
3075    
3076                                    QueryPos qPos = QueryPos.getInstance(q);
3077    
3078                                    qPos.add(companyId);
3079    
3080                                    if (bindCreateDate) {
3081                                            qPos.add(new Timestamp(createDate.getTime()));
3082                                    }
3083    
3084                                    if (!pagination) {
3085                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
3086                                                            end, false);
3087    
3088                                            Collections.sort(list);
3089    
3090                                            list = Collections.unmodifiableList(list);
3091                                    }
3092                                    else {
3093                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
3094                                                            end);
3095                                    }
3096    
3097                                    cacheResult(list);
3098    
3099                                    finderCache.putResult(finderPath, finderArgs, list);
3100                            }
3101                            catch (Exception e) {
3102                                    finderCache.removeResult(finderPath, finderArgs);
3103    
3104                                    throw processException(e);
3105                            }
3106                            finally {
3107                                    closeSession(session);
3108                            }
3109                    }
3110    
3111                    return list;
3112            }
3113    
3114            /**
3115             * Returns the first user in the ordered set where companyId = &#63; and createDate = &#63;.
3116             *
3117             * @param companyId the company ID
3118             * @param createDate the create date
3119             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3120             * @return the first matching user
3121             * @throws NoSuchUserException if a matching user could not be found
3122             */
3123            @Override
3124            public User findByC_CD_First(long companyId, Date createDate,
3125                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
3126                    User user = fetchByC_CD_First(companyId, createDate, orderByComparator);
3127    
3128                    if (user != null) {
3129                            return user;
3130                    }
3131    
3132                    StringBundler msg = new StringBundler(6);
3133    
3134                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3135    
3136                    msg.append("companyId=");
3137                    msg.append(companyId);
3138    
3139                    msg.append(", createDate=");
3140                    msg.append(createDate);
3141    
3142                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3143    
3144                    throw new NoSuchUserException(msg.toString());
3145            }
3146    
3147            /**
3148             * Returns the first user in the ordered set where companyId = &#63; and createDate = &#63;.
3149             *
3150             * @param companyId the company ID
3151             * @param createDate the create date
3152             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3153             * @return the first matching user, or <code>null</code> if a matching user could not be found
3154             */
3155            @Override
3156            public User fetchByC_CD_First(long companyId, Date createDate,
3157                    OrderByComparator<User> orderByComparator) {
3158                    List<User> list = findByC_CD(companyId, createDate, 0, 1,
3159                                    orderByComparator);
3160    
3161                    if (!list.isEmpty()) {
3162                            return list.get(0);
3163                    }
3164    
3165                    return null;
3166            }
3167    
3168            /**
3169             * Returns the last user in the ordered set where companyId = &#63; and createDate = &#63;.
3170             *
3171             * @param companyId the company ID
3172             * @param createDate the create date
3173             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3174             * @return the last matching user
3175             * @throws NoSuchUserException if a matching user could not be found
3176             */
3177            @Override
3178            public User findByC_CD_Last(long companyId, Date createDate,
3179                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
3180                    User user = fetchByC_CD_Last(companyId, createDate, orderByComparator);
3181    
3182                    if (user != null) {
3183                            return user;
3184                    }
3185    
3186                    StringBundler msg = new StringBundler(6);
3187    
3188                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3189    
3190                    msg.append("companyId=");
3191                    msg.append(companyId);
3192    
3193                    msg.append(", createDate=");
3194                    msg.append(createDate);
3195    
3196                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3197    
3198                    throw new NoSuchUserException(msg.toString());
3199            }
3200    
3201            /**
3202             * Returns the last user in the ordered set where companyId = &#63; and createDate = &#63;.
3203             *
3204             * @param companyId the company ID
3205             * @param createDate the create date
3206             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3207             * @return the last matching user, or <code>null</code> if a matching user could not be found
3208             */
3209            @Override
3210            public User fetchByC_CD_Last(long companyId, Date createDate,
3211                    OrderByComparator<User> orderByComparator) {
3212                    int count = countByC_CD(companyId, createDate);
3213    
3214                    if (count == 0) {
3215                            return null;
3216                    }
3217    
3218                    List<User> list = findByC_CD(companyId, createDate, count - 1, count,
3219                                    orderByComparator);
3220    
3221                    if (!list.isEmpty()) {
3222                            return list.get(0);
3223                    }
3224    
3225                    return null;
3226            }
3227    
3228            /**
3229             * Returns the users before and after the current user in the ordered set where companyId = &#63; and createDate = &#63;.
3230             *
3231             * @param userId the primary key of the current user
3232             * @param companyId the company ID
3233             * @param createDate the create date
3234             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3235             * @return the previous, current, and next user
3236             * @throws NoSuchUserException if a user with the primary key could not be found
3237             */
3238            @Override
3239            public User[] findByC_CD_PrevAndNext(long userId, long companyId,
3240                    Date createDate, OrderByComparator<User> orderByComparator)
3241                    throws NoSuchUserException {
3242                    User user = findByPrimaryKey(userId);
3243    
3244                    Session session = null;
3245    
3246                    try {
3247                            session = openSession();
3248    
3249                            User[] array = new UserImpl[3];
3250    
3251                            array[0] = getByC_CD_PrevAndNext(session, user, companyId,
3252                                            createDate, orderByComparator, true);
3253    
3254                            array[1] = user;
3255    
3256                            array[2] = getByC_CD_PrevAndNext(session, user, companyId,
3257                                            createDate, orderByComparator, false);
3258    
3259                            return array;
3260                    }
3261                    catch (Exception e) {
3262                            throw processException(e);
3263                    }
3264                    finally {
3265                            closeSession(session);
3266                    }
3267            }
3268    
3269            protected User getByC_CD_PrevAndNext(Session session, User user,
3270                    long companyId, Date createDate,
3271                    OrderByComparator<User> orderByComparator, boolean previous) {
3272                    StringBundler query = null;
3273    
3274                    if (orderByComparator != null) {
3275                            query = new StringBundler(5 +
3276                                            (orderByComparator.getOrderByConditionFields().length * 3) +
3277                                            (orderByComparator.getOrderByFields().length * 3));
3278                    }
3279                    else {
3280                            query = new StringBundler(4);
3281                    }
3282    
3283                    query.append(_SQL_SELECT_USER_WHERE);
3284    
3285                    query.append(_FINDER_COLUMN_C_CD_COMPANYID_2);
3286    
3287                    boolean bindCreateDate = false;
3288    
3289                    if (createDate == null) {
3290                            query.append(_FINDER_COLUMN_C_CD_CREATEDATE_1);
3291                    }
3292                    else {
3293                            bindCreateDate = true;
3294    
3295                            query.append(_FINDER_COLUMN_C_CD_CREATEDATE_2);
3296                    }
3297    
3298                    if (orderByComparator != null) {
3299                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
3300    
3301                            if (orderByConditionFields.length > 0) {
3302                                    query.append(WHERE_AND);
3303                            }
3304    
3305                            for (int i = 0; i < orderByConditionFields.length; i++) {
3306                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3307                                    query.append(orderByConditionFields[i]);
3308    
3309                                    if ((i + 1) < orderByConditionFields.length) {
3310                                            if (orderByComparator.isAscending() ^ previous) {
3311                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
3312                                            }
3313                                            else {
3314                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
3315                                            }
3316                                    }
3317                                    else {
3318                                            if (orderByComparator.isAscending() ^ previous) {
3319                                                    query.append(WHERE_GREATER_THAN);
3320                                            }
3321                                            else {
3322                                                    query.append(WHERE_LESSER_THAN);
3323                                            }
3324                                    }
3325                            }
3326    
3327                            query.append(ORDER_BY_CLAUSE);
3328    
3329                            String[] orderByFields = orderByComparator.getOrderByFields();
3330    
3331                            for (int i = 0; i < orderByFields.length; i++) {
3332                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3333                                    query.append(orderByFields[i]);
3334    
3335                                    if ((i + 1) < orderByFields.length) {
3336                                            if (orderByComparator.isAscending() ^ previous) {
3337                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
3338                                            }
3339                                            else {
3340                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
3341                                            }
3342                                    }
3343                                    else {
3344                                            if (orderByComparator.isAscending() ^ previous) {
3345                                                    query.append(ORDER_BY_ASC);
3346                                            }
3347                                            else {
3348                                                    query.append(ORDER_BY_DESC);
3349                                            }
3350                                    }
3351                            }
3352                    }
3353                    else {
3354                            query.append(UserModelImpl.ORDER_BY_JPQL);
3355                    }
3356    
3357                    String sql = query.toString();
3358    
3359                    Query q = session.createQuery(sql);
3360    
3361                    q.setFirstResult(0);
3362                    q.setMaxResults(2);
3363    
3364                    QueryPos qPos = QueryPos.getInstance(q);
3365    
3366                    qPos.add(companyId);
3367    
3368                    if (bindCreateDate) {
3369                            qPos.add(new Timestamp(createDate.getTime()));
3370                    }
3371    
3372                    if (orderByComparator != null) {
3373                            Object[] values = orderByComparator.getOrderByConditionValues(user);
3374    
3375                            for (Object value : values) {
3376                                    qPos.add(value);
3377                            }
3378                    }
3379    
3380                    List<User> list = q.list();
3381    
3382                    if (list.size() == 2) {
3383                            return list.get(1);
3384                    }
3385                    else {
3386                            return null;
3387                    }
3388            }
3389    
3390            /**
3391             * Removes all the users where companyId = &#63; and createDate = &#63; from the database.
3392             *
3393             * @param companyId the company ID
3394             * @param createDate the create date
3395             */
3396            @Override
3397            public void removeByC_CD(long companyId, Date createDate) {
3398                    for (User user : findByC_CD(companyId, createDate, QueryUtil.ALL_POS,
3399                                    QueryUtil.ALL_POS, null)) {
3400                            remove(user);
3401                    }
3402            }
3403    
3404            /**
3405             * Returns the number of users where companyId = &#63; and createDate = &#63;.
3406             *
3407             * @param companyId the company ID
3408             * @param createDate the create date
3409             * @return the number of matching users
3410             */
3411            @Override
3412            public int countByC_CD(long companyId, Date createDate) {
3413                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_CD;
3414    
3415                    Object[] finderArgs = new Object[] { companyId, createDate };
3416    
3417                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
3418    
3419                    if (count == null) {
3420                            StringBundler query = new StringBundler(3);
3421    
3422                            query.append(_SQL_COUNT_USER_WHERE);
3423    
3424                            query.append(_FINDER_COLUMN_C_CD_COMPANYID_2);
3425    
3426                            boolean bindCreateDate = false;
3427    
3428                            if (createDate == null) {
3429                                    query.append(_FINDER_COLUMN_C_CD_CREATEDATE_1);
3430                            }
3431                            else {
3432                                    bindCreateDate = true;
3433    
3434                                    query.append(_FINDER_COLUMN_C_CD_CREATEDATE_2);
3435                            }
3436    
3437                            String sql = query.toString();
3438    
3439                            Session session = null;
3440    
3441                            try {
3442                                    session = openSession();
3443    
3444                                    Query q = session.createQuery(sql);
3445    
3446                                    QueryPos qPos = QueryPos.getInstance(q);
3447    
3448                                    qPos.add(companyId);
3449    
3450                                    if (bindCreateDate) {
3451                                            qPos.add(new Timestamp(createDate.getTime()));
3452                                    }
3453    
3454                                    count = (Long)q.uniqueResult();
3455    
3456                                    finderCache.putResult(finderPath, finderArgs, count);
3457                            }
3458                            catch (Exception e) {
3459                                    finderCache.removeResult(finderPath, finderArgs);
3460    
3461                                    throw processException(e);
3462                            }
3463                            finally {
3464                                    closeSession(session);
3465                            }
3466                    }
3467    
3468                    return count.intValue();
3469            }
3470    
3471            private static final String _FINDER_COLUMN_C_CD_COMPANYID_2 = "user.companyId = ? AND ";
3472            private static final String _FINDER_COLUMN_C_CD_CREATEDATE_1 = "user.createDate IS NULL";
3473            private static final String _FINDER_COLUMN_C_CD_CREATEDATE_2 = "user.createDate = ?";
3474            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_MD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
3475                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
3476                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_MD",
3477                            new String[] {
3478                                    Long.class.getName(), Date.class.getName(),
3479                                    
3480                            Integer.class.getName(), Integer.class.getName(),
3481                                    OrderByComparator.class.getName()
3482                            });
3483            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_MD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
3484                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
3485                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_MD",
3486                            new String[] { Long.class.getName(), Date.class.getName() },
3487                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
3488                            UserModelImpl.MODIFIEDDATE_COLUMN_BITMASK);
3489            public static final FinderPath FINDER_PATH_COUNT_BY_C_MD = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
3490                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
3491                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_MD",
3492                            new String[] { Long.class.getName(), Date.class.getName() });
3493    
3494            /**
3495             * Returns all the users where companyId = &#63; and modifiedDate = &#63;.
3496             *
3497             * @param companyId the company ID
3498             * @param modifiedDate the modified date
3499             * @return the matching users
3500             */
3501            @Override
3502            public List<User> findByC_MD(long companyId, Date modifiedDate) {
3503                    return findByC_MD(companyId, modifiedDate, QueryUtil.ALL_POS,
3504                            QueryUtil.ALL_POS, null);
3505            }
3506    
3507            /**
3508             * Returns a range of all the users where companyId = &#63; and modifiedDate = &#63;.
3509             *
3510             * <p>
3511             * 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.
3512             * </p>
3513             *
3514             * @param companyId the company ID
3515             * @param modifiedDate the modified date
3516             * @param start the lower bound of the range of users
3517             * @param end the upper bound of the range of users (not inclusive)
3518             * @return the range of matching users
3519             */
3520            @Override
3521            public List<User> findByC_MD(long companyId, Date modifiedDate, int start,
3522                    int end) {
3523                    return findByC_MD(companyId, modifiedDate, start, end, null);
3524            }
3525    
3526            /**
3527             * Returns an ordered range of all the users where companyId = &#63; and modifiedDate = &#63;.
3528             *
3529             * <p>
3530             * 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.
3531             * </p>
3532             *
3533             * @param companyId the company ID
3534             * @param modifiedDate the modified date
3535             * @param start the lower bound of the range of users
3536             * @param end the upper bound of the range of users (not inclusive)
3537             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3538             * @return the ordered range of matching users
3539             */
3540            @Override
3541            public List<User> findByC_MD(long companyId, Date modifiedDate, int start,
3542                    int end, OrderByComparator<User> orderByComparator) {
3543                    return findByC_MD(companyId, modifiedDate, start, end,
3544                            orderByComparator, true);
3545            }
3546    
3547            /**
3548             * Returns an ordered range of all the users where companyId = &#63; and modifiedDate = &#63;.
3549             *
3550             * <p>
3551             * 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.
3552             * </p>
3553             *
3554             * @param companyId the company ID
3555             * @param modifiedDate the modified date
3556             * @param start the lower bound of the range of users
3557             * @param end the upper bound of the range of users (not inclusive)
3558             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3559             * @param retrieveFromCache whether to retrieve from the finder cache
3560             * @return the ordered range of matching users
3561             */
3562            @Override
3563            public List<User> findByC_MD(long companyId, Date modifiedDate, int start,
3564                    int end, OrderByComparator<User> orderByComparator,
3565                    boolean retrieveFromCache) {
3566                    boolean pagination = true;
3567                    FinderPath finderPath = null;
3568                    Object[] finderArgs = null;
3569    
3570                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3571                                    (orderByComparator == null)) {
3572                            pagination = false;
3573                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_MD;
3574                            finderArgs = new Object[] { companyId, modifiedDate };
3575                    }
3576                    else {
3577                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_MD;
3578                            finderArgs = new Object[] {
3579                                            companyId, modifiedDate,
3580                                            
3581                                            start, end, orderByComparator
3582                                    };
3583                    }
3584    
3585                    List<User> list = null;
3586    
3587                    if (retrieveFromCache) {
3588                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
3589                                            this);
3590    
3591                            if ((list != null) && !list.isEmpty()) {
3592                                    for (User user : list) {
3593                                            if ((companyId != user.getCompanyId()) ||
3594                                                            !Objects.equals(modifiedDate, 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.isDebugEnabled()) {
4083                                    _log.debug(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.isDebugEnabled()) {
4313                                    _log.debug(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                                            !Objects.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.isDebugEnabled()) {
4567                                    _log.debug(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                                            !Objects.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.isDebugEnabled()) {
4821                                    _log.debug(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.isDebugEnabled()) {
5051                                    _log.debug(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                                            !Objects.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.isDebugEnabled()) {
5312                                    _log.debug(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                                            !Objects.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                                                            !Objects.equals(createDate, user.getCreateDate()) ||
6202                                                            !Objects.equals(modifiedDate, user.getModifiedDate())) {
6203                                                    list = null;
6204    
6205                                                    break;
6206                                            }
6207                                    }
6208                            }
6209                    }
6210    
6211                    if (list == null) {
6212                            StringBundler query = null;
6213    
6214                            if (orderByComparator != null) {
6215                                    query = new StringBundler(5 +
6216                                                    (orderByComparator.getOrderByFields().length * 2));
6217                            }
6218                            else {
6219                                    query = new StringBundler(5);
6220                            }
6221    
6222                            query.append(_SQL_SELECT_USER_WHERE);
6223    
6224                            query.append(_FINDER_COLUMN_C_CD_MD_COMPANYID_2);
6225    
6226                            boolean bindCreateDate = false;
6227    
6228                            if (createDate == null) {
6229                                    query.append(_FINDER_COLUMN_C_CD_MD_CREATEDATE_1);
6230                            }
6231                            else {
6232                                    bindCreateDate = true;
6233    
6234                                    query.append(_FINDER_COLUMN_C_CD_MD_CREATEDATE_2);
6235                            }
6236    
6237                            boolean bindModifiedDate = false;
6238    
6239                            if (modifiedDate == null) {
6240                                    query.append(_FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_1);
6241                            }
6242                            else {
6243                                    bindModifiedDate = true;
6244    
6245                                    query.append(_FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_2);
6246                            }
6247    
6248                            if (orderByComparator != null) {
6249                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
6250                                            orderByComparator);
6251                            }
6252                            else
6253                             if (pagination) {
6254                                    query.append(UserModelImpl.ORDER_BY_JPQL);
6255                            }
6256    
6257                            String sql = query.toString();
6258    
6259                            Session session = null;
6260    
6261                            try {
6262                                    session = openSession();
6263    
6264                                    Query q = session.createQuery(sql);
6265    
6266                                    QueryPos qPos = QueryPos.getInstance(q);
6267    
6268                                    qPos.add(companyId);
6269    
6270                                    if (bindCreateDate) {
6271                                            qPos.add(new Timestamp(createDate.getTime()));
6272                                    }
6273    
6274                                    if (bindModifiedDate) {
6275                                            qPos.add(new Timestamp(modifiedDate.getTime()));
6276                                    }
6277    
6278                                    if (!pagination) {
6279                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
6280                                                            end, false);
6281    
6282                                            Collections.sort(list);
6283    
6284                                            list = Collections.unmodifiableList(list);
6285                                    }
6286                                    else {
6287                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
6288                                                            end);
6289                                    }
6290    
6291                                    cacheResult(list);
6292    
6293                                    finderCache.putResult(finderPath, finderArgs, list);
6294                            }
6295                            catch (Exception e) {
6296                                    finderCache.removeResult(finderPath, finderArgs);
6297    
6298                                    throw processException(e);
6299                            }
6300                            finally {
6301                                    closeSession(session);
6302                            }
6303                    }
6304    
6305                    return list;
6306            }
6307    
6308            /**
6309             * Returns the first user in the ordered set where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6310             *
6311             * @param companyId the company ID
6312             * @param createDate the create date
6313             * @param modifiedDate the modified date
6314             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6315             * @return the first matching user
6316             * @throws NoSuchUserException if a matching user could not be found
6317             */
6318            @Override
6319            public User findByC_CD_MD_First(long companyId, Date createDate,
6320                    Date modifiedDate, OrderByComparator<User> orderByComparator)
6321                    throws NoSuchUserException {
6322                    User user = fetchByC_CD_MD_First(companyId, createDate, modifiedDate,
6323                                    orderByComparator);
6324    
6325                    if (user != null) {
6326                            return user;
6327                    }
6328    
6329                    StringBundler msg = new StringBundler(8);
6330    
6331                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
6332    
6333                    msg.append("companyId=");
6334                    msg.append(companyId);
6335    
6336                    msg.append(", createDate=");
6337                    msg.append(createDate);
6338    
6339                    msg.append(", modifiedDate=");
6340                    msg.append(modifiedDate);
6341    
6342                    msg.append(StringPool.CLOSE_CURLY_BRACE);
6343    
6344                    throw new NoSuchUserException(msg.toString());
6345            }
6346    
6347            /**
6348             * Returns the first user in the ordered set where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6349             *
6350             * @param companyId the company ID
6351             * @param createDate the create date
6352             * @param modifiedDate the modified date
6353             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6354             * @return the first matching user, or <code>null</code> if a matching user could not be found
6355             */
6356            @Override
6357            public User fetchByC_CD_MD_First(long companyId, Date createDate,
6358                    Date modifiedDate, OrderByComparator<User> orderByComparator) {
6359                    List<User> list = findByC_CD_MD(companyId, createDate, modifiedDate, 0,
6360                                    1, orderByComparator);
6361    
6362                    if (!list.isEmpty()) {
6363                            return list.get(0);
6364                    }
6365    
6366                    return null;
6367            }
6368    
6369            /**
6370             * Returns the last user in the ordered set where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6371             *
6372             * @param companyId the company ID
6373             * @param createDate the create date
6374             * @param modifiedDate the modified date
6375             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6376             * @return the last matching user
6377             * @throws NoSuchUserException if a matching user could not be found
6378             */
6379            @Override
6380            public User findByC_CD_MD_Last(long companyId, Date createDate,
6381                    Date modifiedDate, OrderByComparator<User> orderByComparator)
6382                    throws NoSuchUserException {
6383                    User user = fetchByC_CD_MD_Last(companyId, createDate, modifiedDate,
6384                                    orderByComparator);
6385    
6386                    if (user != null) {
6387                            return user;
6388                    }
6389    
6390                    StringBundler msg = new StringBundler(8);
6391    
6392                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
6393    
6394                    msg.append("companyId=");
6395                    msg.append(companyId);
6396    
6397                    msg.append(", createDate=");
6398                    msg.append(createDate);
6399    
6400                    msg.append(", modifiedDate=");
6401                    msg.append(modifiedDate);
6402    
6403                    msg.append(StringPool.CLOSE_CURLY_BRACE);
6404    
6405                    throw new NoSuchUserException(msg.toString());
6406            }
6407    
6408            /**
6409             * Returns the last user in the ordered set where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6410             *
6411             * @param companyId the company ID
6412             * @param createDate the create date
6413             * @param modifiedDate the modified date
6414             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6415             * @return the last matching user, or <code>null</code> if a matching user could not be found
6416             */
6417            @Override
6418            public User fetchByC_CD_MD_Last(long companyId, Date createDate,
6419                    Date modifiedDate, OrderByComparator<User> orderByComparator) {
6420                    int count = countByC_CD_MD(companyId, createDate, modifiedDate);
6421    
6422                    if (count == 0) {
6423                            return null;
6424                    }
6425    
6426                    List<User> list = findByC_CD_MD(companyId, createDate, modifiedDate,
6427                                    count - 1, count, orderByComparator);
6428    
6429                    if (!list.isEmpty()) {
6430                            return list.get(0);
6431                    }
6432    
6433                    return null;
6434            }
6435    
6436            /**
6437             * Returns the users before and after the current user in the ordered set where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6438             *
6439             * @param userId the primary key of the current user
6440             * @param companyId the company ID
6441             * @param createDate the create date
6442             * @param modifiedDate the modified date
6443             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6444             * @return the previous, current, and next user
6445             * @throws NoSuchUserException if a user with the primary key could not be found
6446             */
6447            @Override
6448            public User[] findByC_CD_MD_PrevAndNext(long userId, long companyId,
6449                    Date createDate, Date modifiedDate,
6450                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
6451                    User user = findByPrimaryKey(userId);
6452    
6453                    Session session = null;
6454    
6455                    try {
6456                            session = openSession();
6457    
6458                            User[] array = new UserImpl[3];
6459    
6460                            array[0] = getByC_CD_MD_PrevAndNext(session, user, companyId,
6461                                            createDate, modifiedDate, orderByComparator, true);
6462    
6463                            array[1] = user;
6464    
6465                            array[2] = getByC_CD_MD_PrevAndNext(session, user, companyId,
6466                                            createDate, modifiedDate, orderByComparator, false);
6467    
6468                            return array;
6469                    }
6470                    catch (Exception e) {
6471                            throw processException(e);
6472                    }
6473                    finally {
6474                            closeSession(session);
6475                    }
6476            }
6477    
6478            protected User getByC_CD_MD_PrevAndNext(Session session, User user,
6479                    long companyId, Date createDate, Date modifiedDate,
6480                    OrderByComparator<User> orderByComparator, boolean previous) {
6481                    StringBundler query = null;
6482    
6483                    if (orderByComparator != null) {
6484                            query = new StringBundler(6 +
6485                                            (orderByComparator.getOrderByConditionFields().length * 3) +
6486                                            (orderByComparator.getOrderByFields().length * 3));
6487                    }
6488                    else {
6489                            query = new StringBundler(5);
6490                    }
6491    
6492                    query.append(_SQL_SELECT_USER_WHERE);
6493    
6494                    query.append(_FINDER_COLUMN_C_CD_MD_COMPANYID_2);
6495    
6496                    boolean bindCreateDate = false;
6497    
6498                    if (createDate == null) {
6499                            query.append(_FINDER_COLUMN_C_CD_MD_CREATEDATE_1);
6500                    }
6501                    else {
6502                            bindCreateDate = true;
6503    
6504                            query.append(_FINDER_COLUMN_C_CD_MD_CREATEDATE_2);
6505                    }
6506    
6507                    boolean bindModifiedDate = false;
6508    
6509                    if (modifiedDate == null) {
6510                            query.append(_FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_1);
6511                    }
6512                    else {
6513                            bindModifiedDate = true;
6514    
6515                            query.append(_FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_2);
6516                    }
6517    
6518                    if (orderByComparator != null) {
6519                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
6520    
6521                            if (orderByConditionFields.length > 0) {
6522                                    query.append(WHERE_AND);
6523                            }
6524    
6525                            for (int i = 0; i < orderByConditionFields.length; i++) {
6526                                    query.append(_ORDER_BY_ENTITY_ALIAS);
6527                                    query.append(orderByConditionFields[i]);
6528    
6529                                    if ((i + 1) < orderByConditionFields.length) {
6530                                            if (orderByComparator.isAscending() ^ previous) {
6531                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
6532                                            }
6533                                            else {
6534                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
6535                                            }
6536                                    }
6537                                    else {
6538                                            if (orderByComparator.isAscending() ^ previous) {
6539                                                    query.append(WHERE_GREATER_THAN);
6540                                            }
6541                                            else {
6542                                                    query.append(WHERE_LESSER_THAN);
6543                                            }
6544                                    }
6545                            }
6546    
6547                            query.append(ORDER_BY_CLAUSE);
6548    
6549                            String[] orderByFields = orderByComparator.getOrderByFields();
6550    
6551                            for (int i = 0; i < orderByFields.length; i++) {
6552                                    query.append(_ORDER_BY_ENTITY_ALIAS);
6553                                    query.append(orderByFields[i]);
6554    
6555                                    if ((i + 1) < orderByFields.length) {
6556                                            if (orderByComparator.isAscending() ^ previous) {
6557                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
6558                                            }
6559                                            else {
6560                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
6561                                            }
6562                                    }
6563                                    else {
6564                                            if (orderByComparator.isAscending() ^ previous) {
6565                                                    query.append(ORDER_BY_ASC);
6566                                            }
6567                                            else {
6568                                                    query.append(ORDER_BY_DESC);
6569                                            }
6570                                    }
6571                            }
6572                    }
6573                    else {
6574                            query.append(UserModelImpl.ORDER_BY_JPQL);
6575                    }
6576    
6577                    String sql = query.toString();
6578    
6579                    Query q = session.createQuery(sql);
6580    
6581                    q.setFirstResult(0);
6582                    q.setMaxResults(2);
6583    
6584                    QueryPos qPos = QueryPos.getInstance(q);
6585    
6586                    qPos.add(companyId);
6587    
6588                    if (bindCreateDate) {
6589                            qPos.add(new Timestamp(createDate.getTime()));
6590                    }
6591    
6592                    if (bindModifiedDate) {
6593                            qPos.add(new Timestamp(modifiedDate.getTime()));
6594                    }
6595    
6596                    if (orderByComparator != null) {
6597                            Object[] values = orderByComparator.getOrderByConditionValues(user);
6598    
6599                            for (Object value : values) {
6600                                    qPos.add(value);
6601                            }
6602                    }
6603    
6604                    List<User> list = q.list();
6605    
6606                    if (list.size() == 2) {
6607                            return list.get(1);
6608                    }
6609                    else {
6610                            return null;
6611                    }
6612            }
6613    
6614            /**
6615             * Removes all the users where companyId = &#63; and createDate = &#63; and modifiedDate = &#63; from the database.
6616             *
6617             * @param companyId the company ID
6618             * @param createDate the create date
6619             * @param modifiedDate the modified date
6620             */
6621            @Override
6622            public void removeByC_CD_MD(long companyId, Date createDate,
6623                    Date modifiedDate) {
6624                    for (User user : findByC_CD_MD(companyId, createDate, modifiedDate,
6625                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
6626                            remove(user);
6627                    }
6628            }
6629    
6630            /**
6631             * Returns the number of users where companyId = &#63; and createDate = &#63; and modifiedDate = &#63;.
6632             *
6633             * @param companyId the company ID
6634             * @param createDate the create date
6635             * @param modifiedDate the modified date
6636             * @return the number of matching users
6637             */
6638            @Override
6639            public int countByC_CD_MD(long companyId, Date createDate, Date modifiedDate) {
6640                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_CD_MD;
6641    
6642                    Object[] finderArgs = new Object[] { companyId, createDate, modifiedDate };
6643    
6644                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
6645    
6646                    if (count == null) {
6647                            StringBundler query = new StringBundler(4);
6648    
6649                            query.append(_SQL_COUNT_USER_WHERE);
6650    
6651                            query.append(_FINDER_COLUMN_C_CD_MD_COMPANYID_2);
6652    
6653                            boolean bindCreateDate = false;
6654    
6655                            if (createDate == null) {
6656                                    query.append(_FINDER_COLUMN_C_CD_MD_CREATEDATE_1);
6657                            }
6658                            else {
6659                                    bindCreateDate = true;
6660    
6661                                    query.append(_FINDER_COLUMN_C_CD_MD_CREATEDATE_2);
6662                            }
6663    
6664                            boolean bindModifiedDate = false;
6665    
6666                            if (modifiedDate == null) {
6667                                    query.append(_FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_1);
6668                            }
6669                            else {
6670                                    bindModifiedDate = true;
6671    
6672                                    query.append(_FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_2);
6673                            }
6674    
6675                            String sql = query.toString();
6676    
6677                            Session session = null;
6678    
6679                            try {
6680                                    session = openSession();
6681    
6682                                    Query q = session.createQuery(sql);
6683    
6684                                    QueryPos qPos = QueryPos.getInstance(q);
6685    
6686                                    qPos.add(companyId);
6687    
6688                                    if (bindCreateDate) {
6689                                            qPos.add(new Timestamp(createDate.getTime()));
6690                                    }
6691    
6692                                    if (bindModifiedDate) {
6693                                            qPos.add(new Timestamp(modifiedDate.getTime()));
6694                                    }
6695    
6696                                    count = (Long)q.uniqueResult();
6697    
6698                                    finderCache.putResult(finderPath, finderArgs, count);
6699                            }
6700                            catch (Exception e) {
6701                                    finderCache.removeResult(finderPath, finderArgs);
6702    
6703                                    throw processException(e);
6704                            }
6705                            finally {
6706                                    closeSession(session);
6707                            }
6708                    }
6709    
6710                    return count.intValue();
6711            }
6712    
6713            private static final String _FINDER_COLUMN_C_CD_MD_COMPANYID_2 = "user.companyId = ? AND ";
6714            private static final String _FINDER_COLUMN_C_CD_MD_CREATEDATE_1 = "user.createDate IS NULL AND ";
6715            private static final String _FINDER_COLUMN_C_CD_MD_CREATEDATE_2 = "user.createDate = ? AND ";
6716            private static final String _FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_1 = "user.modifiedDate IS NULL";
6717            private static final String _FINDER_COLUMN_C_CD_MD_MODIFIEDDATE_2 = "user.modifiedDate = ?";
6718            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_DU_S = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
6719                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
6720                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_DU_S",
6721                            new String[] {
6722                                    Long.class.getName(), Boolean.class.getName(),
6723                                    Integer.class.getName(),
6724                                    
6725                            Integer.class.getName(), Integer.class.getName(),
6726                                    OrderByComparator.class.getName()
6727                            });
6728            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_DU_S =
6729                    new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
6730                            UserModelImpl.FINDER_CACHE_ENABLED, UserImpl.class,
6731                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_DU_S",
6732                            new String[] {
6733                                    Long.class.getName(), Boolean.class.getName(),
6734                                    Integer.class.getName()
6735                            },
6736                            UserModelImpl.COMPANYID_COLUMN_BITMASK |
6737                            UserModelImpl.DEFAULTUSER_COLUMN_BITMASK |
6738                            UserModelImpl.STATUS_COLUMN_BITMASK);
6739            public static final FinderPath FINDER_PATH_COUNT_BY_C_DU_S = new FinderPath(UserModelImpl.ENTITY_CACHE_ENABLED,
6740                            UserModelImpl.FINDER_CACHE_ENABLED, Long.class,
6741                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_DU_S",
6742                            new String[] {
6743                                    Long.class.getName(), Boolean.class.getName(),
6744                                    Integer.class.getName()
6745                            });
6746    
6747            /**
6748             * Returns all the users where companyId = &#63; and defaultUser = &#63; and status = &#63;.
6749             *
6750             * @param companyId the company ID
6751             * @param defaultUser the default user
6752             * @param status the status
6753             * @return the matching users
6754             */
6755            @Override
6756            public List<User> findByC_DU_S(long companyId, boolean defaultUser,
6757                    int status) {
6758                    return findByC_DU_S(companyId, defaultUser, status, QueryUtil.ALL_POS,
6759                            QueryUtil.ALL_POS, null);
6760            }
6761    
6762            /**
6763             * Returns a range of all the users where companyId = &#63; and defaultUser = &#63; and status = &#63;.
6764             *
6765             * <p>
6766             * 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.
6767             * </p>
6768             *
6769             * @param companyId the company ID
6770             * @param defaultUser the default user
6771             * @param status the status
6772             * @param start the lower bound of the range of users
6773             * @param end the upper bound of the range of users (not inclusive)
6774             * @return the range of matching users
6775             */
6776            @Override
6777            public List<User> findByC_DU_S(long companyId, boolean defaultUser,
6778                    int status, int start, int end) {
6779                    return findByC_DU_S(companyId, defaultUser, status, start, end, null);
6780            }
6781    
6782            /**
6783             * Returns an ordered range of all the users where companyId = &#63; and defaultUser = &#63; and status = &#63;.
6784             *
6785             * <p>
6786             * 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.
6787             * </p>
6788             *
6789             * @param companyId the company ID
6790             * @param defaultUser the default user
6791             * @param status the status
6792             * @param start the lower bound of the range of users
6793             * @param end the upper bound of the range of users (not inclusive)
6794             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
6795             * @return the ordered range of matching users
6796             */
6797            @Override
6798            public List<User> findByC_DU_S(long companyId, boolean defaultUser,
6799                    int status, int start, int end,
6800                    OrderByComparator<User> orderByComparator) {
6801                    return findByC_DU_S(companyId, defaultUser, status, start, end,
6802                            orderByComparator, true);
6803            }
6804    
6805            /**
6806             * Returns an ordered range of all the users where companyId = &#63; and defaultUser = &#63; and status = &#63;.
6807             *
6808             * <p>
6809             * 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.
6810             * </p>
6811             *
6812             * @param companyId the company ID
6813             * @param defaultUser the default user
6814             * @param status the status
6815             * @param start the lower bound of the range of users
6816             * @param end the upper bound of the range of users (not inclusive)
6817             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
6818             * @param retrieveFromCache whether to retrieve from the finder cache
6819             * @return the ordered range of matching users
6820             */
6821            @Override
6822            public List<User> findByC_DU_S(long companyId, boolean defaultUser,
6823                    int status, int start, int end,
6824                    OrderByComparator<User> orderByComparator, boolean retrieveFromCache) {
6825                    boolean pagination = true;
6826                    FinderPath finderPath = null;
6827                    Object[] finderArgs = null;
6828    
6829                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
6830                                    (orderByComparator == null)) {
6831                            pagination = false;
6832                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_DU_S;
6833                            finderArgs = new Object[] { companyId, defaultUser, status };
6834                    }
6835                    else {
6836                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_DU_S;
6837                            finderArgs = new Object[] {
6838                                            companyId, defaultUser, status,
6839                                            
6840                                            start, end, orderByComparator
6841                                    };
6842                    }
6843    
6844                    List<User> list = null;
6845    
6846                    if (retrieveFromCache) {
6847                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
6848                                            this);
6849    
6850                            if ((list != null) && !list.isEmpty()) {
6851                                    for (User user : list) {
6852                                            if ((companyId != user.getCompanyId()) ||
6853                                                            (defaultUser != user.getDefaultUser()) ||
6854                                                            (status != user.getStatus())) {
6855                                                    list = null;
6856    
6857                                                    break;
6858                                            }
6859                                    }
6860                            }
6861                    }
6862    
6863                    if (list == null) {
6864                            StringBundler query = null;
6865    
6866                            if (orderByComparator != null) {
6867                                    query = new StringBundler(5 +
6868                                                    (orderByComparator.getOrderByFields().length * 2));
6869                            }
6870                            else {
6871                                    query = new StringBundler(5);
6872                            }
6873    
6874                            query.append(_SQL_SELECT_USER_WHERE);
6875    
6876                            query.append(_FINDER_COLUMN_C_DU_S_COMPANYID_2);
6877    
6878                            query.append(_FINDER_COLUMN_C_DU_S_DEFAULTUSER_2);
6879    
6880                            query.append(_FINDER_COLUMN_C_DU_S_STATUS_2);
6881    
6882                            if (orderByComparator != null) {
6883                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
6884                                            orderByComparator);
6885                            }
6886                            else
6887                             if (pagination) {
6888                                    query.append(UserModelImpl.ORDER_BY_JPQL);
6889                            }
6890    
6891                            String sql = query.toString();
6892    
6893                            Session session = null;
6894    
6895                            try {
6896                                    session = openSession();
6897    
6898                                    Query q = session.createQuery(sql);
6899    
6900                                    QueryPos qPos = QueryPos.getInstance(q);
6901    
6902                                    qPos.add(companyId);
6903    
6904                                    qPos.add(defaultUser);
6905    
6906                                    qPos.add(status);
6907    
6908                                    if (!pagination) {
6909                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
6910                                                            end, false);
6911    
6912                                            Collections.sort(list);
6913    
6914                                            list = Collections.unmodifiableList(list);
6915                                    }
6916                                    else {
6917                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
6918                                                            end);
6919                                    }
6920    
6921                                    cacheResult(list);
6922    
6923                                    finderCache.putResult(finderPath, finderArgs, list);
6924                            }
6925                            catch (Exception e) {
6926                                    finderCache.removeResult(finderPath, finderArgs);
6927    
6928                                    throw processException(e);
6929                            }
6930                            finally {
6931                                    closeSession(session);
6932                            }
6933                    }
6934    
6935                    return list;
6936            }
6937    
6938            /**
6939             * Returns the first user in the ordered set where companyId = &#63; and defaultUser = &#63; and status = &#63;.
6940             *
6941             * @param companyId the company ID
6942             * @param defaultUser the default user
6943             * @param status the status
6944             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6945             * @return the first matching user
6946             * @throws NoSuchUserException if a matching user could not be found
6947             */
6948            @Override
6949            public User findByC_DU_S_First(long companyId, boolean defaultUser,
6950                    int status, OrderByComparator<User> orderByComparator)
6951                    throws NoSuchUserException {
6952                    User user = fetchByC_DU_S_First(companyId, defaultUser, status,
6953                                    orderByComparator);
6954    
6955                    if (user != null) {
6956                            return user;
6957                    }
6958    
6959                    StringBundler msg = new StringBundler(8);
6960    
6961                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
6962    
6963                    msg.append("companyId=");
6964                    msg.append(companyId);
6965    
6966                    msg.append(", defaultUser=");
6967                    msg.append(defaultUser);
6968    
6969                    msg.append(", status=");
6970                    msg.append(status);
6971    
6972                    msg.append(StringPool.CLOSE_CURLY_BRACE);
6973    
6974                    throw new NoSuchUserException(msg.toString());
6975            }
6976    
6977            /**
6978             * Returns the first user in the ordered set where companyId = &#63; and defaultUser = &#63; and status = &#63;.
6979             *
6980             * @param companyId the company ID
6981             * @param defaultUser the default user
6982             * @param status the status
6983             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6984             * @return the first matching user, or <code>null</code> if a matching user could not be found
6985             */
6986            @Override
6987            public User fetchByC_DU_S_First(long companyId, boolean defaultUser,
6988                    int status, OrderByComparator<User> orderByComparator) {
6989                    List<User> list = findByC_DU_S(companyId, defaultUser, status, 0, 1,
6990                                    orderByComparator);
6991    
6992                    if (!list.isEmpty()) {
6993                            return list.get(0);
6994                    }
6995    
6996                    return null;
6997            }
6998    
6999            /**
7000             * Returns the last user in the ordered set where companyId = &#63; and defaultUser = &#63; and status = &#63;.
7001             *
7002             * @param companyId the company ID
7003             * @param defaultUser the default user
7004             * @param status the status
7005             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
7006             * @return the last matching user
7007             * @throws NoSuchUserException if a matching user could not be found
7008             */
7009            @Override
7010            public User findByC_DU_S_Last(long companyId, boolean defaultUser,
7011                    int status, OrderByComparator<User> orderByComparator)
7012                    throws NoSuchUserException {
7013                    User user = fetchByC_DU_S_Last(companyId, defaultUser, status,
7014                                    orderByComparator);
7015    
7016                    if (user != null) {
7017                            return user;
7018                    }
7019    
7020                    StringBundler msg = new StringBundler(8);
7021    
7022                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
7023    
7024                    msg.append("companyId=");
7025                    msg.append(companyId);
7026    
7027                    msg.append(", defaultUser=");
7028                    msg.append(defaultUser);
7029    
7030                    msg.append(", status=");
7031                    msg.append(status);
7032    
7033                    msg.append(StringPool.CLOSE_CURLY_BRACE);
7034    
7035                    throw new NoSuchUserException(msg.toString());
7036            }
7037    
7038            /**
7039             * Returns the last user in the ordered set where companyId = &#63; and defaultUser = &#63; and status = &#63;.
7040             *
7041             * @param companyId the company ID
7042             * @param defaultUser the default user
7043             * @param status the status
7044             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
7045             * @return the last matching user, or <code>null</code> if a matching user could not be found
7046             */
7047            @Override
7048            public User fetchByC_DU_S_Last(long companyId, boolean defaultUser,
7049                    int status, OrderByComparator<User> orderByComparator) {
7050                    int count = countByC_DU_S(companyId, defaultUser, status);
7051    
7052                    if (count == 0) {
7053                            return null;
7054                    }
7055    
7056                    List<User> list = findByC_DU_S(companyId, defaultUser, status,
7057                                    count - 1, count, orderByComparator);
7058    
7059                    if (!list.isEmpty()) {
7060                            return list.get(0);
7061                    }
7062    
7063                    return null;
7064            }
7065    
7066            /**
7067             * Returns the users before and after the current user in the ordered set where companyId = &#63; and defaultUser = &#63; and status = &#63;.
7068             *
7069             * @param userId the primary key of the current user
7070             * @param companyId the company ID
7071             * @param defaultUser the default user
7072             * @param status the status
7073             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
7074             * @return the previous, current, and next user
7075             * @throws NoSuchUserException if a user with the primary key could not be found
7076             */
7077            @Override
7078            public User[] findByC_DU_S_PrevAndNext(long userId, long companyId,
7079                    boolean defaultUser, int status,
7080                    OrderByComparator<User> orderByComparator) throws NoSuchUserException {
7081                    User user = findByPrimaryKey(userId);
7082    
7083                    Session session = null;
7084    
7085                    try {
7086                            session = openSession();
7087    
7088                            User[] array = new UserImpl[3];
7089    
7090                            array[0] = getByC_DU_S_PrevAndNext(session, user, companyId,
7091                                            defaultUser, status, orderByComparator, true);
7092    
7093                            array[1] = user;
7094    
7095                            array[2] = getByC_DU_S_PrevAndNext(session, user, companyId,
7096                                            defaultUser, status, orderByComparator, false);
7097    
7098                            return array;
7099                    }
7100                    catch (Exception e) {
7101                            throw processException(e);
7102                    }
7103                    finally {
7104                            closeSession(session);
7105                    }
7106            }
7107    
7108            protected User getByC_DU_S_PrevAndNext(Session session, User user,
7109                    long companyId, boolean defaultUser, int status,
7110                    OrderByComparator<User> orderByComparator, boolean previous) {
7111                    StringBundler query = null;
7112    
7113                    if (orderByComparator != null) {
7114                            query = new StringBundler(6 +
7115                                            (orderByComparator.getOrderByConditionFields().length * 3) +
7116                                            (orderByComparator.getOrderByFields().length * 3));
7117                    }
7118                    else {
7119                            query = new StringBundler(5);
7120                    }
7121    
7122                    query.append(_SQL_SELECT_USER_WHERE);
7123    
7124                    query.append(_FINDER_COLUMN_C_DU_S_COMPANYID_2);
7125    
7126                    query.append(_FINDER_COLUMN_C_DU_S_DEFAULTUSER_2);
7127    
7128                    query.append(_FINDER_COLUMN_C_DU_S_STATUS_2);
7129    
7130                    if (orderByComparator != null) {
7131                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
7132    
7133                            if (orderByConditionFields.length > 0) {
7134                                    query.append(WHERE_AND);
7135                            }
7136    
7137                            for (int i = 0; i < orderByConditionFields.length; i++) {
7138                                    query.append(_ORDER_BY_ENTITY_ALIAS);
7139                                    query.append(orderByConditionFields[i]);
7140    
7141                                    if ((i + 1) < orderByConditionFields.length) {
7142                                            if (orderByComparator.isAscending() ^ previous) {
7143                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
7144                                            }
7145                                            else {
7146                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
7147                                            }
7148                                    }
7149                                    else {
7150                                            if (orderByComparator.isAscending() ^ previous) {
7151                                                    query.append(WHERE_GREATER_THAN);
7152                                            }
7153                                            else {
7154                                                    query.append(WHERE_LESSER_THAN);
7155                                            }
7156                                    }
7157                            }
7158    
7159                            query.append(ORDER_BY_CLAUSE);
7160    
7161                            String[] orderByFields = orderByComparator.getOrderByFields();
7162    
7163                            for (int i = 0; i < orderByFields.length; i++) {
7164                                    query.append(_ORDER_BY_ENTITY_ALIAS);
7165                                    query.append(orderByFields[i]);
7166    
7167                                    if ((i + 1) < orderByFields.length) {
7168                                            if (orderByComparator.isAscending() ^ previous) {
7169                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
7170                                            }
7171                                            else {
7172                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
7173                                            }
7174                                    }
7175                                    else {
7176                                            if (orderByComparator.isAscending() ^ previous) {
7177                                                    query.append(ORDER_BY_ASC);
7178                                            }
7179                                            else {
7180                                                    query.append(ORDER_BY_DESC);
7181                                            }
7182                                    }
7183                            }
7184                    }
7185                    else {
7186                            query.append(UserModelImpl.ORDER_BY_JPQL);
7187                    }
7188    
7189                    String sql = query.toString();
7190    
7191                    Query q = session.createQuery(sql);
7192    
7193                    q.setFirstResult(0);
7194                    q.setMaxResults(2);
7195    
7196                    QueryPos qPos = QueryPos.getInstance(q);
7197    
7198                    qPos.add(companyId);
7199    
7200                    qPos.add(defaultUser);
7201    
7202                    qPos.add(status);
7203    
7204                    if (orderByComparator != null) {
7205                            Object[] values = orderByComparator.getOrderByConditionValues(user);
7206    
7207                            for (Object value : values) {
7208                                    qPos.add(value);
7209                            }
7210                    }
7211    
7212                    List<User> list = q.list();
7213    
7214                    if (list.size() == 2) {
7215                            return list.get(1);
7216                    }
7217                    else {
7218                            return null;
7219                    }
7220            }
7221    
7222            /**
7223             * Removes all the users where companyId = &#63; and defaultUser = &#63; and status = &#63; from the database.
7224             *
7225             * @param companyId the company ID
7226             * @param defaultUser the default user
7227             * @param status the status
7228             */
7229            @Override
7230            public void removeByC_DU_S(long companyId, boolean defaultUser, int status) {
7231                    for (User user : findByC_DU_S(companyId, defaultUser, status,
7232                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
7233                            remove(user);
7234                    }
7235            }
7236    
7237            /**
7238             * Returns the number of users where companyId = &#63; and defaultUser = &#63; and status = &#63;.
7239             *
7240             * @param companyId the company ID
7241             * @param defaultUser the default user
7242             * @param status the status
7243             * @return the number of matching users
7244             */
7245            @Override
7246            public int countByC_DU_S(long companyId, boolean defaultUser, int status) {
7247                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_DU_S;
7248    
7249                    Object[] finderArgs = new Object[] { companyId, defaultUser, status };
7250    
7251                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
7252    
7253                    if (count == null) {
7254                            StringBundler query = new StringBundler(4);
7255    
7256                            query.append(_SQL_COUNT_USER_WHERE);
7257    
7258                            query.append(_FINDER_COLUMN_C_DU_S_COMPANYID_2);
7259    
7260                            query.append(_FINDER_COLUMN_C_DU_S_DEFAULTUSER_2);
7261    
7262                            query.append(_FINDER_COLUMN_C_DU_S_STATUS_2);
7263    
7264                            String sql = query.toString();
7265    
7266                            Session session = null;
7267    
7268                            try {
7269                                    session = openSession();
7270    
7271                                    Query q = session.createQuery(sql);
7272    
7273                                    QueryPos qPos = QueryPos.getInstance(q);
7274    
7275                                    qPos.add(companyId);
7276    
7277                                    qPos.add(defaultUser);
7278    
7279                                    qPos.add(status);
7280    
7281                                    count = (Long)q.uniqueResult();
7282    
7283                                    finderCache.putResult(finderPath, finderArgs, count);
7284                            }
7285                            catch (Exception e) {
7286                                    finderCache.removeResult(finderPath, finderArgs);
7287    
7288                                    throw processException(e);
7289                            }
7290                            finally {
7291                                    closeSession(session);
7292                            }
7293                    }
7294    
7295                    return count.intValue();
7296            }
7297    
7298            private static final String _FINDER_COLUMN_C_DU_S_COMPANYID_2 = "user.companyId = ? AND ";
7299            private static final String _FINDER_COLUMN_C_DU_S_DEFAULTUSER_2 = "user.defaultUser = ? AND ";
7300            private static final String _FINDER_COLUMN_C_DU_S_STATUS_2 = "user.status = ?";
7301    
7302            public UserPersistenceImpl() {
7303                    setModelClass(User.class);
7304            }
7305    
7306            /**
7307             * Caches the user in the entity cache if it is enabled.
7308             *
7309             * @param user the user
7310             */
7311            @Override
7312            public void cacheResult(User user) {
7313                    entityCache.putResult(UserModelImpl.ENTITY_CACHE_ENABLED,
7314                            UserImpl.class, user.getPrimaryKey(), user);
7315    
7316                    finderCache.putResult(FINDER_PATH_FETCH_BY_CONTACTID,
7317                            new Object[] { user.getContactId() }, user);
7318    
7319                    finderCache.putResult(FINDER_PATH_FETCH_BY_PORTRAITID,
7320                            new Object[] { user.getPortraitId() }, user);
7321    
7322                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_U,
7323                            new Object[] { user.getCompanyId(), user.getUserId() }, user);
7324    
7325                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_DU,
7326                            new Object[] { user.getCompanyId(), user.getDefaultUser() }, user);
7327    
7328                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_SN,
7329                            new Object[] { user.getCompanyId(), user.getScreenName() }, user);
7330    
7331                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_EA,
7332                            new Object[] { user.getCompanyId(), user.getEmailAddress() }, user);
7333    
7334                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_FID,
7335                            new Object[] { user.getCompanyId(), user.getFacebookId() }, user);
7336    
7337                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_GUID,
7338                            new Object[] { user.getCompanyId(), user.getGoogleUserId() }, user);
7339    
7340                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_O,
7341                            new Object[] { user.getCompanyId(), user.getOpenId() }, user);
7342    
7343                    user.resetOriginalValues();
7344            }
7345    
7346            /**
7347             * Caches the users in the entity cache if it is enabled.
7348             *
7349             * @param users the users
7350             */
7351            @Override
7352            public void cacheResult(List<User> users) {
7353                    for (User user : users) {
7354                            if (entityCache.getResult(UserModelImpl.ENTITY_CACHE_ENABLED,
7355                                                    UserImpl.class, user.getPrimaryKey()) == null) {
7356                                    cacheResult(user);
7357                            }
7358                            else {
7359                                    user.resetOriginalValues();
7360                            }
7361                    }
7362            }
7363    
7364            /**
7365             * Clears the cache for all users.
7366             *
7367             * <p>
7368             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
7369             * </p>
7370             */
7371            @Override
7372            public void clearCache() {
7373                    entityCache.clearCache(UserImpl.class);
7374    
7375                    finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
7376                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
7377                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
7378            }
7379    
7380            /**
7381             * Clears the cache for the user.
7382             *
7383             * <p>
7384             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
7385             * </p>
7386             */
7387            @Override
7388            public void clearCache(User user) {
7389                    entityCache.removeResult(UserModelImpl.ENTITY_CACHE_ENABLED,
7390                            UserImpl.class, user.getPrimaryKey());
7391    
7392                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
7393                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
7394    
7395                    clearUniqueFindersCache((UserModelImpl)user);
7396            }
7397    
7398            @Override
7399            public void clearCache(List<User> users) {
7400                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
7401                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
7402    
7403                    for (User user : users) {
7404                            entityCache.removeResult(UserModelImpl.ENTITY_CACHE_ENABLED,
7405                                    UserImpl.class, user.getPrimaryKey());
7406    
7407                            clearUniqueFindersCache((UserModelImpl)user);
7408                    }
7409            }
7410    
7411            protected void cacheUniqueFindersCache(UserModelImpl userModelImpl,
7412                    boolean isNew) {
7413                    if (isNew) {
7414                            Object[] args = new Object[] { userModelImpl.getContactId() };
7415    
7416                            finderCache.putResult(FINDER_PATH_COUNT_BY_CONTACTID, args,
7417                                    Long.valueOf(1));
7418                            finderCache.putResult(FINDER_PATH_FETCH_BY_CONTACTID, args,
7419                                    userModelImpl);
7420    
7421                            args = new Object[] { userModelImpl.getPortraitId() };
7422    
7423                            finderCache.putResult(FINDER_PATH_COUNT_BY_PORTRAITID, args,
7424                                    Long.valueOf(1));
7425                            finderCache.putResult(FINDER_PATH_FETCH_BY_PORTRAITID, args,
7426                                    userModelImpl);
7427    
7428                            args = new Object[] {
7429                                            userModelImpl.getCompanyId(), userModelImpl.getUserId()
7430                                    };
7431    
7432                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_U, args,
7433                                    Long.valueOf(1));
7434                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_U, args, userModelImpl);
7435    
7436                            args = new Object[] {
7437                                            userModelImpl.getCompanyId(), userModelImpl.getDefaultUser()
7438                                    };
7439    
7440                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_DU, args,
7441                                    Long.valueOf(1));
7442                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_DU, args, userModelImpl);
7443    
7444                            args = new Object[] {
7445                                            userModelImpl.getCompanyId(), userModelImpl.getScreenName()
7446                                    };
7447    
7448                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_SN, args,
7449                                    Long.valueOf(1));
7450                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_SN, args, userModelImpl);
7451    
7452                            args = new Object[] {
7453                                            userModelImpl.getCompanyId(),
7454                                            userModelImpl.getEmailAddress()
7455                                    };
7456    
7457                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_EA, args,
7458                                    Long.valueOf(1));
7459                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_EA, args, userModelImpl);
7460    
7461                            args = new Object[] {
7462                                            userModelImpl.getCompanyId(), userModelImpl.getFacebookId()
7463                                    };
7464    
7465                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_FID, args,
7466                                    Long.valueOf(1));
7467                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_FID, args,
7468                                    userModelImpl);
7469    
7470                            args = new Object[] {
7471                                            userModelImpl.getCompanyId(),
7472                                            userModelImpl.getGoogleUserId()
7473                                    };
7474    
7475                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_GUID, args,
7476                                    Long.valueOf(1));
7477                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_GUID, args,
7478                                    userModelImpl);
7479    
7480                            args = new Object[] {
7481                                            userModelImpl.getCompanyId(), userModelImpl.getOpenId()
7482                                    };
7483    
7484                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_O, args,
7485                                    Long.valueOf(1));
7486                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_O, args, userModelImpl);
7487                    }
7488                    else {
7489                            if ((userModelImpl.getColumnBitmask() &
7490                                            FINDER_PATH_FETCH_BY_CONTACTID.getColumnBitmask()) != 0) {
7491                                    Object[] args = new Object[] { userModelImpl.getContactId() };
7492    
7493                                    finderCache.putResult(FINDER_PATH_COUNT_BY_CONTACTID, args,
7494                                            Long.valueOf(1));
7495                                    finderCache.putResult(FINDER_PATH_FETCH_BY_CONTACTID, args,
7496                                            userModelImpl);
7497                            }
7498    
7499                            if ((userModelImpl.getColumnBitmask() &
7500                                            FINDER_PATH_FETCH_BY_PORTRAITID.getColumnBitmask()) != 0) {
7501                                    Object[] args = new Object[] { userModelImpl.getPortraitId() };
7502    
7503                                    finderCache.putResult(FINDER_PATH_COUNT_BY_PORTRAITID, args,
7504                                            Long.valueOf(1));
7505                                    finderCache.putResult(FINDER_PATH_FETCH_BY_PORTRAITID, args,
7506                                            userModelImpl);
7507                            }
7508    
7509                            if ((userModelImpl.getColumnBitmask() &
7510                                            FINDER_PATH_FETCH_BY_C_U.getColumnBitmask()) != 0) {
7511                                    Object[] args = new Object[] {
7512                                                    userModelImpl.getCompanyId(), userModelImpl.getUserId()
7513                                            };
7514    
7515                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_U, args,
7516                                            Long.valueOf(1));
7517                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_U, args,
7518                                            userModelImpl);
7519                            }
7520    
7521                            if ((userModelImpl.getColumnBitmask() &
7522                                            FINDER_PATH_FETCH_BY_C_DU.getColumnBitmask()) != 0) {
7523                                    Object[] args = new Object[] {
7524                                                    userModelImpl.getCompanyId(),
7525                                                    userModelImpl.getDefaultUser()
7526                                            };
7527    
7528                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_DU, args,
7529                                            Long.valueOf(1));
7530                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_DU, args,
7531                                            userModelImpl);
7532                            }
7533    
7534                            if ((userModelImpl.getColumnBitmask() &
7535                                            FINDER_PATH_FETCH_BY_C_SN.getColumnBitmask()) != 0) {
7536                                    Object[] args = new Object[] {
7537                                                    userModelImpl.getCompanyId(),
7538                                                    userModelImpl.getScreenName()
7539                                            };
7540    
7541                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_SN, args,
7542                                            Long.valueOf(1));
7543                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_SN, args,
7544                                            userModelImpl);
7545                            }
7546    
7547                            if ((userModelImpl.getColumnBitmask() &
7548                                            FINDER_PATH_FETCH_BY_C_EA.getColumnBitmask()) != 0) {
7549                                    Object[] args = new Object[] {
7550                                                    userModelImpl.getCompanyId(),
7551                                                    userModelImpl.getEmailAddress()
7552                                            };
7553    
7554                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_EA, args,
7555                                            Long.valueOf(1));
7556                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_EA, args,
7557                                            userModelImpl);
7558                            }
7559    
7560                            if ((userModelImpl.getColumnBitmask() &
7561                                            FINDER_PATH_FETCH_BY_C_FID.getColumnBitmask()) != 0) {
7562                                    Object[] args = new Object[] {
7563                                                    userModelImpl.getCompanyId(),
7564                                                    userModelImpl.getFacebookId()
7565                                            };
7566    
7567                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_FID, args,
7568                                            Long.valueOf(1));
7569                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_FID, args,
7570                                            userModelImpl);
7571                            }
7572    
7573                            if ((userModelImpl.getColumnBitmask() &
7574                                            FINDER_PATH_FETCH_BY_C_GUID.getColumnBitmask()) != 0) {
7575                                    Object[] args = new Object[] {
7576                                                    userModelImpl.getCompanyId(),
7577                                                    userModelImpl.getGoogleUserId()
7578                                            };
7579    
7580                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_GUID, args,
7581                                            Long.valueOf(1));
7582                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_GUID, args,
7583                                            userModelImpl);
7584                            }
7585    
7586                            if ((userModelImpl.getColumnBitmask() &
7587                                            FINDER_PATH_FETCH_BY_C_O.getColumnBitmask()) != 0) {
7588                                    Object[] args = new Object[] {
7589                                                    userModelImpl.getCompanyId(), userModelImpl.getOpenId()
7590                                            };
7591    
7592                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_O, args,
7593                                            Long.valueOf(1));
7594                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_O, args,
7595                                            userModelImpl);
7596                            }
7597                    }
7598            }
7599    
7600            protected void clearUniqueFindersCache(UserModelImpl userModelImpl) {
7601                    Object[] args = new Object[] { userModelImpl.getContactId() };
7602    
7603                    finderCache.removeResult(FINDER_PATH_COUNT_BY_CONTACTID, args);
7604                    finderCache.removeResult(FINDER_PATH_FETCH_BY_CONTACTID, args);
7605    
7606                    if ((userModelImpl.getColumnBitmask() &
7607                                    FINDER_PATH_FETCH_BY_CONTACTID.getColumnBitmask()) != 0) {
7608                            args = new Object[] { userModelImpl.getOriginalContactId() };
7609    
7610                            finderCache.removeResult(FINDER_PATH_COUNT_BY_CONTACTID, args);
7611                            finderCache.removeResult(FINDER_PATH_FETCH_BY_CONTACTID, args);
7612                    }
7613    
7614                    args = new Object[] { userModelImpl.getPortraitId() };
7615    
7616                    finderCache.removeResult(FINDER_PATH_COUNT_BY_PORTRAITID, args);
7617                    finderCache.removeResult(FINDER_PATH_FETCH_BY_PORTRAITID, args);
7618    
7619                    if ((userModelImpl.getColumnBitmask() &
7620                                    FINDER_PATH_FETCH_BY_PORTRAITID.getColumnBitmask()) != 0) {
7621                            args = new Object[] { userModelImpl.getOriginalPortraitId() };
7622    
7623                            finderCache.removeResult(FINDER_PATH_COUNT_BY_PORTRAITID, args);
7624                            finderCache.removeResult(FINDER_PATH_FETCH_BY_PORTRAITID, args);
7625                    }
7626    
7627                    args = new Object[] {
7628                                    userModelImpl.getCompanyId(), userModelImpl.getUserId()
7629                            };
7630    
7631                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_U, args);
7632                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_U, args);
7633    
7634                    if ((userModelImpl.getColumnBitmask() &
7635                                    FINDER_PATH_FETCH_BY_C_U.getColumnBitmask()) != 0) {
7636                            args = new Object[] {
7637                                            userModelImpl.getOriginalCompanyId(),
7638                                            userModelImpl.getOriginalUserId()
7639                                    };
7640    
7641                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_U, args);
7642                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_U, args);
7643                    }
7644    
7645                    args = new Object[] {
7646                                    userModelImpl.getCompanyId(), userModelImpl.getDefaultUser()
7647                            };
7648    
7649                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_DU, args);
7650                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_DU, args);
7651    
7652                    if ((userModelImpl.getColumnBitmask() &
7653                                    FINDER_PATH_FETCH_BY_C_DU.getColumnBitmask()) != 0) {
7654                            args = new Object[] {
7655                                            userModelImpl.getOriginalCompanyId(),
7656                                            userModelImpl.getOriginalDefaultUser()
7657                                    };
7658    
7659                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_DU, args);
7660                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_DU, args);
7661                    }
7662    
7663                    args = new Object[] {
7664                                    userModelImpl.getCompanyId(), userModelImpl.getScreenName()
7665                            };
7666    
7667                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_SN, args);
7668                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_SN, args);
7669    
7670                    if ((userModelImpl.getColumnBitmask() &
7671                                    FINDER_PATH_FETCH_BY_C_SN.getColumnBitmask()) != 0) {
7672                            args = new Object[] {
7673                                            userModelImpl.getOriginalCompanyId(),
7674                                            userModelImpl.getOriginalScreenName()
7675                                    };
7676    
7677                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_SN, args);
7678                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_SN, args);
7679                    }
7680    
7681                    args = new Object[] {
7682                                    userModelImpl.getCompanyId(), userModelImpl.getEmailAddress()
7683                            };
7684    
7685                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_EA, args);
7686                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_EA, args);
7687    
7688                    if ((userModelImpl.getColumnBitmask() &
7689                                    FINDER_PATH_FETCH_BY_C_EA.getColumnBitmask()) != 0) {
7690                            args = new Object[] {
7691                                            userModelImpl.getOriginalCompanyId(),
7692                                            userModelImpl.getOriginalEmailAddress()
7693                                    };
7694    
7695                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_EA, args);
7696                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_EA, args);
7697                    }
7698    
7699                    args = new Object[] {
7700                                    userModelImpl.getCompanyId(), userModelImpl.getFacebookId()
7701                            };
7702    
7703                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_FID, args);
7704                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_FID, args);
7705    
7706                    if ((userModelImpl.getColumnBitmask() &
7707                                    FINDER_PATH_FETCH_BY_C_FID.getColumnBitmask()) != 0) {
7708                            args = new Object[] {
7709                                            userModelImpl.getOriginalCompanyId(),
7710                                            userModelImpl.getOriginalFacebookId()
7711                                    };
7712    
7713                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_FID, args);
7714                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_FID, args);
7715                    }
7716    
7717                    args = new Object[] {
7718                                    userModelImpl.getCompanyId(), userModelImpl.getGoogleUserId()
7719                            };
7720    
7721                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_GUID, args);
7722                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_GUID, args);
7723    
7724                    if ((userModelImpl.getColumnBitmask() &
7725                                    FINDER_PATH_FETCH_BY_C_GUID.getColumnBitmask()) != 0) {
7726                            args = new Object[] {
7727                                            userModelImpl.getOriginalCompanyId(),
7728                                            userModelImpl.getOriginalGoogleUserId()
7729                                    };
7730    
7731                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_GUID, args);
7732                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_GUID, args);
7733                    }
7734    
7735                    args = new Object[] {
7736                                    userModelImpl.getCompanyId(), userModelImpl.getOpenId()
7737                            };
7738    
7739                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_O, args);
7740                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_O, args);
7741    
7742                    if ((userModelImpl.getColumnBitmask() &
7743                                    FINDER_PATH_FETCH_BY_C_O.getColumnBitmask()) != 0) {
7744                            args = new Object[] {
7745                                            userModelImpl.getOriginalCompanyId(),
7746                                            userModelImpl.getOriginalOpenId()
7747                                    };
7748    
7749                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_O, args);
7750                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_O, args);
7751                    }
7752            }
7753    
7754            /**
7755             * Creates a new user with the primary key. Does not add the user to the database.
7756             *
7757             * @param userId the primary key for the new user
7758             * @return the new user
7759             */
7760            @Override
7761            public User create(long userId) {
7762                    User user = new UserImpl();
7763    
7764                    user.setNew(true);
7765                    user.setPrimaryKey(userId);
7766    
7767                    String uuid = PortalUUIDUtil.generate();
7768    
7769                    user.setUuid(uuid);
7770    
7771                    user.setCompanyId(companyProvider.getCompanyId());
7772    
7773                    return user;
7774            }
7775    
7776            /**
7777             * Removes the user with the primary key from the database. Also notifies the appropriate model listeners.
7778             *
7779             * @param userId the primary key of the user
7780             * @return the user that was removed
7781             * @throws NoSuchUserException if a user with the primary key could not be found
7782             */
7783            @Override
7784            public User remove(long userId) throws NoSuchUserException {
7785                    return remove((Serializable)userId);
7786            }
7787    
7788            /**
7789             * Removes the user with the primary key from the database. Also notifies the appropriate model listeners.
7790             *
7791             * @param primaryKey the primary key of the user
7792             * @return the user that was removed
7793             * @throws NoSuchUserException if a user with the primary key could not be found
7794             */
7795            @Override
7796            public User remove(Serializable primaryKey) throws NoSuchUserException {
7797                    Session session = null;
7798    
7799                    try {
7800                            session = openSession();
7801    
7802                            User user = (User)session.get(UserImpl.class, primaryKey);
7803    
7804                            if (user == null) {
7805                                    if (_log.isDebugEnabled()) {
7806                                            _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
7807                                    }
7808    
7809                                    throw new NoSuchUserException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
7810                                            primaryKey);
7811                            }
7812    
7813                            return remove(user);
7814                    }
7815                    catch (NoSuchUserException nsee) {
7816                            throw nsee;
7817                    }
7818                    catch (Exception e) {
7819                            throw processException(e);
7820                    }
7821                    finally {
7822                            closeSession(session);
7823                    }
7824            }
7825    
7826            @Override
7827            protected User removeImpl(User user) {
7828                    user = toUnwrappedModel(user);
7829    
7830                    userToGroupTableMapper.deleteLeftPrimaryKeyTableMappings(user.getPrimaryKey());
7831    
7832                    userToOrganizationTableMapper.deleteLeftPrimaryKeyTableMappings(user.getPrimaryKey());
7833    
7834                    userToRoleTableMapper.deleteLeftPrimaryKeyTableMappings(user.getPrimaryKey());
7835    
7836                    userToTeamTableMapper.deleteLeftPrimaryKeyTableMappings(user.getPrimaryKey());
7837    
7838                    userToUserGroupTableMapper.deleteLeftPrimaryKeyTableMappings(user.getPrimaryKey());
7839    
7840                    Session session = null;
7841    
7842                    try {
7843                            session = openSession();
7844    
7845                            if (!session.contains(user)) {
7846                                    user = (User)session.get(UserImpl.class, user.getPrimaryKeyObj());
7847                            }
7848    
7849                            if (user != null) {
7850                                    session.delete(user);
7851                            }
7852                    }
7853                    catch (Exception e) {
7854                            throw processException(e);
7855                    }
7856                    finally {
7857                            closeSession(session);
7858                    }
7859    
7860                    if (user != null) {
7861                            clearCache(user);
7862                    }
7863    
7864                    return user;
7865            }
7866    
7867            @Override
7868            public User updateImpl(User user) {
7869                    user = toUnwrappedModel(user);
7870    
7871                    boolean isNew = user.isNew();
7872    
7873                    UserModelImpl userModelImpl = (UserModelImpl)user;
7874    
7875                    if (Validator.isNull(user.getUuid())) {
7876                            String uuid = PortalUUIDUtil.generate();
7877    
7878                            user.setUuid(uuid);
7879                    }
7880    
7881                    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
7882    
7883                    Date now = new Date();
7884    
7885                    if (isNew && (user.getCreateDate() == null)) {
7886                            if (serviceContext == null) {
7887                                    user.setCreateDate(now);
7888                            }
7889                            else {
7890                                    user.setCreateDate(serviceContext.getCreateDate(now));
7891                            }
7892                    }
7893    
7894                    if (!userModelImpl.hasSetModifiedDate()) {
7895                            if (serviceContext == null) {
7896                                    user.setModifiedDate(now);
7897                            }
7898                            else {
7899                                    user.setModifiedDate(serviceContext.getModifiedDate(now));
7900                            }
7901                    }
7902    
7903                    Session session = null;
7904    
7905                    try {
7906                            session = openSession();
7907    
7908                            if (user.isNew()) {
7909                                    session.save(user);
7910    
7911                                    user.setNew(false);
7912                            }
7913                            else {
7914                                    user = (User)session.merge(user);
7915                            }
7916                    }
7917                    catch (Exception e) {
7918                            throw processException(e);
7919                    }
7920                    finally {
7921                            closeSession(session);
7922                    }
7923    
7924                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
7925    
7926                    if (isNew || !UserModelImpl.COLUMN_BITMASK_ENABLED) {
7927                            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
7928                    }
7929    
7930                    else {
7931                            if ((userModelImpl.getColumnBitmask() &
7932                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID.getColumnBitmask()) != 0) {
7933                                    Object[] args = new Object[] { userModelImpl.getOriginalUuid() };
7934    
7935                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
7936                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
7937                                            args);
7938    
7939                                    args = new Object[] { userModelImpl.getUuid() };
7940    
7941                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
7942                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
7943                                            args);
7944                            }
7945    
7946                            if ((userModelImpl.getColumnBitmask() &
7947                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C.getColumnBitmask()) != 0) {
7948                                    Object[] args = new Object[] {
7949                                                    userModelImpl.getOriginalUuid(),
7950                                                    userModelImpl.getOriginalCompanyId()
7951                                            };
7952    
7953                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
7954                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
7955                                            args);
7956    
7957                                    args = new Object[] {
7958                                                    userModelImpl.getUuid(), userModelImpl.getCompanyId()
7959                                            };
7960    
7961                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
7962                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
7963                                            args);
7964                            }
7965    
7966                            if ((userModelImpl.getColumnBitmask() &
7967                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID.getColumnBitmask()) != 0) {
7968                                    Object[] args = new Object[] {
7969                                                    userModelImpl.getOriginalCompanyId()
7970                                            };
7971    
7972                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_COMPANYID, args);
7973                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
7974                                            args);
7975    
7976                                    args = new Object[] { userModelImpl.getCompanyId() };
7977    
7978                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_COMPANYID, args);
7979                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
7980                                            args);
7981                            }
7982    
7983                            if ((userModelImpl.getColumnBitmask() &
7984                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_EMAILADDRESS.getColumnBitmask()) != 0) {
7985                                    Object[] args = new Object[] {
7986                                                    userModelImpl.getOriginalEmailAddress()
7987                                            };
7988    
7989                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_EMAILADDRESS, args);
7990                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_EMAILADDRESS,
7991                                            args);
7992    
7993                                    args = new Object[] { userModelImpl.getEmailAddress() };
7994    
7995                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_EMAILADDRESS, args);
7996                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_EMAILADDRESS,
7997                                            args);
7998                            }
7999    
8000                            if ((userModelImpl.getColumnBitmask() &
8001                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD.getColumnBitmask()) != 0) {
8002                                    Object[] args = new Object[] {
8003                                                    userModelImpl.getOriginalCompanyId(),
8004                                                    userModelImpl.getOriginalCreateDate()
8005                                            };
8006    
8007                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_CD, args);
8008                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD,
8009                                            args);
8010    
8011                                    args = new Object[] {
8012                                                    userModelImpl.getCompanyId(),
8013                                                    userModelImpl.getCreateDate()
8014                                            };
8015    
8016                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_CD, args);
8017                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD,
8018                                            args);
8019                            }
8020    
8021                            if ((userModelImpl.getColumnBitmask() &
8022                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_MD.getColumnBitmask()) != 0) {
8023                                    Object[] args = new Object[] {
8024                                                    userModelImpl.getOriginalCompanyId(),
8025                                                    userModelImpl.getOriginalModifiedDate()
8026                                            };
8027    
8028                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_MD, args);
8029                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_MD,
8030                                            args);
8031    
8032                                    args = new Object[] {
8033                                                    userModelImpl.getCompanyId(),
8034                                                    userModelImpl.getModifiedDate()
8035                                            };
8036    
8037                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_MD, args);
8038                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_MD,
8039                                            args);
8040                            }
8041    
8042                            if ((userModelImpl.getColumnBitmask() &
8043                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_S.getColumnBitmask()) != 0) {
8044                                    Object[] args = new Object[] {
8045                                                    userModelImpl.getOriginalCompanyId(),
8046                                                    userModelImpl.getOriginalStatus()
8047                                            };
8048    
8049                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_S, args);
8050                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_S,
8051                                            args);
8052    
8053                                    args = new Object[] {
8054                                                    userModelImpl.getCompanyId(), userModelImpl.getStatus()
8055                                            };
8056    
8057                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_S, args);
8058                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_S,
8059                                            args);
8060                            }
8061    
8062                            if ((userModelImpl.getColumnBitmask() &
8063                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD_MD.getColumnBitmask()) != 0) {
8064                                    Object[] args = new Object[] {
8065                                                    userModelImpl.getOriginalCompanyId(),
8066                                                    userModelImpl.getOriginalCreateDate(),
8067                                                    userModelImpl.getOriginalModifiedDate()
8068                                            };
8069    
8070                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_CD_MD, args);
8071                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD_MD,
8072                                            args);
8073    
8074                                    args = new Object[] {
8075                                                    userModelImpl.getCompanyId(),
8076                                                    userModelImpl.getCreateDate(),
8077                                                    userModelImpl.getModifiedDate()
8078                                            };
8079    
8080                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_CD_MD, args);
8081                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_CD_MD,
8082                                            args);
8083                            }
8084    
8085                            if ((userModelImpl.getColumnBitmask() &
8086                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_DU_S.getColumnBitmask()) != 0) {
8087                                    Object[] args = new Object[] {
8088                                                    userModelImpl.getOriginalCompanyId(),
8089                                                    userModelImpl.getOriginalDefaultUser(),
8090                                                    userModelImpl.getOriginalStatus()
8091                                            };
8092    
8093                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_DU_S, args);
8094                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_DU_S,
8095                                            args);
8096    
8097                                    args = new Object[] {
8098                                                    userModelImpl.getCompanyId(),
8099                                                    userModelImpl.getDefaultUser(),
8100                                                    userModelImpl.getStatus()
8101                                            };
8102    
8103                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_DU_S, args);
8104                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_DU_S,
8105                                            args);
8106                            }
8107                    }
8108    
8109                    entityCache.putResult(UserModelImpl.ENTITY_CACHE_ENABLED,
8110                            UserImpl.class, user.getPrimaryKey(), user, false);
8111    
8112                    clearUniqueFindersCache(userModelImpl);
8113                    cacheUniqueFindersCache(userModelImpl, isNew);
8114    
8115                    user.resetOriginalValues();
8116    
8117                    return user;
8118            }
8119    
8120            protected User toUnwrappedModel(User user) {
8121                    if (user instanceof UserImpl) {
8122                            return user;
8123                    }
8124    
8125                    UserImpl userImpl = new UserImpl();
8126    
8127                    userImpl.setNew(user.isNew());
8128                    userImpl.setPrimaryKey(user.getPrimaryKey());
8129    
8130                    userImpl.setMvccVersion(user.getMvccVersion());
8131                    userImpl.setUuid(user.getUuid());
8132                    userImpl.setUserId(user.getUserId());
8133                    userImpl.setCompanyId(user.getCompanyId());
8134                    userImpl.setCreateDate(user.getCreateDate());
8135                    userImpl.setModifiedDate(user.getModifiedDate());
8136                    userImpl.setDefaultUser(user.isDefaultUser());
8137                    userImpl.setContactId(user.getContactId());
8138                    userImpl.setPassword(user.getPassword());
8139                    userImpl.setPasswordEncrypted(user.isPasswordEncrypted());
8140                    userImpl.setPasswordReset(user.isPasswordReset());
8141                    userImpl.setPasswordModifiedDate(user.getPasswordModifiedDate());
8142                    userImpl.setDigest(user.getDigest());
8143                    userImpl.setReminderQueryQuestion(user.getReminderQueryQuestion());
8144                    userImpl.setReminderQueryAnswer(user.getReminderQueryAnswer());
8145                    userImpl.setGraceLoginCount(user.getGraceLoginCount());
8146                    userImpl.setScreenName(user.getScreenName());
8147                    userImpl.setEmailAddress(user.getEmailAddress());
8148                    userImpl.setFacebookId(user.getFacebookId());
8149                    userImpl.setGoogleUserId(user.getGoogleUserId());
8150                    userImpl.setLdapServerId(user.getLdapServerId());
8151                    userImpl.setOpenId(user.getOpenId());
8152                    userImpl.setPortraitId(user.getPortraitId());
8153                    userImpl.setLanguageId(user.getLanguageId());
8154                    userImpl.setTimeZoneId(user.getTimeZoneId());
8155                    userImpl.setGreeting(user.getGreeting());
8156                    userImpl.setComments(user.getComments());
8157                    userImpl.setFirstName(user.getFirstName());
8158                    userImpl.setMiddleName(user.getMiddleName());
8159                    userImpl.setLastName(user.getLastName());
8160                    userImpl.setJobTitle(user.getJobTitle());
8161                    userImpl.setLoginDate(user.getLoginDate());
8162                    userImpl.setLoginIP(user.getLoginIP());
8163                    userImpl.setLastLoginDate(user.getLastLoginDate());
8164                    userImpl.setLastLoginIP(user.getLastLoginIP());
8165                    userImpl.setLastFailedLoginDate(user.getLastFailedLoginDate());
8166                    userImpl.setFailedLoginAttempts(user.getFailedLoginAttempts());
8167                    userImpl.setLockout(user.isLockout());
8168                    userImpl.setLockoutDate(user.getLockoutDate());
8169                    userImpl.setAgreedToTermsOfUse(user.isAgreedToTermsOfUse());
8170                    userImpl.setEmailAddressVerified(user.isEmailAddressVerified());
8171                    userImpl.setStatus(user.getStatus());
8172    
8173                    return userImpl;
8174            }
8175    
8176            /**
8177             * Returns the user with the primary key or throws a {@link com.liferay.portal.kernel.exception.NoSuchModelException} if it could not be found.
8178             *
8179             * @param primaryKey the primary key of the user
8180             * @return the user
8181             * @throws NoSuchUserException if a user with the primary key could not be found
8182             */
8183            @Override
8184            public User findByPrimaryKey(Serializable primaryKey)
8185                    throws NoSuchUserException {
8186                    User user = fetchByPrimaryKey(primaryKey);
8187    
8188                    if (user == null) {
8189                            if (_log.isDebugEnabled()) {
8190                                    _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
8191                            }
8192    
8193                            throw new NoSuchUserException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
8194                                    primaryKey);
8195                    }
8196    
8197                    return user;
8198            }
8199    
8200            /**
8201             * Returns the user with the primary key or throws a {@link NoSuchUserException} if it could not be found.
8202             *
8203             * @param userId the primary key of the user
8204             * @return the user
8205             * @throws NoSuchUserException if a user with the primary key could not be found
8206             */
8207            @Override
8208            public User findByPrimaryKey(long userId) throws NoSuchUserException {
8209                    return findByPrimaryKey((Serializable)userId);
8210            }
8211    
8212            /**
8213             * Returns the user with the primary key or returns <code>null</code> if it could not be found.
8214             *
8215             * @param primaryKey the primary key of the user
8216             * @return the user, or <code>null</code> if a user with the primary key could not be found
8217             */
8218            @Override
8219            public User fetchByPrimaryKey(Serializable primaryKey) {
8220                    User user = (User)entityCache.getResult(UserModelImpl.ENTITY_CACHE_ENABLED,
8221                                    UserImpl.class, primaryKey);
8222    
8223                    if (user == _nullUser) {
8224                            return null;
8225                    }
8226    
8227                    if (user == null) {
8228                            Session session = null;
8229    
8230                            try {
8231                                    session = openSession();
8232    
8233                                    user = (User)session.get(UserImpl.class, primaryKey);
8234    
8235                                    if (user != null) {
8236                                            cacheResult(user);
8237                                    }
8238                                    else {
8239                                            entityCache.putResult(UserModelImpl.ENTITY_CACHE_ENABLED,
8240                                                    UserImpl.class, primaryKey, _nullUser);
8241                                    }
8242                            }
8243                            catch (Exception e) {
8244                                    entityCache.removeResult(UserModelImpl.ENTITY_CACHE_ENABLED,
8245                                            UserImpl.class, primaryKey);
8246    
8247                                    throw processException(e);
8248                            }
8249                            finally {
8250                                    closeSession(session);
8251                            }
8252                    }
8253    
8254                    return user;
8255            }
8256    
8257            /**
8258             * Returns the user with the primary key or returns <code>null</code> if it could not be found.
8259             *
8260             * @param userId the primary key of the user
8261             * @return the user, or <code>null</code> if a user with the primary key could not be found
8262             */
8263            @Override
8264            public User fetchByPrimaryKey(long userId) {
8265                    return fetchByPrimaryKey((Serializable)userId);
8266            }
8267    
8268            @Override
8269            public Map<Serializable, User> fetchByPrimaryKeys(
8270                    Set<Serializable> primaryKeys) {
8271                    if (primaryKeys.isEmpty()) {
8272                            return Collections.emptyMap();
8273                    }
8274    
8275                    Map<Serializable, User> map = new HashMap<Serializable, User>();
8276    
8277                    if (primaryKeys.size() == 1) {
8278                            Iterator<Serializable> iterator = primaryKeys.iterator();
8279    
8280                            Serializable primaryKey = iterator.next();
8281    
8282                            User user = fetchByPrimaryKey(primaryKey);
8283    
8284                            if (user != null) {
8285                                    map.put(primaryKey, user);
8286                            }
8287    
8288                            return map;
8289                    }
8290    
8291                    Set<Serializable> uncachedPrimaryKeys = null;
8292    
8293                    for (Serializable primaryKey : primaryKeys) {
8294                            User user = (User)entityCache.getResult(UserModelImpl.ENTITY_CACHE_ENABLED,
8295                                            UserImpl.class, primaryKey);
8296    
8297                            if (user == null) {
8298                                    if (uncachedPrimaryKeys == null) {
8299                                            uncachedPrimaryKeys = new HashSet<Serializable>();
8300                                    }
8301    
8302                                    uncachedPrimaryKeys.add(primaryKey);
8303                            }
8304                            else {
8305                                    map.put(primaryKey, user);
8306                            }
8307                    }
8308    
8309                    if (uncachedPrimaryKeys == null) {
8310                            return map;
8311                    }
8312    
8313                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
8314                                    1);
8315    
8316                    query.append(_SQL_SELECT_USER_WHERE_PKS_IN);
8317    
8318                    for (Serializable primaryKey : uncachedPrimaryKeys) {
8319                            query.append(String.valueOf(primaryKey));
8320    
8321                            query.append(StringPool.COMMA);
8322                    }
8323    
8324                    query.setIndex(query.index() - 1);
8325    
8326                    query.append(StringPool.CLOSE_PARENTHESIS);
8327    
8328                    String sql = query.toString();
8329    
8330                    Session session = null;
8331    
8332                    try {
8333                            session = openSession();
8334    
8335                            Query q = session.createQuery(sql);
8336    
8337                            for (User user : (List<User>)q.list()) {
8338                                    map.put(user.getPrimaryKeyObj(), user);
8339    
8340                                    cacheResult(user);
8341    
8342                                    uncachedPrimaryKeys.remove(user.getPrimaryKeyObj());
8343                            }
8344    
8345                            for (Serializable primaryKey : uncachedPrimaryKeys) {
8346                                    entityCache.putResult(UserModelImpl.ENTITY_CACHE_ENABLED,
8347                                            UserImpl.class, primaryKey, _nullUser);
8348                            }
8349                    }
8350                    catch (Exception e) {
8351                            throw processException(e);
8352                    }
8353                    finally {
8354                            closeSession(session);
8355                    }
8356    
8357                    return map;
8358            }
8359    
8360            /**
8361             * Returns all the users.
8362             *
8363             * @return the users
8364             */
8365            @Override
8366            public List<User> findAll() {
8367                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
8368            }
8369    
8370            /**
8371             * Returns a range of all the users.
8372             *
8373             * <p>
8374             * 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.
8375             * </p>
8376             *
8377             * @param start the lower bound of the range of users
8378             * @param end the upper bound of the range of users (not inclusive)
8379             * @return the range of users
8380             */
8381            @Override
8382            public List<User> findAll(int start, int end) {
8383                    return findAll(start, end, null);
8384            }
8385    
8386            /**
8387             * Returns an ordered range of all the users.
8388             *
8389             * <p>
8390             * 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.
8391             * </p>
8392             *
8393             * @param start the lower bound of the range of users
8394             * @param end the upper bound of the range of users (not inclusive)
8395             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
8396             * @return the ordered range of users
8397             */
8398            @Override
8399            public List<User> findAll(int start, int end,
8400                    OrderByComparator<User> orderByComparator) {
8401                    return findAll(start, end, orderByComparator, true);
8402            }
8403    
8404            /**
8405             * Returns an ordered range of all the users.
8406             *
8407             * <p>
8408             * 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.
8409             * </p>
8410             *
8411             * @param start the lower bound of the range of users
8412             * @param end the upper bound of the range of users (not inclusive)
8413             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
8414             * @param retrieveFromCache whether to retrieve from the finder cache
8415             * @return the ordered range of users
8416             */
8417            @Override
8418            public List<User> findAll(int start, int end,
8419                    OrderByComparator<User> orderByComparator, boolean retrieveFromCache) {
8420                    boolean pagination = true;
8421                    FinderPath finderPath = null;
8422                    Object[] finderArgs = null;
8423    
8424                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
8425                                    (orderByComparator == null)) {
8426                            pagination = false;
8427                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
8428                            finderArgs = FINDER_ARGS_EMPTY;
8429                    }
8430                    else {
8431                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
8432                            finderArgs = new Object[] { start, end, orderByComparator };
8433                    }
8434    
8435                    List<User> list = null;
8436    
8437                    if (retrieveFromCache) {
8438                            list = (List<User>)finderCache.getResult(finderPath, finderArgs,
8439                                            this);
8440                    }
8441    
8442                    if (list == null) {
8443                            StringBundler query = null;
8444                            String sql = null;
8445    
8446                            if (orderByComparator != null) {
8447                                    query = new StringBundler(2 +
8448                                                    (orderByComparator.getOrderByFields().length * 2));
8449    
8450                                    query.append(_SQL_SELECT_USER);
8451    
8452                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
8453                                            orderByComparator);
8454    
8455                                    sql = query.toString();
8456                            }
8457                            else {
8458                                    sql = _SQL_SELECT_USER;
8459    
8460                                    if (pagination) {
8461                                            sql = sql.concat(UserModelImpl.ORDER_BY_JPQL);
8462                                    }
8463                            }
8464    
8465                            Session session = null;
8466    
8467                            try {
8468                                    session = openSession();
8469    
8470                                    Query q = session.createQuery(sql);
8471    
8472                                    if (!pagination) {
8473                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
8474                                                            end, false);
8475    
8476                                            Collections.sort(list);
8477    
8478                                            list = Collections.unmodifiableList(list);
8479                                    }
8480                                    else {
8481                                            list = (List<User>)QueryUtil.list(q, getDialect(), start,
8482                                                            end);
8483                                    }
8484    
8485                                    cacheResult(list);
8486    
8487                                    finderCache.putResult(finderPath, finderArgs, list);
8488                            }
8489                            catch (Exception e) {
8490                                    finderCache.removeResult(finderPath, finderArgs);
8491    
8492                                    throw processException(e);
8493                            }
8494                            finally {
8495                                    closeSession(session);
8496                            }
8497                    }
8498    
8499                    return list;
8500            }
8501    
8502            /**
8503             * Removes all the users from the database.
8504             *
8505             */
8506            @Override
8507            public void removeAll() {
8508                    for (User user : findAll()) {
8509                            remove(user);
8510                    }
8511            }
8512    
8513            /**
8514             * Returns the number of users.
8515             *
8516             * @return the number of users
8517             */
8518            @Override
8519            public int countAll() {
8520                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
8521                                    FINDER_ARGS_EMPTY, this);
8522    
8523                    if (count == null) {
8524                            Session session = null;
8525    
8526                            try {
8527                                    session = openSession();
8528    
8529                                    Query q = session.createQuery(_SQL_COUNT_USER);
8530    
8531                                    count = (Long)q.uniqueResult();
8532    
8533                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
8534                                            count);
8535                            }
8536                            catch (Exception e) {
8537                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
8538                                            FINDER_ARGS_EMPTY);
8539    
8540                                    throw processException(e);
8541                            }
8542                            finally {
8543                                    closeSession(session);
8544                            }
8545                    }
8546    
8547                    return count.intValue();
8548            }
8549    
8550            /**
8551             * Returns the primaryKeys of groups associated with the user.
8552             *
8553             * @param pk the primary key of the user
8554             * @return long[] of the primaryKeys of groups associated with the user
8555             */
8556            @Override
8557            public long[] getGroupPrimaryKeys(long pk) {
8558                    long[] pks = userToGroupTableMapper.getRightPrimaryKeys(pk);
8559    
8560                    return pks.clone();
8561            }
8562    
8563            /**
8564             * Returns all the groups associated with the user.
8565             *
8566             * @param pk the primary key of the user
8567             * @return the groups associated with the user
8568             */
8569            @Override
8570            public List<com.liferay.portal.kernel.model.Group> getGroups(long pk) {
8571                    return getGroups(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
8572            }
8573    
8574            /**
8575             * Returns a range of all the groups associated with the user.
8576             *
8577             * <p>
8578             * 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.
8579             * </p>
8580             *
8581             * @param pk the primary key of the user
8582             * @param start the lower bound of the range of users
8583             * @param end the upper bound of the range of users (not inclusive)
8584             * @return the range of groups associated with the user
8585             */
8586            @Override
8587            public List<com.liferay.portal.kernel.model.Group> getGroups(long pk,
8588                    int start, int end) {
8589                    return getGroups(pk, start, end, null);
8590            }
8591    
8592            /**
8593             * Returns an ordered range of all the groups associated with the user.
8594             *
8595             * <p>
8596             * 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.
8597             * </p>
8598             *
8599             * @param pk the primary key of the user
8600             * @param start the lower bound of the range of users
8601             * @param end the upper bound of the range of users (not inclusive)
8602             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
8603             * @return the ordered range of groups associated with the user
8604             */
8605            @Override
8606            public List<com.liferay.portal.kernel.model.Group> getGroups(long pk,
8607                    int start, int end,
8608                    OrderByComparator<com.liferay.portal.kernel.model.Group> orderByComparator) {
8609                    return userToGroupTableMapper.getRightBaseModels(pk, start, end,
8610                            orderByComparator);
8611            }
8612    
8613            /**
8614             * Returns the number of groups associated with the user.
8615             *
8616             * @param pk the primary key of the user
8617             * @return the number of groups associated with the user
8618             */
8619            @Override
8620            public int getGroupsSize(long pk) {
8621                    long[] pks = userToGroupTableMapper.getRightPrimaryKeys(pk);
8622    
8623                    return pks.length;
8624            }
8625    
8626            /**
8627             * Returns <code>true</code> if the group is associated with the user.
8628             *
8629             * @param pk the primary key of the user
8630             * @param groupPK the primary key of the group
8631             * @return <code>true</code> if the group is associated with the user; <code>false</code> otherwise
8632             */
8633            @Override
8634            public boolean containsGroup(long pk, long groupPK) {
8635                    return userToGroupTableMapper.containsTableMapping(pk, groupPK);
8636            }
8637    
8638            /**
8639             * Returns <code>true</code> if the user has any groups associated with it.
8640             *
8641             * @param pk the primary key of the user to check for associations with groups
8642             * @return <code>true</code> if the user has any groups associated with it; <code>false</code> otherwise
8643             */
8644            @Override
8645            public boolean containsGroups(long pk) {
8646                    if (getGroupsSize(pk) > 0) {
8647                            return true;
8648                    }
8649                    else {
8650                            return false;
8651                    }
8652            }
8653    
8654            /**
8655             * Adds an association between the user and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8656             *
8657             * @param pk the primary key of the user
8658             * @param groupPK the primary key of the group
8659             */
8660            @Override
8661            public void addGroup(long pk, long groupPK) {
8662                    User user = fetchByPrimaryKey(pk);
8663    
8664                    if (user == null) {
8665                            userToGroupTableMapper.addTableMapping(companyProvider.getCompanyId(),
8666                                    pk, groupPK);
8667                    }
8668                    else {
8669                            userToGroupTableMapper.addTableMapping(user.getCompanyId(), pk,
8670                                    groupPK);
8671                    }
8672            }
8673    
8674            /**
8675             * Adds an association between the user and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8676             *
8677             * @param pk the primary key of the user
8678             * @param group the group
8679             */
8680            @Override
8681            public void addGroup(long pk, com.liferay.portal.kernel.model.Group group) {
8682                    User user = fetchByPrimaryKey(pk);
8683    
8684                    if (user == null) {
8685                            userToGroupTableMapper.addTableMapping(companyProvider.getCompanyId(),
8686                                    pk, group.getPrimaryKey());
8687                    }
8688                    else {
8689                            userToGroupTableMapper.addTableMapping(user.getCompanyId(), pk,
8690                                    group.getPrimaryKey());
8691                    }
8692            }
8693    
8694            /**
8695             * Adds an association between the user and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8696             *
8697             * @param pk the primary key of the user
8698             * @param groupPKs the primary keys of the groups
8699             */
8700            @Override
8701            public void addGroups(long pk, long[] groupPKs) {
8702                    long companyId = 0;
8703    
8704                    User user = fetchByPrimaryKey(pk);
8705    
8706                    if (user == null) {
8707                            companyId = companyProvider.getCompanyId();
8708                    }
8709                    else {
8710                            companyId = user.getCompanyId();
8711                    }
8712    
8713                    for (long groupPK : groupPKs) {
8714                            userToGroupTableMapper.addTableMapping(companyId, pk, groupPK);
8715                    }
8716            }
8717    
8718            /**
8719             * Adds an association between the user and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8720             *
8721             * @param pk the primary key of the user
8722             * @param groups the groups
8723             */
8724            @Override
8725            public void addGroups(long pk,
8726                    List<com.liferay.portal.kernel.model.Group> groups) {
8727                    long companyId = 0;
8728    
8729                    User user = fetchByPrimaryKey(pk);
8730    
8731                    if (user == null) {
8732                            companyId = companyProvider.getCompanyId();
8733                    }
8734                    else {
8735                            companyId = user.getCompanyId();
8736                    }
8737    
8738                    for (com.liferay.portal.kernel.model.Group group : groups) {
8739                            userToGroupTableMapper.addTableMapping(companyId, pk,
8740                                    group.getPrimaryKey());
8741                    }
8742            }
8743    
8744            /**
8745             * Clears all associations between the user and its groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8746             *
8747             * @param pk the primary key of the user to clear the associated groups from
8748             */
8749            @Override
8750            public void clearGroups(long pk) {
8751                    userToGroupTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
8752            }
8753    
8754            /**
8755             * Removes the association between the user and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8756             *
8757             * @param pk the primary key of the user
8758             * @param groupPK the primary key of the group
8759             */
8760            @Override
8761            public void removeGroup(long pk, long groupPK) {
8762                    userToGroupTableMapper.deleteTableMapping(pk, groupPK);
8763            }
8764    
8765            /**
8766             * Removes the association between the user and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8767             *
8768             * @param pk the primary key of the user
8769             * @param group the group
8770             */
8771            @Override
8772            public void removeGroup(long pk, com.liferay.portal.kernel.model.Group group) {
8773                    userToGroupTableMapper.deleteTableMapping(pk, group.getPrimaryKey());
8774            }
8775    
8776            /**
8777             * Removes the association between the user and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8778             *
8779             * @param pk the primary key of the user
8780             * @param groupPKs the primary keys of the groups
8781             */
8782            @Override
8783            public void removeGroups(long pk, long[] groupPKs) {
8784                    for (long groupPK : groupPKs) {
8785                            userToGroupTableMapper.deleteTableMapping(pk, groupPK);
8786                    }
8787            }
8788    
8789            /**
8790             * Removes the association between the user and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8791             *
8792             * @param pk the primary key of the user
8793             * @param groups the groups
8794             */
8795            @Override
8796            public void removeGroups(long pk,
8797                    List<com.liferay.portal.kernel.model.Group> groups) {
8798                    for (com.liferay.portal.kernel.model.Group group : groups) {
8799                            userToGroupTableMapper.deleteTableMapping(pk, group.getPrimaryKey());
8800                    }
8801            }
8802    
8803            /**
8804             * 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.
8805             *
8806             * @param pk the primary key of the user
8807             * @param groupPKs the primary keys of the groups to be associated with the user
8808             */
8809            @Override
8810            public void setGroups(long pk, long[] groupPKs) {
8811                    Set<Long> newGroupPKsSet = SetUtil.fromArray(groupPKs);
8812                    Set<Long> oldGroupPKsSet = SetUtil.fromArray(userToGroupTableMapper.getRightPrimaryKeys(
8813                                            pk));
8814    
8815                    Set<Long> removeGroupPKsSet = new HashSet<Long>(oldGroupPKsSet);
8816    
8817                    removeGroupPKsSet.removeAll(newGroupPKsSet);
8818    
8819                    for (long removeGroupPK : removeGroupPKsSet) {
8820                            userToGroupTableMapper.deleteTableMapping(pk, removeGroupPK);
8821                    }
8822    
8823                    newGroupPKsSet.removeAll(oldGroupPKsSet);
8824    
8825                    long companyId = 0;
8826    
8827                    User user = fetchByPrimaryKey(pk);
8828    
8829                    if (user == null) {
8830                            companyId = companyProvider.getCompanyId();
8831                    }
8832                    else {
8833                            companyId = user.getCompanyId();
8834                    }
8835    
8836                    for (long newGroupPK : newGroupPKsSet) {
8837                            userToGroupTableMapper.addTableMapping(companyId, pk, newGroupPK);
8838                    }
8839            }
8840    
8841            /**
8842             * 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.
8843             *
8844             * @param pk the primary key of the user
8845             * @param groups the groups to be associated with the user
8846             */
8847            @Override
8848            public void setGroups(long pk,
8849                    List<com.liferay.portal.kernel.model.Group> groups) {
8850                    try {
8851                            long[] groupPKs = new long[groups.size()];
8852    
8853                            for (int i = 0; i < groups.size(); i++) {
8854                                    com.liferay.portal.kernel.model.Group group = groups.get(i);
8855    
8856                                    groupPKs[i] = group.getPrimaryKey();
8857                            }
8858    
8859                            setGroups(pk, groupPKs);
8860                    }
8861                    catch (Exception e) {
8862                            throw processException(e);
8863                    }
8864            }
8865    
8866            /**
8867             * Returns the primaryKeys of organizations associated with the user.
8868             *
8869             * @param pk the primary key of the user
8870             * @return long[] of the primaryKeys of organizations associated with the user
8871             */
8872            @Override
8873            public long[] getOrganizationPrimaryKeys(long pk) {
8874                    long[] pks = userToOrganizationTableMapper.getRightPrimaryKeys(pk);
8875    
8876                    return pks.clone();
8877            }
8878    
8879            /**
8880             * Returns all the organizations associated with the user.
8881             *
8882             * @param pk the primary key of the user
8883             * @return the organizations associated with the user
8884             */
8885            @Override
8886            public List<com.liferay.portal.kernel.model.Organization> getOrganizations(
8887                    long pk) {
8888                    return getOrganizations(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
8889            }
8890    
8891            /**
8892             * Returns a range of all the organizations associated with the user.
8893             *
8894             * <p>
8895             * 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.
8896             * </p>
8897             *
8898             * @param pk the primary key of the user
8899             * @param start the lower bound of the range of users
8900             * @param end the upper bound of the range of users (not inclusive)
8901             * @return the range of organizations associated with the user
8902             */
8903            @Override
8904            public List<com.liferay.portal.kernel.model.Organization> getOrganizations(
8905                    long pk, int start, int end) {
8906                    return getOrganizations(pk, start, end, null);
8907            }
8908    
8909            /**
8910             * Returns an ordered range of all the organizations associated with the user.
8911             *
8912             * <p>
8913             * 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.
8914             * </p>
8915             *
8916             * @param pk the primary key of the user
8917             * @param start the lower bound of the range of users
8918             * @param end the upper bound of the range of users (not inclusive)
8919             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
8920             * @return the ordered range of organizations associated with the user
8921             */
8922            @Override
8923            public List<com.liferay.portal.kernel.model.Organization> getOrganizations(
8924                    long pk, int start, int end,
8925                    OrderByComparator<com.liferay.portal.kernel.model.Organization> orderByComparator) {
8926                    return userToOrganizationTableMapper.getRightBaseModels(pk, start, end,
8927                            orderByComparator);
8928            }
8929    
8930            /**
8931             * Returns the number of organizations associated with the user.
8932             *
8933             * @param pk the primary key of the user
8934             * @return the number of organizations associated with the user
8935             */
8936            @Override
8937            public int getOrganizationsSize(long pk) {
8938                    long[] pks = userToOrganizationTableMapper.getRightPrimaryKeys(pk);
8939    
8940                    return pks.length;
8941            }
8942    
8943            /**
8944             * Returns <code>true</code> if the organization is associated with the user.
8945             *
8946             * @param pk the primary key of the user
8947             * @param organizationPK the primary key of the organization
8948             * @return <code>true</code> if the organization is associated with the user; <code>false</code> otherwise
8949             */
8950            @Override
8951            public boolean containsOrganization(long pk, long organizationPK) {
8952                    return userToOrganizationTableMapper.containsTableMapping(pk,
8953                            organizationPK);
8954            }
8955    
8956            /**
8957             * Returns <code>true</code> if the user has any organizations associated with it.
8958             *
8959             * @param pk the primary key of the user to check for associations with organizations
8960             * @return <code>true</code> if the user has any organizations associated with it; <code>false</code> otherwise
8961             */
8962            @Override
8963            public boolean containsOrganizations(long pk) {
8964                    if (getOrganizationsSize(pk) > 0) {
8965                            return true;
8966                    }
8967                    else {
8968                            return false;
8969                    }
8970            }
8971    
8972            /**
8973             * Adds an association between the user and the organization. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8974             *
8975             * @param pk the primary key of the user
8976             * @param organizationPK the primary key of the organization
8977             */
8978            @Override
8979            public void addOrganization(long pk, long organizationPK) {
8980                    User user = fetchByPrimaryKey(pk);
8981    
8982                    if (user == null) {
8983                            userToOrganizationTableMapper.addTableMapping(companyProvider.getCompanyId(),
8984                                    pk, organizationPK);
8985                    }
8986                    else {
8987                            userToOrganizationTableMapper.addTableMapping(user.getCompanyId(),
8988                                    pk, organizationPK);
8989                    }
8990            }
8991    
8992            /**
8993             * Adds an association between the user and the organization. Also notifies the appropriate model listeners and clears the mapping table finder cache.
8994             *
8995             * @param pk the primary key of the user
8996             * @param organization the organization
8997             */
8998            @Override
8999            public void addOrganization(long pk,
9000                    com.liferay.portal.kernel.model.Organization organization) {
9001                    User user = fetchByPrimaryKey(pk);
9002    
9003                    if (user == null) {
9004                            userToOrganizationTableMapper.addTableMapping(companyProvider.getCompanyId(),
9005                                    pk, organization.getPrimaryKey());
9006                    }
9007                    else {
9008                            userToOrganizationTableMapper.addTableMapping(user.getCompanyId(),
9009                                    pk, organization.getPrimaryKey());
9010                    }
9011            }
9012    
9013            /**
9014             * Adds an association between the user and the organizations. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9015             *
9016             * @param pk the primary key of the user
9017             * @param organizationPKs the primary keys of the organizations
9018             */
9019            @Override
9020            public void addOrganizations(long pk, long[] organizationPKs) {
9021                    long companyId = 0;
9022    
9023                    User user = fetchByPrimaryKey(pk);
9024    
9025                    if (user == null) {
9026                            companyId = companyProvider.getCompanyId();
9027                    }
9028                    else {
9029                            companyId = user.getCompanyId();
9030                    }
9031    
9032                    for (long organizationPK : organizationPKs) {
9033                            userToOrganizationTableMapper.addTableMapping(companyId, pk,
9034                                    organizationPK);
9035                    }
9036            }
9037    
9038            /**
9039             * Adds an association between the user and the organizations. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9040             *
9041             * @param pk the primary key of the user
9042             * @param organizations the organizations
9043             */
9044            @Override
9045            public void addOrganizations(long pk,
9046                    List<com.liferay.portal.kernel.model.Organization> organizations) {
9047                    long companyId = 0;
9048    
9049                    User user = fetchByPrimaryKey(pk);
9050    
9051                    if (user == null) {
9052                            companyId = companyProvider.getCompanyId();
9053                    }
9054                    else {
9055                            companyId = user.getCompanyId();
9056                    }
9057    
9058                    for (com.liferay.portal.kernel.model.Organization organization : organizations) {
9059                            userToOrganizationTableMapper.addTableMapping(companyId, pk,
9060                                    organization.getPrimaryKey());
9061                    }
9062            }
9063    
9064            /**
9065             * Clears all associations between the user and its organizations. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9066             *
9067             * @param pk the primary key of the user to clear the associated organizations from
9068             */
9069            @Override
9070            public void clearOrganizations(long pk) {
9071                    userToOrganizationTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
9072            }
9073    
9074            /**
9075             * Removes the association between the user and the organization. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9076             *
9077             * @param pk the primary key of the user
9078             * @param organizationPK the primary key of the organization
9079             */
9080            @Override
9081            public void removeOrganization(long pk, long organizationPK) {
9082                    userToOrganizationTableMapper.deleteTableMapping(pk, organizationPK);
9083            }
9084    
9085            /**
9086             * Removes the association between the user and the organization. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9087             *
9088             * @param pk the primary key of the user
9089             * @param organization the organization
9090             */
9091            @Override
9092            public void removeOrganization(long pk,
9093                    com.liferay.portal.kernel.model.Organization organization) {
9094                    userToOrganizationTableMapper.deleteTableMapping(pk,
9095                            organization.getPrimaryKey());
9096            }
9097    
9098            /**
9099             * Removes the association between the user and the organizations. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9100             *
9101             * @param pk the primary key of the user
9102             * @param organizationPKs the primary keys of the organizations
9103             */
9104            @Override
9105            public void removeOrganizations(long pk, long[] organizationPKs) {
9106                    for (long organizationPK : organizationPKs) {
9107                            userToOrganizationTableMapper.deleteTableMapping(pk, organizationPK);
9108                    }
9109            }
9110    
9111            /**
9112             * Removes the association between the user and the organizations. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9113             *
9114             * @param pk the primary key of the user
9115             * @param organizations the organizations
9116             */
9117            @Override
9118            public void removeOrganizations(long pk,
9119                    List<com.liferay.portal.kernel.model.Organization> organizations) {
9120                    for (com.liferay.portal.kernel.model.Organization organization : organizations) {
9121                            userToOrganizationTableMapper.deleteTableMapping(pk,
9122                                    organization.getPrimaryKey());
9123                    }
9124            }
9125    
9126            /**
9127             * 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.
9128             *
9129             * @param pk the primary key of the user
9130             * @param organizationPKs the primary keys of the organizations to be associated with the user
9131             */
9132            @Override
9133            public void setOrganizations(long pk, long[] organizationPKs) {
9134                    Set<Long> newOrganizationPKsSet = SetUtil.fromArray(organizationPKs);
9135                    Set<Long> oldOrganizationPKsSet = SetUtil.fromArray(userToOrganizationTableMapper.getRightPrimaryKeys(
9136                                            pk));
9137    
9138                    Set<Long> removeOrganizationPKsSet = new HashSet<Long>(oldOrganizationPKsSet);
9139    
9140                    removeOrganizationPKsSet.removeAll(newOrganizationPKsSet);
9141    
9142                    for (long removeOrganizationPK : removeOrganizationPKsSet) {
9143                            userToOrganizationTableMapper.deleteTableMapping(pk,
9144                                    removeOrganizationPK);
9145                    }
9146    
9147                    newOrganizationPKsSet.removeAll(oldOrganizationPKsSet);
9148    
9149                    long companyId = 0;
9150    
9151                    User user = fetchByPrimaryKey(pk);
9152    
9153                    if (user == null) {
9154                            companyId = companyProvider.getCompanyId();
9155                    }
9156                    else {
9157                            companyId = user.getCompanyId();
9158                    }
9159    
9160                    for (long newOrganizationPK : newOrganizationPKsSet) {
9161                            userToOrganizationTableMapper.addTableMapping(companyId, pk,
9162                                    newOrganizationPK);
9163                    }
9164            }
9165    
9166            /**
9167             * 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.
9168             *
9169             * @param pk the primary key of the user
9170             * @param organizations the organizations to be associated with the user
9171             */
9172            @Override
9173            public void setOrganizations(long pk,
9174                    List<com.liferay.portal.kernel.model.Organization> organizations) {
9175                    try {
9176                            long[] organizationPKs = new long[organizations.size()];
9177    
9178                            for (int i = 0; i < organizations.size(); i++) {
9179                                    com.liferay.portal.kernel.model.Organization organization = organizations.get(i);
9180    
9181                                    organizationPKs[i] = organization.getPrimaryKey();
9182                            }
9183    
9184                            setOrganizations(pk, organizationPKs);
9185                    }
9186                    catch (Exception e) {
9187                            throw processException(e);
9188                    }
9189            }
9190    
9191            /**
9192             * Returns the primaryKeys of roles associated with the user.
9193             *
9194             * @param pk the primary key of the user
9195             * @return long[] of the primaryKeys of roles associated with the user
9196             */
9197            @Override
9198            public long[] getRolePrimaryKeys(long pk) {
9199                    long[] pks = userToRoleTableMapper.getRightPrimaryKeys(pk);
9200    
9201                    return pks.clone();
9202            }
9203    
9204            /**
9205             * Returns all the roles associated with the user.
9206             *
9207             * @param pk the primary key of the user
9208             * @return the roles associated with the user
9209             */
9210            @Override
9211            public List<com.liferay.portal.kernel.model.Role> getRoles(long pk) {
9212                    return getRoles(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
9213            }
9214    
9215            /**
9216             * Returns a range of all the roles associated with the user.
9217             *
9218             * <p>
9219             * 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.
9220             * </p>
9221             *
9222             * @param pk the primary key of the user
9223             * @param start the lower bound of the range of users
9224             * @param end the upper bound of the range of users (not inclusive)
9225             * @return the range of roles associated with the user
9226             */
9227            @Override
9228            public List<com.liferay.portal.kernel.model.Role> getRoles(long pk,
9229                    int start, int end) {
9230                    return getRoles(pk, start, end, null);
9231            }
9232    
9233            /**
9234             * Returns an ordered range of all the roles associated with the user.
9235             *
9236             * <p>
9237             * 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.
9238             * </p>
9239             *
9240             * @param pk the primary key of the user
9241             * @param start the lower bound of the range of users
9242             * @param end the upper bound of the range of users (not inclusive)
9243             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
9244             * @return the ordered range of roles associated with the user
9245             */
9246            @Override
9247            public List<com.liferay.portal.kernel.model.Role> getRoles(long pk,
9248                    int start, int end,
9249                    OrderByComparator<com.liferay.portal.kernel.model.Role> orderByComparator) {
9250                    return userToRoleTableMapper.getRightBaseModels(pk, start, end,
9251                            orderByComparator);
9252            }
9253    
9254            /**
9255             * Returns the number of roles associated with the user.
9256             *
9257             * @param pk the primary key of the user
9258             * @return the number of roles associated with the user
9259             */
9260            @Override
9261            public int getRolesSize(long pk) {
9262                    long[] pks = userToRoleTableMapper.getRightPrimaryKeys(pk);
9263    
9264                    return pks.length;
9265            }
9266    
9267            /**
9268             * Returns <code>true</code> if the role is associated with the user.
9269             *
9270             * @param pk the primary key of the user
9271             * @param rolePK the primary key of the role
9272             * @return <code>true</code> if the role is associated with the user; <code>false</code> otherwise
9273             */
9274            @Override
9275            public boolean containsRole(long pk, long rolePK) {
9276                    return userToRoleTableMapper.containsTableMapping(pk, rolePK);
9277            }
9278    
9279            /**
9280             * Returns <code>true</code> if the user has any roles associated with it.
9281             *
9282             * @param pk the primary key of the user to check for associations with roles
9283             * @return <code>true</code> if the user has any roles associated with it; <code>false</code> otherwise
9284             */
9285            @Override
9286            public boolean containsRoles(long pk) {
9287                    if (getRolesSize(pk) > 0) {
9288                            return true;
9289                    }
9290                    else {
9291                            return false;
9292                    }
9293            }
9294    
9295            /**
9296             * Adds an association between the user and the role. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9297             *
9298             * @param pk the primary key of the user
9299             * @param rolePK the primary key of the role
9300             */
9301            @Override
9302            public void addRole(long pk, long rolePK) {
9303                    User user = fetchByPrimaryKey(pk);
9304    
9305                    if (user == null) {
9306                            userToRoleTableMapper.addTableMapping(companyProvider.getCompanyId(),
9307                                    pk, rolePK);
9308                    }
9309                    else {
9310                            userToRoleTableMapper.addTableMapping(user.getCompanyId(), pk,
9311                                    rolePK);
9312                    }
9313            }
9314    
9315            /**
9316             * Adds an association between the user and the role. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9317             *
9318             * @param pk the primary key of the user
9319             * @param role the role
9320             */
9321            @Override
9322            public void addRole(long pk, com.liferay.portal.kernel.model.Role role) {
9323                    User user = fetchByPrimaryKey(pk);
9324    
9325                    if (user == null) {
9326                            userToRoleTableMapper.addTableMapping(companyProvider.getCompanyId(),
9327                                    pk, role.getPrimaryKey());
9328                    }
9329                    else {
9330                            userToRoleTableMapper.addTableMapping(user.getCompanyId(), pk,
9331                                    role.getPrimaryKey());
9332                    }
9333            }
9334    
9335            /**
9336             * Adds an association between the user and the roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9337             *
9338             * @param pk the primary key of the user
9339             * @param rolePKs the primary keys of the roles
9340             */
9341            @Override
9342            public void addRoles(long pk, long[] rolePKs) {
9343                    long companyId = 0;
9344    
9345                    User user = fetchByPrimaryKey(pk);
9346    
9347                    if (user == null) {
9348                            companyId = companyProvider.getCompanyId();
9349                    }
9350                    else {
9351                            companyId = user.getCompanyId();
9352                    }
9353    
9354                    for (long rolePK : rolePKs) {
9355                            userToRoleTableMapper.addTableMapping(companyId, pk, rolePK);
9356                    }
9357            }
9358    
9359            /**
9360             * Adds an association between the user and the roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9361             *
9362             * @param pk the primary key of the user
9363             * @param roles the roles
9364             */
9365            @Override
9366            public void addRoles(long pk,
9367                    List<com.liferay.portal.kernel.model.Role> roles) {
9368                    long companyId = 0;
9369    
9370                    User user = fetchByPrimaryKey(pk);
9371    
9372                    if (user == null) {
9373                            companyId = companyProvider.getCompanyId();
9374                    }
9375                    else {
9376                            companyId = user.getCompanyId();
9377                    }
9378    
9379                    for (com.liferay.portal.kernel.model.Role role : roles) {
9380                            userToRoleTableMapper.addTableMapping(companyId, pk,
9381                                    role.getPrimaryKey());
9382                    }
9383            }
9384    
9385            /**
9386             * Clears all associations between the user and its roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9387             *
9388             * @param pk the primary key of the user to clear the associated roles from
9389             */
9390            @Override
9391            public void clearRoles(long pk) {
9392                    userToRoleTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
9393            }
9394    
9395            /**
9396             * Removes the association between the user and the role. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9397             *
9398             * @param pk the primary key of the user
9399             * @param rolePK the primary key of the role
9400             */
9401            @Override
9402            public void removeRole(long pk, long rolePK) {
9403                    userToRoleTableMapper.deleteTableMapping(pk, rolePK);
9404            }
9405    
9406            /**
9407             * Removes the association between the user and the role. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9408             *
9409             * @param pk the primary key of the user
9410             * @param role the role
9411             */
9412            @Override
9413            public void removeRole(long pk, com.liferay.portal.kernel.model.Role role) {
9414                    userToRoleTableMapper.deleteTableMapping(pk, role.getPrimaryKey());
9415            }
9416    
9417            /**
9418             * Removes the association between the user and the roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9419             *
9420             * @param pk the primary key of the user
9421             * @param rolePKs the primary keys of the roles
9422             */
9423            @Override
9424            public void removeRoles(long pk, long[] rolePKs) {
9425                    for (long rolePK : rolePKs) {
9426                            userToRoleTableMapper.deleteTableMapping(pk, rolePK);
9427                    }
9428            }
9429    
9430            /**
9431             * Removes the association between the user and the roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9432             *
9433             * @param pk the primary key of the user
9434             * @param roles the roles
9435             */
9436            @Override
9437            public void removeRoles(long pk,
9438                    List<com.liferay.portal.kernel.model.Role> roles) {
9439                    for (com.liferay.portal.kernel.model.Role role : roles) {
9440                            userToRoleTableMapper.deleteTableMapping(pk, role.getPrimaryKey());
9441                    }
9442            }
9443    
9444            /**
9445             * 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.
9446             *
9447             * @param pk the primary key of the user
9448             * @param rolePKs the primary keys of the roles to be associated with the user
9449             */
9450            @Override
9451            public void setRoles(long pk, long[] rolePKs) {
9452                    Set<Long> newRolePKsSet = SetUtil.fromArray(rolePKs);
9453                    Set<Long> oldRolePKsSet = SetUtil.fromArray(userToRoleTableMapper.getRightPrimaryKeys(
9454                                            pk));
9455    
9456                    Set<Long> removeRolePKsSet = new HashSet<Long>(oldRolePKsSet);
9457    
9458                    removeRolePKsSet.removeAll(newRolePKsSet);
9459    
9460                    for (long removeRolePK : removeRolePKsSet) {
9461                            userToRoleTableMapper.deleteTableMapping(pk, removeRolePK);
9462                    }
9463    
9464                    newRolePKsSet.removeAll(oldRolePKsSet);
9465    
9466                    long companyId = 0;
9467    
9468                    User user = fetchByPrimaryKey(pk);
9469    
9470                    if (user == null) {
9471                            companyId = companyProvider.getCompanyId();
9472                    }
9473                    else {
9474                            companyId = user.getCompanyId();
9475                    }
9476    
9477                    for (long newRolePK : newRolePKsSet) {
9478                            userToRoleTableMapper.addTableMapping(companyId, pk, newRolePK);
9479                    }
9480            }
9481    
9482            /**
9483             * 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.
9484             *
9485             * @param pk the primary key of the user
9486             * @param roles the roles to be associated with the user
9487             */
9488            @Override
9489            public void setRoles(long pk,
9490                    List<com.liferay.portal.kernel.model.Role> roles) {
9491                    try {
9492                            long[] rolePKs = new long[roles.size()];
9493    
9494                            for (int i = 0; i < roles.size(); i++) {
9495                                    com.liferay.portal.kernel.model.Role role = roles.get(i);
9496    
9497                                    rolePKs[i] = role.getPrimaryKey();
9498                            }
9499    
9500                            setRoles(pk, rolePKs);
9501                    }
9502                    catch (Exception e) {
9503                            throw processException(e);
9504                    }
9505            }
9506    
9507            /**
9508             * Returns the primaryKeys of teams associated with the user.
9509             *
9510             * @param pk the primary key of the user
9511             * @return long[] of the primaryKeys of teams associated with the user
9512             */
9513            @Override
9514            public long[] getTeamPrimaryKeys(long pk) {
9515                    long[] pks = userToTeamTableMapper.getRightPrimaryKeys(pk);
9516    
9517                    return pks.clone();
9518            }
9519    
9520            /**
9521             * Returns all the teams associated with the user.
9522             *
9523             * @param pk the primary key of the user
9524             * @return the teams associated with the user
9525             */
9526            @Override
9527            public List<com.liferay.portal.kernel.model.Team> getTeams(long pk) {
9528                    return getTeams(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
9529            }
9530    
9531            /**
9532             * Returns a range of all the teams associated with the user.
9533             *
9534             * <p>
9535             * 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.
9536             * </p>
9537             *
9538             * @param pk the primary key of the user
9539             * @param start the lower bound of the range of users
9540             * @param end the upper bound of the range of users (not inclusive)
9541             * @return the range of teams associated with the user
9542             */
9543            @Override
9544            public List<com.liferay.portal.kernel.model.Team> getTeams(long pk,
9545                    int start, int end) {
9546                    return getTeams(pk, start, end, null);
9547            }
9548    
9549            /**
9550             * Returns an ordered range of all the teams associated with the user.
9551             *
9552             * <p>
9553             * 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.
9554             * </p>
9555             *
9556             * @param pk the primary key of the user
9557             * @param start the lower bound of the range of users
9558             * @param end the upper bound of the range of users (not inclusive)
9559             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
9560             * @return the ordered range of teams associated with the user
9561             */
9562            @Override
9563            public List<com.liferay.portal.kernel.model.Team> getTeams(long pk,
9564                    int start, int end,
9565                    OrderByComparator<com.liferay.portal.kernel.model.Team> orderByComparator) {
9566                    return userToTeamTableMapper.getRightBaseModels(pk, start, end,
9567                            orderByComparator);
9568            }
9569    
9570            /**
9571             * Returns the number of teams associated with the user.
9572             *
9573             * @param pk the primary key of the user
9574             * @return the number of teams associated with the user
9575             */
9576            @Override
9577            public int getTeamsSize(long pk) {
9578                    long[] pks = userToTeamTableMapper.getRightPrimaryKeys(pk);
9579    
9580                    return pks.length;
9581            }
9582    
9583            /**
9584             * Returns <code>true</code> if the team is associated with the user.
9585             *
9586             * @param pk the primary key of the user
9587             * @param teamPK the primary key of the team
9588             * @return <code>true</code> if the team is associated with the user; <code>false</code> otherwise
9589             */
9590            @Override
9591            public boolean containsTeam(long pk, long teamPK) {
9592                    return userToTeamTableMapper.containsTableMapping(pk, teamPK);
9593            }
9594    
9595            /**
9596             * Returns <code>true</code> if the user has any teams associated with it.
9597             *
9598             * @param pk the primary key of the user to check for associations with teams
9599             * @return <code>true</code> if the user has any teams associated with it; <code>false</code> otherwise
9600             */
9601            @Override
9602            public boolean containsTeams(long pk) {
9603                    if (getTeamsSize(pk) > 0) {
9604                            return true;
9605                    }
9606                    else {
9607                            return false;
9608                    }
9609            }
9610    
9611            /**
9612             * Adds an association between the user and the team. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9613             *
9614             * @param pk the primary key of the user
9615             * @param teamPK the primary key of the team
9616             */
9617            @Override
9618            public void addTeam(long pk, long teamPK) {
9619                    User user = fetchByPrimaryKey(pk);
9620    
9621                    if (user == null) {
9622                            userToTeamTableMapper.addTableMapping(companyProvider.getCompanyId(),
9623                                    pk, teamPK);
9624                    }
9625                    else {
9626                            userToTeamTableMapper.addTableMapping(user.getCompanyId(), pk,
9627                                    teamPK);
9628                    }
9629            }
9630    
9631            /**
9632             * Adds an association between the user and the team. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9633             *
9634             * @param pk the primary key of the user
9635             * @param team the team
9636             */
9637            @Override
9638            public void addTeam(long pk, com.liferay.portal.kernel.model.Team team) {
9639                    User user = fetchByPrimaryKey(pk);
9640    
9641                    if (user == null) {
9642                            userToTeamTableMapper.addTableMapping(companyProvider.getCompanyId(),
9643                                    pk, team.getPrimaryKey());
9644                    }
9645                    else {
9646                            userToTeamTableMapper.addTableMapping(user.getCompanyId(), pk,
9647                                    team.getPrimaryKey());
9648                    }
9649            }
9650    
9651            /**
9652             * Adds an association between the user and the teams. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9653             *
9654             * @param pk the primary key of the user
9655             * @param teamPKs the primary keys of the teams
9656             */
9657            @Override
9658            public void addTeams(long pk, long[] teamPKs) {
9659                    long companyId = 0;
9660    
9661                    User user = fetchByPrimaryKey(pk);
9662    
9663                    if (user == null) {
9664                            companyId = companyProvider.getCompanyId();
9665                    }
9666                    else {
9667                            companyId = user.getCompanyId();
9668                    }
9669    
9670                    for (long teamPK : teamPKs) {
9671                            userToTeamTableMapper.addTableMapping(companyId, pk, teamPK);
9672                    }
9673            }
9674    
9675            /**
9676             * Adds an association between the user and the teams. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9677             *
9678             * @param pk the primary key of the user
9679             * @param teams the teams
9680             */
9681            @Override
9682            public void addTeams(long pk,
9683                    List<com.liferay.portal.kernel.model.Team> teams) {
9684                    long companyId = 0;
9685    
9686                    User user = fetchByPrimaryKey(pk);
9687    
9688                    if (user == null) {
9689                            companyId = companyProvider.getCompanyId();
9690                    }
9691                    else {
9692                            companyId = user.getCompanyId();
9693                    }
9694    
9695                    for (com.liferay.portal.kernel.model.Team team : teams) {
9696                            userToTeamTableMapper.addTableMapping(companyId, pk,
9697                                    team.getPrimaryKey());
9698                    }
9699            }
9700    
9701            /**
9702             * Clears all associations between the user and its teams. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9703             *
9704             * @param pk the primary key of the user to clear the associated teams from
9705             */
9706            @Override
9707            public void clearTeams(long pk) {
9708                    userToTeamTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
9709            }
9710    
9711            /**
9712             * Removes the association between the user and the team. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9713             *
9714             * @param pk the primary key of the user
9715             * @param teamPK the primary key of the team
9716             */
9717            @Override
9718            public void removeTeam(long pk, long teamPK) {
9719                    userToTeamTableMapper.deleteTableMapping(pk, teamPK);
9720            }
9721    
9722            /**
9723             * Removes the association between the user and the team. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9724             *
9725             * @param pk the primary key of the user
9726             * @param team the team
9727             */
9728            @Override
9729            public void removeTeam(long pk, com.liferay.portal.kernel.model.Team team) {
9730                    userToTeamTableMapper.deleteTableMapping(pk, team.getPrimaryKey());
9731            }
9732    
9733            /**
9734             * Removes the association between the user and the teams. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9735             *
9736             * @param pk the primary key of the user
9737             * @param teamPKs the primary keys of the teams
9738             */
9739            @Override
9740            public void removeTeams(long pk, long[] teamPKs) {
9741                    for (long teamPK : teamPKs) {
9742                            userToTeamTableMapper.deleteTableMapping(pk, teamPK);
9743                    }
9744            }
9745    
9746            /**
9747             * Removes the association between the user and the teams. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9748             *
9749             * @param pk the primary key of the user
9750             * @param teams the teams
9751             */
9752            @Override
9753            public void removeTeams(long pk,
9754                    List<com.liferay.portal.kernel.model.Team> teams) {
9755                    for (com.liferay.portal.kernel.model.Team team : teams) {
9756                            userToTeamTableMapper.deleteTableMapping(pk, team.getPrimaryKey());
9757                    }
9758            }
9759    
9760            /**
9761             * 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.
9762             *
9763             * @param pk the primary key of the user
9764             * @param teamPKs the primary keys of the teams to be associated with the user
9765             */
9766            @Override
9767            public void setTeams(long pk, long[] teamPKs) {
9768                    Set<Long> newTeamPKsSet = SetUtil.fromArray(teamPKs);
9769                    Set<Long> oldTeamPKsSet = SetUtil.fromArray(userToTeamTableMapper.getRightPrimaryKeys(
9770                                            pk));
9771    
9772                    Set<Long> removeTeamPKsSet = new HashSet<Long>(oldTeamPKsSet);
9773    
9774                    removeTeamPKsSet.removeAll(newTeamPKsSet);
9775    
9776                    for (long removeTeamPK : removeTeamPKsSet) {
9777                            userToTeamTableMapper.deleteTableMapping(pk, removeTeamPK);
9778                    }
9779    
9780                    newTeamPKsSet.removeAll(oldTeamPKsSet);
9781    
9782                    long companyId = 0;
9783    
9784                    User user = fetchByPrimaryKey(pk);
9785    
9786                    if (user == null) {
9787                            companyId = companyProvider.getCompanyId();
9788                    }
9789                    else {
9790                            companyId = user.getCompanyId();
9791                    }
9792    
9793                    for (long newTeamPK : newTeamPKsSet) {
9794                            userToTeamTableMapper.addTableMapping(companyId, pk, newTeamPK);
9795                    }
9796            }
9797    
9798            /**
9799             * 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.
9800             *
9801             * @param pk the primary key of the user
9802             * @param teams the teams to be associated with the user
9803             */
9804            @Override
9805            public void setTeams(long pk,
9806                    List<com.liferay.portal.kernel.model.Team> teams) {
9807                    try {
9808                            long[] teamPKs = new long[teams.size()];
9809    
9810                            for (int i = 0; i < teams.size(); i++) {
9811                                    com.liferay.portal.kernel.model.Team team = teams.get(i);
9812    
9813                                    teamPKs[i] = team.getPrimaryKey();
9814                            }
9815    
9816                            setTeams(pk, teamPKs);
9817                    }
9818                    catch (Exception e) {
9819                            throw processException(e);
9820                    }
9821            }
9822    
9823            /**
9824             * Returns the primaryKeys of user groups associated with the user.
9825             *
9826             * @param pk the primary key of the user
9827             * @return long[] of the primaryKeys of user groups associated with the user
9828             */
9829            @Override
9830            public long[] getUserGroupPrimaryKeys(long pk) {
9831                    long[] pks = userToUserGroupTableMapper.getRightPrimaryKeys(pk);
9832    
9833                    return pks.clone();
9834            }
9835    
9836            /**
9837             * Returns all the user groups associated with the user.
9838             *
9839             * @param pk the primary key of the user
9840             * @return the user groups associated with the user
9841             */
9842            @Override
9843            public List<com.liferay.portal.kernel.model.UserGroup> getUserGroups(
9844                    long pk) {
9845                    return getUserGroups(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
9846            }
9847    
9848            /**
9849             * Returns a range of all the user groups associated with the user.
9850             *
9851             * <p>
9852             * 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.
9853             * </p>
9854             *
9855             * @param pk the primary key of the user
9856             * @param start the lower bound of the range of users
9857             * @param end the upper bound of the range of users (not inclusive)
9858             * @return the range of user groups associated with the user
9859             */
9860            @Override
9861            public List<com.liferay.portal.kernel.model.UserGroup> getUserGroups(
9862                    long pk, int start, int end) {
9863                    return getUserGroups(pk, start, end, null);
9864            }
9865    
9866            /**
9867             * Returns an ordered range of all the user groups associated with the user.
9868             *
9869             * <p>
9870             * 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.
9871             * </p>
9872             *
9873             * @param pk the primary key of the user
9874             * @param start the lower bound of the range of users
9875             * @param end the upper bound of the range of users (not inclusive)
9876             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
9877             * @return the ordered range of user groups associated with the user
9878             */
9879            @Override
9880            public List<com.liferay.portal.kernel.model.UserGroup> getUserGroups(
9881                    long pk, int start, int end,
9882                    OrderByComparator<com.liferay.portal.kernel.model.UserGroup> orderByComparator) {
9883                    return userToUserGroupTableMapper.getRightBaseModels(pk, start, end,
9884                            orderByComparator);
9885            }
9886    
9887            /**
9888             * Returns the number of user groups associated with the user.
9889             *
9890             * @param pk the primary key of the user
9891             * @return the number of user groups associated with the user
9892             */
9893            @Override
9894            public int getUserGroupsSize(long pk) {
9895                    long[] pks = userToUserGroupTableMapper.getRightPrimaryKeys(pk);
9896    
9897                    return pks.length;
9898            }
9899    
9900            /**
9901             * Returns <code>true</code> if the user group is associated with the user.
9902             *
9903             * @param pk the primary key of the user
9904             * @param userGroupPK the primary key of the user group
9905             * @return <code>true</code> if the user group is associated with the user; <code>false</code> otherwise
9906             */
9907            @Override
9908            public boolean containsUserGroup(long pk, long userGroupPK) {
9909                    return userToUserGroupTableMapper.containsTableMapping(pk, userGroupPK);
9910            }
9911    
9912            /**
9913             * Returns <code>true</code> if the user has any user groups associated with it.
9914             *
9915             * @param pk the primary key of the user to check for associations with user groups
9916             * @return <code>true</code> if the user has any user groups associated with it; <code>false</code> otherwise
9917             */
9918            @Override
9919            public boolean containsUserGroups(long pk) {
9920                    if (getUserGroupsSize(pk) > 0) {
9921                            return true;
9922                    }
9923                    else {
9924                            return false;
9925                    }
9926            }
9927    
9928            /**
9929             * Adds an association between the user and the user group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9930             *
9931             * @param pk the primary key of the user
9932             * @param userGroupPK the primary key of the user group
9933             */
9934            @Override
9935            public void addUserGroup(long pk, long userGroupPK) {
9936                    User user = fetchByPrimaryKey(pk);
9937    
9938                    if (user == null) {
9939                            userToUserGroupTableMapper.addTableMapping(companyProvider.getCompanyId(),
9940                                    pk, userGroupPK);
9941                    }
9942                    else {
9943                            userToUserGroupTableMapper.addTableMapping(user.getCompanyId(), pk,
9944                                    userGroupPK);
9945                    }
9946            }
9947    
9948            /**
9949             * Adds an association between the user and the user group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9950             *
9951             * @param pk the primary key of the user
9952             * @param userGroup the user group
9953             */
9954            @Override
9955            public void addUserGroup(long pk,
9956                    com.liferay.portal.kernel.model.UserGroup userGroup) {
9957                    User user = fetchByPrimaryKey(pk);
9958    
9959                    if (user == null) {
9960                            userToUserGroupTableMapper.addTableMapping(companyProvider.getCompanyId(),
9961                                    pk, userGroup.getPrimaryKey());
9962                    }
9963                    else {
9964                            userToUserGroupTableMapper.addTableMapping(user.getCompanyId(), pk,
9965                                    userGroup.getPrimaryKey());
9966                    }
9967            }
9968    
9969            /**
9970             * Adds an association between the user and the user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9971             *
9972             * @param pk the primary key of the user
9973             * @param userGroupPKs the primary keys of the user groups
9974             */
9975            @Override
9976            public void addUserGroups(long pk, long[] userGroupPKs) {
9977                    long companyId = 0;
9978    
9979                    User user = fetchByPrimaryKey(pk);
9980    
9981                    if (user == null) {
9982                            companyId = companyProvider.getCompanyId();
9983                    }
9984                    else {
9985                            companyId = user.getCompanyId();
9986                    }
9987    
9988                    for (long userGroupPK : userGroupPKs) {
9989                            userToUserGroupTableMapper.addTableMapping(companyId, pk,
9990                                    userGroupPK);
9991                    }
9992            }
9993    
9994            /**
9995             * Adds an association between the user and the user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
9996             *
9997             * @param pk the primary key of the user
9998             * @param userGroups the user groups
9999             */
10000            @Override
10001            public void addUserGroups(long pk,
10002                    List<com.liferay.portal.kernel.model.UserGroup> userGroups) {
10003                    long companyId = 0;
10004    
10005                    User user = fetchByPrimaryKey(pk);
10006    
10007                    if (user == null) {
10008                            companyId = companyProvider.getCompanyId();
10009                    }
10010                    else {
10011                            companyId = user.getCompanyId();
10012                    }
10013    
10014                    for (com.liferay.portal.kernel.model.UserGroup userGroup : userGroups) {
10015                            userToUserGroupTableMapper.addTableMapping(companyId, pk,
10016                                    userGroup.getPrimaryKey());
10017                    }
10018            }
10019    
10020            /**
10021             * Clears all associations between the user and its user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
10022             *
10023             * @param pk the primary key of the user to clear the associated user groups from
10024             */
10025            @Override
10026            public void clearUserGroups(long pk) {
10027                    userToUserGroupTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
10028            }
10029    
10030            /**
10031             * Removes the association between the user and the user group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
10032             *
10033             * @param pk the primary key of the user
10034             * @param userGroupPK the primary key of the user group
10035             */
10036            @Override
10037            public void removeUserGroup(long pk, long userGroupPK) {
10038                    userToUserGroupTableMapper.deleteTableMapping(pk, userGroupPK);
10039            }
10040    
10041            /**
10042             * Removes the association between the user and the user group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
10043             *
10044             * @param pk the primary key of the user
10045             * @param userGroup the user group
10046             */
10047            @Override
10048            public void removeUserGroup(long pk,
10049                    com.liferay.portal.kernel.model.UserGroup userGroup) {
10050                    userToUserGroupTableMapper.deleteTableMapping(pk,
10051                            userGroup.getPrimaryKey());
10052            }
10053    
10054            /**
10055             * Removes the association between the user and the user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
10056             *
10057             * @param pk the primary key of the user
10058             * @param userGroupPKs the primary keys of the user groups
10059             */
10060            @Override
10061            public void removeUserGroups(long pk, long[] userGroupPKs) {
10062                    for (long userGroupPK : userGroupPKs) {
10063                            userToUserGroupTableMapper.deleteTableMapping(pk, userGroupPK);
10064                    }
10065            }
10066    
10067            /**
10068             * Removes the association between the user and the user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
10069             *
10070             * @param pk the primary key of the user
10071             * @param userGroups the user groups
10072             */
10073            @Override
10074            public void removeUserGroups(long pk,
10075                    List<com.liferay.portal.kernel.model.UserGroup> userGroups) {
10076                    for (com.liferay.portal.kernel.model.UserGroup userGroup : userGroups) {
10077                            userToUserGroupTableMapper.deleteTableMapping(pk,
10078                                    userGroup.getPrimaryKey());
10079                    }
10080            }
10081    
10082            /**
10083             * 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.
10084             *
10085             * @param pk the primary key of the user
10086             * @param userGroupPKs the primary keys of the user groups to be associated with the user
10087             */
10088            @Override
10089            public void setUserGroups(long pk, long[] userGroupPKs) {
10090                    Set<Long> newUserGroupPKsSet = SetUtil.fromArray(userGroupPKs);
10091                    Set<Long> oldUserGroupPKsSet = SetUtil.fromArray(userToUserGroupTableMapper.getRightPrimaryKeys(
10092                                            pk));
10093    
10094                    Set<Long> removeUserGroupPKsSet = new HashSet<Long>(oldUserGroupPKsSet);
10095    
10096                    removeUserGroupPKsSet.removeAll(newUserGroupPKsSet);
10097    
10098                    for (long removeUserGroupPK : removeUserGroupPKsSet) {
10099                            userToUserGroupTableMapper.deleteTableMapping(pk, removeUserGroupPK);
10100                    }
10101    
10102                    newUserGroupPKsSet.removeAll(oldUserGroupPKsSet);
10103    
10104                    long companyId = 0;
10105    
10106                    User user = fetchByPrimaryKey(pk);
10107    
10108                    if (user == null) {
10109                            companyId = companyProvider.getCompanyId();
10110                    }
10111                    else {
10112                            companyId = user.getCompanyId();
10113                    }
10114    
10115                    for (long newUserGroupPK : newUserGroupPKsSet) {
10116                            userToUserGroupTableMapper.addTableMapping(companyId, pk,
10117                                    newUserGroupPK);
10118                    }
10119            }
10120    
10121            /**
10122             * 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.
10123             *
10124             * @param pk the primary key of the user
10125             * @param userGroups the user groups to be associated with the user
10126             */
10127            @Override
10128            public void setUserGroups(long pk,
10129                    List<com.liferay.portal.kernel.model.UserGroup> userGroups) {
10130                    try {
10131                            long[] userGroupPKs = new long[userGroups.size()];
10132    
10133                            for (int i = 0; i < userGroups.size(); i++) {
10134                                    com.liferay.portal.kernel.model.UserGroup userGroup = userGroups.get(i);
10135    
10136                                    userGroupPKs[i] = userGroup.getPrimaryKey();
10137                            }
10138    
10139                            setUserGroups(pk, userGroupPKs);
10140                    }
10141                    catch (Exception e) {
10142                            throw processException(e);
10143                    }
10144            }
10145    
10146            @Override
10147            public Set<String> getBadColumnNames() {
10148                    return _badColumnNames;
10149            }
10150    
10151            @Override
10152            protected Map<String, Integer> getTableColumnsMap() {
10153                    return UserModelImpl.TABLE_COLUMNS_MAP;
10154            }
10155    
10156            /**
10157             * Initializes the user persistence.
10158             */
10159            public void afterPropertiesSet() {
10160                    userToGroupTableMapper = TableMapperFactory.getTableMapper("Users_Groups",
10161                                    "companyId", "userId", "groupId", this, groupPersistence);
10162    
10163                    userToOrganizationTableMapper = TableMapperFactory.getTableMapper("Users_Orgs",
10164                                    "companyId", "userId", "organizationId", this,
10165                                    organizationPersistence);
10166    
10167                    userToRoleTableMapper = TableMapperFactory.getTableMapper("Users_Roles",
10168                                    "companyId", "userId", "roleId", this, rolePersistence);
10169    
10170                    userToTeamTableMapper = TableMapperFactory.getTableMapper("Users_Teams",
10171                                    "companyId", "userId", "teamId", this, teamPersistence);
10172    
10173                    userToUserGroupTableMapper = TableMapperFactory.getTableMapper("Users_UserGroups",
10174                                    "companyId", "userId", "userGroupId", this, userGroupPersistence);
10175            }
10176    
10177            public void destroy() {
10178                    entityCache.removeCache(UserImpl.class.getName());
10179                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
10180                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
10181                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
10182    
10183                    TableMapperFactory.removeTableMapper("Users_Groups");
10184                    TableMapperFactory.removeTableMapper("Users_Orgs");
10185                    TableMapperFactory.removeTableMapper("Users_Roles");
10186                    TableMapperFactory.removeTableMapper("Users_Teams");
10187                    TableMapperFactory.removeTableMapper("Users_UserGroups");
10188            }
10189    
10190            @BeanReference(type = CompanyProviderWrapper.class)
10191            protected CompanyProvider companyProvider;
10192            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
10193            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
10194            @BeanReference(type = GroupPersistence.class)
10195            protected GroupPersistence groupPersistence;
10196            protected TableMapper<User, com.liferay.portal.kernel.model.Group> userToGroupTableMapper;
10197            @BeanReference(type = OrganizationPersistence.class)
10198            protected OrganizationPersistence organizationPersistence;
10199            protected TableMapper<User, com.liferay.portal.kernel.model.Organization> userToOrganizationTableMapper;
10200            @BeanReference(type = RolePersistence.class)
10201            protected RolePersistence rolePersistence;
10202            protected TableMapper<User, com.liferay.portal.kernel.model.Role> userToRoleTableMapper;
10203            @BeanReference(type = TeamPersistence.class)
10204            protected TeamPersistence teamPersistence;
10205            protected TableMapper<User, com.liferay.portal.kernel.model.Team> userToTeamTableMapper;
10206            @BeanReference(type = UserGroupPersistence.class)
10207            protected UserGroupPersistence userGroupPersistence;
10208            protected TableMapper<User, com.liferay.portal.kernel.model.UserGroup> userToUserGroupTableMapper;
10209            private static final String _SQL_SELECT_USER = "SELECT user FROM User user";
10210            private static final String _SQL_SELECT_USER_WHERE_PKS_IN = "SELECT user FROM User user WHERE userId IN (";
10211            private static final String _SQL_SELECT_USER_WHERE = "SELECT user FROM User user WHERE ";
10212            private static final String _SQL_COUNT_USER = "SELECT COUNT(user) FROM User user";
10213            private static final String _SQL_COUNT_USER_WHERE = "SELECT COUNT(user) FROM User user WHERE ";
10214            private static final String _ORDER_BY_ENTITY_ALIAS = "user.";
10215            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No User exists with the primary key ";
10216            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No User exists with the key {";
10217            private static final Log _log = LogFactoryUtil.getLog(UserPersistenceImpl.class);
10218            private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
10219                                    "uuid", "password"
10220                            });
10221            private static final User _nullUser = new UserImpl() {
10222                            @Override
10223                            public Object clone() {
10224                                    return this;
10225                            }
10226    
10227                            @Override
10228                            public CacheModel<User> toCacheModel() {
10229                                    return _nullUserCacheModel;
10230                            }
10231                    };
10232    
10233            private static final CacheModel<User> _nullUserCacheModel = new NullCacheModel();
10234    
10235            private static class NullCacheModel implements CacheModel<User>, MVCCModel {
10236                    @Override
10237                    public long getMvccVersion() {
10238                            return -1;
10239                    }
10240    
10241                    @Override
10242                    public void setMvccVersion(long mvccVersion) {
10243                    }
10244    
10245                    @Override
10246                    public User toEntityModel() {
10247                            return _nullUser;
10248                    }
10249            }
10250    }