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