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.util.List;
 
 
 import  org.apache.cayenne.CayenneException;
 import  org.apache.cayenne.CayenneRuntimeException;
 import  org.apache.cayenne.access.DbLoader;
 import  org.apache.cayenne.access.DbLoaderDelegate;
 import  org.apache.cayenne.dba.DbAdapter;
 import  org.apache.cayenne.map.DataMap;
 import  org.apache.cayenne.map.DbEntity;
 import  org.apache.cayenne.map.ObjEntity;
 import  org.apache.cayenne.map.event.DataMapEvent;
 import  org.apache.cayenne.map.event.EntityEvent;
 import  org.apache.cayenne.map.event.MapEvent;
 import  org.apache.cayenne.project.NamedObjectFactory;
 import  org.apache.cayenne.util.DeleteRuleUpdater;
 import  org.apache.cayenne.util.Util;
Stateful helper class that encapsulates access to DbLoader.
 
 public class DbLoaderHelper {
 
     private static Log logObj = LogFactory.getLog(DbLoaderHelper.class);
 
     // TODO: this is a temp hack... need to delegate to DbAdapter, or configurable in
     // preferences...
     private static final Collection EXCLUDED_TABLES = Arrays.asList("AUTO_PK_SUPPORT""auto_pk_support");
 
 
     protected boolean overwritePreferenceSet;
     protected boolean overwritingEntities;
     protected boolean stoppingReverseEngineering;
     protected boolean existingMap;
 
     protected ProjectController mediator;
     protected String dbUserName;
     protected DbLoader loader;
     protected DataMap dataMap;
     protected String schemaName;
     protected String tableNamePattern;
     protected boolean loadProcedures;
     protected boolean meaningfulPk;
     protected String procedureNamePattern;
     protected List schemas;
 
     protected String loadStatusNote;
    
    
Obj Entities which were added to project during reverse engineering
 
     protected List<ObjEntity> addedObjEntities;
 
     static synchronized DbLoaderMergeDialog getMergeDialogInstance() {
         if ( == null) {
              = new DbLoaderMergeDialog(Application.getFrame());
         }
 
         return ;
     }
 
     public DbLoaderHelper(ProjectController mediatorConnection connection,
            DbAdapter adapterString dbUserName) {
        this. = dbUserName;
        this. = mediator;
        this. = new DbLoader(connectionadapternew LoaderDelegate());
    }
    public void setOverwritingEntities(boolean overwritePreference) {
        this. = overwritePreference;
    }
    public void setOverwritePreferenceSet(boolean overwritePreferenceSet) {
        this. = overwritePreferenceSet;
    }
    public void setStoppingReverseEngineering(boolean stopReverseEngineering) {
        this. = stopReverseEngineering;
    }
    public boolean isOverwritePreferenceSet() {
        return ;
    }
    public boolean isOverwritingEntities() {
        return ;
    }
    public boolean isStoppingReverseEngineering() {
        return ;
    }

    
Performs reverse engineering of the DB using internal DbLoader. This method should be invoked outside EventDispatchThread, or it will throw an exception.
    public void execute() {
         = false;
        // load schemas...
        LongRunningTask loadSchemasTask = new LoadSchemasTask(Application
                .getFrame(), "Loading Schemas");
        loadSchemasTask.startAndWait();
        if () {
            return;
        }
        final DbLoaderOptionsDialog dialog = new DbLoaderOptionsDialog(
                ,
                ,
                false);
        try {
            // since we are not inside EventDisptahcer Thread, must run it via
            // SwingUtilities
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    dialog.setVisible(true);
                    dialog.dispose();
                }
            });
        }
        catch (Throwable th) {
            processException(th"Error Reengineering Database");
            return;
        }
        if (dialog.getChoice() == .) {
            return;
        }
        this. = dialog.getSelectedSchema();
        this. = dialog.getTableNamePattern();
        this. = dialog.isLoadingProcedures();
        this. = dialog.isMeaningfulPk();
        this. = dialog.getProcedureNamePattern();
        this. = new ArrayList<ObjEntity>();
        
        this..setNamingStrategy(dialog.getNamingStrategy());
        // load DataMap...
        LongRunningTask loadDataMapTask = new LoadDataMapTask(Application
                .getFrame(), "Reengineering DB");
        loadDataMapTask.startAndWait();
    }
    protected void processException(final Throwable thfinal String message) {
        .info("Exception on reverse engineering", Util.unwindException(th));
        cleanup();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(Application.getFrame(), th
                        .getMessage(), message.);
            }
        });
    }
    protected void cleanup() {
         = "Closing connection...";
        try {
            if (.getConnection() != null) {
                .getConnection().close();
            }
        }
        catch (SQLException e) {
            .warn("Error closing connection."e);
        }
    }
    final class LoaderDelegate implements DbLoaderDelegate {
        public boolean overwriteDbEntity(DbEntity entthrows CayenneException {
            checkCanceled();
            if (!) {
                DbLoaderMergeDialog dialog = DbLoaderHelper.getMergeDialogInstance();
                dialog.initFromModel(DbLoaderHelper.thisent.getName());
                dialog.centerWindow();
                dialog.setVisible(true);
                dialog.setVisible(false);
            }
            if () {
                throw new CayenneException("Should stop DB import.");
            }
            return ;
        }
        public void dbEntityAdded(DbEntity entity) {
            checkCanceled();
             = "Importing table '" + entity.getName() + "'...";
            // TODO: hack to prevent PK tables from being visible... this should really be
            // delegated to DbAdapter to decide...
            if (.contains(entity.getName()) && entity.getDataMap() != null) {
                entity.getDataMap().removeDbEntity(entity.getName());
            }
            else if () {
                
                        .fireDbEntityEvent(new EntityEvent(thisentity, MapEvent.ADD));
            }
        }
        public void objEntityAdded(ObjEntity entity) {
            checkCanceled();
             = "Creating ObjEntity '" + entity.getName() + "'...";
            .add(entity);
            if () {
                .fireObjEntityEvent(new EntityEvent(thisentity, MapEvent.ADD));
            }
        }
        public void dbEntityRemoved(DbEntity entity) {
            checkCanceled();
            if () {
                .fireDbEntityEvent(new EntityEvent(
                        Application.getFrame(),
                        entity,
                        MapEvent.REMOVE));
            }
        }
        public void objEntityRemoved(ObjEntity entity) {
            checkCanceled();
            if () {
                .fireObjEntityEvent(new EntityEvent(Application
                        .getFrame(), entity, MapEvent.REMOVE));
            }
        }
        void checkCanceled() {
            if (isStoppingReverseEngineering()) {
                throw new CayenneRuntimeException("Reengineering was canceled.");
            }
        }
    }
    abstract class DbLoaderTask extends LongRunningTask {
        public DbLoaderTask(JFrame frameString title) {
            super(frametitle);
            setMinValue(0);
            setMaxValue(10);
        }
        @Override
        protected String getCurrentNote() {
            return ;
        }
        @Override
        protected int getCurrentValue() {
            return getMinValue();
        }
        @Override
        protected boolean isIndeterminate() {
            return true;
        }
        @Override
        public boolean isCanceled() {
            return isStoppingReverseEngineering();
        }
        @Override
        public void setCanceled(boolean b) {
            if (b) {
                 = "Canceling..";
            }
            setStoppingReverseEngineering(b);
        }
    }
    final class LoadSchemasTask extends DbLoaderTask {
        public LoadSchemasTask(JFrame frameString title) {
            super(frametitle);
        }
        @Override
        protected void execute() {
             = "Loading available schemas...";
            try {
                 = .getSchemas();
            }
            catch (Throwable th) {
                processException(th"Error Loading Schemas");
            }
        }
    }
    final class LoadDataMapTask extends DbLoaderTask {
        public LoadDataMapTask(JFrame frameString title) {
            super(frametitle);
        }
        @Override
        protected void execute() {
             = "Preparing...";
            DbLoaderHelper.this. = .getCurrentDataMap();
            DbLoaderHelper.this. =  != null;
            if (!) {
                 = (DataMap) NamedObjectFactory.createObject(DataMap.classnull);
                .setName(NamedObjectFactory.createName(DataMap.class
                        .getCurrentDataDomain()));
                .setDefaultSchema();
            }
            if (isCanceled()) {
                return;
            }
             = "Importing tables...";
            try {
                .setCreatingMeaningfulPK();
                .loadDataMapFromDB();
                
                
Update default rules for relationships
                for (ObjEntity addedObjEntity : ) {
                    DeleteRuleUpdater.updateObjEntity(addedObjEntity);
                }
            }
            catch (Throwable th) {
                if (!isCanceled()) {
                    processException(th"Error Reengineering Database");
                }
            }
            if () {
                 = "Importing procedures...";
                try {
                    
                            .loadProceduresFromDB(
                                    ,
                                    ,
                                    );
                }
                catch (Throwable th) {
                    if (!isCanceled()) {
                        processException(th"Error Reengineering Database");
                    }
                }
            }
            cleanup();
            // fire up events
             = "Updating view...";
            if (.getCurrentDataMap() != null) {
                .fireDataMapEvent(new DataMapEvent(
                        Application.getFrame(),
                        ,
                        MapEvent.CHANGE));
                .fireDataMapDisplayEvent(new DataMapDisplayEvent(
                        Application.getFrame(),
                        ,
                        .getCurrentDataDomain(),
                        .getCurrentDataNode()));
            }
            else {
                .addDataMap(Application.getFrame(), );
            }
        }
    }
New to GrepCode? Check out our FAQ X