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