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

import java.util.HashMap;
import java.util.Map;

import com.fp.bpmlib.task.client.TaskUtil;
import com.fp.common.logger.APPLogger;
import com.fp.dto.rules.TransactionRule;
import com.fp.dto.save.SaveRequest;
import com.fp.simple.dto.Responses;

// TODO: Auto-generated Javadoc
/**
 * Class TaskComplete encargada de completar tareas.
 * 
 * @author gfiallos
 */
public class TaskComplete extends TransactionRule {

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

    /**
     * Normal process.
     * 
     * @param pReq the req
     * @return save request
     * @throws Exception la exception
     */
    @Override
    public SaveRequest normalProcess(SaveRequest pReq) throws Exception {
        if (pReq.get(TaskUtil.BPM_TID) == null) {
            return pReq;
        }
        // SaveProcessor.saveAndFlush(pReq);
        long tid = pReq.getLong(TaskUtil.BPM_TID);
        TaskUtil util = new TaskUtil(tid);
        try {
            @SuppressWarnings("unchecked")
            Map<String, Object> data = (Map<String, Object>) pReq.get(TaskUtil.BPM_DATA);
            APPLogger.getLogger().warn("Parametros a la Tarea " + data);
            if (data == null) {
                data = new HashMap<String, Object>();
            }
            Responses resp = Responses.findResponse(pReq.getString(TaskUtil.BPM_STATUS));
            data.put("datosmail", pReq.get("datosmail") == null ? "" : pReq.get("datosmail"));
            String taskname = util.complete(pReq.getJournalId(), data, resp, pReq.getString(TaskUtil.BPM_OBS));
            pReq.put("taskname", taskname);
        } finally {
            util.close();
        }
        return pReq;
    }

    /**
     * Reverse process.
     * 
     * @param pReq the req
     * @return save request
     * @throws Exception la exception
     */
    @Override
    public SaveRequest reverseProcess(SaveRequest pReq) throws Exception {
        return pReq;
    }

}
