fixed bug 18120: Dependency error when packaging a module
authorAsier Lostalé <asier.lostale@openbravo.com>
Thu, 28 Jul 2011 17:33:23 +0200
changeset 13332 951aa8d5e79f
parent 13331 6b145146b223
child 13333 52f07f8abaa9
fixed bug 18120: Dependency error when packaging a module
src/org/openbravo/erpCommon/modules/VersionUtility.java
src/org/openbravo/service/system/ModuleValidator.java
--- a/src/org/openbravo/erpCommon/modules/VersionUtility.java	Thu Jul 28 14:45:19 2011 +0200
+++ b/src/org/openbravo/erpCommon/modules/VersionUtility.java	Thu Jul 28 17:33:23 2011 +0200
@@ -503,6 +503,46 @@
       return -1;
   }
 
+  /**
+   * Compares 2 versions taking into account just their major versions.
+   * 
+   * @return<ul> <li>-1 in case ver1 is lower than ver2 <li>0 in case ver1 equals ver2 <li>1 in case
+   *             ver1 is higher than ver2 </ul>
+   */
+  static public int versionCompareStrictMajorVersion(String ver1, String ver2) {
+    if ((ver1 == null || ver1.equals("")) && (ver2 == null || ver2.equals("")))
+      return 0;
+    if ((ver1 == null || ver1.equals("")))
+      return 1;
+    if ((ver2 == null || ver2.equals("")))
+      return -1;
+
+    String s1[] = ver1.split("[.]");
+    String s2[] = ver2.split("[.]");
+
+    int n1[] = new int[3];
+    int n2[] = new int[3];
+
+    for (int i = 0; i < 3; i++) {
+      if (s1.length - 1 < i || s1[i] == null || s1[i].equals(""))
+        n1[i] = 0;
+      else
+        n1[i] = Integer.parseInt(s1[i]);
+
+      if (s2.length - 1 < i || s2[i] == null || s2[i].equals(""))
+        n2[i] = 0;
+      else
+        n2[i] = Integer.parseInt(s2[i]);
+    }
+
+    if (n1[0] > n2[0] || (n1[0] == n2[0] && n1[1] > n2[1]))
+      return 1;
+    else if (n1[0] == n2[0] && n1[1] == n2[1])
+      return 0;
+    else
+      return -1;
+  }
+
   @SuppressWarnings("unchecked")
   static private HashMap<String, Mod> modules2mods(Module[] modules) {
     HashMap<String, Mod> mods = new HashMap<String, Mod>();
--- a/src/org/openbravo/service/system/ModuleValidator.java	Thu Jul 28 14:45:19 2011 +0200
+++ b/src/org/openbravo/service/system/ModuleValidator.java	Thu Jul 28 17:33:23 2011 +0200
@@ -233,38 +233,68 @@
    */
   private void checkDependencyVersion(Module module, SystemValidationResult result) {
     for (ModuleDependency dependentModule : module.getModuleDependencyList()) {
-      String depDefinedVersion = dependentModule.getFirstVersion();
+      String firstVer = dependentModule.getFirstVersion();
+      String lastVer = dependentModule.getLastVersion();
       String depActualVersion = dependentModule.getDependentModule().getVersion();
 
       if (dependentModule.isIncluded()) {
         // for inclusions check the dependency matches exactly with the defined one
-        if (!depActualVersion.equals(depDefinedVersion)) {
+        if (!depActualVersion.equals(firstVer)) {
           result.addWarning(SystemValidationType.MODULE_ERROR, module.getName()
               + " defines inclussion of module " + dependentModule.getDependentModule().getName()
-              + " in version " + depDefinedVersion + ", but actual version in DB is "
-              + depActualVersion + ". They must exactly match.");
+              + " in version " + firstVer + ", but actual version in DB is " + depActualVersion
+              + ". They must exactly match.");
         }
         // check dependencies for included modules recursively
         checkDependencyVersion(dependentModule.getDependentModule(), result);
       } else {
         VersionUtility.VersionComparator vc = new VersionComparator();
-        if (vc.compare(depDefinedVersion, depActualVersion) == 1) {
+        String enforcement = dependentModule.getDependencyEnforcement();
+
+        if (lastVer != null && vc.compare(firstVer, lastVer) > 0) {
           result.addError(SystemValidationType.MODULE_ERROR, module.getName()
               + " defines dependency on " + dependentModule.getDependentModule().getName()
-              + " start version " + depDefinedVersion + ", but actual version in DB is "
-              + depActualVersion);
-        } else if (dependentModule.getLastVersion() == null
-            && vc.compareMajorVersions(depDefinedVersion, depActualVersion) != 0) {
-          result.addError(SystemValidationType.MODULE_ERROR, module.getName()
-              + " defines dependency on " + dependentModule.getDependentModule().getName()
-              + " start version " + depDefinedVersion + ", but actual version in DB is "
-              + depActualVersion + ". Which has a different major version.");
-        } else if (dependentModule.getLastVersion() != null
-            && vc.compare(depActualVersion, dependentModule.getLastVersion()) == 1) {
-          result.addError(SystemValidationType.MODULE_ERROR, module.getName()
-              + " defines dependency on " + dependentModule.getDependentModule().getName()
-              + " end version " + dependentModule.getLastVersion()
-              + ", but actual version in DB is " + depActualVersion);
+              + ". It is incorrect beacuase first version (" + firstVer
+              + ") is higher than last version (" + lastVer + ")");
+        }
+
+        if ("MAJOR".equals(enforcement)) {
+          if (vc.compare(firstVer, depActualVersion) > 0) {
+            result.addError(SystemValidationType.MODULE_ERROR, module.getName()
+                + " defines dependency on " + dependentModule.getDependentModule().getName()
+                + " start version " + firstVer + ", but actual version in DB is "
+                + depActualVersion + ".");
+          } else if (lastVer == null && vc.compareMajorVersions(firstVer, depActualVersion) != 0) {
+            result.addError(SystemValidationType.MODULE_ERROR, module.getName()
+                + " defines dependency on " + dependentModule.getDependentModule().getName()
+                + " start version " + firstVer + ", but actual version in DB is "
+                + depActualVersion + ". Which has a different major version.");
+          } else if (lastVer != null
+              && VersionUtility.versionCompareStrictMajorVersion(depActualVersion, lastVer) > 0) {
+            result.addError(SystemValidationType.MODULE_ERROR, module.getName()
+                + " defines dependency on " + dependentModule.getDependentModule().getName()
+                + " end version " + lastVer + ", but actual version in DB is " + depActualVersion);
+          }
+        } else if ("MINOR".equals(enforcement)) {
+          if (lastVer == null && vc.compare(firstVer, depActualVersion) != 0) {
+            result.addError(SystemValidationType.MODULE_ERROR, module.getName()
+                + " defines a MINOR enforcement dependency on "
+                + dependentModule.getDependentModule().getName() + " version " + firstVer
+                + ", but actual version in DB is " + depActualVersion
+                + ". Both versions must be exactly the same.");
+          } else if (lastVer != null && vc.compare(depActualVersion, lastVer) > 0) {
+            result.addError(SystemValidationType.MODULE_ERROR, module.getName()
+                + " defines a MINOR enforcement dependency on "
+                + dependentModule.getDependentModule().getName() + " end version " + lastVer
+                + ", but actual version in DB is " + depActualVersion);
+          }
+        } else { // NONE
+          if (vc.compare(firstVer, depActualVersion) > 0) {
+            result.addError(SystemValidationType.MODULE_ERROR, module.getName()
+                + " defines NONE enforcement dependency on "
+                + dependentModule.getDependentModule().getName() + " start version " + firstVer
+                + ", but actual version in DB is " + depActualVersion + ".");
+          }
         }
       }
     }