org.rimudb.editor.actions.GenerateJavaAction.java Source code

Java tutorial

Introduction

Here is the source code for org.rimudb.editor.actions.GenerateJavaAction.java

Source

/*
 * Copyright (c) 2008-2011 Simon Ritchie.
 * All rights reserved. 
 * 
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License as published 
 * by the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program.  If not, see http://www.gnu.org/licenses/>.
 */
package org.rimudb.editor.actions;

import java.awt.event.*;
import java.io.*;

import javax.swing.*;

import org.apache.commons.logging.*;
import org.rimudb.configuration.*;
import org.rimudb.editor.*;
import org.rimudb.editor.exporter.*;
import org.rimudb.editor.generator.*;
import org.rimudb.editor.util.*;
import org.rimudb.util.*;

/**
 * @author Simon Ritchie
 *
 */
public class GenerateJavaAction extends AbstractAction {
    private final static Log log = LogFactory.getLog(GenerateJavaAction.class);
    private static final long serialVersionUID = 1L;
    private final RimuDBEditor rimudbEditor;

    public GenerateJavaAction(RimuDBEditor rimudbEditor, String name) {
        super(name);
        this.rimudbEditor = rimudbEditor;
    }

    public GenerateJavaAction(RimuDBEditor rimudbEditor, String name, Icon icon) {
        super(name, icon);
        this.rimudbEditor = rimudbEditor;
    }

    public GenerateJavaAction(RimuDBEditor rimudbEditor, Icon icon) {
        super(null, icon);
        this.rimudbEditor = rimudbEditor;
    }

    public void actionPerformed(ActionEvent e) {
        log.debug("actionPerformed()");
        ValidationMessage messages[] = rimudbEditor.getDescriptorEditor().validateData();
        if (messages != null) {
            StringBuilder sb = new StringBuilder();
            boolean hasErrorMessages = false;
            boolean hasWarningMessages = false;
            for (int i = 0; i < messages.length; i++) {
                sb.append(messages[i].getMessage());
                sb.append("\n");
                if (messages[i].getLevel() == ValidationMessage.ERROR) {
                    hasErrorMessages = true;
                }
                if (messages[i].getLevel() == ValidationMessage.WARNING) {
                    hasWarningMessages = true;
                }
            }
            if (hasErrorMessages) {
                JOptionPane.showMessageDialog(rimudbEditor, sb.toString(), "Error", JOptionPane.ERROR_MESSAGE);
                return;
            } else if (hasWarningMessages) {
                JOptionPane.showMessageDialog(rimudbEditor, sb.toString(), "Warning", JOptionPane.WARNING_MESSAGE);
            }
        }
        CreateClassesDialog dialog = new CreateClassesDialog(rimudbEditor, rimudbEditor.getFile());
        dialog.setVisible(true);
        if (dialog.getResult() == CreateClassesDialog.OK) {
            final CreateClassesDialog finalDialog = dialog;
            Thread runner = new Thread() {
                public void run() {
                    buildClasses(finalDialog.isDirectoryOutputSelected(), finalDialog.getDirectory(),
                            finalDialog.isDOSelected(), finalDialog.isFinderSelected(),
                            finalDialog.isGenerateCreateTableSelected(), finalDialog.getDatabase(),
                            finalDialog.isUseQuotes());
                }
            };
            runner.start();
        }
    }

    private void buildClasses(boolean directoryOutput, String directory, boolean doClass, boolean finderClass,
            boolean createTableSQL, String databaseName, boolean useQuotes) {
        DescriptorModel descriptorModel = rimudbEditor.getDescriptorEditor().createDescriptorModel();
        Generator generator = new Generator();
        generator.setDbDirectory(directory);

        GeneratedClassesDialog dialog = null;
        if (!directoryOutput) {
            dialog = new GeneratedClassesDialog(rimudbEditor);
        }

        try {
            // Build the data object
            if (doClass) {
                String text = generator.createDO(descriptorModel, directoryOutput);
                if (!directoryOutput) {
                    String title = descriptorModel.getDataObjectClass();
                    dialog.addTabbedPanel(title, text);
                }
            }

            // Build the finder
            if (finderClass) {
                String text = generator.createFinder(descriptorModel, directoryOutput);
                if (!directoryOutput) {
                    String title = descriptorModel.getFinderClass();
                    dialog.addTabbedPanel(title, text);
                }
            }

            // Build the CREATE TABLE statement
            if (createTableSQL) {
                String tableName = rimudbEditor.getDescriptorEditor().getTableName();
                // Assume version 10 for Oracle
                int databaseMajorVersion = 0;
                if (databaseName.equals("Oracle")) {
                    databaseMajorVersion = 10;
                }
                SQLExporter exporter = new SQLExporter(descriptorModel, tableName, databaseName,
                        databaseMajorVersion, useQuotes);
                String text = exporter.build();
                if (directoryOutput) {
                    generator.writeFileSQLFile(text, tableName);
                } else {
                    dialog.addTabbedPanel("CREATE TABLE", text);
                }
            }

        } catch (IOException e) {
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame, RimuUtils.convertStackTrace(e), "Error",
                    JOptionPane.ERROR_MESSAGE);
        }

        if (dialog != null) {
            dialog.setVisible(true);
        }
    }

}