package com.es.app.data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.persistence.EntityManager;
import javax.persistence.FlushModeType;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.es.core.jpa.JPAPersistenceManager;
public class GstData {
private static Logger logger = LoggerFactory.getLogger(GstData.class);
/** Grabar datos leidos en lotes */
/*
* public static boolean gstDataReadtoSave( ArrayList<Object> fileInput,
* Map<Integer, Integer> idx) { if (!fileInput.isEmpty()) { try {
* ArrayList<Object> arrrSave = new ArrayList<Object>(); for
* (ArrayList<String> arStr : fileInput) {
* arrrSave.add(GstContent.genWStemData(arStr, idx)); } return
* saveObject(arrrSave); } catch (Exception e) { return false; } } return
* true; }
*/
/** Grabar datos leidos de archivo linea por linea */
/*
* public static boolean gstDataReadtoSave(ArrayList<String> fileInput) { if
* (!fileInput.isEmpty()) { try { return
* saveObject(genWStemData(fileInput)); } catch (Exception e) { return
* false; } } return true; }
*/
/** Grabar Objetos en array */
public static boolean saveObject(ArrayList<Object> arg0) {
JPAPersistenceManager gt = new JPAPersistenceManager();
@SuppressWarnings("static-access")
EntityManager em = gt.getEntityManager();
boolean state = false;
em.getTransaction().begin();
try {
for (Object objS : arg0)
em.persist(objS);
em.getTransaction().commit();
em.close();
state = true;
} catch (DatabaseException e) {
logger.warn("Error de base de datos: SAVE ", e);
rollBack(em);
} catch (Exception e) {
logger.warn("Error general de datos: SAVE ", e);
rollBack(em);
} finally {
if (em.getTransaction().isActive()) {
rollBack(em);
em.close();
}
}
return state;
}
/** Grabar Objetos individual */
public static boolean saveObject(Object arg0) {
JPAPersistenceManager gt = new JPAPersistenceManager();
@SuppressWarnings("static-access")
EntityManager em = gt.getEntityManager();
em.setFlushMode(FlushModeType.COMMIT);
boolean state = false;
em.getTransaction().begin();
try {
em.persist(arg0);
em.getTransaction().commit();
em.close();
state = true;
} catch (DatabaseException e) {
logger.warn("Error de base de datos: SAVE ", e);
rollBack(em);
} catch (Exception e) {
logger.warn("Error general de datos: SAVE ", e);
rollBack(em);
} finally {
if (em.getTransaction().isActive()) {
rollBack(em);
em.close();
}
}
return state;
}
public static boolean updateObject(ArrayList<Object> arg0) {
@SuppressWarnings("static-access")
EntityManager em = new JPAPersistenceManager().getEntityManager();
em.getTransaction().begin();
em.setFlushMode(FlushModeType.COMMIT);
boolean state = false;
try {
for (Object objS : arg0)
em.merge(objS);
em.getTransaction().commit();
em.close();
state = true;
} catch (DatabaseException e) {
logger.warn("Error de base de datos: UPDATE ", e);
rollBack(em);
} catch (Exception e) {
logger.warn("Error general de datos: UPDATE ", e);
rollBack(em);
} finally {
if (em.getTransaction().isActive()) {
rollBack(em);
em.close();
}
}
return state;
}
public static boolean updateObject(Object arg0) {
@SuppressWarnings("static-access")
EntityManager em = new JPAPersistenceManager().getEntityManager();
em.getTransaction().begin();
em.setFlushMode(FlushModeType.COMMIT);
boolean state = false;
try {
em.merge(arg0);
em.getTransaction().commit();
em.close();
state = true;
} catch (DatabaseException e) {
logger.warn("Error de base de datos: UPDATE ", e);
rollBack(em);
} catch (Exception e) {
logger.warn("Error general de datos: UPDATE ", e);
rollBack(em);
} finally {
if (em.getTransaction().isActive()) {
rollBack(em);
em.close();
}
}
return state;
}
public static boolean removeObject(Object arg0) {
@SuppressWarnings("static-access")
EntityManager em = new JPAPersistenceManager().getEntityManager();
boolean state = false;
try {
em.getTransaction().begin();
em.remove(em.merge(arg0));
em.getTransaction().commit();
em.close();
state = true;
} catch (DatabaseException e) {
logger.warn("Error de base de datos: REMOVE ", e);
rollBack(em);
} catch (Exception e) {
logger.warn("Error general de datos: REMOVE ", e);
rollBack(em);
} finally {
if (em.getTransaction().isActive()) {
rollBack(em);
em.close();
}
}
return state;
}
/**
* Llamar a procedimientos: <br/>
* <p>
* Con Parametros: HashParametros<k,v>,"nombreProc"(1,2...,5)
* <p/>
* <p>
* Sin Parametros: null,"nombreProc"
* <p/>
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean execProcedure(HashMap paramshm, String procedure) {
EntityManager em = JPAPersistenceManager.getEntityManager();
em.getTransaction().begin();
try {
Object a = null;
if (paramshm != null) {
Query query = em
.createNativeQuery("Select " + procedure.trim());
Iterator<Object> itKeys = paramshm.keySet().iterator();
while (itKeys.hasNext()) {
Object key = itKeys.next();
query.setParameter((Integer) key, paramshm.get(key));
}
a = query.getSingleResult();
} else
a = em.createNativeQuery("Select " + procedure + "()")
.getSingleResult();
System.out.println(a.toString());
em.getTransaction().commit();
} catch (DatabaseException e) {
logger.warn("Error base de datos: execProcedure ", e);
em.getTransaction().rollback();
return false;
} catch (PersistenceException e) {
logger.warn("Error de persistencia datos: execProcedure ", e);
em.getTransaction().rollback();
return false;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
return false;
}
}
return true;
}
/**
* Llamar a procedimientotos: <br/>
* <p>
* Con Parametros: HashParametros<k,v>,"nombreProc"(1,2...,5)
* <p/>
* <p>
* Sin Parametros: null,"nombreProc"
* <p/>
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String getProcedure(HashMap paramshm, String procedure) {
EntityManager em = JPAPersistenceManager.getEntityManager();
em.getTransaction().begin();
try {
Object a = null;
if (paramshm != null) {
Query query = em
.createNativeQuery("Select " + procedure.trim());
Iterator<Object> itKeys = paramshm.keySet().iterator();
while (itKeys.hasNext()) {
Object key = itKeys.next();
query.setParameter((Integer) key, paramshm.get(key));
}
a = query.getSingleResult();
} else
a = em.createNativeQuery("Select " + procedure + "()")
.getSingleResult();
em.getTransaction().commit();
return a.toString();
} catch (DatabaseException e) {
logger.warn("Error base de datos: DatabaseException ", e);
em.getTransaction().rollback();
return null;
} catch (PersistenceException e) {
logger.warn("Error base de datos: PersistenceException ", e);
em.getTransaction().rollback();
return null;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
return null;
}
}
}
@SuppressWarnings({ "rawtypes" })
public static boolean callValSec(HashMap paramshm) {
EntityManager em = JPAPersistenceManager.getEntityManager();
em.getTransaction().begin();
try {
Object a = em.createNativeQuery("Select valsecrep(?,?)")
.setParameter(1, paramshm.get(1))
// "idCarga"
.setParameter(2, paramshm.get(2)).getSingleResult();
// "fecha"
System.out.println(a.toString());
em.getTransaction().commit();
} catch (DatabaseException e) {
logger.warn("Error base de datos: DatabaseException ", e);
em.getTransaction().rollback();
return false;
} catch (PersistenceException e) {
logger.warn("Error base de datos: PersistenceException ", e);
em.getTransaction().rollback();
return false;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
return false;
}
}
return true;
}
@SuppressWarnings({ "rawtypes" })
public static boolean callNewClients(HashMap paramshm) {
EntityManager em = JPAPersistenceManager.getEntityManager();
em.getTransaction().begin();
try {
Object a = em.createNativeQuery("Select getnewclients(?)")
.setParameter(1, paramshm.get(1)).getSingleResult();
System.out.println(a.toString());
em.getTransaction().commit();
} catch (DatabaseException e) {
logger.warn("Error base de datos: DatabaseException ", e);
em.getTransaction().rollback();
return false;
} catch (PersistenceException e) {
logger.warn("Error base de datos: PersistenceException ", e);
em.getTransaction().rollback();
return false;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
return false;
}
}
return true;
}
@SuppressWarnings({ "rawtypes" })
public static boolean callCheckEmi(HashMap paramshm) {
EntityManager em = JPAPersistenceManager.getEntityManager();
try {
return (Boolean) em.createNativeQuery("Select chekemision(?)")
.setParameter(1, paramshm.get(1)).getSingleResult();
} catch (DatabaseException e) {
logger.warn("Error base de datos: DatabaseException ", e);
em.getTransaction().rollback();
return false;
} catch (PersistenceException e) {
logger.warn("Error base de datos: PersistenceException ", e);
em.getTransaction().rollback();
return false;
}
}
@SuppressWarnings({ "rawtypes" })
public static boolean callGetXML(HashMap paramshm) {
EntityManager em = JPAPersistenceManager.getEntityManager();
em.getTransaction().begin();
try {
Object a = em.createNativeQuery("Select getxml(?,?,?,?,?)")
.setParameter(1, paramshm.get(1))
// "fecha"
.setParameter(2, paramshm.get(2))
// "ruc"
.setParameter(3, paramshm.get(3))
// "ambiente"
.setParameter(4, paramshm.get(4))
// "logcarga"
.setParameter(5, paramshm.get(5)).getSingleResult();// "tipoemi"
System.out.println(a.toString());
em.getTransaction().commit();
} catch (DatabaseException e) {
logger.warn("Error base de datos: DatabaseException ", e);
em.getTransaction().rollback();
return false;
} catch (PersistenceException e) {
logger.warn("Error base de datos: PersistenceException ", e);
em.getTransaction().rollback();
return false;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
return false;
}
}
return true;
}
public static boolean rollBack(EntityManager em) {
try {
em.getTransaction().rollback();
return true;
} catch (Exception e) {
logger.warn("Error general: rollBack ", e);
return false;
}
}
/** Buscar objetos en base de datos */
@SuppressWarnings({ "unchecked" })
public static <C> C findObject(Class<C> clase, Serializable id) {
Object objResul = null;
try {
objResul = JPAPersistenceManager.findObject(clase, id);
JPAPersistenceManager.getEntityManager().refresh(objResul);
} catch (Exception e) {
logger.warn("Error general: findObject ", e);
objResul = null;
}
return (C) objResul;
}
/** Buscar objetos en base de datos por campo en especifico */
@SuppressWarnings({})
public static Object findObjectByField(String Table, String field,
String value) {
JPAPersistenceManager gt = new JPAPersistenceManager();
Object objResul = null;
try {
objResul = gt.getObjects(
"Select a From " + Table + " a Where a." + field + "="
+ value).get(0);
} catch (Exception e) {
logger.warn("Error general: findObjectByField ", e);
objResul = null;
}
return objResul;
}
public static Object getSingleObject(String sql, boolean limit) {
@SuppressWarnings("static-access")
EntityManager em = new JPAPersistenceManager().getEntityManager();
if (limit)
return em.createQuery(sql).setMaxResults(1).getSingleResult();
else
return em.createQuery(sql).getSingleResult();
}
public static Object getSingleNativeObject(String sql, boolean limit) {
@SuppressWarnings("static-access")
EntityManager em = new JPAPersistenceManager().getEntityManager();
if (limit)
return em.createQuery(sql).setMaxResults(1).getSingleResult();
else
return em.createQuery(sql).getSingleResult();
}
}