1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.model.impl;
16  
17  import com.liferay.portal.kernel.exception.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.PropsKeys;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.Account;
25  import com.liferay.portal.model.Company;
26  import com.liferay.portal.model.CompanyConstants;
27  import com.liferay.portal.model.Group;
28  import com.liferay.portal.model.Shard;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.service.AccountLocalServiceUtil;
31  import com.liferay.portal.service.GroupLocalServiceUtil;
32  import com.liferay.portal.service.ShardLocalServiceUtil;
33  import com.liferay.portal.service.UserLocalServiceUtil;
34  import com.liferay.portal.util.PrefsPropsUtil;
35  import com.liferay.portal.util.PropsValues;
36  
37  import java.security.Key;
38  
39  import java.util.Locale;
40  import java.util.TimeZone;
41  
42  /**
43   * <a href="CompanyImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class CompanyImpl extends CompanyModelImpl implements Company {
48  
49      public CompanyImpl() {
50      }
51  
52      public int compareTo(Company company) {
53          String webId1 = getWebId();
54          String webId2 = company.getWebId();
55  
56          if (webId1.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
57              return -1;
58          }
59          else if (webId2.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
60              return 1;
61          }
62          else {
63              return webId1.compareTo(webId2);
64          }
65      }
66  
67      public Account getAccount() {
68          Account account = null;
69  
70          try {
71              account = AccountLocalServiceUtil.getAccount(
72                  getCompanyId(), getAccountId());
73          }
74          catch (Exception e) {
75              account = new AccountImpl();
76  
77              _log.error(e, e);
78          }
79  
80          return account;
81      }
82  
83      public String getAdminName() {
84          return "Administrator";
85      }
86  
87      public String getAuthType() throws SystemException {
88          return PrefsPropsUtil.getString(
89              getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTH_TYPE,
90              PropsValues.COMPANY_SECURITY_AUTH_TYPE);
91      }
92  
93      public User getDefaultUser() {
94          User defaultUser = null;
95  
96          try {
97              defaultUser = UserLocalServiceUtil.getDefaultUser(getCompanyId());
98          }
99          catch (Exception e) {
100             _log.error(e, e);
101         }
102 
103         return defaultUser;
104     }
105 
106     public String getDefaultWebId() {
107         return PropsValues.COMPANY_DEFAULT_WEB_ID;
108     }
109 
110     public String getEmailAddress() {
111 
112         // Primary email address
113 
114         return "admin@" + getMx();
115     }
116 
117     public Group getGroup() {
118         if (getCompanyId() > CompanyConstants.SYSTEM) {
119             try {
120                 return GroupLocalServiceUtil.getCompanyGroup(getCompanyId());
121             }
122             catch (Exception e) {
123                 _log.error(e);
124             }
125         }
126 
127         return new GroupImpl();
128     }
129 
130     public Key getKeyObj() {
131         if (_keyObj == null) {
132             String key = getKey();
133 
134             if (Validator.isNotNull(key)) {
135                 _keyObj = (Key)Base64.stringToObject(key);
136             }
137         }
138 
139         return _keyObj;
140     }
141 
142     public Locale getLocale() {
143         return getDefaultUser().getLocale();
144     }
145 
146     public String getName() {
147         return getAccount().getName();
148     }
149 
150     public String getShardName() {
151         String shardName = PropsValues.SHARD_DEFAULT_NAME;
152 
153         try {
154             Shard shard = ShardLocalServiceUtil.getShard(
155                 Company.class.getName(), getCompanyId());
156 
157             shardName = shard.getName();
158         }
159         catch (Exception e) {
160             _log.error(e, e);
161         }
162 
163         return shardName;
164     }
165 
166     public String getShortName() {
167         return getName();
168     }
169 
170     public TimeZone getTimeZone() {
171         return getDefaultUser().getTimeZone();
172     }
173 
174     public boolean hasCompanyMx(String emailAddress) {
175         emailAddress = emailAddress.trim().toLowerCase();
176 
177         int pos = emailAddress.indexOf(StringPool.AT);
178 
179         if (pos == -1) {
180             return false;
181         }
182 
183         String mx = emailAddress.substring(pos + 1, emailAddress.length());
184 
185         if (mx.equals(getMx())) {
186             return true;
187         }
188 
189         try {
190             String[] mailHostNames = PrefsPropsUtil.getStringArray(
191                 getCompanyId(), PropsKeys.ADMIN_MAIL_HOST_NAMES,
192                 StringPool.NEW_LINE, PropsValues.ADMIN_MAIL_HOST_NAMES);
193 
194             for (int i = 0; i < mailHostNames.length; i++) {
195                 if (mx.equalsIgnoreCase(mailHostNames[i])) {
196                     return true;
197                 }
198             }
199         }
200         catch (Exception e) {
201             _log.error(e, e);
202         }
203 
204         return false;
205     }
206 
207     public boolean isAutoLogin() throws SystemException {
208         return PrefsPropsUtil.getBoolean(
209             getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
210             PropsValues.COMPANY_SECURITY_AUTO_LOGIN);
211     }
212 
213     public boolean isCommunityLogo() throws SystemException {
214         return PrefsPropsUtil.getBoolean(
215             getCompanyId(), PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO,
216             PropsValues.COMPANY_SECURITY_COMMUNITY_LOGO);
217     }
218 
219     public boolean isSendPassword() throws SystemException {
220         return PrefsPropsUtil.getBoolean(
221             getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
222             PropsValues.COMPANY_SECURITY_SEND_PASSWORD);
223     }
224 
225     public boolean isStrangers() throws SystemException {
226         return PrefsPropsUtil.getBoolean(
227             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS,
228             PropsValues.COMPANY_SECURITY_STRANGERS);
229     }
230 
231     public boolean isStrangersVerify() throws SystemException {
232         return PrefsPropsUtil.getBoolean(
233             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
234             PropsValues.COMPANY_SECURITY_STRANGERS_VERIFY);
235     }
236 
237     public boolean isStrangersWithMx() throws SystemException {
238         return PrefsPropsUtil.getBoolean(
239             getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
240             PropsValues.COMPANY_SECURITY_STRANGERS_WITH_MX);
241     }
242 
243     public void setKey(String key) {
244         _keyObj = null;
245 
246         super.setKey(key);
247     }
248 
249     public void setKeyObj(Key keyObj) {
250         _keyObj = keyObj;
251 
252         super.setKey(Base64.objectToString(keyObj));
253     }
254 
255     private static Log _log = LogFactoryUtil.getLog(CompanyImpl.class);
256 
257     private Key _keyObj = null;
258 
259 }