package com.fp.armas.rules.save.armas;

import java.sql.Date;
import java.util.List;

import com.fp.dto.rules.TransactionRule;
import com.fp.dto.save.SaveRequest;
import com.fp.persistence.parmas.fun.TarmRegistroArmas;
import com.fp.persistence.parmas.soli.TarmArmas;
import com.fp.sessionbeans.helper.Sequence;

public class ImporteArmaExcel extends TransactionRule {

	private static final long serialVersionUID = -7465655492847129136L;

	/**
	 * Armas a Registrar
	 */
	@Override
	public SaveRequest normalProcess(SaveRequest pSaveRequest) throws Exception {
		//lista de armas
		 List<Object> listArmas = pSaveRequest.getSaveBeanModifiedRecords("ARMASIMPORTEEXCEL");
		 //cabecera del arma
		 TarmRegistroArmas registroArmas = (TarmRegistroArmas) pSaveRequest.getSaveBeanModifiedObject("TREGISTROARMAS");
		 registroArmas.setPk(getCodigoRegistro());
		 registroArmas.setFcreacion(new Date(System.currentTimeMillis()));
		 
		 for (Object object : listArmas) {
			 TarmArmas armaNueva = (TarmArmas)object;
			 armaNueva.setPk(getCodigoArma());
			 armaNueva.setFecharegistro(new Date(new java.util.Date().getTime()));
			 armaNueva.setCregistro(registroArmas.getPk());
			 armaNueva.setCodigoarma(armaNueva.getPk());
			 
		}
    	
		return pSaveRequest;
	}

	@Override
	public SaveRequest reverseProcess(SaveRequest pRequest) throws Exception {
		return pRequest;
	}
	
	private Long getCodigoRegistro() throws Exception{
		Sequence sequence = new Sequence();
		Long cregistro = sequence.getNextValue("CREGISTRO").longValue();
		return cregistro;
	}
	
	private String getCodigoArma() throws Exception{
		Sequence sequence = new Sequence();
		String carma = sequence.getNextValue("CARMA").toString();
		return carma;
	}
}
