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