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.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    /**
050     * @author Brian Wing Shun Chan
051     */
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                    // Primary email address
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)
199                    throws SystemException {
200    
201                    emailAddress = emailAddress.trim().toLowerCase();
202    
203                    int pos = emailAddress.indexOf(CharPool.AT);
204    
205                    if (pos == -1) {
206                            return false;
207                    }
208    
209                    String mx = emailAddress.substring(pos + 1, emailAddress.length());
210    
211                    if (mx.equals(getMx())) {
212                            return true;
213                    }
214    
215                    String[] mailHostNames = PrefsPropsUtil.getStringArray(
216                            getCompanyId(), PropsKeys.ADMIN_MAIL_HOST_NAMES,
217                            StringPool.NEW_LINE, PropsValues.ADMIN_MAIL_HOST_NAMES);
218    
219                    for (int i = 0; i < mailHostNames.length; i++) {
220                            if (mx.equalsIgnoreCase(mailHostNames[i])) {
221                                    return true;
222                            }
223                    }
224    
225                    return false;
226            }
227    
228            public boolean isAutoLogin() throws SystemException {
229                    return PrefsPropsUtil.getBoolean(
230                            getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
231                            PropsValues.COMPANY_SECURITY_AUTO_LOGIN);
232            }
233    
234            public boolean isSendPassword() throws SystemException {
235                    return PrefsPropsUtil.getBoolean(
236                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
237                            PropsValues.COMPANY_SECURITY_SEND_PASSWORD);
238            }
239    
240            public boolean isSendPasswordResetLink() throws SystemException {
241                    return PrefsPropsUtil.getBoolean(
242                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK,
243                            PropsValues.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK);
244            }
245    
246            public boolean isSiteLogo() throws SystemException {
247                    return PrefsPropsUtil.getBoolean(
248                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SITE_LOGO,
249                            PropsValues.COMPANY_SECURITY_SITE_LOGO);
250            }
251    
252            public boolean isStrangers() throws SystemException {
253                    return PrefsPropsUtil.getBoolean(
254                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS,
255                            PropsValues.COMPANY_SECURITY_STRANGERS);
256            }
257    
258            public boolean isStrangersVerify() throws SystemException {
259                    return PrefsPropsUtil.getBoolean(
260                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
261                            PropsValues.COMPANY_SECURITY_STRANGERS_VERIFY);
262            }
263    
264            public boolean isStrangersWithMx() throws SystemException {
265                    return PrefsPropsUtil.getBoolean(
266                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
267                            PropsValues.COMPANY_SECURITY_STRANGERS_WITH_MX);
268            }
269    
270            @Override
271            public void setKey(String key) {
272                    _keyObj = null;
273    
274                    super.setKey(key);
275            }
276    
277            @Override
278            public void setKeyObj(Key keyObj) {
279                    _keyObj = keyObj;
280            }
281    
282            @CacheField
283            private Key _keyObj;
284    
285    }