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