01: package org.osbl.client.wings.devtools.hibernate;
02:
03: import org.osbl.client.wings.shell.Application;
04: import org.osbl.client.wings.shell.Environment;
05: import org.osbl.client.wings.form.DelegateEnvironment;
06: import org.osbl.ServiceProvider;
07: import org.wings.*;
08: import org.hibernate.metadata.ClassMetadata;
09: import org.hibernate.SessionFactory;
10: import org.hibernate.engine.SessionFactoryImplementor;
11: import org.hibernate.type.*;
12:
13: import java.util.*;
14: import java.awt.event.ItemListener;
15: import java.awt.event.ItemEvent;
16:
17: public class HibernateReferences extends SPanel implements Application {
18: SessionFactory sessionFactory = (SessionFactory) ServiceProvider
19: .getInstance().getService("SessionFactory");
20:
21: protected Environment environment = new DelegateEnvironment(true);
22:
23: SComboBox combo;
24: private Map<String, ClassMetadata> allClassMetadata;
25:
26: public HibernateReferences() {
27: super (new SBorderLayout(10, 10));
28:
29: String[] entityNames = getAllClassMetadata().keySet().toArray(
30: new String[0]);
31: Arrays.sort(entityNames);
32: combo = new SComboBox(entityNames);
33: combo.addItemListener(new ItemListener() {
34: public void itemStateChanged(ItemEvent e) {
35: if (e.getStateChange() == ItemEvent.SELECTED)
36: showMetadata(getAllClassMetadata().get(
37: combo.getSelectedItem()));
38: }
39: });
40:
41: add(combo, SBorderLayout.NORTH);
42: environment.setContentPane(this );
43: environment
44: .setTitle("org.osbl.client.wings.devtools.hibernate.references.title");
45: }
46:
47: private void showMetadata(ClassMetadata classMetadata) {
48: System.out.println(classMetadata.getEntityName());
49:
50: for (Iterator<ClassMetadata> iterator = getAllClassMetadata()
51: .values().iterator(); iterator.hasNext();) {
52: ClassMetadata metadata = iterator.next();
53: for (Type type : metadata.getPropertyTypes()) {
54: if (!type.isEntityType())
55: continue;
56:
57: if (type instanceof ManyToOneType) {
58: ManyToOneType manyToOneType = (ManyToOneType) type;
59: if (manyToOneType.getAssociatedEntityName().equals(
60: classMetadata.getEntityName()))
61: System.out.println("<-m2o "
62: + metadata.getEntityName());
63: } else if (type instanceof OneToOneType) {
64: OneToOneType oneToOneType = (OneToOneType) type;
65: if (oneToOneType.getAssociatedEntityName().equals(
66: classMetadata.getEntityName()))
67: System.out.println("<-o2o "
68: + metadata.getEntityName());
69: } else if (type instanceof CollectionType) {
70: CollectionType collectionType = (CollectionType) type;
71: if (collectionType.getAssociatedEntityName(
72: (SessionFactoryImplementor) sessionFactory)
73: .equals(classMetadata.getEntityName()))
74: System.out.println("<-o2m "
75: + metadata.getEntityName());
76: }
77: }
78: }
79: }
80:
81: public Environment getEnvironment() {
82: return environment;
83: }
84:
85: public Map<String, ClassMetadata> getAllClassMetadata() {
86: if (allClassMetadata == null)
87: allClassMetadata = sessionFactory.getAllClassMetadata();
88: return allClassMetadata;
89: }
90: }
|