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.Group;
40  import com.liferay.portal.model.Organization;
41  import com.liferay.portal.model.PasswordPolicy;
42  import com.liferay.portal.model.Role;
43  import com.liferay.portal.model.User;
44  import com.liferay.portal.model.UserGroup;
45  import com.liferay.portal.service.CompanyLocalServiceUtil;
46  import com.liferay.portal.service.ContactLocalServiceUtil;
47  import com.liferay.portal.service.GroupLocalServiceUtil;
48  import com.liferay.portal.service.OrganizationLocalServiceUtil;
49  import com.liferay.portal.service.PasswordPolicyLocalServiceUtil;
50  import com.liferay.portal.service.RoleLocalServiceUtil;
51  import com.liferay.portal.service.UserGroupLocalServiceUtil;
52  import com.liferay.portal.theme.ThemeDisplay;
53  import com.liferay.portal.util.PropsKeys;
54  import com.liferay.portal.util.PropsUtil;
55  import com.liferay.portal.util.PropsValues;
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.addAll(
255                 GroupLocalServiceUtil.search(
256                     getCompanyId(), null, null, groupParams, start, end));
257 
258             List<Organization> userOrgs =
259                 OrganizationLocalServiceUtil.getUserOrganizations(
260                     getUserId(), start, end);
261 
262             for (Organization organization : userOrgs) {
263                 myPlaces.add(0, organization.getGroup());
264 
265                 if (!PropsValues.ORGANIZATIONS_MEMBERSHIP_STRICT) {
266                     for (Organization ancestorOrganization :
267                             organization.getAncestors()) {
268 
269                         myPlaces.add(0, ancestorOrganization.getGroup());
270                     }
271                 }
272             }
273 
274             if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
275                 PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
276 
277                 Group userGroup = getGroup();
278 
279                 myPlaces.add(0, userGroup);
280             }
281 
282             if ((max != QueryUtil.ALL_POS) && (myPlaces.size() > max)) {
283                 myPlaces = ListUtil.subList(myPlaces, start, end);
284             }
285         }
286         catch (Exception e) {
287             if (_log.isWarnEnabled()) {
288                 _log.warn(e, e);
289             }
290         }
291 
292         return myPlaces;
293     }
294 
295     public long[] getOrganizationIds() {
296         List<Organization> organizations = getOrganizations();
297 
298         long[] organizationIds = new long[organizations.size()];
299 
300         for (int i = 0; i < organizations.size(); i++) {
301             Organization organization = organizations.get(i);
302 
303             organizationIds[i] = organization.getOrganizationId();
304         }
305 
306         return organizationIds;
307     }
308 
309     public List<Organization> getOrganizations() {
310         try {
311             return OrganizationLocalServiceUtil.getUserOrganizations(
312                 getUserId());
313         }
314         catch (Exception e) {
315             if (_log.isWarnEnabled()) {
316                 _log.warn(
317                     "Unable to get organizations for user " + getUserId());
318             }
319         }
320 
321         return new ArrayList<Organization>();
322     }
323 
324     public boolean getPasswordModified() {
325         return _passwordModified;
326     }
327 
328     public PasswordPolicy getPasswordPolicy()
329         throws PortalException, SystemException {
330 
331         PasswordPolicy passwordPolicy =
332             PasswordPolicyLocalServiceUtil.getPasswordPolicyByUserId(
333                 getUserId());
334 
335         return passwordPolicy;
336     }
337 
338     public String getPasswordUnencrypted() {
339         return _passwordUnencrypted;
340     }
341 
342     public int getPrivateLayoutsPageCount() {
343         try {
344             Group group = getGroup();
345 
346             if (group == null) {
347                 return 0;
348             }
349             else {
350                 return group.getPrivateLayoutsPageCount();
351             }
352         }
353         catch (Exception e) {
354             _log.error(e, e);
355         }
356 
357         return 0;
358     }
359 
360     public int getPublicLayoutsPageCount() {
361         try {
362             Group group = getGroup();
363 
364             if (group == null) {
365                 return 0;
366             }
367             else {
368                 return group.getPublicLayoutsPageCount();
369             }
370         }
371         catch (Exception e) {
372             _log.error(e, e);
373         }
374 
375         return 0;
376     }
377 
378     public Set<String> getReminderQueryQuestions()
379         throws PortalException, SystemException {
380 
381         Set<String> questions = new TreeSet<String>();
382 
383         List<Organization> organizations = getOrganizations();
384 
385         for (Organization organization : organizations) {
386             Set<String> organizationQuestions =
387                 organization.getReminderQueryQuestions(getLanguageId());
388 
389             if (organizationQuestions.size() == 0) {
390                 Organization parentOrganization =
391                     organization.getParentOrganization();
392 
393                 while ((organizationQuestions.size() == 0) &&
394                         (parentOrganization != null)) {
395 
396                     organizationQuestions =
397                         parentOrganization.getReminderQueryQuestions(
398                             getLanguageId());
399 
400                     parentOrganization =
401                         parentOrganization.getParentOrganization();
402                 }
403             }
404 
405             questions.addAll(organizationQuestions);
406         }
407 
408         if (questions.size() == 0) {
409             Set<String> defaultQuestions = SetUtil.fromArray(
410                 PropsUtil.getArray(PropsKeys.USERS_REMINDER_QUERIES_QUESTIONS));
411 
412             questions.addAll(defaultQuestions);
413         }
414 
415         return questions;
416     }
417 
418     public long[] getRoleIds() {
419         List<Role> roles = getRoles();
420 
421         long[] roleIds = new long[roles.size()];
422 
423         for (int i = 0; i < roles.size(); i++) {
424             Role role = roles.get(i);
425 
426             roleIds[i] = role.getRoleId();
427         }
428 
429         return roleIds;
430     }
431 
432     public List<Role> getRoles() {
433         try {
434             return RoleLocalServiceUtil.getUserRoles(getUserId());
435         }
436         catch (Exception e) {
437             if (_log.isWarnEnabled()) {
438                 _log.warn("Unable to get roles for user " + getUserId());
439             }
440         }
441 
442         return new ArrayList<Role>();
443     }
444 
445     public long[] getUserGroupIds() {
446         List<UserGroup> userGroups = getUserGroups();
447 
448         long[] userGroupIds = new long[userGroups.size()];
449 
450         for (int i = 0; i < userGroups.size(); i++) {
451             UserGroup userGroup = userGroups.get(i);
452 
453             userGroupIds[i] = userGroup.getUserGroupId();
454         }
455 
456         return userGroupIds;
457     }
458 
459     public List<UserGroup> getUserGroups() {
460         try {
461             return UserGroupLocalServiceUtil.getUserUserGroups(getUserId());
462         }
463         catch (Exception e) {
464             if (_log.isWarnEnabled()) {
465                 _log.warn("Unable to get user groups for user " + getUserId());
466             }
467         }
468 
469         return new ArrayList<UserGroup>();
470     }
471 
472     public TimeZone getTimeZone() {
473         return _timeZone;
474     }
475 
476     public boolean hasCompanyMx() {
477         return hasCompanyMx(getEmailAddress());
478     }
479 
480     public boolean hasCompanyMx(String emailAddress) {
481         if (Validator.isNull(emailAddress)) {
482             return false;
483         }
484 
485         try {
486             Company company = CompanyLocalServiceUtil.getCompanyById(
487                 getCompanyId());
488 
489             return company.hasCompanyMx(emailAddress);
490         }
491         catch (Exception e) {
492             _log.error(e, e);
493         }
494 
495         return false;
496     }
497 
498     public boolean hasMyPlaces() {
499         try {
500             if (isDefaultUser()) {
501                 return false;
502             }
503 
504             LinkedHashMap<String, Object> groupParams =
505                 new LinkedHashMap<String, Object>();
506 
507             groupParams.put("usersGroups", new Long(getUserId()));
508             //groupParams.put("pageCount", StringPool.BLANK);
509 
510             int count = GroupLocalServiceUtil.searchCount(
511                 getCompanyId(), null, null, groupParams);
512 
513             if (count > 0) {
514                 return true;
515             }
516 
517             count = OrganizationLocalServiceUtil.getUserOrganizationsCount(
518                 getUserId());
519 
520             if (count > 0) {
521                 return true;
522             }
523 
524             if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
525                 PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
526 
527                 return true;
528             }
529         }
530         catch (Exception e) {
531             if (_log.isWarnEnabled()) {
532                 _log.warn(e, e);
533             }
534         }
535 
536         return false;
537     }
538 
539     public boolean hasOrganization() {
540         if (getOrganizations().size() > 0) {
541             return true;
542         }
543         else {
544             return false;
545         }
546     }
547 
548     public boolean hasPrivateLayouts() {
549         if (getPrivateLayoutsPageCount() > 0) {
550             return true;
551         }
552         else {
553             return false;
554         }
555     }
556 
557     public boolean hasPublicLayouts() {
558         if (getPublicLayoutsPageCount() > 0) {
559             return true;
560         }
561         else {
562             return false;
563         }
564     }
565 
566     public boolean isFemale() {
567         return getFemale();
568     }
569 
570     public boolean isMale() {
571         return getMale();
572     }
573 
574     public boolean isPasswordModified() {
575         return _passwordModified;
576     }
577 
578     public void setLanguageId(String languageId) {
579         _locale = LocaleUtil.fromLanguageId(languageId);
580 
581         super.setLanguageId(LocaleUtil.toLanguageId(_locale));
582     }
583 
584     public void setPasswordModified(boolean passwordModified) {
585         _passwordModified = passwordModified;
586     }
587 
588     public void setPasswordUnencrypted(String passwordUnencrypted) {
589         _passwordUnencrypted = passwordUnencrypted;
590     }
591 
592     public void setTimeZoneId(String timeZoneId) {
593         if (Validator.isNull(timeZoneId)) {
594             timeZoneId = TimeZoneUtil.getDefault().getID();
595         }
596 
597         _timeZone = TimeZone.getTimeZone(timeZoneId);
598 
599         super.setTimeZoneId(timeZoneId);
600     }
601 
602     private static Log _log = LogFactoryUtil.getLog(UserImpl.class);
603 
604     private boolean _passwordModified;
605     private String _passwordUnencrypted;
606     private Locale _locale;
607     private TimeZone _timeZone;
608 
609 }