Initial experiment with identifying the project from the coordinates.

Change-Id: I10a74613f28cf11cd4a6df9317844b0a970e7ca8
diff --git a/classes/common.php b/classes/common.php
index 5710f46..963efe5 100644
--- a/classes/common.php
+++ b/classes/common.php
@@ -616,37 +616,64 @@
 
    $max = 5;
    while ($max-- > 0) {
-    $ca = current($va);
-    $cb = current($vb);
+	    $ca = current($va);
+	    $cb = current($vb);
 
-    // FALSE means that we've run out of terms. If both numbers
-    // run out of terms, then they're the same. It's unlikely
-    // that either of the following two lines will ever evaluate
-    // to true since the getVersion() method should always answer
-    // a three-part version number. But, we'll leave it just in
-    // case something changes later.
-    if ($ca === FALSE && ($cb === FALSE)) {
-     return 0;
-    } elseif ($ca === FALSE) {
-      return -1;
-    } elseif ($cb === FALSE) {
-       return 1;
-    } else {
-     // Compare as integers rather than strings.
-     $ca = (int) $ca;
-     $cb = (int) $cb;
+	    // FALSE means that we've run out of terms. If we're out of terms,
+	    // assume that the value is zero. This allows us to, for example,
+	    // meaningfully and correctly compare '3.0' and '3.0.0'.
+	    if ($ca === FALSE) $ca = 0;
+		if ($cb === FALSE) $cb = 0;
 
-     if ($ca > $cb) {
-      return 1;
-     } elseif ($cb > $ca) {
-       return -1;
-     }
+		// Compare as integers rather than strings.
+		$ca = (int) $ca;
+		$cb = (int) $cb;
 
-     next($va);
-     next($vb);
-    }
+		if ($ca > $cb) {
+			return 1;
+		} elseif ($cb > $ca) {
+			return -1;
+		}
+
+		next($va);
+		next($vb);
    }
 
    return 0;
 }
+
+/**
+ * This function answers the id of the Eclipse project that produces the bits
+ * with the provided ClearlyDefined coordinates.
+ *
+ * FIXME This function doesn't belong here.
+ */
+function getEclipseProjectFor($id) {
+	// FIXME We should draw on a data source of some kind rather than hardcoding.
+	$map = array(
+			'*/*/org.glassfish.grizzly/grizzly-npn*/2.0.0+' => 'ee4j.grizzly',
+			'*/*/org.glassfish.grizzly/grizzly-thrift/1.3.15+' => 'ee4j.grizzly',
+			'*/*/org.glassfish.grizzly/grizzly-memcached/1.3.19+' => 'ee4j.grizzly',
+			'*/*/org.glassfish.grizzly/*/2.4.4+' => 'ee4j.grizzly',
+			'*/*/org.glassfish.hk2*/*/2.6+' => 'ee4j.glassfish',
+			'*/*/org.glassfish.mq/*/5.1.3+' => 'ee4j.openmq',
+			'*/*/org.glassfish.tyrus*/*/1.15+' => 'ee4j.tyrus'
+	);
+
+	$parts = explode('/', $id);
+	foreach($map as $pattern => $project) {
+		$bits = explode('/', $pattern);
+		if (count($bits) < 5) continue;
+		if (!fnmatch($bits[0], $parts[0])) continue;
+		if (!fnmatch($bits[1], $parts[1])) continue;
+		if (!fnmatch($bits[2], $parts[2])) continue;
+		if (!fnmatch($bits[3], $parts[3])) continue;
+		if ($bits[4] == '*') return $project;
+		if (preg_match('/^(?<version>.*)\+$/', $bits[4], $matches)) {
+			if (compareSemanticVersion($matches['version'], $parts[4]) <= 0) return $project;
+		} elseif (compareSemanticVersion($bits[4], $parts[4]) == 0) return $project;
+	}
+
+	return null;
+}
 ?>
\ No newline at end of file
diff --git a/services/license_check.php b/services/license_check.php
index 93eaadf..68232a4 100755
--- a/services/license_check.php
+++ b/services/license_check.php
@@ -49,6 +49,9 @@
  * TODO Parameters for default type and provider.
  */
 
+require_once dirname(__FILE__) . '/../classes/common.php';
+require_once dirname(__FILE__) . '/../classes/License.class.inc';
+
 /**
  * Try to massage the content identifier into ClearlyDefined coordinates. That is,
  * for example, recognize Maven coordinates of the form <em>groupid:artifactid:version</em>, and
@@ -159,6 +162,23 @@
 
 function matchAgainstEclipseProjects(&$results) {
 	foreach(array_keys($results['unmatched']) as $id) {
+		// Attempt to determine the Eclipse Project id from the
+		// ClearlyDefined coordinates. If we can identify the project
+		// then we can provide more information back.
+		if ($projectId = getEclipseProjectFor($id)) {
+			$license = License::getLicense($projectId);
+			unset($results['unmatched'][$id]);
+			$results['approved'][$id] = array(
+					'id' => $id,
+					'license' => $license == null ? '' : $license->getSPDXCode(),
+					'status' => 'approved',
+					'sourceUrl' => '',
+					'definitionUrl' => '',
+					'authority' => $projectId,
+					'confidence' => 100
+			);
+		}
+
 		if ($parts = preg_split('/\//', $id)) {
 			if (matchesEclipseNamespace($parts[2], $parts[3])) {
 				unset($results['unmatched'][$id]);