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