1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
53  public class CompanyImpl extends CompanyModelImpl implements Company {
54  
55      public CompanyImpl() {
56      }
57  
58      public int compareTo(Company company) {
59          String webId1 = getWebId();
60          String webId2 = company.getWebId();
61  
62          if (webId1.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
63              return -1;
64          }
65          else if (webId2.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
66              return 1;
67          }
68          else {
69              return webId1.compareTo(webId2);
70          }
71      }
72  
73      public Account getAccount() {
74          Account account = null;
75  
76          try {
77              account = AccountLocalServiceUtil.getAccount(
78                  getCompanyId(), getAccountId());
79          }
80          catch (Exception e) {
81              account = new AccountImpl();
82  
83              _log.error(e, e);
84          }
85  
86          return account;
87      }
88  
89      public String getAdminName() {
90          return "Administrator";
91      }
92  
93      public String getAuthType() throws SystemException {
94          return PrefsPropsUtil.getString(
95              getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTH_TYPE,
96              PropsValues.COMPANY_SECURITY_AUTH_TYPE);
97      }
98  
99      public User getDefaultUser() {
100         User defaultUser = null;
101 
102         try {
103             defaultUser = UserLocalServiceUtil.getDefaultUser(getCompanyId());
104         }
105         catch (Exception e) {
106             _log.error(e, e);
107         }
108 
109         return defaultUser;
110     }
111 
112     public String getDefaultWebId() {
113         return PropsValues.COMPANY_DEFAULT_WEB_ID;
114     }
115 
116     public String getEmailAddress() {
117 
118         // Primary email address
119 
120         return "admin@" + getMx();
121     }
122 
123     public Key getKeyObj() {
124         if (_keyObj == null) {
125             String key = getKey();
126 
127             if (Validator.isNotNull(key)) {
128                 _keyObj = (Key)Base64.stringToObject(key);
129             }
130         }
131 
132         return _keyObj;
133     }
134 
135     public Locale getLocale() {
136         return getDefaultUser().getLocale();
137     }
138 
139     public String getName() {
140         return getAccount().getName();
141     }
142 
143     public String getShardName() {
144         String shardName = PropsValues.SHARD_DEFAULT_NAME;
145 
146         try {
147             Shard shard = ShardLocalServiceUtil.getShard(
148                 Company.class.getName(), getCompanyId());
149 
150             shardName = shard.getName();
151         }
152         catch (Exception e) {
153             _log.error(e, e);
154         }
155 
156         return shardName;
157     }
158 
159     public String getShortName() {
160         return getName();
161     }
162 
163     public TimeZone getTimeZone() {
164         return getDefaultUser().getTimeZone();
165     }
166 
167     public boolean hasCompanyMx(String emailAddress) {
168         emailAddress = emailAddress.trim().toLowerCase();
169 
170         int pos = emailAddress.indexOf(StringPool.AT);
171 
172         if (pos == -1) {
173             return false;
174         }
175 
176         String mx = emailAddress.substring(pos + 1, emailAddress.length());
177 
178         if (mx.equals(getMx())) {
179             return true;
180         }
181 
182         try {
183             String[] mailHostNames = PrefsPropsUtil.getStringArray(
184                 getCompanyId(), PropsKeys.ADMIN_MAIL_HOST_NAMES,
185                 StringPool.NEW_LINE, PropsValues.ADMIN_MAIL_HOST_NAMES);
186 
187             for (int i = 0; i < mailHostNames.length; i++) {
188                 if (mx.equalsIgnoreCase(mailHostNames[i])) {
189                     return true;
190                 }
191             }
192         }
193         catch (Exception e) {
194             _log.error(e, e);
195         }
196 
197         return false;
198     }
199 
200     public boolean isAutoLogin() throws SystemException {
201         return PrefsPropsUtil.getBoolean(
202             getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
203             PropsValues.COMPANY_SECURITY_AUTO_LOGIN);
204     }
205 
206     public boolean isCommunityLogo() throws SystemException {
207         return PrefsPropsUtil.getBoolean(
208             getCompanyId(), PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO,
209             PropsValues.COMPANY_SECURITY_COMMUNITY_LOGO);
210     }
211 
212     public boolean isSendPassword() throws SystemException {
213         return PrefsPropsUtil.getBoolean(
214             getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
215             PropsValues.COMPANY_SECURITY_SEND_PASSWORD);
216     }
217 
218     public boolean isStrangers() throws SystemException {
219         return PrefsPropsUtil.getBoolean(
220             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS,
221             PropsValues.COMPANY_SECURITY_STRANGERS);
222     }
223 
224     public boolean isStrangersVerify() throws SystemException {
225         return PrefsPropsUtil.getBoolean(
226             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
227             PropsValues.COMPANY_SECURITY_STRANGERS_VERIFY);
228     }
229 
230     public boolean isStrangersWithMx() throws SystemException {
231         return PrefsPropsUtil.getBoolean(
232             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
233             PropsValues.COMPANY_SECURITY_STRANGERS_WITH_MX);
234     }
235 
236     public void setKey(String key) {
237         _keyObj = null;
238 
239         super.setKey(key);
240     }
241 
242     public void setKeyObj(Key keyObj) {
243         _keyObj = keyObj;
244 
245         super.setKey(Base64.objectToString(keyObj));
246     }
247 
248     private static Log _log = LogFactoryUtil.getLog(CompanyImpl.class);
249 
250     private Key _keyObj = null;
251 
252 }