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