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 long getGroupId() throws PortalException, SystemException {
122                    Group group = getGroup();
123    
124                    return group.getGroupId();
125            }
126    
127            @Override
128            public Key getKeyObj() {
129                    if (_keyObj == null) {
130                            String key = getKey();
131    
132                            if (Validator.isNotNull(key)) {
133                                    _keyObj = (Key)Base64.stringToObjectSilent(key);
134                            }
135                    }
136    
137                    return _keyObj;
138            }
139    
140            @Override
141            public Locale getLocale() throws PortalException, SystemException {
142                    return getDefaultUser().getLocale();
143            }
144    
145            @AutoEscape
146            @Override
147            public String getName() throws PortalException, SystemException {
148                    return getAccount().getName();
149            }
150    
151            @Override
152            public String getPortalURL(long groupId)
153                    throws PortalException, SystemException {
154    
155                    String portalURL = PortalUtil.getPortalURL(
156                            getVirtualHostname(), Http.HTTP_PORT, false);
157    
158                    if (groupId <= 0) {
159                            return portalURL;
160                    }
161    
162                    Group group = GroupLocalServiceUtil.getGroup(groupId);
163    
164                    if (group.hasPublicLayouts()) {
165                            LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
166                                    groupId, false);
167    
168                            if (Validator.isNotNull(layoutSet.getVirtualHostname())) {
169                                    portalURL = PortalUtil.getPortalURL(
170                                            layoutSet.getVirtualHostname(), Http.HTTP_PORT, false);
171                            }
172                    }
173                    else if (group.hasPrivateLayouts()) {
174                            LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
175                                    groupId, true);
176    
177                            if (Validator.isNotNull(layoutSet.getVirtualHostname())) {
178                                    portalURL = PortalUtil.getPortalURL(
179                                            layoutSet.getVirtualHostname(), Http.HTTP_PORT, false);
180                            }
181                    }
182    
183                    return portalURL;
184            }
185    
186            @Override
187            public String getShardName() throws PortalException, SystemException {
188                    Shard shard = ShardLocalServiceUtil.getShard(
189                            Company.class.getName(), getCompanyId());
190    
191                    return shard.getName();
192            }
193    
194            @Override
195            public String getShortName() throws PortalException, SystemException {
196                    return getName();
197            }
198    
199            @Override
200            public TimeZone getTimeZone() throws PortalException, SystemException {
201                    return getDefaultUser().getTimeZone();
202            }
203    
204            @Override
205            public String getVirtualHostname() {
206                    if (_virtualHostname != null) {
207                            return _virtualHostname;
208                    }
209    
210                    VirtualHost virtualHost = null;
211    
212                    try {
213                            virtualHost = VirtualHostLocalServiceUtil.fetchVirtualHost(
214                                    getCompanyId(), 0);
215                    }
216                    catch (Exception e) {
217                    }
218    
219                    if (virtualHost == null) {
220                            return StringPool.BLANK;
221                    }
222    
223                    _virtualHostname = virtualHost.getHostname();
224    
225                    return _virtualHostname;
226            }
227    
228            @Override
229            public boolean hasCompanyMx(String emailAddress) throws SystemException {
230                    emailAddress = StringUtil.toLowerCase(emailAddress.trim());
231    
232                    int pos = emailAddress.indexOf(CharPool.AT);
233    
234                    if (pos == -1) {
235                            return false;
236                    }
237    
238                    String mx = emailAddress.substring(pos + 1);
239    
240                    if (mx.equals(getMx())) {
241                            return true;
242                    }
243    
244                    String[] mailHostNames = PrefsPropsUtil.getStringArray(
245                            getCompanyId(), PropsKeys.ADMIN_MAIL_HOST_NAMES,
246                            StringPool.NEW_LINE, PropsValues.ADMIN_MAIL_HOST_NAMES);
247    
248                    for (int i = 0; i < mailHostNames.length; i++) {
249                            if (StringUtil.equalsIgnoreCase(mx, mailHostNames[i])) {
250                                    return true;
251                            }
252                    }
253    
254                    return false;
255            }
256    
257            @Override
258            public boolean isAutoLogin() throws SystemException {
259                    return PrefsPropsUtil.getBoolean(
260                            getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
261                            PropsValues.COMPANY_SECURITY_AUTO_LOGIN);
262            }
263    
264            @Override
265            public boolean isSendPassword() throws SystemException {
266                    return PrefsPropsUtil.getBoolean(
267                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
268                            PropsValues.COMPANY_SECURITY_SEND_PASSWORD);
269            }
270    
271            @Override
272            public boolean isSendPasswordResetLink() throws SystemException {
273                    return PrefsPropsUtil.getBoolean(
274                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK,
275                            PropsValues.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK);
276            }
277    
278            @Override
279            public boolean isSiteLogo() throws SystemException {
280                    return PrefsPropsUtil.getBoolean(
281                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SITE_LOGO,
282                            PropsValues.COMPANY_SECURITY_SITE_LOGO);
283            }
284    
285            @Override
286            public boolean isStrangers() throws SystemException {
287                    return PrefsPropsUtil.getBoolean(
288                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS,
289                            PropsValues.COMPANY_SECURITY_STRANGERS);
290            }
291    
292            @Override
293            public boolean isStrangersVerify() throws SystemException {
294                    return PrefsPropsUtil.getBoolean(
295                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
296                            PropsValues.COMPANY_SECURITY_STRANGERS_VERIFY);
297            }
298    
299            @Override
300            public boolean isStrangersWithMx() throws SystemException {
301                    return PrefsPropsUtil.getBoolean(
302                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
303                            PropsValues.COMPANY_SECURITY_STRANGERS_WITH_MX);
304            }
305    
306            @Override
307            public void setKey(String key) {
308                    _keyObj = null;
309    
310                    super.setKey(key);
311            }
312    
313            @Override
314            public void setKeyObj(Key keyObj) {
315                    _keyObj = keyObj;
316            }
317    
318            @Override
319            public void setVirtualHostname(String virtualHostname) {
320                    _virtualHostname = virtualHostname;
321            }
322    
323            @CacheField
324            private Key _keyObj;
325    
326            @CacheField
327            private String _virtualHostname;
328    
329    }