1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.model.impl;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.Base64;
21  import com.liferay.portal.kernel.util.CharPool;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.model.Account;
26  import com.liferay.portal.model.Company;
27  import com.liferay.portal.model.Shard;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.service.AccountLocalServiceUtil;
30  import com.liferay.portal.service.ShardLocalServiceUtil;
31  import com.liferay.portal.service.UserLocalServiceUtil;
32  import com.liferay.portal.util.PrefsPropsUtil;
33  import com.liferay.portal.util.PropsValues;
34  
35  import java.security.Key;
36  
37  import java.util.Locale;
38  import java.util.TimeZone;
39  
40  /**
41   * <a href="CompanyImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class CompanyImpl extends CompanyModelImpl implements Company {
46  
47      public CompanyImpl() {
48      }
49  
50      public int compareTo(Company company) {
51          String webId1 = getWebId();
52          String webId2 = company.getWebId();
53  
54          if (webId1.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
55              return -1;
56          }
57          else if (webId2.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
58              return 1;
59          }
60          else {
61              return webId1.compareTo(webId2);
62          }
63      }
64  
65      public Account getAccount() {
66          Account account = null;
67  
68          try {
69              account = AccountLocalServiceUtil.getAccount(
70                  getCompanyId(), getAccountId());
71          }
72          catch (Exception e) {
73              account = new AccountImpl();
74  
75              _log.error(e, e);
76          }
77  
78          return account;
79      }
80  
81      public String getAdminName() {
82          return "Administrator";
83      }
84  
85      public String getAuthType() throws SystemException {
86          return PrefsPropsUtil.getString(
87              getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTH_TYPE,
88              PropsValues.COMPANY_SECURITY_AUTH_TYPE);
89      }
90  
91      public User getDefaultUser() {
92          User defaultUser = null;
93  
94          try {
95              defaultUser = UserLocalServiceUtil.getDefaultUser(getCompanyId());
96          }
97          catch (Exception e) {
98              _log.error(e, e);
99          }
100 
101         return defaultUser;
102     }
103 
104     public String getDefaultWebId() {
105         return PropsValues.COMPANY_DEFAULT_WEB_ID;
106     }
107 
108     public String getEmailAddress() {
109 
110         // Primary email address
111 
112         return "admin@" + getMx();
113     }
114 
115     public Key getKeyObj() {
116         if (_keyObj == null) {
117             String key = getKey();
118 
119             if (Validator.isNotNull(key)) {
120                 _keyObj = (Key)Base64.stringToObject(key);
121             }
122         }
123 
124         return _keyObj;
125     }
126 
127     public Locale getLocale() {
128         return getDefaultUser().getLocale();
129     }
130 
131     public String getName() {
132         return getAccount().getName();
133     }
134 
135     public String getShardName() {
136         String shardName = PropsValues.SHARD_DEFAULT_NAME;
137 
138         try {
139             Shard shard = ShardLocalServiceUtil.getShard(
140                 Company.class.getName(), getCompanyId());
141 
142             shardName = shard.getName();
143         }
144         catch (Exception e) {
145             _log.error(e, e);
146         }
147 
148         return shardName;
149     }
150 
151     public String getShortName() {
152         return getName();
153     }
154 
155     public TimeZone getTimeZone() {
156         return getDefaultUser().getTimeZone();
157     }
158 
159     public boolean hasCompanyMx(String emailAddress) {
160         emailAddress = emailAddress.trim().toLowerCase();
161 
162         int pos = emailAddress.indexOf(CharPool.AT);
163 
164         if (pos == -1) {
165             return false;
166         }
167 
168         String mx = emailAddress.substring(pos + 1, emailAddress.length());
169 
170         if (mx.equals(getMx())) {
171             return true;
172         }
173 
174         try {
175             String[] mailHostNames = PrefsPropsUtil.getStringArray(
176                 getCompanyId(), PropsKeys.ADMIN_MAIL_HOST_NAMES,
177                 StringPool.NEW_LINE, PropsValues.ADMIN_MAIL_HOST_NAMES);
178 
179             for (int i = 0; i < mailHostNames.length; i++) {
180                 if (mx.equalsIgnoreCase(mailHostNames[i])) {
181                     return true;
182                 }
183             }
184         }
185         catch (Exception e) {
186             _log.error(e, e);
187         }
188 
189         return false;
190     }
191 
192     public boolean isAutoLogin() throws SystemException {
193         return PrefsPropsUtil.getBoolean(
194             getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
195             PropsValues.COMPANY_SECURITY_AUTO_LOGIN);
196     }
197 
198     public boolean isCommunityLogo() throws SystemException {
199         return PrefsPropsUtil.getBoolean(
200             getCompanyId(), PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO,
201             PropsValues.COMPANY_SECURITY_COMMUNITY_LOGO);
202     }
203 
204     public boolean isSendPassword() throws SystemException {
205         return PrefsPropsUtil.getBoolean(
206             getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
207             PropsValues.COMPANY_SECURITY_SEND_PASSWORD);
208     }
209 
210     public boolean isStrangers() throws SystemException {
211         return PrefsPropsUtil.getBoolean(
212             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS,
213             PropsValues.COMPANY_SECURITY_STRANGERS);
214     }
215 
216     public boolean isStrangersVerify() throws SystemException {
217         return PrefsPropsUtil.getBoolean(
218             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
219             PropsValues.COMPANY_SECURITY_STRANGERS_VERIFY);
220     }
221 
222     public boolean isStrangersWithMx() throws SystemException {
223         return PrefsPropsUtil.getBoolean(
224             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
225             PropsValues.COMPANY_SECURITY_STRANGERS_WITH_MX);
226     }
227 
228     public void setKey(String key) {
229         _keyObj = null;
230 
231         super.setKey(key);
232     }
233 
234     public void setKeyObj(Key keyObj) {
235         _keyObj = keyObj;
236 
237         super.setKey(Base64.objectToString(keyObj));
238     }
239 
240     private static Log _log = LogFactoryUtil.getLog(CompanyImpl.class);
241 
242     private Key _keyObj = null;
243 
244 }