1   /**
2    * Copyright (c) 2000-2008 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.DuplicateOrganizationException;
26  import com.liferay.portal.NoSuchOrganizationException;
27  import com.liferay.portal.OrganizationNameException;
28  import com.liferay.portal.OrganizationParentException;
29  import com.liferay.portal.PortalException;
30  import com.liferay.portal.RequiredOrganizationException;
31  import com.liferay.portal.SystemException;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.model.Group;
36  import com.liferay.portal.model.Location;
37  import com.liferay.portal.model.Organization;
38  import com.liferay.portal.model.Role;
39  import com.liferay.portal.model.User;
40  import com.liferay.portal.model.impl.ListTypeImpl;
41  import com.liferay.portal.model.impl.OrganizationImpl;
42  import com.liferay.portal.model.impl.ResourceImpl;
43  import com.liferay.portal.model.impl.RoleImpl;
44  import com.liferay.portal.security.permission.PermissionCacheUtil;
45  import com.liferay.portal.service.base.OrganizationLocalServiceBaseImpl;
46  import com.liferay.portal.util.PropsValues;
47  import com.liferay.portal.util.comparator.OrganizationNameComparator;
48  import com.liferay.util.UniqueList;
49  
50  import java.rmi.RemoteException;
51  
52  import java.util.ArrayList;
53  import java.util.Iterator;
54  import java.util.LinkedHashMap;
55  import java.util.List;
56  
57  /**
58   * <a href="OrganizationLocalServiceImpl.java.html"><b><i>View Source</i></b>
59   * </a>
60   *
61   * @author Brian Wing Shun Chan
62   * @author Jorge Ferrer
63   *
64   */
65  public class OrganizationLocalServiceImpl
66      extends OrganizationLocalServiceBaseImpl {
67  
68      public void addGroupOrganizations(long groupId, long[] organizationIds)
69          throws PortalException, SystemException {
70  
71          groupPersistence.addOrganizations(groupId, organizationIds);
72  
73          PermissionCacheUtil.clearCache();
74      }
75  
76      public Organization addOrganization(
77              long userId, long parentOrganizationId, String name,
78              int type, boolean recursable, long regionId, long countryId,
79              int statusId, String comments)
80          throws PortalException, SystemException {
81  
82          // Organization
83  
84          User user = userPersistence.findByPrimaryKey(userId);
85          parentOrganizationId = getParentOrganizationId(
86              user.getCompanyId(), parentOrganizationId);
87          recursable = true;
88  
89          validate(
90              user.getCompanyId(), parentOrganizationId, name, type, countryId,
91              statusId);
92  
93          long organizationId = counterLocalService.increment();
94  
95          Organization organization = organizationPersistence.create(
96              organizationId);
97  
98          organization.setCompanyId(user.getCompanyId());
99          organization.setParentOrganizationId(parentOrganizationId);
100         organization.setName(name);
101 
102         if (type == OrganizationImpl.TYPE_LOCATION) {
103             organization.setLocation(true);
104         }
105         else {
106             organization.setLocation(false);
107         }
108 
109         organization.setRecursable(recursable);
110         organization.setRegionId(regionId);
111         organization.setCountryId(countryId);
112         organization.setStatusId(statusId);
113         organization.setComments(comments);
114 
115         organizationPersistence.update(organization);
116 
117         // Group
118 
119         Group group = groupLocalService.addGroup(
120             userId, Organization.class.getName(), organizationId, null, null, 0,
121             null, true);
122 
123         // Role
124 
125         Role role = roleLocalService.getRole(
126             organization.getCompanyId(), RoleImpl.ORGANIZATION_OWNER);
127 
128         userGroupRoleLocalService.addUserGroupRoles(
129             userId, group.getGroupId(), new long[] {role.getRoleId()});
130 
131         // User
132 
133         userPersistence.addOrganization(userId, organizationId);
134 
135         // Resources
136 
137         addOrganizationResources(userId, organization);
138 
139         return organization;
140     }
141 
142     public void addOrganizationResources(long userId, Organization organization)
143         throws PortalException, SystemException {
144 
145         String name = Organization.class.getName();
146 
147         if (organization.isLocation()) {
148             name = Location.class.getName();
149         }
150 
151         resourceLocalService.addResources(
152             organization.getCompanyId(), 0, userId, name,
153             organization.getOrganizationId(), false, false, false);
154     }
155 
156     public void addPasswordPolicyOrganizations(
157             long passwordPolicyId, long[] organizationIds)
158         throws PortalException, SystemException {
159 
160         passwordPolicyRelLocalService.addPasswordPolicyRels(
161             passwordPolicyId, Organization.class.getName(), organizationIds);
162     }
163 
164     public void deleteOrganization(long organizationId)
165         throws PortalException, SystemException {
166 
167         Organization organization = organizationPersistence.findByPrimaryKey(
168             organizationId);
169 
170         deleteOrganization(organization);
171     }
172 
173     public void deleteOrganization(Organization organization)
174         throws PortalException, SystemException {
175 
176         if ((organizationPersistence.containsUsers(
177                 organization.getOrganizationId())) ||
178             (organizationPersistence.countByC_P(
179                 organization.getCompanyId(),
180                 organization.getOrganizationId()) > 0)) {
181 
182             throw new RequiredOrganizationException();
183         }
184 
185         // Addresses
186 
187         addressLocalService.deleteAddresses(
188             organization.getCompanyId(), Organization.class.getName(),
189             organization.getOrganizationId());
190 
191         // Email addresses
192 
193         emailAddressLocalService.deleteEmailAddresses(
194             organization.getCompanyId(), Organization.class.getName(),
195             organization.getOrganizationId());
196 
197         // Password policy relation
198 
199         passwordPolicyRelLocalService.deletePasswordPolicyRel(
200             Organization.class.getName(), organization.getOrganizationId());
201 
202         // Phone
203 
204         phoneLocalService.deletePhones(
205             organization.getCompanyId(), Organization.class.getName(),
206             organization.getOrganizationId());
207 
208         // Website
209 
210         websiteLocalService.deleteWebsites(
211             organization.getCompanyId(), Organization.class.getName(),
212             organization.getOrganizationId());
213 
214         // Group
215 
216         Group group = organization.getGroup();
217 
218         groupLocalService.deleteGroup(group.getGroupId());
219 
220         // Resources
221 
222         String name = Organization.class.getName();
223 
224         if (organization.isLocation()) {
225             name = Location.class.getName();
226         }
227 
228         resourceLocalService.deleteResource(
229             organization.getCompanyId(), name, ResourceImpl.SCOPE_INDIVIDUAL,
230             organization.getOrganizationId());
231 
232         // Organization
233 
234         organizationPersistence.remove(organization.getOrganizationId());
235 
236         // Permission cache
237 
238         PermissionCacheUtil.clearCache();
239     }
240 
241     public List getGroupOrganizations(long groupId)
242         throws PortalException, SystemException {
243 
244         return groupPersistence.getOrganizations(groupId);
245     }
246 
247     /**
248      * Gets a list of organizations that a user has access to administrate. This
249      * includes organizations that a user belongs to and all suborganizations of
250      * those organizations.
251      *
252      * @param       userId the user id of the user
253      * @return      a list of organizations
254      */
255     public List getManageableOrganizations(long userId)
256         throws PortalException, SystemException {
257 
258         List manageableOrganizations = new UniqueList();
259 
260         List userOrganizations = userPersistence.getOrganizations(userId);
261 
262         manageableOrganizations.addAll(userOrganizations);
263         manageableOrganizations.addAll(getSuborganizations(userOrganizations));
264 
265         return manageableOrganizations;
266     }
267 
268     public Organization getOrganization(long organizationId)
269         throws PortalException, SystemException {
270 
271         return organizationPersistence.findByPrimaryKey(organizationId);
272     }
273 
274     public Organization getOrganization(long companyId, String name)
275         throws PortalException, SystemException {
276 
277         return organizationPersistence.findByC_N(companyId, name);
278     }
279 
280     public long getOrganizationId(long companyId, String name)
281         throws PortalException, SystemException {
282 
283         try {
284             Organization organization = organizationPersistence.findByC_N(
285                 companyId, name);
286 
287             return organization.getOrganizationId();
288         }
289         catch (NoSuchOrganizationException nsoge) {
290             return 0;
291         }
292     }
293 
294     public List getOrganizations(long[] organizationIds)
295         throws PortalException, SystemException {
296 
297         List organizations = new ArrayList();
298 
299         for (int i = 0; i < organizationIds.length; i++) {
300             Organization organization = getOrganization(organizationIds[i]);
301 
302             organizations.add(organization);
303         }
304 
305         return organizations;
306     }
307 
308     public List getParentOrganizations(long organizationId)
309         throws PortalException, SystemException {
310 
311         if (organizationId == OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
312             return new ArrayList();
313         }
314 
315         Organization organization =
316             organizationPersistence.findByPrimaryKey(organizationId);
317 
318         return getParentOrganizations(organization, true);
319     }
320 
321     public List getSuborganizations(List organizations)
322         throws SystemException {
323 
324         List allSuborganizations = new ArrayList();
325 
326         for (int i = 0; i < organizations.size(); i++) {
327             Organization organization = (Organization)organizations.get(i);
328 
329             List suborganizations = organizationPersistence.findByC_P(
330                 organization.getCompanyId(), organization.getOrganizationId());
331 
332             addSuborganizations(allSuborganizations, suborganizations);
333         }
334 
335         return allSuborganizations;
336     }
337 
338     public List getSubsetOrganizations(
339             List allOrganizations, List availableOrganizations)
340         throws PortalException, SystemException {
341 
342         List subsetOrganizations = new ArrayList();
343 
344         Iterator itr = allOrganizations.iterator();
345 
346         while (itr.hasNext()) {
347             Organization organization = (Organization)itr.next();
348 
349             if (availableOrganizations.contains(organization)) {
350                 subsetOrganizations.add(organization);
351             }
352         }
353 
354         return subsetOrganizations;
355     }
356 
357     public List getUserOrganizations(long userId)
358         throws PortalException, SystemException {
359 
360         return userPersistence.getOrganizations(userId);
361     }
362 
363     public boolean hasGroupOrganization(long groupId, long organizationId)
364         throws PortalException, SystemException {
365 
366         return groupPersistence.containsOrganization(groupId, organizationId);
367     }
368 
369     public boolean hasUserOrganization(long userId, long organizationId)
370         throws PortalException, SystemException {
371 
372         return userPersistence.containsOrganization(userId, organizationId);
373     }
374 
375     public boolean hasPasswordPolicyOrganization(
376             long passwordPolicyId, long organizationId)
377         throws PortalException, SystemException {
378 
379         return passwordPolicyRelLocalService.hasPasswordPolicyRel(
380             passwordPolicyId, Organization.class.getName(), organizationId);
381     }
382 
383     public List search(
384             long companyId, long parentOrganizationId, String keywords,
385             int type, Long regionId, Long countryId, LinkedHashMap params,
386             int begin, int end)
387         throws PortalException, SystemException {
388 
389         return search(
390             companyId, parentOrganizationId, keywords, type, regionId,
391             countryId, params, begin, end,
392             new OrganizationNameComparator(true));
393     }
394 
395     public List search(
396             long companyId, long parentOrganizationId, String keywords,
397             int type, Long regionId, Long countryId, LinkedHashMap params,
398             int begin, int end, OrderByComparator obc)
399         throws PortalException, SystemException {
400 
401         String parentOrganizationComparator = StringPool.EQUAL;
402 
403         if (parentOrganizationId ==
404                 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
405 
406             parentOrganizationComparator = StringPool.NOT_EQUAL;
407         }
408 
409         return organizationFinder.findByKeywords(
410             companyId, parentOrganizationId, parentOrganizationComparator,
411             keywords, type, regionId, countryId, params, begin, end,
412             obc);
413     }
414 
415     public List search(
416             long companyId, long parentOrganizationId, String name, int type,
417             String street, String city, String zip,
418             Long regionId, Long countryId, LinkedHashMap params,
419             boolean andOperator, int begin, int end)
420         throws PortalException, SystemException {
421 
422         return search(
423             companyId, parentOrganizationId, name, type, street, city, zip,
424             regionId, countryId, params, andOperator, begin, end,
425             new OrganizationNameComparator(true));
426     }
427 
428     public List search(
429             long companyId, long parentOrganizationId, String name, int type,
430             String street, String city, String zip,
431             Long regionId, Long countryId, LinkedHashMap params,
432             boolean andOperator, int begin, int end, OrderByComparator obc)
433         throws PortalException, SystemException {
434 
435         String parentOrganizationComparator = StringPool.EQUAL;
436 
437         if (parentOrganizationId ==
438                 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
439 
440             parentOrganizationComparator = StringPool.NOT_EQUAL;
441         }
442 
443         return organizationFinder.findByC_PO_N_T_S_C_Z_R_C(
444             companyId, parentOrganizationId, parentOrganizationComparator,
445             name, type, street, city, zip, regionId, countryId, params,
446             andOperator, begin, end, obc);
447     }
448 
449     public int searchCount(
450             long companyId, long parentOrganizationId, String keywords,
451             int type, Long regionId, Long countryId,
452             LinkedHashMap params)
453         throws PortalException, SystemException {
454 
455         String parentOrganizationComparator = StringPool.EQUAL;
456 
457         if (parentOrganizationId ==
458                 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
459 
460             parentOrganizationComparator = StringPool.NOT_EQUAL;
461         }
462 
463         return organizationFinder.countByKeywords(
464             companyId, parentOrganizationId, parentOrganizationComparator,
465             keywords, type, regionId, countryId, params);
466     }
467 
468     public int searchCount(
469             long companyId, long parentOrganizationId, String name, int type,
470             String street, String city, String zip,
471             Long regionId, Long countryId, LinkedHashMap params,
472             boolean andOperator)
473         throws PortalException, SystemException {
474 
475         String parentOrganizationComparator = StringPool.EQUAL;
476 
477         if (parentOrganizationId ==
478                 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
479 
480             parentOrganizationComparator = StringPool.NOT_EQUAL;
481         }
482 
483         return organizationFinder.countByC_PO_N_T_S_C_Z_R_C(
484             companyId, parentOrganizationId, parentOrganizationComparator,
485             name, type, street, city, zip, regionId, countryId, params,
486             andOperator);
487     }
488 
489     public void setGroupOrganizations(long groupId, long[] organizationIds)
490         throws PortalException, SystemException {
491 
492         groupPersistence.setOrganizations(groupId, organizationIds);
493 
494         PermissionCacheUtil.clearCache();
495     }
496 
497     public void unsetGroupOrganizations(long groupId, long[] organizationIds)
498         throws PortalException, SystemException {
499 
500         groupPersistence.removeOrganizations(groupId, organizationIds);
501 
502         PermissionCacheUtil.clearCache();
503     }
504 
505     public void unsetPasswordPolicyOrganizations(
506             long passwordPolicyId, long[] organizationIds)
507         throws PortalException, SystemException {
508 
509         passwordPolicyRelLocalService.deletePasswordPolicyRels(
510             passwordPolicyId, Organization.class.getName(), organizationIds);
511     }
512 
513     public Organization updateOrganization(
514             long companyId, long organizationId, long parentOrganizationId,
515             String name, int type, boolean recursable, long regionId,
516             long countryId, int statusId, String comments)
517         throws PortalException, SystemException {
518 
519         parentOrganizationId = getParentOrganizationId(
520             companyId, parentOrganizationId);
521         recursable = true;
522 
523         validate(
524             companyId, organizationId, parentOrganizationId, name, type,
525             countryId, statusId);
526 
527         Organization organization = organizationPersistence.findByPrimaryKey(
528             organizationId);
529 
530         organization.setParentOrganizationId(parentOrganizationId);
531         organization.setName(name);
532 
533         if (type == OrganizationImpl.TYPE_LOCATION) {
534             organization.setLocation(true);
535         }
536         else {
537             organization.setLocation(false);
538         }
539 
540         organization.setRecursable(recursable);
541         organization.setRegionId(regionId);
542         organization.setCountryId(countryId);
543         organization.setStatusId(statusId);
544         organization.setComments(comments);
545 
546         organizationPersistence.update(organization);
547 
548         return organization;
549     }
550 
551     protected void addSuborganizations(
552             List allSuborganizations, List organizations)
553         throws SystemException {
554 
555         for (int i = 0; i < organizations.size(); i++) {
556             Organization organization = (Organization)organizations.get(i);
557 
558             if (!allSuborganizations.contains(organization)) {
559                 allSuborganizations.add(organization);
560 
561                 List suborganizations = organizationPersistence.findByC_P(
562                     organization.getCompanyId(),
563                     organization.getOrganizationId());
564 
565                 addSuborganizations(allSuborganizations, suborganizations);
566             }
567         }
568     }
569 
570     protected long getParentOrganizationId(
571             long companyId, long parentOrganizationId)
572         throws PortalException, SystemException {
573 
574         if (parentOrganizationId !=
575                 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
576 
577             // Ensure parent organization exists and belongs to the proper
578             // company
579 
580             try {
581                 Organization parentOrganization =
582                     organizationPersistence.findByPrimaryKey(
583                         parentOrganizationId);
584 
585                 if (companyId != parentOrganization.getCompanyId()) {
586                     parentOrganizationId =
587                         OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID;
588                 }
589             }
590             catch (NoSuchOrganizationException nsoe) {
591                 parentOrganizationId =
592                     OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID;
593             }
594         }
595 
596         return parentOrganizationId;
597     }
598 
599     protected List getParentOrganizations(
600             Organization organization, boolean lastOrganization)
601         throws PortalException, SystemException {
602 
603         List organizations = new ArrayList();
604 
605         if (!lastOrganization) {
606             organizations.add(organization);
607         }
608 
609         long parentOrganizationId = organization.getParentOrganizationId();
610 
611         if (parentOrganizationId ==
612                 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
613 
614             return organizations;
615         }
616 
617         Organization parentOrganization =
618             organizationPersistence.findByPrimaryKey(parentOrganizationId);
619 
620         List parentOrganizatons = getParentOrganizations(
621             parentOrganization, false);
622 
623         organizations.addAll(parentOrganizatons);
624 
625         return organizations;
626     }
627 
628     protected boolean isParentOrganization(
629             long parentOrganizationId, long organizationId)
630         throws PortalException, SystemException {
631 
632         // Return true if parentOrganizationId is among the parent organizatons
633         // of organizationId
634 
635         Organization parentOrganization =
636             organizationPersistence.findByPrimaryKey(
637                 parentOrganizationId);
638 
639         List parentOrganizations = getParentOrganizations(organizationId);
640 
641         if (parentOrganizations.contains(parentOrganization)) {
642             return true;
643         }
644         else {
645             return false;
646         }
647     }
648 
649     protected void validate(
650             long companyId, long parentOrganizationId, String name, int type,
651             long countryId, int statusId)
652         throws PortalException, SystemException {
653 
654         validate(
655             companyId, 0, parentOrganizationId, name, type, countryId,
656             statusId);
657     }
658 
659     protected void validate(
660             long companyId, long organizationId, long parentOrganizationId,
661             String name, int type, long countryId, int statusId)
662         throws PortalException, SystemException {
663 
664         if ((type == OrganizationImpl.TYPE_LOCATION) ||
665             (parentOrganizationId !=
666                 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID)) {
667 
668             try {
669                 Organization parentOrganization =
670                     organizationPersistence.findByPrimaryKey(
671                         parentOrganizationId);
672 
673                 if ((companyId != parentOrganization.getCompanyId()) ||
674                     (parentOrganizationId == organizationId) ||
675                     (parentOrganization.isLocation())) {
676 
677                     throw new OrganizationParentException();
678                 }
679             }
680             catch (NoSuchOrganizationException nsoe) {
681                 throw new OrganizationParentException();
682             }
683         }
684 
685         if ((organizationId > 0) &&
686             (parentOrganizationId !=
687                 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID)) {
688 
689             // Prevent circular organizational references
690 
691             if (isParentOrganization(organizationId, parentOrganizationId)) {
692                 throw new OrganizationParentException();
693             }
694         }
695 
696         if (Validator.isNull(name)) {
697             throw new OrganizationNameException();
698         }
699         else {
700             try {
701                 Organization organization = organizationPersistence.findByC_N(
702                     companyId, name);
703 
704                 if (organization.getName().equalsIgnoreCase(name)) {
705                     if ((organizationId <= 0) ||
706                         (organization.getOrganizationId() != organizationId)) {
707 
708                         throw new DuplicateOrganizationException();
709                     }
710                 }
711             }
712             catch (NoSuchOrganizationException nsoe) {
713             }
714         }
715 
716         try {
717             if ((countryId > 0) || PropsValues.ORGANIZATIONS_COUNTRY_REQUIRED) {
718                 countryPersistence.findByPrimaryKey(countryId);
719             }
720 
721             listTypeService.validate(
722                 statusId, ListTypeImpl.ORGANIZATION_STATUS);
723         }
724         catch (RemoteException re) {
725             throw new SystemException(re);
726         }
727     }
728 
729 }