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