package com.bitplan.test.quicktable;
import com.bitplan.Projektverwaltung.*;
import com.bitplan.Projektverwaltung.impl.*;
import com.bitplan.bobase.UI;
import java.sql.Types;
import java.util.Collection;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.JFrame;
import quick.dbtable.Column;
import quick.dbtable.DBTable;
import quick.dbtable.DBTableCellListener;
import quick.dbtable.DatabaseChangeListener;
public class HibernateTable extends JFrame {
org.hibernate.Session session;
DBTable dBTable1;
public HibernateTable()
{
//set Frame properties
setSize(600,400);
setVisible(true);
//create a new quicktable
dBTable1 = new DBTable();
//add to frame
getContentPane().add(dBTable1);
//to create the navigation bars for the table
dBTable1.createControlPanel();
try
{
// get Hibernate objects
session=com.bitplan.bobase.HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx=session.beginTransaction();
java.util.List customerList=session.createSQLQuery("select * from Kunde").addEntity(KundeImpl.class).list();
System.out.println("found "+customerList.size()+" customers");
if (customerList.size()==0) {
System.out.println("Creating new customer ");
String[] namen={"Fahl","Müller","Meier","Schulze"};
String[] orte ={"Willich","Münster","Frankfurt","Stuttgart"};
for (int i=0;i<namen.length;i++) {
Kunde k=new KundeImpl();
k.name(namen[i]);
k.adresse(orte[i]);
session.save(k);
}
customerList=session.createSQLQuery("select * from Kunde").addEntity(KundeImpl.class).list();
}
//Hibernate Server usually returns a collection of Remote Objects for a findXXX method
//Using a collection of remote objects directly will be very ineffecient
//so implement the find method such that it returns a collection of local objects.
//The performance will improve a lot when you use local objects
Column c = new Column();
c.setReadOnly(true);
c.setType(Types.VARCHAR);
//we want the column header of this column to be "Order No". In this header we want the
//"Order" to be in first line & "No." to be in second line. To get this we have to separate
//the Order & No. by a newline character
c.setHeaderValue("oid");
//we want the display width of the column to be 60 (this is not the maximum data length)
c.setPreferredWidth(15);
dBTable1.addColumn(c);
c = new Column();
c.setReadOnly(true);
c.setType(Types.VARCHAR);
//we want the column header of this column to be "Order No". In this header we want the
//"Order" to be in first line & "No." to be in second line. To get this we have to separate
//the Order & No. by a newline character
c.setHeaderValue("name");
//we want the display width of the column to be 60 (this is not the maximum data length)
c.setPreferredWidth(15);
dBTable1.addColumn(c);
//load the collection of objects into quicktable
dBTable1.refreshDataObject(customerList,null);
}
catch(Exception e)
{
e.printStackTrace();
}
//add the cell listener so that any changes to any changes made by teh customer in the table
//are updated in the actual data object
dBTable1.addTableCellListener(new HibernateCellListener());
//whenever customer add/delete/modify execute teh corresponding create/remove/set methods
dBTable1.addDatabaseChangeListener(new HibernateChangeListener());
//make all the columns to be editable
dBTable1.setEditable(true);
} // TestQuicktable
//Whenever a customer tries to add/modify/delete a record, we have to call the corresponding Hibernate method
class HibernateChangeListener extends DatabaseChangeListener
{
public boolean beforeDelete(int row)
{
//get the customer id corresponding to this row
String customerId =(String) dBTable1.getValueAt(row, 0);
int option = JOptionPane.showConfirmDialog( HibernateTable.this,"Are you sure to delete the Customer:" + customerId ,"",JOptionPane.OK_CANCEL_OPTION);
if( option == JOptionPane.OK_OPTION )
{
try
{
session.load(Kunde.class,customerId);
JOptionPane.showMessageDialog( HibernateTable.this,"Sucessfully deleted","",JOptionPane.OK_OPTION);
}
catch(Exception e)
{
JOptionPane.showMessageDialog( HibernateTable.this,"Unable to delete " + e.getMessage() ,"",JOptionPane.OK_OPTION);
}
}
//since we have handled the delete, we don't want quicktable to do its delete
return false;
}
public boolean beforeInsert(int row)
{
//get the customer id corresponding to this row
Object customerId = dBTable1.getValueAt(row, 0);
try
{
//to increase the performance, try to do the following create, in one method call
Kunde c = new KundeImpl();
c.name( (String)dBTable1.getValueAt(row, 1));
c.adresse((String)dBTable1.getValueAt(row, 2));
}
catch(Exception e)
{
e.printStackTrace();
}
//since we have handled the delete, we don't want quicktable to do its delete
return false;
}
public boolean beforeUpdate(int row)
{
// whenever customer changes the data in a cell,, we should not update the server immediately
//Instead we should combine all the updates for a record & then do the update in one step,
//when the customer moves to other record
Kunde c = (Kunde)dBTable1.getRowObject(row);
updateLocalCopyInServer(c);
return false;
}
}
/**
* update a local Ocpy
* @param c
*/
public void updateLocalCopyInServer(Kunde c)
{
//this should update the local copy in the server
//I leave the implementation to the developers
}
//whenever customer changes a cell data, we have to update the underlying data object
class HibernateCellListener implements DBTableCellListener
{
public Object cellValueChanged(int row, int col, Object oldValue, Object newValue)
{
try {
Kunde c = (Kunde)dBTable1.getRowObject(row);
//based on which column the customer has updated, call the corresponding object method
if( col == 2)
{
c.name((String)dBTable1.getValueAt(row, 1));
}
else if( col == 3)
{
c.adresse((String)dBTable1.getValueAt(row, 2));
}
} catch (Throwable t) {
UI.internalError(t,"cellValueChanged");
}
//always return the newvalue, so that quicktable updates this newvalue in table
return newValue;
}
}
/**
* @param args
*/
public static void main(String[] args) {
{
try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exception e){}
//create a new table frame
HibernateTable myframe = new HibernateTable();
}
} // main
} // TestQuicktable