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

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

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

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

    /** Constante 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 {
        String status = pReq.getString("STATUS");
        Long tid = pReq.getLong("TID");
        TaskUtil t = new TaskUtil(tid);
        try {
            switch (status.charAt(0)) {
            case 'S':
                t.start();
                break;
            case 'C':
                Map<String, Object> data = new HashMap<String, Object>();
                t.complete(pReq.getJournalId(), data, Responses.OK, "OBS");
                break;
            default:
                throw new Exception("Error " + status);
            }
        } finally {
            t.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;
    }

}
