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

<liferay-ui:search-container
	iteratorURL="<%= portletURL %>"
>
	<%@ include file="/html/portlet/workflow_instances/workflow_search_results.jspf" %>

	<liferay-ui:search-container-results
		results="<%= resultsInstances %>"
		total="<%= totalInstances %>"
	/>

	<liferay-ui:search-container-row
		className="com.liferay.portal.kernel.workflow.WorkflowInstance"
		modelVar="workflowInstance"
		stringKey="<%= true %>"
	>
		<liferay-ui:search-container-row-parameter
			name="workflowInstance"
			value="<%= workflowInstance %>"
		/>

		<%
		Map<String, Serializable> workflowContext = workflowInstance.getWorkflowContext();

		long companyId = GetterUtil.getLong((String)workflowContext.get(WorkflowConstants.CONTEXT_COMPANY_ID));
		long groupId = GetterUtil.getLong((String)workflowContext.get(WorkflowConstants.CONTEXT_GROUP_ID));
		String className = (String)workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME);
		long classPK = GetterUtil.getLong((String)workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));

		WorkflowHandler workflowHandler = WorkflowHandlerRegistryUtil.getWorkflowHandler(className);
		%>

		<portlet:renderURL var="rowURL">
			<portlet:param name="struts_action" value="/workflow_instance/edit_workflow_instance" />
			<portlet:param name="redirect" value="<%= currentURL %>" />
			<portlet:param name="workflowInstanceId" value="<%= String.valueOf(workflowInstance.getWorkflowInstanceId()) %>" />
		</portlet:renderURL>

		<liferay-ui:search-container-column-text
			buffer="buffer"
			href="<%= rowURL %>"
			name="definition"
		>

			<%
			buffer.append(LanguageUtil.get(pageContext, workflowInstance.getWorkflowDefinitionName()));
			%>

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

		<liferay-ui:search-container-column-text
			buffer="buffer"
			name="asset-title"
			href="<%= rowURL %>"
		>

			<%
			buffer.append(workflowHandler.getTitle(classPK));
			%>

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

		<liferay-ui:search-container-column-text
			buffer="buffer"
			name="asset-type"
			href="<%= rowURL %>"
		>

			<%
			buffer.append(workflowHandler.getType(locale));
			%>

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

		<liferay-ui:search-container-column-text
			buffer="buffer"
			href="<%= rowURL %>"
			name="status"
		>

			<%
			buffer.append(LanguageUtil.get(pageContext, workflowInstance.getState()));
			%>

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

		<liferay-ui:search-container-column-text
			buffer="buffer"
			name="last-activity-date"
			href="<%= rowURL %>"
		>

			<%
			List<WorkflowLog> workflowLogs = WorkflowLogManagerUtil.getWorkflowLogsByWorkflowInstance(company.getCompanyId(), workflowInstance.getWorkflowInstanceId(), null, 0, 1, WorkflowComparatorFactoryUtil.getLogCreateDateComparator());

			if (workflowLogs.isEmpty()) {
				buffer.append(LanguageUtil.get(pageContext, "never"));
			}
			else {
				WorkflowLog workflowLog = workflowLogs.get(0);

				buffer.append(dateFormatDateTime.format(workflowLog.getCreateDate()));
			}
			%>

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

		<liferay-ui:search-container-column-text
			buffer="buffer"
			name="end-date"
			href="<%= rowURL %>"
		>

			<%
			if (workflowInstance.getEndDate() == null) {
				buffer.append(LanguageUtil.get(pageContext, "never"));
			}
			else {
				buffer.append(dateFormatDateTime.format(workflowInstance.getEndDate()));
			}
			%>

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

		<c:if test="<%= !allInstances %>">
			<liferay-ui:search-container-column-jsp
				align="right"
				path="/html/portlet/workflow_instances/workflow_instance_action.jsp"
			/>
		</c:if>
	</liferay-ui:search-container-row>

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