You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ja...@apache.org on 2009/06/22 17:56:54 UTC

svn commit: r787287 - /ofbiz/trunk/framework/entityext/src/org/ofbiz/entityext/data/EntityDataLoadContainer.java

Author: jaz
Date: Mon Jun 22 15:56:54 2009
New Revision: 787287

URL: http://svn.apache.org/viewvc?rev=787287&view=rev
Log:
added command line options to the install directive which drops/re-creates indices/fks/pks and adjusts column widths; updated the -install -help option to detail these changes

Modified:
    ofbiz/trunk/framework/entityext/src/org/ofbiz/entityext/data/EntityDataLoadContainer.java

Modified: ofbiz/trunk/framework/entityext/src/org/ofbiz/entityext/data/EntityDataLoadContainer.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entityext/src/org/ofbiz/entityext/data/EntityDataLoadContainer.java?rev=787287&r1=787286&r2=787287&view=diff
==============================================================================
--- ofbiz/trunk/framework/entityext/src/org/ofbiz/entityext/data/EntityDataLoadContainer.java (original)
+++ ofbiz/trunk/framework/entityext/src/org/ofbiz/entityext/data/EntityDataLoadContainer.java Mon Jun 22 15:56:54 2009
@@ -22,6 +22,8 @@
 import java.net.MalformedURLException;
 import java.text.NumberFormat;
 import java.util.List;
+import java.util.Map;
+import java.util.TreeSet;
 import java.io.File;
 
 import javolution.util.FastList;
@@ -35,6 +37,8 @@
 import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.entity.GenericDelegator;
 import org.ofbiz.entity.GenericEntityException;
+import org.ofbiz.entity.jdbc.DatabaseUtil;
+import org.ofbiz.entity.model.ModelEntity;
 import org.ofbiz.entity.util.EntityDataLoader;
 import org.ofbiz.service.ServiceDispatcher;
 
@@ -56,6 +60,11 @@
     protected boolean useDummyFks = false;
     protected boolean maintainTxs = false;
     protected boolean tryInserts = false;
+    protected boolean repairColumns = false;
+    protected boolean dropPks = false;
+    protected boolean createPks = false;
+    protected boolean dropConstraints = false;
+    protected boolean createConstraints = false;
     protected int txTimeout = -1;
 
     public EntityDataLoadContainer() {
@@ -132,6 +141,26 @@
                     this.maintainTxs = "true".equalsIgnoreCase(argumentVal);
                 } else if ("inserts".equalsIgnoreCase(argumentName)) {
                     this.tryInserts = "true".equalsIgnoreCase(argumentVal);
+                } else if ("repair-columns".equalsIgnoreCase(argumentName)) {
+                    if (UtilValidate.isEmpty(argumentVal) || "true".equalsIgnoreCase(argumentVal)) {
+                        repairColumns = true;
+                    }
+                } else if ("drop-pks".equalsIgnoreCase(argumentName)) {
+                    if (UtilValidate.isEmpty(argumentVal) || "true".equalsIgnoreCase(argumentVal)) {
+                        dropPks = true;                        
+                    }
+                } else if ("create-pks".equalsIgnoreCase(argumentName)) {
+                    if (UtilValidate.isEmpty(argumentVal) || "true".equalsIgnoreCase(argumentVal)) {
+                        createPks = true;
+                    } 
+                } else if ("drop-constraints".equalsIgnoreCase(argumentName)) {
+                    if (UtilValidate.isEmpty(argumentVal) || "true".equalsIgnoreCase(argumentVal)) {
+                        dropConstraints = true;                      
+                    }
+                } else if ("create-constraints".equalsIgnoreCase(argumentName)) {
+                    if (UtilValidate.isEmpty(argumentVal) || "true".equalsIgnoreCase(argumentVal)) {
+                        createConstraints = true;
+                    }
                 } else if ("help".equalsIgnoreCase(argumentName)) {
                     Debug.log("--------------------------------------", module);
                     Debug.log("java -jar ofbiz.jar -install [options]", module);
@@ -142,6 +171,11 @@
                     Debug.log("-createfks ........... create dummy (placeholder) FKs", module);
                     Debug.log("-maintainTxs ......... maintain timestamps in data file", module);
                     Debug.log("-inserts ............. use mostly inserts option", module);
+                    Debug.log("-repair-columns ........... repair column sizes", module);
+                    Debug.log("-drop-pks ............ drop primary keys", module);
+                    Debug.log("-create-pks .......... create primary keys", module);
+                    Debug.log("-drop-constraints..... drop indexes and foreign keys before loading", module);
+                    Debug.log("-create-constraints... create indexes and foreign keys after loading (default is true w/ drop-constraints)", module);
                     Debug.log("-help ................ display this information", module);
                     System.exit(1);
                 }
@@ -200,6 +234,91 @@
             throw new ContainerException("Unable to locate the datasource helper for the group [" + groupNameToUse + "]");
         }
 
+        // get the database util object
+        DatabaseUtil dbUtil = new DatabaseUtil(helperName);
+        Map<String, ModelEntity> modelEntities;
+        try {
+            modelEntities = delegator.getModelEntityMapByGroup(groupNameToUse);
+        } catch (GenericEntityException e) {
+            throw new ContainerException(e.getMessage(), e);
+        }        
+        TreeSet<String> modelEntityNames = new TreeSet<String>(modelEntities.keySet());    
+        
+        // check for drop index/fks
+        if (dropConstraints) {                   
+            List<String> messages = FastList.newInstance();
+            
+            Debug.logImportant("Dropping foreign key indcies...", module);
+            for (String entityName : modelEntityNames) {
+                ModelEntity modelEntity = modelEntities.get(entityName);
+                if (modelEntity != null) {
+                    dbUtil.deleteForeignKeyIndices(modelEntity, messages);
+                }
+            }
+            
+            Debug.logImportant("Dropping declared indices...", module);
+            for (String entityName : modelEntityNames) {
+                ModelEntity modelEntity = modelEntities.get(entityName);
+                if (modelEntity != null) {
+                    dbUtil.deleteDeclaredIndices(modelEntity, messages);
+                }
+            }
+            
+            Debug.logImportant("Dropping foreign keys...", module);
+            for (String entityName : modelEntityNames) {
+                ModelEntity modelEntity = modelEntities.get(entityName);
+                if (modelEntity != null) {
+                    dbUtil.deleteForeignKeys(modelEntity, modelEntities, messages);
+                }
+            }
+            
+            if (messages.size() > 0) {
+                if (Debug.infoOn()) {
+                    for (String message : messages) {
+                        Debug.logInfo(message, module);
+                    }
+                }
+            }
+        }
+        
+        // drop pks
+        if (dropPks) {
+            List<String> messages = FastList.newInstance();
+            Debug.logImportant("Dropping primary keys...", module);
+            for (String entityName : modelEntityNames) {
+                ModelEntity modelEntity = modelEntities.get(entityName); 
+                if (modelEntity != null) {
+                    dbUtil.deletePrimaryKey(modelEntity, messages);
+                }
+            }
+            
+            if (messages.size() > 0) {
+                if (Debug.infoOn()) {
+                    for (String message : messages) {
+                        Debug.logInfo(message, module);
+                    }
+                }
+            }
+        }
+        
+        // repair columns
+        if (repairColumns) {
+            List<String> fieldsToRepair = FastList.newInstance();
+            List<String> messages = FastList.newInstance();
+            dbUtil.checkDb(modelEntities, fieldsToRepair, messages, false, false, false, false);
+            if (fieldsToRepair.size() > 0) {
+                messages = FastList.newInstance();
+                dbUtil.repairColumnSizeChanges(modelEntities, fieldsToRepair, messages);
+                if (messages.size() > 0) {
+                    if (Debug.infoOn()) {
+                        for (String message : messages) {
+                            Debug.logInfo(message, module);
+                        }
+                    }
+                }
+            }
+        }
+        
         // get the reader name URLs first
         List<URL> urlList = null;
         if (readerNames != null) {
@@ -285,6 +404,63 @@
 
         Debug.logImportant("=-=-=-=-=-=-= Finished the data load with " + totalRowsChanged + " rows changed.", module);
 
+        // create primary keys
+        if (createPks) {
+            List<String> messages = FastList.newInstance();
+            
+            Debug.logImportant("Creating primary keys...", module);
+            for (String entityName : modelEntityNames) {
+                ModelEntity modelEntity = modelEntities.get(entityName);
+                if (modelEntity != null) {
+                    dbUtil.createPrimaryKey(modelEntity, messages);
+                }
+            }
+            if (messages.size() > 0) {
+                if (Debug.infoOn()) {
+                    for (String message : messages) {
+                        Debug.logInfo(message, module);
+                    }
+                }
+            }
+        }
+        
+        // create constraints
+        if (createConstraints) {                   
+            List<String> messages = FastList.newInstance();
+            
+            Debug.logImportant("Creating foreign keys...", module);
+            for (String entityName : modelEntityNames) {
+                ModelEntity modelEntity = modelEntities.get(entityName);
+                if (modelEntity != null) {
+                    dbUtil.createForeignKeys(modelEntity, modelEntities, messages);
+                }
+            }
+            
+            Debug.logImportant("Creating foreign key indcies...", module);
+            for (String entityName : modelEntityNames) {
+                ModelEntity modelEntity = modelEntities.get(entityName);
+                if (modelEntity != null) {
+                    dbUtil.createForeignKeyIndices(modelEntity, messages);
+                }
+            }
+            
+            Debug.logImportant("Creating declared indices...", module);
+            for (String entityName : modelEntityNames) {
+                ModelEntity modelEntity = modelEntities.get(entityName);
+                if (modelEntity != null) {
+                    dbUtil.createDeclaredIndices(modelEntity, messages);
+                }
+            }
+                                    
+            if (messages.size() > 0) {
+                if (Debug.infoOn()) {
+                    for (String message : messages) {
+                        Debug.logInfo(message, module);
+                    }
+                }
+            }
+        }
+        
         return true;
     }