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