<%
/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
%>

<%
assetEntryQuery.setGroupIds(groupIds);
assetEntryQuery.setEnd(searchContainer.getEnd());
assetEntryQuery.setExcludeZeroViewCount(excludeZeroViewCount);
assetEntryQuery.setOrderByCol1(orderByColumn1);
assetEntryQuery.setOrderByCol2(orderByColumn2);
assetEntryQuery.setOrderByType1(orderByType1);
assetEntryQuery.setOrderByType2(orderByType2);
assetEntryQuery.setStart(searchContainer.getStart());

int total = 0;

if (assetVocabularyId > 0) {
	AssetVocabulary assetVocabulary = AssetVocabularyLocalServiceUtil.getVocabulary(assetVocabularyId);

	List<AssetCategory> assetCategories = AssetCategoryLocalServiceUtil.getVocabularyRootCategories(assetVocabulary.getVocabularyId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);

	for (AssetCategory assetCategory : assetCategories) {
		long[] oldAllCategoryIds = assetEntryQuery.getAllCategoryIds();

		long[] newAllAssetCategoryIds = ArrayUtil.append(oldAllCategoryIds, assetCategory.getCategoryId());

		assetEntryQuery.setAllCategoryIds(newAllAssetCategoryIds);

		int groupTotal = AssetEntryServiceUtil.getEntriesCount(assetEntryQuery);

		total += groupTotal;

		searchContainer.setTotal(groupTotal);

		List results = AssetEntryServiceUtil.getEntries(assetEntryQuery);

		searchContainer.setResults(results);

		request.setAttribute("view.jsp-results", results);

		if (!results.isEmpty()) {
%>

			<h3 class="asset-entries-group-label"><%= assetCategory.getName() %></h3>

			<%@ include file="/html/portlet/asset_publisher/view_dynamic_list_asset.jspf" %>

<%
		}

		assetEntryQuery.setAllCategoryIds(oldAllCategoryIds);
	}
}
else if (!groupByClass) {
	if(!anyAssetType) {
		assetEntryQuery.setClassNameIds(classNameIds);
	}

	total = AssetEntryServiceUtil.getEntriesCount(assetEntryQuery);

	searchContainer.setTotal(total);

	List results = AssetEntryServiceUtil.getEntries(assetEntryQuery);

	searchContainer.setResults(results);

	request.setAttribute("view.jsp-results", results);
%>

	<%@ include file="/html/portlet/asset_publisher/view_dynamic_list_asset.jspf" %>

<%
}
else {
	for (long classNameId : classNameIds) {
		long[] groupClassNameIds = {classNameId};

		assetEntryQuery.setClassNameIds(groupClassNameIds);

		String groupClassName = PortalUtil.getClassName(classNameId);

		AssetRendererFactory groupAssetRendererFactory = AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(groupClassName);

		int groupTotal = AssetEntryServiceUtil.getEntriesCount(assetEntryQuery);

		total += groupTotal;

		searchContainer.setTotal(groupTotal);

		List results = AssetEntryServiceUtil.getEntries(assetEntryQuery);

		searchContainer.setResults(results);

		request.setAttribute("view.jsp-results", results);

		if (!results.isEmpty()) {
%>

			<h3 class="asset-entries-group-label"><liferay-ui:message key="<%= groupAssetRendererFactory.getType() %>" /></h3>

			<%@ include file="/html/portlet/asset_publisher/view_dynamic_list_asset.jspf" %>

<%
		}
	}
}

if (total == 0) {
	if (!showPortletWithNoResults && !((assetCategoryId > 0) || Validator.isNotNull(assetTagName))) {
		renderRequest.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.TRUE);
	}
%>

	<p>
		<liferay-ui:message key="there-are-no-results" />
	</p>

<%
}
%>