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