/*
 * Created on 24/06/2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package controladores;

import java.awt.event.ActionEvent;
import java.util.Iterator;
import modelo.Sistema;
import views.LogWindow;
import javax.swing.AbstractAction;

import javax.jdo.*;

/**
 * @author JPG y GAS
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class ControladorLoguin extends AbstractAction {

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	
	protected LogWindow owner=null; 
	public ControladorLoguin(LogWindow owner){
		super();
		this.setOwner(owner);
		
	}
	
	public void actionPerformed(ActionEvent e) {
		String userName= owner.jTextFieldUser.getText();
		String password= owner.jPasswordField.getText();
		PersistenceManager pm = null;
					try {
						  //gets the persistence manager and starts the transaction
						pm = this.getPersistenceManager();
						pm.currentTransaction().begin();
						Sistema sistema = this.getSistemaByExtent(pm);
						System.out.println("Exitos , Sistema encontrado");
						//commits the transaction
									   pm.currentTransaction().commit();
					}
				    catch ( Exception ex ) {
							 System.out.println(ex.getMessage());
						System.out.println("ex.getMessage()");
							 pm.currentTransaction().rollback();
						     ex.printStackTrace();
				    }
				    finally {
									   //closes the persistence manager
									   pm.close();
				   }
	 }


	

	

	/**
	 * @return
	 */
	public LogWindow getOwner() {
		return owner;
	}

	/**
	 * @param frame
	 */
	public void setOwner(LogWindow frame) {
		owner = frame;
	}
	public PersistenceManager getPersistenceManager() throws JDOUserException{
			 PersistenceManagerFactory pmf = null;
			 java.util.Properties pmfProps = new java.util.Properties();
			   pmfProps.put(
				   "javax.jdo.PersistenceManagerFactoryClass",
				   "com.poet.jdo.PersistenceManagerFactories" );
				   // names the class responsible for creating the
				   // FastObjects persistence manager factory implementation
			   pmfProps.put(
				   "javax.jdo.option.ConnectionURL",
				   "fastobjects://LOCAL/bin/my_base.j1" );
				   // names a connection to the database
				   // specified by the URL
			   pmf =
				   JDOHelper.getPersistenceManagerFactory( pmfProps );
				   // gets a persistence manager factory
				   // with the specified properties
				   // (non-managed)
			   PersistenceManager pm = null;
			   try
			   {

				   pm = pmf.getPersistenceManager();
			   }catch(JDOUserException ex){}
			 return pm;
		 }

		 public Sistema getSistemaByExtent(PersistenceManager pm){
		   Iterator i =pm.getExtent(Sistema.class,true).iterator();
		   return (Sistema)i.next();

		 }

}
