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.SystemException;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.Base64;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Account;
32  import com.liferay.portal.model.Company;
33  import com.liferay.portal.model.Shard;
34  import com.liferay.portal.model.User;
35  import com.liferay.portal.service.AccountLocalServiceUtil;
36  import com.liferay.portal.service.ShardLocalServiceUtil;
37  import com.liferay.portal.service.UserLocalServiceUtil;
38  import com.liferay.portal.util.PrefsPropsUtil;
39  import com.liferay.portal.util.PropsKeys;
40  import com.liferay.portal.util.PropsValues;
41  
42  import java.security.Key;
43  
44  import java.util.Locale;
45  import java.util.TimeZone;
46  
47  /**
48   * <a href="CompanyImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   */
52  public class CompanyImpl extends CompanyModelImpl implements Company {
53  
54      public CompanyImpl() {
55      }
56  
57      public int compareTo(Company company) {
58          String webId1 = getWebId();
59          String webId2 = company.getWebId();
60  
61          if (webId1.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
62              return -1;
63          }
64          else if (webId2.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
65              return 1;
66          }
67          else {
68              return webId1.compareTo(webId2);
69          }
70      }
71  
72      public Account getAccount() {
73          Account account = null;
74  
75          try {
76              account = AccountLocalServiceUtil.getAccount(
77                  getCompanyId(), getAccountId());
78          }
79          catch (Exception e) {
80              account = new AccountImpl();
81  
82              _log.error(e, e);
83          }
84  
85          return account;
86      }
87  
88      public String getAdminName() {
89          return "Administrator";
90      }
91  
92      public String getAuthType() throws SystemException {
93          return PrefsPropsUtil.getString(
94              getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTH_TYPE,
95              PropsValues.COMPANY_SECURITY_AUTH_TYPE);
96      }
97  
98      public User getDefaultUser() {
99          User defaultUser = null;
100 
101         try {
102             defaultUser = UserLocalServiceUtil.getDefaultUser(getCompanyId());
103         }
104         catch (Exception e) {
105             _log.error(e, e);
106         }
107 
108         return defaultUser;
109     }
110 
111     public String getDefaultWebId() {
112         return PropsValues.COMPANY_DEFAULT_WEB_ID;
113     }
114 
115     public String getEmailAddress() {
116 
117         // Primary email address
118 
119         return "admin@" + getMx();
120     }
121 
122     public Key getKeyObj() {
123         if (_keyObj == null) {
124             String key = getKey();
125 
126             if (Validator.isNotNull(key)) {
127                 _keyObj = (Key)Base64.stringToObject(key);
128             }
129         }
130 
131         return _keyObj;
132     }
133 
134     public Locale getLocale() {
135         return getDefaultUser().getLocale();
136     }
137 
138     public String getName() {
139         return getAccount().getName();
140     }
141 
142     public String getShardName() {
143         String shardName = PropsValues.SHARD_DEFAULT_NAME;
144 
145         try {
146             Shard shard = ShardLocalServiceUtil.getShard(
147                 Company.class.getName(), getCompanyId());
148 
149             shardName = shard.getName();
150         }
151         catch (Exception e) {
152             _log.error(e, e);
153         }
154 
155         return shardName;
156     }
157 
158     public String getShortName() {
159         return getName();
160     }
161 
162     public TimeZone getTimeZone() {
163         return getDefaultUser().getTimeZone();
164     }
165 
166     public boolean hasCompanyMx(String emailAddress) {
167         emailAddress = emailAddress.trim().toLowerCase();
168 
169         int pos = emailAddress.indexOf(StringPool.AT);
170 
171         if (pos == -1) {
172             return false;
173         }
174 
175         String mx = emailAddress.substring(pos + 1, emailAddress.length());
176 
177         if (mx.equals(getMx())) {
178             return true;
179         }
180 
181         try {
182             String[] mailHostNames = PrefsPropsUtil.getStringArray(
183                 getCompanyId(), PropsKeys.ADMIN_MAIL_HOST_NAMES,
184                 StringPool.NEW_LINE, PropsValues.ADMIN_MAIL_HOST_NAMES);
185 
186             for (int i = 0; i < mailHostNames.length; i++) {
187                 if (mx.equalsIgnoreCase(mailHostNames[i])) {
188                     return true;
189                 }
190             }
191         }
192         catch (Exception e) {
193             _log.error(e, e);
194         }
195 
196         return false;
197     }
198 
199     public boolean isAutoLogin() throws SystemException {
200         return PrefsPropsUtil.getBoolean(
201             getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
202             PropsValues.COMPANY_SECURITY_AUTO_LOGIN);
203     }
204 
205     public boolean isCommunityLogo() throws SystemException {
206         return PrefsPropsUtil.getBoolean(
207             getCompanyId(), PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO,
208             PropsValues.COMPANY_SECURITY_COMMUNITY_LOGO);
209     }
210 
211     public boolean isSendPassword() throws SystemException {
212         return PrefsPropsUtil.getBoolean(
213             getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
214             PropsValues.COMPANY_SECURITY_SEND_PASSWORD);
215     }
216 
217     public boolean isStrangers() throws SystemException {
218         return PrefsPropsUtil.getBoolean(
219             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS,
220             PropsValues.COMPANY_SECURITY_STRANGERS);
221     }
222 
223     public boolean isStrangersVerify() throws SystemException {
224         return PrefsPropsUtil.getBoolean(
225             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
226             PropsValues.COMPANY_SECURITY_STRANGERS_VERIFY);
227     }
228 
229     public boolean isStrangersWithMx() throws SystemException {
230         return PrefsPropsUtil.getBoolean(
231             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
232             PropsValues.COMPANY_SECURITY_STRANGERS_WITH_MX);
233     }
234 
235     public void setKey(String key) {
236         _keyObj = null;
237 
238         super.setKey(key);
239     }
240 
241     public void setKeyObj(Key keyObj) {
242         _keyObj = keyObj;
243 
244         super.setKey(Base64.objectToString(keyObj));
245     }
246 
247     private static Log _log = LogFactoryUtil.getLog(CompanyImpl.class);
248 
249     private Key _keyObj = null;
250 
251 }