/*
 * 
 */
package com.fp.bpmlib.query.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kie.api.task.model.Task;

import com.fp.base.persistence.util.helper.GeneralDescriptions;
import com.fp.bpmlib.ProcessUtil;
import com.fp.bpmlib.task.client.HumanTaskClient;
import com.fp.bpmlib.task.client.TaskUtil;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;

// TODO: Auto-generated Javadoc
/**
 * Class TransactionTasks encargada de consultar las Tareas por transacción.
 * 
 * @author gfiallos
 */
public class AsignedTasks extends QueryRule {

    /** serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /**
     * Process.
     * 
     * @param pQueryRequest the query request
     * @return query request
     * @throws Exception la exception
     */
    @Override
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        HumanTaskClient htc = new HumanTaskClient();
        try {
            List<Object[]> t = htc.getAsignedTasks(pQueryRequest.getString("userCri"));
            List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
            Task task;
            TaskUtil tu;
            Map<String, Object> rec;
            List<Map<String, Object>> data1;
            
            for (Object[] obj : t) {
                task = (Task) obj[0];
                tu = new TaskUtil(task, htc);
                rec = new HashMap<String, Object>();
                rec.put("tid", tu.getTid());
                rec.put("workItem", task.getTaskData().getWorkItemId());
                rec.put("detail", tu.getSubject());
                rec.put("userasigned", task.getTaskData().getActualOwner().getId());
                rec.put("nameasigned", GeneralDescriptions.getOfficername(task.getTaskData().getActualOwner().getId()));
                rec.put("logsolicitude", tu.getRequest().get("logsolicitude"));
                rec.put("rulename", task.getName()); 
                data1 = ProcessUtil.getInstance().transactionData(tu.getRequest().getJournalId(), pQueryRequest, false);
                for (Map<String, Object> map : data1) {
                    rec.put((String) map.get("NAME"), map.get("VALUE"));
                }
                data.add(rec);
            }
            pQueryRequest.getResponse().put("tasks", data);
            return pQueryRequest;
        } finally {
            htc.close();
        }
    }
}