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