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