details:   /erp/devel/pi/rev/86a42dfae94e
changeset: 8353:86a42dfae94e
user:      Antonio Moreno <antonio.moreno <at> openbravo.com>
date:      Tue Sep 07 11:55:32 2010 +0200
summary:   Fixed issue 14397. Check constraints will be disabled when importing 
reference data

details:   /erp/devel/pi/rev/c59596c69f9f
changeset: 8354:c59596c69f9f
user:      Antonio Moreno <antonio.moreno <at> openbravo.com>
date:      Tue Sep 07 13:13:56 2010 +0200
summary:   Fixed issue 14402. Added a property to bypass the 
checkReferencedOrganizations check.
The way this works is by setting the disableCheckReferencedOrganizations to 
true. For example, in import.sample.data:
ant impo.sample.data -DdisableCheckReferencedOrganizations=true

diffstat:

 build.xml                                                 |    1 +
 src-db/database/lib/dbsourcemanager.jar                   |    0 
 src/build.xml                                             |    2 +-
 src/org/openbravo/dal/core/OBInterceptor.java             |   17 ++-
 src/org/openbravo/service/db/ImportReferenceDataTask.java |   71 ++++++++++++++
 5 files changed, 87 insertions(+), 4 deletions(-)

diffs (169 lines):

diff -r 85e7ba79d184 -r c59596c69f9f build.xml
--- a/build.xml Tue Sep 07 14:08:29 2010 +0530
+++ b/build.xml Tue Sep 07 13:13:56 2010 +0200
@@ -101,6 +101,7 @@
   <property name="friendlyWarnings" value="false"/>
   <property name="checkTranslationConsistency" value="true"/>
   <property name="buildValidation" value="true"/>
+  <property name="disableCheckReferencedOrganizations" value="false"/>
 
   <available file=".hg" property="is.hg" />
 
diff -r 85e7ba79d184 -r c59596c69f9f src-db/database/lib/dbsourcemanager.jar
Binary file src-db/database/lib/dbsourcemanager.jar has changed
diff -r 85e7ba79d184 -r c59596c69f9f src/build.xml
--- a/src/build.xml     Tue Sep 07 14:08:29 2010 +0530
+++ b/src/build.xml     Tue Sep 07 13:13:56 2010 +0200
@@ -235,7 +235,7 @@
       <classpath refid="project.class.path" />
     </taskdef>
     <echo message="Importing sample reference data" />
-    <importsampledata userId="0" adminMode="true" 
propertiesFile="${base.config}/Openbravo.properties" />
+    <importsampledata userId="0" adminMode="true" 
propertiesFile="${base.config}/Openbravo.properties" 
disableCheckReferencedOrganizations="${disableCheckReferencedOrganizations}"/>
     <ant dir="${base.db}" target="database.postupdate.${bbdd.rdbms}" 
inheritAll="true" inheritRefs="true" />
   </target>
 
diff -r 85e7ba79d184 -r c59596c69f9f 
src/org/openbravo/dal/core/OBInterceptor.java
--- a/src/org/openbravo/dal/core/OBInterceptor.java     Tue Sep 07 14:08:29 
2010 +0530
+++ b/src/org/openbravo/dal/core/OBInterceptor.java     Tue Sep 07 13:13:56 
2010 +0200
@@ -61,6 +61,8 @@
 
   private static ThreadLocal<Boolean> preventUpdateInfoChange = new 
ThreadLocal<Boolean>();
 
+  private static ThreadLocal<Boolean> disableCheckReferencedOrganizations = 
new ThreadLocal<Boolean>();
+
   /**
    * If true is passed and we are in adminMode then the update info 
(updated/updatedBy) is not
    * updated when an object gets updated.
@@ -72,6 +74,15 @@
   }
 
   /**
+   * If true is passed then the checkReferencedOrganizations check is not done
+   * 
+   * @param value
+   */
+  public static void setDisableCheckReferencedOrganizations(boolean value) {
+    disableCheckReferencedOrganizations.set(value);
+  }
+
+  /**
    * Determines if the object is transient (==new and not yet persisted in 
Hibernate).
    * 
    * @param entity
@@ -160,9 +171,9 @@
     // }
 
     doEvent(entity, currentState, propertyNames);
-
-    checkReferencedOrganizations(entity, currentState, previousState, 
propertyNames);
-
+    if (!disableCheckReferencedOrganizations.get()) {
+      checkReferencedOrganizations(entity, currentState, previousState, 
propertyNames);
+    }
     if (entity instanceof Traceable || entity instanceof ClientEnabled
         || entity instanceof OrganizationEnabled) {
       return true;
diff -r 85e7ba79d184 -r c59596c69f9f 
src/org/openbravo/service/db/ImportReferenceDataTask.java
--- a/src/org/openbravo/service/db/ImportReferenceDataTask.java Tue Sep 07 
14:08:29 2010 +0530
+++ b/src/org/openbravo/service/db/ImportReferenceDataTask.java Tue Sep 07 
13:13:56 2010 +0200
@@ -21,11 +21,22 @@
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStreamReader;
+import java.util.Properties;
+import java.util.Vector;
 
+import org.apache.commons.dbcp.BasicDataSource;
+import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.PlatformFactory;
+import org.apache.ddlutils.model.Database;
 import org.apache.log4j.Logger;
 import org.openbravo.base.exception.OBException;
+import org.openbravo.dal.core.OBInterceptor;
 import org.openbravo.dal.service.OBDal;
+import org.openbravo.ddlutils.task.DatabaseUtils;
+import org.openbravo.ddlutils.util.DBSMOBUtil;
 
 /**
  * Imports client data for the clients defined in the clients parameter from 
the
@@ -33,11 +44,26 @@
  */
 public class ImportReferenceDataTask extends ReferenceDataTask {
   private static final Logger log = 
Logger.getLogger(ImportReferenceDataTask.class);
+  private Platform platform;
+  private Database xmlModel;
+  private boolean disableCheckReferencedOrganizations = false;
+
+  @Override
+  public void execute() {
+    try {
+      disableConstraints();
+    } catch (Exception e) {
+      log.info("Error disabling check constraint");
+    }
+    super.execute();
+    enableConstraints();
+  }
 
   @Override
   protected void doExecute() {
     final File importDir = getReferenceDataDir();
 
+    
OBInterceptor.setDisableCheckReferencedOrganizations(disableCheckReferencedOrganizations);
     for (final File importFile : importDir.listFiles()) {
       if (importFile.isDirectory() || !importFile.getName().endsWith(".xml")) {
         continue;
@@ -69,4 +95,49 @@
     OBDal.getInstance().commitAndClose();
   }
 
+  private void disableConstraints() throws FileNotFoundException, IOException {
+    String obDir = getProject().getBaseDir().toString() + "/../";
+    Properties obProp = new Properties();
+    obProp.load(new FileInputStream(new File(obDir, 
"config/Openbravo.properties")));
+    // We disable check constraints before inserting reference data
+    String driver = obProp.getProperty("bbdd.driver");
+    String url = obProp.getProperty("bbdd.rdbms").equals("POSTGRE") ? obProp
+        .getProperty("bbdd.url")
+        + "/" + obProp.getProperty("bbdd.sid") : 
obProp.getProperty("bbdd.url");
+    String user = obProp.getProperty("bbdd.user");
+    String password = obProp.getProperty("bbdd.password");
+    BasicDataSource datasource = DBSMOBUtil.getDataSource(driver, url, user, 
password);
+
+    platform = PlatformFactory.createNewPlatformInstance(datasource);
+
+    Vector<File> dirs = new Vector<File>();
+    dirs.add(new File(obDir, "/src-db/database/model/"));
+    File modules = new File(obDir, "/modules");
+
+    for (int j = 0; j < modules.listFiles().length; j++) {
+      final File dirF = new File(modules.listFiles()[j], 
"/src-db/database/model/");
+      if (dirF.exists()) {
+        dirs.add(dirF);
+      }
+    }
+    File[] fileArray = new File[dirs.size()];
+    for (int i = 0; i < dirs.size(); i++) {
+      fileArray[i] = dirs.get(i);
+    }
+    xmlModel = DatabaseUtils.readDatabase(fileArray);
+
+    platform.disableCheckConstraints(xmlModel);
+  }
+
+  private void enableConstraints() {
+    platform.enableCheckConstraints(xmlModel);
+  }
+
+  public void setDisableCheckReferencedOrganizations(boolean 
disableCheckReferencedOrganizations) {
+    this.disableCheckReferencedOrganizations = 
disableCheckReferencedOrganizations;
+  }
+
+  public boolean isDisableCheckReferencedOrganizations() {
+    return disableCheckReferencedOrganizations;
+  }
 }

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to