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