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