65 lines
1.9 KiB
Plaintext
Executable File
65 lines
1.9 KiB
Plaintext
Executable File
package com.fp.armas.portal.service;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.util.concurrent.Future;
|
|
|
|
import javax.ejb.AsyncResult;
|
|
import javax.ejb.Asynchronous;
|
|
import javax.ejb.Stateless;
|
|
import javax.ejb.TransactionAttribute;
|
|
import javax.ejb.TransactionAttributeType;
|
|
import javax.inject.Inject;
|
|
import javax.persistence.Query;
|
|
|
|
import com.fp.armas.portal.dao.general.GenericDaoImpl;
|
|
import com.fp.armas.portal.dao.registro.SecuenciaDao;
|
|
import com.fp.armas.portal.model.Tgenesequence;
|
|
import com.fp.armas.portal.util.RegistroException;
|
|
|
|
/**
|
|
* EJB que se encarga de obtener indices de las bases de datos, lo ejecutamos en una diferente transacción
|
|
* @author dcruz
|
|
*
|
|
*/
|
|
@Stateless
|
|
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
|
|
public class SequenceBean {
|
|
|
|
/**
|
|
* DAO Genérico
|
|
*/
|
|
@Inject
|
|
private GenericDaoImpl<Tgenesequence> genericDao;
|
|
|
|
/**
|
|
* Referencia al DAO {@link SecuenciaDao}
|
|
*/
|
|
@Inject
|
|
private SecuenciaDao secuenciaDao;
|
|
|
|
/**
|
|
* Retorna una secuencia específica
|
|
* @param codigoSecuencia
|
|
* @return
|
|
*/
|
|
@Asynchronous
|
|
public Future<Long> retornaSecuencia(String codigoSecuencia) {
|
|
Thread.currentThread().setName("SEQUENCEBEAN");
|
|
Query query = genericDao.getEntityManager().createNativeQuery("SELECT CURRENTVALUE FROM TGENESEQUENCES WHERE CODE=:code");
|
|
query.setParameter("code", codigoSecuencia);
|
|
Long valorSecuencia = ((BigDecimal) query.getSingleResult()).longValue();
|
|
this.guardarSecuencia(codigoSecuencia, valorSecuencia + 1);
|
|
return new AsyncResult<Long>(valorSecuencia + 1);
|
|
}
|
|
|
|
/**
|
|
* En este método almaceno el valor actual de secuencia y evitar duplicados
|
|
* @param codigoSecuencia
|
|
*/
|
|
public void guardarSecuencia(String codigoSecuencia, Long valorActual) throws RegistroException{
|
|
Tgenesequence sequence = secuenciaDao.obtenerSecuencia(codigoSecuencia);
|
|
sequence.setCurrentvalue(valorActual);
|
|
secuenciaDao.update(sequence);
|
|
}
|
|
}
|