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

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.bpmlib.db.BPMProperties;
import com.fp.bpmlib.task.client.HumanTaskClient;
import com.fp.bpmlib.task.client.TaskUtil;
import com.fp.common.properties.PropertiesHandler;
import com.fp.dto.data.TransactionDTO;
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 TransactionTasksGroup 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 {
        PropertiesHandler ph = BPMProperties.getInstance();
        HumanTaskClient htc = new HumanTaskClient();
        try {
            List<Object[]> t = htc.getTasksByGroup(pQueryRequest.getString("groupId"), pQueryRequest.getString("cri"));
            List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
            TransactionDTO trnP = new TransactionDTO(pQueryRequest.getTransactionModule(), pQueryRequest.getTransactionCode(),
                    pQueryRequest.getTransactionVersion());
            for (Object[] obj : t) {
                Task task = (Task) obj[0];
                TaskUtil tu = new TaskUtil(task, htc);
                TransactionDTO trn1 = tu.getTransaction();
                if (trn1.compareTo(trnP) != 0) {
                    continue;
                }
                Map<String, Object> rec = new HashMap<String, Object>();
                rec.put("tid", tu.getTid());
                rec.put("logsolicitude",tu.getRequest().get("logsolicitude"));
                rec.put("i",
                        ph.getStringValue(BPMProperties.INBOX_ICON_PATH) + tu.getStatus() + ph.getStringValue(BPMProperties.INBOX_ICON_EXTENSION));
                rec.put("c", "w" + tu.getWorkItemId());
                rec.put("d", tu.getSubject());
                Map<String, Object> m = new HashMap<String, Object>();
                rec.put("desc", m);
                m.put("c", "t" + tu.getTid());
                m.put("d", null);
                data.add(rec);
            }
            pQueryRequest.getResponse().put("tasks", data);
            return pQueryRequest;
        } finally {
            htc.close();
        }
    }

}