Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / Java / Java SE

Persist in DataBase with AspectJ (AOP)

Rate me:
Please Sign up or sign in to vote.
2.75/5 (4 votes)
6 Sep 20046 min read 42.6K   583   13  
One of the multiple forms to give persistence in data base to an object of a class with oriented programming to aspects (AOP) in AspectJ
package persistencia_Aspect;

import java.sql.*;
import java.util.Hashtable;
import java.util.Vector;
import java.lang.reflect.*;
import sun.misc.Compare;

/**
 * @author mlorente@programemos.com
 */
public class Sentencias {
	
	public static String quitarCamposSentencia(Object objeto, String sentencia) {
		int inicio=0;
		int fin=0;
		
		inicio=sentencia.indexOf("{$");
		while (inicio>0) {
			fin=sentencia.indexOf("}");
			sentencia = sentencia.substring(0, inicio) + sentencia.substring(fin+1, sentencia.length());
			inicio=sentencia.indexOf("{$");
		}
		return sentencia;
	}

	public static String reemplazarCamposSentencia(Object objeto, String sentencia) {
		String campo="";
		String valor="";
		int inicio=0;
		int fin=0;
		
		try {
			Class c=objeto.getClass();
			Method m;
			while (sentencia.indexOf("{$")>0) {
				inicio=sentencia.indexOf("{$") + 2;
				fin=sentencia.indexOf("}");
				campo=sentencia.substring(inicio, fin);	
				
				m = c.getDeclaredMethod(campo, new Class[] {});
				
				sentencia=sentencia.replace("{$" + campo + "}", dameValorMetodo(objeto, m).toString());
			}
		} catch (NoSuchMethodException e) {
			System.out.println(e.getMessage());
		}
		return sentencia;
	}

	public static void actualizarDatosObjeto(Object objeto, ResultSet rs, Vector campos) {
		try {
			Class c = objeto.getClass();
			Method m;
			Hashtable hsMethod= dameHashMethod(c);
								
			while (rs.next()) {
				for (int i=1 ; i<=campos.size(); i++) {
					m = (Method)hsMethod.get(campos.elementAt(i-1).toString());					
					dameValorMetodo(objeto, m, rs.getObject(i));
				}
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		} 
	}
	
	public static Object dameValorMetodo(Object objeto, Method metodo) {		
		Object o=null;
		try {			
			o = metodo.invoke(objeto, new Object[] {});
		} catch (InvocationTargetException e){
			System.out.println(e.getMessage());
		} catch (IllegalAccessException e) {
		  System.out.println(e);
		} catch (SecurityException e) {
	       System.out.println(e);
	    } 						
	    return o;
	}

	public static Object dameValorMetodo(Object objeto, Method metodo, Object Parametro) {		
		Object o=null;
		try {			
			o = metodo.invoke(objeto, new Object[] {Parametro});
		} catch (InvocationTargetException e){
			System.out.println(e.getMessage());
		} catch (IllegalAccessException e) {
		  System.out.println(e);
		} catch (SecurityException e) {
	       System.out.println(e);
	    } 						
	    return o;
	}
	
	public static String dameCampoValor(Object objeto, Field f) {
		
		String result="";
		int b;

		try {
			f.setAccessible(true);
					
			result=f.getName() + "=";
			String tipo=f.getType().toString();

			if (tipo.equals("int"))
				result=result + f.getInt(objeto);
			else if (tipo=="byte") 
				result=result + f.getByte(objeto);
			else if (tipo=="short")
				result=result + f.getShort(objeto);
			else if (tipo=="int")
				result=result + f.getInt(objeto);
			else if (tipo=="long")
				result=result + f.getLong(objeto);
			else if (tipo=="float")
				result=result + f.getFloat(objeto);
			else if (tipo=="double")
				result=result + f.getDouble(objeto);
			else if (tipo=="boolean")
				result=result + f.getBoolean(objeto);
			else if (tipo=="char")
				result=result + "'" + f.getChar(objeto) + "'";
			else if (tipo=="String")
				result=result + "'" + f.toGenericString() + "'";
			else 
				result=result + "'" + f.toGenericString() + "'";
			
	    } catch (SecurityException e) {
	        System.out.println(e);
	    } catch (IllegalAccessException e) {
	        System.out.println(e);
	    }						
	    return result;
	}
	
	public static Vector dameCampos(String sentencia) {
		Vector v = new Vector();
		int inicio=0;
		int fin=0;
		String campo;
		
		inicio=sentencia.indexOf("{$");
		while (inicio>-1) {
			fin=sentencia.indexOf("}");
			campo=sentencia.substring(inicio + 2, fin);
			v.addElement(campo);
			sentencia=sentencia.replace("{$" + campo + "}", "");
			inicio=sentencia.indexOf("{$");
		}
		return v;		
	}
	
	private static Hashtable dameHashMethod(Class c) {
		Hashtable hsMethod = new Hashtable();
		
		for (int n=0; n<c.getDeclaredMethods().length; n++) {
			hsMethod.put(c.getDeclaredMethods()[n].getName(), c.getDeclaredMethods()[n]);
		}
		
		return hsMethod;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Spain Spain
Mariano Lorente is programmer analyst in a technological company of Zaragoza (Spain), in addition to webmaster of www.programemos.com Web dedicated to the oriented programming aspects (AOP) with AspectJ and articles of programming.

In its worked race to in systems like AS/400, CICS and lately in Windows, languages like COBOL/400, RPG, COBOL/CICS, Visual Basic, C#, Asp, Asp.Net, XML, Xslt, SQL Server, Oracle, etc…

MCP - Designing and Implementing Desktop Applications with Microsoft Visual Basic 6.0

MCP - Designing and Implementing Distributed Applications with Visual Microsoft Basic 6.0

Comments and Discussions