jueves, octubre 04, 2007

Programmatically Build a Spring Application Context

¿Cómo contruir un contexto de Spring programaticamente?
Respuesta en el Link.

viernes, septiembre 28, 2007

Dates in HSQLDB

Encontre un link para el manejo de fechas en HSQL (y muestra el ejemplo con Oracle).
Para obtener
2007-09-28

Se hace:

year(current_date) '-' month(current_date) '-'
(dayOfMonth(current_date)-60

El post original está aquí.

jueves, febrero 08, 2007

Flavors to Printer

DocFlavor[] flavors = impSelected.getSupportedDocFlavors();
for (int i = 0; i < flavors.length; i++ ) {
try {
System.out.println(BeanUtils.describe(flavors[1]).toString());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
} // for

martes, enero 02, 2007

LongRaw Datatype Oracle

      String query = "SELECT name,airline_logo FROM " +
"otn_airline_longraw WHERE code=?"
;

// Create a PreparedStatement object for executing the query
PreparedStatement pst = connection.prepareStatement(query);

// bind the parameter with code value
pst.setString(1,lcode);

// Obtain the result-set for the selected airline record
ResultSet result = pst.executeQuery();

// get user home folder name
userHome = System.getProperty("user.home");

if( result.next() ) {

// Fetch column values
name = result.getString(1); // Obtain the airline code

// append the file name with user home directory, file separator and
// file extension GIF
fullName = userHome+File.separator+name+".gif";

// LONGRAW data can be accessed in two ways:
// 1) By retrieving all the data at once (using getBytes method)
// 2) By using streams. The LONGRAW data is made available to the program
// as a stream, and the data can be retrieved chunk by chunk, which is
// more eficient in terms of memory usage
// In this sample we illustrate retrieval using streams method.
gifdata = result.getBinaryStream(2);
// create new file
File gifFile = new File(fullName);

// Write the byte array into a local file
FileOutputStream file= new
FileOutputStream(gifFile);

int chunk=0;
// write to the local file until image (LONGRAW) data is found
while( (chunk = gifdata.read()) != -1) {
file.write(chunk);
}

El ejemplo competo está acá [Cátalogo de ejemplos].
Creo que tiene las mejores practicas las clases:
Hace falta un finally para cerrar recursos.
Declarar como final algunos objetos.


sábado, diciembre 16, 2006

Error creating bean with name 'xfire.typeMappingRegistry'

IBM WebSphere Application Server - ND, 6.0.0.1
Número de build: o0445.08
Fecha de build: 11/10/04

Frameworks: Spring & XFire.

Error:
;)org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xfire.typeMappingRegistry' defined in class path resource [org/codehaus/xfire/spring/xfire.xml]: Initialization of bean failed; nested exception is java.lang.VerifyError: (class: org/codehaus/xfire/aegis/type/basic/ObjectType, method: writeSchema signature: (Lorg/jdom/Element V) Incompatible argument to method


La solución es:
WAS tiene po defecto una libreria de JDOM versión 0.7, XFire usa la versión 1.0, al levantar la aplicación el contenedor utiliza las clases de 0.7 de JDOM dado que están en el classLoader del servidor sin importarle la del classLoader de la aplicación (WEB-INF\lib).
Hay que copiar dentro de las librerias del server (C:\Archivos de programa\IBM\WebSphere\AppServer1\lib) la versión 1.0 de JDOM.

Referencia:
Error creating bean with name 'xfire.typeMappingRegistry'

jueves, junio 29, 2006

one-to-one with Hibernate

Cuando tiene una relación uno a uno en la BD, se supone que ambas tablas tiene la misma llave.
Cuando Hibernate hace el mapeo crea una relación one-to-one y como tienen la misma llave, ambas tablas deberian usar el mismo Objeto PK.
Tip: Solemos usar herramientas para generar los mapeos y las clases Java a partir de la BD que no saben identificar el escenario antes descrito (no conozco ningúna herramienta capaz), para no invertirle demasido tiempo a ésto basta con que en la clase hija (de la relación de BD) use la misma PK que la padre, y de la PK que se generó para la clase hija hacer que extienda de la clase PK del padre:
Ejemplo:
Padre en la BD:
name="mx.net.vladpax.java.hbm.Familia" table="IEC_SUBDELEGACION">
name="comp_id" class="mx.net.vladpax.java.hbm.FamiliaPK">
name="cveFamilia" column="CVE_FAMILIA" type="java.lang.Integer" length="2"/>
name="cveTipoFamilia" column="CVE_TIPO_FAMILIA" type="java.lang.Integer" length="2"/>

Hija en la BD:
name="mx.net.vladpax.java.hbm.CasaXFam" table="IEC_MPIO_XSUBDEL" >
name="comp_id" class="
mx.net.vladpax.java.hbm.FamiliaPK">
name="
cveFamilia" column="CVE_FAMILIA" type="java.lang.Integer" length="2"/>
name="
cveTipoFamilia" column="CVE_TIPO_FAMILIA" type="java.lang.Integer" length="2"/>
Si vemos tenemos la misma clase, en la hija la gerramienta me generó una llamada: 'CasaXFamPK' e identica a SubdelegacionPK
La cual modifique de la siguiente forma:

public class CasaXFamPK extends FamiliaPK {

La excepción provacada cuando caemos en el error de no usar la misma clase para relaciones one-to-one es:
exception getting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) getter of paquete.java..nombreClaseHijaPK.?; nested exception is org.hibernate.PropertyAccessException: exception getting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) getter of paquete.java.nombreClaseHijaPK.?

martes, junio 20, 2006

Manejo de BLOB con Hibernate, Spring y Oracle

Clase Form
public class DocumentacionProbatoriaForm extends ValidatorForm{

/**
* Crea una instancia de clase DocumentacionProbatoriaForm.
*/
public DocumentacionProbatoriaForm() {
super();
// TODO Auto-generated constructor stub
}

/**
atributo para almacenar el rutaArchivo
*/
private String rutaArchivo;

/**
atributo para almacenar el archivo
*/
private FormFile archivo;

}// end class

Clase Action
public class DocumentacionProbatoriaMappingDispatchAction extends
MappingDispatchAction {


public ActionForward cargarDocumentoProbatorio(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {

if (logger.isInfoEnabled()) {
logger.info(" #### METODO: cargarDocumentoProbatorio");
}
DocumentacionProbatoriaForm forma = (DocumentacionProbatoriaForm) form;

FormFile docto = forma.getArchivo();


if (logger.isDebugEnabled()) {
logger.debug("size ::" + docto.getFileSize());
logger.debug("name ::" + docto.getFileName());
}//
DocumentacionProbatoriaVO vo = new DocumentacionProbatoriaVO();

vo.setCvePersona4modif(cvePersona4modif);
vo.setArchivo(docto.getFileData());


service = (DocumentacionProbatoriaService) super
.getBean("documentacionProbatoriaService");
// guardamos
vo = service.guardarDocumentacion(vo);
}//end method
}//end class


Implementación de la Clase de Servicio

public class DocumentacionProbatoriaServiceImpl implements
DocumentacionProbatoriaService {

private static Logger logger = Logger.getLogger(DocumentacionProbatoriaServiceImpl.class);

private DocProbatoriaModifDAO docProbatoriaModifDAO;

private FoliadorDAO foliadorDAO;

/**
* Crea una instancia de clase DocumentacionProbatoriaServiceImpl.
*/
public DocumentacionProbatoriaServiceImpl() {
super();
// TODO Auto-generated constructor stub
}

public DocumentacionProbatoriaVO guardarDocumentacion(
DocumentacionProbatoriaVO vo) throws BusinessException {

try {
Integer cveDocProbatoriaModif = this.foliadorDAO.getCveDocProbatoriaModif(vo.getCvePersona4modif());

DocProbatoriaModifPK pk = new DocProbatoriaModifPK();
pk.setCveDocProbatoriaModif(cveDocProbatoriaModif);
pk.setCvePersona4modif(vo.getCvePersona4modif());

DocProbatoriaModif docProba = new DocProbatoriaModif();
docProba.setComp_id(pk);
docProba.setDocProbatoria(this.creaBlob(vo.getArchivo()));
docProba.setDesDocProbatoria(vo.getDesDocProbatoria());
docProba.setDesNombreArchivo(vo.getNombreArchivo());

this.docProbatoriaModifDAO.insertar(docProba);

vo.setCveDocProbatoriaModif(cveDocProbatoriaModif);

// TODO Auto-generated method stub

}
catch (InfrastructureException e) {
logger.error(e.getMessage(), e);
}
return vo;
}

public DocumentacionProbatoriaVO recuperarDocumentacion(
DocumentacionProbatoriaVO vo) throws BusinessException {
try {
DocProbatoriaModifPK pk = new DocProbatoriaModifPK();
pk.setCveDocProbatoriaModif(vo.getCveDocProbatoriaModif());
pk.setCvePersona4modif(vo.getCvePersona4modif());
DocProbatoriaModif result= this.docProbatoriaModifDAO.findByPK(pk);
vo.setArchivo(this.toByteArray(result.getDocProbatoria()));
vo.setDesDocProbatoria(vo.getDesDocProbatoria());
vo.setNombreArchivo(result.getDesNombreArchivo());
return vo;
}
catch (InfrastructureException e) {
logger.error(e.getMessage(), e);
throw new BusinessException(e.getMessage(), e);
}
}


/**
* Recupera todos los documentos de una persona.
Recupera todos los datos excepto el BLOB.

* @param cvePersona4modif
* @return Lista con objetos DocumentacionProbatoriaVO.
* @throws BusinessException
*/
public List recuperaDocumentosXPersona(Integer cvePersona4modif) throws BusinessException{
List response = new ArrayList();

try {
if (logger.isDebugEnabled()){
logger.debug("cvePersona4modif :: " + cvePersona4modif);
}

final List temp = this.docProbatoriaModifDAO.recuperaListaDoctos( cvePersona4modif);
DocProbatoriaModif persitente = null;
DocumentacionProbatoriaVO vo = null;
if (temp!= null && !temp.isEmpty()){
final Iterator it = temp.iterator();
while(it.hasNext()) {
persitente =(DocProbatoriaModif) it.next();
vo = new DocumentacionProbatoriaVO();
vo.setArchivo(null);
vo.setCveDocProbatoriaModif(persitente.getComp_id().getCveDocProbatoriaModif());
vo.setCvePersona4modif(persitente.getComp_id().getCvePersona4modif());
vo.setDesDocProbatoria(persitente.getDesDocProbatoria());
vo.setNombreArchivo(persitente.getDesNombreArchivo());
if (logger.isDebugEnabled()){
logger.debug("DesNombreArchivo :: "
+ persitente.getDesNombreArchivo() + ", "
+ vo.getCveDocProbatoriaModif());
}
response.add(vo);
}
}

return response;
}
catch (InfrastructureException e) {
logger.error(e.getMessage(), e);
throw new BusinessException(e.getMessage(), e);
}
}

/**
* Elimina un documento.
* @param vo
* @throws BusinessException
*/
public void eliminarDocumento(DocumentacionProbatoriaVO vo) throws BusinessException{

try {
DocProbatoriaModifPK pk = new DocProbatoriaModifPK();



pk.setCveDocProbatoriaModif(vo.getCveDocProbatoriaModif());
pk.setCvePersona4modif(vo.getCvePersona4modif());

if (logger.isInfoEnabled()){
logger.info("pk :: " + GeneralSimeUtil.getObjectAsString(pk));
}

DocProbatoriaModif objetoPersistente = this.docProbatoriaModifDAO.findByPK(pk);

this.docProbatoriaModifDAO.eliminar(objetoPersistente);


if (logger.isDebugEnabled()) {
logger.debug("Eliminar [OK]");
}

}
catch (InfrastructureException e) {
logger.error(e.getMessage(), e);
throw new BusinessException(e.getMessage(), e);
}

}

/**
* Crea un BLob a apartir de un arrelog de bytes.
* @param input
* @return
*/
private Blob creaBlob(byte[] input){
return Hibernate.createBlob(input);
}


private byte[] toByteArray(Blob fromBlob) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
return toByteArrayImpl(fromBlob, baos);
}
catch (SQLException e) {
throw new RuntimeException(e);
}
catch (IOException e) {
throw new RuntimeException(e);
}
finally {
if (baos != null) {
try {
baos.close();
}
catch (IOException ex) {
}
}
}
}

private byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos)
throws SQLException, IOException {
byte[] buf = new byte[4000];
InputStream is = fromBlob.getBinaryStream();
try {
for (;;) {
int dataSize = is.read(buf);

if (dataSize == -1)
break;
baos.write(buf, 0, dataSize);
}
}
finally {
if (is != null) {
try {
is.close();
}
catch (IOException ex) {
}
}
}
return baos.toByteArray();
}

/**
* Método que establece el valor del atributo docProbatoriaModifDAO.
*
* @param docProbatoriaModifDAO
* Valor a establecer en el atributo docProbatoriaModifDAO.
*/
public void setDocProbatoriaModifDAO(
DocProbatoriaModifDAO docProbatoriaModifDAO) {
this.docProbatoriaModifDAO = docProbatoriaModifDAO;
}

/**
* Método que establece el valor del atributo foliadorDAO.
*
* @param foliadorDAO
* Valor a establecer en el atributo foliadorDAO.
*/
public void setFoliadorDAO(FoliadorDAO foliadorDAO) {
this.foliadorDAO = foliadorDAO;
}

}//end class

Clase DAO
public class DocProbatoriaModifDAOImpl extends DAOSimeBaseImpl implements DocProbatoriaModifDAO {

/**
* Crea una instancia de clase DocProbatoriaModifDAOImpl.
*/
public DocProbatoriaModifDAOImpl() {
super();
// TODO Auto-generated constructor stub
}

public DocProbatoriaModif findByPK(DocProbatoriaModifPK pk) throws InfrastructureException {
return (DocProbatoriaModif) super.getHibernateTemplate().load(DocProbatoriaModif.class, pk);
}
public List recuperaListaDoctos(Integer cvePersona4modif) throws InfrastructureException{
List response = new ArrayList();

StringBuffer query = new StringBuffer();
query.append(" from DocProbatoriaModif as obj");
query.append(" where obj.comp_id.cvePersona4modif = ");
query.append(cvePersona4modif);

response = super.getHibernateTemplate().find(query.toString());

if (logger.isDebugEnabled()){
logger.debug("query :: " + query);
logger.debug("response.size() :: " + response.size());
}

return response;
}
}//end class

Clase VO
public class DocumentacionProbatoriaVO extends GenericSimeVO {

/**
*
*/
private static final long serialVersionUID = 1L;
private java.lang.Integer cvePersona4modif;
private java.lang.Integer cveDocProbatoriaModif;
/**
* Representación en un arreglo de bytes del archivo
*/
private byte[] archivo;
private java.lang.String nombreArchivo;
private java.lang.String desDocProbatoria;
}//end class

Clase de mapeo de HB:

public abstract class BaseDocProbatoriaModif implements Serializable {

// primary key
private mx.net.vlad.integracion.hbm.DocProbatoriaModifPK comp_id;

// fields
private java.sql.Blob docProbatoria;
private java.lang.String desDocProbatoria;
private java.lang.String desNombreArchivo;
}//end class




Nota
La clases de tipo bean obviamente tienen sus métodos get&set.
La clases que considero más importante y por lo tanto la he puesto intacta es el servicio, ya que hace la transformación del arreglo de bytes al objeto BLOB y viceversa.
La configuración de Spring no tiene nada de especial es por ello que no la pongo.


miércoles, febrero 15, 2006

Can't connect to X11 window server using '0.0' as the value of the DISPLAY

198 cd /usr/openwin/bin/
199 ./Xvfb :0 -screen 0 1152x900x8 &
200 export DISPLAY="127.0.0.1:0.0"
201 ./xclock

xclock no debe generar nigún error.

Ambiente:
SO: Sun Microsystems Inc. SunOS 5.9 Generic May 2002
Server: Weblogic 8.1 sp 3

En el startWeblogic
JAVA_OPTIONS="-Djava.awt.headless=true ${JAVA_OPTIONS}"

martes, diciembre 13, 2005

HqlToken Panic

Error:
CharScanner; panic: ClassNotFoundException: org.hibernate.hql.ast.HqlToken
Inclusive tira al server.
Ambiente:
Hibernate 3
WL 8.1

Agregar la propiedad

org.hibernate.hql.classic.ClassicQueryTranslatorFactory

martes, noviembre 22, 2005