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.service.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.UnicodeProperties;
020    import com.liferay.portal.model.Account;
021    import com.liferay.portal.model.Address;
022    import com.liferay.portal.model.Company;
023    import com.liferay.portal.model.EmailAddress;
024    import com.liferay.portal.model.Phone;
025    import com.liferay.portal.model.RoleConstants;
026    import com.liferay.portal.model.Website;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.service.base.CompanyServiceBaseImpl;
029    import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
030    
031    import java.io.InputStream;
032    
033    import java.util.List;
034    
035    /**
036     * The implementation of the company remote service. Each company refers to a
037     * separate portal instance.
038     *
039     * @author Brian Wing Shun Chan
040     * @author Julio Camarero
041     */
042    public class CompanyServiceImpl extends CompanyServiceBaseImpl {
043    
044            /**
045             * Adds a company.
046             *
047             * @param  webId the company's web domain
048             * @param  virtualHost the company's virtual host name
049             * @param  mx the company's mail domain
050             * @param  shardName the company's shard
051             * @param  system whether the company is the very first company (i.e., the
052             * @param  maxUsers the max number of company users (optionally
053             *         <code>0</code>)
054             * @param  active whether the company is active
055             * @return the company
056             * @throws PortalException if the web domain, virtual host name, or mail
057             *         domain was invalid or if the user was not a universal
058             *         administrator
059             * @throws SystemException if a system exception occurred
060             */
061            public Company addCompany(
062                            String webId, String virtualHost, String mx, String shardName,
063                            boolean system, int maxUsers, boolean active)
064                    throws PortalException, SystemException {
065    
066                    if (!getPermissionChecker().isOmniadmin()) {
067                            throw new PrincipalException();
068                    }
069    
070                    return companyLocalService.addCompany(
071                            webId, virtualHost, mx, shardName, system, maxUsers, active);
072            }
073    
074            /**
075             * Deletes the company's logo.
076             *
077             * @param  companyId the primary key of the company
078             * @throws PortalException if the company with the primary key could not be
079             *         found or if the company's logo could not be found or if the user
080             *         was not an administrator
081             * @throws SystemException if a system exception occurred
082             */
083            public void deleteLogo(long companyId)
084                    throws PortalException, SystemException {
085    
086                    if (!roleLocalService.hasUserRole(
087                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
088    
089                            throw new PrincipalException();
090                    }
091    
092                    companyLocalService.deleteLogo(companyId);
093            }
094    
095            /**
096             * Returns the company with the primary key.
097             *
098             * @param  companyId the primary key of the company
099             * @return Returns the company with the primary key
100             * @throws PortalException if a company with the primary key could not be
101             *         found
102             * @throws SystemException if a system exception occurred
103             */
104            public Company getCompanyById(long companyId)
105                    throws PortalException, SystemException {
106    
107                    return companyLocalService.getCompanyById(companyId);
108            }
109    
110            /**
111             * Returns the company with the logo.
112             *
113             * @param  logoId the ID of the company's logo
114             * @return Returns the company with the logo
115             * @throws PortalException if the company with the logo could not be found
116             * @throws SystemException if a system exception occurred
117             */
118            public Company getCompanyByLogoId(long logoId)
119                    throws PortalException, SystemException {
120    
121                    return companyLocalService.getCompanyByLogoId(logoId);
122            }
123    
124            /**
125             * Returns the company with the mail domian.
126             *
127             * @param  mx the company's mail domain
128             * @return Returns the company with the mail domain
129             * @throws PortalException if the company with the mail domain could not be
130             *         found
131             * @throws SystemException if a system exception occurred
132             */
133            public Company getCompanyByMx(String mx)
134                    throws PortalException, SystemException {
135    
136                    return companyLocalService.getCompanyByMx(mx);
137            }
138    
139            /**
140             * Returns the company with the virtual host name.
141             *
142             * @param  virtualHost the company's virtual host name
143             * @return Returns the company with the virtual host name
144             * @throws PortalException if the company with the virtual host name could
145             *         not be found or if the virtual host was not associated with a
146             *         company
147             * @throws SystemException if a system exception occurred
148             */
149            public Company getCompanyByVirtualHost(String virtualHost)
150                    throws PortalException, SystemException {
151    
152                    return companyLocalService.getCompanyByVirtualHost(virtualHost);
153            }
154    
155            /**
156             * Returns the company with the web domain.
157             *
158             * @param  webId the company's web domain
159             * @return Returns the company with the web domain
160             * @throws PortalException if the company with the web domain could not be
161             *         found
162             * @throws SystemException if a system exception occurred
163             */
164            public Company getCompanyByWebId(String webId)
165                    throws PortalException, SystemException {
166    
167                    return companyLocalService.getCompanyByWebId(webId);
168            }
169    
170            /**
171             * Removes the values that match the keys of the company's preferences.
172             *
173             * This method is called by {@link
174             * com.liferay.portlet.portalsettings.action.EditLDAPServerAction} remotely
175             * through {@link com.liferay.portal.service.CompanyService}.
176             *
177             * @param  companyId the primary key of the company
178             * @param  keys the company's preferences keys to be remove
179             * @throws PortalException if the user was not an administrator
180             * @throws SystemException if a system exception occurred
181             */
182            public void removePreferences(long companyId, String[] keys)
183                    throws PortalException, SystemException {
184    
185                    if (!roleLocalService.hasUserRole(
186                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
187    
188                            throw new PrincipalException();
189                    }
190    
191                    companyLocalService.removePreferences(companyId, keys);
192            }
193    
194            /**
195             * Updates the company
196             *
197             * @param  companyId the primary key of the company
198             * @param  virtualHost the company's virtual host name
199             * @param  mx the company's mail domain
200             * @param  maxUsers the max number of company users (optionally
201             *         <code>0</code>)
202             * @param  active whether the company is active
203             * @return the company with the primary key
204             * @throws PortalException if a company with the primary key could not be
205             *         found or if the new information was invalid or if the user was
206             *         not a universal administrator
207             * @throws SystemException if a system exception occurred
208             */
209            public Company updateCompany(
210                            long companyId, String virtualHost, String mx, int maxUsers,
211                            boolean active)
212                    throws PortalException, SystemException {
213    
214                    if (!getPermissionChecker().isOmniadmin()) {
215                            throw new PrincipalException();
216                    }
217    
218                    return companyLocalService.updateCompany(
219                            companyId, virtualHost, mx, maxUsers, active);
220            }
221    
222            /**
223             * Updates the company with additional account information.
224             *
225             * @param  companyId the primary key of the company
226             * @param  virtualHost the company's virtual host name
227             * @param  mx the company's mail domain
228             * @param  homeURL the company's home URL (optionally <code>null</code>)
229             * @param  name the company's account name (optionally <code>null</code>)
230             * @param  legalName the company's account legal name (optionally
231             *         <code>null</code>)
232             * @param  legalId the company's account legal ID (optionally
233             *         <code>null</code>)
234             * @param  legalType the company's account legal type (optionally
235             *         <code>null</code>)
236             * @param  sicCode the company's account SIC code (optionally
237             *         <code>null</code>)
238             * @param  tickerSymbol the company's account ticker symbol (optionally
239             *         <code>null</code>)
240             * @param  industry the the company's account industry (optionally
241             *         <code>null</code>)
242             * @param  type the company's account type (optionally <code>null</code>)
243             * @param  size the company's account size (optionally <code>null</code>)
244             * @return the the company with the primary key
245             * @throws PortalException if a company with the primary key could not be
246             *         found or if the new information was invalid or if the user was
247             *         not an administrator
248             * @throws SystemException if a system exception occurred
249             */
250            public Company updateCompany(
251                            long companyId, String virtualHost, String mx, String homeURL,
252                            String name, String legalName, String legalId, String legalType,
253                            String sicCode, String tickerSymbol, String industry, String type,
254                            String size)
255                    throws PortalException, SystemException {
256    
257                    if (!roleLocalService.hasUserRole(
258                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
259    
260                            throw new PrincipalException();
261                    }
262    
263                    return companyLocalService.updateCompany(
264                            companyId, virtualHost, mx, homeURL, name, legalName, legalId,
265                            legalType, sicCode, tickerSymbol, industry, type, size);
266            }
267    
268            /**
269             * Updates the company with addition information.
270             *
271             * @param  companyId the primary key of the company
272             * @param  virtualHost the company's virtual host name
273             * @param  mx the company's mail domain
274             * @param  homeURL the company's home URL (optionally <code>null</code>)
275             * @param  name the company's account name (optionally <code>null</code>)
276             * @param  legalName the company's account legal name (optionally
277             *         <code>null</code>)
278             * @param  legalId the company's accout legal ID (optionally
279             *         <code>null</code>)
280             * @param  legalType the company's account legal type (optionally
281             *         <code>null</code>)
282             * @param  sicCode the company's account SIC code (optionally
283             *         <code>null</code>)
284             * @param  tickerSymbol the company's account ticker symbol (optionally
285             *         <code>null</code>)
286             * @param  industry the the company's account industry (optionally
287             *         <code>null</code>)
288             * @param  type the company's account type (optionally <code>null</code>)
289             * @param  size the company's account size (optionally <code>null</code>)
290             * @param  languageId the ID of the company's default user's language
291             * @param  timeZoneId the ID of the company's default user's time zone
292             * @param  addresses the company's addresses
293             * @param  emailAddresses the company's email addresses
294             * @param  phones the company's phone numbers
295             * @param  websites the company's websites
296             * @param  properties the company's properties
297             * @return the company with the primary key
298             * @throws PortalException the company with the primary key could not be
299             *         found or if the new information was invalid or if the user was
300             *         not an administrator
301             * @throws SystemException if a system exception occurred
302             */
303            public Company updateCompany(
304                            long companyId, String virtualHost, String mx, String homeURL,
305                            String name, String legalName, String legalId, String legalType,
306                            String sicCode, String tickerSymbol, String industry, String type,
307                            String size, String languageId, String timeZoneId,
308                            List<Address> addresses, List<EmailAddress> emailAddresses,
309                            List<Phone> phones, List<Website> websites,
310                            UnicodeProperties properties)
311                    throws PortalException, SystemException {
312    
313                    Company company = updateCompany(
314                            companyId, virtualHost, mx, homeURL, name, legalName, legalId,
315                            legalType, sicCode, tickerSymbol, industry, type, size);
316    
317                    updateDisplay(company.getCompanyId(), languageId, timeZoneId);
318    
319                    updatePreferences(company.getCompanyId(), properties);
320    
321                    UsersAdminUtil.updateAddresses(
322                            Account.class.getName(), company.getAccountId(), addresses);
323    
324                    UsersAdminUtil.updateEmailAddresses(
325                            Account.class.getName(), company.getAccountId(), emailAddresses);
326    
327                    UsersAdminUtil.updatePhones(
328                            Account.class.getName(), company.getAccountId(), phones);
329    
330                    UsersAdminUtil.updateWebsites(
331                            Account.class.getName(), company.getAccountId(), websites);
332    
333                    return company;
334            }
335    
336            /**
337             * Update the company's display.
338             *
339             * @param  companyId the primary key of the company
340             * @param  languageId the ID of the company's default user's language
341             * @param  timeZoneId the ID of the company's default user's time zone
342             * @throws PortalException if the company's default user could not be found
343             *         or if the user was not an administrator
344             * @throws SystemException if a system exception occurred
345             */
346            public void updateDisplay(
347                            long companyId, String languageId, String timeZoneId)
348                    throws PortalException, SystemException {
349    
350                    if (!roleLocalService.hasUserRole(
351                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
352    
353                            throw new PrincipalException();
354                    }
355    
356                    companyLocalService.updateDisplay(companyId, languageId, timeZoneId);
357            }
358    
359            /**
360             * Updates the company's logo.
361             *
362             * @param  companyId the primary key of the company
363             * @param  inputStream the input stream of the company's logo image
364             * @return the company with the primary key
365             * @throws PortalException if the company's logo ID could not be found or if
366             *         the logo's image was corrupted or if the user was an
367             *         administrator
368             * @throws SystemException if a system exception occurred
369             */
370            public Company updateLogo(long companyId, InputStream inputStream)
371                    throws PortalException, SystemException {
372    
373                    if (!roleLocalService.hasUserRole(
374                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
375    
376                            throw new PrincipalException();
377                    }
378    
379                    return companyLocalService.updateLogo(companyId, inputStream);
380            }
381    
382            /**
383             * Updates the company's preferences. The company's default properties are
384             * found in portal.properties.
385             *
386             * @param  companyId the primary key of the company
387             * @param  properties the company's properties. See {@link
388             *         com.liferay.portal.kernel.util.UnicodeProperties}
389             * @throws PortalException if the user was not an administrator
390             * @throws SystemException if a system exception occurred
391             */
392            public void updatePreferences(long companyId, UnicodeProperties properties)
393                    throws PortalException, SystemException {
394    
395                    if (!roleLocalService.hasUserRole(
396                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
397    
398                            throw new PrincipalException();
399                    }
400    
401                    companyLocalService.updatePreferences(companyId, properties);
402            }
403    
404            /**
405             * Updates the company's security properties.
406             *
407             * @param  companyId the primary key of the company
408             * @param  authType the company's method of authenticating users
409             * @param  autoLogin whether to allow users to select the "remember me"
410             *         feature
411             * @param  sendPassword whether to allow users to ask the company to send
412             *         their passwords
413             * @param  strangers whether to allow strangers to create accounts to
414             *         register themselves in the company
415             * @param  strangersWithMx whether to allow strangers to create accounts
416             *         with email addresses that match the company mail suffix
417             * @param  strangersVerify whether to require strangers who create accounts
418             *         to be verified via email
419             * @param  siteLogo whether to to allow site administrators to use their own
420             *         logo instead of the enterprise logo
421             * @throws PortalException if the user was not an administrator
422             * @throws SystemException if a system exception occurred
423             */
424            public void updateSecurity(
425                            long companyId, String authType, boolean autoLogin,
426                            boolean sendPassword, boolean strangers, boolean strangersWithMx,
427                            boolean strangersVerify, boolean siteLogo)
428                    throws PortalException, SystemException {
429    
430                    if (!roleLocalService.hasUserRole(
431                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
432    
433                            throw new PrincipalException();
434                    }
435    
436                    companyLocalService.updateSecurity(
437                            companyId, authType, autoLogin, sendPassword, strangers,
438                            strangersWithMx, strangersVerify, siteLogo);
439            }
440    
441    }