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