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