001    /**
002     * Copyright (c) 2000-2010 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.PropsKeys;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Account;
024    import com.liferay.portal.model.Company;
025    import com.liferay.portal.model.CompanyConstants;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.model.Shard;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.service.AccountLocalServiceUtil;
030    import com.liferay.portal.service.GroupLocalServiceUtil;
031    import com.liferay.portal.service.ShardLocalServiceUtil;
032    import com.liferay.portal.service.UserLocalServiceUtil;
033    import com.liferay.portal.util.PrefsPropsUtil;
034    import com.liferay.portal.util.PropsValues;
035    
036    import java.security.Key;
037    
038    import java.util.Locale;
039    import java.util.TimeZone;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class CompanyImpl extends CompanyModelImpl implements Company {
045    
046            public CompanyImpl() {
047            }
048    
049            public int compareTo(Company company) {
050                    String webId1 = getWebId();
051                    String webId2 = company.getWebId();
052    
053                    if (webId1.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
054                            return -1;
055                    }
056                    else if (webId2.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
057                            return 1;
058                    }
059                    else {
060                            return webId1.compareTo(webId2);
061                    }
062            }
063    
064            public Account getAccount() throws PortalException, SystemException {
065                    return AccountLocalServiceUtil.getAccount(
066                            getCompanyId(), getAccountId());
067            }
068    
069            public String getAdminName() {
070                    return "Administrator";
071            }
072    
073            public String getAuthType() throws SystemException {
074                    return PrefsPropsUtil.getString(
075                            getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTH_TYPE,
076                            PropsValues.COMPANY_SECURITY_AUTH_TYPE);
077            }
078    
079            public User getDefaultUser() throws PortalException, SystemException {
080                    return UserLocalServiceUtil.getDefaultUser(getCompanyId());
081            }
082    
083            public String getDefaultWebId() {
084                    return PropsValues.COMPANY_DEFAULT_WEB_ID;
085            }
086    
087            public String getEmailAddress() {
088    
089                    // Primary email address
090    
091                    return "admin@" + getMx();
092            }
093    
094            public Group getGroup() throws PortalException, SystemException {
095                    if (getCompanyId() > CompanyConstants.SYSTEM) {
096                            return GroupLocalServiceUtil.getCompanyGroup(getCompanyId());
097                    }
098    
099                    return new GroupImpl();
100            }
101    
102            public Key getKeyObj() {
103                    if (_keyObj == null) {
104                            String key = getKey();
105    
106                            if (Validator.isNotNull(key)) {
107                                    _keyObj = (Key)Base64.stringToObject(key);
108                            }
109                    }
110    
111                    return _keyObj;
112            }
113    
114            public Locale getLocale() throws PortalException, SystemException {
115                    return getDefaultUser().getLocale();
116            }
117    
118            public String getName() throws PortalException, SystemException {
119                    return getAccount().getName();
120            }
121    
122            public String getShardName() throws PortalException, SystemException {
123                    Shard shard = ShardLocalServiceUtil.getShard(
124                            Company.class.getName(), getCompanyId());
125    
126                    return shard.getName();
127            }
128    
129            public String getShortName() throws PortalException, SystemException {
130                    return getName();
131            }
132    
133            public TimeZone getTimeZone() throws PortalException, SystemException {
134                    return getDefaultUser().getTimeZone();
135            }
136    
137            public boolean hasCompanyMx(String emailAddress)
138                    throws SystemException {
139    
140                    emailAddress = emailAddress.trim().toLowerCase();
141    
142                    int pos = emailAddress.indexOf(StringPool.AT);
143    
144                    if (pos == -1) {
145                            return false;
146                    }
147    
148                    String mx = emailAddress.substring(pos + 1, emailAddress.length());
149    
150                    if (mx.equals(getMx())) {
151                            return true;
152                    }
153    
154                    String[] mailHostNames = PrefsPropsUtil.getStringArray(
155                            getCompanyId(), PropsKeys.ADMIN_MAIL_HOST_NAMES,
156                            StringPool.NEW_LINE, PropsValues.ADMIN_MAIL_HOST_NAMES);
157    
158                    for (int i = 0; i < mailHostNames.length; i++) {
159                            if (mx.equalsIgnoreCase(mailHostNames[i])) {
160                                    return true;
161                            }
162                    }
163    
164                    return false;
165            }
166    
167            public boolean isAutoLogin() throws SystemException {
168                    return PrefsPropsUtil.getBoolean(
169                            getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
170                            PropsValues.COMPANY_SECURITY_AUTO_LOGIN);
171            }
172    
173            public boolean isCommunityLogo() throws SystemException {
174                    return PrefsPropsUtil.getBoolean(
175                            getCompanyId(), PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO,
176                            PropsValues.COMPANY_SECURITY_COMMUNITY_LOGO);
177            }
178    
179            public boolean isSendPassword() throws SystemException {
180                    return PrefsPropsUtil.getBoolean(
181                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
182                            PropsValues.COMPANY_SECURITY_SEND_PASSWORD);
183            }
184    
185            public boolean isSendPasswordResetLink() throws SystemException {
186                    return PrefsPropsUtil.getBoolean(
187                            getCompanyId(), PropsKeys.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK,
188                            PropsValues.COMPANY_SECURITY_SEND_PASSWORD_RESET_LINK);
189            }
190    
191            public boolean isStrangers() throws SystemException {
192                    return PrefsPropsUtil.getBoolean(
193                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS,
194                            PropsValues.COMPANY_SECURITY_STRANGERS);
195            }
196    
197            public boolean isStrangersVerify() throws SystemException {
198                    return PrefsPropsUtil.getBoolean(
199                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
200                            PropsValues.COMPANY_SECURITY_STRANGERS_VERIFY);
201            }
202    
203            public boolean isStrangersWithMx() throws SystemException {
204                    return PrefsPropsUtil.getBoolean(
205                            getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
206                            PropsValues.COMPANY_SECURITY_STRANGERS_WITH_MX);
207            }
208    
209            public void setKey(String key) {
210                    _keyObj = null;
211    
212                    super.setKey(key);
213            }
214    
215            public void setKeyObj(Key keyObj) {
216                    _keyObj = keyObj;
217    
218                    super.setKey(Base64.objectToString(keyObj));
219            }
220    
221            private Key _keyObj = null;
222    
223    }