/portal-impl/src/com/liferay/portlet/asset/service/impl/AssetVocabularyLocalServiceImpl.java

https://github.com/viktorkovacs/liferay-portal-trunk · Java · 310 lines · 207 code · 74 blank · 29 comment · 13 complexity · 6b04655b13cdd52334d14ae3f9ef8221 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.portlet.asset.service.impl;
  15. import com.liferay.portal.kernel.exception.PortalException;
  16. import com.liferay.portal.kernel.exception.SystemException;
  17. import com.liferay.portal.kernel.util.ListUtil;
  18. import com.liferay.portal.kernel.util.LocaleUtil;
  19. import com.liferay.portal.kernel.util.StringPool;
  20. import com.liferay.portal.kernel.util.Validator;
  21. import com.liferay.portal.model.Group;
  22. import com.liferay.portal.model.ResourceConstants;
  23. import com.liferay.portal.model.User;
  24. import com.liferay.portal.service.ServiceContext;
  25. import com.liferay.portal.util.PropsValues;
  26. import com.liferay.portlet.asset.DuplicateVocabularyException;
  27. import com.liferay.portlet.asset.VocabularyNameException;
  28. import com.liferay.portlet.asset.model.AssetVocabulary;
  29. import com.liferay.portlet.asset.service.base.AssetVocabularyLocalServiceBaseImpl;
  30. import com.liferay.portlet.asset.util.AssetUtil;
  31. import java.util.ArrayList;
  32. import java.util.Date;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Locale;
  36. import java.util.Map;
  37. /**
  38. * @author Alvaro del Castillo
  39. * @author Eduardo Lundgren
  40. * @author Jorge Ferrer
  41. */
  42. public class AssetVocabularyLocalServiceImpl
  43. extends AssetVocabularyLocalServiceBaseImpl {
  44. /**
  45. * @deprecated
  46. */
  47. public AssetVocabulary addVocabulary(
  48. long userId, Map<Locale, String> titleMap,
  49. Map<Locale, String> descriptionMap, String settings,
  50. ServiceContext serviceContext)
  51. throws PortalException, SystemException {
  52. return addVocabulary(
  53. userId, StringPool.BLANK, titleMap, descriptionMap, settings,
  54. serviceContext);
  55. }
  56. public AssetVocabulary addVocabulary(
  57. long userId, String title, Map<Locale, String> titleMap,
  58. Map<Locale, String> descriptionMap, String settings,
  59. ServiceContext serviceContext)
  60. throws PortalException, SystemException {
  61. // Vocabulary
  62. User user = userPersistence.findByPrimaryKey(userId);
  63. long groupId = serviceContext.getScopeGroupId();
  64. String name = titleMap.get(LocaleUtil.getDefault());
  65. Date now = new Date();
  66. validate(groupId, name);
  67. long vocabularyId = counterLocalService.increment();
  68. AssetVocabulary vocabulary = assetVocabularyPersistence.create(
  69. vocabularyId);
  70. vocabulary.setUuid(serviceContext.getUuid());
  71. vocabulary.setGroupId(groupId);
  72. vocabulary.setCompanyId(user.getCompanyId());
  73. vocabulary.setUserId(user.getUserId());
  74. vocabulary.setUserName(user.getFullName());
  75. vocabulary.setCreateDate(now);
  76. vocabulary.setModifiedDate(now);
  77. vocabulary.setName(name);
  78. if (Validator.isNotNull(title)) {
  79. vocabulary.setTitle(title);
  80. }
  81. else {
  82. vocabulary.setTitleMap(titleMap);
  83. }
  84. vocabulary.setDescriptionMap(descriptionMap);
  85. vocabulary.setSettings(settings);
  86. assetVocabularyPersistence.update(vocabulary, false);
  87. // Resources
  88. if (serviceContext.getAddCommunityPermissions() ||
  89. serviceContext.getAddGuestPermissions()) {
  90. addVocabularyResources(
  91. vocabulary, serviceContext.getAddCommunityPermissions(),
  92. serviceContext.getAddGuestPermissions());
  93. }
  94. else {
  95. addVocabularyResources(
  96. vocabulary, serviceContext.getCommunityPermissions(),
  97. serviceContext.getGuestPermissions());
  98. }
  99. return vocabulary;
  100. }
  101. public void addVocabularyResources(
  102. AssetVocabulary vocabulary, boolean addCommunityPermissions,
  103. boolean addGuestPermissions)
  104. throws PortalException, SystemException {
  105. resourceLocalService.addResources(
  106. vocabulary.getCompanyId(), vocabulary.getGroupId(),
  107. vocabulary.getUserId(), AssetVocabulary.class.getName(),
  108. vocabulary.getVocabularyId(), false, addCommunityPermissions,
  109. addGuestPermissions);
  110. }
  111. public void addVocabularyResources(
  112. AssetVocabulary vocabulary, String[] communityPermissions,
  113. String[] guestPermissions)
  114. throws PortalException, SystemException {
  115. resourceLocalService.addModelResources(
  116. vocabulary.getCompanyId(), vocabulary.getGroupId(),
  117. vocabulary.getUserId(), AssetVocabulary.class.getName(),
  118. vocabulary.getVocabularyId(), communityPermissions,
  119. guestPermissions);
  120. }
  121. public void deleteVocabulary(AssetVocabulary vocabulary)
  122. throws PortalException, SystemException {
  123. // Vocabulary
  124. assetVocabularyPersistence.remove(vocabulary);
  125. // Resources
  126. resourceLocalService.deleteResource(
  127. vocabulary.getCompanyId(), AssetVocabulary.class.getName(),
  128. ResourceConstants.SCOPE_INDIVIDUAL, vocabulary.getVocabularyId());
  129. // Categories
  130. assetCategoryLocalService.deleteVocabularyCategories(
  131. vocabulary.getVocabularyId());
  132. }
  133. public void deleteVocabulary(long vocabularyId)
  134. throws PortalException, SystemException {
  135. AssetVocabulary vocabulary =
  136. assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
  137. deleteVocabulary(vocabulary);
  138. }
  139. public List<AssetVocabulary> getCompanyVocabularies(long companyId)
  140. throws SystemException {
  141. return assetVocabularyPersistence.findByCompanyId(companyId);
  142. }
  143. public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
  144. throws PortalException, SystemException {
  145. List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>();
  146. for (long groupId : groupIds) {
  147. vocabularies.addAll(getGroupVocabularies(groupId));
  148. }
  149. return vocabularies;
  150. }
  151. public List<AssetVocabulary> getGroupVocabularies(long groupId)
  152. throws PortalException, SystemException {
  153. List<AssetVocabulary> vocabularies =
  154. assetVocabularyPersistence.findByGroupId(groupId);
  155. if (vocabularies.isEmpty()) {
  156. Group group = groupLocalService.getGroup(groupId);
  157. long defaultUserId = userLocalService.getDefaultUserId(
  158. group.getCompanyId());
  159. ServiceContext serviceContext = new ServiceContext();
  160. serviceContext.setScopeGroupId(groupId);
  161. Map<Locale, String> titleMap = new HashMap<Locale, String>();
  162. titleMap.put(
  163. LocaleUtil.getDefault(), PropsValues.ASSET_VOCABULARY_DEFAULT);
  164. AssetVocabulary vocabulary =
  165. assetVocabularyLocalService.addVocabulary(
  166. defaultUserId, StringPool.BLANK, titleMap, null,
  167. StringPool.BLANK, serviceContext);
  168. vocabularies = ListUtil.copy(vocabularies);
  169. vocabularies.add(vocabulary);
  170. }
  171. return vocabularies;
  172. }
  173. public AssetVocabulary getGroupVocabulary(long groupId, String name)
  174. throws PortalException, SystemException {
  175. return assetVocabularyPersistence.findByG_N(groupId, name);
  176. }
  177. public AssetVocabulary getVocabulary(long vocabularyId)
  178. throws PortalException, SystemException {
  179. return assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
  180. }
  181. /**
  182. * @deprecated
  183. */
  184. public AssetVocabulary updateVocabulary(
  185. long vocabularyId, Map<Locale, String> titleMap,
  186. Map<Locale, String> descriptionMap, String settings,
  187. ServiceContext serviceContext)
  188. throws PortalException, SystemException {
  189. return updateVocabulary(
  190. vocabularyId, StringPool.BLANK, titleMap, descriptionMap, settings,
  191. serviceContext);
  192. }
  193. public AssetVocabulary updateVocabulary(
  194. long vocabularyId, String title, Map<Locale, String> titleMap,
  195. Map<Locale, String> descriptionMap, String settings,
  196. ServiceContext serviceContext)
  197. throws PortalException, SystemException {
  198. long groupId = serviceContext.getScopeGroupId();
  199. String name = titleMap.get(LocaleUtil.getDefault());
  200. AssetVocabulary vocabulary =
  201. assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
  202. if (!vocabulary.getName().equals(name)) {
  203. validate(groupId, name);
  204. }
  205. vocabulary.setModifiedDate(new Date());
  206. vocabulary.setName(name);
  207. vocabulary.setTitleMap(titleMap);
  208. if (Validator.isNotNull(title)) {
  209. vocabulary.setTitle(title);
  210. }
  211. vocabulary.setDescriptionMap(descriptionMap);
  212. vocabulary.setSettings(settings);
  213. assetVocabularyPersistence.update(vocabulary, false);
  214. return vocabulary;
  215. }
  216. protected boolean hasVocabulary(long groupId, String name)
  217. throws SystemException {
  218. if (assetVocabularyPersistence.countByG_N(groupId, name) == 0) {
  219. return false;
  220. }
  221. else {
  222. return true;
  223. }
  224. }
  225. protected void validate(long groupId, String name)
  226. throws PortalException, SystemException {
  227. if (!AssetUtil.isValidWord(name)) {
  228. throw new VocabularyNameException();
  229. }
  230. if (hasVocabulary(groupId, name)) {
  231. throw new DuplicateVocabularyException(
  232. "A category vocabulary with the name " + name +
  233. " already exists");
  234. }
  235. }
  236. }