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

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

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

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

// TODO: Auto-generated Javadoc
/**
 * Class TransactionList encargada de listar las transacciones para el buzón de la persona.
 * 
 * @author gfiallos
 */
public class TransactionListGroup extends QueryRule {

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

    /** El valor de htc. */
    private HumanTaskClient htc = null;

    /** El valor de trn. */
    private final Map<String, Integer> trn = new HashMap<String, Integer>();

    /** El valor de trn dto. */
    private final Map<String, TransactionDTO> trnDTO = new HashMap<String, TransactionDTO>();

    /** El valor de request. */
    private QueryRequest request;

    /**
     * Process.
     * 
     * @param pQueryRequest the query request
     * @return query request
     * @throws Exception la exception
     */
    @Override
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        htc = new HumanTaskClient();
        try {
            request = pQueryRequest;
            this.prepareList();
            List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
            Map<String, Object> rec = null;
            TransactionDTO tr = null;
            for (Entry<String, Integer> e : trn.entrySet()) {
                rec = new HashMap<String, Object>();
                rec.put("c", e.getValue());
                 tr = trnDTO.get(e.getKey());
                rec.put("m", tr.getModule());
                rec.put("t", tr.getTransaction());
                rec.put("v", tr.getVersion());
                rec.put("n", Constant.capitalize(GeneralDescriptions.getTransactiondesc(tr.getModule(), tr.getTransaction(), tr.getVersion())));
                data.add(rec);
            }
            pQueryRequest.getResponse().put("transactions", data);
            return pQueryRequest;
        } finally {
            htc.close();
        }
    }

    /**
     * Prepara la lista de Tareas.
     * 
     * @throws Exception la exception
     */
    private void prepareList() throws Exception {
        if (request.get("groupId") == null) {
            return;
        }
        
        List<Object[]> t = htc.getTasksByGroup(request.getString("groupId"), "%");
        Task task = null;
        TaskUtil tu = null;
        TransactionDTO tr=null;
        Integer count = null;
        for (Object[] obj : t) {
             task = (Task) obj[0];
             tu = new TaskUtil(task, htc);
             tr = tu.getTransaction();
             count = trn.get(tr.toString());
            if (count == null) {
                count = 1;
            } else {
                count++;
            }
            trn.put(tr.toString(), count);
            trnDTO.put(tr.toString(), tr);
        }
    }

}