001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.jsonwebservice.JSONWebService;
020    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceMode;
021    import com.liferay.portal.kernel.util.UnicodeProperties;
022    import com.liferay.portal.model.Account;
023    import com.liferay.portal.model.Address;
024    import com.liferay.portal.model.Company;
025    import com.liferay.portal.model.EmailAddress;
026    import com.liferay.portal.model.Phone;
027    import com.liferay.portal.model.RoleConstants;
028    import com.liferay.portal.model.Website;
029    import com.liferay.portal.security.auth.PrincipalException;
030    import com.liferay.portal.security.permission.PermissionChecker;
031    import com.liferay.portal.service.base.CompanyServiceBaseImpl;
032    import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
033    
034    import java.io.InputStream;
035    
036    import java.util.List;
037    
038    /**
039     * Provides the local service for accessing, adding, checking, and updating
040     * companies. Its methods include permission checks. Each company refers to a
041     * separate portal instance.
042     *
043     * @author Brian Wing Shun Chan
044     * @author Julio Camarero
045     */
046    @JSONWebService
047    public class CompanyServiceImpl extends CompanyServiceBaseImpl {
048    
049            /**
050             * Adds a company.
051             *
052             * @param  webId the company's web domain
053             * @param  virtualHost the company's virtual host name
054             * @param  mx the company's mail domain
055             * @param  shardName the company's shard
056             * @param  system whether the company is the very first company (i.e., the
057             * @param  maxUsers the max number of company users (optionally
058             *         <code>0</code>)
059             * @param  active whether the company is active
060             * @return the company
061             * @throws PortalException if the web domain, virtual host name, or mail
062             *         domain was invalid or if the user was not a universal
063             *         administrator
064             * @throws SystemException if a system exception occurred
065             */
066            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
067            @Override
068            public Company addCompany(
069                            String webId, String virtualHost, String mx, String shardName,
070                            boolean system, int maxUsers, boolean active)
071                    throws PortalException, SystemException {
072    
073                    PermissionChecker permissionChecker = getPermissionChecker();
074    
075                    if (!permissionChecker.isOmniadmin()) {
076                            throw new PrincipalException();
077                    }
078    
079                    return companyLocalService.addCompany(
080                            webId, virtualHost, mx, shardName, system, maxUsers, active);
081            }
082    
083            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
084            @Override
085            public Company deleteCompany(long companyId)
086                    throws PortalException, SystemException {
087    
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             * @throws SystemException if a system exception occurred
105             */
106            @Override
107            public void deleteLogo(long companyId)
108                    throws PortalException, SystemException {
109    
110                    if (!roleLocalService.hasUserRole(
111                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
112    
113                            throw new PrincipalException();
114                    }
115    
116                    companyLocalService.deleteLogo(companyId);
117            }
118    
119            /**
120             * Returns the company with the primary key.
121             *
122             * @param  companyId the primary key of the company
123             * @return Returns the company with the primary key
124             * @throws PortalException if a company with the primary key could not be
125             *         found
126             * @throws SystemException if a system exception occurred
127             */
128            @Override
129            public Company getCompanyById(long companyId)
130                    throws PortalException, SystemException {
131    
132                    return companyLocalService.getCompanyById(companyId);
133            }
134    
135            /**
136             * Returns the company with the logo.
137             *
138             * @param  logoId the ID of the company's logo
139             * @return Returns the company with the logo
140             * @throws PortalException if the company with the logo could not be found
141             * @throws SystemException if a system exception occurred
142             */
143            @Override
144            public Company getCompanyByLogoId(long logoId)
145                    throws PortalException, SystemException {
146    
147                    return companyLocalService.getCompanyByLogoId(logoId);
148            }
149    
150            /**
151             * Returns the company with the mail domian.
152             *
153             * @param  mx the company's mail domain
154             * @return Returns the company with the mail domain
155             * @throws PortalException if the company with the mail domain could not be
156             *         found
157             * @throws SystemException if a system exception occurred
158             */
159            @Override
160            public Company getCompanyByMx(String mx)
161                    throws PortalException, SystemException {
162    
163                    return companyLocalService.getCompanyByMx(mx);
164            }
165    
166            /**
167             * Returns the company with the virtual host name.
168             *
169             * @param  virtualHost the company's virtual host name
170             * @return Returns the company with the virtual host name
171             * @throws PortalException if the company with the virtual host name could
172             *         not be found or if the virtual host was not associated with a
173             *         company
174             * @throws SystemException if a system exception occurred
175             */
176            @Override
177            public Company getCompanyByVirtualHost(String virtualHost)
178                    throws PortalException, SystemException {
179    
180                    return companyLocalService.getCompanyByVirtualHost(virtualHost);
181            }
182    
183            /**
184             * Returns the company with the web domain.
185             *
186             * @param  webId the company's web domain
187             * @return Returns the company with the web domain
188             * @throws PortalException if the company with the web domain could not be
189             *         found
190             * @throws SystemException if a system exception occurred
191             */
192            @Override
193            public Company getCompanyByWebId(String webId)
194                    throws PortalException, SystemException {
195    
196                    return companyLocalService.getCompanyByWebId(webId);
197            }
198    
199            /**
200             * Removes the values that match the keys of the company's preferences.
201             *
202             * This method is called by {@link
203             * com.liferay.portlet.portalsettings.action.EditLDAPServerAction} remotely
204             * through {@link com.liferay.portal.service.CompanyService}.
205             *
206             * @param  companyId the primary key of the company
207             * @param  keys the company's preferences keys to be remove
208             * @throws PortalException if the user was not an administrator
209             * @throws SystemException if a system exception occurred
210             */
211            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
212            @Override
213            public void removePreferences(long companyId, String[] keys)
214                    throws PortalException, SystemException {
215    
216                    if (!roleLocalService.hasUserRole(
217                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
218    
219                            throw new PrincipalException();
220                    }
221    
222                    companyLocalService.removePreferences(companyId, keys);
223            }
224    
225            /**
226             * Updates the company
227             *
228             * @param  companyId the primary key of the company
229             * @param  virtualHost the company's virtual host name
230             * @param  mx the company's mail domain
231             * @param  maxUsers the max number of company users (optionally
232             *         <code>0</code>)
233             * @param  active whether the company is active
234             * @return the company with the primary key
235             * @throws PortalException if a company with the primary key could not be
236             *         found or if the new information was invalid or if the user was
237             *         not a universal administrator
238             * @throws SystemException if a system exception occurred
239             */
240            @Override
241            public Company updateCompany(
242                            long companyId, String virtualHost, String mx, int maxUsers,
243                            boolean active)
244                    throws PortalException, SystemException {
245    
246                    PermissionChecker permissionChecker = getPermissionChecker();
247    
248                    if (!permissionChecker.isOmniadmin()) {
249                            throw new PrincipalException();
250                    }
251    
252                    return companyLocalService.updateCompany(
253                            companyId, virtualHost, mx, maxUsers, active);
254            }
255    
256            /**
257             * Updates the company with additional account information.
258             *
259             * @param  companyId the primary key of the company
260             * @param  virtualHost the company's virtual host name
261             * @param  mx the company's mail domain
262             * @param  homeURL the company's home URL (optionally <code>null</code>)
263             * @param  name the company's account name (optionally <code>null</code>)
264             * @param  legalName the company's account legal name (optionally
265             *         <code>null</code>)
266             * @param  legalId the company's account legal ID (optionally
267             *         <code>null</code>)
268             * @param  legalType the company's account legal type (optionally
269             *         <code>null</code>)
270             * @param  sicCode the company's account SIC code (optionally
271             *         <code>null</code>)
272             * @param  tickerSymbol the company's account ticker symbol (optionally
273             *         <code>null</code>)
274             * @param  industry the the company's account industry (optionally
275             *         <code>null</code>)
276             * @param  type the company's account type (optionally <code>null</code>)
277             * @param  size the company's account size (optionally <code>null</code>)
278             * @return the the company with the primary key
279             * @throws PortalException if a company with the primary key could not be
280             *         found or if the new information was invalid or if the user was
281             *         not an administrator
282             * @throws SystemException if a system exception occurred
283             */
284            @Override
285            public Company updateCompany(
286                            long companyId, String virtualHost, String mx, String homeURL,
287                            String name, String legalName, String legalId, String legalType,
288                            String sicCode, String tickerSymbol, String industry, String type,
289                            String size)
290                    throws PortalException, SystemException {
291    
292                    if (!roleLocalService.hasUserRole(
293                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
294    
295                            throw new PrincipalException();
296                    }
297    
298                    return companyLocalService.updateCompany(
299                            companyId, virtualHost, mx, homeURL, name, legalName, legalId,
300                            legalType, sicCode, tickerSymbol, industry, type, size);
301            }
302    
303            /**
304             * Updates the company with addition information.
305             *
306             * @param  companyId the primary key of the company
307             * @param  virtualHost the company's virtual host name
308             * @param  mx the company's mail domain
309             * @param  homeURL the company's home URL (optionally <code>null</code>)
310             * @param  name the company's account name (optionally <code>null</code>)
311             * @param  legalName the company's account legal name (optionally
312             *         <code>null</code>)
313             * @param  legalId the company's accout legal ID (optionally
314             *         <code>null</code>)
315             * @param  legalType the company's account legal type (optionally
316             *         <code>null</code>)
317             * @param  sicCode the company's account SIC code (optionally
318             *         <code>null</code>)
319             * @param  tickerSymbol the company's account ticker symbol (optionally
320             *         <code>null</code>)
321             * @param  industry the the company's account industry (optionally
322             *         <code>null</code>)
323             * @param  type the company's account type (optionally <code>null</code>)
324             * @param  size the company's account size (optionally <code>null</code>)
325             * @param  languageId the ID of the company's default user's language
326             * @param  timeZoneId the ID of the company's default user's time zone
327             * @param  addresses the company's addresses
328             * @param  emailAddresses the company's email addresses
329             * @param  phones the company's phone numbers
330             * @param  websites the company's websites
331             * @param  properties the company's properties
332             * @return the company with the primary key
333             * @throws PortalException the company with the primary key could not be
334             *         found or if the new information was invalid or if the user was
335             *         not an administrator
336             * @throws SystemException if a system exception occurred
337             */
338            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
339            @Override
340            public Company updateCompany(
341                            long companyId, String virtualHost, String mx, String homeURL,
342                            String name, String legalName, String legalId, String legalType,
343                            String sicCode, String tickerSymbol, String industry, String type,
344                            String size, String languageId, String timeZoneId,
345                            List<Address> addresses, List<EmailAddress> emailAddresses,
346                            List<Phone> phones, List<Website> websites,
347                            UnicodeProperties properties)
348                    throws PortalException, SystemException {
349    
350                    Company company = updateCompany(
351                            companyId, virtualHost, mx, homeURL, name, legalName, legalId,
352                            legalType, sicCode, tickerSymbol, industry, type, size);
353    
354                    updateDisplay(company.getCompanyId(), languageId, timeZoneId);
355    
356                    updatePreferences(company.getCompanyId(), properties);
357    
358                    UsersAdminUtil.updateAddresses(
359                            Account.class.getName(), company.getAccountId(), addresses);
360    
361                    UsersAdminUtil.updateEmailAddresses(
362                            Account.class.getName(), company.getAccountId(), emailAddresses);
363    
364                    UsersAdminUtil.updatePhones(
365                            Account.class.getName(), company.getAccountId(), phones);
366    
367                    UsersAdminUtil.updateWebsites(
368                            Account.class.getName(), company.getAccountId(), websites);
369    
370                    return company;
371            }
372    
373            /**
374             * Update the company's display.
375             *
376             * @param  companyId the primary key of the company
377             * @param  languageId the ID of the company's default user's language
378             * @param  timeZoneId the ID of the company's default user's time zone
379             * @throws PortalException if the company's default user could not be found
380             *         or if the user was not an administrator
381             * @throws SystemException if a system exception occurred
382             */
383            @Override
384            public void updateDisplay(
385                            long companyId, String languageId, String timeZoneId)
386                    throws PortalException, SystemException {
387    
388                    if (!roleLocalService.hasUserRole(
389                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
390    
391                            throw new PrincipalException();
392                    }
393    
394                    companyLocalService.updateDisplay(companyId, languageId, timeZoneId);
395            }
396    
397            /**
398             * Updates the company's logo.
399             *
400             * @param  companyId the primary key of the company
401             * @param  bytes the bytes of the company's logo image
402             * @return the company with the primary key
403             * @throws PortalException if the company's logo ID could not be found or if
404             *         the logo's image was corrupted or if the user was an
405             *         administrator
406             * @throws SystemException if a system exception occurred
407             */
408            @Override
409            public Company updateLogo(long companyId, byte[] bytes)
410                    throws PortalException, SystemException {
411    
412                    if (!roleLocalService.hasUserRole(
413                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
414    
415                            throw new PrincipalException();
416                    }
417    
418                    return companyLocalService.updateLogo(companyId, bytes);
419            }
420    
421            /**
422             * Updates the company's logo.
423             *
424             * @param  companyId the primary key of the company
425             * @param  inputStream the input stream of the company's logo image
426             * @return the company with the primary key
427             * @throws PortalException if the company's logo ID could not be found or if
428             *         the logo's image was corrupted or if the user was an
429             *         administrator
430             * @throws SystemException if a system exception occurred
431             */
432            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
433            @Override
434            public Company updateLogo(long companyId, InputStream inputStream)
435                    throws PortalException, SystemException {
436    
437                    if (!roleLocalService.hasUserRole(
438                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
439    
440                            throw new PrincipalException();
441                    }
442    
443                    return companyLocalService.updateLogo(companyId, inputStream);
444            }
445    
446            /**
447             * Updates the company's preferences. The company's default properties are
448             * found in portal.properties.
449             *
450             * @param  companyId the primary key of the company
451             * @param  properties the company's properties. See {@link
452             *         com.liferay.portal.kernel.util.UnicodeProperties}
453             * @throws PortalException if the user was not an administrator
454             * @throws SystemException if a system exception occurred
455             */
456            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
457            @Override
458            public void updatePreferences(long companyId, UnicodeProperties properties)
459                    throws PortalException, SystemException {
460    
461                    if (!roleLocalService.hasUserRole(
462                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
463    
464                            throw new PrincipalException();
465                    }
466    
467                    companyLocalService.updatePreferences(companyId, properties);
468            }
469    
470            /**
471             * Updates the company's security properties.
472             *
473             * @param  companyId the primary key of the company
474             * @param  authType the company's method of authenticating users
475             * @param  autoLogin whether to allow users to select the "remember me"
476             *         feature
477             * @param  sendPassword whether to allow users to ask the company to send
478             *         their passwords
479             * @param  strangers whether to allow strangers to create accounts to
480             *         register themselves in the company
481             * @param  strangersWithMx whether to allow strangers to create accounts
482             *         with email addresses that match the company mail suffix
483             * @param  strangersVerify whether to require strangers who create accounts
484             *         to be verified via email
485             * @param  siteLogo whether to to allow site administrators to use their own
486             *         logo instead of the enterprise logo
487             * @throws PortalException if the user was not an administrator
488             * @throws SystemException if a system exception occurred
489             */
490            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
491            @Override
492            public void updateSecurity(
493                            long companyId, String authType, boolean autoLogin,
494                            boolean sendPassword, boolean strangers, boolean strangersWithMx,
495                            boolean strangersVerify, boolean siteLogo)
496                    throws PortalException, SystemException {
497    
498                    if (!roleLocalService.hasUserRole(
499                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
500    
501                            throw new PrincipalException();
502                    }
503    
504                    companyLocalService.updateSecurity(
505                            companyId, authType, autoLogin, sendPassword, strangers,
506                            strangersWithMx, strangersVerify, siteLogo);
507            }
508    
509    }