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 int getUserOrganizationsCount(long userId)
364         throws PortalException, SystemException {
365 
366         return userPersistence.getOrganizationsSize(userId);
367     }
368 
369     public boolean hasGroupOrganization(long groupId, long organizationId)
370         throws PortalException, SystemException {
371 
372         return groupPersistence.containsOrganization(groupId, organizationId);
373     }
374 
375     public boolean hasUserOrganization(long userId, long organizationId)
376         throws PortalException, SystemException {
377 
378         return userPersistence.containsOrganization(userId, organizationId);
379     }
380 
381     public boolean hasPasswordPolicyOrganization(
382             long passwordPolicyId, long organizationId)
383         throws PortalException, SystemException {
384 
385         return passwordPolicyRelLocalService.hasPasswordPolicyRel(
386             passwordPolicyId, Organization.class.getName(), organizationId);
387     }
388 
389     public List search(
390             long companyId, long parentOrganizationId, String keywords,
391             int type, Long regionId, Long countryId, LinkedHashMap params,
392             int begin, int end)
393         throws PortalException, SystemException {
394 
395         return search(
396             companyId, parentOrganizationId, keywords, type, regionId,
397             countryId, params, begin, end,
398             new OrganizationNameComparator(true));
399     }
400 
401     public List search(
402             long companyId, long parentOrganizationId, String keywords,
403             int type, Long regionId, Long countryId, LinkedHashMap params,
404             int begin, int end, OrderByComparator obc)
405         throws PortalException, SystemException {
406 
407         String parentOrganizationComparator = StringPool.EQUAL;
408 
409         if (parentOrganizationId ==
410                 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
411 
412             parentOrganizationComparator = StringPool.NOT_EQUAL;
413         }
414 
415         return organizationFinder.findByKeywords(
416             companyId, parentOrganizationId, parentOrganizationComparator,
417             keywords, type, regionId, countryId, params, begin, end,
418             obc);
419     }
420 
421     public List search(
422             long companyId, long parentOrganizationId, String name, int type,
423             String street, String city, String zip,
424             Long regionId, Long countryId, LinkedHashMap params,
425             boolean andOperator, int begin, int end)
426         throws PortalException, SystemException {
427 
428         return search(
429             companyId, parentOrganizationId, name, type, street, city, zip,
430             regionId, countryId, params, andOperator, begin, end,
431             new OrganizationNameComparator(true));
432     }
433 
434     public List search(
435             long companyId, long parentOrganizationId, String name, int type,
436             String street, String city, String zip,
437             Long regionId, Long countryId, LinkedHashMap params,
438             boolean andOperator, int begin, int end, OrderByComparator obc)
439         throws PortalException, SystemException {
440 
441         String parentOrganizationComparator = StringPool.EQUAL;
442 
443         if (parentOrganizationId ==
444                 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
445 
446             parentOrganizationComparator = StringPool.NOT_EQUAL;
447         }
448 
449         return organizationFinder.findByC_PO_N_T_S_C_Z_R_C(
450             companyId, parentOrganizationId, parentOrganizationComparator,
451             name, type, street, city, zip, regionId, countryId, params,
452             andOperator, begin, end, obc);
453     }
454 
455     public int searchCount(
456             long companyId, long parentOrganizationId, String keywords,
457             int type, Long regionId, Long countryId,
458             LinkedHashMap params)
459         throws PortalException, SystemException {
460 
461         String parentOrganizationComparator = StringPool.EQUAL;
462 
463         if (parentOrganizationId ==
464                 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
465 
466             parentOrganizationComparator = StringPool.NOT_EQUAL;
467         }
468 
469         return organizationFinder.countByKeywords(
470             companyId, parentOrganizationId, parentOrganizationComparator,
471             keywords, type, regionId, countryId, params);
472     }
473 
474     public int searchCount(
475             long companyId, long parentOrganizationId, String name, int type,
476             String street, String city, String zip,
477             Long regionId, Long countryId, LinkedHashMap params,
478             boolean andOperator)
479         throws PortalException, SystemException {
480 
481         String parentOrganizationComparator = StringPool.EQUAL;
482 
483         if (parentOrganizationId ==
484                 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
485 
486             parentOrganizationComparator = StringPool.NOT_EQUAL;
487         }
488 
489         return organizationFinder.countByC_PO_N_T_S_C_Z_R_C(
490             companyId, parentOrganizationId, parentOrganizationComparator,
491             name, type, street, city, zip, regionId, countryId, params,
492             andOperator);
493     }
494 
495     public void setGroupOrganizations(long groupId, long[] organizationIds)
496         throws PortalException, SystemException {
497 
498         groupPersistence.setOrganizations(groupId, organizationIds);
499 
500         PermissionCacheUtil.clearCache();
501     }
502 
503     public void unsetGroupOrganizations(long groupId, long[] organizationIds)
504         throws PortalException, SystemException {
505 
506         groupPersistence.removeOrganizations(groupId, organizationIds);
507 
508         PermissionCacheUtil.clearCache();
509     }
510 
511     public void unsetPasswordPolicyOrganizations(
512             long passwordPolicyId, long[] organizationIds)
513         throws PortalException, SystemException {
514 
515         passwordPolicyRelLocalService.deletePasswordPolicyRels(
516             passwordPolicyId, Organization.class.getName(), organizationIds);
517     }
518 
519     public Organization updateOrganization(
520             long companyId, long organizationId, long parentOrganizationId,
521             String name, int type, boolean recursable, long regionId,
522             long countryId, int statusId, String comments)
523         throws PortalException, SystemException {
524 
525         parentOrganizationId = getParentOrganizationId(
526             companyId, parentOrganizationId);
527         recursable = true;
528 
529         validate(
530             companyId, organizationId, parentOrganizationId, name, type,
531             countryId, statusId);
532 
533         Organization organization = organizationPersistence.findByPrimaryKey(
534             organizationId);
535 
536         organization.setParentOrganizationId(parentOrganizationId);
537         organization.setName(name);
538 
539         if (type == OrganizationImpl.TYPE_LOCATION) {
540             organization.setLocation(true);
541         }
542         else {
543             organization.setLocation(false);
544         }
545 
546         organization.setRecursable(recursable);
547         organization.setRegionId(regionId);
548         organization.setCountryId(countryId);
549         organization.setStatusId(statusId);
550         organization.setComments(comments);
551 
552         organizationPersistence.update(organization);
553 
554         return organization;
555     }
556 
557     protected void addSuborganizations(
558             List allSuborganizations, List organizations)
559         throws SystemException {
560 
561         for (int i = 0; i < organizations.size(); i++) {
562             Organization organization = (Organization)organizations.get(i);
563 
564             if (!allSuborganizations.contains(organization)) {
565                 allSuborganizations.add(organization);
566 
567                 List suborganizations = organizationPersistence.findByC_P(
568                     organization.getCompanyId(),
569                     organization.getOrganizationId());
570 
571                 addSuborganizations(allSuborganizations, suborganizations);
572             }
573         }
574     }
575 
576     protected long getParentOrganizationId(
577             long companyId, long parentOrganizationId)
578         throws PortalException, SystemException {
579 
580         if (parentOrganizationId !=
581                 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
582 
583             // Ensure parent organization exists and belongs to the proper
584             // company
585 
586             try {
587                 Organization parentOrganization =
588                     organizationPersistence.findByPrimaryKey(
589                         parentOrganizationId);
590 
591                 if (companyId != parentOrganization.getCompanyId()) {
592                     parentOrganizationId =
593                         OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID;
594                 }
595             }
596             catch (NoSuchOrganizationException nsoe) {
597                 parentOrganizationId =
598                     OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID;
599             }
600         }
601 
602         return parentOrganizationId;
603     }
604 
605     protected List getParentOrganizations(
606             Organization organization, boolean lastOrganization)
607         throws PortalException, SystemException {
608 
609         List organizations = new ArrayList();
610 
611         if (!lastOrganization) {
612             organizations.add(organization);
613         }
614 
615         long parentOrganizationId = organization.getParentOrganizationId();
616 
617         if (parentOrganizationId ==
618                 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
619 
620             return organizations;
621         }
622 
623         Organization parentOrganization =
624             organizationPersistence.findByPrimaryKey(parentOrganizationId);
625 
626         List parentOrganizatons = getParentOrganizations(
627             parentOrganization, false);
628 
629         organizations.addAll(parentOrganizatons);
630 
631         return organizations;
632     }
633 
634     protected boolean isParentOrganization(
635             long parentOrganizationId, long organizationId)
636         throws PortalException, SystemException {
637 
638         // Return true if parentOrganizationId is among the parent organizatons
639         // of organizationId
640 
641         Organization parentOrganization =
642             organizationPersistence.findByPrimaryKey(
643                 parentOrganizationId);
644 
645         List parentOrganizations = getParentOrganizations(organizationId);
646 
647         if (parentOrganizations.contains(parentOrganization)) {
648             return true;
649         }
650         else {
651             return false;
652         }
653     }
654 
655     protected void validate(
656             long companyId, long parentOrganizationId, String name, int type,
657             long countryId, int statusId)
658         throws PortalException, SystemException {
659 
660         validate(
661             companyId, 0, parentOrganizationId, name, type, countryId,
662             statusId);
663     }
664 
665     protected void validate(
666             long companyId, long organizationId, long parentOrganizationId,
667             String name, int type, long countryId, int statusId)
668         throws PortalException, SystemException {
669 
670         if ((type == OrganizationImpl.TYPE_LOCATION) ||
671             (parentOrganizationId !=
672                 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID)) {
673 
674             try {
675                 Organization parentOrganization =
676                     organizationPersistence.findByPrimaryKey(
677                         parentOrganizationId);
678 
679                 if ((companyId != parentOrganization.getCompanyId()) ||
680                     (parentOrganizationId == organizationId) ||
681                     (parentOrganization.isLocation())) {
682 
683                     throw new OrganizationParentException();
684                 }
685             }
686             catch (NoSuchOrganizationException nsoe) {
687                 throw new OrganizationParentException();
688             }
689         }
690 
691         if ((organizationId > 0) &&
692             (parentOrganizationId !=
693                 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID)) {
694 
695             // Prevent circular organizational references
696 
697             if (isParentOrganization(organizationId, parentOrganizationId)) {
698                 throw new OrganizationParentException();
699             }
700         }
701 
702         if (Validator.isNull(name)) {
703             throw new OrganizationNameException();
704         }
705         else {
706             try {
707                 Organization organization = organizationPersistence.findByC_N(
708                     companyId, name);
709 
710                 if (organization.getName().equalsIgnoreCase(name)) {
711                     if ((organizationId <= 0) ||
712                         (organization.getOrganizationId() != organizationId)) {
713 
714                         throw new DuplicateOrganizationException();
715                     }
716                 }
717             }
718             catch (NoSuchOrganizationException nsoe) {
719             }
720         }
721 
722         try {
723             if ((countryId > 0) || PropsValues.ORGANIZATIONS_COUNTRY_REQUIRED) {
724                 countryPersistence.findByPrimaryKey(countryId);
725             }
726 
727             listTypeService.validate(
728                 statusId, ListTypeImpl.ORGANIZATION_STATUS);
729         }
730         catch (RemoteException re) {
731             throw new SystemException(re);
732         }
733     }
734 
735 }