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