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