001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.model.impl;
016    
017    import com.liferay.portal.kernel.bean.AutoEscape;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.Base64;
021    import com.liferay.portal.kernel.util.CharPool;
022    import com.liferay.portal.kernel.util.Http;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.model.Account;
028    import com.liferay.portal.model.CacheField;
029    import com.liferay.portal.model.Company;
030    import com.liferay.portal.model.CompanyConstants;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.model.LayoutSet;
033    import com.liferay.portal.model.Shard;
034    import com.liferay.portal.model.User;
035    import com.liferay.portal.model.VirtualHost;
036    import com.liferay.portal.service.AccountLocalServiceUtil;
037    import com.liferay.portal.service.GroupLocalServiceUtil;
038    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
039    import com.liferay.portal.service.ShardLocalServiceUtil;
040    import com.liferay.portal.service.UserLocalServiceUtil;
041    import com.liferay.portal.service.VirtualHostLocalServiceUtil;
042    import com.liferay.portal.util.PortalUtil;
043    import com.liferay.portal.util.PrefsPropsUtil;
044    import com.liferay.portal.util.PropsValues;
045    
046    import java.security.Key;
047    
048    import java.util.Locale;
049    import java.util.TimeZone;
050    
051    /**
052     * @author Brian Wing Shun Chan
053     */
054    public class CompanyImpl extends CompanyBaseImpl {
055    
056            public CompanyImpl() {
057            }
058    
059            @Override
060            public int compareTo(Company company) {
061                    String webId1 = getWebId();
062                    String webId2 = company.getWebId();
063    
064                    if (webId1.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
065                            return -1;
066                    }
067                    else if (webId2.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
068                            return 1;
069                    }
070                    else {
071                            return webId1.compareTo(webId2);
072                    }
073            }
074    
075            @Override
076            public Account getAccount() throws PortalException, SystemException {
077                    return AccountLocalServiceUtil.getAccount(
078                            getCompanyId(), getAccountId());
079            }
080    
081            @Override
082            public String getAdminName() {
083                    return "Administrator";
084            }
085    
086            @Override
087            public String getAuthType() throws SystemException {
088                    return PrefsPropsUtil.getString(
089                            getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTH_TYPE,
090                            PropsValues.COMPANY_SECURITY_AUTH_TYPE);
091            }
092    
093            @Override
094            public User getDefaultUser() throws PortalException, SystemException {
095                    return UserLocalServiceUtil.getDefaultUser(getCompanyId());
096            }
097    
098            @Override
099            public String getDefaultWebId() {
100                    return PropsValues.COMPANY_DEFAULT_WEB_ID;
101            }
102    
103            @Override
104            public String getEmailAddress() {
105    
106                    // Primary email address
107    
108                    return "admin@" + getMx();
109            }
110    
111            @Override
112            public Group getGroup() throws PortalException, SystemException {
113                    if (getCompanyId() > CompanyConstants.SYSTEM) {
114                            return GroupLocalServiceUtil.getCompanyGroup(getCompanyId());
115                    }
116    
117                    return new GroupImpl();
118            }
119    
120            @Override
121            public Key getKeyObj() {
122                    if (_keyObj == null) {
123                            String key = getKey();
124    
125                            if (Validator.isNotNull(key)) {
126                                    _keyObj = (Key)Base64.stringToObjectSilent(key);
127                            }
128                    }
129    
130                    return _keyObj;
131            }
132    
133            @Override
134            public Locale getLocale() throws PortalException, SystemException {
135                    return getDefaultUser().getLocale();
136            }
137    
138            @AutoEscape
139            @Override
140            public String getName() throws PortalException, SystemException {
141                    return getAccount().getName();
142            }
143    
144            @Override
145            public String getPortalURL(long groupId)
146                    throws PortalException, SystemException {
147    
148                    String portalURL = PortalUtil.getPortalURL(
149                            getVirtualHostname(), Http.HTTP_PORT, false);
150    
151                    if (groupId <= 0) {
152                            return portalURL;
153                    }
154    
155                    Group group = GroupLocalServiceUtil.getGroup(groupId);
156    
157                    if (group.hasPublicLayouts()) {
158                            LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
159                                    groupId, false);
160    
161                            if (Validator.isNotNull(layoutSet.getVirtualHostname())) {
162                                    portalURL = PortalUtil.getPortalURL(
163                                            layoutSet.getVirtualHostname(), Http.HTTP_PORT, false);
164                            }
165                    }
166                    else if (group.hasPrivateLayouts()) {
167                            LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
168                                    groupId, true);
169    
170                            if (Validator.isNotNull(layoutSet.getVirtualHostname())) {
171                                    portalURL = PortalUtil.getPortalURL(
172                                            layoutSet.getVirtualHostname(), Http.HTTP_PORT, false);
173                            }
174                    }
175    
176                    return portalURL;
177            }
178    
179            @Override
180            public String getShardName() throws PortalException, SystemException {
181                    Shard shard = ShardLocalServiceUtil.getShard(
182                            Company.class.getName(), getCompanyId());
183    
184                    return shard.getName();
185            }
186    
187            @Override
188            public String getShortName() throws PortalException, SystemException {
189                    return getName();
190            }
191    
192            @Override
193            public TimeZone getTimeZone() throws PortalException, SystemException {
194                    return getDefaultUser().getTimeZone();
195            }
196    
197            @Override
198            public String getVirtualHostname() {
199                    if (_virtualHostname != null) {
200                            return _virtualHostname;
201                    }
202    
203                    try {
204                            VirtualHost virtualHost =
205                                    VirtualHostLocalServiceUtil.fetchVirtualHost(getCompanyId(), 0);
206    
207                            if (virtualHost == null) {
208                                    _virtualHostname = StringPool.BLANK;
209                            }
210                            else {
211                                    _virtualHostname = virtualHost.getHostname();
212                            }
213                    }
214                    catch (Exception e) {
215                            _virtualHostname = StringPool.BLANK;
216                    }
217    
218                    return _virtualHostname;
219            }
220    
221            @Override
222            public boolean hasCompanyMx(String emailAddress) throws SystemException {
223                    emailAddress = StringUtil.toLowerCase(emailAddress.trim());
224    
225                    int pos = emailAddress.indexOf(CharPool.AT);
226    
227                    if (pos == -1) {
228                            return false;
229                    }
230    
231                    String mx = emailAddress.substring(pos + 1);
232    
233                    if (mx.equals(getMx())) {
234                            return true;
235                    }
236    
237                    String[] mailHostNames = PrefsPropsUtil.getStringArray(
238                            getCompanyId(), PropsKeys.ADMIN_MAIL_HOST_NAMES,
239                            StringPool.NEW_LINE, PropsValues.ADMIN_MAIL_HOST_NAMES);
240    
241                    for (int i = 0; i < mailHostNames.length; i++) {
242                            if (StringUtil.equalsIgnoreCase(mx, mailHostNames[i])) {
243                                    return true;
244                            }
245                    }
246    
247                    return false;
248            }
249    
250            @Override
251            public boolean isAutoLogin() throws SystemException {
252                    return PrefsPropsUtil.getBoolean(
253                            getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
254                            PropsValues.COMPANY_SECURITY_AUTO_LOGIN);
255            }
256    
257            @Override
258            public boolean isSendPassword() throws SystemException {
259                    return PrefsPropsUtil.getBoolean(
260                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
261                            PropsValues.COMPANY_SECURITY_SEND_PASSWORD);
262            }
263    
264            @Override
265            public boolean isSendPasswordResetLink() throws SystemException {
266                    return PrefsPropsUtil.getBoolean(
267                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK,
268                            PropsValues.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK);
269            }
270    
271            @Override
272            public boolean isSiteLogo() throws SystemException {
273                    return PrefsPropsUtil.getBoolean(
274                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SITE_LOGO,
275                            PropsValues.COMPANY_SECURITY_SITE_LOGO);
276            }
277    
278            @Override
279            public boolean isStrangers() throws SystemException {
280                    return PrefsPropsUtil.getBoolean(
281                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS,
282                            PropsValues.COMPANY_SECURITY_STRANGERS);
283            }
284    
285            @Override
286            public boolean isStrangersVerify() throws SystemException {
287                    return PrefsPropsUtil.getBoolean(
288                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
289                            PropsValues.COMPANY_SECURITY_STRANGERS_VERIFY);
290            }
291    
292            @Override
293            public boolean isStrangersWithMx() throws SystemException {
294                    return PrefsPropsUtil.getBoolean(
295                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
296                            PropsValues.COMPANY_SECURITY_STRANGERS_WITH_MX);
297            }
298    
299            @Override
300            public void setKey(String key) {
301                    _keyObj = null;
302    
303                    super.setKey(key);
304            }
305    
306            @Override
307            public void setKeyObj(Key keyObj) {
308                    _keyObj = keyObj;
309            }
310    
311            @Override
312            public void setVirtualHostname(String virtualHostname) {
313                    _virtualHostname = virtualHostname;
314            }
315    
316            @CacheField
317            private Key _keyObj;
318    
319            @CacheField
320            private String _virtualHostname;
321    
322    }