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