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