@Indexable(type = IndexableType.DELETE) @Override @SystemEvent(type = SystemEventConstants.TYPE_DELETE) public AssetCategory deleteCategory(AssetCategory category, boolean skipRebuildTree) throws PortalException { // Categories List<AssetCategory> categories = assetCategoryPersistence.findByParentCategoryId(category.getCategoryId()); for (AssetCategory curCategory : categories) { assetCategoryLocalService.deleteCategory(curCategory, true); } if (!categories.isEmpty() && !skipRebuildTree) { final long groupId = category.getGroupId(); TransactionCommitCallbackUtil.registerCallback( new Callable<Void>() { @Override public Void call() throws Exception { assetCategoryLocalService.rebuildTree(groupId, true); return null; } }); } // Entries List<AssetEntry> entries = assetCategoryPersistence.getAssetEntries(category.getCategoryId()); // Category assetCategoryPersistence.remove(category); // Resources resourceLocalService.deleteResource( category.getCompanyId(), AssetCategory.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId()); // Properties assetCategoryPropertyLocalService.deleteCategoryProperties(category.getCategoryId()); // Indexer assetEntryLocalService.reindex(entries); return category; }
protected void validate(long categoryId, long parentCategoryId, String name, long vocabularyId) throws PortalException { if (Validator.isNull(name)) { StringBundler sb = new StringBundler(5); sb.append("Asset category name cannot be null for key {categoryId="); sb.append(categoryId); sb.append(", vocabularyId="); sb.append(vocabularyId); sb.append("}"); throw new AssetCategoryNameException( "Category name cannot be null for category " + categoryId + " and vocabulary " + vocabularyId); } AssetCategory category = assetCategoryPersistence.fetchByP_N_V(parentCategoryId, name, vocabularyId); if ((category != null) && (category.getCategoryId() != categoryId)) { StringBundler sb = new StringBundler(4); sb.append("There is another category named "); sb.append(name); sb.append(" as a child of category "); sb.append(parentCategoryId); throw new DuplicateCategoryException(sb.toString()); } }
@Override public void deleteCategories(List<AssetCategory> categories) throws PortalException { List<Long> rebuildTreeGroupIds = new ArrayList<>(); for (AssetCategory category : categories) { if (!rebuildTreeGroupIds.contains(category.getGroupId()) && (getChildCategoriesCount(category.getCategoryId()) > 0)) { final long groupId = category.getGroupId(); TransactionCommitCallbackUtil.registerCallback( new Callable<Void>() { @Override public Void call() throws Exception { assetCategoryLocalService.rebuildTree(groupId, true); return null; } }); rebuildTreeGroupIds.add(groupId); } assetCategoryLocalService.deleteCategory(category, true); } }
@Override public void addCategoryResources(AssetCategory category, ModelPermissions modelPermissions) throws PortalException { resourceLocalService.addModelResources( category.getCompanyId(), category.getGroupId(), category.getUserId(), AssetCategory.class.getName(), category.getCategoryId(), modelPermissions); }
protected void updateArticleType() throws Exception { if (!hasSelectedArticleTypes()) { return; } List<String> types = getArticleTypes(); if (types.size() <= 0) { return; } Locale localeThreadLocalDefaultLocale = LocaleThreadLocal.getDefaultLocale(); try { List<Company> companies = _companyLocalService.getCompanies(); for (Company company : companies) { LocaleThreadLocal.setDefaultLocale(company.getLocale()); Set<Locale> locales = LanguageUtil.getAvailableLocales(company.getGroupId()); Locale defaultLocale = LocaleUtil.fromLanguageId( UpgradeProcessUtil.getDefaultLanguageId(company.getCompanyId())); Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(locales, defaultLocale, "type"); AssetVocabulary assetVocabulary = addAssetVocabulary( company.getGroupId(), company.getCompanyId(), "type", nameMap, new HashMap<Locale, String>()); Map<String, Long> journalArticleTypesToAssetCategoryIds = new HashMap<>(); for (String type : types) { AssetCategory assetCategory = addAssetCategory( company.getGroupId(), company.getCompanyId(), type, assetVocabulary.getVocabularyId()); journalArticleTypesToAssetCategoryIds.put(type, assetCategory.getCategoryId()); } updateArticles(company.getCompanyId(), journalArticleTypesToAssetCategoryIds); } } finally { LocaleThreadLocal.setDefaultLocale(localeThreadLocalDefaultLocale); } }
protected void updateChildrenVocabularyId(AssetCategory category, long vocabularyId) { List<AssetCategory> childrenCategories = assetCategoryPersistence.findByParentCategoryId(category.getCategoryId()); if (!childrenCategories.isEmpty()) { for (AssetCategory childCategory : childrenCategories) { childCategory.setVocabularyId(vocabularyId); assetCategoryPersistence.update(childCategory); updateChildrenVocabularyId(childCategory, vocabularyId); } } }
@Override public void addCategoryResources( AssetCategory category, boolean addGroupPermissions, boolean addGuestPermissions) throws PortalException { resourceLocalService.addResources( category.getCompanyId(), category.getGroupId(), category.getUserId(), AssetCategory.class.getName(), category.getCategoryId(), false, addGroupPermissions, addGuestPermissions); }
@Override protected StagedModel addStagedModel( Group group, Map<String, List<StagedModel>> dependentStagedModelsMap) throws Exception { List<StagedModel> vocabularyDependentStagedModels = dependentStagedModelsMap.get(AssetVocabulary.class.getSimpleName()); AssetVocabulary vocabulary = (AssetVocabulary) vocabularyDependentStagedModels.get(0); List<StagedModel> categoryDependentStagedModels = dependentStagedModelsMap.get(AssetCategory.class.getSimpleName()); AssetCategory category = (AssetCategory) categoryDependentStagedModels.get(0); return AssetTestUtil.addCategory( group.getGroupId(), vocabulary.getVocabularyId(), category.getCategoryId()); }
@Indexable(type = IndexableType.REINDEX) @Override public AssetCategory updateCategory( long userId, long categoryId, long parentCategoryId, Map<Locale, String> titleMap, Map<Locale, String> descriptionMap, long vocabularyId, String[] categoryProperties, ServiceContext serviceContext) throws PortalException { // Category String name = titleMap.get(LocaleUtil.getSiteDefault()); name = ModelHintsUtil.trimString(AssetCategory.class.getName(), "name", name); if (categoryProperties == null) { categoryProperties = new String[0]; } validate(categoryId, parentCategoryId, name, vocabularyId); if (parentCategoryId > 0) { assetCategoryPersistence.findByPrimaryKey(parentCategoryId); } AssetCategory category = assetCategoryPersistence.findByPrimaryKey(categoryId); String oldName = category.getName(); if (vocabularyId != category.getVocabularyId()) { assetVocabularyPersistence.findByPrimaryKey(vocabularyId); parentCategoryId = AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID; category.setVocabularyId(vocabularyId); updateChildrenVocabularyId(category, vocabularyId); } category.setParentCategoryId(parentCategoryId); category.setName(name); category.setTitleMap(titleMap); category.setDescriptionMap(descriptionMap); assetCategoryPersistence.update(category); // Properties List<AssetCategoryProperty> oldCategoryProperties = assetCategoryPropertyPersistence.findByCategoryId(categoryId); oldCategoryProperties = ListUtil.copy(oldCategoryProperties); for (int i = 0; i < categoryProperties.length; i++) { String[] categoryProperty = StringUtil.split( categoryProperties[i], AssetCategoryConstants.PROPERTY_KEY_VALUE_SEPARATOR); if (categoryProperty.length <= 1) { categoryProperty = StringUtil.split(categoryProperties[i], CharPool.COLON); } String key = StringPool.BLANK; if (categoryProperty.length > 0) { key = GetterUtil.getString(categoryProperty[0]); } String value = StringPool.BLANK; if (categoryProperty.length > 1) { value = GetterUtil.getString(categoryProperty[1]); } if (Validator.isNotNull(key)) { boolean addCategoryProperty = true; AssetCategoryProperty oldCategoryProperty = null; Iterator<AssetCategoryProperty> iterator = oldCategoryProperties.iterator(); while (iterator.hasNext()) { oldCategoryProperty = iterator.next(); if ((categoryId == oldCategoryProperty.getCategoryId()) && key.equals(oldCategoryProperty.getKey())) { addCategoryProperty = false; if (!value.equals(oldCategoryProperty.getValue())) { assetCategoryPropertyLocalService.updateCategoryProperty( userId, oldCategoryProperty.getCategoryPropertyId(), key, value); } iterator.remove(); break; } } if (addCategoryProperty) { assetCategoryPropertyLocalService.addCategoryProperty(userId, categoryId, key, value); } } } for (AssetCategoryProperty categoryProperty : oldCategoryProperties) { assetCategoryPropertyLocalService.deleteAssetCategoryProperty(categoryProperty); } // Indexer if (!oldName.equals(name)) { List<AssetEntry> entries = assetCategoryPersistence.getAssetEntries(category.getCategoryId()); assetEntryLocalService.reindex(entries); } return category; }