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

import com.fp.dto.helper.FileHelper;
import com.fp.dto.rules.TransactionRule;
import com.fp.dto.save.SaveRequest;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.commondb.helper.FormatDates;
import com.fp.persistence.pgeneral.image.TgeneFiles;
import com.fp.persistence.pgeneral.image.TgeneFilesDetail;
import com.fp.persistence.pgeneral.image.TgeneFilesDetailKey;
import com.fp.sessionbeans.helper.Sequence;
import java.util.List;

/**
 * Clase que sube un archivo
 * @author pgalindo
 */
public class SafeFileScore extends TransactionRule {

    @Override
    public SaveRequest normalProcess(SaveRequest sr) throws Exception {
        TgeneFilesDetail filesDetail = null;
        Integer filecode = null;
        String xlsfile = sr.getString("xlsFile");
        if (xlsfile.compareTo("YES") == 0) {
            List<FileHelper> fileItem = (List<FileHelper>) sr.get("file");
            if ((fileItem == null || !fileItem.isEmpty())) {
                String code = sr.getString("filecode");
                filecode = ((code.compareTo("") == 0) ? null : Integer.valueOf(code));
                if (filecode == null) {
                    filesDetail = this.createImage(sr, fileItem);
                    filecode = filesDetail.getPk().getCode();
                } else {
                    filecode = sr.getInteger("filecode");
                    filesDetail = this.upDateImage(sr, fileItem, filecode);
                    filecode = filesDetail.getPk().getCode();
                }
                sr.getResponse().put("codes", "[" + filecode + "]");
            }
        }
        return sr;
    }

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

    /**
     * Metodo que crea un nuevo registro en la tabla TGENEFILESDETAIL
     * @param pSaveRequest
     * @param imageItem
     * @return TgeneFilesDetail
     * @throws Exception 
     */
    private TgeneFilesDetail createImage(SaveRequest pSaveRequest, List<FileHelper> imageItem) throws Exception {
        Sequence sequence = new Sequence();
        Integer filecode = Integer.valueOf(sequence.getNextValue("FILECODE").toString());
        TgeneFiles tgeneFiles = new TgeneFiles(filecode);
        PersistenceHelper.save(tgeneFiles);
        TgeneFilesDetailKey tgeneFilesDetailKey = new TgeneFilesDetailKey(filecode, FormatDates.getDefaultExpiryTimestamp());
        TgeneFilesDetail tgeneFilesDetail = new TgeneFilesDetail();
        tgeneFilesDetail.setPk(tgeneFilesDetailKey);
        tgeneFilesDetail.setImage(imageItem.get(0).getImage());
        tgeneFilesDetail.setCapturedate(FormatDates.getInstance().getDataBaseDate());
        tgeneFilesDetail.setImagesize(Integer.parseInt(String.valueOf(imageItem.get(0).getSize())));
        tgeneFilesDetail.setIngressuser(pSaveRequest.getUser());
        tgeneFilesDetail.setExtension(imageItem.get(0).getExtension());
        PersistenceHelper.save(tgeneFilesDetail);
        return tgeneFilesDetail;
    }

    /**
     * Metodo que actualiza el registro de la tabla TGENEFILESDETAIL
     * @param pSaveRequest
     * @param imageItem
     * @param imagecode
     * @return
     * @throws Exception 
     */
    private TgeneFilesDetail upDateImage(SaveRequest pSaveRequest, List<FileHelper> imageItem, Integer imagecode) throws Exception {
        TgeneFilesDetail tgeneFilesDetail = null;
        TgeneFiles tgeneFiles = TgeneFiles.find(PersistenceHelper.getEntityManager(), imagecode);
        if (tgeneFiles != null) {
            TgeneFilesDetailKey filesDetailKey = new TgeneFilesDetailKey(tgeneFiles.getPk(), FormatDates.getDefaultExpiryTimestamp());

            tgeneFilesDetail = TgeneFilesDetail.find(PersistenceHelper.getEntityManager(), filesDetailKey);
            if (tgeneFilesDetail != null) {
                tgeneFilesDetail.setImage(imageItem.get(0).getImage());
                tgeneFilesDetail.setCapturedate(FormatDates.getInstance().getDataBaseDate());
                tgeneFilesDetail.setImagesize(Integer.parseInt(String.valueOf(imageItem.get(0).getSize())));
                tgeneFilesDetail.setModifyuser(pSaveRequest.getUser());
                tgeneFilesDetail.setExtension(imageItem.get(0).getExtension());
                PersistenceHelper.update(tgeneFilesDetail);
            }
        }
        return tgeneFilesDetail;
    }
}
