01: package net.ar.webonswing.petstore.contributors;
02:
03: import javax.swing.*;
04:
05: import net.ar.webonswing.managers.persistence.*;
06: import net.ar.webonswing.petstore.*;
07: import net.ar.webonswing.petstore.helpers.*;
08: import net.ar.webonswing.ui.*;
09: import net.sf.hibernate.exception.*;
10:
11: import org.springframework.orm.hibernate.*;
12:
13: public class HibernateObjectPersistenceContributor extends
14: RootPaneUIContributor {
15: protected String propertyName;
16:
17: public HibernateObjectPersistenceContributor(String aPropertyName) {
18: propertyName = aPropertyName;
19: }
20:
21: public void doPersistenceContribution(
22: PersistenceContributionContainer aPersistenceManager) {
23: ApplicationFrame frame = (ApplicationFrame) ((JRootPane) getJComponent())
24: .getTopLevelAncestor();
25:
26: Object property = Binder.getValue(frame, propertyName);
27: Integer id = (Integer) Binder.getValue(property, "id");
28: String className = property.getClass().getName();
29:
30: aPersistenceManager.persistValue(theComponent, new Object[] {
31: id, className });
32: }
33:
34: public void restorePersistedValue(
35: PersistenceContributionContainer aPersistenceManager) {
36: ApplicationFrame frame = (ApplicationFrame) ((JRootPane) getJComponent())
37: .getTopLevelAncestor();
38:
39: try {
40: Object[] values = (Object[]) aPersistenceManager
41: .restoreValue(theComponent);
42: Integer id = (Integer) values[0];
43:
44: if (id != null) {
45: Class propertyClass = Class.forName(values[1]
46: .toString());
47: Object propertyValue = new HibernateTemplate(frame
48: .getDao().getSessionFactory()).load(
49: propertyClass, id);
50: Binder.setValue(frame, propertyName, propertyValue);
51: }
52:
53: frame.init();
54: } catch (Exception e) {
55: throw new NestableRuntimeException(e);
56: }
57: }
58: }
|