001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.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                    if (Validator.isNull(getScreenName())) {
107                            throw new IllegalStateException("Screen name cannot be null");
108                    }
109                    else if (Validator.isNull(getEmailAddress())) {
110                            throw new IllegalStateException("Email address cannot be null");
111                    }
112    
113                    StringBundler sb = new StringBundler(5);
114    
115                    String digest1 = DigesterUtil.digestHex(
116                            Digester.MD5, getEmailAddress(), Portal.PORTAL_REALM, password);
117    
118                    sb.append(digest1);
119                    sb.append(StringPool.COMMA);
120    
121                    String digest2 = DigesterUtil.digestHex(
122                            Digester.MD5, getScreenName(), Portal.PORTAL_REALM, password);
123    
124                    sb.append(digest2);
125                    sb.append(StringPool.COMMA);
126    
127                    String digest3 = DigesterUtil.digestHex(
128                            Digester.MD5, String.valueOf(getUserId()), Portal.PORTAL_REALM,
129                            password);
130    
131                    sb.append(digest3);
132    
133                    return sb.toString();
134            }
135    
136            public String getDisplayEmailAddress() {
137                    String emailAddress = super.getEmailAddress();
138    
139                    EmailAddressGenerator emailAddressGenerator =
140                            EmailAddressGeneratorFactory.getInstance();
141    
142                    if (emailAddressGenerator.isFake(emailAddress)) {
143                            emailAddress = StringPool.BLANK;
144                    }
145    
146                    return emailAddress;
147            }
148    
149            public String getDisplayURL(ThemeDisplay themeDisplay)
150                    throws PortalException, SystemException {
151    
152                    return getDisplayURL(
153                            themeDisplay.getPortalURL(), themeDisplay.getPathMain());
154            }
155    
156            public String getDisplayURL(String portalURL, String mainPath)
157                    throws PortalException, SystemException {
158    
159                    if (isDefaultUser()) {
160                            return StringPool.BLANK;
161                    }
162    
163                    Group group = getGroup();
164    
165                    int publicLayoutsPageCount = group.getPublicLayoutsPageCount();
166    
167                    if (publicLayoutsPageCount > 0) {
168                            StringBundler sb = new StringBundler(5);
169    
170                            sb.append(portalURL);
171                            sb.append(mainPath);
172                            sb.append("/my_places/view?groupId=");
173                            sb.append(group.getGroupId());
174                            sb.append("&privateLayout=0");
175    
176                            return sb.toString();
177                    }
178    
179                    return StringPool.BLANK;
180            }
181    
182            public boolean getFemale() throws PortalException, SystemException {
183                    return !getMale();
184            }
185    
186            public String getFullName() {
187                    FullNameGenerator fullNameGenerator =
188                            FullNameGeneratorFactory.getInstance();
189    
190                    return fullNameGenerator.getFullName(
191                            getFirstName(), getMiddleName(), getLastName());
192            }
193    
194            public Group getGroup() throws PortalException, SystemException {
195                    return GroupLocalServiceUtil.getUserGroup(getCompanyId(), getUserId());
196            }
197    
198            public long[] getGroupIds() throws PortalException, SystemException {
199                    List<Group> groups = getGroups();
200    
201                    long[] groupIds = new long[groups.size()];
202    
203                    for (int i = 0; i < groups.size(); i++) {
204                            Group group = groups.get(i);
205    
206                            groupIds[i] = group.getGroupId();
207                    }
208    
209                    return groupIds;
210            }
211    
212            public List<Group> getGroups() throws PortalException, SystemException {
213                    return GroupLocalServiceUtil.getUserGroups(getUserId());
214            }
215    
216            public Locale getLocale() {
217                    return _locale;
218            }
219    
220            public String getLogin() throws PortalException, SystemException {
221                    String login = null;
222    
223                    Company company = CompanyLocalServiceUtil.getCompanyById(
224                            getCompanyId());
225    
226                    if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_EA)) {
227                            login = getEmailAddress();
228                    }
229                    else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
230                            login = getScreenName();
231                    }
232                    else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
233                            login = String.valueOf(getUserId());
234                    }
235    
236                    return login;
237            }
238    
239            public boolean getMale() throws PortalException, SystemException {
240                    return getContact().getMale();
241            }
242    
243            public List<Group> getMyPlaces() throws PortalException, SystemException {
244                    return getMyPlaces(QueryUtil.ALL_POS);
245            }
246    
247            public List<Group> getMyPlaces(int max)
248                    throws PortalException, SystemException {
249    
250                    List<Group> myPlaces = new UniqueList<Group>();
251    
252                    if (isDefaultUser()) {
253                            return myPlaces;
254                    }
255    
256                    int start = QueryUtil.ALL_POS;
257                    int end = QueryUtil.ALL_POS;
258    
259                    if (max != QueryUtil.ALL_POS) {
260                            start = 0;
261                            end = max;
262                    }
263    
264                    LinkedHashMap<String, Object> groupParams =
265                            new LinkedHashMap<String, Object>();
266    
267                    groupParams.put("usersGroups", new Long(getUserId()));
268                    //groupParams.put("pageCount", StringPool.BLANK);
269    
270                    myPlaces.addAll(
271                            GroupLocalServiceUtil.search(
272                                    getCompanyId(), null, null, groupParams, start, end));
273    
274                    LinkedHashMap<String, Object> organizationParams =
275                            new LinkedHashMap<String, Object>();
276    
277                    organizationParams.put("usersOrgs", new Long(getUserId()));
278    
279                    List<Organization> userOrgs = OrganizationLocalServiceUtil.search(
280                            getCompanyId(), OrganizationConstants.ANY_PARENT_ORGANIZATION_ID,
281                            null, null, null, null, organizationParams, start, end);
282    
283                    for (Organization organization : userOrgs) {
284                            myPlaces.add(0, organization.getGroup());
285    
286                            if (!PropsValues.ORGANIZATIONS_MEMBERSHIP_STRICT) {
287                                    for (Organization ancestorOrganization :
288                                                    organization.getAncestors()) {
289    
290                                            myPlaces.add(0, ancestorOrganization.getGroup());
291                                    }
292                            }
293                    }
294    
295                    if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
296                            PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
297    
298                            Group userGroup = getGroup();
299    
300                            myPlaces.add(0, userGroup);
301                    }
302    
303                    if ((max != QueryUtil.ALL_POS) && (myPlaces.size() > max)) {
304                            myPlaces = ListUtil.subList(myPlaces, start, end);
305                    }
306    
307                    return myPlaces;
308            }
309    
310            public long[] getOrganizationIds() throws PortalException, SystemException {
311                    List<Organization> organizations = getOrganizations();
312    
313                    long[] organizationIds = new long[organizations.size()];
314    
315                    for (int i = 0; i < organizations.size(); i++) {
316                            Organization organization = organizations.get(i);
317    
318                            organizationIds[i] = organization.getOrganizationId();
319                    }
320    
321                    return organizationIds;
322            }
323    
324            public List<Organization> getOrganizations()
325                    throws PortalException, SystemException {
326    
327                    return OrganizationLocalServiceUtil.getUserOrganizations(
328                            getUserId());
329            }
330    
331            public boolean getPasswordModified() {
332                    return _passwordModified;
333            }
334    
335            public PasswordPolicy getPasswordPolicy()
336                    throws PortalException, SystemException {
337    
338                    return PasswordPolicyLocalServiceUtil.getPasswordPolicyByUserId(
339                            getUserId());
340            }
341    
342            public String getPasswordUnencrypted() {
343                    return _passwordUnencrypted;
344            }
345    
346            public int getPrivateLayoutsPageCount()
347                    throws PortalException, SystemException {
348    
349                    Group group = getGroup();
350    
351                    return group.getPrivateLayoutsPageCount();
352            }
353    
354            public int getPublicLayoutsPageCount()
355                    throws PortalException, SystemException {
356    
357                    Group group = getGroup();
358    
359                    return group.getPublicLayoutsPageCount();
360            }
361    
362            public Set<String> getReminderQueryQuestions()
363                    throws PortalException, SystemException {
364    
365                    Set<String> questions = new TreeSet<String>();
366    
367                    List<Organization> organizations =
368                            OrganizationLocalServiceUtil.getUserOrganizations(
369                                    getUserId(), true);
370    
371                    for (Organization organization : organizations) {
372                            Set<String> organizationQuestions =
373                                    organization.getReminderQueryQuestions(getLanguageId());
374    
375                            if (organizationQuestions.size() == 0) {
376                                    Organization parentOrganization =
377                                            organization.getParentOrganization();
378    
379                                    while ((organizationQuestions.size() == 0) &&
380                                                    (parentOrganization != null)) {
381    
382                                            organizationQuestions =
383                                                    parentOrganization.getReminderQueryQuestions(
384                                                            getLanguageId());
385    
386                                            parentOrganization =
387                                                    parentOrganization.getParentOrganization();
388                                    }
389                            }
390    
391                            questions.addAll(organizationQuestions);
392                    }
393    
394                    if (questions.size() == 0) {
395                            Set<String> defaultQuestions = SetUtil.fromArray(
396                                    PropsUtil.getArray(PropsKeys.USERS_REMINDER_QUERIES_QUESTIONS));
397    
398                            questions.addAll(defaultQuestions);
399                    }
400    
401                    return questions;
402            }
403    
404            public long[] getRoleIds() throws SystemException {
405                    List<Role> roles = getRoles();
406    
407                    long[] roleIds = new long[roles.size()];
408    
409                    for (int i = 0; i < roles.size(); i++) {
410                            Role role = roles.get(i);
411    
412                            roleIds[i] = role.getRoleId();
413                    }
414    
415                    return roleIds;
416            }
417    
418            public List<Role> getRoles() throws SystemException {
419                    return RoleLocalServiceUtil.getUserRoles(getUserId());
420            }
421    
422            public double getSocialContributionEquity() {
423                    if (_socialContributionEquity == null) {
424                            try {
425                                    SocialEquityValue socialEquityValue =
426                                            SocialEquityUserLocalServiceUtil.getContributionEquity(
427                                                    getUserId());
428    
429                                    _socialContributionEquity = new AtomicReference<Double>(
430                                            socialEquityValue.getValue());
431                            }
432                            catch (SystemException se) {
433                                    return 0;
434                            }
435                    }
436    
437                    return _socialContributionEquity.get();
438            }
439    
440            public double getSocialParticipationEquity() {
441                    if (_socialParticipationEquity == null) {
442                            try {
443                                    SocialEquityValue socialEquityValue =
444                                            SocialEquityUserLocalServiceUtil.getParticipationEquity(
445                                                    getUserId());
446    
447                                    _socialParticipationEquity = new AtomicReference<Double>(
448                                            socialEquityValue.getValue());
449                            }
450                            catch (SystemException se) {
451                                    return 0;
452                            }
453                    }
454    
455                    return _socialParticipationEquity.get();
456            }
457    
458            public double getSocialPersonalEquity() {
459                    return getSocialContributionEquity() + getSocialParticipationEquity();
460            }
461    
462            public long[] getTeamIds() throws SystemException {
463                    List<Team> teams = getTeams();
464    
465                    long[] teamIds = new long[teams.size()];
466    
467                    for (int i = 0; i < teams.size(); i++) {
468                            Team team = teams.get(i);
469    
470                            teamIds[i] = team.getTeamId();
471                    }
472    
473                    return teamIds;
474            }
475    
476            public List<Team> getTeams() throws SystemException {
477                    return TeamLocalServiceUtil.getUserTeams(getUserId());
478            }
479    
480            public long[] getUserGroupIds() throws SystemException {
481                    List<UserGroup> userGroups = getUserGroups();
482    
483                    long[] userGroupIds = new long[userGroups.size()];
484    
485                    for (int i = 0; i < userGroups.size(); i++) {
486                            UserGroup userGroup = userGroups.get(i);
487    
488                            userGroupIds[i] = userGroup.getUserGroupId();
489                    }
490    
491                    return userGroupIds;
492            }
493    
494            public List<UserGroup> getUserGroups() throws SystemException {
495                    return UserGroupLocalServiceUtil.getUserUserGroups(getUserId());
496            }
497    
498            public TimeZone getTimeZone() {
499                    return _timeZone;
500            }
501    
502            public boolean hasCompanyMx() throws PortalException, SystemException {
503                    return hasCompanyMx(getEmailAddress());
504            }
505    
506            public boolean hasCompanyMx(String emailAddress)
507                    throws PortalException, SystemException {
508    
509                    if (Validator.isNull(emailAddress)) {
510                            return false;
511                    }
512    
513                    Company company = CompanyLocalServiceUtil.getCompanyById(
514                            getCompanyId());
515    
516                    return company.hasCompanyMx(emailAddress);
517            }
518    
519            public boolean hasMyPlaces() throws SystemException {
520                    if (isDefaultUser()) {
521                            return false;
522                    }
523    
524                    LinkedHashMap<String, Object> groupParams =
525                            new LinkedHashMap<String, Object>();
526    
527                    groupParams.put("usersGroups", new Long(getUserId()));
528                    //groupParams.put("pageCount", StringPool.BLANK);
529    
530                    int count = GroupLocalServiceUtil.searchCount(
531                            getCompanyId(), null, null, groupParams);
532    
533                    if (count > 0) {
534                            return true;
535                    }
536    
537                    count = OrganizationLocalServiceUtil.getUserOrganizationsCount(
538                            getUserId());
539    
540                    if (count > 0) {
541                            return true;
542                    }
543    
544                    if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
545                            PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
546    
547                            return true;
548                    }
549    
550                    return false;
551            }
552    
553            public boolean hasOrganization() throws PortalException, SystemException {
554                    if (getOrganizations().size() > 0) {
555                            return true;
556                    }
557                    else {
558                            return false;
559                    }
560            }
561    
562            public boolean hasPrivateLayouts() throws PortalException, SystemException {
563                    if (getPrivateLayoutsPageCount() > 0) {
564                            return true;
565                    }
566                    else {
567                            return false;
568                    }
569            }
570    
571            public boolean hasPublicLayouts() throws PortalException, SystemException {
572                    if (getPublicLayoutsPageCount() > 0) {
573                            return true;
574                    }
575                    else {
576                            return false;
577                    }
578            }
579    
580            public boolean hasReminderQuery() {
581                    if (Validator.isNotNull(getReminderQueryQuestion()) &&
582                            Validator.isNotNull(getReminderQueryAnswer())) {
583    
584                            return true;
585                    }
586                    else {
587                            return false;
588                    }
589            }
590    
591            public boolean isFemale() throws PortalException, SystemException {
592                    return getFemale();
593            }
594    
595            public boolean isMale() throws PortalException, SystemException {
596                    return getMale();
597            }
598    
599            public boolean isPasswordModified() {
600                    return _passwordModified;
601            }
602    
603            public void setLanguageId(String languageId) {
604                    _locale = LocaleUtil.fromLanguageId(languageId);
605    
606                    super.setLanguageId(LocaleUtil.toLanguageId(_locale));
607            }
608    
609            public void setPasswordModified(boolean passwordModified) {
610                    _passwordModified = passwordModified;
611            }
612    
613            public void setPasswordUnencrypted(String passwordUnencrypted) {
614                    _passwordUnencrypted = passwordUnencrypted;
615            }
616    
617            public void setTimeZoneId(String timeZoneId) {
618                    if (Validator.isNull(timeZoneId)) {
619                            timeZoneId = TimeZoneUtil.getDefault().getID();
620                    }
621    
622                    _timeZone = TimeZoneUtil.getTimeZone(timeZoneId);
623    
624                    super.setTimeZoneId(timeZoneId);
625            }
626    
627            public void updateSocialContributionEquity(double value) {
628                    if (_socialContributionEquity != null) {
629                            double currentValue = 0;
630                            double newValue = 0;
631    
632                            do {
633                                    currentValue = _socialContributionEquity.get();
634    
635                                    newValue = currentValue + value;
636                            }
637                            while (!_socialContributionEquity.compareAndSet(
638                                                    currentValue, newValue));
639                    }
640            }
641    
642            public void updateSocialParticipationEquity(double value) {
643                    if (_socialParticipationEquity != null) {
644                            double currentValue = 0;
645                            double newValue = 0;
646    
647                            do {
648                                    currentValue = _socialParticipationEquity.get();
649    
650                                    newValue = currentValue + value;
651                            }
652                            while (!_socialParticipationEquity.compareAndSet(
653                                                    currentValue, newValue));
654                    }
655            }
656    
657            private Locale _locale;
658            private boolean _passwordModified;
659            private String _passwordUnencrypted;
660            private AtomicReference<Double> _socialContributionEquity;
661            private AtomicReference<Double> _socialParticipationEquity;
662            private TimeZone _timeZone;
663    
664    }