/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fp.general.score.save;

import com.fp.dto.rules.TransactionRule;
import com.fp.dto.save.SaveRequest;
import com.fp.persistence.pgeneral.score.TgeneScoreAdjustment;
import com.fp.persistence.pgeneral.score.TgeneScoreHeader;
import com.fp.persistence.pgeneral.score.TgeneScoreHeaderKey;
import java.sql.Date;
import java.util.List;

/**
 *
 * @author pgalindo
 */
public class SafeScoreHeaderDetail extends TransactionRule {

    @Override
    public SaveRequest normalProcess(SaveRequest sr) throws Exception {
        TgeneScoreHeader scoreHeader = null;
        List<TgeneScoreAdjustment> lTgeneScoreAdjustment = null;
        FileScore scoreAdjustment = new FileScore();
        String xlsfile = sr.getString("xlsFile");
        //String pk = (String) sr.get("pk_scoredate");
        //APPDates date = new APPDates(pk);
        if (xlsfile.compareTo("NO") == 0) {
            Date scoredate = sr.getDate("scoredate");
            Integer company = sr.getInteger("companycode");
            Integer filecode = sr.getInteger("filecode");

            scoreHeader = this.saveHeader(scoredate, company, filecode);
            lTgeneScoreAdjustment = scoreAdjustment.loadScore(scoreHeader, sr, filecode);
            sr.putSaveBean("TGENESCOREHEADER", scoreHeader, 1);
            sr.putSaveBean("TGENESCOREADJUSTMENT", lTgeneScoreAdjustment, 2);
            // GRABAR CALIFICACION MANUAL
        }
        return sr;
    }

    @Override
    public SaveRequest reverseProcess(SaveRequest sr) throws Exception {
        return sr;
    }

    /**
     * Metodo que graba la cabecera de la tabla de calificacion
     * @param sr
     * @return
     * @throws Exception 
     */
    private TgeneScoreHeader saveHeader(Date scoredate, Integer companycode, Integer filecode) throws Exception {
        TgeneScoreHeader tgeneScoreHeader = new TgeneScoreHeader();
        tgeneScoreHeader.setPk(new TgeneScoreHeaderKey(scoredate, companycode));
        tgeneScoreHeader.setFilecode(filecode); //PGA
        tgeneScoreHeader.setTabulated("N");
        return tgeneScoreHeader;
    }
}
