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            @Override
182            public String getVirtualHostname() {
183                    if (Validator.isNotNull(_virtualHostname)) {
184                            return _virtualHostname;
185                    }
186    
187                    try {
188                            VirtualHost virtualHost =
189                                    VirtualHostLocalServiceUtil.fetchVirtualHost(getCompanyId(), 0);
190    
191                            if (virtualHost == null) {
192                                    _virtualHostname = StringPool.BLANK;
193                            }
194                            else {
195                                    _virtualHostname = virtualHost.getHostname();
196                            }
197                    }
198                    catch (Exception e) {
199                            _virtualHostname = StringPool.BLANK;
200                    }
201    
202                    return _virtualHostname;
203            }
204    
205            public boolean hasCompanyMx(String emailAddress) throws SystemException {
206                    emailAddress = emailAddress.trim().toLowerCase();
207    
208                    int pos = emailAddress.indexOf(CharPool.AT);
209    
210                    if (pos == -1) {
211                            return false;
212                    }
213    
214                    String mx = emailAddress.substring(pos + 1);
215    
216                    if (mx.equals(getMx())) {
217                            return true;
218                    }
219    
220                    String[] mailHostNames = PrefsPropsUtil.getStringArray(
221                            getCompanyId(), PropsKeys.ADMIN_MAIL_HOST_NAMES,
222                            StringPool.NEW_LINE, PropsValues.ADMIN_MAIL_HOST_NAMES);
223    
224                    for (int i = 0; i < mailHostNames.length; i++) {
225                            if (mx.equalsIgnoreCase(mailHostNames[i])) {
226                                    return true;
227                            }
228                    }
229    
230                    return false;
231            }
232    
233            public boolean isAutoLogin() throws SystemException {
234                    return PrefsPropsUtil.getBoolean(
235                            getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
236                            PropsValues.COMPANY_SECURITY_AUTO_LOGIN);
237            }
238    
239            public boolean isSendPassword() throws SystemException {
240                    return PrefsPropsUtil.getBoolean(
241                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
242                            PropsValues.COMPANY_SECURITY_SEND_PASSWORD);
243            }
244    
245            public boolean isSendPasswordResetLink() throws SystemException {
246                    return PrefsPropsUtil.getBoolean(
247                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK,
248                            PropsValues.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK);
249            }
250    
251            public boolean isSiteLogo() throws SystemException {
252                    return PrefsPropsUtil.getBoolean(
253                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SITE_LOGO,
254                            PropsValues.COMPANY_SECURITY_SITE_LOGO);
255            }
256    
257            public boolean isStrangers() throws SystemException {
258                    return PrefsPropsUtil.getBoolean(
259                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS,
260                            PropsValues.COMPANY_SECURITY_STRANGERS);
261            }
262    
263            public boolean isStrangersVerify() throws SystemException {
264                    return PrefsPropsUtil.getBoolean(
265                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
266                            PropsValues.COMPANY_SECURITY_STRANGERS_VERIFY);
267            }
268    
269            public boolean isStrangersWithMx() throws SystemException {
270                    return PrefsPropsUtil.getBoolean(
271                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
272                            PropsValues.COMPANY_SECURITY_STRANGERS_WITH_MX);
273            }
274    
275            @Override
276            public void setKey(String key) {
277                    _keyObj = null;
278    
279                    super.setKey(key);
280            }
281    
282            @Override
283            public void setKeyObj(Key keyObj) {
284                    _keyObj = keyObj;
285            }
286    
287            @Override
288            public void setVirtualHostname(String virtualHostname) {
289                    _virtualHostname = virtualHostname;
290            }
291    
292            @CacheField
293            private Key _keyObj;
294    
295            @CacheField
296            private String _virtualHostname;
297    
298    }