1   /**
2    * Copyright (c) 2000-2008 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.util.LocaleUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.TimeZoneUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Company;
32  import com.liferay.portal.model.CompanyConstants;
33  import com.liferay.portal.model.Contact;
34  import com.liferay.portal.model.Group;
35  import com.liferay.portal.model.Organization;
36  import com.liferay.portal.model.OrganizationConstants;
37  import com.liferay.portal.model.PasswordPolicy;
38  import com.liferay.portal.model.User;
39  import com.liferay.portal.service.CompanyLocalServiceUtil;
40  import com.liferay.portal.service.ContactLocalServiceUtil;
41  import com.liferay.portal.service.GroupLocalServiceUtil;
42  import com.liferay.portal.service.OrganizationLocalServiceUtil;
43  import com.liferay.portal.service.PasswordPolicyLocalServiceUtil;
44  import com.liferay.portal.service.RoleLocalServiceUtil;
45  import com.liferay.portal.util.PortalUtil;
46  import com.liferay.portal.util.comparator.OrganizationNameComparator;
47  import com.liferay.util.dao.hibernate.QueryUtil;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.Date;
52  import java.util.LinkedHashMap;
53  import java.util.List;
54  import java.util.Locale;
55  import java.util.TimeZone;
56  
57  import org.apache.commons.logging.Log;
58  import org.apache.commons.logging.LogFactory;
59  
60  /**
61   * <a href="UserImpl.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   *
65   */
66  public class UserImpl extends UserModelImpl implements User {
67  
68      public static String getFullName(
69          String firstName, String middleName, String lastName) {
70  
71          return ContactImpl.getFullName(firstName, middleName, lastName);
72      }
73  
74      public UserImpl() {
75      }
76  
77      public String getCompanyMx() {
78          String companyMx = null;
79  
80          try {
81              Company company = CompanyLocalServiceUtil.getCompanyById(
82                  getCompanyId());
83  
84              companyMx = company.getMx();
85          }
86          catch (Exception e) {
87              _log.error(e);
88          }
89  
90          return companyMx;
91      }
92  
93      public boolean hasCompanyMx() {
94          return hasCompanyMx(getEmailAddress());
95      }
96  
97      public boolean hasCompanyMx(String emailAddress) {
98          try {
99              Company company = CompanyLocalServiceUtil.getCompanyById(
100                 getCompanyId());
101 
102             return company.hasCompanyMx(emailAddress);
103         }
104         catch (Exception e) {
105             _log.error(e);
106         }
107 
108         return false;
109     }
110 
111     public String getLogin() throws PortalException, SystemException {
112         String login = null;
113 
114         Company company = CompanyLocalServiceUtil.getCompanyById(
115             getCompanyId());
116 
117         if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_EA)) {
118             login = getEmailAddress();
119         }
120         else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
121             login = getScreenName();
122         }
123         else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
124             login = String.valueOf(getUserId());
125         }
126 
127         return login;
128     }
129 
130     public PasswordPolicy getPasswordPolicy()
131         throws PortalException, SystemException {
132 
133         PasswordPolicy passwordPolicy =
134             PasswordPolicyLocalServiceUtil.getPasswordPolicyByUserId(
135                 getUserId());
136 
137         return passwordPolicy;
138     }
139 
140     public String getPasswordUnencrypted() {
141         return _passwordUnencrypted;
142     }
143 
144     public void setPasswordUnencrypted(String passwordUnencrypted) {
145         _passwordUnencrypted = passwordUnencrypted;
146     }
147 
148     public boolean getPasswordModified() {
149         return _passwordModified;
150     }
151 
152     public boolean isPasswordModified() {
153         return _passwordModified;
154     }
155 
156     public void setPasswordModified(boolean passwordModified) {
157         _passwordModified = passwordModified;
158     }
159 
160     public Locale getLocale() {
161         return _locale;
162     }
163 
164     public void setLanguageId(String languageId) {
165         _locale = LocaleUtil.fromLanguageId(languageId);
166 
167         super.setLanguageId(LocaleUtil.toLanguageId(_locale));
168     }
169 
170     public TimeZone getTimeZone() {
171         return _timeZone;
172     }
173 
174     public void setTimeZoneId(String timeZoneId) {
175         if (Validator.isNull(timeZoneId)) {
176             timeZoneId = TimeZoneUtil.getDefault().getID();
177         }
178 
179         _timeZone = TimeZone.getTimeZone(timeZoneId);
180 
181         super.setTimeZoneId(timeZoneId);
182     }
183 
184     public Contact getContact() {
185         Contact contact = null;
186 
187         try {
188             contact = ContactLocalServiceUtil.getContact(getContactId());
189         }
190         catch (Exception e) {
191             contact = new ContactImpl();
192 
193             _log.error(e);
194         }
195 
196         return contact;
197     }
198 
199     public String getFirstName() {
200         return getContact().getFirstName();
201     }
202 
203     public String getMiddleName() {
204         return getContact().getMiddleName();
205     }
206 
207     public String getLastName() {
208         return getContact().getLastName();
209     }
210 
211     public String getFullName() {
212         return getContact().getFullName();
213     }
214 
215     public boolean getMale() {
216         return getContact().getMale();
217     }
218 
219     public boolean isMale() {
220         return getMale();
221     }
222 
223     public boolean getFemale() {
224         return !getMale();
225     }
226 
227     public boolean isFemale() {
228         return getFemale();
229     }
230 
231     public Date getBirthday() {
232         return getContact().getBirthday();
233     }
234 
235     public Group getGroup() {
236         Group group = null;
237 
238         try {
239             group = GroupLocalServiceUtil.getUserGroup(
240                 getCompanyId(), getUserId());
241         }
242         catch (Exception e) {
243         }
244 
245         return group;
246     }
247 
248     /**
249      * @deprecated Will return the first regular organization of the list in
250      * alphabetical order.
251      */
252     public Organization getOrganization() {
253         try {
254             List<Organization> organizations =
255                 OrganizationLocalServiceUtil.getUserOrganizations(getUserId());
256 
257             Collections.sort(
258                 organizations, new OrganizationNameComparator(true));
259 
260             for (Organization organization : organizations) {
261                 if (!organization.isLocation()) {
262                     return organization;
263                 }
264             }
265         }
266         catch (Exception e) {
267             if (_log.isWarnEnabled()) {
268                 _log.warn(
269                     "Unable to get an organization for user " + getUserId());
270             }
271         }
272 
273         return new OrganizationImpl();
274     }
275 
276     public long[] getOrganizationIds() {
277         List<Organization> organizations = getOrganizations();
278 
279         long[] organizationIds = new long[organizations.size()];
280 
281         for (int i = 0; i < organizations.size(); i++) {
282             Organization organization = organizations.get(i);
283 
284             organizationIds[i] = organization.getOrganizationId();
285         }
286 
287         return organizationIds;
288     }
289 
290     public List<Organization> getOrganizations() {
291         try {
292             return OrganizationLocalServiceUtil.getUserOrganizations(
293                 getUserId());
294         }
295         catch (Exception e) {
296             if (_log.isWarnEnabled()) {
297                 _log.warn(
298                     "Unable to get organizations for user " + getUserId());
299             }
300         }
301 
302         return new ArrayList<Organization>();
303     }
304 
305     public boolean hasOrganization() {
306         if (getOrganizations().size() > 0) {
307             return true;
308         }
309         else {
310             return false;
311         }
312     }
313 
314     /**
315      * @deprecated
316      */
317     public Organization getLocation() {
318         try {
319             List<Organization> organizations =
320                 OrganizationLocalServiceUtil.getUserOrganizations(getUserId());
321 
322             for (Organization organization : organizations) {
323                 if (organization.isLocation()) {
324                     return organization;
325                 }
326             }
327         }
328         catch (Exception e) {
329             if (_log.isWarnEnabled()) {
330                 _log.warn("Unable to get a location for user " + getUserId());
331             }
332         }
333 
334         return new OrganizationImpl();
335     }
336 
337     /**
338      * @deprecated
339      */
340     public long getLocationId() {
341         Organization location = getLocation();
342 
343         if (location == null) {
344             return OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID;
345         }
346 
347         return location.getOrganizationId();
348     }
349 
350     /**
351      * @deprecated
352      */
353     public boolean hasLocation() {
354         if (getLocation().getOrganizationId() > 0) {
355             return true;
356         }
357         else {
358             return false;
359         }
360     }
361 
362     public int getPrivateLayoutsPageCount() {
363         try {
364             Group group = getGroup();
365 
366             if (group == null) {
367                 return 0;
368             }
369             else {
370                 return group.getPrivateLayoutsPageCount();
371             }
372         }
373         catch (Exception e) {
374             _log.error(e);
375         }
376 
377         return 0;
378     }
379 
380     public boolean hasPrivateLayouts() {
381         if (getPrivateLayoutsPageCount() > 0) {
382             return true;
383         }
384         else {
385             return false;
386         }
387     }
388 
389     public int getPublicLayoutsPageCount() {
390         try {
391             Group group = getGroup();
392 
393             if (group == null) {
394                 return 0;
395             }
396             else {
397                 return group.getPublicLayoutsPageCount();
398             }
399         }
400         catch (Exception e) {
401             _log.error(e);
402         }
403 
404         return 0;
405     }
406 
407     public boolean hasPublicLayouts() {
408         if (getPublicLayoutsPageCount() > 0) {
409             return true;
410         }
411         else {
412             return false;
413         }
414     }
415 
416     public boolean isLayoutsRequired() {
417         try {
418             return RoleLocalServiceUtil.hasUserRole(
419                 getUserId(), getCompanyId(), RoleImpl.POWER_USER, true);
420         }
421         catch (Exception e) {
422             return false;
423         }
424     }
425 
426     public List<Group> getMyPlaces() {
427         List<Group> myPlaces = new ArrayList<Group>();
428 
429         try {
430             if (isDefaultUser()) {
431                 return myPlaces;
432             }
433 
434             LinkedHashMap<String, Object> groupParams =
435                 new LinkedHashMap<String, Object>();
436 
437             groupParams.put("usersGroups", new Long(getUserId()));
438             //groupParams.put("pageCount", StringPool.BLANK);
439 
440             myPlaces = GroupLocalServiceUtil.search(
441                 getCompanyId(), null, null, groupParams, QueryUtil.ALL_POS,
442                 QueryUtil.ALL_POS);
443 
444             List<Organization> userOrgs = getOrganizations();
445 
446             for (Organization organization : userOrgs) {
447                 myPlaces.add(0, organization.getGroup());
448             }
449 
450             if (isLayoutsRequired()) {
451                 Group userGroup = getGroup();
452 
453                 myPlaces.add(0, userGroup);
454             }
455         }
456         catch (Exception e) {
457             if (_log.isWarnEnabled()) {
458                 _log.warn(e, e);
459             }
460         }
461 
462         return myPlaces;
463     }
464 
465     public boolean hasMyPlaces() {
466         try {
467             if (isDefaultUser()) {
468                 return false;
469             }
470 
471             LinkedHashMap<String, Object> groupParams =
472                 new LinkedHashMap<String, Object>();
473 
474             groupParams.put("usersGroups", new Long(getUserId()));
475             //groupParams.put("pageCount", StringPool.BLANK);
476 
477             int count = GroupLocalServiceUtil.searchCount(
478                 getCompanyId(), null, null, groupParams);
479 
480             if (count > 0) {
481                 return true;
482             }
483 
484             count = OrganizationLocalServiceUtil.getUserOrganizationsCount(
485                 getUserId());
486 
487             if (count > 0) {
488                 return true;
489             }
490 
491             if (isLayoutsRequired()) {
492                 return true;
493             }
494         }
495         catch (Exception e) {
496             if (_log.isWarnEnabled()) {
497                 _log.warn(e, e);
498             }
499         }
500 
501         return false;
502     }
503 
504     public String getDisplayURL(String portalURL) {
505         try {
506             Group group = getGroup();
507 
508             int publicLayoutsPageCount = group.getPublicLayoutsPageCount();
509 
510             if (publicLayoutsPageCount > 0) {
511                 return portalURL + PortalUtil.getPathMain() +
512                     "/my_places/view?groupId=" + group.getGroupId() +
513                         "&privateLayout=0";
514             }
515         }
516         catch (Exception e) {
517             _log.error(e);
518         }
519 
520         return StringPool.BLANK;
521     }
522 
523     private static Log _log = LogFactory.getLog(UserImpl.class);
524 
525     private boolean _passwordModified;
526     private String _passwordUnencrypted;
527     private Locale _locale;
528     private TimeZone _timeZone;
529 
530 }