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