1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.model.impl;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryUtil;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.exception.SystemException;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.ListUtil;
23  import com.liferay.portal.kernel.util.LocaleUtil;
24  import com.liferay.portal.kernel.util.PropsKeys;
25  import com.liferay.portal.kernel.util.SetUtil;
26  import com.liferay.portal.kernel.util.StringBundler;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.TimeZoneUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Company;
31  import com.liferay.portal.model.CompanyConstants;
32  import com.liferay.portal.model.Contact;
33  import com.liferay.portal.model.ContactConstants;
34  import com.liferay.portal.model.Group;
35  import com.liferay.portal.model.Organization;
36  import com.liferay.portal.model.OrganizationConstants;
37  import com.liferay.portal.model.PasswordPolicy;
38  import com.liferay.portal.model.Role;
39  import com.liferay.portal.model.User;
40  import com.liferay.portal.model.UserGroup;
41  import com.liferay.portal.security.auth.EmailAddressGenerator;
42  import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
43  import com.liferay.portal.service.CompanyLocalServiceUtil;
44  import com.liferay.portal.service.ContactLocalServiceUtil;
45  import com.liferay.portal.service.GroupLocalServiceUtil;
46  import com.liferay.portal.service.OrganizationLocalServiceUtil;
47  import com.liferay.portal.service.PasswordPolicyLocalServiceUtil;
48  import com.liferay.portal.service.RoleLocalServiceUtil;
49  import com.liferay.portal.service.UserGroupLocalServiceUtil;
50  import com.liferay.portal.theme.ThemeDisplay;
51  import com.liferay.portal.util.PropsUtil;
52  import com.liferay.portal.util.PropsValues;
53  import com.liferay.util.UniqueList;
54  
55  import java.util.ArrayList;
56  import java.util.Date;
57  import java.util.LinkedHashMap;
58  import java.util.List;
59  import java.util.Locale;
60  import java.util.Set;
61  import java.util.TimeZone;
62  import java.util.TreeSet;
63  
64  /**
65   * <a href="UserImpl.java.html"><b><i>View Source</i></b></a>
66   *
67   * @author Brian Wing Shun Chan
68   * @author Jorge Ferrer
69   * @author Wesley Gong
70   */
71  public class UserImpl extends UserModelImpl implements User {
72  
73      public UserImpl() {
74      }
75  
76      public Date getBirthday() {
77          return getContact().getBirthday();
78      }
79  
80      public String getCompanyMx() {
81          String companyMx = null;
82  
83          try {
84              Company company = CompanyLocalServiceUtil.getCompanyById(
85                  getCompanyId());
86  
87              companyMx = company.getMx();
88          }
89          catch (Exception e) {
90              _log.error(e, e);
91          }
92  
93          return companyMx;
94      }
95  
96      public Contact getContact() {
97          Contact contact = null;
98  
99          try {
100             contact = ContactLocalServiceUtil.getContact(getContactId());
101         }
102         catch (Exception e) {
103             contact = new ContactImpl();
104 
105             _log.error(e, e);
106         }
107 
108         return contact;
109     }
110 
111     public String getDisplayEmailAddress() {
112         String emailAddress = super.getEmailAddress();
113 
114         EmailAddressGenerator emailAddressGenerator =
115             EmailAddressGeneratorFactory.getInstance();
116 
117         if (emailAddressGenerator.isFake(emailAddress)) {
118             emailAddress = StringPool.BLANK;
119         }
120 
121         return emailAddress;
122     }
123 
124     public String getDisplayURL(ThemeDisplay themeDisplay) {
125         return getDisplayURL(
126             themeDisplay.getPortalURL(), themeDisplay.getPathMain());
127 
128     }
129 
130     public String getDisplayURL(String portalURL, String mainPath) {
131         try {
132             Group group = getGroup();
133 
134             if (group != null) {
135                 int publicLayoutsPageCount = group.getPublicLayoutsPageCount();
136 
137                 if (publicLayoutsPageCount > 0) {
138                     StringBundler sb = new StringBundler(5);
139 
140                     sb.append(portalURL);
141                     sb.append(mainPath);
142                     sb.append("/my_places/view?groupId=");
143                     sb.append(group.getGroupId());
144                     sb.append("&privateLayout=0");
145 
146                     return sb.toString();
147                 }
148             }
149         }
150         catch (Exception e) {
151             _log.error(e, e);
152         }
153 
154         return StringPool.BLANK;
155     }
156 
157     public boolean getFemale() {
158         return !getMale();
159     }
160 
161     public String getFullName() {
162         return ContactConstants.getFullName(
163             getFirstName(), getMiddleName(), getLastName());
164     }
165 
166     public Group getGroup() {
167         Group group = null;
168 
169         try {
170             group = GroupLocalServiceUtil.getUserGroup(
171                 getCompanyId(), getUserId());
172         }
173         catch (Exception e) {
174         }
175 
176         return group;
177     }
178 
179     public long[] getGroupIds() {
180         List<Group> groups = getGroups();
181 
182         long[] groupIds = new long[groups.size()];
183 
184         for (int i = 0; i < groups.size(); i++) {
185             Group group = groups.get(i);
186 
187             groupIds[i] = group.getGroupId();
188         }
189 
190         return groupIds;
191     }
192 
193     public List<Group> getGroups() {
194         try {
195             return GroupLocalServiceUtil.getUserGroups(getUserId());
196         }
197         catch (Exception e) {
198             if (_log.isWarnEnabled()) {
199                 _log.warn("Unable to get groups for user " + getUserId());
200             }
201         }
202 
203         return new ArrayList<Group>();
204     }
205 
206     public Locale getLocale() {
207         return _locale;
208     }
209 
210     public String getLogin() throws PortalException, SystemException {
211         String login = null;
212 
213         Company company = CompanyLocalServiceUtil.getCompanyById(
214             getCompanyId());
215 
216         if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_EA)) {
217             login = getEmailAddress();
218         }
219         else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
220             login = getScreenName();
221         }
222         else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
223             login = String.valueOf(getUserId());
224         }
225 
226         return login;
227     }
228 
229     public boolean getMale() {
230         return getContact().getMale();
231     }
232 
233     public List<Group> getMyPlaces() {
234         return getMyPlaces(QueryUtil.ALL_POS);
235     }
236 
237     public List<Group> getMyPlaces(int max) {
238         List<Group> myPlaces = new UniqueList<Group>();
239 
240         try {
241             if (isDefaultUser()) {
242                 return myPlaces;
243             }
244 
245             int start = QueryUtil.ALL_POS;
246             int end = QueryUtil.ALL_POS;
247 
248             if (max != QueryUtil.ALL_POS) {
249                 start = 0;
250                 end = max;
251             }
252 
253             LinkedHashMap<String, Object> groupParams =
254                 new LinkedHashMap<String, Object>();
255 
256             groupParams.put("usersGroups", new Long(getUserId()));
257             //groupParams.put("pageCount", StringPool.BLANK);
258 
259             myPlaces.addAll(
260                 GroupLocalServiceUtil.search(
261                     getCompanyId(), null, null, groupParams, start, end));
262 
263             LinkedHashMap<String, Object> organizationParams =
264                 new LinkedHashMap<String, Object>();
265 
266             organizationParams.put("usersOrgs", new Long(getUserId()));
267 
268             List<Organization> userOrgs = OrganizationLocalServiceUtil.search(
269                 getCompanyId(),
270                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null, null,
271                 null, null, organizationParams, start, end);
272 
273             for (Organization organization : userOrgs) {
274                 myPlaces.add(0, organization.getGroup());
275 
276                 if (!PropsValues.ORGANIZATIONS_MEMBERSHIP_STRICT) {
277                     for (Organization ancestorOrganization :
278                             organization.getAncestors()) {
279 
280                         myPlaces.add(0, ancestorOrganization.getGroup());
281                     }
282                 }
283             }
284 
285             if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
286                 PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
287 
288                 Group userGroup = getGroup();
289 
290                 myPlaces.add(0, userGroup);
291             }
292 
293             if ((max != QueryUtil.ALL_POS) && (myPlaces.size() > max)) {
294                 myPlaces = ListUtil.subList(myPlaces, start, end);
295             }
296         }
297         catch (Exception e) {
298             if (_log.isWarnEnabled()) {
299                 _log.warn(e, e);
300             }
301         }
302 
303         return myPlaces;
304     }
305 
306     public long[] getOrganizationIds() {
307         List<Organization> organizations = getOrganizations();
308 
309         long[] organizationIds = new long[organizations.size()];
310 
311         for (int i = 0; i < organizations.size(); i++) {
312             Organization organization = organizations.get(i);
313 
314             organizationIds[i] = organization.getOrganizationId();
315         }
316 
317         return organizationIds;
318     }
319 
320     public List<Organization> getOrganizations() {
321         try {
322             return OrganizationLocalServiceUtil.getUserOrganizations(
323                 getUserId());
324         }
325         catch (Exception e) {
326             if (_log.isWarnEnabled()) {
327                 _log.warn(
328                     "Unable to get organizations for user " + getUserId());
329             }
330         }
331 
332         return new ArrayList<Organization>();
333     }
334 
335     public boolean getPasswordModified() {
336         return _passwordModified;
337     }
338 
339     public PasswordPolicy getPasswordPolicy()
340         throws PortalException, SystemException {
341 
342         PasswordPolicy passwordPolicy =
343             PasswordPolicyLocalServiceUtil.getPasswordPolicyByUserId(
344                 getUserId());
345 
346         return passwordPolicy;
347     }
348 
349     public String getPasswordUnencrypted() {
350         return _passwordUnencrypted;
351     }
352 
353     public int getPrivateLayoutsPageCount() {
354         try {
355             Group group = getGroup();
356 
357             if (group == null) {
358                 return 0;
359             }
360             else {
361                 return group.getPrivateLayoutsPageCount();
362             }
363         }
364         catch (Exception e) {
365             _log.error(e, e);
366         }
367 
368         return 0;
369     }
370 
371     public int getPublicLayoutsPageCount() {
372         try {
373             Group group = getGroup();
374 
375             if (group == null) {
376                 return 0;
377             }
378             else {
379                 return group.getPublicLayoutsPageCount();
380             }
381         }
382         catch (Exception e) {
383             _log.error(e, e);
384         }
385 
386         return 0;
387     }
388 
389     public Set<String> getReminderQueryQuestions()
390         throws PortalException, SystemException {
391 
392         Set<String> questions = new TreeSet<String>();
393 
394         List<Organization> organizations =
395             OrganizationLocalServiceUtil.getUserOrganizations(
396                 getUserId(), true);
397 
398         for (Organization organization : organizations) {
399             Set<String> organizationQuestions =
400                 organization.getReminderQueryQuestions(getLanguageId());
401 
402             if (organizationQuestions.size() == 0) {
403                 Organization parentOrganization =
404                     organization.getParentOrganization();
405 
406                 while ((organizationQuestions.size() == 0) &&
407                         (parentOrganization != null)) {
408 
409                     organizationQuestions =
410                         parentOrganization.getReminderQueryQuestions(
411                             getLanguageId());
412 
413                     parentOrganization =
414                         parentOrganization.getParentOrganization();
415                 }
416             }
417 
418             questions.addAll(organizationQuestions);
419         }
420 
421         if (questions.size() == 0) {
422             Set<String> defaultQuestions = SetUtil.fromArray(
423                 PropsUtil.getArray(PropsKeys.USERS_REMINDER_QUERIES_QUESTIONS));
424 
425             questions.addAll(defaultQuestions);
426         }
427 
428         return questions;
429     }
430 
431     public long[] getRoleIds() {
432         List<Role> roles = getRoles();
433 
434         long[] roleIds = new long[roles.size()];
435 
436         for (int i = 0; i < roles.size(); i++) {
437             Role role = roles.get(i);
438 
439             roleIds[i] = role.getRoleId();
440         }
441 
442         return roleIds;
443     }
444 
445     public List<Role> getRoles() {
446         try {
447             return RoleLocalServiceUtil.getUserRoles(getUserId());
448         }
449         catch (Exception e) {
450             if (_log.isWarnEnabled()) {
451                 _log.warn("Unable to get roles for user " + getUserId());
452             }
453         }
454 
455         return new ArrayList<Role>();
456     }
457 
458     public long[] getUserGroupIds() {
459         List<UserGroup> userGroups = getUserGroups();
460 
461         long[] userGroupIds = new long[userGroups.size()];
462 
463         for (int i = 0; i < userGroups.size(); i++) {
464             UserGroup userGroup = userGroups.get(i);
465 
466             userGroupIds[i] = userGroup.getUserGroupId();
467         }
468 
469         return userGroupIds;
470     }
471 
472     public List<UserGroup> getUserGroups() {
473         try {
474             return UserGroupLocalServiceUtil.getUserUserGroups(getUserId());
475         }
476         catch (Exception e) {
477             if (_log.isWarnEnabled()) {
478                 _log.warn("Unable to get user groups for user " + getUserId());
479             }
480         }
481 
482         return new ArrayList<UserGroup>();
483     }
484 
485     public TimeZone getTimeZone() {
486         return _timeZone;
487     }
488 
489     public boolean hasCompanyMx() {
490         return hasCompanyMx(getEmailAddress());
491     }
492 
493     public boolean hasCompanyMx(String emailAddress) {
494         if (Validator.isNull(emailAddress)) {
495             return false;
496         }
497 
498         try {
499             Company company = CompanyLocalServiceUtil.getCompanyById(
500                 getCompanyId());
501 
502             return company.hasCompanyMx(emailAddress);
503         }
504         catch (Exception e) {
505             _log.error(e, e);
506         }
507 
508         return false;
509     }
510 
511     public boolean hasMyPlaces() {
512         try {
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         catch (Exception e) {
544             if (_log.isWarnEnabled()) {
545                 _log.warn(e, e);
546             }
547         }
548 
549         return false;
550     }
551 
552     public boolean hasOrganization() {
553         if (getOrganizations().size() > 0) {
554             return true;
555         }
556         else {
557             return false;
558         }
559     }
560 
561     public boolean hasPrivateLayouts() {
562         if (getPrivateLayoutsPageCount() > 0) {
563             return true;
564         }
565         else {
566             return false;
567         }
568     }
569 
570     public boolean hasPublicLayouts() {
571         if (getPublicLayoutsPageCount() > 0) {
572             return true;
573         }
574         else {
575             return false;
576         }
577     }
578 
579     public boolean hasReminderQuery() {
580         if (Validator.isNotNull(getReminderQueryQuestion()) &&
581             Validator.isNotNull(getReminderQueryAnswer())) {
582 
583             return true;
584         }
585         else {
586             return false;
587         }
588     }
589 
590     public boolean isFemale() {
591         return getFemale();
592     }
593 
594     public boolean isMale() {
595         return getMale();
596     }
597 
598     public boolean isPasswordModified() {
599         return _passwordModified;
600     }
601 
602     public void setLanguageId(String languageId) {
603         _locale = LocaleUtil.fromLanguageId(languageId);
604 
605         super.setLanguageId(LocaleUtil.toLanguageId(_locale));
606     }
607 
608     public void setPasswordModified(boolean passwordModified) {
609         _passwordModified = passwordModified;
610     }
611 
612     public void setPasswordUnencrypted(String passwordUnencrypted) {
613         _passwordUnencrypted = passwordUnencrypted;
614     }
615 
616     public void setTimeZoneId(String timeZoneId) {
617         if (Validator.isNull(timeZoneId)) {
618             timeZoneId = TimeZoneUtil.getDefault().getID();
619         }
620 
621         _timeZone = TimeZone.getTimeZone(timeZoneId);
622 
623         super.setTimeZoneId(timeZoneId);
624     }
625 
626     private static Log _log = LogFactoryUtil.getLog(UserImpl.class);
627 
628     private Locale _locale;
629     private boolean _passwordModified;
630     private String _passwordUnencrypted;
631     private TimeZone _timeZone;
632 
633 }