Liferay get Journal Article based on Journal Structure name

Recently, our project uses Liferay web content product a lot. It's very useful. The good thing is content author can go there and change without help with developer. And we can set template for those content and directly display on the page.

At the same time, our web contents are associated with specific vocabulary, category. We can query journal article based on category name, but if content author associates other articles with different structure and template, then the code will break. So we need to add a constraint that we also query based on structure name.

There are two ways to do that:

1. Directly query article based on structure name. This method has the limitation it hard to associated those articles with category.

public static void getJournalArticle(RenderRequest renderRequest, long groupId, long orgGroupId) throws SystemException, PortalException{
	ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
	List<JournalStructure> journalStructures = JournalStructureLocalServiceUtil.getStructures(groupId);
	for(JournalStructure journalStructure :  journalStructures){
		if(journalStructure.getNameCurrentValue().equalsIgnoreCase("YouBarStructure")){
			List<JournalArticle> journalArticles = JournalArticleLocalServiceUtil.getStructureArticles(orgGroupId, journalStructure.getStructureId());
			for(JournalArticle journalArticle : journalArticles){
				boolean isLastestOne = JournalArticleLocalServiceUtil.isLatestVersion(orgGroupId, journalArticle.getArticleId(), journalArticle.getVersion());
				if(isLastestOne){
					String content = JournalArticleLocalServiceUtil.getArticleContent(journalArticle, "", "", themeDisplay.getLanguageId(), themeDisplay);
						
					List<AssetCategory> assetCategories = AssetCategoryLocalServiceUtil.getCategories(JournalArticle.class.getName(), journalArticle.getResourcePrimKey());
					for(AssetCategory assetCategory : assetCategories){
					AssetVocabulary assetVocabulary = AssetVocabularyServiceUtil.getVocabulary(assetCategory.getVocabularyId());
					    _logger.info(assetVocabulary.getGroupId());
							
					}
				}
			}
			break;
		}
	}

}


2. The second way is to use AssetEntryQuery.

1) get structure id based on structure name

public static long getStructureIdByStructureName(long groupId, String structureName)
			throws SystemException {
		List<JournalStructure> journalStructures = JournalStructureLocalServiceUtil
				.getStructures(groupId);
		for (JournalStructure journalStructure : journalStructures) {
			if (journalStructure.getNameCurrentValue().equalsIgnoreCase(structureName)) {
				return journalStructure.getId();
			}
		}
		return -1;
	}

2) Set AssetEntryQuery. This is the way used by AssetPublisher.

public static AssetEntryQuery getAssetsByCategoryStructure(long[] assetCategoryIds,
			long groupId, String structureName) throws SystemException {
		long journalArticleClassId = ClassNameServiceUtil.getClassNameId(JournalArticle.class);
		AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
		assetEntryQuery.setAnyCategoryIds(assetCategoryIds);
		long structureId = getStructureIdByStructureName(groupId, structureName);
		assetEntryQuery.setClassTypeIds(new long[] { structureId });
		assetEntryQuery.setClassNameIds(new long[] { journalArticleClassId });
		return assetEntryQuery;
	}
3) Get Journal Article list

public static List<JournalArticle> getJournalArticleContentByCategoryStructure(
			long[] assetCategoryIds, long groupId, String structureName) throws Exception {
		List<JournalArticle> journalArticles = new ArrayList<JournalArticle>();
		List<AssetEntry> assetEntries = AssetEntryLocalServiceUtil
				.getEntries(getAssetsByCategoryStructure(assetCategoryIds, groupId, structureName));
		for (AssetEntry assetEntry : assetEntries) {
			journalArticles.add(JournalArticleLocalServiceUtil.getLatestArticle(assetEntry
					.getClassPK()));
		}
		return journalArticles;
	}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值