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

<%
long groupId = themeDisplay.getScopeGroupId();

if (portletName.equals(PortletKeys.WORKFLOW_DEFINITIONS)) {
	groupId = WorkflowConstants.DEFAULT_GROUP_ID;
}

List<WorkflowDefinition> workflowDefinitions = WorkflowDefinitionManagerUtil.getActiveWorkflowDefinitions(company.getCompanyId(), 0, 100, null);
List<WorkflowHandler> workflowHandlers = WorkflowHandlerRegistryUtil.getWorkflowHandlers();

PortletURL portletURL = renderResponse.createRenderURL();

portletURL.setParameter("struts_action", "/workflow_definition_links/view");
%>

<portlet:actionURL var="editWorkflowDefinitionLinkURL">
	<portlet:param name="struts_action" value="/workflow_definition_links/edit_workflow_definition_link" />
</portlet:actionURL>

<aui:form action="<%= editWorkflowDefinitionLinkURL %>" method="post">
	<aui:input name="redirect" type="hidden" value="<%= currentURL %>" />
	<aui:input name="groupId" type="hidden" value="<%= groupId %>" />

	<liferay-ui:search-container
		iteratorURL="<%= portletURL %>"
	>
		<liferay-ui:search-container-results
			results="<%= ListUtil.subList(workflowHandlers, searchContainer.getStart(), searchContainer.getEnd()) %>"
			total="<%= workflowHandlers.size() %>"
		/>

		<liferay-ui:search-container-row
			className="com.liferay.portal.kernel.workflow.WorkflowHandler"
			modelVar="workflowHandler"
		>

			<liferay-ui:search-container-row-parameter
				name="workflowHandler"
				value="<%= workflowHandler %>"
			/>

			<liferay-ui:search-container-column-text
				buffer="buffer"
				name="resource"
			>

				<%
				buffer.append("<img align=\"left\" border=\"0\" src=\"");
				buffer.append(workflowHandler.getIconPath((LiferayPortletRequest)renderRequest));
				buffer.append("\" style=\"margin-right: 5px;\">");
				buffer.append(LanguageUtil.get(pageContext, "model.resource." + workflowHandler.getClassName()));
				%>

			</liferay-ui:search-container-column-text>

			<liferay-ui:search-container-column-text
				name="workflow"
			>
				<aui:select label="" name='<%= "workflowDefinitionName@" + workflowHandler.getClassName() %>'>

					<%
					WorkflowDefinitionLink workflowDefinitionLink = null;

					try {
						if (portletName.equals(PortletKeys.WORKFLOW_DEFINITIONS)) {
							workflowDefinitionLink = WorkflowDefinitionLinkLocalServiceUtil.getDefaultWorkflowDefinitionLink(company.getCompanyId(), workflowHandler.getClassName());
						}
						else {
							workflowDefinitionLink = WorkflowDefinitionLinkLocalServiceUtil.getWorkflowDefinitionLink(company.getCompanyId(), themeDisplay.getScopeGroupId(), workflowHandler.getClassName(), true);
						}
					}
					catch (NoSuchWorkflowDefinitionLinkException nswdle) {
					}

					String defaultOptionMessage = null;

					if (!portletName.equals(PortletKeys.WORKFLOW_DEFINITIONS)) {
						try {
							WorkflowDefinitionLink defaultWorkflowDefinitionLink = WorkflowDefinitionLinkLocalServiceUtil.getDefaultWorkflowDefinitionLink(company.getCompanyId(), workflowHandler.getClassName());

							defaultOptionMessage = LanguageUtil.get(pageContext, "default") + StringPool.COLON + StringPool.SPACE + defaultWorkflowDefinitionLink.getWorkflowDefinitionName();
						}
						catch (NoSuchWorkflowDefinitionLinkException nswdle) {
							defaultOptionMessage = LanguageUtil.get(pageContext, "default") + StringPool.COLON + StringPool.SPACE  + LanguageUtil.get(pageContext, "no-workflow");
						}
					}
					else {
						defaultOptionMessage = LanguageUtil.get(pageContext, "no-workflow");
					}
					%>

					<aui:option><%= defaultOptionMessage %></aui:option>

					<%
					for (WorkflowDefinition workflowDefinition : workflowDefinitions) {
						boolean selected = false;

						if ((workflowDefinitionLink != null) && (workflowDefinitionLink.getWorkflowDefinitionName().equals(workflowDefinition.getName())) && (workflowDefinitionLink.getWorkflowDefinitionVersion() == workflowDefinition.getVersion())) {
							selected = true;
						}
					%>

						<aui:option label='<%= workflowDefinition.getName() + " (" + LanguageUtil.format(locale, "version-x", workflowDefinition.getVersion()) + ")" %>' selected="<%= selected %>" value="<%= workflowDefinition.getName() + StringPool.AT + workflowDefinition.getVersion() %>" />

					<%
					}
					%>

				</aui:select>
			</liferay-ui:search-container-column-text>
		</liferay-ui:search-container-row>

		<liferay-ui:search-iterator />
	</liferay-ui:search-container>

	<aui:button-row>
		<aui:button type="submit" />
	</aui:button-row>
</aui:form>