PageRenderTime 63ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/portal-impl/src/com/liferay/portal/service/impl/OrganizationLocalServiceImpl.java

https://github.com/viktorkovacs/liferay-portal-trunk
Java | 1075 lines | 744 code | 282 blank | 49 comment | 70 complexity | 47981e2213442bbbec1e388c95b6f7d7 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portal.service.impl;
  15. import com.liferay.portal.DuplicateOrganizationException;
  16. import com.liferay.portal.OrganizationNameException;
  17. import com.liferay.portal.OrganizationParentException;
  18. import com.liferay.portal.OrganizationTypeException;
  19. import com.liferay.portal.RequiredOrganizationException;
  20. import com.liferay.portal.kernel.cache.ThreadLocalCachable;
  21. import com.liferay.portal.kernel.configuration.Filter;
  22. import com.liferay.portal.kernel.dao.orm.QueryUtil;
  23. import com.liferay.portal.kernel.exception.PortalException;
  24. import com.liferay.portal.kernel.exception.SystemException;
  25. import com.liferay.portal.kernel.search.Hits;
  26. import com.liferay.portal.kernel.search.Indexer;
  27. import com.liferay.portal.kernel.search.IndexerRegistryUtil;
  28. import com.liferay.portal.kernel.search.QueryConfig;
  29. import com.liferay.portal.kernel.search.SearchContext;
  30. import com.liferay.portal.kernel.search.Sort;
  31. import com.liferay.portal.kernel.util.ArrayUtil;
  32. import com.liferay.portal.kernel.util.GetterUtil;
  33. import com.liferay.portal.kernel.util.OrderByComparator;
  34. import com.liferay.portal.kernel.util.PropsKeys;
  35. import com.liferay.portal.kernel.util.StringPool;
  36. import com.liferay.portal.kernel.util.Validator;
  37. import com.liferay.portal.kernel.workflow.WorkflowConstants;
  38. import com.liferay.portal.model.Company;
  39. import com.liferay.portal.model.Group;
  40. import com.liferay.portal.model.LayoutSet;
  41. import com.liferay.portal.model.ListTypeConstants;
  42. import com.liferay.portal.model.Organization;
  43. import com.liferay.portal.model.OrganizationConstants;
  44. import com.liferay.portal.model.ResourceConstants;
  45. import com.liferay.portal.model.Role;
  46. import com.liferay.portal.model.RoleConstants;
  47. import com.liferay.portal.model.User;
  48. import com.liferay.portal.model.impl.OrganizationImpl;
  49. import com.liferay.portal.security.permission.PermissionCacheUtil;
  50. import com.liferay.portal.service.ServiceContext;
  51. import com.liferay.portal.service.base.OrganizationLocalServiceBaseImpl;
  52. import com.liferay.portal.util.PropsUtil;
  53. import com.liferay.portal.util.PropsValues;
  54. import com.liferay.portal.util.comparator.OrganizationNameComparator;
  55. import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
  56. import com.liferay.portlet.expando.model.ExpandoBridge;
  57. import java.io.Serializable;
  58. import java.util.ArrayList;
  59. import java.util.HashMap;
  60. import java.util.Iterator;
  61. import java.util.LinkedHashMap;
  62. import java.util.List;
  63. import java.util.Map;
  64. /**
  65. * @author Brian Wing Shun Chan
  66. * @author Jorge Ferrer
  67. * @author Julio Camarero
  68. * @author Hugo Huijser
  69. * @author Juan Fernández
  70. */
  71. public class OrganizationLocalServiceImpl
  72. extends OrganizationLocalServiceBaseImpl {
  73. public void addGroupOrganizations(long groupId, long[] organizationIds)
  74. throws PortalException, SystemException {
  75. groupPersistence.addOrganizations(groupId, organizationIds);
  76. Indexer indexer = IndexerRegistryUtil.getIndexer(Organization.class);
  77. indexer.reindex(organizationIds);
  78. PermissionCacheUtil.clearCache();
  79. }
  80. public Organization addOrganization(
  81. long userId, long parentOrganizationId, String name, String type,
  82. boolean recursable, long regionId, long countryId, int statusId,
  83. String comments, ServiceContext serviceContext)
  84. throws PortalException, SystemException {
  85. // Organization
  86. User user = userPersistence.findByPrimaryKey(userId);
  87. parentOrganizationId = getParentOrganizationId(
  88. user.getCompanyId(), parentOrganizationId);
  89. recursable = true;
  90. validate(
  91. user.getCompanyId(), parentOrganizationId, name, type, countryId,
  92. statusId);
  93. long organizationId = counterLocalService.increment();
  94. Organization organization = organizationPersistence.create(
  95. organizationId);
  96. organization.setCompanyId(user.getCompanyId());
  97. organization.setParentOrganizationId(parentOrganizationId);
  98. organization.setName(name);
  99. organization.setType(type);
  100. organization.setRecursable(recursable);
  101. organization.setRegionId(regionId);
  102. organization.setCountryId(countryId);
  103. organization.setStatusId(statusId);
  104. organization.setComments(comments);
  105. organizationPersistence.update(organization, false);
  106. // Group
  107. Group group = groupLocalService.addGroup(
  108. userId, Organization.class.getName(), organizationId, name, null,
  109. 0, null, true, null);
  110. if (PropsValues.ORGANIZATIONS_ASSIGNMENT_AUTO) {
  111. // Role
  112. Role role = roleLocalService.getRole(
  113. organization.getCompanyId(), RoleConstants.ORGANIZATION_OWNER);
  114. userGroupRoleLocalService.addUserGroupRoles(
  115. userId, group.getGroupId(), new long[] {role.getRoleId()});
  116. // User
  117. userPersistence.addOrganization(userId, organizationId);
  118. }
  119. // Resources
  120. addOrganizationResources(userId, organization);
  121. // Asset
  122. if (serviceContext != null) {
  123. updateAsset(
  124. userId, organization, serviceContext.getAssetCategoryIds(),
  125. serviceContext.getAssetTagNames());
  126. }
  127. // Expando
  128. ExpandoBridge expandoBridge = organization.getExpandoBridge();
  129. expandoBridge.setAttributes(serviceContext);
  130. // Indexer
  131. Indexer indexer = IndexerRegistryUtil.getIndexer(Organization.class);
  132. indexer.reindex(
  133. new String[] {String.valueOf(organization.getCompanyId())});
  134. return organization;
  135. }
  136. public void addOrganizationResources(long userId, Organization organization)
  137. throws PortalException, SystemException {
  138. String name = Organization.class.getName();
  139. resourceLocalService.addResources(
  140. organization.getCompanyId(), 0, userId, name,
  141. organization.getOrganizationId(), false, false, false);
  142. }
  143. public void addPasswordPolicyOrganizations(
  144. long passwordPolicyId, long[] organizationIds)
  145. throws SystemException {
  146. passwordPolicyRelLocalService.addPasswordPolicyRels(
  147. passwordPolicyId, Organization.class.getName(), organizationIds);
  148. }
  149. public void deleteLogo(long organizationId)
  150. throws PortalException, SystemException {
  151. Organization organization = getOrganization(organizationId);
  152. Group group = organization.getGroup();
  153. LayoutSet publicLayoutSet = layoutSetLocalService.getLayoutSet(
  154. group.getGroupId(), false);
  155. if (publicLayoutSet.isLogo()) {
  156. long logoId = publicLayoutSet.getLogoId();
  157. publicLayoutSet.setLogo(false);
  158. publicLayoutSet.setLogoId(0);
  159. layoutSetPersistence.update(publicLayoutSet, false);
  160. imageLocalService.deleteImage(logoId);
  161. }
  162. LayoutSet privateLayoutSet = layoutSetLocalService.getLayoutSet(
  163. group.getGroupId(), true);
  164. if (privateLayoutSet.isLogo()) {
  165. long logoId = privateLayoutSet.getLogoId();
  166. privateLayoutSet.setLogo(false);
  167. privateLayoutSet.setLogoId(0);
  168. layoutSetPersistence.update(privateLayoutSet, false);
  169. if (imageLocalService.getImage(logoId) != null) {
  170. imageLocalService.deleteImage(logoId);
  171. }
  172. }
  173. }
  174. public void deleteOrganization(long organizationId)
  175. throws PortalException, SystemException {
  176. Organization organization = organizationPersistence.findByPrimaryKey(
  177. organizationId);
  178. deleteOrganization(organization);
  179. }
  180. public void deleteOrganization(Organization organization)
  181. throws PortalException, SystemException {
  182. if ((userLocalService.getOrganizationUsersCount(
  183. organization.getOrganizationId(),
  184. WorkflowConstants.STATUS_APPROVED) > 0) ||
  185. (organizationPersistence.countByC_P(
  186. organization.getCompanyId(),
  187. organization.getOrganizationId()) > 0)) {
  188. throw new RequiredOrganizationException();
  189. }
  190. // Asset
  191. assetEntryLocalService.deleteEntry(
  192. Organization.class.getName(), organization.getOrganizationId());
  193. // Addresses
  194. addressLocalService.deleteAddresses(
  195. organization.getCompanyId(), Organization.class.getName(),
  196. organization.getOrganizationId());
  197. // Email addresses
  198. emailAddressLocalService.deleteEmailAddresses(
  199. organization.getCompanyId(), Organization.class.getName(),
  200. organization.getOrganizationId());
  201. // Expando
  202. expandoValueLocalService.deleteValues(
  203. Organization.class.getName(), organization.getOrganizationId());
  204. // Password policy relation
  205. passwordPolicyRelLocalService.deletePasswordPolicyRel(
  206. Organization.class.getName(), organization.getOrganizationId());
  207. // Phone
  208. phoneLocalService.deletePhones(
  209. organization.getCompanyId(), Organization.class.getName(),
  210. organization.getOrganizationId());
  211. // Website
  212. websiteLocalService.deleteWebsites(
  213. organization.getCompanyId(), Organization.class.getName(),
  214. organization.getOrganizationId());
  215. // Group
  216. Group group = organization.getGroup();
  217. groupLocalService.deleteGroup(group);
  218. // Resources
  219. String name = Organization.class.getName();
  220. resourceLocalService.deleteResource(
  221. organization.getCompanyId(), name,
  222. ResourceConstants.SCOPE_INDIVIDUAL,
  223. organization.getOrganizationId());
  224. // Organization
  225. organizationPersistence.remove(organization);
  226. // Permission cache
  227. PermissionCacheUtil.clearCache();
  228. // Indexer
  229. Indexer indexer = IndexerRegistryUtil.getIndexer(Organization.class);
  230. indexer.delete(organization);
  231. indexer.reindex(
  232. new String[] {String.valueOf(organization.getCompanyId())});
  233. }
  234. public List<Organization> getGroupOrganizations(long groupId)
  235. throws SystemException {
  236. return groupPersistence.getOrganizations(groupId);
  237. }
  238. public Organization getOrganization(long organizationId)
  239. throws PortalException, SystemException {
  240. return organizationPersistence.findByPrimaryKey(organizationId);
  241. }
  242. public Organization getOrganization(long companyId, String name)
  243. throws PortalException, SystemException {
  244. return organizationPersistence.findByC_N(companyId, name);
  245. }
  246. public long getOrganizationId(long companyId, String name)
  247. throws SystemException {
  248. Organization organization = organizationPersistence.fetchByC_N(
  249. companyId, name);
  250. if (organization != null) {
  251. return organization.getOrganizationId();
  252. }
  253. else {
  254. return 0;
  255. }
  256. }
  257. public List<Organization> getOrganizations(
  258. long companyId, long parentOrganizationId)
  259. throws SystemException {
  260. if (parentOrganizationId ==
  261. OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
  262. return organizationPersistence.findByCompanyId(companyId);
  263. }
  264. else {
  265. return organizationPersistence.findByC_P(
  266. companyId, parentOrganizationId);
  267. }
  268. }
  269. public List<Organization> getOrganizations(
  270. long companyId, long parentOrganizationId, int start, int end)
  271. throws SystemException {
  272. return organizationPersistence.findByC_P(
  273. companyId, parentOrganizationId, start, end);
  274. }
  275. public List<Organization> getOrganizations(long[] organizationIds)
  276. throws PortalException, SystemException {
  277. List<Organization> organizations = new ArrayList<Organization>(
  278. organizationIds.length);
  279. for (long organizationId : organizationIds) {
  280. Organization organization = getOrganization(organizationId);
  281. organizations.add(organization);
  282. }
  283. return organizations;
  284. }
  285. public int getOrganizationsCount(long companyId, long parentOrganizationId)
  286. throws SystemException {
  287. if (parentOrganizationId ==
  288. OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
  289. return organizationPersistence.countByCompanyId(companyId);
  290. }
  291. else {
  292. return organizationPersistence.countByC_P(
  293. companyId, parentOrganizationId);
  294. }
  295. }
  296. public List<Organization> getParentOrganizations(long organizationId)
  297. throws PortalException, SystemException {
  298. if (organizationId ==
  299. OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
  300. return new ArrayList<Organization>();
  301. }
  302. Organization organization =
  303. organizationPersistence.findByPrimaryKey(organizationId);
  304. return getParentOrganizations(organization, true);
  305. }
  306. public List<Organization> getSuborganizations(
  307. List<Organization> organizations)
  308. throws SystemException {
  309. List<Organization> allSuborganizations = new ArrayList<Organization>();
  310. for (int i = 0; i < organizations.size(); i++) {
  311. Organization organization = organizations.get(i);
  312. List<Organization> suborganizations =
  313. organizationPersistence.findByC_P(
  314. organization.getCompanyId(),
  315. organization.getOrganizationId());
  316. addSuborganizations(allSuborganizations, suborganizations);
  317. }
  318. return allSuborganizations;
  319. }
  320. public List<Organization> getSubsetOrganizations(
  321. List<Organization> allOrganizations,
  322. List<Organization> availableOrganizations) {
  323. List<Organization> subsetOrganizations = new ArrayList<Organization>();
  324. Iterator<Organization> itr = allOrganizations.iterator();
  325. while (itr.hasNext()) {
  326. Organization organization = itr.next();
  327. if (availableOrganizations.contains(organization)) {
  328. subsetOrganizations.add(organization);
  329. }
  330. }
  331. return subsetOrganizations;
  332. }
  333. public List<Organization> getUserOrganizations(long userId)
  334. throws PortalException, SystemException {
  335. return getUserOrganizations(userId, false);
  336. }
  337. public List<Organization> getUserOrganizations(
  338. long userId, boolean inheritUserGroups)
  339. throws PortalException, SystemException {
  340. return getUserOrganizations(
  341. userId, inheritUserGroups, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
  342. }
  343. public List<Organization> getUserOrganizations(
  344. long userId, boolean inheritUserGroups, int start, int end)
  345. throws PortalException, SystemException {
  346. if (inheritUserGroups &&
  347. PropsValues.ORGANIZATIONS_USER_GROUP_MEMBERSHIP_ENABLED) {
  348. User user = userPersistence.findByPrimaryKey(userId);
  349. LinkedHashMap<String, Object> organizationParams =
  350. new LinkedHashMap<String, Object>();
  351. organizationParams.put("usersOrgs", new Long(userId));
  352. return search(
  353. user.getCompanyId(),
  354. OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null, null,
  355. null, null, organizationParams, start, end);
  356. }
  357. else {
  358. return userPersistence.getOrganizations(userId, start, end);
  359. }
  360. }
  361. public List<Organization> getUserOrganizations(
  362. long userId, int start, int end)
  363. throws PortalException, SystemException {
  364. return getUserOrganizations(userId, false, start, end);
  365. }
  366. @ThreadLocalCachable
  367. public int getUserOrganizationsCount(long userId) throws SystemException {
  368. return userPersistence.getOrganizationsSize(userId);
  369. }
  370. public boolean hasGroupOrganization(long groupId, long organizationId)
  371. throws SystemException {
  372. return groupPersistence.containsOrganization(groupId, organizationId);
  373. }
  374. public boolean hasPasswordPolicyOrganization(
  375. long passwordPolicyId, long organizationId)
  376. throws SystemException {
  377. return passwordPolicyRelLocalService.hasPasswordPolicyRel(
  378. passwordPolicyId, Organization.class.getName(), organizationId);
  379. }
  380. public boolean hasUserOrganization(long userId, long organizationId)
  381. throws SystemException {
  382. return userPersistence.containsOrganization(userId, organizationId);
  383. }
  384. public boolean hasUserOrganization(
  385. long userId, long organizationId, boolean inheritSuborganizations,
  386. boolean inheritUserGroups, boolean includeSpecifiedOrganization)
  387. throws PortalException, SystemException {
  388. if (!inheritSuborganizations && !inheritUserGroups) {
  389. return userPersistence.containsOrganization(userId, organizationId);
  390. }
  391. if (inheritSuborganizations) {
  392. LinkedHashMap<String, Object> params =
  393. new LinkedHashMap<String, Object>();
  394. Long[][] leftAndRightOrganizationIds =
  395. EnterpriseAdminUtil.getLeftAndRightOrganizationIds(
  396. organizationId);
  397. if (!includeSpecifiedOrganization) {
  398. leftAndRightOrganizationIds[0][0] =
  399. leftAndRightOrganizationIds[0][0].longValue() + 1;
  400. }
  401. params.put("usersOrgsTree", leftAndRightOrganizationIds);
  402. if (userFinder.countByUser(userId, params) > 0) {
  403. return true;
  404. }
  405. }
  406. if (inheritUserGroups) {
  407. if (organizationFinder.countByO_U(organizationId, userId) > 0) {
  408. return true;
  409. }
  410. }
  411. return false;
  412. }
  413. public void rebuildTree(long companyId, boolean force)
  414. throws SystemException {
  415. organizationPersistence.rebuildTree(companyId, force);
  416. }
  417. public Hits search(
  418. long companyId, long parentOrganizationId, String keywords,
  419. LinkedHashMap<String, Object> params, int start, int end, Sort sort)
  420. throws SystemException {
  421. String name = null;
  422. String type = null;
  423. String street = null;
  424. String city = null;
  425. String zip = null;
  426. String region = null;
  427. String country = null;
  428. boolean andOperator = false;
  429. if (Validator.isNotNull(keywords)) {
  430. name = keywords;
  431. type = keywords;
  432. street = keywords;
  433. city = keywords;
  434. zip = keywords;
  435. region = keywords;
  436. country = keywords;
  437. }
  438. else {
  439. andOperator = true;
  440. }
  441. return search(
  442. companyId, parentOrganizationId, name, type, street, city, zip,
  443. region, country, params, andOperator, start, end, sort);
  444. }
  445. public List<Organization> search(
  446. long companyId, long parentOrganizationId, String keywords,
  447. String type, Long regionId, Long countryId,
  448. LinkedHashMap<String, Object> params,
  449. int start, int end)
  450. throws SystemException {
  451. return search(
  452. companyId, parentOrganizationId, keywords, type, regionId,
  453. countryId, params, start, end,
  454. new OrganizationNameComparator(true));
  455. }
  456. public List<Organization> search(
  457. long companyId, long parentOrganizationId, String keywords,
  458. String type, Long regionId, Long countryId,
  459. LinkedHashMap<String, Object> params,
  460. int start, int end, OrderByComparator obc)
  461. throws SystemException {
  462. String parentOrganizationIdComparator = StringPool.EQUAL;
  463. if (parentOrganizationId ==
  464. OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
  465. parentOrganizationIdComparator = StringPool.NOT_EQUAL;
  466. }
  467. return organizationFinder.findByKeywords(
  468. companyId, parentOrganizationId, parentOrganizationIdComparator,
  469. keywords, type, regionId, countryId, params, start, end,
  470. obc);
  471. }
  472. public List<Organization> search(
  473. long companyId, long parentOrganizationId, String name, String type,
  474. String street, String city, String zip,
  475. Long regionId, Long countryId,
  476. LinkedHashMap<String, Object> params, boolean andOperator,
  477. int start, int end)
  478. throws SystemException {
  479. return search(
  480. companyId, parentOrganizationId, name, type, street, city, zip,
  481. regionId, countryId, params, andOperator, start, end,
  482. new OrganizationNameComparator(true));
  483. }
  484. public List<Organization> search(
  485. long companyId, long parentOrganizationId, String name, String type,
  486. String street, String city, String zip,
  487. Long regionId, Long countryId, LinkedHashMap<String, Object> params,
  488. boolean andOperator, int start, int end, OrderByComparator obc)
  489. throws SystemException {
  490. String parentOrganizationIdComparator = StringPool.EQUAL;
  491. if (parentOrganizationId ==
  492. OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
  493. parentOrganizationIdComparator = StringPool.NOT_EQUAL;
  494. }
  495. return organizationFinder.findByC_PO_N_T_S_C_Z_R_C(
  496. companyId, parentOrganizationId, parentOrganizationIdComparator,
  497. name, type, street, city, zip, regionId, countryId, params,
  498. andOperator, start, end, obc);
  499. }
  500. public Hits search(
  501. long companyId, long parentOrganizationId, String name, String type,
  502. String street, String city, String zip, String region,
  503. String country, LinkedHashMap<String, Object> params,
  504. boolean andSearch, int start, int end, Sort sort)
  505. throws SystemException {
  506. try {
  507. Map<String, Serializable> attributes =
  508. new HashMap<String, Serializable>();
  509. attributes.put("city", city);
  510. attributes.put("country", country);
  511. attributes.put("name", name);
  512. attributes.put("params", params);
  513. if (parentOrganizationId !=
  514. OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
  515. attributes.put(
  516. "parentOrganizationId",
  517. String.valueOf(parentOrganizationId));
  518. }
  519. attributes.put("region", region);
  520. attributes.put("street", street);
  521. attributes.put("type", type);
  522. attributes.put("zip", zip);
  523. SearchContext searchContext = new SearchContext();
  524. searchContext.setAndSearch(andSearch);
  525. searchContext.setAttributes(attributes);
  526. searchContext.setCompanyId(companyId);
  527. searchContext.setEnd(end);
  528. searchContext.setSorts(new Sort[] {sort});
  529. QueryConfig queryConfig = new QueryConfig();
  530. queryConfig.setHighlightEnabled(false);
  531. queryConfig.setScoreEnabled(false);
  532. searchContext.setQueryConfig(queryConfig);
  533. searchContext.setStart(start);
  534. Indexer indexer = IndexerRegistryUtil.getIndexer(
  535. Organization.class);
  536. return indexer.search(searchContext);
  537. }
  538. catch (Exception e) {
  539. throw new SystemException(e);
  540. }
  541. }
  542. public int searchCount(
  543. long companyId, long parentOrganizationId, String keywords,
  544. String type, Long regionId, Long countryId,
  545. LinkedHashMap<String, Object> params)
  546. throws SystemException {
  547. String parentOrganizationIdComparator = StringPool.EQUAL;
  548. if (parentOrganizationId ==
  549. OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
  550. parentOrganizationIdComparator = StringPool.NOT_EQUAL;
  551. }
  552. return organizationFinder.countByKeywords(
  553. companyId, parentOrganizationId, parentOrganizationIdComparator,
  554. keywords, type, regionId, countryId, params);
  555. }
  556. public int searchCount(
  557. long companyId, long parentOrganizationId, String name, String type,
  558. String street, String city, String zip,
  559. Long regionId, Long countryId, LinkedHashMap<String, Object> params,
  560. boolean andOperator)
  561. throws SystemException {
  562. String parentOrganizationIdComparator = StringPool.EQUAL;
  563. if (parentOrganizationId ==
  564. OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
  565. parentOrganizationIdComparator = StringPool.NOT_EQUAL;
  566. }
  567. return organizationFinder.countByC_PO_N_T_S_C_Z_R_C(
  568. companyId, parentOrganizationId, parentOrganizationIdComparator,
  569. name, type, street, city, zip, regionId, countryId, params,
  570. andOperator);
  571. }
  572. public void setGroupOrganizations(long groupId, long[] organizationIds)
  573. throws PortalException, SystemException {
  574. groupPersistence.setOrganizations(groupId, organizationIds);
  575. Indexer indexer = IndexerRegistryUtil.getIndexer(Organization.class);
  576. indexer.reindex(organizationIds);
  577. PermissionCacheUtil.clearCache();
  578. }
  579. public void unsetGroupOrganizations(long groupId, long[] organizationIds)
  580. throws PortalException, SystemException {
  581. groupPersistence.removeOrganizations(groupId, organizationIds);
  582. Indexer indexer = IndexerRegistryUtil.getIndexer(Organization.class);
  583. indexer.reindex(organizationIds);
  584. PermissionCacheUtil.clearCache();
  585. }
  586. public void unsetPasswordPolicyOrganizations(
  587. long passwordPolicyId, long[] organizationIds)
  588. throws SystemException {
  589. passwordPolicyRelLocalService.deletePasswordPolicyRels(
  590. passwordPolicyId, Organization.class.getName(), organizationIds);
  591. }
  592. public void updateAsset(
  593. long userId, Organization organization, long[] assetCategoryIds,
  594. String[] assetTagNames)
  595. throws PortalException, SystemException {
  596. User user = userPersistence.findByPrimaryKey(userId);
  597. Company company = companyPersistence.findByPrimaryKey(
  598. user.getCompanyId());
  599. Group companyGroup = company.getGroup();
  600. assetEntryLocalService.updateEntry(
  601. userId, companyGroup.getGroupId(), Organization.class.getName(),
  602. organization.getOrganizationId(), null, assetCategoryIds,
  603. assetTagNames, false, null, null, null, null, null,
  604. organization.getName(), StringPool.BLANK, null, null, null, 0, 0,
  605. null, false);
  606. }
  607. public Organization updateOrganization(
  608. long companyId, long organizationId, long parentOrganizationId,
  609. String name, String type, boolean recursable, long regionId,
  610. long countryId, int statusId, String comments,
  611. ServiceContext serviceContext)
  612. throws PortalException, SystemException {
  613. // Organization
  614. parentOrganizationId = getParentOrganizationId(
  615. companyId, parentOrganizationId);
  616. recursable = true;
  617. validate(
  618. companyId, organizationId, parentOrganizationId, name, type,
  619. countryId, statusId);
  620. Organization organization = organizationPersistence.findByPrimaryKey(
  621. organizationId);
  622. organization.setParentOrganizationId(parentOrganizationId);
  623. organization.setName(name);
  624. organization.setType(type);
  625. organization.setRecursable(recursable);
  626. organization.setRegionId(regionId);
  627. organization.setCountryId(countryId);
  628. organization.setStatusId(statusId);
  629. organization.setComments(comments);
  630. organizationPersistence.update(organization, false);
  631. // Asset
  632. if (serviceContext != null) {
  633. updateAsset(
  634. serviceContext.getUserId(), organization,
  635. serviceContext.getAssetCategoryIds(),
  636. serviceContext.getAssetTagNames());
  637. }
  638. // Expando
  639. ExpandoBridge expandoBridge = organization.getExpandoBridge();
  640. expandoBridge.setAttributes(serviceContext);
  641. // Indexer
  642. Indexer indexer = IndexerRegistryUtil.getIndexer(Organization.class);
  643. indexer.reindex(
  644. new String[] {String.valueOf(organization.getCompanyId())});
  645. return organization;
  646. }
  647. protected void addSuborganizations(
  648. List<Organization> allSuborganizations,
  649. List<Organization> organizations)
  650. throws SystemException {
  651. for (Organization organization : organizations) {
  652. if (!allSuborganizations.contains(organization)) {
  653. allSuborganizations.add(organization);
  654. List<Organization> suborganizations =
  655. organizationPersistence.findByC_P(
  656. organization.getCompanyId(),
  657. organization.getOrganizationId());
  658. addSuborganizations(allSuborganizations, suborganizations);
  659. }
  660. }
  661. }
  662. protected long getParentOrganizationId(
  663. long companyId, long parentOrganizationId)
  664. throws SystemException {
  665. if (parentOrganizationId !=
  666. OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
  667. // Ensure parent organization exists and belongs to the proper
  668. // company
  669. Organization parentOrganization =
  670. organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
  671. if ((parentOrganization == null) ||
  672. (companyId != parentOrganization.getCompanyId())) {
  673. parentOrganizationId =
  674. OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID;
  675. }
  676. }
  677. return parentOrganizationId;
  678. }
  679. protected List<Organization> getParentOrganizations(
  680. Organization organization, boolean lastOrganization)
  681. throws PortalException, SystemException {
  682. List<Organization> organizations = new ArrayList<Organization>();
  683. if (!lastOrganization) {
  684. organizations.add(organization);
  685. }
  686. long parentOrganizationId = organization.getParentOrganizationId();
  687. if (parentOrganizationId ==
  688. OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
  689. return organizations;
  690. }
  691. Organization parentOrganization =
  692. organizationPersistence.findByPrimaryKey(parentOrganizationId);
  693. List<Organization> parentOrganizatons = getParentOrganizations(
  694. parentOrganization, false);
  695. organizations.addAll(parentOrganizatons);
  696. return organizations;
  697. }
  698. protected boolean isParentOrganization(
  699. long parentOrganizationId, long organizationId)
  700. throws PortalException, SystemException {
  701. // Return true if parentOrganizationId is among the parent organizatons
  702. // of organizationId
  703. Organization parentOrganization =
  704. organizationPersistence.findByPrimaryKey(
  705. parentOrganizationId);
  706. List<Organization> parentOrganizations = getParentOrganizations(
  707. organizationId);
  708. if (parentOrganizations.contains(parentOrganization)) {
  709. return true;
  710. }
  711. else {
  712. return false;
  713. }
  714. }
  715. protected void validate(
  716. long companyId, long organizationId, long parentOrganizationId,
  717. String name, String type, long countryId, int statusId)
  718. throws PortalException, SystemException {
  719. if (!ArrayUtil.contains(PropsValues.ORGANIZATIONS_TYPES, type)) {
  720. throw new OrganizationTypeException(
  721. "Invalid organization type " + type);
  722. }
  723. if ((parentOrganizationId ==
  724. OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
  725. if (!OrganizationImpl.isRootable(type)) {
  726. throw new OrganizationParentException(
  727. "Organization of type " + type + " cannot be a root");
  728. }
  729. }
  730. else {
  731. Organization parentOrganization =
  732. organizationPersistence.fetchByPrimaryKey(
  733. parentOrganizationId);
  734. if (parentOrganization == null) {
  735. throw new OrganizationParentException(
  736. "Organization " + parentOrganizationId + " doesn't exist");
  737. }
  738. String[] childrenTypes = OrganizationImpl.getChildrenTypes(
  739. parentOrganization.getType());
  740. if (childrenTypes.length == 0) {
  741. throw new OrganizationParentException(
  742. "Organization of type " + type + " cannot have children");
  743. }
  744. if ((companyId != parentOrganization.getCompanyId()) ||
  745. (parentOrganizationId == organizationId)) {
  746. throw new OrganizationParentException();
  747. }
  748. if (!ArrayUtil.contains(childrenTypes, type)) {
  749. throw new OrganizationParentException(
  750. "Type " + type + " not allowed as child of " +
  751. parentOrganization.getType());
  752. }
  753. }
  754. if ((organizationId > 0) &&
  755. (parentOrganizationId !=
  756. OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
  757. // Prevent circular organizational references
  758. if (isParentOrganization(organizationId, parentOrganizationId)) {
  759. throw new OrganizationParentException();
  760. }
  761. }
  762. if (Validator.isNull(name)) {
  763. throw new OrganizationNameException();
  764. }
  765. else {
  766. Organization organization = organizationPersistence.fetchByC_N(
  767. companyId, name);
  768. if ((organization != null) &&
  769. (organization.getName().equalsIgnoreCase(name))) {
  770. if ((organizationId <= 0) ||
  771. (organization.getOrganizationId() != organizationId)) {
  772. throw new DuplicateOrganizationException();
  773. }
  774. }
  775. }
  776. boolean countryRequired = GetterUtil.getBoolean(
  777. PropsUtil.get(
  778. PropsKeys.ORGANIZATIONS_COUNTRY_REQUIRED, new Filter(type)));
  779. if (countryRequired || (countryId > 0)) {
  780. countryPersistence.findByPrimaryKey(countryId);
  781. }
  782. listTypeService.validate(
  783. statusId, ListTypeConstants.ORGANIZATION_STATUS);
  784. }
  785. protected void validate(
  786. long companyId, long parentOrganizationId, String name, String type,
  787. long countryId, int statusId)
  788. throws PortalException, SystemException {
  789. validate(
  790. companyId, 0, parentOrganizationId, name, type, countryId,
  791. statusId);
  792. }
  793. }