<%
/**
 * 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
	searchContainer="<%= new WorkflowTaskSearch(renderRequest, portletURL) %>"
>
	<%@ include file="/html/portlet/workflow_tasks/workflow_search_results.jspf" %>

	<liferay-ui:search-container-results
		results="<%= resultsTasks %>"
		total="<%= totalTasks %>"
	/>

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

		<%
		WorkflowInstance workflowInstance = WorkflowInstanceManagerUtil.getWorkflowInstance(company.getCompanyId(), workflowTask.getWorkflowInstanceId());

		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_tasks/edit_workflow_task" />
			<portlet:param name="redirect" value="<%= currentURL %>" />
			<portlet:param name="workflowTaskId" value="<%= String.valueOf(workflowTask.getWorkflowTaskId()) %>" />
		</portlet:renderURL>

		<c:if test="<%= allTasks %>">
			<liferay-ui:search-container-column-text
				buffer="buffer"
			>

				<%
				if (workflowTask.isAssignedToSingleUser() && (workflowTask.getAssigneeUserId() > 0)) {
					User assigneeUser = UserLocalServiceUtil.getUser(workflowTask.getAssigneeUserId());

					buffer.append("<img alt=\"");
					buffer.append(HtmlUtil.escape(assigneeUser.getFullName()));
					buffer.append("\" class=\"user-avatar\" src=\"");
					buffer.append(themeDisplay.getPathImage());
					buffer.append("/user_");

					if (assigneeUser.isFemale()) {
						buffer.append("female");
					}
					else {
						buffer.append("male");
					}

					buffer.append("_portrait?img_id=");
					buffer.append(assigneeUser.getPortraitId());
					buffer.append("&t=");
					buffer.append(ImageServletTokenUtil.getToken(assigneeUser.getPortraitId()));
					buffer.append("\" title=\"");
					buffer.append(HtmlUtil.escape(assigneeUser.getFullName()));
					buffer.append("\" />");
				}
				%>

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

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

			<%
			buffer.append("<span class=\"task-name\" id=\"");
			buffer.append(workflowTask.getWorkflowTaskId());
			buffer.append("\">");
			buffer.append(LanguageUtil.get(pageContext, workflowTask.getName()));
			buffer.append("</span>");
			%>

		</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"
			name="last-activity-date"
			href="<%= rowURL %>"
		>

			<%
			List<WorkflowLog> workflowLogs = WorkflowLogManagerUtil.getWorkflowLogsByWorkflowInstance(company.getCompanyId(), workflowTask.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="due-date"
			href="<%= rowURL %>"
			orderable="<%= true %>"
		>

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

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

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

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