1
22
23 package com.liferay.portal.service.impl;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.portal.DuplicateOrganizationException;
27 import com.liferay.portal.NoSuchOrganizationException;
28 import com.liferay.portal.OrganizationNameException;
29 import com.liferay.portal.OrganizationParentException;
30 import com.liferay.portal.PortalException;
31 import com.liferay.portal.RequiredOrganizationException;
32 import com.liferay.portal.SystemException;
33 import com.liferay.portal.kernel.util.GetterUtil;
34 import com.liferay.portal.kernel.util.OrderByComparator;
35 import com.liferay.portal.kernel.util.StringPool;
36 import com.liferay.portal.kernel.util.Validator;
37 import com.liferay.portal.model.Group;
38 import com.liferay.portal.model.Location;
39 import com.liferay.portal.model.Organization;
40 import com.liferay.portal.model.User;
41 import com.liferay.portal.model.impl.ListTypeImpl;
42 import com.liferay.portal.model.impl.OrganizationImpl;
43 import com.liferay.portal.model.impl.ResourceImpl;
44 import com.liferay.portal.security.permission.PermissionCacheUtil;
45 import com.liferay.portal.service.AddressLocalServiceUtil;
46 import com.liferay.portal.service.CountryServiceUtil;
47 import com.liferay.portal.service.EmailAddressLocalServiceUtil;
48 import com.liferay.portal.service.GroupLocalServiceUtil;
49 import com.liferay.portal.service.ListTypeServiceUtil;
50 import com.liferay.portal.service.PasswordPolicyRelLocalServiceUtil;
51 import com.liferay.portal.service.PhoneLocalServiceUtil;
52 import com.liferay.portal.service.ResourceLocalServiceUtil;
53 import com.liferay.portal.service.WebsiteLocalServiceUtil;
54 import com.liferay.portal.service.base.OrganizationLocalServiceBaseImpl;
55 import com.liferay.portal.service.persistence.GroupUtil;
56 import com.liferay.portal.service.persistence.OrganizationFinder;
57 import com.liferay.portal.service.persistence.OrganizationUtil;
58 import com.liferay.portal.service.persistence.UserUtil;
59 import com.liferay.portal.util.PropsUtil;
60 import com.liferay.portal.util.comparator.OrganizationNameComparator;
61
62 import java.rmi.RemoteException;
63
64 import java.util.ArrayList;
65 import java.util.Iterator;
66 import java.util.LinkedHashMap;
67 import java.util.List;
68
69
76 public class OrganizationLocalServiceImpl
77 extends OrganizationLocalServiceBaseImpl {
78
79 public void addGroupOrganizations(long groupId, long[] organizationIds)
80 throws PortalException, SystemException {
81
82 GroupUtil.addOrganizations(groupId, organizationIds);
83
84 PermissionCacheUtil.clearCache();
85 }
86
87 public Organization addOrganization(
88 long userId, long parentOrganizationId, String name,
89 boolean location, boolean recursable, long regionId, long countryId,
90 int statusId)
91 throws PortalException, SystemException {
92
93
95 User user = UserUtil.findByPrimaryKey(userId);
96 parentOrganizationId = getParentOrganizationId(
97 user.getCompanyId(), parentOrganizationId);
98
99 validate(
100 user.getCompanyId(), parentOrganizationId, name, location,
101 countryId, statusId);
102
103 long organizationId = CounterLocalServiceUtil.increment();
104
105 Organization organization = OrganizationUtil.create(organizationId);
106
107 organization.setCompanyId(user.getCompanyId());
108 organization.setParentOrganizationId(parentOrganizationId);
109 organization.setName(name);
110 organization.setLocation(location);
111 organization.setRecursable(recursable);
112 organization.setRegionId(regionId);
113 organization.setCountryId(countryId);
114 organization.setStatusId(statusId);
115
116 OrganizationUtil.update(organization);
117
118
120 GroupLocalServiceUtil.addGroup(
121 userId, Organization.class.getName(),
122 organization.getOrganizationId(), null, null, null, null, true);
123
124
126 addOrganizationResources(userId, organization);
127
128 return organization;
129 }
130
131 public void addOrganizationResources(long userId, Organization organization)
132 throws PortalException, SystemException {
133
134 String name = Organization.class.getName();
135
136 if (organization.isLocation()) {
137 name = Location.class.getName();
138 }
139
140 ResourceLocalServiceUtil.addResources(
141 organization.getCompanyId(), 0, userId, name,
142 organization.getOrganizationId(), false, false, false);
143 }
144
145 public void addPasswordPolicyOrganizations(
146 long passwordPolicyId, long[] organizationIds)
147 throws PortalException, SystemException {
148
149 PasswordPolicyRelLocalServiceUtil.addPasswordPolicyRels(
150 passwordPolicyId, Organization.class.getName(), organizationIds);
151 }
152
153 public void deleteOrganization(long organizationId)
154 throws PortalException, SystemException {
155
156 Organization organization =
157 OrganizationUtil.findByPrimaryKey(organizationId);
158
159 deleteOrganization(organization);
160 }
161
162 public void deleteOrganization(Organization organization)
163 throws PortalException, SystemException {
164
165 if ((OrganizationUtil.containsUsers(
166 organization.getOrganizationId())) ||
167 (OrganizationUtil.countByC_P(
168 organization.getCompanyId(),
169 organization.getOrganizationId()) > 0)) {
170
171 throw new RequiredOrganizationException();
172 }
173
174
176 AddressLocalServiceUtil.deleteAddresses(
177 organization.getCompanyId(), Organization.class.getName(),
178 organization.getOrganizationId());
179
180
182 EmailAddressLocalServiceUtil.deleteEmailAddresses(
183 organization.getCompanyId(), Organization.class.getName(),
184 organization.getOrganizationId());
185
186
188 PasswordPolicyRelLocalServiceUtil.deletePasswordPolicyRel(
189 Organization.class.getName(), organization.getOrganizationId());
190
191
193 PhoneLocalServiceUtil.deletePhones(
194 organization.getCompanyId(), Organization.class.getName(),
195 organization.getOrganizationId());
196
197
199 WebsiteLocalServiceUtil.deleteWebsites(
200 organization.getCompanyId(), Organization.class.getName(),
201 organization.getOrganizationId());
202
203
205 Group group = organization.getGroup();
206
207 GroupLocalServiceUtil.deleteGroup(group.getGroupId());
208
209
211 String name = Organization.class.getName();
212
213 if (organization.isLocation()) {
214 name = Location.class.getName();
215 }
216
217 ResourceLocalServiceUtil.deleteResource(
218 organization.getCompanyId(), name, ResourceImpl.SCOPE_INDIVIDUAL,
219 organization.getOrganizationId());
220
221
223 OrganizationUtil.remove(organization.getOrganizationId());
224
225
227 PermissionCacheUtil.clearCache();
228 }
229
230 public List getAncestorOrganizations(long organizationId)
231 throws PortalException, SystemException {
232
233 List organizations = null;
234
235 if (organizationId == OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
236 organizations = new ArrayList();
237 }
238 else {
239 Organization organization =
240 OrganizationUtil.findByPrimaryKey(organizationId);
241
242 organizations = getAncestorOrganizations(
243 organization.getParentOrganizationId());
244
245 organizations.add(organization);
246 }
247
248 return organizations;
249 }
250
251 public List getGroupOrganizations(long groupId)
252 throws PortalException, SystemException {
253
254 return GroupUtil.getOrganizations(groupId);
255 }
256
257 public Organization getOrganization(long organizationId)
258 throws PortalException, SystemException {
259
260 return OrganizationUtil.findByPrimaryKey(organizationId);
261 }
262
263 public long getOrganizationId(long companyId, String name)
264 throws PortalException, SystemException {
265
266 try {
267 Organization organization = OrganizationUtil.findByC_N(
268 companyId, name);
269
270 return organization.getOrganizationId();
271 }
272 catch (NoSuchOrganizationException nsoge) {
273 return 0;
274 }
275 }
276
277 public List getRecursableAncestorOrganizations(long organizationId)
278 throws PortalException, SystemException {
279
280 List organizations = new ArrayList();
281
282 Iterator itr = getAncestorOrganizations(organizationId).iterator();
283
284 while (itr.hasNext()){
285 Organization organization = (Organization)itr.next();
286
287 if (organization.isRecursable()){
288 organizations.add(organization);
289 }
290 }
291
292 return organizations;
293 }
294
295 public List getUserOrganizations(long userId)
296 throws PortalException, SystemException {
297
298 return UserUtil.getOrganizations(userId);
299 }
300
301 public boolean hasGroupOrganization(long groupId, long organizationId)
302 throws PortalException, SystemException {
303
304 return GroupUtil.containsOrganization(groupId, organizationId);
305 }
306
307 public boolean hasUserOrganization(long userId, long organizationId)
308 throws PortalException, SystemException {
309
310 return UserUtil.containsOrganization(userId, organizationId);
311 }
312
313 public boolean hasPasswordPolicyOrganization(
314 long passwordPolicyId, long organizationId)
315 throws PortalException, SystemException {
316
317 return PasswordPolicyRelLocalServiceUtil.hasPasswordPolicyRel(
318 passwordPolicyId, Organization.class.getName(), organizationId);
319 }
320
321 public boolean isAncestor(long locationId, long ancestorOrganizationId)
322 throws PortalException, SystemException {
323
324 Organization location = OrganizationUtil.findByPrimaryKey(locationId);
325
326 Iterator itr = getAncestorOrganizations(
327 ancestorOrganizationId).iterator();
328
329 while (itr.hasNext()){
330 Organization ancestor = (Organization)itr.next();
331
332 if (ancestor.getOrganizationId() ==
333 location.getParentOrganizationId()){
334
335 return true;
336 }
337 }
338
339 return false;
340 }
341
342 public List search(
343 long companyId, long parentOrganizationId, String keywords,
344 boolean location, Long regionId, Long countryId,
345 LinkedHashMap params, int begin, int end)
346 throws PortalException, SystemException {
347
348 return search(
349 companyId, parentOrganizationId, keywords, location, regionId,
350 countryId, params, begin, end,
351 new OrganizationNameComparator(true));
352 }
353
354 public List search(
355 long companyId, long parentOrganizationId, String keywords,
356 boolean location, Long regionId, Long countryId,
357 LinkedHashMap params, int begin, int end, OrderByComparator obc)
358 throws PortalException, SystemException {
359
360 String parentOrganizationComparator = StringPool.EQUAL;
361
362 if (parentOrganizationId ==
363 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
364
365 parentOrganizationComparator = StringPool.NOT_EQUAL;
366 }
367
368 if (location){
369 if (parentOrganizationId ==
370 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
371
372 parentOrganizationId =
373 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID;
374 }
375
376
379 List organizations = null;
380
381 if (parentOrganizationId ==
382 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
383
384 organizations = OrganizationFinder.findByKeywords(
385 companyId, parentOrganizationId,
386 parentOrganizationComparator, keywords, location, regionId,
387 countryId, params, begin, end, obc);
388 }
389 else {
390 Organization grandParentOrganization =
391 OrganizationUtil.findByPrimaryKey(parentOrganizationId);
392
393 long grandParentOrganizationId =
394 grandParentOrganization.getParentOrganizationId();
395
396 organizations = search(
397 companyId, grandParentOrganizationId, keywords, location,
398 regionId, countryId, params, begin, end, obc);
399
400 organizations.addAll(
401 OrganizationFinder.findByKeywords(
402 companyId, parentOrganizationId,
403 parentOrganizationComparator, keywords, location,
404 regionId, countryId, params, begin, end, obc));
405 }
406
407 return organizations;
408
409 }
410 else {
411 return OrganizationFinder.findByKeywords(
412 companyId, parentOrganizationId, parentOrganizationComparator,
413 keywords, location, regionId, countryId, params, begin, end,
414 obc);
415 }
416 }
417
418 public List search(
419 long companyId, long parentOrganizationId, String name,
420 boolean location, String street, String city, String zip,
421 Long regionId, Long countryId, LinkedHashMap params,
422 boolean andOperator, int begin, int end)
423 throws PortalException, SystemException {
424
425 return search(
426 companyId, parentOrganizationId, name, location, street, city, zip,
427 regionId, countryId, params, andOperator, begin, end,
428 new OrganizationNameComparator(true));
429 }
430
431 public List search(
432 long companyId, long parentOrganizationId, String name,
433 boolean location, String street, String city, String zip,
434 Long regionId, Long countryId, LinkedHashMap params,
435 boolean andOperator, int begin, int end, OrderByComparator obc)
436 throws PortalException, SystemException {
437
438 String parentOrganizationComparator = StringPool.EQUAL;
439
440 if (parentOrganizationId ==
441 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
442
443 parentOrganizationComparator = StringPool.NOT_EQUAL;
444 }
445
446 if (location){
447 if (parentOrganizationId ==
448 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
449
450 parentOrganizationId =
451 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID;
452 }
453
454
457 List organizations = null;
458
459 if (parentOrganizationId ==
460 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
461
462 organizations = OrganizationFinder.findByC_PO_N_L_S_C_Z_R_C(
463 companyId, parentOrganizationId,
464 parentOrganizationComparator, name, location, street, city,
465 zip, regionId, countryId, params, andOperator, begin, end,
466 obc);
467 }
468 else {
469 Organization grandParentOrganization =
470 OrganizationUtil.findByPrimaryKey(parentOrganizationId);
471
472 long grandParentOrganizationId =
473 grandParentOrganization.getParentOrganizationId();
474
475 organizations = search(
476 companyId, grandParentOrganizationId, name, location,
477 street, city, zip, regionId, countryId, params, andOperator,
478 begin, end, obc);
479
480 organizations.addAll(
481 OrganizationFinder.findByC_PO_N_L_S_C_Z_R_C(
482 companyId, parentOrganizationId,
483 parentOrganizationComparator, name, location, street,
484 city, zip, regionId, countryId, params, andOperator,
485 begin, end, obc));
486 }
487
488 return organizations;
489
490 }
491 else {
492 return OrganizationFinder.findByC_PO_N_L_S_C_Z_R_C(
493 companyId, parentOrganizationId, parentOrganizationComparator,
494 name, location, street, city, zip, regionId, countryId, params,
495 andOperator, begin, end, obc);
496 }
497 }
498
499 public int searchCount(
500 long companyId, long parentOrganizationId, String keywords,
501 boolean location, Long regionId, Long countryId,
502 LinkedHashMap params)
503 throws PortalException, SystemException {
504
505 String parentOrganizationComparator = StringPool.EQUAL;
506
507 if (parentOrganizationId ==
508 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
509
510 parentOrganizationComparator = StringPool.NOT_EQUAL;
511 }
512
513 if (location){
514 if (parentOrganizationId ==
515 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
516
517 parentOrganizationId =
518 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID;
519 }
520
521
524 int count = 0;
525
526 if (parentOrganizationId ==
527 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
528
529 count = OrganizationFinder.countByKeywords(
530 companyId, parentOrganizationId,
531 parentOrganizationComparator, keywords, location, regionId,
532 countryId, params);
533 }
534 else {
535 Organization grandParentOrganization =
536 OrganizationUtil.findByPrimaryKey(parentOrganizationId);
537
538 long grandParentOrganizationId =
539 grandParentOrganization.getParentOrganizationId();
540
541 count = searchCount(
542 companyId, grandParentOrganizationId, keywords, location,
543 regionId, countryId, params);
544
545 count += OrganizationFinder.countByKeywords(
546 companyId, parentOrganizationId,
547 parentOrganizationComparator, keywords, location, regionId,
548 countryId, params);
549 }
550
551 return count;
552 }
553 else {
554 return OrganizationFinder.countByKeywords(
555 companyId, parentOrganizationId, parentOrganizationComparator,
556 keywords, location, regionId, countryId, params);
557 }
558 }
559
560 public int searchCount(
561 long companyId, long parentOrganizationId, String name,
562 boolean location, String street, String city, String zip,
563 Long regionId, Long countryId, LinkedHashMap params,
564 boolean andOperator)
565 throws PortalException, SystemException {
566
567 String parentOrganizationComparator = StringPool.EQUAL;
568
569 if (parentOrganizationId ==
570 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
571
572 parentOrganizationComparator = StringPool.NOT_EQUAL;
573 }
574
575 if (location){
576 if (parentOrganizationId ==
577 OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
578
579 parentOrganizationId =
580 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID;
581 }
582
583
586 int count = 0;
587
588 if (parentOrganizationId ==
589 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
590
591 count = OrganizationFinder.countByC_PO_N_L_S_C_Z_R_C(
592 companyId, parentOrganizationId,
593 parentOrganizationComparator, name, location, street, city,
594 zip, regionId, countryId, params, andOperator);
595 }
596 else {
597 Organization grandParentOrganization =
598 OrganizationUtil.findByPrimaryKey(parentOrganizationId);
599
600 long grandParentOrganizationId =
601 grandParentOrganization.getParentOrganizationId();
602
603 count = searchCount(
604 companyId, grandParentOrganizationId, name, location,
605 street, city, zip, regionId, countryId, params,
606 andOperator);
607
608 count += OrganizationFinder.countByC_PO_N_L_S_C_Z_R_C(
609 companyId, parentOrganizationId,
610 parentOrganizationComparator, name, location, street, city,
611 zip, regionId, countryId, params, andOperator);
612 }
613
614 return count;
615 }
616 else {
617 return OrganizationFinder.countByC_PO_N_L_S_C_Z_R_C(
618 companyId, parentOrganizationId, parentOrganizationComparator,
619 name, location, street, city, zip, regionId, countryId, params,
620 andOperator);
621 }
622 }
623
624 public void setGroupOrganizations(long groupId, long[] organizationIds)
625 throws PortalException, SystemException {
626
627 GroupUtil.setOrganizations(groupId, organizationIds);
628
629 PermissionCacheUtil.clearCache();
630 }
631
632 public void unsetGroupOrganizations(long groupId, long[] organizationIds)
633 throws PortalException, SystemException {
634
635 GroupUtil.removeOrganizations(groupId, organizationIds);
636
637 PermissionCacheUtil.clearCache();
638 }
639
640 public void unsetPasswordPolicyOrganizations(
641 long passwordPolicyId, long[] organizationIds)
642 throws PortalException, SystemException {
643
644 PasswordPolicyRelLocalServiceUtil.deletePasswordPolicyRels(
645 passwordPolicyId, Organization.class.getName(), organizationIds);
646 }
647
648 public Organization updateOrganization(
649 long companyId, long organizationId, long parentOrganizationId,
650 String name, boolean location, boolean recursable, long regionId,
651 long countryId, int statusId)
652 throws PortalException, SystemException {
653
654 parentOrganizationId = getParentOrganizationId(
655 companyId, parentOrganizationId);
656
657 validate(
658 companyId, organizationId, parentOrganizationId, name, location,
659 countryId, statusId);
660
661 Organization organization =
662 OrganizationUtil.findByPrimaryKey(organizationId);
663
664 organization.setParentOrganizationId(parentOrganizationId);
665 organization.setName(name);
666 organization.setLocation(location);
667 organization.setRecursable(recursable);
668 organization.setRegionId(regionId);
669 organization.setCountryId(countryId);
670 organization.setStatusId(statusId);
671
672 OrganizationUtil.update(organization);
673
674 return organization;
675 }
676
677 public Organization updateOrganization(long organizationId, String comments)
678 throws PortalException, SystemException {
679
680 Organization organization =
681 OrganizationUtil.findByPrimaryKey(organizationId);
682
683 organization.setComments(comments);
684
685 OrganizationUtil.update(organization);
686
687 return organization;
688 }
689
690 protected long getParentOrganizationId(
691 long companyId, long parentOrganizationId)
692 throws PortalException, SystemException {
693
694 if (parentOrganizationId !=
695 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
696
697
700 try {
701 Organization parentOrganization =
702 OrganizationUtil.findByPrimaryKey(parentOrganizationId);
703
704 if (companyId != parentOrganization.getCompanyId()) {
705 parentOrganizationId =
706 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID;
707 }
708 }
709 catch (NoSuchOrganizationException nsoe) {
710 parentOrganizationId =
711 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID;
712 }
713 }
714
715 return parentOrganizationId;
716 }
717
718 protected void validate(
719 long companyId, long parentOrganizationId, String name,
720 boolean location, long countryId, int statusId)
721 throws PortalException, SystemException {
722
723 validate(
724 companyId, 0, parentOrganizationId, name, location, countryId,
725 statusId);
726 }
727
728 protected void validate(
729 long companyId, long organizationId, long parentOrganizationId,
730 String name, boolean location, long countryId, int statusId)
731 throws PortalException, SystemException {
732
733 if ((location) ||
734 (parentOrganizationId !=
735 OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID)) {
736
737 try {
738 Organization organization =
739 OrganizationUtil.findByPrimaryKey(parentOrganizationId);
740
741 if ((companyId != organization.getCompanyId()) ||
742 (parentOrganizationId == organizationId)) {
743
744 throw new OrganizationParentException();
745 }
746 }
747 catch (NoSuchOrganizationException nsoe) {
748 throw new OrganizationParentException();
749 }
750
751 }
752
753 if (Validator.isNull(name)) {
754 throw new OrganizationNameException();
755 }
756 else {
757 try {
758 Organization organization =
759 OrganizationUtil.findByC_N(companyId, name);
760
761 if (organization.getName().equalsIgnoreCase(name)) {
762 if ((organizationId <= 0) ||
763 (organization.getOrganizationId() != organizationId)) {
764
765 throw new DuplicateOrganizationException();
766 }
767 }
768 }
769 catch (NoSuchOrganizationException nsoe) {
770 }
771 }
772
773 try {
774 boolean countryRequired = GetterUtil.getBoolean(PropsUtil.get(
775 PropsUtil.ORGANIZATIONS_COUNTRY_REQUIRED));
776
777 if ((countryId > 0) || countryRequired) {
778 CountryServiceUtil.getCountry(countryId);
779 }
780
781 ListTypeServiceUtil.validate(
782 statusId, ListTypeImpl.ORGANIZATION_STATUS);
783 }
784 catch (RemoteException re) {
785 throw new SystemException(re);
786 }
787 }
788
789 }