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