001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.PropsKeys;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Account;
025    import com.liferay.portal.model.CacheField;
026    import com.liferay.portal.model.Company;
027    import com.liferay.portal.model.CompanyConstants;
028    import com.liferay.portal.model.Group;
029    import com.liferay.portal.model.Shard;
030    import com.liferay.portal.model.User;
031    import com.liferay.portal.model.VirtualHost;
032    import com.liferay.portal.service.AccountLocalServiceUtil;
033    import com.liferay.portal.service.GroupLocalServiceUtil;
034    import com.liferay.portal.service.ShardLocalServiceUtil;
035    import com.liferay.portal.service.UserLocalServiceUtil;
036    import com.liferay.portal.service.VirtualHostLocalServiceUtil;
037    import com.liferay.portal.util.PrefsPropsUtil;
038    import com.liferay.portal.util.PropsValues;
039    
040    import java.security.Key;
041    
042    import java.util.Locale;
043    import java.util.TimeZone;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public class CompanyImpl extends CompanyBaseImpl {
049    
050            public CompanyImpl() {
051            }
052    
053            @Override
054            public int compareTo(Company company) {
055                    String webId1 = getWebId();
056                    String webId2 = company.getWebId();
057    
058                    if (webId1.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
059                            return -1;
060                    }
061                    else if (webId2.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
062                            return 1;
063                    }
064                    else {
065                            return webId1.compareTo(webId2);
066                    }
067            }
068    
069            public Account getAccount() throws PortalException, SystemException {
070                    return AccountLocalServiceUtil.getAccount(
071                            getCompanyId(), getAccountId());
072            }
073    
074            public String getAdminName() {
075                    return "Administrator";
076            }
077    
078            public String getAuthType() throws SystemException {
079                    return PrefsPropsUtil.getString(
080                            getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTH_TYPE,
081                            PropsValues.COMPANY_SECURITY_AUTH_TYPE);
082            }
083    
084            public User getDefaultUser() throws PortalException, SystemException {
085                    return UserLocalServiceUtil.getDefaultUser(getCompanyId());
086            }
087    
088            public String getDefaultWebId() {
089                    return PropsValues.COMPANY_DEFAULT_WEB_ID;
090            }
091    
092            public String getEmailAddress() {
093    
094                    // Primary email address
095    
096                    return "admin@" + getMx();
097            }
098    
099            public Group getGroup() throws PortalException, SystemException {
100                    if (getCompanyId() > CompanyConstants.SYSTEM) {
101                            return GroupLocalServiceUtil.getCompanyGroup(getCompanyId());
102                    }
103    
104                    return new GroupImpl();
105            }
106    
107            @Override
108            public Key getKeyObj() {
109                    if (_keyObj == null) {
110                            String key = getKey();
111    
112                            if (Validator.isNotNull(key)) {
113                                    _keyObj = (Key)Base64.stringToObjectSilent(key);
114                            }
115                    }
116    
117                    return _keyObj;
118            }
119    
120            public Locale getLocale() throws PortalException, SystemException {
121                    return getDefaultUser().getLocale();
122            }
123    
124            public String getName() throws PortalException, SystemException {
125                    return getAccount().getName();
126            }
127    
128            public String getShardName() throws PortalException, SystemException {
129                    Shard shard = ShardLocalServiceUtil.getShard(
130                            Company.class.getName(), getCompanyId());
131    
132                    return shard.getName();
133            }
134    
135            public String getShortName() throws PortalException, SystemException {
136                    return getName();
137            }
138    
139            public TimeZone getTimeZone() throws PortalException, SystemException {
140                    return getDefaultUser().getTimeZone();
141            }
142    
143            public String getVirtualHostname() {
144                    try {
145                            VirtualHost virtualHost =
146                                    VirtualHostLocalServiceUtil.fetchVirtualHost(getCompanyId(), 0);
147    
148                            if (virtualHost == null) {
149                                    return StringPool.BLANK;
150                            }
151                            else {
152                                    return virtualHost.getHostname();
153                            }
154                    }
155                    catch (Exception e) {
156                            return StringPool.BLANK;
157                    }
158            }
159    
160            public boolean hasCompanyMx(String emailAddress)
161                    throws SystemException {
162    
163                    emailAddress = emailAddress.trim().toLowerCase();
164    
165                    int pos = emailAddress.indexOf(CharPool.AT);
166    
167                    if (pos == -1) {
168                            return false;
169                    }
170    
171                    String mx = emailAddress.substring(pos + 1, emailAddress.length());
172    
173                    if (mx.equals(getMx())) {
174                            return true;
175                    }
176    
177                    String[] mailHostNames = PrefsPropsUtil.getStringArray(
178                            getCompanyId(), PropsKeys.ADMIN_MAIL_HOST_NAMES,
179                            StringPool.NEW_LINE, PropsValues.ADMIN_MAIL_HOST_NAMES);
180    
181                    for (int i = 0; i < mailHostNames.length; i++) {
182                            if (mx.equalsIgnoreCase(mailHostNames[i])) {
183                                    return true;
184                            }
185                    }
186    
187                    return false;
188            }
189    
190            public boolean isAutoLogin() throws SystemException {
191                    return PrefsPropsUtil.getBoolean(
192                            getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
193                            PropsValues.COMPANY_SECURITY_AUTO_LOGIN);
194            }
195    
196            public boolean isSendPassword() throws SystemException {
197                    return PrefsPropsUtil.getBoolean(
198                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
199                            PropsValues.COMPANY_SECURITY_SEND_PASSWORD);
200            }
201    
202            public boolean isSendPasswordResetLink() throws SystemException {
203                    return PrefsPropsUtil.getBoolean(
204                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK,
205                            PropsValues.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK);
206            }
207    
208            public boolean isSiteLogo() throws SystemException {
209                    return PrefsPropsUtil.getBoolean(
210                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SITE_LOGO,
211                            PropsValues.COMPANY_SECURITY_SITE_LOGO);
212            }
213    
214            public boolean isStrangers() throws SystemException {
215                    return PrefsPropsUtil.getBoolean(
216                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS,
217                            PropsValues.COMPANY_SECURITY_STRANGERS);
218            }
219    
220            public boolean isStrangersVerify() throws SystemException {
221                    return PrefsPropsUtil.getBoolean(
222                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
223                            PropsValues.COMPANY_SECURITY_STRANGERS_VERIFY);
224            }
225    
226            public boolean isStrangersWithMx() throws SystemException {
227                    return PrefsPropsUtil.getBoolean(
228                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
229                            PropsValues.COMPANY_SECURITY_STRANGERS_WITH_MX);
230            }
231    
232            @Override
233            public void setKey(String key) {
234                    _keyObj = null;
235    
236                    super.setKey(key);
237            }
238    
239            @Override
240            public void setKeyObj(Key keyObj) {
241                    _keyObj = keyObj;
242            }
243    
244            @CacheField
245            private Key _keyObj;
246    
247    }