1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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 PortalException, SystemException {
222 
223         return organizationLocalService.getUserOrganizations(userId);
224     }
225 
226     public List<Organization> getUserOrganizations(
227             long userId, boolean inheritUserGroups)
228         throws PortalException, SystemException {
229 
230         return organizationLocalService.getUserOrganizations(
231             userId, inheritUserGroups);
232     }
233 
234     public void setGroupOrganizations(long groupId, long[] organizationIds)
235         throws PortalException, SystemException {
236 
237         GroupPermissionUtil.check(
238             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
239 
240         organizationLocalService.setGroupOrganizations(
241             groupId, organizationIds);
242     }
243 
244     public void unsetGroupOrganizations(long groupId, long[] organizationIds)
245         throws PortalException, SystemException {
246 
247         GroupPermissionUtil.check(
248             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
249 
250         organizationLocalService.unsetGroupOrganizations(
251             groupId, organizationIds);
252     }
253 
254     public void unsetPasswordPolicyOrganizations(
255             long passwordPolicyId, long[] organizationIds)
256         throws PortalException, SystemException {
257 
258         PasswordPolicyPermissionUtil.check(
259             getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
260 
261         organizationLocalService.unsetPasswordPolicyOrganizations(
262             passwordPolicyId, organizationIds);
263     }
264 
265     public Organization updateOrganization(
266             long organizationId, long parentOrganizationId, String name,
267             String type, boolean recursable, long regionId, long countryId,
268             int statusId, String comments, ServiceContext serviceContext)
269         throws PortalException, SystemException {
270 
271         OrganizationPermissionUtil.check(
272             getPermissionChecker(), organizationId, ActionKeys.UPDATE);
273 
274         return organizationLocalService.updateOrganization(
275             getUser().getCompanyId(), organizationId, parentOrganizationId,
276             name, type, recursable, regionId, countryId, statusId, comments,
277             serviceContext);
278     }
279 
280     public Organization updateOrganization(
281             long organizationId, long parentOrganizationId, String name,
282             String type, boolean recursable, long regionId, long countryId,
283             int statusId, String comments, List<Address> addresses,
284             List<EmailAddress> emailAddresses, List<OrgLabor> orgLabors,
285             List<Phone> phones, List<Website> websites,
286             ServiceContext serviceContext)
287         throws PortalException, SystemException {
288 
289         Organization organization = updateOrganization(
290             organizationId, parentOrganizationId, name, type, recursable,
291             regionId, countryId, statusId, comments, serviceContext);
292 
293         EnterpriseAdminUtil.updateAddresses(
294             Organization.class.getName(), organizationId, addresses);
295 
296         EnterpriseAdminUtil.updateEmailAddresses(
297             Organization.class.getName(), organizationId, emailAddresses);
298 
299         EnterpriseAdminUtil.updateOrgLabors(organizationId, orgLabors);
300 
301         EnterpriseAdminUtil.updatePhones(
302             Organization.class.getName(), organizationId, phones);
303 
304         EnterpriseAdminUtil.updateWebsites(
305             Organization.class.getName(), organizationId, websites);
306 
307         return organization;
308     }
309 
310 }