details:   /erp/devel/pi/rev/4e8a51c71835
changeset: 8003:4e8a51c71835
user:      Martin Taal <martin.taal <at> openbravo.com>
date:      Mon Aug 02 19:15:57 2010 +0200
summary:   Fixes issue 14080: When exporting and then importing the client in 
the same environment i get and error

diffstat:

 src/org/openbravo/service/db/DataImportService.java |  54 +++++++++++++++++---
 1 files changed, 44 insertions(+), 10 deletions(-)

diffs (97 lines):

diff -r e6fc833ee397 -r 4e8a51c71835 
src/org/openbravo/service/db/DataImportService.java
--- a/src/org/openbravo/service/db/DataImportService.java       Mon Aug 02 
19:46:28 2010 +0530
+++ b/src/org/openbravo/service/db/DataImportService.java       Mon Aug 02 
19:15:57 2010 +0200
@@ -208,12 +208,27 @@
         // so that the objects on which other depend are inserted first
         final List<BaseOBObject> toInsert = xec.getToInsert();
         int done = 0;
+        final Set<BaseOBObject> visited = new HashSet<BaseOBObject>();
         final Set<BaseOBObject> inserted = new HashSet<BaseOBObject>();
         for (int i = toInsert.size() - 1; i > -1; i--) {
+
           final BaseOBObject ins = toInsert.get(i);
-          // for (final BaseOBObject ins : toInsert) {
-          insertObjectGraph(ins, inserted);
+
+          // use a temporary visited which is not used in the second round
+          final Set<BaseOBObject> tempVisited = new 
HashSet<BaseOBObject>(inserted);
+
+          // insert the mandatory properties
+          insertObjectGraph(ins, tempVisited, inserted, true);
+
+          // insert the non-mandatory references
+          insertObjectGraph(ins, visited, inserted, null);
+
+          if ((i % 100) == 0) {
+            OBDal.getInstance().flush();
+          }
+
           ir.getInsertedObjects().add(ins);
+
           done++;
         }
         Check.isTrue(done == toInsert.size(),
@@ -511,11 +526,20 @@
     final List<BaseOBObject> toInsert = xec.getToInsert();
     int done = 0;
     final Set<BaseOBObject> inserted = new HashSet<BaseOBObject>();
+    final Set<BaseOBObject> visited = new HashSet<BaseOBObject>();
     for (int i = toInsert.size() - 1; i > -1; i--) {
       final BaseOBObject ins = toInsert.get(i);
-      // for (final BaseOBObject ins : toInsert) {
-      insertObjectGraph(ins, inserted);
+
+      // use a temporary visited which is not used in the second round
+      final Set<BaseOBObject> tempVisited = new 
HashSet<BaseOBObject>(inserted);
+
+      insertObjectGraph(ins, tempVisited, inserted, true);
+      insertObjectGraph(ins, visited, inserted, null);
+
+      OBDal.getInstance().flush();
+
       ir.getInsertedObjects().add(ins);
+
       done++;
     }
     Check.isTrue(done == toInsert.size(), "Not all objects have been inserted, 
check for loop: "
@@ -581,24 +605,34 @@
   // which have not been inserted yet
   // this works fine as long the graph has no cycles
   // if there are cycles then Hibernate needs to resolve those
-  private void insertObjectGraph(BaseOBObject toInsert, Set<BaseOBObject> 
inserted) {
+  // the mandatory parameter is used to first insert only mandatory properties
+  // to take care of cylic relations, i.e. that at least first the mandatory
+  // properties are saved.
+  // This method is called twice with first the mandatory on true and then 
with the
+  // value null. Between the 2 calls the inserted Set is cleared.
+  private void insertObjectGraph(BaseOBObject toInsert, Set<BaseOBObject> 
visited,
+      Set<BaseOBObject> inserted, Boolean mandatory) {
     // prevent infinite looping and don't do the ones we already inserted
     // in a previous objectgraph
-    if (inserted.contains(toInsert)) {
+    if (visited.contains(toInsert)) {
       return;
     }
-    inserted.add(toInsert);
+    visited.add(toInsert);
     final Entity entity = toInsert.getEntity();
     for (final Property property : entity.getProperties()) {
-      if (!property.isPrimitive() && !property.isOneToMany()) {
+      if (!property.isPrimitive() && !property.isOneToMany()
+          && (mandatory == null || property.isMandatory() == mandatory)) {
         final Object value = toInsert.get(property.getName());
         if (value instanceof BaseOBObject && ((BaseOBObject) 
value).isNewOBObject()) {
-          insertObjectGraph((BaseOBObject) value, inserted);
+          insertObjectGraph((BaseOBObject) value, visited, inserted, 
mandatory);
         }
       }
     }
     try {
-      OBDal.getInstance().save(toInsert);
+      if (!inserted.contains(toInsert)) {
+        inserted.add(toInsert);
+        OBDal.getInstance().save(toInsert);
+      }
     } catch (final Exception e) {
       log.warn("There was a problem inserting data in the database.");
       log.info("The following exception was raised: ", e);

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to