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.util.ListUtil;
21  import com.liferay.portal.kernel.util.LocaleUtil;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.kernel.util.SetUtil;
24  import com.liferay.portal.kernel.util.StringBundler;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.TimeZoneUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Company;
29  import com.liferay.portal.model.CompanyConstants;
30  import com.liferay.portal.model.Contact;
31  import com.liferay.portal.model.Group;
32  import com.liferay.portal.model.Organization;
33  import com.liferay.portal.model.OrganizationConstants;
34  import com.liferay.portal.model.PasswordPolicy;
35  import com.liferay.portal.model.Role;
36  import com.liferay.portal.model.Team;
37  import com.liferay.portal.model.User;
38  import com.liferay.portal.model.UserGroup;
39  import com.liferay.portal.security.auth.EmailAddressGenerator;
40  import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
41  import com.liferay.portal.security.auth.FullNameGenerator;
42  import com.liferay.portal.security.auth.FullNameGeneratorFactory;
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.TeamLocalServiceUtil;
50  import com.liferay.portal.service.UserGroupLocalServiceUtil;
51  import com.liferay.portal.theme.ThemeDisplay;
52  import com.liferay.portal.util.PropsUtil;
53  import com.liferay.portal.util.PropsValues;
54  import com.liferay.util.UniqueList;
55  
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() throws PortalException, SystemException {
77          return getContact().getBirthday();
78      }
79  
80      public String getCompanyMx() throws PortalException, SystemException {
81          Company company = CompanyLocalServiceUtil.getCompanyById(
82              getCompanyId());
83  
84          return company.getMx();
85      }
86  
87      public Contact getContact() throws PortalException, SystemException {
88          return ContactLocalServiceUtil.getContact(getContactId());
89      }
90  
91      public String getDisplayEmailAddress() {
92          String emailAddress = super.getEmailAddress();
93  
94          EmailAddressGenerator emailAddressGenerator =
95              EmailAddressGeneratorFactory.getInstance();
96  
97          if (emailAddressGenerator.isFake(emailAddress)) {
98              emailAddress = StringPool.BLANK;
99          }
100 
101         return emailAddress;
102     }
103 
104     public String getDisplayURL(ThemeDisplay themeDisplay)
105         throws PortalException, SystemException {
106 
107         return getDisplayURL(
108             themeDisplay.getPortalURL(), themeDisplay.getPathMain());
109     }
110 
111     public String getDisplayURL(String portalURL, String mainPath)
112         throws PortalException, SystemException {
113 
114         if (isDefaultUser()) {
115             return StringPool.BLANK;
116         }
117 
118         Group group = getGroup();
119 
120         int publicLayoutsPageCount = group.getPublicLayoutsPageCount();
121 
122         if (publicLayoutsPageCount > 0) {
123             StringBundler sb = new StringBundler(5);
124 
125             sb.append(portalURL);
126             sb.append(mainPath);
127             sb.append("/my_places/view?groupId=");
128             sb.append(group.getGroupId());
129             sb.append("&privateLayout=0");
130 
131             return sb.toString();
132         }
133 
134         return StringPool.BLANK;
135     }
136 
137     public boolean getFemale() throws PortalException, SystemException {
138         return !getMale();
139     }
140 
141     public String getFullName() {
142         FullNameGenerator fullNameGenerator =
143             FullNameGeneratorFactory.getInstance();
144 
145         return fullNameGenerator.getFullName(
146             getFirstName(), getMiddleName(), getLastName());
147     }
148 
149     public Group getGroup() throws PortalException, SystemException {
150         return GroupLocalServiceUtil.getUserGroup(getCompanyId(), getUserId());
151     }
152 
153     public long[] getGroupIds() throws PortalException, SystemException {
154         List<Group> groups = getGroups();
155 
156         long[] groupIds = new long[groups.size()];
157 
158         for (int i = 0; i < groups.size(); i++) {
159             Group group = groups.get(i);
160 
161             groupIds[i] = group.getGroupId();
162         }
163 
164         return groupIds;
165     }
166 
167     public List<Group> getGroups() throws PortalException, SystemException {
168         return GroupLocalServiceUtil.getUserGroups(getUserId());
169     }
170 
171     public Locale getLocale() {
172         return _locale;
173     }
174 
175     public String getLogin() throws PortalException, SystemException {
176         String login = null;
177 
178         Company company = CompanyLocalServiceUtil.getCompanyById(
179             getCompanyId());
180 
181         if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_EA)) {
182             login = getEmailAddress();
183         }
184         else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
185             login = getScreenName();
186         }
187         else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
188             login = String.valueOf(getUserId());
189         }
190 
191         return login;
192     }
193 
194     public boolean getMale() throws PortalException, SystemException {
195         return getContact().getMale();
196     }
197 
198     public List<Group> getMyPlaces() throws PortalException, SystemException {
199         return getMyPlaces(QueryUtil.ALL_POS);
200     }
201 
202     public List<Group> getMyPlaces(int max)
203         throws PortalException, SystemException {
204 
205         List<Group> myPlaces = new UniqueList<Group>();
206 
207         if (isDefaultUser()) {
208             return myPlaces;
209         }
210 
211         int start = QueryUtil.ALL_POS;
212         int end = QueryUtil.ALL_POS;
213 
214         if (max != QueryUtil.ALL_POS) {
215             start = 0;
216             end = max;
217         }
218 
219         LinkedHashMap<String, Object> groupParams =
220             new LinkedHashMap<String, Object>();
221 
222         groupParams.put("usersGroups", new Long(getUserId()));
223         //groupParams.put("pageCount", StringPool.BLANK);
224 
225         myPlaces.addAll(
226             GroupLocalServiceUtil.search(
227                 getCompanyId(), null, null, groupParams, start, end));
228 
229         LinkedHashMap<String, Object> organizationParams =
230             new LinkedHashMap<String, Object>();
231 
232         organizationParams.put("usersOrgs", new Long(getUserId()));
233 
234         List<Organization> userOrgs = OrganizationLocalServiceUtil.search(
235             getCompanyId(), OrganizationConstants.ANY_PARENT_ORGANIZATION_ID,
236             null, null, null, null, organizationParams, start, end);
237 
238         for (Organization organization : userOrgs) {
239             myPlaces.add(0, organization.getGroup());
240 
241             if (!PropsValues.ORGANIZATIONS_MEMBERSHIP_STRICT) {
242                 for (Organization ancestorOrganization :
243                         organization.getAncestors()) {
244 
245                     myPlaces.add(0, ancestorOrganization.getGroup());
246                 }
247             }
248         }
249 
250         if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
251             PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
252 
253             Group userGroup = getGroup();
254 
255             myPlaces.add(0, userGroup);
256         }
257 
258         if ((max != QueryUtil.ALL_POS) && (myPlaces.size() > max)) {
259             myPlaces = ListUtil.subList(myPlaces, start, end);
260         }
261 
262         return myPlaces;
263     }
264 
265     public long[] getOrganizationIds() throws PortalException, SystemException {
266         List<Organization> organizations = getOrganizations();
267 
268         long[] organizationIds = new long[organizations.size()];
269 
270         for (int i = 0; i < organizations.size(); i++) {
271             Organization organization = organizations.get(i);
272 
273             organizationIds[i] = organization.getOrganizationId();
274         }
275 
276         return organizationIds;
277     }
278 
279     public List<Organization> getOrganizations()
280         throws PortalException, SystemException {
281 
282         return OrganizationLocalServiceUtil.getUserOrganizations(
283             getUserId());
284     }
285 
286     public boolean getPasswordModified() {
287         return _passwordModified;
288     }
289 
290     public PasswordPolicy getPasswordPolicy()
291         throws PortalException, SystemException {
292 
293         return PasswordPolicyLocalServiceUtil.getPasswordPolicyByUserId(
294             getUserId());
295     }
296 
297     public String getPasswordUnencrypted() {
298         return _passwordUnencrypted;
299     }
300 
301     public int getPrivateLayoutsPageCount()
302         throws PortalException, SystemException {
303 
304         Group group = getGroup();
305 
306         return group.getPrivateLayoutsPageCount();
307     }
308 
309     public int getPublicLayoutsPageCount()
310         throws PortalException, SystemException {
311 
312         Group group = getGroup();
313 
314         return group.getPublicLayoutsPageCount();
315     }
316 
317     public Set<String> getReminderQueryQuestions()
318         throws PortalException, SystemException {
319 
320         Set<String> questions = new TreeSet<String>();
321 
322         List<Organization> organizations =
323             OrganizationLocalServiceUtil.getUserOrganizations(
324                 getUserId(), true);
325 
326         for (Organization organization : organizations) {
327             Set<String> organizationQuestions =
328                 organization.getReminderQueryQuestions(getLanguageId());
329 
330             if (organizationQuestions.size() == 0) {
331                 Organization parentOrganization =
332                     organization.getParentOrganization();
333 
334                 while ((organizationQuestions.size() == 0) &&
335                         (parentOrganization != null)) {
336 
337                     organizationQuestions =
338                         parentOrganization.getReminderQueryQuestions(
339                             getLanguageId());
340 
341                     parentOrganization =
342                         parentOrganization.getParentOrganization();
343                 }
344             }
345 
346             questions.addAll(organizationQuestions);
347         }
348 
349         if (questions.size() == 0) {
350             Set<String> defaultQuestions = SetUtil.fromArray(
351                 PropsUtil.getArray(PropsKeys.USERS_REMINDER_QUERIES_QUESTIONS));
352 
353             questions.addAll(defaultQuestions);
354         }
355 
356         return questions;
357     }
358 
359     public long[] getRoleIds() throws SystemException {
360         List<Role> roles = getRoles();
361 
362         long[] roleIds = new long[roles.size()];
363 
364         for (int i = 0; i < roles.size(); i++) {
365             Role role = roles.get(i);
366 
367             roleIds[i] = role.getRoleId();
368         }
369 
370         return roleIds;
371     }
372 
373     public List<Role> getRoles() throws SystemException {
374         return RoleLocalServiceUtil.getUserRoles(getUserId());
375     }
376 
377     public long[] getTeamIds() throws SystemException {
378         List<Team> teams = getTeams();
379 
380         long[] teamIds = new long[teams.size()];
381 
382         for (int i = 0; i < teams.size(); i++) {
383             Team team = teams.get(i);
384 
385             teamIds[i] = team.getTeamId();
386         }
387 
388         return teamIds;
389     }
390 
391     public List<Team> getTeams() throws SystemException {
392         return TeamLocalServiceUtil.getUserTeams(getUserId());
393     }
394 
395     public long[] getUserGroupIds() throws SystemException {
396         List<UserGroup> userGroups = getUserGroups();
397 
398         long[] userGroupIds = new long[userGroups.size()];
399 
400         for (int i = 0; i < userGroups.size(); i++) {
401             UserGroup userGroup = userGroups.get(i);
402 
403             userGroupIds[i] = userGroup.getUserGroupId();
404         }
405 
406         return userGroupIds;
407     }
408 
409     public List<UserGroup> getUserGroups() throws SystemException {
410         return UserGroupLocalServiceUtil.getUserUserGroups(getUserId());
411     }
412 
413     public TimeZone getTimeZone() {
414         return _timeZone;
415     }
416 
417     public boolean hasCompanyMx() throws PortalException, SystemException {
418         return hasCompanyMx(getEmailAddress());
419     }
420 
421     public boolean hasCompanyMx(String emailAddress)
422         throws PortalException, SystemException {
423 
424         if (Validator.isNull(emailAddress)) {
425             return false;
426         }
427 
428         Company company = CompanyLocalServiceUtil.getCompanyById(
429             getCompanyId());
430 
431         return company.hasCompanyMx(emailAddress);
432     }
433 
434     public boolean hasMyPlaces() throws SystemException {
435         if (isDefaultUser()) {
436             return false;
437         }
438 
439         LinkedHashMap<String, Object> groupParams =
440             new LinkedHashMap<String, Object>();
441 
442         groupParams.put("usersGroups", new Long(getUserId()));
443         //groupParams.put("pageCount", StringPool.BLANK);
444 
445         int count = GroupLocalServiceUtil.searchCount(
446             getCompanyId(), null, null, groupParams);
447 
448         if (count > 0) {
449             return true;
450         }
451 
452         count = OrganizationLocalServiceUtil.getUserOrganizationsCount(
453             getUserId());
454 
455         if (count > 0) {
456             return true;
457         }
458 
459         if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
460             PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
461 
462             return true;
463         }
464 
465         return false;
466     }
467 
468     public boolean hasOrganization() throws PortalException, SystemException {
469         if (getOrganizations().size() > 0) {
470             return true;
471         }
472         else {
473             return false;
474         }
475     }
476 
477     public boolean hasPrivateLayouts() throws PortalException, SystemException {
478         if (getPrivateLayoutsPageCount() > 0) {
479             return true;
480         }
481         else {
482             return false;
483         }
484     }
485 
486     public boolean hasPublicLayouts() throws PortalException, SystemException {
487         if (getPublicLayoutsPageCount() > 0) {
488             return true;
489         }
490         else {
491             return false;
492         }
493     }
494 
495     public boolean hasReminderQuery() {
496         if (Validator.isNotNull(getReminderQueryQuestion()) &&
497             Validator.isNotNull(getReminderQueryAnswer())) {
498 
499             return true;
500         }
501         else {
502             return false;
503         }
504     }
505 
506     public boolean isFemale() throws PortalException, SystemException {
507         return getFemale();
508     }
509 
510     public boolean isMale() throws PortalException, SystemException {
511         return getMale();
512     }
513 
514     public boolean isPasswordModified() {
515         return _passwordModified;
516     }
517 
518     public void setLanguageId(String languageId) {
519         _locale = LocaleUtil.fromLanguageId(languageId);
520 
521         super.setLanguageId(LocaleUtil.toLanguageId(_locale));
522     }
523 
524     public void setPasswordModified(boolean passwordModified) {
525         _passwordModified = passwordModified;
526     }
527 
528     public void setPasswordUnencrypted(String passwordUnencrypted) {
529         _passwordUnencrypted = passwordUnencrypted;
530     }
531 
532     public void setTimeZoneId(String timeZoneId) {
533         if (Validator.isNull(timeZoneId)) {
534             timeZoneId = TimeZoneUtil.getDefault().getID();
535         }
536 
537         _timeZone = TimeZoneUtil.getTimeZone(timeZoneId);
538 
539         super.setTimeZoneId(timeZoneId);
540     }
541 
542     private Locale _locale;
543     private boolean _passwordModified;
544     private String _passwordUnencrypted;
545     private TimeZone _timeZone;
546 
547 }