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