1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.ListUtil;
28  import com.liferay.portal.model.Address;
29  import com.liferay.portal.model.EmailAddress;
30  import com.liferay.portal.model.OrgLabor;
31  import com.liferay.portal.model.Organization;
32  import com.liferay.portal.model.OrganizationConstants;
33  import com.liferay.portal.model.Phone;
34  import com.liferay.portal.model.Website;
35  import com.liferay.portal.security.auth.PrincipalException;
36  import com.liferay.portal.security.permission.ActionKeys;
37  import com.liferay.portal.security.permission.PermissionChecker;
38  import com.liferay.portal.service.ServiceContext;
39  import com.liferay.portal.service.base.OrganizationServiceBaseImpl;
40  import com.liferay.portal.service.permission.GroupPermissionUtil;
41  import com.liferay.portal.service.permission.OrganizationPermissionUtil;
42  import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
43  import com.liferay.portal.service.permission.PortalPermissionUtil;
44  import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
45  
46  import java.util.Iterator;
47  import java.util.LinkedHashMap;
48  import java.util.List;
49  
50  /**
51   * <a href="OrganizationServiceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   * @author Jorge Ferrer
55   * @author Julio Camarero
56   *
57   */
58  public class OrganizationServiceImpl extends OrganizationServiceBaseImpl {
59  
60      public void addGroupOrganizations(long groupId, long[] organizationIds)
61          throws PortalException, SystemException {
62  
63          GroupPermissionUtil.check(
64              getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
65  
66          organizationLocalService.addGroupOrganizations(
67              groupId, organizationIds);
68      }
69  
70      public void addPasswordPolicyOrganizations(
71              long passwordPolicyId, long[] organizationIds)
72          throws PortalException, SystemException {
73  
74          PasswordPolicyPermissionUtil.check(
75              getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
76  
77          organizationLocalService.addPasswordPolicyOrganizations(
78              passwordPolicyId, organizationIds);
79      }
80  
81      public Organization addOrganization(
82              long parentOrganizationId, String name, String type,
83              boolean recursable, long regionId, long countryId, int statusId,
84              String comments, ServiceContext serviceContext)
85          throws PortalException, SystemException {
86  
87          if (!OrganizationPermissionUtil.contains(
88                  getPermissionChecker(), parentOrganizationId,
89                  ActionKeys.MANAGE_SUBORGANIZATIONS) &&
90              !PortalPermissionUtil.contains(
91                  getPermissionChecker(), ActionKeys.ADD_ORGANIZATION)) {
92  
93              throw new PrincipalException(
94                  "User " + getUserId() + " does not have permissions to add " +
95                      "an organization with parent " + parentOrganizationId);
96          }
97  
98          return organizationLocalService.addOrganization(
99              getUserId(), parentOrganizationId, name, type, recursable,
100             regionId, countryId, statusId, comments, serviceContext);
101     }
102 
103     public Organization addOrganization(
104             long parentOrganizationId, String name, String type,
105             boolean recursable, long regionId, long countryId, int statusId,
106             String comments, List<Address> addresses,
107             List<EmailAddress> emailAddresses, List<OrgLabor> orgLabors,
108             List<Phone> phones, List<Website> websites,
109             ServiceContext serviceContext)
110         throws PortalException, SystemException {
111 
112         Organization organization = addOrganization(
113             parentOrganizationId, name, type, recursable, regionId, countryId,
114             statusId, comments, serviceContext);
115 
116         EnterpriseAdminUtil.updateAddresses(
117             Organization.class.getName(), organization.getOrganizationId(),
118             addresses);
119 
120         EnterpriseAdminUtil.updateEmailAddresses(
121             Organization.class.getName(), organization.getOrganizationId(),
122             emailAddresses);
123 
124         EnterpriseAdminUtil.updateOrgLabors(organization.getOrganizationId(),
125             orgLabors);
126 
127         EnterpriseAdminUtil.updatePhones(
128             Organization.class.getName(), organization.getOrganizationId(),
129             phones);
130 
131         EnterpriseAdminUtil.updateWebsites(
132             Organization.class.getName(), organization.getOrganizationId(),
133             websites);
134 
135         return organization;
136     }
137 
138     public void deleteLogo(long organizationId)
139         throws PortalException, SystemException {
140 
141         OrganizationPermissionUtil.check(
142             getPermissionChecker(), organizationId, ActionKeys.UPDATE);
143 
144         organizationLocalService.deleteLogo(organizationId);
145     }
146 
147     public void deleteOrganization(long organizationId)
148         throws PortalException, SystemException {
149 
150         OrganizationPermissionUtil.check(
151             getPermissionChecker(), organizationId, ActionKeys.DELETE);
152 
153         organizationLocalService.deleteOrganization(organizationId);
154     }
155 
156     public List<Organization> getManageableOrganizations(
157             String actionId, int max)
158         throws PortalException, SystemException {
159 
160         PermissionChecker permissionChecker = getPermissionChecker();
161 
162         if (permissionChecker.isCompanyAdmin()) {
163             return organizationLocalService.search(
164                 permissionChecker.getCompanyId(),
165                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null, null,
166                 null, null, null, 0, max);
167         }
168 
169         LinkedHashMap<String, Object> params =
170             new LinkedHashMap<String, Object>();
171 
172         List<Organization> userOrganizations =
173             organizationLocalService.getUserOrganizations(
174                 permissionChecker.getUserId());
175 
176         Long[][] leftAndRightOrganizationIds =
177             EnterpriseAdminUtil.getLeftAndRightOrganizationIds(
178                 userOrganizations);
179 
180         params.put("organizationsTree", leftAndRightOrganizationIds);
181 
182         List<Organization> manageableOrganizations =
183             organizationLocalService.search(
184                 permissionChecker.getCompanyId(),
185                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null, null,
186                 null, null, params, 0, max);
187 
188         manageableOrganizations = ListUtil.copy(manageableOrganizations);
189 
190         Iterator<Organization> itr = manageableOrganizations.iterator();
191 
192         while (itr.hasNext()) {
193             Organization organization = itr.next();
194 
195             if (!OrganizationPermissionUtil.contains(
196                     permissionChecker, organization, actionId)) {
197 
198                 itr.remove();
199             }
200         }
201 
202         return manageableOrganizations;
203     }
204 
205     public Organization getOrganization(long organizationId)
206         throws PortalException, SystemException {
207 
208         OrganizationPermissionUtil.check(
209             getPermissionChecker(), organizationId, ActionKeys.VIEW);
210 
211         return organizationLocalService.getOrganization(organizationId);
212     }
213 
214     public long getOrganizationId(long companyId, String name)
215         throws SystemException {
216 
217         return organizationLocalService.getOrganizationId(companyId, name);
218     }
219 
220     public List<Organization> getUserOrganizations(long userId)
221         throws SystemException {
222 
223         return organizationLocalService.getUserOrganizations(userId);
224     }
225 
226     public void setGroupOrganizations(long groupId, long[] organizationIds)
227         throws PortalException, SystemException {
228 
229         GroupPermissionUtil.check(
230             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
231 
232         organizationLocalService.setGroupOrganizations(
233             groupId, organizationIds);
234     }
235 
236     public void unsetGroupOrganizations(long groupId, long[] organizationIds)
237         throws PortalException, SystemException {
238 
239         GroupPermissionUtil.check(
240             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
241 
242         organizationLocalService.unsetGroupOrganizations(
243             groupId, organizationIds);
244     }
245 
246     public void unsetPasswordPolicyOrganizations(
247             long passwordPolicyId, long[] organizationIds)
248         throws PortalException, SystemException {
249 
250         PasswordPolicyPermissionUtil.check(
251             getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
252 
253         organizationLocalService.unsetPasswordPolicyOrganizations(
254             passwordPolicyId, organizationIds);
255     }
256 
257     public Organization updateOrganization(
258             long organizationId, long parentOrganizationId, String name,
259             String type, boolean recursable, long regionId, long countryId,
260             int statusId, String comments, ServiceContext serviceContext)
261         throws PortalException, SystemException {
262 
263         OrganizationPermissionUtil.check(
264             getPermissionChecker(), organizationId, ActionKeys.UPDATE);
265 
266         return organizationLocalService.updateOrganization(
267             getUser().getCompanyId(), organizationId, parentOrganizationId,
268             name, type, recursable, regionId, countryId, statusId, comments,
269             serviceContext);
270     }
271 
272     public Organization updateOrganization(
273             long organizationId, long parentOrganizationId, String name,
274             String type, boolean recursable, long regionId, long countryId,
275             int statusId, String comments, List<Address> addresses,
276             List<EmailAddress> emailAddresses, List<OrgLabor> orgLabors,
277             List<Phone> phones, List<Website> websites,
278             ServiceContext serviceContext)
279         throws PortalException, SystemException {
280 
281         Organization organization = updateOrganization(
282             organizationId, parentOrganizationId, name, type, recursable,
283             regionId, countryId, statusId, comments, serviceContext);
284 
285         EnterpriseAdminUtil.updateAddresses(
286             Organization.class.getName(), organizationId, addresses);
287 
288         EnterpriseAdminUtil.updateEmailAddresses(
289             Organization.class.getName(), organizationId, emailAddresses);
290 
291         EnterpriseAdminUtil.updateOrgLabors(organizationId, orgLabors);
292 
293         EnterpriseAdminUtil.updatePhones(
294             Organization.class.getName(), organizationId, phones);
295 
296         EnterpriseAdminUtil.updateWebsites(
297             Organization.class.getName(), organizationId, websites);
298 
299         return organization;
300     }
301 
302 }