001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.model.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.Digester;
021    import com.liferay.portal.kernel.util.DigesterUtil;
022    import com.liferay.portal.kernel.util.ListUtil;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.kernel.util.SetUtil;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.TimeZoneUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.model.Company;
031    import com.liferay.portal.model.CompanyConstants;
032    import com.liferay.portal.model.Contact;
033    import com.liferay.portal.model.Group;
034    import com.liferay.portal.model.Organization;
035    import com.liferay.portal.model.OrganizationConstants;
036    import com.liferay.portal.model.PasswordPolicy;
037    import com.liferay.portal.model.Role;
038    import com.liferay.portal.model.Team;
039    import com.liferay.portal.model.User;
040    import com.liferay.portal.model.UserGroup;
041    import com.liferay.portal.security.auth.EmailAddressGenerator;
042    import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
043    import com.liferay.portal.security.auth.FullNameGenerator;
044    import com.liferay.portal.security.auth.FullNameGeneratorFactory;
045    import com.liferay.portal.service.CompanyLocalServiceUtil;
046    import com.liferay.portal.service.ContactLocalServiceUtil;
047    import com.liferay.portal.service.GroupLocalServiceUtil;
048    import com.liferay.portal.service.OrganizationLocalServiceUtil;
049    import com.liferay.portal.service.PasswordPolicyLocalServiceUtil;
050    import com.liferay.portal.service.RoleLocalServiceUtil;
051    import com.liferay.portal.service.TeamLocalServiceUtil;
052    import com.liferay.portal.service.UserGroupLocalServiceUtil;
053    import com.liferay.portal.theme.ThemeDisplay;
054    import com.liferay.portal.util.Portal;
055    import com.liferay.portal.util.PropsUtil;
056    import com.liferay.portal.util.PropsValues;
057    import com.liferay.portlet.social.model.SocialEquityValue;
058    import com.liferay.portlet.social.service.SocialEquityUserLocalServiceUtil;
059    import com.liferay.util.UniqueList;
060    
061    import java.util.Date;
062    import java.util.LinkedHashMap;
063    import java.util.List;
064    import java.util.Locale;
065    import java.util.Set;
066    import java.util.TimeZone;
067    import java.util.TreeSet;
068    import java.util.concurrent.atomic.AtomicReference;
069    
070    /**
071     * @author Brian Wing Shun Chan
072     * @author Jorge Ferrer
073     * @author Wesley Gong
074     */
075    public class UserImpl extends UserModelImpl implements User {
076    
077            public UserImpl() {
078            }
079    
080            public Date getBirthday() throws PortalException, SystemException {
081                    return getContact().getBirthday();
082            }
083    
084            public String getCompanyMx() throws PortalException, SystemException {
085                    Company company = CompanyLocalServiceUtil.getCompanyById(
086                            getCompanyId());
087    
088                    return company.getMx();
089            }
090    
091            public Contact getContact() throws PortalException, SystemException {
092                    return ContactLocalServiceUtil.getContact(getContactId());
093            }
094    
095            public String getDigest() {
096                    String digest = super.getDigest();
097    
098                    if (Validator.isNull(digest) && !isPasswordEncrypted()) {
099                            digest = getDigest(getPassword());
100                    }
101    
102                    return digest;
103            }
104    
105            public String getDigest(String password) {
106                    StringBundler sb = new StringBundler(5);
107    
108                    String digest1 = DigesterUtil.digestHex(
109                            Digester.MD5, getEmailAddress(), Portal.PORTAL_REALM, password);
110    
111                    sb.append(digest1);
112                    sb.append(StringPool.COMMA);
113    
114                    String digest2 = DigesterUtil.digestHex(
115                            Digester.MD5, getScreenName(), Portal.PORTAL_REALM, password);
116    
117                    sb.append(digest2);
118                    sb.append(StringPool.COMMA);
119    
120                    String digest3 = DigesterUtil.digestHex(
121                            Digester.MD5, String.valueOf(getUserId()), Portal.PORTAL_REALM,
122                            password);
123    
124                    sb.append(digest3);
125    
126                    return sb.toString();
127            }
128    
129            public String getDisplayEmailAddress() {
130                    String emailAddress = super.getEmailAddress();
131    
132                    EmailAddressGenerator emailAddressGenerator =
133                            EmailAddressGeneratorFactory.getInstance();
134    
135                    if (emailAddressGenerator.isFake(emailAddress)) {
136                            emailAddress = StringPool.BLANK;
137                    }
138    
139                    return emailAddress;
140            }
141    
142            public String getDisplayURL(ThemeDisplay themeDisplay)
143                    throws PortalException, SystemException {
144    
145                    return getDisplayURL(
146                            themeDisplay.getPortalURL(), themeDisplay.getPathMain());
147            }
148    
149            public String getDisplayURL(String portalURL, String mainPath)
150                    throws PortalException, SystemException {
151    
152                    if (isDefaultUser()) {
153                            return StringPool.BLANK;
154                    }
155    
156                    Group group = getGroup();
157    
158                    int publicLayoutsPageCount = group.getPublicLayoutsPageCount();
159    
160                    if (publicLayoutsPageCount > 0) {
161                            StringBundler sb = new StringBundler(5);
162    
163                            sb.append(portalURL);
164                            sb.append(mainPath);
165                            sb.append("/my_places/view?groupId=");
166                            sb.append(group.getGroupId());
167                            sb.append("&privateLayout=0");
168    
169                            return sb.toString();
170                    }
171    
172                    return StringPool.BLANK;
173            }
174    
175            public boolean getFemale() throws PortalException, SystemException {
176                    return !getMale();
177            }
178    
179            public String getFullName() {
180                    FullNameGenerator fullNameGenerator =
181                            FullNameGeneratorFactory.getInstance();
182    
183                    return fullNameGenerator.getFullName(
184                            getFirstName(), getMiddleName(), getLastName());
185            }
186    
187            public Group getGroup() throws PortalException, SystemException {
188                    return GroupLocalServiceUtil.getUserGroup(getCompanyId(), getUserId());
189            }
190    
191            public long[] getGroupIds() throws PortalException, SystemException {
192                    List<Group> groups = getGroups();
193    
194                    long[] groupIds = new long[groups.size()];
195    
196                    for (int i = 0; i < groups.size(); i++) {
197                            Group group = groups.get(i);
198    
199                            groupIds[i] = group.getGroupId();
200                    }
201    
202                    return groupIds;
203            }
204    
205            public List<Group> getGroups() throws PortalException, SystemException {
206                    return GroupLocalServiceUtil.getUserGroups(getUserId());
207            }
208    
209            public Locale getLocale() {
210                    return _locale;
211            }
212    
213            public String getLogin() throws PortalException, SystemException {
214                    String login = null;
215    
216                    Company company = CompanyLocalServiceUtil.getCompanyById(
217                            getCompanyId());
218    
219                    if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_EA)) {
220                            login = getEmailAddress();
221                    }
222                    else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
223                            login = getScreenName();
224                    }
225                    else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
226                            login = String.valueOf(getUserId());
227                    }
228    
229                    return login;
230            }
231    
232            public boolean getMale() throws PortalException, SystemException {
233                    return getContact().getMale();
234            }
235    
236            public List<Group> getMyPlaces() throws PortalException, SystemException {
237                    return getMyPlaces(QueryUtil.ALL_POS);
238            }
239    
240            public List<Group> getMyPlaces(int max)
241                    throws PortalException, SystemException {
242    
243                    List<Group> myPlaces = new UniqueList<Group>();
244    
245                    if (isDefaultUser()) {
246                            return myPlaces;
247                    }
248    
249                    int start = QueryUtil.ALL_POS;
250                    int end = QueryUtil.ALL_POS;
251    
252                    if (max != QueryUtil.ALL_POS) {
253                            start = 0;
254                            end = max;
255                    }
256    
257                    LinkedHashMap<String, Object> groupParams =
258                            new LinkedHashMap<String, Object>();
259    
260                    groupParams.put("usersGroups", new Long(getUserId()));
261                    //groupParams.put("pageCount", StringPool.BLANK);
262    
263                    myPlaces.addAll(
264                            GroupLocalServiceUtil.search(
265                                    getCompanyId(), null, null, groupParams, start, end));
266    
267                    LinkedHashMap<String, Object> organizationParams =
268                            new LinkedHashMap<String, Object>();
269    
270                    organizationParams.put("usersOrgs", new Long(getUserId()));
271    
272                    List<Organization> userOrgs = OrganizationLocalServiceUtil.search(
273                            getCompanyId(), OrganizationConstants.ANY_PARENT_ORGANIZATION_ID,
274                            null, null, null, null, organizationParams, start, end);
275    
276                    for (Organization organization : userOrgs) {
277                            myPlaces.add(0, organization.getGroup());
278    
279                            if (!PropsValues.ORGANIZATIONS_MEMBERSHIP_STRICT) {
280                                    for (Organization ancestorOrganization :
281                                                    organization.getAncestors()) {
282    
283                                            myPlaces.add(0, ancestorOrganization.getGroup());
284                                    }
285                            }
286                    }
287    
288                    if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
289                            PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
290    
291                            Group userGroup = getGroup();
292    
293                            myPlaces.add(0, userGroup);
294                    }
295    
296                    if ((max != QueryUtil.ALL_POS) && (myPlaces.size() > max)) {
297                            myPlaces = ListUtil.subList(myPlaces, start, end);
298                    }
299    
300                    return myPlaces;
301            }
302    
303            public long[] getOrganizationIds() throws PortalException, SystemException {
304                    List<Organization> organizations = getOrganizations();
305    
306                    long[] organizationIds = new long[organizations.size()];
307    
308                    for (int i = 0; i < organizations.size(); i++) {
309                            Organization organization = organizations.get(i);
310    
311                            organizationIds[i] = organization.getOrganizationId();
312                    }
313    
314                    return organizationIds;
315            }
316    
317            public List<Organization> getOrganizations()
318                    throws PortalException, SystemException {
319    
320                    return OrganizationLocalServiceUtil.getUserOrganizations(
321                            getUserId());
322            }
323    
324            public boolean getPasswordModified() {
325                    return _passwordModified;
326            }
327    
328            public PasswordPolicy getPasswordPolicy()
329                    throws PortalException, SystemException {
330    
331                    return PasswordPolicyLocalServiceUtil.getPasswordPolicyByUserId(
332                            getUserId());
333            }
334    
335            public String getPasswordUnencrypted() {
336                    return _passwordUnencrypted;
337            }
338    
339            public int getPrivateLayoutsPageCount()
340                    throws PortalException, SystemException {
341    
342                    Group group = getGroup();
343    
344                    return group.getPrivateLayoutsPageCount();
345            }
346    
347            public int getPublicLayoutsPageCount()
348                    throws PortalException, SystemException {
349    
350                    Group group = getGroup();
351    
352                    return group.getPublicLayoutsPageCount();
353            }
354    
355            public Set<String> getReminderQueryQuestions()
356                    throws PortalException, SystemException {
357    
358                    Set<String> questions = new TreeSet<String>();
359    
360                    List<Organization> organizations =
361                            OrganizationLocalServiceUtil.getUserOrganizations(
362                                    getUserId(), true);
363    
364                    for (Organization organization : organizations) {
365                            Set<String> organizationQuestions =
366                                    organization.getReminderQueryQuestions(getLanguageId());
367    
368                            if (organizationQuestions.size() == 0) {
369                                    Organization parentOrganization =
370                                            organization.getParentOrganization();
371    
372                                    while ((organizationQuestions.size() == 0) &&
373                                                    (parentOrganization != null)) {
374    
375                                            organizationQuestions =
376                                                    parentOrganization.getReminderQueryQuestions(
377                                                            getLanguageId());
378    
379                                            parentOrganization =
380                                                    parentOrganization.getParentOrganization();
381                                    }
382                            }
383    
384                            questions.addAll(organizationQuestions);
385                    }
386    
387                    if (questions.size() == 0) {
388                            Set<String> defaultQuestions = SetUtil.fromArray(
389                                    PropsUtil.getArray(PropsKeys.USERS_REMINDER_QUERIES_QUESTIONS));
390    
391                            questions.addAll(defaultQuestions);
392                    }
393    
394                    return questions;
395            }
396    
397            public long[] getRoleIds() throws SystemException {
398                    List<Role> roles = getRoles();
399    
400                    long[] roleIds = new long[roles.size()];
401    
402                    for (int i = 0; i < roles.size(); i++) {
403                            Role role = roles.get(i);
404    
405                            roleIds[i] = role.getRoleId();
406                    }
407    
408                    return roleIds;
409            }
410    
411            public List<Role> getRoles() throws SystemException {
412                    return RoleLocalServiceUtil.getUserRoles(getUserId());
413            }
414    
415            public double getSocialContributionEquity() {
416                    if (_socialContributionEquity == null) {
417                            try {
418                                    SocialEquityValue socialEquityValue =
419                                            SocialEquityUserLocalServiceUtil.getContributionEquity(
420                                                    getUserId());
421    
422                                    _socialContributionEquity = new AtomicReference<Double>(
423                                            socialEquityValue.getValue());
424                            }
425                            catch (SystemException se) {
426                                    return 0;
427                            }
428                    }
429    
430                    return _socialContributionEquity.get();
431            }
432    
433            public double getSocialParticipationEquity() {
434                    if (_socialParticipationEquity == null) {
435                            try {
436                                    SocialEquityValue socialEquityValue =
437                                            SocialEquityUserLocalServiceUtil.getParticipationEquity(
438                                                    getUserId());
439    
440                                    _socialParticipationEquity = new AtomicReference<Double>(
441                                            socialEquityValue.getValue());
442                            }
443                            catch (SystemException se) {
444                                    return 0;
445                            }
446                    }
447    
448                    return _socialParticipationEquity.get();
449            }
450    
451            public double getSocialPersonalEquity() {
452                    return getSocialContributionEquity() + getSocialParticipationEquity();
453            }
454    
455            public long[] getTeamIds() throws SystemException {
456                    List<Team> teams = getTeams();
457    
458                    long[] teamIds = new long[teams.size()];
459    
460                    for (int i = 0; i < teams.size(); i++) {
461                            Team team = teams.get(i);
462    
463                            teamIds[i] = team.getTeamId();
464                    }
465    
466                    return teamIds;
467            }
468    
469            public List<Team> getTeams() throws SystemException {
470                    return TeamLocalServiceUtil.getUserTeams(getUserId());
471            }
472    
473            public long[] getUserGroupIds() throws SystemException {
474                    List<UserGroup> userGroups = getUserGroups();
475    
476                    long[] userGroupIds = new long[userGroups.size()];
477    
478                    for (int i = 0; i < userGroups.size(); i++) {
479                            UserGroup userGroup = userGroups.get(i);
480    
481                            userGroupIds[i] = userGroup.getUserGroupId();
482                    }
483    
484                    return userGroupIds;
485            }
486    
487            public List<UserGroup> getUserGroups() throws SystemException {
488                    return UserGroupLocalServiceUtil.getUserUserGroups(getUserId());
489            }
490    
491            public TimeZone getTimeZone() {
492                    return _timeZone;
493            }
494    
495            public boolean hasCompanyMx() throws PortalException, SystemException {
496                    return hasCompanyMx(getEmailAddress());
497            }
498    
499            public boolean hasCompanyMx(String emailAddress)
500                    throws PortalException, SystemException {
501    
502                    if (Validator.isNull(emailAddress)) {
503                            return false;
504                    }
505    
506                    Company company = CompanyLocalServiceUtil.getCompanyById(
507                            getCompanyId());
508    
509                    return company.hasCompanyMx(emailAddress);
510            }
511    
512            public boolean hasMyPlaces() throws SystemException {
513                    if (isDefaultUser()) {
514                            return false;
515                    }
516    
517                    LinkedHashMap<String, Object> groupParams =
518                            new LinkedHashMap<String, Object>();
519    
520                    groupParams.put("usersGroups", new Long(getUserId()));
521                    //groupParams.put("pageCount", StringPool.BLANK);
522    
523                    int count = GroupLocalServiceUtil.searchCount(
524                            getCompanyId(), null, null, groupParams);
525    
526                    if (count > 0) {
527                            return true;
528                    }
529    
530                    count = OrganizationLocalServiceUtil.getUserOrganizationsCount(
531                            getUserId());
532    
533                    if (count > 0) {
534                            return true;
535                    }
536    
537                    if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
538                            PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
539    
540                            return true;
541                    }
542    
543                    return false;
544            }
545    
546            public boolean hasOrganization() throws PortalException, SystemException {
547                    if (getOrganizations().size() > 0) {
548                            return true;
549                    }
550                    else {
551                            return false;
552                    }
553            }
554    
555            public boolean hasPrivateLayouts() throws PortalException, SystemException {
556                    if (getPrivateLayoutsPageCount() > 0) {
557                            return true;
558                    }
559                    else {
560                            return false;
561                    }
562            }
563    
564            public boolean hasPublicLayouts() throws PortalException, SystemException {
565                    if (getPublicLayoutsPageCount() > 0) {
566                            return true;
567                    }
568                    else {
569                            return false;
570                    }
571            }
572    
573            public boolean hasReminderQuery() {
574                    if (Validator.isNotNull(getReminderQueryQuestion()) &&
575                            Validator.isNotNull(getReminderQueryAnswer())) {
576    
577                            return true;
578                    }
579                    else {
580                            return false;
581                    }
582            }
583    
584            public boolean isFemale() throws PortalException, SystemException {
585                    return getFemale();
586            }
587    
588            public boolean isMale() throws PortalException, SystemException {
589                    return getMale();
590            }
591    
592            public boolean isPasswordModified() {
593                    return _passwordModified;
594            }
595    
596            public void setLanguageId(String languageId) {
597                    _locale = LocaleUtil.fromLanguageId(languageId);
598    
599                    super.setLanguageId(LocaleUtil.toLanguageId(_locale));
600            }
601    
602            public void setPasswordModified(boolean passwordModified) {
603                    _passwordModified = passwordModified;
604            }
605    
606            public void setPasswordUnencrypted(String passwordUnencrypted) {
607                    _passwordUnencrypted = passwordUnencrypted;
608            }
609    
610            public void setTimeZoneId(String timeZoneId) {
611                    if (Validator.isNull(timeZoneId)) {
612                            timeZoneId = TimeZoneUtil.getDefault().getID();
613                    }
614    
615                    _timeZone = TimeZoneUtil.getTimeZone(timeZoneId);
616    
617                    super.setTimeZoneId(timeZoneId);
618            }
619    
620            public void updateSocialContributionEquity(double value) {
621                    if (_socialContributionEquity != null) {
622                            double currentValue = 0;
623                            double newValue = 0;
624    
625                            do {
626                                    currentValue = _socialContributionEquity.get();
627    
628                                    newValue = currentValue + value;
629                            }
630                            while (!_socialContributionEquity.compareAndSet(
631                                                    currentValue, newValue));
632                    }
633            }
634    
635            public void updateSocialParticipationEquity(double value) {
636                    if (_socialParticipationEquity != null) {
637                            double currentValue = 0;
638                            double newValue = 0;
639    
640                            do {
641                                    currentValue = _socialParticipationEquity.get();
642    
643                                    newValue = currentValue + value;
644                            }
645                            while (!_socialParticipationEquity.compareAndSet(
646                                                    currentValue, newValue));
647                    }
648            }
649    
650            private Locale _locale;
651            private boolean _passwordModified;
652            private String _passwordUnencrypted;
653            private AtomicReference<Double> _socialContributionEquity;
654            private AtomicReference<Double> _socialParticipationEquity;
655            private TimeZone _timeZone;
656    
657    }