Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package org.apache.cayenne.modeler.dialog.db;
 
 import java.io.File;
 
 
 import  org.apache.cayenne.access.DbGenerator;
 import  org.apache.cayenne.dba.DbAdapter;
 import  org.apache.cayenne.map.DataMap;
 import  org.apache.cayenne.validation.ValidationResult;

 
 public class DBGeneratorOptions extends CayenneController {
 
     protected DBGeneratorOptionsView view;
     protected ObjectBinding[] optionBindings;
     protected ObjectBinding sqlBinding;
 
     protected DBConnectionInfo connectionInfo;
     protected DataMap dataMap;
     protected DbGenerator generator;
     protected String textForSQL;
 
     protected TableSelectorController tables;
 
     public DBGeneratorOptions(ProjectController parentString title,
             DBConnectionInfo connectionInfo, DataMap dataMap) {
         super(parent);
 
         this. = dataMap;
         this. = new TableSelectorController(parent);
         this. = new DBGeneratorOptionsView(.getView());
         this. = connectionInfo;
         this. = (DBGeneratorDefaultsparent
                 .getPreferenceDomainForProject()
                 .getDetail("DbGenerator"DBGeneratorDefaults.classtrue);
 
         this..setTitle(title);
         initController();
 
         .updateTables(dataMap);
         prepareGenerator();
         createSQL();
         refreshView();
     }
 
     public Component getView() {
         return ;
     }
 
         return ;
     }
 
     public String getTextForSQL() {
         return ;
     }
 
     protected void initController() {
        BindingBuilder builder = new BindingBuilder(
                getApplication().getBindingFactory(),
                this);
         = builder.bindToTextArea(.getSql(), "textForSQL");
         = new ObjectBinding[5];
                "generatorDefaults.createFK",
                "refreshSQLAction()");
                "generatorDefaults.createPK",
                "refreshSQLAction()");
                "generatorDefaults.createTables",
                "refreshSQLAction()");
                "generatorDefaults.dropPK",
                "refreshSQLAction()");
                "generatorDefaults.dropTables",
                "refreshSQLAction()");
        builder.bindToAction(.getGenerateButton(), "generateSchemaAction()");
        builder.bindToAction(.getSaveSqlButton(), "storeSQLAction()");
        builder.bindToAction(.getCancelButton(), "closeAction()");
        // refresh SQL if different tables were selected
        .getTabs().addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                if (.getTabs().getSelectedIndex() == 0) {
                    // this assumes that some tables where checked/unchecked... not very
                    // efficient
                    refreshGeneratorAction();
                }
            }
        });
    }

    
Creates new internal DbGenerator instance.
    protected void prepareGenerator() {
        try {
            DbAdapter adapter = .makeAdapter(getApplication()
                    .getClassLoadingService());
            this. = new DbGenerator(adapter.getExcludedTables());
        }
        catch (Exception ex) {
            reportError("Error loading adapter"ex);
        }
    }

    
Returns SQL statements generated for selected schema generation options.
    protected void createSQL() {
        // convert them to string representation for display
        StringBuffer buf = new StringBuffer();
        Iterator it = .configuredStatements().iterator();
        String batchTerminator = .getAdapter().getBatchTerminator();
        String lineEnd = (batchTerminator != null)
                ? "\n" + batchTerminator + "\n\n"
                : "\n\n";
        while (it.hasNext()) {
            buf.append(it.next()).append(lineEnd);
        }
         = buf.toString();
    }
    protected void refreshView() {
        for (ObjectBinding optionBinding : ) {
            optionBinding.updateView();
        }
        .updateView();
    }
    // ===============
    //    Actions
    // ===============

    
Starts options dialog.
    public void startupAction() {
        .pack();
        .setModal(true);
        makeCloseableOnEscape();
        centerView();
        .setVisible(true);
    }
    public void refreshGeneratorAction() {
        prepareGenerator();
        refreshSQLAction();
    }

    
Updates a text area showing generated SQL.
    public void refreshSQLAction() {
        // sync generator with defaults, make SQL, then sync the view...
        createSQL();
        .updateView();
    }

    
Performs configured schema operations via DbGenerator.
    public void generateSchemaAction() {
        refreshGeneratorAction();
        // sanity check...
        if (.isEmpty(true)) {
            JOptionPane.showMessageDialog(getView(), "Nothing to generate.");
            return;
        }
        try {
            DataSource dataSource = .makeDataSource(getApplication()
                    .getClassLoadingService());
            .runGenerator(dataSource);
            ValidationResult failures = .getFailures();
            if (failures == null || !failures.hasFailures()) {
                JOptionPane.showMessageDialog(getView(), "Schema Generation Complete.");
            }
            else {
                new ValidationResultBrowser(this)
                        .startupAction("Schema Generation Complete",
                                "Schema generation finished. The following problem(s) were ignored.",
                                failures);
            }
        }
        catch (Throwable th) {
            reportError("Schema Generation Error"th);
        }
    }

    
Allows user to save generated SQL in a file.
    public void storeSQLAction() {
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Save SQL Script");
        File projectDir = Application.getProject().getProjectDirectory();
        if (projectDir != null) {
            fc.setCurrentDirectory(projectDir);
        }
        if (fc.showSaveDialog(getView()) == .) {
            refreshGeneratorAction();
            try {
                File file = fc.getSelectedFile();
                FileWriter fw = new FileWriter(file);
                PrintWriter pw = new PrintWriter(fw);
                pw.print();
                pw.flush();
                pw.close();
            }
            catch (IOException ex) {
                reportError("Error Saving SQL"ex);
            }
        }
    }
    public void closeAction() {
        .dispose();
    }
New to GrepCode? Check out our FAQ X