001    /**
002     * Copyright (c) 2000-present 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.jsonwebservice.JSONWebService;
019    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceMode;
020    import com.liferay.portal.kernel.util.UnicodeProperties;
021    import com.liferay.portal.model.Account;
022    import com.liferay.portal.model.Address;
023    import com.liferay.portal.model.Company;
024    import com.liferay.portal.model.EmailAddress;
025    import com.liferay.portal.model.Phone;
026    import com.liferay.portal.model.RoleConstants;
027    import com.liferay.portal.model.Website;
028    import com.liferay.portal.security.auth.PrincipalException;
029    import com.liferay.portal.security.permission.PermissionChecker;
030    import com.liferay.portal.service.base.CompanyServiceBaseImpl;
031    import com.liferay.portal.util.PrefsPropsUtil;
032    import com.liferay.portlet.ratings.transformer.RatingsDataTransformerUtil;
033    import com.liferay.users.admin.kernel.util.UsersAdminUtil;
034    
035    import java.io.InputStream;
036    
037    import java.util.List;
038    
039    import javax.portlet.PortletPreferences;
040    
041    /**
042     * Provides the local service for accessing, adding, checking, and updating
043     * companies. Its methods include permission checks. Each company refers to a
044     * separate portal instance.
045     *
046     * @author Brian Wing Shun Chan
047     * @author Julio Camarero
048     */
049    @JSONWebService
050    public class CompanyServiceImpl extends CompanyServiceBaseImpl {
051    
052            /**
053             * Adds a company.
054             *
055             * @param  webId the company's web domain
056             * @param  virtualHost the company's virtual host name
057             * @param  mx the company's mail domain
058             * @param  system whether the company is the very first company (i.e., the
059             * @param  maxUsers the max number of company users (optionally
060             *         <code>0</code>)
061             * @param  active whether the company is active
062             * @return the company
063             */
064            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
065            @Override
066            public Company addCompany(
067                            String webId, String virtualHost, String mx, boolean system,
068                            int maxUsers, boolean active)
069                    throws PortalException {
070    
071                    PermissionChecker permissionChecker = getPermissionChecker();
072    
073                    if (!permissionChecker.isOmniadmin()) {
074                            throw new PrincipalException.MustBeOmniadmin(permissionChecker);
075                    }
076    
077                    return companyLocalService.addCompany(
078                            webId, virtualHost, mx, system, maxUsers, active);
079            }
080    
081            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
082            @Override
083            public Company deleteCompany(long companyId) throws PortalException {
084                    PermissionChecker permissionChecker = getPermissionChecker();
085    
086                    if (!permissionChecker.isOmniadmin()) {
087                            throw new PrincipalException.MustBeOmniadmin(permissionChecker);
088                    }
089    
090                    return companyLocalService.deleteCompany(companyId);
091            }
092    
093            /**
094             * Deletes the company's logo.
095             *
096             * @param companyId the primary key of the company
097             */
098            @Override
099            public void deleteLogo(long companyId) throws PortalException {
100                    if (!roleLocalService.hasUserRole(
101                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
102    
103                            throw new PrincipalException();
104                    }
105    
106                    companyLocalService.deleteLogo(companyId);
107            }
108    
109            /**
110             * Returns the company with the primary key.
111             *
112             * @param  companyId the primary key of the company
113             * @return Returns the company with the primary key
114             */
115            @Override
116            public Company getCompanyById(long companyId) throws PortalException {
117                    return companyLocalService.getCompanyById(companyId);
118            }
119    
120            /**
121             * Returns the company with the logo.
122             *
123             * @param  logoId the ID of the company's logo
124             * @return Returns the company with the logo
125             */
126            @Override
127            public Company getCompanyByLogoId(long logoId) throws PortalException {
128                    return companyLocalService.getCompanyByLogoId(logoId);
129            }
130    
131            /**
132             * Returns the company with the mail domian.
133             *
134             * @param  mx the company's mail domain
135             * @return Returns the company with the mail domain
136             */
137            @Override
138            public Company getCompanyByMx(String mx) throws PortalException {
139                    return companyLocalService.getCompanyByMx(mx);
140            }
141    
142            /**
143             * Returns the company with the virtual host name.
144             *
145             * @param  virtualHost the company's virtual host name
146             * @return Returns the company with the virtual host name
147             */
148            @Override
149            public Company getCompanyByVirtualHost(String virtualHost)
150                    throws PortalException {
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             */
161            @Override
162            public Company getCompanyByWebId(String webId) throws PortalException {
163                    return companyLocalService.getCompanyByWebId(webId);
164            }
165    
166            /**
167             * Removes the values that match the keys of the company's preferences.
168             *
169             * This method is called by {@link
170             * com.liferay.portlet.portalsettings.action.EditLDAPServerAction} remotely
171             * through {@link com.liferay.portal.service.CompanyService}.
172             *
173             * @param companyId the primary key of the company
174             * @param keys the company's preferences keys to be remove
175             */
176            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
177            @Override
178            public void removePreferences(long companyId, String[] keys)
179                    throws PortalException {
180    
181                    if (!roleLocalService.hasUserRole(
182                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
183    
184                            throw new PrincipalException();
185                    }
186    
187                    companyLocalService.removePreferences(companyId, keys);
188            }
189    
190            /**
191             * Updates the company
192             *
193             * @param  companyId the primary key of the company
194             * @param  virtualHost the company's virtual host name
195             * @param  mx the company's mail domain
196             * @param  maxUsers the max number of company users (optionally
197             *         <code>0</code>)
198             * @param  active whether the company is active
199             * @return the company with the primary key
200             */
201            @Override
202            public Company updateCompany(
203                            long companyId, String virtualHost, String mx, int maxUsers,
204                            boolean active)
205                    throws PortalException {
206    
207                    PermissionChecker permissionChecker = getPermissionChecker();
208    
209                    if (!permissionChecker.isOmniadmin()) {
210                            throw new PrincipalException.MustBeOmniadmin(permissionChecker);
211                    }
212    
213                    return companyLocalService.updateCompany(
214                            companyId, virtualHost, mx, maxUsers, active);
215            }
216    
217            /**
218             * Updates the company with additional account information.
219             *
220             * @param  companyId the primary key of the company
221             * @param  virtualHost the company's virtual host name
222             * @param  mx the company's mail domain
223             * @param  homeURL the company's home URL (optionally <code>null</code>)
224             * @param  logo whether to update the company's logo
225             * @param  logoBytes the new logo image data
226             * @param  name the company's account name (optionally <code>null</code>)
227             * @param  legalName the company's account legal name (optionally
228             *         <code>null</code>)
229             * @param  legalId the company's account legal ID (optionally
230             *         <code>null</code>)
231             * @param  legalType the company's account legal type (optionally
232             *         <code>null</code>)
233             * @param  sicCode the company's account SIC code (optionally
234             *         <code>null</code>)
235             * @param  tickerSymbol the company's account ticker symbol (optionally
236             *         <code>null</code>)
237             * @param  industry the the company's account industry (optionally
238             *         <code>null</code>)
239             * @param  type the company's account type (optionally <code>null</code>)
240             * @param  size the company's account size (optionally <code>null</code>)
241             * @return the the company with the primary key
242             */
243            @Override
244            public Company updateCompany(
245                            long companyId, String virtualHost, String mx, String homeURL,
246                            boolean logo, byte[] logoBytes, String name, String legalName,
247                            String legalId, String legalType, String sicCode,
248                            String tickerSymbol, String industry, String type, String size)
249                    throws PortalException {
250    
251                    if (!roleLocalService.hasUserRole(
252                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
253    
254                            throw new PrincipalException();
255                    }
256    
257                    return companyLocalService.updateCompany(
258                            companyId, virtualHost, mx, homeURL, logo, logoBytes, name,
259                            legalName, legalId, legalType, sicCode, tickerSymbol, industry,
260                            type, size);
261            }
262    
263            /**
264             * Updates the company with addition information.
265             *
266             * @param  companyId the primary key of the company
267             * @param  virtualHost the company's virtual host name
268             * @param  mx the company's mail domain
269             * @param  homeURL the company's home URL (optionally <code>null</code>)
270             * @param  logo if the company has a custom logo
271             * @param  logoBytes the new logo image data
272             * @param  name the company's account name (optionally <code>null</code>)
273             * @param  legalName the company's account legal name (optionally
274             *         <code>null</code>)
275             * @param  legalId the company's accout legal ID (optionally
276             *         <code>null</code>)
277             * @param  legalType the company's account legal type (optionally
278             *         <code>null</code>)
279             * @param  sicCode the company's account SIC code (optionally
280             *         <code>null</code>)
281             * @param  tickerSymbol the company's account ticker symbol (optionally
282             *         <code>null</code>)
283             * @param  industry the the company's account industry (optionally
284             *         <code>null</code>)
285             * @param  type the company's account type (optionally <code>null</code>)
286             * @param  size the company's account size (optionally <code>null</code>)
287             * @param  languageId the ID of the company's default user's language
288             * @param  timeZoneId the ID of the company's default user's time zone
289             * @param  addresses the company's addresses
290             * @param  emailAddresses the company's email addresses
291             * @param  phones the company's phone numbers
292             * @param  websites the company's websites
293             * @param  properties the company's properties
294             * @return the company with the primary key
295             */
296            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
297            @Override
298            public Company updateCompany(
299                            long companyId, String virtualHost, String mx, String homeURL,
300                            boolean logo, byte[] logoBytes, String name, String legalName,
301                            String legalId, String legalType, String sicCode,
302                            String tickerSymbol, String industry, String type, String size,
303                            String languageId, String timeZoneId, List<Address> addresses,
304                            List<EmailAddress> emailAddresses, List<Phone> phones,
305                            List<Website> websites, UnicodeProperties properties)
306                    throws PortalException {
307    
308                    PortletPreferences oldCompanyPortletPreferences =
309                            PrefsPropsUtil.getPreferences(companyId);
310    
311                    Company company = updateCompany(
312                            companyId, virtualHost, mx, homeURL, logo, logoBytes, name,
313                            legalName, legalId, legalType, sicCode, tickerSymbol, industry,
314                            type, size);
315    
316                    updateDisplay(company.getCompanyId(), languageId, timeZoneId);
317    
318                    updatePreferences(company.getCompanyId(), properties);
319    
320                    RatingsDataTransformerUtil.transformCompanyRatingsData(
321                            companyId, oldCompanyPortletPreferences, properties);
322    
323                    UsersAdminUtil.updateAddresses(
324                            Account.class.getName(), company.getAccountId(), addresses);
325    
326                    UsersAdminUtil.updateEmailAddresses(
327                            Account.class.getName(), company.getAccountId(), emailAddresses);
328    
329                    UsersAdminUtil.updatePhones(
330                            Account.class.getName(), company.getAccountId(), phones);
331    
332                    UsersAdminUtil.updateWebsites(
333                            Account.class.getName(), company.getAccountId(), websites);
334    
335                    return company;
336            }
337    
338            /**
339             * Updates the company with additional account information.
340             *
341             * @param      companyId the primary key of the company
342             * @param      virtualHost the company's virtual host name
343             * @param      mx the company's mail domain
344             * @param      homeURL the company's home URL (optionally <code>null</code>)
345             * @param      name the company's account name (optionally
346             *             <code>null</code>)
347             * @param      legalName the company's account legal name (optionally
348             *             <code>null</code>)
349             * @param      legalId the company's account legal ID (optionally
350             *             <code>null</code>)
351             * @param      legalType the company's account legal type (optionally
352             *             <code>null</code>)
353             * @param      sicCode the company's account SIC code (optionally
354             *             <code>null</code>)
355             * @param      tickerSymbol the company's account ticker symbol (optionally
356             *             <code>null</code>)
357             * @param      industry the the company's account industry (optionally
358             *             <code>null</code>)
359             * @param      type the company's account type (optionally
360             *             <code>null</code>)
361             * @param      size the company's account size (optionally
362             *             <code>null</code>)
363             * @return     the the company with the primary key
364             * @deprecated As of 7.0.0, replaced by {@link #updateCompany(long, String,
365             *             String, String, boolean, byte[], String, String, String,
366             *             String, String, String, String, String, String)}
367             */
368            @Deprecated
369            @Override
370            public Company updateCompany(
371                            long companyId, String virtualHost, String mx, String homeURL,
372                            String name, String legalName, String legalId, String legalType,
373                            String sicCode, String tickerSymbol, String industry, String type,
374                            String size)
375                    throws PortalException {
376    
377                    return updateCompany(
378                            companyId, virtualHost, mx, homeURL, true, null, name, legalName,
379                            legalId, legalType, sicCode, tickerSymbol, industry, type, size);
380            }
381    
382            /**
383             * Updates the company with addition information.
384             *
385             * @param      companyId the primary key of the company
386             * @param      virtualHost the company's virtual host name
387             * @param      mx the company's mail domain
388             * @param      homeURL the company's home URL (optionally <code>null</code>)
389             * @param      name the company's account name (optionally
390             *             <code>null</code>)
391             * @param      legalName the company's account legal name (optionally
392             *             <code>null</code>)
393             * @param      legalId the company's accout legal ID (optionally
394             *             <code>null</code>)
395             * @param      legalType the company's account legal type (optionally
396             *             <code>null</code>)
397             * @param      sicCode the company's account SIC code (optionally
398             *             <code>null</code>)
399             * @param      tickerSymbol the company's account ticker symbol (optionally
400             *             <code>null</code>)
401             * @param      industry the the company's account industry (optionally
402             *             <code>null</code>)
403             * @param      type the company's account type (optionally
404             *             <code>null</code>)
405             * @param      size the company's account size (optionally
406             *             <code>null</code>)
407             * @param      languageId the ID of the company's default user's language
408             * @param      timeZoneId the ID of the company's default user's time zone
409             * @param      addresses the company's addresses
410             * @param      emailAddresses the company's email addresses
411             * @param      phones the company's phone numbers
412             * @param      websites the company's websites
413             * @param      properties the company's properties
414             * @return     the company with the primary key
415             * @deprecated As of 7.0.0, replaced by {@link #updateCompany(long, String,
416             *             String, String, boolean, byte[], String, String, String,
417             *             String, String, String, String, String, String, String,
418             *             String, List, List, List, List, UnicodeProperties)}
419             */
420            @Deprecated
421            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
422            @Override
423            public Company updateCompany(
424                            long companyId, String virtualHost, String mx, String homeURL,
425                            String name, String legalName, String legalId, String legalType,
426                            String sicCode, String tickerSymbol, String industry, String type,
427                            String size, String languageId, String timeZoneId,
428                            List<Address> addresses, List<EmailAddress> emailAddresses,
429                            List<Phone> phones, List<Website> websites,
430                            UnicodeProperties properties)
431                    throws PortalException {
432    
433                    return updateCompany(
434                            companyId, virtualHost, mx, homeURL, name, legalName, legalId,
435                            legalType, sicCode, tickerSymbol, industry, type, size, languageId,
436                            timeZoneId, addresses, emailAddresses, phones, websites,
437                            properties);
438            }
439    
440            /**
441             * Update the company's display.
442             *
443             * @param companyId the primary key of the company
444             * @param languageId the ID of the company's default user's language
445             * @param timeZoneId the ID of the company's default user's time zone
446             */
447            @Override
448            public void updateDisplay(
449                            long companyId, String languageId, String timeZoneId)
450                    throws PortalException {
451    
452                    if (!roleLocalService.hasUserRole(
453                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
454    
455                            throw new PrincipalException();
456                    }
457    
458                    companyLocalService.updateDisplay(companyId, languageId, timeZoneId);
459            }
460    
461            /**
462             * Updates the company's logo.
463             *
464             * @param  companyId the primary key of the company
465             * @param  bytes the bytes of the company's logo image
466             * @return the company with the primary key
467             */
468            @Override
469            public Company updateLogo(long companyId, byte[] bytes)
470                    throws PortalException {
471    
472                    if (!roleLocalService.hasUserRole(
473                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
474    
475                            throw new PrincipalException();
476                    }
477    
478                    return companyLocalService.updateLogo(companyId, bytes);
479            }
480    
481            /**
482             * Updates the company's logo.
483             *
484             * @param  companyId the primary key of the company
485             * @param  inputStream the input stream of the company's logo image
486             * @return the company with the primary key
487             */
488            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
489            @Override
490            public Company updateLogo(long companyId, InputStream inputStream)
491                    throws PortalException {
492    
493                    if (!roleLocalService.hasUserRole(
494                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
495    
496                            throw new PrincipalException();
497                    }
498    
499                    return companyLocalService.updateLogo(companyId, inputStream);
500            }
501    
502            /**
503             * Updates the company's preferences. The company's default properties are
504             * found in portal.properties.
505             *
506             * @param companyId the primary key of the company
507             * @param properties the company's properties. See {@link UnicodeProperties}
508             */
509            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
510            @Override
511            public void updatePreferences(long companyId, UnicodeProperties properties)
512                    throws PortalException {
513    
514                    if (!roleLocalService.hasUserRole(
515                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
516    
517                            throw new PrincipalException();
518                    }
519    
520                    companyLocalService.updatePreferences(companyId, properties);
521            }
522    
523            /**
524             * Updates the company's security properties.
525             *
526             * @param companyId the primary key of the company
527             * @param authType the company's method of authenticating users
528             * @param autoLogin whether to allow users to select the "remember me"
529             *        feature
530             * @param sendPassword whether to allow users to ask the company to send
531             *        their passwords
532             * @param strangers whether to allow strangers to create accounts to
533             *        register themselves in the company
534             * @param strangersWithMx whether to allow strangers to create accounts with
535             *        email addresses that match the company mail suffix
536             * @param strangersVerify whether to require strangers who create accounts
537             *        to be verified via email
538             * @param siteLogo whether to to allow site administrators to use their own
539             *        logo instead of the enterprise logo
540             */
541            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
542            @Override
543            public void updateSecurity(
544                            long companyId, String authType, boolean autoLogin,
545                            boolean sendPassword, boolean strangers, boolean strangersWithMx,
546                            boolean strangersVerify, boolean siteLogo)
547                    throws PortalException {
548    
549                    if (!roleLocalService.hasUserRole(
550                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
551    
552                            throw new PrincipalException();
553                    }
554    
555                    companyLocalService.updateSecurity(
556                            companyId, authType, autoLogin, sendPassword, strangers,
557                            strangersWithMx, strangersVerify, siteLogo);
558            }
559    
560    }