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