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