<%
/**
 * 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.
 */
%>

<%
Map<String, PortletURL> addPortletURLs = getAddPortletURLs(renderRequest, renderResponse, allAssetTagNames, classNameIds);
%>

<c:if test="<%= !addPortletURLs.isEmpty() %>">
	<div class="lfr-meta-actions edit-controls">
		<liferay-ui:icon-menu
			align="left"
			icon='<%= themeDisplay.getPathThemeImages() + "/common/add.png" %>'
			message="add-new"
			showWhenSingleIcon="<%= true %>"
		>

			<%
			for (Map.Entry<String, PortletURL> entry : addPortletURLs.entrySet()) {
				String className = entry.getKey();
				PortletURL addPortletURL = entry.getValue();

				AssetRendererFactory assetRendererFactory = AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(className);
			%>

				<liferay-ui:icon
					message='<%= "model.resource." + className %>'
					src="<%= assetRendererFactory.getIconPath(renderRequest) %>"
					url='<%= addPortletURL.toString() %>'
				/>

			<%
			}
			%>

		</liferay-ui:icon-menu>
	</div>
</c:if>

<%!
public PortletURL getAddPortletURL(RenderRequest renderRequest, PortletResponse portletResponse, String[] allAssetTagNames, String className) throws Exception {
	ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);

	AssetRendererFactory assetRendererFactory = AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(className);

	PortletURL addPortletURL = null;

	if (assetRendererFactory != null) {
		addPortletURL = assetRendererFactory.getURLAdd((LiferayPortletRequest)renderRequest, (LiferayPortletResponse)portletResponse);

		if (addPortletURL != null) {
			addPortletURL.setWindowState(WindowState.MAXIMIZED);
			addPortletURL.setPortletMode(PortletMode.VIEW);

			addPortletURL.setParameter("redirect", themeDisplay.getURLCurrent());
			addPortletURL.setParameter("backURL", themeDisplay.getURLCurrent());

			String referringPortletResource = ParamUtil.getString(renderRequest, "portletResource");

			if (Validator.isNotNull(referringPortletResource)) {
				addPortletURL.setParameter("referringPortletResource", referringPortletResource);
			}
			else {
				PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();

				addPortletURL.setParameter("referringPortletResource", portletDisplay.getId());

				addPortletURL.setParameter("assetTagNames", StringUtil.merge(allAssetTagNames));
			}
		}
	}

	return addPortletURL;
}

public Map<String, PortletURL> getAddPortletURLs(RenderRequest renderRequest, PortletResponse portletResponse, String[] allAssetTagNames, long[] classNameIds) throws Exception {
	ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);

	Map<String, PortletURL> addPortletURLs = new TreeMap<String, PortletURL>(new ModelResourceComparator(themeDisplay.getLocale()));

	for (long classNameId : classNameIds) {
		ClassName className = ClassNameServiceUtil.getClassName(classNameId);

		PortletURL addPortletURL = getAddPortletURL(renderRequest, portletResponse, allAssetTagNames, className.getValue());

		if (addPortletURL != null) {
			addPortletURLs.put(className.getValue(), addPortletURL);
		}
	}

	return addPortletURLs;
}
%>