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