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