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