001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.DuplicateOrganizationException;
018 import com.liferay.portal.OrganizationNameException;
019 import com.liferay.portal.OrganizationParentException;
020 import com.liferay.portal.OrganizationTypeException;
021 import com.liferay.portal.RequiredOrganizationException;
022 import com.liferay.portal.kernel.cache.ThreadLocalCachable;
023 import com.liferay.portal.kernel.configuration.Filter;
024 import com.liferay.portal.kernel.dao.orm.QueryUtil;
025 import com.liferay.portal.kernel.exception.PortalException;
026 import com.liferay.portal.kernel.exception.SystemException;
027 import com.liferay.portal.kernel.search.Hits;
028 import com.liferay.portal.kernel.search.Indexer;
029 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
030 import com.liferay.portal.kernel.search.QueryConfig;
031 import com.liferay.portal.kernel.search.SearchContext;
032 import com.liferay.portal.kernel.search.Sort;
033 import com.liferay.portal.kernel.util.ArrayUtil;
034 import com.liferay.portal.kernel.util.GetterUtil;
035 import com.liferay.portal.kernel.util.ListUtil;
036 import com.liferay.portal.kernel.util.OrderByComparator;
037 import com.liferay.portal.kernel.util.PropsKeys;
038 import com.liferay.portal.kernel.util.StringPool;
039 import com.liferay.portal.kernel.util.Validator;
040 import com.liferay.portal.kernel.workflow.WorkflowConstants;
041 import com.liferay.portal.model.Company;
042 import com.liferay.portal.model.Group;
043 import com.liferay.portal.model.GroupConstants;
044 import com.liferay.portal.model.LayoutSet;
045 import com.liferay.portal.model.ListTypeConstants;
046 import com.liferay.portal.model.Organization;
047 import com.liferay.portal.model.OrganizationConstants;
048 import com.liferay.portal.model.ResourceConstants;
049 import com.liferay.portal.model.Role;
050 import com.liferay.portal.model.RoleConstants;
051 import com.liferay.portal.model.User;
052 import com.liferay.portal.model.UserGroupRole;
053 import com.liferay.portal.model.impl.OrganizationImpl;
054 import com.liferay.portal.security.permission.PermissionCacheUtil;
055 import com.liferay.portal.service.ServiceContext;
056 import com.liferay.portal.service.base.OrganizationLocalServiceBaseImpl;
057 import com.liferay.portal.util.PropsUtil;
058 import com.liferay.portal.util.PropsValues;
059 import com.liferay.portal.util.comparator.OrganizationNameComparator;
060
061 import java.io.Serializable;
062
063 import java.util.ArrayList;
064 import java.util.Collections;
065 import java.util.HashMap;
066 import java.util.HashSet;
067 import java.util.Iterator;
068 import java.util.LinkedHashMap;
069 import java.util.List;
070 import java.util.Map;
071 import java.util.Set;
072
073
082 public class OrganizationLocalServiceImpl
083 extends OrganizationLocalServiceBaseImpl {
084
085
094 public void addGroupOrganizations(long groupId, long[] organizationIds)
095 throws PortalException, SystemException {
096
097 groupPersistence.addOrganizations(groupId, organizationIds);
098
099 PermissionCacheUtil.clearCache();
100 }
101
102
134 public Organization addOrganization(
135 long userId, long parentOrganizationId, String name, String type,
136 boolean recursable, long regionId, long countryId, int statusId,
137 String comments, boolean site, ServiceContext serviceContext)
138 throws PortalException, SystemException {
139
140
141
142 User user = userPersistence.findByPrimaryKey(userId);
143 parentOrganizationId = getParentOrganizationId(
144 user.getCompanyId(), parentOrganizationId);
145 recursable = true;
146
147 validate(
148 user.getCompanyId(), parentOrganizationId, name, type, countryId,
149 statusId);
150
151 long organizationId = counterLocalService.increment();
152
153 Organization organization = organizationPersistence.create(
154 organizationId);
155
156 organization.setCompanyId(user.getCompanyId());
157 organization.setParentOrganizationId(parentOrganizationId);
158
159 String treePath = organization.buildTreePath();
160
161 organization.setTreePath(treePath);
162
163 organization.setName(name);
164 organization.setType(type);
165 organization.setRecursable(recursable);
166 organization.setRegionId(regionId);
167 organization.setCountryId(countryId);
168 organization.setStatusId(statusId);
169 organization.setComments(comments);
170 organization.setExpandoBridgeAttributes(serviceContext);
171
172 organizationPersistence.update(organization);
173
174
175
176 long parentGroupId = GroupConstants.DEFAULT_PARENT_GROUP_ID;
177
178 if (parentOrganizationId !=
179 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
180
181 Organization parentOrganization =
182 organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
183
184 if (parentOrganization != null) {
185 parentGroupId = parentOrganization.getGroupId();
186 }
187 }
188
189 Group group = groupLocalService.addGroup(
190 userId, parentGroupId, Organization.class.getName(), organizationId,
191 GroupConstants.DEFAULT_LIVE_GROUP_ID, name, null,
192 GroupConstants.TYPE_SITE_PRIVATE, null, site, true, null);
193
194
195
196 Role role = roleLocalService.getRole(
197 organization.getCompanyId(), RoleConstants.ORGANIZATION_OWNER);
198
199 userGroupRoleLocalService.addUserGroupRoles(
200 userId, group.getGroupId(), new long[] {role.getRoleId()});
201
202
203
204 addOrganizationResources(userId, organization);
205
206
207
208 if (serviceContext != null) {
209 updateAsset(
210 userId, organization, serviceContext.getAssetCategoryIds(),
211 serviceContext.getAssetTagNames());
212 }
213
214
215
216 if ((serviceContext == null) || serviceContext.isIndexingEnabled()) {
217 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
218 Organization.class);
219
220 indexer.reindex(organization);
221 }
222
223 return organization;
224 }
225
226
235 public void addOrganizationResources(long userId, Organization organization)
236 throws PortalException, SystemException {
237
238 String name = Organization.class.getName();
239
240 resourceLocalService.addResources(
241 organization.getCompanyId(), 0, userId, name,
242 organization.getOrganizationId(), false, false, false);
243 }
244
245
253 public void addPasswordPolicyOrganizations(
254 long passwordPolicyId, long[] organizationIds)
255 throws SystemException {
256
257 passwordPolicyRelLocalService.addPasswordPolicyRels(
258 passwordPolicyId, Organization.class.getName(), organizationIds);
259 }
260
261
270 public void deleteLogo(long organizationId)
271 throws PortalException, SystemException {
272
273 Organization organization = getOrganization(organizationId);
274
275 Group group = organization.getGroup();
276
277 LayoutSet publicLayoutSet = layoutSetLocalService.getLayoutSet(
278 group.getGroupId(), false);
279
280 if (publicLayoutSet.isLogo()) {
281 long logoId = publicLayoutSet.getLogoId();
282
283 publicLayoutSet.setLogo(false);
284 publicLayoutSet.setLogoId(0);
285
286 layoutSetPersistence.update(publicLayoutSet);
287
288 imageLocalService.deleteImage(logoId);
289 }
290
291 LayoutSet privateLayoutSet = layoutSetLocalService.getLayoutSet(
292 group.getGroupId(), true);
293
294 if (privateLayoutSet.isLogo()) {
295 long logoId = privateLayoutSet.getLogoId();
296
297 privateLayoutSet.setLogo(false);
298 privateLayoutSet.setLogoId(0);
299
300 layoutSetPersistence.update(privateLayoutSet);
301
302 if (imageLocalService.getImage(logoId) != null) {
303 imageLocalService.deleteImage(logoId);
304 }
305 }
306 }
307
308
319 @Override
320 public Organization deleteOrganization(long organizationId)
321 throws PortalException, SystemException {
322
323 Organization organization = organizationPersistence.findByPrimaryKey(
324 organizationId);
325
326 return deleteOrganization(organization);
327 }
328
329
339 @Override
340 public Organization deleteOrganization(Organization organization)
341 throws PortalException, SystemException {
342
343 if ((userLocalService.getOrganizationUsersCount(
344 organization.getOrganizationId(),
345 WorkflowConstants.STATUS_APPROVED) > 0) ||
346 (organizationPersistence.countByC_P(
347 organization.getCompanyId(),
348 organization.getOrganizationId()) > 0)) {
349
350 throw new RequiredOrganizationException();
351 }
352
353
354
355 assetEntryLocalService.deleteEntry(
356 Organization.class.getName(), organization.getOrganizationId());
357
358
359
360 addressLocalService.deleteAddresses(
361 organization.getCompanyId(), Organization.class.getName(),
362 organization.getOrganizationId());
363
364
365
366 emailAddressLocalService.deleteEmailAddresses(
367 organization.getCompanyId(), Organization.class.getName(),
368 organization.getOrganizationId());
369
370
371
372 expandoValueLocalService.deleteValues(
373 Organization.class.getName(), organization.getOrganizationId());
374
375
376
377 passwordPolicyRelLocalService.deletePasswordPolicyRel(
378 Organization.class.getName(), organization.getOrganizationId());
379
380
381
382 phoneLocalService.deletePhones(
383 organization.getCompanyId(), Organization.class.getName(),
384 organization.getOrganizationId());
385
386
387
388 websiteLocalService.deleteWebsites(
389 organization.getCompanyId(), Organization.class.getName(),
390 organization.getOrganizationId());
391
392
393
394 Group group = organization.getGroup();
395
396 if (group.isSite()) {
397 group.setSite(false);
398
399 groupPersistence.update(group);
400 }
401
402 groupLocalService.deleteGroup(group);
403
404
405
406 String name = Organization.class.getName();
407
408 resourceLocalService.deleteResource(
409 organization.getCompanyId(), name,
410 ResourceConstants.SCOPE_INDIVIDUAL,
411 organization.getOrganizationId());
412
413
414
415 organizationPersistence.remove(organization);
416
417
418
419 PermissionCacheUtil.clearCache();
420
421 return organization;
422 }
423
424
431 public List<Organization> getGroupOrganizations(long groupId)
432 throws SystemException {
433
434 return groupPersistence.getOrganizations(groupId);
435 }
436
437
447 public Organization getOrganization(long companyId, String name)
448 throws PortalException, SystemException {
449
450 return organizationPersistence.findByC_N(companyId, name);
451 }
452
453
462 public long getOrganizationId(long companyId, String name)
463 throws SystemException {
464
465 Organization organization = organizationPersistence.fetchByC_N(
466 companyId, name);
467
468 if (organization != null) {
469 return organization.getOrganizationId();
470 }
471 else {
472 return 0;
473 }
474 }
475
476 public List<Organization> getOrganizations(
477 long userId, int start, int end, OrderByComparator obc)
478 throws PortalException, SystemException {
479
480 User user = userPersistence.findByPrimaryKey(userId);
481
482 List<Organization> organizations = ListUtil.copy(
483 userPersistence.getOrganizations(userId));
484
485 Iterator<Organization> iterator = organizations.iterator();
486
487 while (iterator.hasNext()) {
488 Organization organization = iterator.next();
489
490 if ((organization.getCompanyId() != user.getCompanyId()) ||
491 (organization.getParentOrganization() == null)) {
492
493 iterator.remove();
494 }
495 }
496
497 if (organizations.isEmpty()) {
498 return organizations;
499 }
500
501 if (obc == null) {
502 obc = new OrganizationNameComparator(true);
503 }
504
505 Collections.sort(organizations, obc);
506
507 if ((start != QueryUtil.ALL_POS) || (end != QueryUtil.ALL_POS)) {
508 organizations = ListUtil.subList(organizations, start, end);
509 }
510
511 return organizations;
512 }
513
514
523 public List<Organization> getOrganizations(
524 long companyId, long parentOrganizationId)
525 throws SystemException {
526
527 return getOrganizations(
528 companyId, parentOrganizationId, QueryUtil.ALL_POS,
529 QueryUtil.ALL_POS);
530 }
531
532
557 public List<Organization> getOrganizations(
558 long companyId, long parentOrganizationId, int start, int end)
559 throws SystemException {
560
561 if (parentOrganizationId ==
562 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
563
564 return organizationPersistence.findByCompanyId(
565 companyId, start, end);
566 }
567 else {
568 return organizationPersistence.findByC_P(
569 companyId, parentOrganizationId, start, end);
570 }
571 }
572
573
582 public List<Organization> getOrganizations(long[] organizationIds)
583 throws PortalException, SystemException {
584
585 List<Organization> organizations = new ArrayList<Organization>(
586 organizationIds.length);
587
588 for (long organizationId : organizationIds) {
589 Organization organization = getOrganization(organizationId);
590
591 organizations.add(organization);
592 }
593
594 return organizations;
595 }
596
597
606 public int getOrganizationsCount(long companyId, long parentOrganizationId)
607 throws SystemException {
608
609 if (parentOrganizationId ==
610 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
611
612 return organizationPersistence.countByCompanyId(companyId);
613 }
614 else {
615 return organizationPersistence.countByC_P(
616 companyId, parentOrganizationId);
617 }
618 }
619
620
630 public List<Organization> getParentOrganizations(long organizationId)
631 throws PortalException, SystemException {
632
633 if (organizationId ==
634 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
635
636 return new ArrayList<Organization>();
637 }
638
639 Organization organization = organizationPersistence.findByPrimaryKey(
640 organizationId);
641
642 return getParentOrganizations(organization, true);
643 }
644
645
653 public List<Organization> getSuborganizations(
654 List<Organization> organizations)
655 throws SystemException {
656
657 List<Organization> allSuborganizations = new ArrayList<Organization>();
658
659 for (int i = 0; i < organizations.size(); i++) {
660 Organization organization = organizations.get(i);
661
662 List<Organization> suborganizations =
663 organizationPersistence.findByC_P(
664 organization.getCompanyId(),
665 organization.getOrganizationId());
666
667 addSuborganizations(allSuborganizations, suborganizations);
668 }
669
670 return allSuborganizations;
671 }
672
673
682 public List<Organization> getSubsetOrganizations(
683 List<Organization> allOrganizations,
684 List<Organization> availableOrganizations) {
685
686 List<Organization> subsetOrganizations = new ArrayList<Organization>();
687
688 for (Organization organization : allOrganizations) {
689 if (availableOrganizations.contains(organization)) {
690 subsetOrganizations.add(organization);
691 }
692 }
693
694 return subsetOrganizations;
695 }
696
697
705 public List<Organization> getUserOrganizations(long userId)
706 throws PortalException, SystemException {
707
708 return getUserOrganizations(
709 userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
710 }
711
712
726 public List<Organization> getUserOrganizations(
727 long userId, boolean includeAdministrative)
728 throws PortalException, SystemException {
729
730 if (!includeAdministrative) {
731 return getUserOrganizations(userId);
732 }
733
734 Set<Organization> organizations = new HashSet<Organization>();
735
736 List<UserGroupRole> userGroupRoles =
737 userGroupRoleLocalService.getUserGroupRoles(userId);
738
739 for (UserGroupRole userGroupRole : userGroupRoles) {
740 Role role = userGroupRole.getRole();
741
742 String roleName = role.getName();
743
744 if (roleName.equals(RoleConstants.ORGANIZATION_ADMINISTRATOR) ||
745 roleName.equals(RoleConstants.ORGANIZATION_OWNER)) {
746
747 Group group = userGroupRole.getGroup();
748
749 Organization organization =
750 organizationPersistence.findByPrimaryKey(
751 group.getOrganizationId());
752
753 organizations.add(organization);
754 }
755 }
756
757 organizations.addAll(getUserOrganizations(userId));
758
759 return new ArrayList<Organization>(organizations);
760 }
761
762
783 public List<Organization> getUserOrganizations(
784 long userId, int start, int end)
785 throws PortalException, SystemException {
786
787 return userPersistence.getOrganizations(userId, start, end);
788 }
789
790
797 @ThreadLocalCachable
798 public int getUserOrganizationsCount(long userId) throws SystemException {
799 return userPersistence.getOrganizationsSize(userId);
800 }
801
802
811 public boolean hasGroupOrganization(long groupId, long organizationId)
812 throws SystemException {
813
814 return groupPersistence.containsOrganization(groupId, organizationId);
815 }
816
817
827 public boolean hasPasswordPolicyOrganization(
828 long passwordPolicyId, long organizationId)
829 throws SystemException {
830
831 return passwordPolicyRelLocalService.hasPasswordPolicyRel(
832 passwordPolicyId, Organization.class.getName(), organizationId);
833 }
834
835
846 public boolean hasUserOrganization(long userId, long organizationId)
847 throws SystemException {
848
849 return userPersistence.containsOrganization(userId, organizationId);
850 }
851
852
905 public boolean hasUserOrganization(
906 long userId, long organizationId, boolean inheritSuborganizations,
907 boolean includeSpecifiedOrganization)
908 throws PortalException, SystemException {
909
910 if (!inheritSuborganizations) {
911 return userPersistence.containsOrganization(userId, organizationId);
912 }
913
914 LinkedHashMap<String, Object> params =
915 new LinkedHashMap<String, Object>();
916
917 List<Organization> organizationsTree = new ArrayList<Organization>();
918
919 Organization organization = organizationPersistence.findByPrimaryKey(
920 organizationId);
921
922 if (!includeSpecifiedOrganization) {
923 organizationsTree.add(organization);
924 }
925 else {
926 organizationsTree.add(organization.getParentOrganization());
927 }
928
929 params.put("usersOrgsTree", organizationsTree);
930
931 if (userFinder.countByUser(userId, params) > 0) {
932 return true;
933 }
934
935 return false;
936 }
937
938
952 public void rebuildTree(long companyId)
953 throws PortalException, SystemException {
954
955 List<Organization> organizations =
956 organizationPersistence.findByCompanyId(companyId);
957
958 for (Organization organization : organizations) {
959 String treePath = organization.buildTreePath();
960
961 organization.setTreePath(treePath);
962
963 organizationPersistence.update(organization);
964 }
965 }
966
967
990 public List<Organization> search(
991 long companyId, LinkedHashMap<String, Object> params, int start,
992 int end)
993 throws SystemException {
994
995 return organizationFinder.findByCompanyId(
996 companyId, params, start, end,
997 new OrganizationNameComparator(true));
998 }
999
1000
1033 public Hits search(
1034 long companyId, long parentOrganizationId, String keywords,
1035 LinkedHashMap<String, Object> params, int start, int end, Sort sort)
1036 throws SystemException {
1037
1038 String name = null;
1039 String type = null;
1040 String street = null;
1041 String city = null;
1042 String zip = null;
1043 String region = null;
1044 String country = null;
1045 boolean andOperator = false;
1046
1047 if (Validator.isNotNull(keywords)) {
1048 name = keywords;
1049 type = keywords;
1050 street = keywords;
1051 city = keywords;
1052 zip = keywords;
1053 region = keywords;
1054 country = keywords;
1055 }
1056 else {
1057 andOperator = true;
1058 }
1059
1060 if (params != null) {
1061 params.put("keywords", keywords);
1062 }
1063
1064 return search(
1065 companyId, parentOrganizationId, name, type, street, city, zip,
1066 region, country, params, andOperator, start, end, sort);
1067 }
1068
1069
1106 public List<Organization> search(
1107 long companyId, long parentOrganizationId, String keywords,
1108 String type, Long regionId, Long countryId,
1109 LinkedHashMap<String, Object> params, int start, int end)
1110 throws SystemException {
1111
1112 return search(
1113 companyId, parentOrganizationId, keywords, type, regionId,
1114 countryId, params, start, end,
1115 new OrganizationNameComparator(true));
1116 }
1117
1118
1158 public List<Organization> search(
1159 long companyId, long parentOrganizationId, String keywords,
1160 String type, Long regionId, Long countryId,
1161 LinkedHashMap<String, Object> params, int start, int end,
1162 OrderByComparator obc)
1163 throws SystemException {
1164
1165 String parentOrganizationIdComparator = StringPool.EQUAL;
1166
1167 if (parentOrganizationId ==
1168 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
1169
1170 parentOrganizationIdComparator = StringPool.NOT_EQUAL;
1171 }
1172
1173 return organizationFinder.findByKeywords(
1174 companyId, parentOrganizationId, parentOrganizationIdComparator,
1175 keywords, type, regionId, countryId, params, start, end, obc);
1176 }
1177
1178
1222 public List<Organization> search(
1223 long companyId, long parentOrganizationId, String name, String type,
1224 String street, String city, String zip, Long regionId,
1225 Long countryId, LinkedHashMap<String, Object> params,
1226 boolean andOperator, int start, int end)
1227 throws SystemException {
1228
1229 return search(
1230 companyId, parentOrganizationId, name, type, street, city, zip,
1231 regionId, countryId, params, andOperator, start, end,
1232 new OrganizationNameComparator(true));
1233 }
1234
1235
1282 public List<Organization> search(
1283 long companyId, long parentOrganizationId, String name, String type,
1284 String street, String city, String zip, Long regionId,
1285 Long countryId, LinkedHashMap<String, Object> params,
1286 boolean andOperator, int start, int end, OrderByComparator obc)
1287 throws SystemException {
1288
1289 String parentOrganizationIdComparator = StringPool.EQUAL;
1290
1291 if (parentOrganizationId ==
1292 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
1293
1294 parentOrganizationIdComparator = StringPool.NOT_EQUAL;
1295 }
1296
1297 return organizationFinder.findByC_PO_N_T_S_C_Z_R_C(
1298 companyId, parentOrganizationId, parentOrganizationIdComparator,
1299 name, type, street, city, zip, regionId, countryId, params,
1300 andOperator, start, end, obc);
1301 }
1302
1303
1344 public Hits search(
1345 long companyId, long parentOrganizationId, String name, String type,
1346 String street, String city, String zip, String region,
1347 String country, LinkedHashMap<String, Object> params,
1348 boolean andSearch, int start, int end, Sort sort)
1349 throws SystemException {
1350
1351 try {
1352 SearchContext searchContext = new SearchContext();
1353
1354 searchContext.setAndSearch(andSearch);
1355
1356 Map<String, Serializable> attributes =
1357 new HashMap<String, Serializable>();
1358
1359 attributes.put("city", city);
1360 attributes.put("country", country);
1361 attributes.put("name", name);
1362 attributes.put("params", params);
1363
1364 if (parentOrganizationId !=
1365 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
1366
1367 attributes.put(
1368 "parentOrganizationId",
1369 String.valueOf(parentOrganizationId));
1370 }
1371
1372 attributes.put("region", region);
1373 attributes.put("street", street);
1374 attributes.put("type", type);
1375 attributes.put("zip", zip);
1376
1377 searchContext.setAttributes(attributes);
1378
1379 searchContext.setCompanyId(companyId);
1380 searchContext.setEnd(end);
1381
1382 if (params != null) {
1383 String keywords = (String)params.remove("keywords");
1384
1385 if (Validator.isNotNull(keywords)) {
1386 searchContext.setKeywords(keywords);
1387 }
1388 }
1389
1390 QueryConfig queryConfig = new QueryConfig();
1391
1392 queryConfig.setHighlightEnabled(false);
1393 queryConfig.setScoreEnabled(false);
1394
1395 searchContext.setQueryConfig(queryConfig);
1396
1397 if (sort != null) {
1398 searchContext.setSorts(new Sort[] {sort});
1399 }
1400
1401 searchContext.setStart(start);
1402
1403 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1404 Organization.class);
1405
1406 return indexer.search(searchContext);
1407 }
1408 catch (Exception e) {
1409 throw new SystemException(e);
1410 }
1411 }
1412
1413
1435 public int searchCount(
1436 long companyId, long parentOrganizationId, String keywords,
1437 String type, Long regionId, Long countryId,
1438 LinkedHashMap<String, Object> params)
1439 throws SystemException {
1440
1441 String parentOrganizationIdComparator = StringPool.EQUAL;
1442
1443 if (parentOrganizationId ==
1444 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
1445
1446 parentOrganizationIdComparator = StringPool.NOT_EQUAL;
1447 }
1448
1449 return organizationFinder.countByKeywords(
1450 companyId, parentOrganizationId, parentOrganizationIdComparator,
1451 keywords, type, regionId, countryId, params);
1452 }
1453
1454
1483 public int searchCount(
1484 long companyId, long parentOrganizationId, String name, String type,
1485 String street, String city, String zip, Long regionId,
1486 Long countryId, LinkedHashMap<String, Object> params,
1487 boolean andOperator)
1488 throws SystemException {
1489
1490 String parentOrganizationIdComparator = StringPool.EQUAL;
1491
1492 if (parentOrganizationId ==
1493 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
1494
1495 parentOrganizationIdComparator = StringPool.NOT_EQUAL;
1496 }
1497
1498 return organizationFinder.countByC_PO_N_T_S_C_Z_R_C(
1499 companyId, parentOrganizationId, parentOrganizationIdComparator,
1500 name, type, street, city, zip, regionId, countryId, params,
1501 andOperator);
1502 }
1503
1504
1513 public void setGroupOrganizations(long groupId, long[] organizationIds)
1514 throws PortalException, SystemException {
1515
1516 groupPersistence.setOrganizations(groupId, organizationIds);
1517
1518 PermissionCacheUtil.clearCache();
1519 }
1520
1521
1529 public void unsetGroupOrganizations(long groupId, long[] organizationIds)
1530 throws PortalException, SystemException {
1531
1532 groupPersistence.removeOrganizations(groupId, organizationIds);
1533
1534 PermissionCacheUtil.clearCache();
1535 }
1536
1537
1544 public void unsetPasswordPolicyOrganizations(
1545 long passwordPolicyId, long[] organizationIds)
1546 throws SystemException {
1547
1548 passwordPolicyRelLocalService.deletePasswordPolicyRels(
1549 passwordPolicyId, Organization.class.getName(), organizationIds);
1550 }
1551
1552
1563 public void updateAsset(
1564 long userId, Organization organization, long[] assetCategoryIds,
1565 String[] assetTagNames)
1566 throws PortalException, SystemException {
1567
1568 User user = userPersistence.findByPrimaryKey(userId);
1569
1570 Company company = companyPersistence.findByPrimaryKey(
1571 user.getCompanyId());
1572
1573 Group companyGroup = company.getGroup();
1574
1575 assetEntryLocalService.updateEntry(
1576 userId, companyGroup.getGroupId(), null, null,
1577 Organization.class.getName(), organization.getOrganizationId(),
1578 null, 0, assetCategoryIds, assetTagNames, false, null, null, null,
1579 null, organization.getName(), StringPool.BLANK, null, null, null, 0,
1580 0, null, false);
1581 }
1582
1583
1610 public Organization updateOrganization(
1611 long companyId, long organizationId, long parentOrganizationId,
1612 String name, String type, boolean recursable, long regionId,
1613 long countryId, int statusId, String comments, boolean site,
1614 ServiceContext serviceContext)
1615 throws PortalException, SystemException {
1616
1617
1618
1619 parentOrganizationId = getParentOrganizationId(
1620 companyId, parentOrganizationId);
1621 recursable = true;
1622
1623 validate(
1624 companyId, organizationId, parentOrganizationId, name, type,
1625 countryId, statusId);
1626
1627 Organization organization = organizationPersistence.findByPrimaryKey(
1628 organizationId);
1629
1630 long oldParentOrganizationId = organization.getParentOrganizationId();
1631 String oldName = organization.getName();
1632
1633 organization.setParentOrganizationId(parentOrganizationId);
1634
1635 String treePath = organization.buildTreePath();
1636
1637 organization.setTreePath(treePath);
1638
1639 organization.setName(name);
1640 organization.setType(type);
1641 organization.setRecursable(recursable);
1642 organization.setRegionId(regionId);
1643 organization.setCountryId(countryId);
1644 organization.setStatusId(statusId);
1645 organization.setComments(comments);
1646 organization.setExpandoBridgeAttributes(serviceContext);
1647
1648 organizationPersistence.update(organization);
1649
1650
1651
1652 Group group = organization.getGroup();
1653
1654 long parentGroupId = group.getParentGroupId();
1655
1656 boolean organizationGroup = isOrganizationGroup(
1657 oldParentOrganizationId, group.getParentGroupId());
1658
1659 if (organizationGroup) {
1660 if (parentOrganizationId !=
1661 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1662
1663 Organization parentOrganization =
1664 organizationPersistence.fetchByPrimaryKey(
1665 parentOrganizationId);
1666
1667 parentGroupId = parentOrganization.getGroupId();
1668 }
1669 else {
1670 parentGroupId = GroupConstants.DEFAULT_PARENT_GROUP_ID;
1671 }
1672 }
1673
1674 if (!oldName.equals(name) || organizationGroup) {
1675 groupLocalService.updateGroup(
1676 group.getGroupId(), parentGroupId, name, group.getDescription(),
1677 group.getType(), group.getFriendlyURL(), group.isActive(),
1678 null);
1679 }
1680
1681 if (group.isSite() != site) {
1682 groupLocalService.updateSite(group.getGroupId(), site);
1683 }
1684
1685
1686
1687 if (serviceContext != null) {
1688 updateAsset(
1689 serviceContext.getUserId(), organization,
1690 serviceContext.getAssetCategoryIds(),
1691 serviceContext.getAssetTagNames());
1692 }
1693
1694
1695
1696 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1697 Organization.class);
1698
1699 if (oldParentOrganizationId != parentOrganizationId) {
1700 long[] organizationIds = getReindexOrganizationIds(organization);
1701
1702 indexer.reindex(organizationIds);
1703 }
1704 else {
1705 indexer.reindex(organization);
1706 }
1707
1708 return organization;
1709 }
1710
1711 protected void addSuborganizations(
1712 List<Organization> allSuborganizations,
1713 List<Organization> organizations)
1714 throws SystemException {
1715
1716 for (Organization organization : organizations) {
1717 if (!allSuborganizations.contains(organization)) {
1718 allSuborganizations.add(organization);
1719
1720 List<Organization> suborganizations =
1721 organizationPersistence.findByC_P(
1722 organization.getCompanyId(),
1723 organization.getOrganizationId());
1724
1725 addSuborganizations(allSuborganizations, suborganizations);
1726 }
1727 }
1728 }
1729
1730 protected long getParentOrganizationId(
1731 long companyId, long parentOrganizationId)
1732 throws SystemException {
1733
1734 if (parentOrganizationId !=
1735 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1736
1737
1738
1739
1740 Organization parentOrganization =
1741 organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
1742
1743 if ((parentOrganization == null) ||
1744 (companyId != parentOrganization.getCompanyId())) {
1745
1746 parentOrganizationId =
1747 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID;
1748 }
1749 }
1750
1751 return parentOrganizationId;
1752 }
1753
1754 protected List<Organization> getParentOrganizations(
1755 Organization organization, boolean lastOrganization)
1756 throws PortalException, SystemException {
1757
1758 List<Organization> organizations = new ArrayList<Organization>();
1759
1760 if (!lastOrganization) {
1761 organizations.add(organization);
1762 }
1763
1764 long parentOrganizationId = organization.getParentOrganizationId();
1765
1766 if (parentOrganizationId ==
1767 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1768
1769 return organizations;
1770 }
1771
1772 Organization parentOrganization =
1773 organizationPersistence.findByPrimaryKey(parentOrganizationId);
1774
1775 List<Organization> parentOrganizatons = getParentOrganizations(
1776 parentOrganization, false);
1777
1778 organizations.addAll(parentOrganizatons);
1779
1780 return organizations;
1781 }
1782
1783 protected long[] getReindexOrganizationIds(Organization organization)
1784 throws PortalException, SystemException {
1785
1786 List<Organization> organizationsTree = new ArrayList<Organization>();
1787
1788 organizationsTree.add(organization);
1789
1790 LinkedHashMap<String, Object> params =
1791 new LinkedHashMap<String, Object>();
1792
1793 params.put("organizationsTree", organizationsTree);
1794
1795 List<Organization> organizations = search(
1796 organization.getCompanyId(), params, QueryUtil.ALL_POS,
1797 QueryUtil.ALL_POS);
1798
1799 long[] organizationIds = new long[organizations.size()];
1800
1801 for (int i = 0; i < organizations.size(); i++) {
1802 Organization curOrganization = organizations.get(i);
1803
1804 String treePath = curOrganization.buildTreePath();
1805
1806 curOrganization.setTreePath(treePath.toString());
1807
1808 organizationPersistence.update(curOrganization);
1809
1810 organizationIds[i] = curOrganization.getOrganizationId();
1811 }
1812
1813 if (!ArrayUtil.contains(
1814 organizationIds, organization.getOrganizationId())) {
1815
1816 organizationIds = ArrayUtil.append(
1817 organizationIds, organization.getOrganizationId());
1818 }
1819
1820 return organizationIds;
1821 }
1822
1823 protected boolean isOrganizationGroup(long organizationId, long groupId)
1824 throws SystemException {
1825
1826 if ((organizationId ==
1827 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) &&
1828 (groupId == GroupConstants.DEFAULT_PARENT_GROUP_ID)) {
1829
1830 return true;
1831 }
1832
1833 if (organizationId !=
1834 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1835
1836 Organization organization =
1837 organizationPersistence.fetchByPrimaryKey(organizationId);
1838
1839 if (organization.getGroupId() == groupId) {
1840 return true;
1841 }
1842 }
1843
1844 return false;
1845 }
1846
1847 protected boolean isParentOrganization(
1848 long parentOrganizationId, long organizationId)
1849 throws PortalException, SystemException {
1850
1851
1852
1853
1854 Organization parentOrganization =
1855 organizationPersistence.findByPrimaryKey(parentOrganizationId);
1856
1857 List<Organization> parentOrganizations = getParentOrganizations(
1858 organizationId);
1859
1860 if (parentOrganizations.contains(parentOrganization)) {
1861 return true;
1862 }
1863 else {
1864 return false;
1865 }
1866 }
1867
1868 protected void validate(
1869 long companyId, long organizationId, long parentOrganizationId,
1870 String name, String type, long countryId, int statusId)
1871 throws PortalException, SystemException {
1872
1873 if (!ArrayUtil.contains(PropsValues.ORGANIZATIONS_TYPES, type)) {
1874 throw new OrganizationTypeException(
1875 "Invalid organization type " + type);
1876 }
1877
1878 if (parentOrganizationId ==
1879 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1880
1881 if (!OrganizationImpl.isRootable(type)) {
1882 throw new OrganizationParentException(
1883 "Organization of type " + type + " cannot be a root");
1884 }
1885 }
1886 else {
1887 Organization parentOrganization =
1888 organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
1889
1890 if (parentOrganization == null) {
1891 throw new OrganizationParentException(
1892 "Organization " + parentOrganizationId + " doesn't exist");
1893 }
1894
1895 String[] childrenTypes = OrganizationImpl.getChildrenTypes(
1896 parentOrganization.getType());
1897
1898 if (childrenTypes.length == 0) {
1899 throw new OrganizationParentException(
1900 "Organization of type " + type + " cannot have children");
1901 }
1902
1903 if ((companyId != parentOrganization.getCompanyId()) ||
1904 (parentOrganizationId == organizationId)) {
1905
1906 throw new OrganizationParentException();
1907 }
1908
1909 if (!ArrayUtil.contains(childrenTypes, type)) {
1910 throw new OrganizationParentException(
1911 "Type " + type + " not allowed as child of " +
1912 parentOrganization.getType());
1913 }
1914 }
1915
1916 if ((organizationId > 0) &&
1917 (parentOrganizationId !=
1918 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
1919
1920
1921
1922 if (isParentOrganization(organizationId, parentOrganizationId)) {
1923 throw new OrganizationParentException();
1924 }
1925 }
1926
1927 if (Validator.isNull(name)) {
1928 throw new OrganizationNameException();
1929 }
1930 else {
1931 Organization organization = organizationPersistence.fetchByC_N(
1932 companyId, name);
1933
1934 if ((organization != null) &&
1935 organization.getName().equalsIgnoreCase(name)) {
1936
1937 if ((organizationId <= 0) ||
1938 (organization.getOrganizationId() != organizationId)) {
1939
1940 throw new DuplicateOrganizationException(
1941 "There is another organization named " + name);
1942 }
1943 }
1944 }
1945
1946 boolean countryRequired = GetterUtil.getBoolean(
1947 PropsUtil.get(
1948 PropsKeys.ORGANIZATIONS_COUNTRY_REQUIRED, new Filter(type)));
1949
1950 if (countryRequired || (countryId > 0)) {
1951 countryPersistence.findByPrimaryKey(countryId);
1952 }
1953
1954 listTypeService.validate(
1955 statusId, ListTypeConstants.ORGANIZATION_STATUS);
1956 }
1957
1958 protected void validate(
1959 long companyId, long parentOrganizationId, String name, String type,
1960 long countryId, int statusId)
1961 throws PortalException, SystemException {
1962
1963 validate(
1964 companyId, 0, parentOrganizationId, name, type, countryId,
1965 statusId);
1966 }
1967
1968 }