简体   繁体   中英

Add the category in the web content programmatically in Liferay 7.2

I want to add the category in a web content programmatically in Liferay 7. The code works fine!.

Below is showed de code:

long [] categoryId = new long[1];
List<String> listLaboratorioNombre = new ArrayList<String>();
boolean laboratorioNombre;

for(ProductosDTO index : listaProductos) {
       titleMap.put(themeDisplay.getLocale(), index.getLaboratorioNombre());
       descriptionMap.put(themeDisplay.getLocale(), index.getPromocionNombre());
                        
        //Se crean las categorias
        laboratorioNombre = listLaboratorioNombre.contains(index.getLaboratorioNombre());
                        
        if(!laboratorioNombre) {
           listLaboratorioNombre.add(index.getLaboratorioNombre());
                            
            AssetCategoryLocalServiceUtil.addCategory(
               userId, groupId, index.getLaboratorioNombre(), Long.parseLong(_configuration.vocabularyId()), serviceContext);
        }
                    
        //Se obtiene el id de la categoria
        listCategories = AssetVocabularyLocalServiceUtil.getGroupVocabulary(groupId, vocabularyName).getCategories();

        for(AssetCategory category : listCategories) {
           if(category.getName().equals(index.getLaboratorioNombre())) {
              categoryId[0] = category.getCategoryId();
              break;
            }
        }
                        
        //Se crean los contenidos web
        content = getContent(themeDisplay, index);
                        
        article = JournalArticleLocalServiceUtil.addArticle(
           userId, groupId, folderId, titleMap, descriptionMap, content, ddmStructureKey, ddmTemplateKey, serviceContext);
                        
        JournalArticleLocalServiceUtil.updateAsset(userId, article, new long[]{categoryId}, null, null, null);

}

I have tried adding the category with the following code:

JournalArticleLocalServiceUtil.updateAsset(userId, article, new long[]{categoryId}, null, null, null);

However I can't add it to the web content. Category field is empty. Category is not selected

How can I add the category to web content?

Regards!

It looks from your code snippet. you are adding category and article also programmatically.

Anyway, if you want to attach category to article or to any asset. you need assetentryId and assetcategoryId. So, when you create article using JournalArticleLocalserviceUtil . It also adds assetentry with reference of this article's primkey as classPK. which you can use to get assetentryid.

another point that you need to consider is DXP 7.2 introduced new table AssetEntryAssetCategoryRel by deprecating AssetEntries_AssetCategories table. If you still want to refer old table, Then use, AssetCategoryLocalServiceUtil class methods as per need.

If you have any short term plan upgrading to 7.3, then I would suggest you to start referring to new table API by AssetEntryAssetCategoryRelLocalServiceUtil class methods as old table and API will not be available from 7.3 version.

Note:- If you are writing this code in OSGI component based class then try to use service reference instead *serviceutil or *localserviceutil .

For Example,

@reference
private volatile AssetEntryAssetCategoryRelLocalService _assetEntryAssetCategoryRelLocalService; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM