package org.jivesoftware.spark.plugin;

import org.jivesoftware.spark.util.log.Log;

public class PluginDependency
{
	private String name;
	private String version;
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getVersion() {
		return version;
	}
	
	public void setVersion(String version) {
		this.version = version;
	}
	
	public boolean compareVersion(String version) {
		
		if(version != null && getVersion() != null) {
			String checkVersion[] = version.split("\\.");
			String originalVersion[] = getVersion().split("\\.");
			int maxlength = (originalVersion.length >= checkVersion.length)?originalVersion.length:checkVersion.length;


			// go through all Version-parts
			for(int i= 0; i < maxlength; i++) {
				// if the checked version is too short
				if ( checkVersion.length <= i 
					&& originalVersion[i].equals(0))
					return true;
				else if (checkVersion.length <= i)
					return false;
				// if the original version is long enough
				if(originalVersion.length > i) {
					
					// convert to integer
					try {
						int originalVersNumber = Integer.valueOf(originalVersion[i]).intValue();
						int checkVersNumber = Integer.valueOf(checkVersion[i]).intValue();
						
						// check the numbers
						if(checkVersNumber > originalVersNumber) {
							return true;
						}
						else if(checkVersNumber < originalVersNumber) {
							return false;
						}
					}
					catch(Exception e) {
						Log.error("Version " + checkVersion + " contains letters.", e);
						return false;
					}
				}
				else {
					return true;
				}
			}
			return true;
		}
		return false;
	}
}

@@ -502,16 +502,18 @@
 %09%09String
+%5B%5D
  checkVe
@@ -517,18 +517,16 @@
 kVersion
-%5B%5D
  = versi
@@ -551,16 +551,18 @@
 %09%09String
+%5B%5D
  origina
@@ -569,18 +569,16 @@
 lVersion
-%5B%5D
  = getVe
@@ -622,56 +622,17 @@
 h = 
-(originalVersion.length %3E= checkVersion.length)?
+Math.max(
 orig
@@ -653,9 +653,10 @@
 ngth
-:
+, 
 chec
@@ -670,16 +670,17 @@
 n.length
+)
 ;%0A%0A%0A%09%09%09/
@@ -861,17 +861,19 @@
 .equals(
-0
+%220%22
 ))%0A%09%09%09%09%09
@@ -1109,23 +1109,24 @@
 Integer.
-valueOf
+parseInt
 (origina
@@ -1133,35 +1133,24 @@
 lVersion%5Bi%5D)
-.intValue()
 ;%0A%09%09%09%09%09%09int 
@@ -1179,15 +1179,16 @@
 ger.
-valueOf
+parseInt
 (che
@@ -1204,19 +1204,8 @@
 %5Bi%5D)
-.intValue()
 ;%0A%09%09
@@ -1458,22 +1458,17 @@
 ion %22 + 
-checkV
+v
 ersion +

fix comparison of plugin dependency version numbers.