1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.model.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.Base64;
20  import com.liferay.portal.kernel.util.PropsKeys;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.Account;
24  import com.liferay.portal.model.Company;
25  import com.liferay.portal.model.CompanyConstants;
26  import com.liferay.portal.model.Group;
27  import com.liferay.portal.model.Shard;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.service.AccountLocalServiceUtil;
30  import com.liferay.portal.service.GroupLocalServiceUtil;
31  import com.liferay.portal.service.ShardLocalServiceUtil;
32  import com.liferay.portal.service.UserLocalServiceUtil;
33  import com.liferay.portal.util.PrefsPropsUtil;
34  import com.liferay.portal.util.PropsValues;
35  
36  import java.security.Key;
37  
38  import java.util.Locale;
39  import java.util.TimeZone;
40  
41  /**
42   * <a href="CompanyImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class CompanyImpl extends CompanyModelImpl implements Company {
47  
48      public CompanyImpl() {
49      }
50  
51      public int compareTo(Company company) {
52          String webId1 = getWebId();
53          String webId2 = company.getWebId();
54  
55          if (webId1.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
56              return -1;
57          }
58          else if (webId2.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
59              return 1;
60          }
61          else {
62              return webId1.compareTo(webId2);
63          }
64      }
65  
66      public Account getAccount() throws PortalException, SystemException {
67          return AccountLocalServiceUtil.getAccount(
68              getCompanyId(), getAccountId());
69      }
70  
71      public String getAdminName() {
72          return "Administrator";
73      }
74  
75      public String getAuthType() throws SystemException {
76          return PrefsPropsUtil.getString(
77              getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTH_TYPE,
78              PropsValues.COMPANY_SECURITY_AUTH_TYPE);
79      }
80  
81      public User getDefaultUser() throws PortalException, SystemException {
82          return UserLocalServiceUtil.getDefaultUser(getCompanyId());
83      }
84  
85      public String getDefaultWebId() {
86          return PropsValues.COMPANY_DEFAULT_WEB_ID;
87      }
88  
89      public String getEmailAddress() {
90  
91          // Primary email address
92  
93          return "admin@" + getMx();
94      }
95  
96      public Group getGroup() throws PortalException, SystemException {
97          if (getCompanyId() > CompanyConstants.SYSTEM) {
98              return GroupLocalServiceUtil.getCompanyGroup(getCompanyId());
99          }
100 
101         return new GroupImpl();
102     }
103 
104     public Key getKeyObj() {
105         if (_keyObj == null) {
106             String key = getKey();
107 
108             if (Validator.isNotNull(key)) {
109                 _keyObj = (Key)Base64.stringToObject(key);
110             }
111         }
112 
113         return _keyObj;
114     }
115 
116     public Locale getLocale() throws PortalException, SystemException {
117         return getDefaultUser().getLocale();
118     }
119 
120     public String getName() throws PortalException, SystemException {
121         return getAccount().getName();
122     }
123 
124     public String getShardName() throws PortalException, SystemException {
125         Shard shard = ShardLocalServiceUtil.getShard(
126             Company.class.getName(), getCompanyId());
127 
128         return shard.getName();
129     }
130 
131     public String getShortName() throws PortalException, SystemException {
132         return getName();
133     }
134 
135     public TimeZone getTimeZone() throws PortalException, SystemException {
136         return getDefaultUser().getTimeZone();
137     }
138 
139     public boolean hasCompanyMx(String emailAddress)
140         throws SystemException {
141 
142         emailAddress = emailAddress.trim().toLowerCase();
143 
144         int pos = emailAddress.indexOf(StringPool.AT);
145 
146         if (pos == -1) {
147             return false;
148         }
149 
150         String mx = emailAddress.substring(pos + 1, emailAddress.length());
151 
152         if (mx.equals(getMx())) {
153             return true;
154         }
155 
156         String[] mailHostNames = PrefsPropsUtil.getStringArray(
157             getCompanyId(), PropsKeys.ADMIN_MAIL_HOST_NAMES,
158             StringPool.NEW_LINE, PropsValues.ADMIN_MAIL_HOST_NAMES);
159 
160         for (int i = 0; i < mailHostNames.length; i++) {
161             if (mx.equalsIgnoreCase(mailHostNames[i])) {
162                 return true;
163             }
164         }
165 
166         return false;
167     }
168 
169     public boolean isAutoLogin() throws SystemException {
170         return PrefsPropsUtil.getBoolean(
171             getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
172             PropsValues.COMPANY_SECURITY_AUTO_LOGIN);
173     }
174 
175     public boolean isCommunityLogo() throws SystemException {
176         return PrefsPropsUtil.getBoolean(
177             getCompanyId(), PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO,
178             PropsValues.COMPANY_SECURITY_COMMUNITY_LOGO);
179     }
180 
181     public boolean isSendPassword() throws SystemException {
182         return PrefsPropsUtil.getBoolean(
183             getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
184             PropsValues.COMPANY_SECURITY_SEND_PASSWORD);
185     }
186 
187     public boolean isSendPasswordResetLink() throws SystemException {
188         return PrefsPropsUtil.getBoolean(
189             getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK,
190             PropsValues.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK);
191     }
192 
193     public boolean isStrangers() throws SystemException {
194         return PrefsPropsUtil.getBoolean(
195             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS,
196             PropsValues.COMPANY_SECURITY_STRANGERS);
197     }
198 
199     public boolean isStrangersVerify() throws SystemException {
200         return PrefsPropsUtil.getBoolean(
201             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
202             PropsValues.COMPANY_SECURITY_STRANGERS_VERIFY);
203     }
204 
205     public boolean isStrangersWithMx() throws SystemException {
206         return PrefsPropsUtil.getBoolean(
207             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
208             PropsValues.COMPANY_SECURITY_STRANGERS_WITH_MX);
209     }
210 
211     public void setKey(String key) {
212         _keyObj = null;
213 
214         super.setKey(key);
215     }
216 
217     public void setKeyObj(Key keyObj) {
218         _keyObj = keyObj;
219 
220         super.setKey(Base64.objectToString(keyObj));
221     }
222 
223     private Key _keyObj = null;
224 
225 }