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;
	}


你可能感兴趣的:(liferay,article,journal,journal,structure,AssetEntryQuery)