package projectv1a; import java.util.Enumeration; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.transaction.NotSupportedException; import javax.transaction.SystemException; import javax.transaction.UserTransaction; import org.exolab.castor.jdo.Database; import org.exolab.castor.jdo.JDOManager; import org.exolab.castor.mapping.MappingException; import org.exolab.castor.jdo.PersistenceException; import org.exolab.castor.jdo.ClassNotPersistenceCapableException; import org.exolab.castor.jdo.TransactionNotInProgressException; import org.exolab.castor.jdo.DuplicateIdentityException; import org.exolab.castor.jdo.TransactionAbortedException; import org.exolab.castor.jdo.OQLQuery; import org.exolab.castor.jdo.QueryResults; import org.exolab.castor.jdo.QueryException; /** * Clase DataBase * * Se utiliza para controlar Castor y realizar las operaciones deseadas */ public class DataBase { private Database db; private InitialContext ctx; private UserTransaction ut; public DataBase() { } /** * Método connect * * Para conectar por medio de castor la aplicacion con la Base de Datos */ public void connect(){ JDOManager jdoManager; // load the JDO configuration file and construct a new JDOManager for the database 'mydb' try{ /* MIRAR EL FICHERO BIEN */ // Hay que cambiar esta ruta y ponerla relativa JDOManager.loadConfiguration("./jdo-config.xml"); jdoManager = JDOManager.createInstance("IKAT"); db = jdoManager.getDatabase(); }catch(MappingException e){ System.out.println("Ha petado al conectar - mappingException"); e.printStackTrace(); }catch(PersistenceException e){ System.out.println("Ha petado al conectar - persistanceException"); e.printStackTrace(); } } /** * Método experimental */ public CommunicationInterface load(){ CommunicationInterface cI = new CommunicationInterface(); try{ db.setAutoStore(true); db.begin(); //** HECHAR UN VISTAZO AL LOAD EN EL ESCRITORIO **// Class type = CommunicationInterface.class; // La clase que queremos cargar int _identity = 1; // La clave de la tabla cI = (CommunicationInterface) db.load(type, new Integer(1)); /* OQLQuery oql = db.getOQLQuery("SELECT c FROM projectv1a.CommunicationInterface c where identity=$1"); oql.bind(_identity); QueryResults resultado = oql.execute(); while(resultado.hasMore()){ cI = (CommunicationInterface) resultado.next(); } resultado.close(); oql.close(); */ } catch(PersistenceException e){ System.out.println("Ha petado al cargar la base de datos"); e.printStackTrace(); System.out.println("Error al cargar la base de datos"); } try{ db.commit(); } catch(TransactionNotInProgressException e){ System.out.println("Ha petado en el load -> commit - TransactionNotInProgressException"); } catch(TransactionAbortedException e){ System.out.println("Ha petado en el load -> commit - TransactionAbortedException"); } catch(Exception e){ System.out.println("Ha petado en el load -> commit - "+e.getMessage()); e.printStackTrace(); } return cI; } public ClassSpecification loadClass(){ ClassSpecification iL = new ClassSpecification(); try{ db.setAutoStore(true); db.begin(); //** HECHAR UN VISTAZO AL LOAD EN EL ESCRITORIO **// Class type = ClassSpecification.class; // La clase que queremos cargar int identity = 1; // La clave de la tabla iL = (ClassSpecification) db.load(type, identity); } catch(PersistenceException e){ System.out.println("Ha petado al cargar la base de datos"); e.printStackTrace(); System.out.println("Error al cargar la base de datos"); } try{ db.commit(); } catch(TransactionNotInProgressException e){ System.out.println("Ha petado en el load -> commit - TransactionNotInProgressException"); } catch(TransactionAbortedException e){ System.out.println("Ha petado en el load -> commit - TransactionAbortedException"); } catch(Exception e){ System.out.println("Ha petado en el load -> commit - "+e.getMessage()); e.printStackTrace(); } return iL; } /* public void CommunicationInterface initialContext(){ CommunicationInterface cI = new CommunicationInterface(); // accedemos a la base de datos a traves de JNDI try{ ctx = new InitialContext(); db = (Database) ctx.lookup("java:comp/env/jdo/mydb"); //Comenzamos la transacción ut = (UserTransaction) ctx.lookup("java:comp/UserTransaction"); try{ ut.begin(); }catch(NotSupportedException e){ System.out.println("NotSuppertedException"); }catch(SystemException e){ System.out.println("SystemException"); } } catch(NamingException e){ e.printStackTrace(); } }*/ /** * commit */ public void commit(){ // Explicitly commit transaction try{ db.commit(); } catch(TransactionNotInProgressException e){ System.out.println("TransactionNotInProgressException"); } catch(TransactionAbortedException e){ System.out.println("TransactionAbortedException"); } catch(Exception e){ e.printStackTrace(); } } public void close(){ try{ db.close(); } catch(PersistenceException e){ e.printStackTrace(); } } /* public void insert(classes.Lom lom){ try{ db.setAutoStore(true); db.begin(); db.create(lom); } catch(PersistenceException e){ e.printStackTrace(); } // Explicitly commit transaction try{ db.commit(); } catch(TransactionNotInProgressException e){ } catch(TransactionAbortedException e){ } catch(Exception e){ e.printStackTrace(); } try{ db.close(); } catch(PersistenceException e){ e.printStackTrace(); } }*/ public void insert(SlotSpecification slotSpecification){ try{ db.setAutoStore(true); db.begin(); db.create(slotSpecification); } catch(PersistenceException e){ e.printStackTrace(); } // Explicitly commit transaction try{ db.commit(); } catch(TransactionNotInProgressException e){ e.printStackTrace(); } catch(TransactionAbortedException e){ e.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } try{ db.close(); } catch(PersistenceException e){ e.printStackTrace(); } } public void insert(CommunicationInterface commInterface){ try{ db.setAutoStore(true); db.begin(); db.create(commInterface); } catch(PersistenceException e){ e.printStackTrace(); } // Explicitly commit transaction try{ db.commit(); } catch(TransactionNotInProgressException e){ e.printStackTrace(); } catch(TransactionAbortedException e){ e.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } try{ db.close(); } catch(PersistenceException e){ e.printStackTrace(); } } /* public classes.Lom select(Integer x){ OQLQuery oql; QueryResults results; System.out.println("En el select()"); try{ db.begin(); oql = db.getOQLQuery("SELECT p FROM classes.Lom p WHERE p.identity=$1"); oql.bind(x); results = oql.execute(Database.ReadOnly); Lom lom; System.out.println("en el try"); while(results.hasMore()){ lom = (Lom)results.next(); System.out.println("Lom del try: " +lom); return lom; } }catch(PersistenceException e){ System.out.println("Exception"); e.printStackTrace(); } return null; } public Enumeration select(String consulta){ OQLQuery oql; QueryResults results; System.out.println("En el select()"); try{ db.begin(); oql = db.getOQLQuery(consulta); results = oql.execute(Database.ReadOnly); return results; }catch(PersistenceException e){ System.out.println("Exception"); e.printStackTrace(); } return null; }*/ }