001
014
015 package com.liferay.portal.model.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.util.Base64;
020 import com.liferay.portal.kernel.util.CharPool;
021 import com.liferay.portal.kernel.util.Http;
022 import com.liferay.portal.kernel.util.PropsKeys;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.model.Account;
026 import com.liferay.portal.model.CacheField;
027 import com.liferay.portal.model.Company;
028 import com.liferay.portal.model.CompanyConstants;
029 import com.liferay.portal.model.Group;
030 import com.liferay.portal.model.LayoutSet;
031 import com.liferay.portal.model.Shard;
032 import com.liferay.portal.model.User;
033 import com.liferay.portal.model.VirtualHost;
034 import com.liferay.portal.service.AccountLocalServiceUtil;
035 import com.liferay.portal.service.GroupLocalServiceUtil;
036 import com.liferay.portal.service.LayoutSetLocalServiceUtil;
037 import com.liferay.portal.service.ShardLocalServiceUtil;
038 import com.liferay.portal.service.UserLocalServiceUtil;
039 import com.liferay.portal.service.VirtualHostLocalServiceUtil;
040 import com.liferay.portal.util.PortalUtil;
041 import com.liferay.portal.util.PrefsPropsUtil;
042 import com.liferay.portal.util.PropsValues;
043
044 import java.security.Key;
045
046 import java.util.Locale;
047 import java.util.TimeZone;
048
049
052 public class CompanyImpl extends CompanyBaseImpl {
053
054 public CompanyImpl() {
055 }
056
057 @Override
058 public int compareTo(Company company) {
059 String webId1 = getWebId();
060 String webId2 = company.getWebId();
061
062 if (webId1.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
063 return -1;
064 }
065 else if (webId2.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
066 return 1;
067 }
068 else {
069 return webId1.compareTo(webId2);
070 }
071 }
072
073 public Account getAccount() throws PortalException, SystemException {
074 return AccountLocalServiceUtil.getAccount(
075 getCompanyId(), getAccountId());
076 }
077
078 public String getAdminName() {
079 return "Administrator";
080 }
081
082 public String getAuthType() throws SystemException {
083 return PrefsPropsUtil.getString(
084 getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTH_TYPE,
085 PropsValues.COMPANY_SECURITY_AUTH_TYPE);
086 }
087
088 public User getDefaultUser() throws PortalException, SystemException {
089 return UserLocalServiceUtil.getDefaultUser(getCompanyId());
090 }
091
092 public String getDefaultWebId() {
093 return PropsValues.COMPANY_DEFAULT_WEB_ID;
094 }
095
096 public String getEmailAddress() {
097
098
099
100 return "admin@" + getMx();
101 }
102
103 public Group getGroup() throws PortalException, SystemException {
104 if (getCompanyId() > CompanyConstants.SYSTEM) {
105 return GroupLocalServiceUtil.getCompanyGroup(getCompanyId());
106 }
107
108 return new GroupImpl();
109 }
110
111 @Override
112 public Key getKeyObj() {
113 if (_keyObj == null) {
114 String key = getKey();
115
116 if (Validator.isNotNull(key)) {
117 _keyObj = (Key)Base64.stringToObjectSilent(key);
118 }
119 }
120
121 return _keyObj;
122 }
123
124 public Locale getLocale() throws PortalException, SystemException {
125 return getDefaultUser().getLocale();
126 }
127
128 public String getName() throws PortalException, SystemException {
129 return getAccount().getName();
130 }
131
132 public String getPortalURL(long groupId)
133 throws PortalException, SystemException {
134
135 String portalURL = PortalUtil.getPortalURL(
136 getVirtualHostname(), Http.HTTP_PORT, false);
137
138 if (groupId <= 0) {
139 return portalURL;
140 }
141
142 Group group = GroupLocalServiceUtil.getGroup(groupId);
143
144 if (group.hasPublicLayouts()) {
145 LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
146 groupId, false);
147
148 if (Validator.isNotNull(layoutSet.getVirtualHostname())) {
149 portalURL = PortalUtil.getPortalURL(
150 layoutSet.getVirtualHostname(), Http.HTTP_PORT, false);
151 }
152 }
153 else if (group.hasPrivateLayouts()) {
154 LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
155 groupId, true);
156
157 if (Validator.isNotNull(layoutSet.getVirtualHostname())) {
158 portalURL = PortalUtil.getPortalURL(
159 layoutSet.getVirtualHostname(), Http.HTTP_PORT, false);
160 }
161 }
162
163 return portalURL;
164 }
165
166 public String getShardName() throws PortalException, SystemException {
167 Shard shard = ShardLocalServiceUtil.getShard(
168 Company.class.getName(), getCompanyId());
169
170 return shard.getName();
171 }
172
173 public String getShortName() throws PortalException, SystemException {
174 return getName();
175 }
176
177 public TimeZone getTimeZone() throws PortalException, SystemException {
178 return getDefaultUser().getTimeZone();
179 }
180
181 public String getVirtualHostname() {
182 try {
183 VirtualHost virtualHost =
184 VirtualHostLocalServiceUtil.fetchVirtualHost(getCompanyId(), 0);
185
186 if (virtualHost == null) {
187 return StringPool.BLANK;
188 }
189 else {
190 return virtualHost.getHostname();
191 }
192 }
193 catch (Exception e) {
194 return StringPool.BLANK;
195 }
196 }
197
198 public boolean hasCompanyMx(String emailAddress) throws SystemException {
199 emailAddress = emailAddress.trim().toLowerCase();
200
201 int pos = emailAddress.indexOf(CharPool.AT);
202
203 if (pos == -1) {
204 return false;
205 }
206
207 String mx = emailAddress.substring(pos + 1);
208
209 if (mx.equals(getMx())) {
210 return true;
211 }
212
213 String[] mailHostNames = PrefsPropsUtil.getStringArray(
214 getCompanyId(), PropsKeys.ADMIN_MAIL_HOST_NAMES,
215 StringPool.NEW_LINE, PropsValues.ADMIN_MAIL_HOST_NAMES);
216
217 for (int i = 0; i < mailHostNames.length; i++) {
218 if (mx.equalsIgnoreCase(mailHostNames[i])) {
219 return true;
220 }
221 }
222
223 return false;
224 }
225
226 public boolean isAutoLogin() throws SystemException {
227 return PrefsPropsUtil.getBoolean(
228 getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
229 PropsValues.COMPANY_SECURITY_AUTO_LOGIN);
230 }
231
232 public boolean isSendPassword() throws SystemException {
233 return PrefsPropsUtil.getBoolean(
234 getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
235 PropsValues.COMPANY_SECURITY_SEND_PASSWORD);
236 }
237
238 public boolean isSendPasswordResetLink() throws SystemException {
239 return PrefsPropsUtil.getBoolean(
240 getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK,
241 PropsValues.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK);
242 }
243
244 public boolean isSiteLogo() throws SystemException {
245 return PrefsPropsUtil.getBoolean(
246 getCompanyId(), PropsKeys.COMPANY_SECURITY_SITE_LOGO,
247 PropsValues.COMPANY_SECURITY_SITE_LOGO);
248 }
249
250 public boolean isStrangers() throws SystemException {
251 return PrefsPropsUtil.getBoolean(
252 getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS,
253 PropsValues.COMPANY_SECURITY_STRANGERS);
254 }
255
256 public boolean isStrangersVerify() throws SystemException {
257 return PrefsPropsUtil.getBoolean(
258 getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
259 PropsValues.COMPANY_SECURITY_STRANGERS_VERIFY);
260 }
261
262 public boolean isStrangersWithMx() throws SystemException {
263 return PrefsPropsUtil.getBoolean(
264 getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
265 PropsValues.COMPANY_SECURITY_STRANGERS_WITH_MX);
266 }
267
268 @Override
269 public void setKey(String key) {
270 _keyObj = null;
271
272 super.setKey(key);
273 }
274
275 @Override
276 public void setKeyObj(Key keyObj) {
277 _keyObj = keyObj;
278 }
279
280 @CacheField
281 private Key _keyObj;
282
283 }