001: package org.osbl.client.wings.devtools.hibernate;
002:
003: import org.osbl.client.wings.form.GenericObjectList;
004: import org.osbl.client.wings.form.ObjectTableModel;
005: import org.osbl.client.wings.shell.Client;
006: import org.osbl.client.wings.XIcons;
007: import org.osbl.persistence.*;
008: import org.osbl.persistence.hibernate.HibernateQueryCommand;
009: import org.conform.*;
010: import org.conform.modifier.InlineModifier;
011: import org.conform.hibernate.HibernateEnvironment;
012: import org.wings.*;
013:
014: import java.awt.event.ActionListener;
015: import java.awt.event.ActionEvent;
016: import java.util.List;
017: import java.util.ArrayList;
018:
019: public class HibernateQueryList extends GenericObjectList {
020: Persistence persistence;
021: SPanel component;
022: private STextField queryTextField;
023:
024: public SComponent getComponent() {
025: if (component == null) {
026: SComponent table = super .getComponent();
027:
028: queryTextField = new STextField();
029: queryTextField.setPreferredSize(SDimension.FULLWIDTH);
030:
031: SButton button = new SButton(XIcons.KEY_ENTER);
032: button.addActionListener(new ActionListener() {
033: public void actionPerformed(ActionEvent e) {
034: performQuery();
035: }
036: });
037:
038: SPanel north = new SPanel(new SBorderLayout(10, 10));
039: north.add(new SLabel("Query"), SBorderLayout.WEST);
040: north.add(this .queryTextField, SBorderLayout.CENTER);
041: north.add(button, SBorderLayout.EAST);
042:
043: component = new SPanel(new SBorderLayout());
044: component.add(north, SBorderLayout.NORTH);
045: component.add(table, SBorderLayout.CENTER);
046: environment.setContentPane(component);
047: environment.setDefaultButton(button);
048: }
049: return component;
050: }
051:
052: private void performQuery() {
053: String query = queryTextField.getText();
054: if (query == null || query.length() == 0)
055: return;
056:
057: HibernateQueryCommand command = (HibernateQueryCommand) getCommandFactory()
058: .createCommand("query");
059: command.setQuery(query);
060: Class type = command.getType();
061:
062: beanMeta = createBeanMeta(type);
063: model = createModel(beanMeta);
064:
065: setModel(model);
066: setColumnModel(model.getColumnModel());
067: refresh();
068: }
069:
070: protected BeanMeta createBeanMeta() {
071: return createBeanMeta(org.osbl.client.wings.devtools.hibernate.Bean.class);
072: }
073:
074: protected BeanMeta createBeanMeta(Class type) {
075: return new VariationBeanMetaProvider(Client.getInstance()
076: .getBeanMetaProvider()).addModifier(
077: new InlineModifier() {
078: protected void configure() {
079: for (PropertyMeta propertyMeta : bean()
080: .getProperties()) {
081: if (propertyMeta.getRelationType() == PropertyMeta.ONE_TO_MANY_RELATION) {
082: propertyMeta.setApplicable(false);
083: }
084: }
085: }
086: }).getBeanMeta(type);
087: }
088:
089: protected ObjectTableModel createModel(BeanMeta beanMeta) {
090: HibernateQueryCommand command = (HibernateQueryCommand) getCommandFactory()
091: .createCommand("query");
092: if (queryTextField != null)
093: command.setQuery(queryTextField.getText());
094: return new HibernateQueryCommandTableModel(command, beanMeta,
095: propertyNames(beanMeta));
096: }
097:
098: private String[] propertyNames(BeanMeta beanMeta) {
099: PropertyMeta[] properties = beanMeta.getProperties();
100: List<String> names = new ArrayList<String>(properties.length);
101: for (PropertyMeta property : properties) {
102: if (property.isApplicable())
103: names.add(property.getName());
104: }
105: return names.toArray(new String[names.size()]);
106: }
107:
108: public Object getRow(int index) { // was protected
109: SaveOrUpdateCommand command = (SaveOrUpdateCommand) getCommandFactory()
110: .createCommand("update");
111: //command.setObject();
112: try {
113: HibernateEnvironment.getInstance().beginTransaction();
114: command.execute();
115: } finally {
116: HibernateEnvironment.getInstance().endTransaction();
117: }
118: return null;
119: }
120:
121: private Persistence getCommandFactory() {
122: if (persistence == null)
123: persistence = (Persistence) Client.getInstance()
124: .getServiceProvider()
125: .getService("QueryPersistence");
126: return persistence;
127: }
128: }
|