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