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