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