1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.ListUtil;
31  import com.liferay.portal.kernel.util.LocaleUtil;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.TimeZoneUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.model.Company;
36  import com.liferay.portal.model.CompanyConstants;
37  import com.liferay.portal.model.Contact;
38  import com.liferay.portal.model.ContactConstants;
39  import com.liferay.portal.model.Group;
40  import com.liferay.portal.model.Organization;
41  import com.liferay.portal.model.OrganizationConstants;
42  import com.liferay.portal.model.PasswordPolicy;
43  import com.liferay.portal.model.User;
44  import com.liferay.portal.service.CompanyLocalServiceUtil;
45  import com.liferay.portal.service.ContactLocalServiceUtil;
46  import com.liferay.portal.service.GroupLocalServiceUtil;
47  import com.liferay.portal.service.OrganizationLocalServiceUtil;
48  import com.liferay.portal.service.PasswordPolicyLocalServiceUtil;
49  import com.liferay.portal.theme.ThemeDisplay;
50  import com.liferay.portal.util.PropsValues;
51  import com.liferay.portal.util.comparator.OrganizationNameComparator;
52  
53  import java.util.ArrayList;
54  import java.util.Collections;
55  import java.util.Date;
56  import java.util.LinkedHashMap;
57  import java.util.List;
58  import java.util.Locale;
59  import java.util.TimeZone;
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 getFullName() {
195         return ContactConstants.getFullName(
196             getFirstName(), getMiddleName(), getLastName());
197     }
198 
199     public boolean getMale() {
200         return getContact().getMale();
201     }
202 
203     public boolean isMale() {
204         return getMale();
205     }
206 
207     public boolean getFemale() {
208         return !getMale();
209     }
210 
211     public boolean isFemale() {
212         return getFemale();
213     }
214 
215     public Date getBirthday() {
216         return getContact().getBirthday();
217     }
218 
219     public Group getGroup() {
220         Group group = null;
221 
222         try {
223             group = GroupLocalServiceUtil.getUserGroup(
224                 getCompanyId(), getUserId());
225         }
226         catch (Exception e) {
227         }
228 
229         return group;
230     }
231 
232     /**
233      * @deprecated Will return the first regular organization of the list in
234      * alphabetical order.
235      */
236     public Organization getOrganization() {
237         try {
238             List<Organization> organizations =
239                 OrganizationLocalServiceUtil.getUserOrganizations(getUserId());
240 
241             Collections.sort(
242                 organizations, new OrganizationNameComparator(true));
243 
244             for (Organization organization : organizations) {
245                 if (!organization.isLocation()) {
246                     return organization;
247                 }
248             }
249         }
250         catch (Exception e) {
251             if (_log.isWarnEnabled()) {
252                 _log.warn(
253                     "Unable to get an organization for user " + getUserId());
254             }
255         }
256 
257         return new OrganizationImpl();
258     }
259 
260     public long[] getOrganizationIds() {
261         List<Organization> organizations = getOrganizations();
262 
263         long[] organizationIds = new long[organizations.size()];
264 
265         for (int i = 0; i < organizations.size(); i++) {
266             Organization organization = organizations.get(i);
267 
268             organizationIds[i] = organization.getOrganizationId();
269         }
270 
271         return organizationIds;
272     }
273 
274     public List<Organization> getOrganizations() {
275         try {
276             return OrganizationLocalServiceUtil.getUserOrganizations(
277                 getUserId());
278         }
279         catch (Exception e) {
280             if (_log.isWarnEnabled()) {
281                 _log.warn(
282                     "Unable to get organizations for user " + getUserId());
283             }
284         }
285 
286         return new ArrayList<Organization>();
287     }
288 
289     public boolean hasOrganization() {
290         if (getOrganizations().size() > 0) {
291             return true;
292         }
293         else {
294             return false;
295         }
296     }
297 
298     /**
299      * @deprecated
300      */
301     public Organization getLocation() {
302         try {
303             List<Organization> organizations =
304                 OrganizationLocalServiceUtil.getUserOrganizations(getUserId());
305 
306             for (Organization organization : organizations) {
307                 if (organization.isLocation()) {
308                     return organization;
309                 }
310             }
311         }
312         catch (Exception e) {
313             if (_log.isWarnEnabled()) {
314                 _log.warn("Unable to get a location for user " + getUserId());
315             }
316         }
317 
318         return new OrganizationImpl();
319     }
320 
321     /**
322      * @deprecated
323      */
324     public long getLocationId() {
325         Organization location = getLocation();
326 
327         if (location == null) {
328             return OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID;
329         }
330 
331         return location.getOrganizationId();
332     }
333 
334     /**
335      * @deprecated
336      */
337     public boolean hasLocation() {
338         if (getLocation().getOrganizationId() > 0) {
339             return true;
340         }
341         else {
342             return false;
343         }
344     }
345 
346     public int getPrivateLayoutsPageCount() {
347         try {
348             Group group = getGroup();
349 
350             if (group == null) {
351                 return 0;
352             }
353             else {
354                 return group.getPrivateLayoutsPageCount();
355             }
356         }
357         catch (Exception e) {
358             _log.error(e);
359         }
360 
361         return 0;
362     }
363 
364     public boolean hasPrivateLayouts() {
365         if (getPrivateLayoutsPageCount() > 0) {
366             return true;
367         }
368         else {
369             return false;
370         }
371     }
372 
373     public int getPublicLayoutsPageCount() {
374         try {
375             Group group = getGroup();
376 
377             if (group == null) {
378                 return 0;
379             }
380             else {
381                 return group.getPublicLayoutsPageCount();
382             }
383         }
384         catch (Exception e) {
385             _log.error(e);
386         }
387 
388         return 0;
389     }
390 
391     public boolean hasPublicLayouts() {
392         if (getPublicLayoutsPageCount() > 0) {
393             return true;
394         }
395         else {
396             return false;
397         }
398     }
399 
400     public List<Group> getMyPlaces() {
401         return getMyPlaces(QueryUtil.ALL_POS);
402     }
403 
404     public List<Group> getMyPlaces(int max) {
405         List<Group> myPlaces = new ArrayList<Group>();
406 
407         try {
408             if (isDefaultUser()) {
409                 return myPlaces;
410             }
411 
412             int start = QueryUtil.ALL_POS;
413             int end = QueryUtil.ALL_POS;
414 
415             if (max != QueryUtil.ALL_POS) {
416                 start = 0;
417                 end = max;
418             }
419 
420             LinkedHashMap<String, Object> groupParams =
421                 new LinkedHashMap<String, Object>();
422 
423             groupParams.put("usersGroups", new Long(getUserId()));
424             //groupParams.put("pageCount", StringPool.BLANK);
425 
426             myPlaces = GroupLocalServiceUtil.search(
427                 getCompanyId(), null, null, groupParams, start, end);
428 
429             List<Organization> userOrgs =
430                 OrganizationLocalServiceUtil.getUserOrganizations(
431                     getUserId(), start, end);
432 
433             for (Organization organization : userOrgs) {
434                 myPlaces.add(0, organization.getGroup());
435             }
436 
437             if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
438                 PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
439 
440                 Group userGroup = getGroup();
441 
442                 myPlaces.add(0, userGroup);
443             }
444 
445             if ((max != QueryUtil.ALL_POS) && (myPlaces.size() > max)) {
446                 myPlaces = ListUtil.subList(myPlaces, start, end);
447             }
448         }
449         catch (Exception e) {
450             if (_log.isWarnEnabled()) {
451                 _log.warn(e, e);
452             }
453         }
454 
455         return myPlaces;
456     }
457 
458     public boolean hasMyPlaces() {
459         try {
460             if (isDefaultUser()) {
461                 return false;
462             }
463 
464             LinkedHashMap<String, Object> groupParams =
465                 new LinkedHashMap<String, Object>();
466 
467             groupParams.put("usersGroups", new Long(getUserId()));
468             //groupParams.put("pageCount", StringPool.BLANK);
469 
470             int count = GroupLocalServiceUtil.searchCount(
471                 getCompanyId(), null, null, groupParams);
472 
473             if (count > 0) {
474                 return true;
475             }
476 
477             count = OrganizationLocalServiceUtil.getUserOrganizationsCount(
478                 getUserId());
479 
480             if (count > 0) {
481                 return true;
482             }
483 
484             if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
485                 PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
486 
487                 return true;
488             }
489         }
490         catch (Exception e) {
491             if (_log.isWarnEnabled()) {
492                 _log.warn(e, e);
493             }
494         }
495 
496         return false;
497     }
498 
499     public String getDisplayURL(ThemeDisplay themeDisplay) {
500         return getDisplayURL(
501             themeDisplay.getPortalURL(), themeDisplay.getPathMain());
502 
503     }
504 
505     public String getDisplayURL(String portalURL, String mainPath) {
506         try {
507             Group group = getGroup();
508 
509             if (group != null) {
510                 int publicLayoutsPageCount = group.getPublicLayoutsPageCount();
511 
512                 if (publicLayoutsPageCount > 0) {
513                     StringBuilder sb = new StringBuilder();
514 
515                     sb.append(portalURL);
516                     sb.append(mainPath);
517                     sb.append("/my_places/view?groupId=");
518                     sb.append(group.getGroupId());
519                     sb.append("&privateLayout=0");
520 
521                     return sb.toString();
522                 }
523             }
524         }
525         catch (Exception e) {
526             _log.error(e, e);
527         }
528 
529         return StringPool.BLANK;
530     }
531 
532     private static Log _log = LogFactoryUtil.getLog(UserImpl.class);
533 
534     private boolean _passwordModified;
535     private String _passwordUnencrypted;
536     private Locale _locale;
537     private TimeZone _timeZone;
538 
539 }