Finished and completed all implemention of the GenericFriendFinder face app and added duplicate handling functionality to FacebookFriendFinder 

git-svn-id: https://svn.apache.org/repos/asf/incubator/photark/trunk@1158132 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/facebook/FacebookFriendFinder.java b/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/facebook/FacebookFriendFinder.java
index a9f56e7..e00297e 100644
--- a/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/facebook/FacebookFriendFinder.java
+++ b/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/facebook/FacebookFriendFinder.java
@@ -24,12 +24,39 @@
 @Remotable
 public interface FacebookFriendFinder {
 
+
+    /**
+     * Recognize all facebook friends who are in the give picture
+     * @param pathToFile Local file location of the image which should be recognized
+     * @param photarkUid Current logged in user's Photark security ID
+     * @return Entry
+     */
     public Entry<String, String[]>[] getAllMyFBFriendsFromPictureLocal(String pathToFile,String photarkUid);
 
+
+
+    /**
+     * Recognize all facebook friends who are in the give picture
+     * @param fileUrl  URL of the file location of the image which should be recognized
+     * @param photarkUid  Current logged in user's Photark security ID
+     * @return Entry
+     */
     public Entry<String, String[]>[] getAllMyFBFriendsFromPictureUrl(String fileUrl,String photarkUid);
 
+
+
+    /**
+     *  Authenticate Photark Face recognition Facebook  app
+     * @param facebookId
+     * @param fbAccessToken
+     */
     public void setFacebookAuth(String facebookId, String fbAccessToken);
 
+
+    /** Stores the Facebook access token in local JCR with the user's security credentials
+     *
+     * @param photarkUid
+     * @param accessToken
+     */
     public void storeFacebookAccessToken(String photarkUid, String accessToken);
-      public Entry<String, String[]>[] check();
 }
diff --git a/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/facebook/FacebookFriendFinderImpl.java b/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/facebook/FacebookFriendFinderImpl.java
index 15c9f7a..8640a47 100644
--- a/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/facebook/FacebookFriendFinderImpl.java
+++ b/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/facebook/FacebookFriendFinderImpl.java
@@ -36,15 +36,12 @@
 import org.oasisopen.sca.annotation.Scope;
 
 import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.*;
 
 @Scope("COMPOSITE")
 public class FacebookFriendFinderImpl implements FacebookFriendFinder {
 
     private FaceRecognitionService faceRecognitionService;
-    private final String adamFBUserId = "";
-    private final String adamAccessToken = "";
     private AccessManager accessManager;
 
     @Init
@@ -63,14 +60,6 @@
         this.accessManager = accessManager;
     }
 
-    public Entry<String, String[]>[] check() {
-        List<Entry<String, String[]>> detectedFriends = new ArrayList<Entry<String, String[]>>();
-        detectedFriends.add(new Entry<String, String[]>("uid", new String[]{"AAAA", "BBBB"}));
-        Entry<String, String[]>[] imageArray = new Entry[detectedFriends.size()];
-        return detectedFriends.toArray(imageArray);
-
-    }
-
     public Entry<String, String[]>[] getAllMyFBFriendsFromPictureLocal(String pathToFile, String photarkUid) {
 
         return processFBFriends(pathToFile, true, photarkUid);
@@ -96,6 +85,8 @@
         PhotarkPhoto photo = null;
         List<Entry<String, String[]>> detectedFriends = new ArrayList<Entry<String, String[]>>();
         String accessToken = accessManager.getUserFacebookAccessToken(photarkUid, Constants.REGISTERED_USER_LIST);
+        FacebookClient facebookClient = new DefaultFacebookClient(accessToken);
+
         try {
             faceRecognitionService.setFacebookOauth2(getMyFacebookUserId(accessToken), accessToken);
             if (isLocal) {
@@ -106,6 +97,7 @@
 
 //          output tuple = [name, link, gender, confidence,]
 
+            Map<String, Integer> faceMap = new HashMap<String, Integer>();
             for (PhotArkFace face : photo.getPhotArkFaces()) {
 
                 String uid = "";
@@ -116,7 +108,13 @@
                     uid = face.getGuess().getGuessID();
                     confidence = face.getGuess().getConfidence();
                     gender = face.getGender();
-                    detectedFriends.add(new Entry<String, String[]>(uid, getFacebookUserDataTuple(accessToken, uid, gender, confidence)));
+
+                    faceMap.put(uid, Integer.valueOf(confidence));
+                    if (faceMap.containsKey(uid) && (faceMap.get(uid) < Integer.valueOf(confidence))) {
+                        faceMap.remove(uid);
+                    }
+
+                    detectedFriends.add(new Entry<String, String[]>(uid, getFacebookUserDataTuple(facebookClient, uid, gender, confidence)));
                 } else {
                     System.out.println("??? Unidentified ..");
                 }
@@ -127,11 +125,31 @@
         } catch (FaceServerException e) {
             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
         }
-        //TODO If want can validate and remove duplicates from the "detectedFriends".
 
-        Entry<String, String[]>[] dataArray = new Entry[detectedFriends.size()];
-        return detectedFriends.toArray(dataArray);
+        return removeDuplicates(detectedFriends);
+    }
 
+    private Entry<String, String[]>[] removeDuplicates(List<Entry<String, String[]>> detectedFriends) {
+        List<Entry<String, String[]>> filteredDetectedFriends = new ArrayList<Entry<String, String[]>>();
+        Map faceFilter = new HashMap();
+        for (int i = 0; i < detectedFriends.size(); i++) {
+            Entry<String, String[]> entry = detectedFriends.get(i);
+            String key = entry.getKey();
+            int maxConfidence = 0;
+            if (!faceFilter.containsKey(key)) {
+                for (int j = 0; j < detectedFriends.size(); j++) {
+                    Entry<String, String[]> subEntry = detectedFriends.get(j);
+                    int confidence = Integer.valueOf(subEntry.getData()[3]);
+                    if ((key == subEntry.getKey()) && (confidence > maxConfidence)) {
+                        maxConfidence = confidence;
+                    }
+                }
+            }
+            filteredDetectedFriends.add(new Entry<String, String[]>(entry.getData()[0], new String[]{entry.getData()[0], entry.getData()[1], entry.getData()[2], String.valueOf(maxConfidence)}));
+            faceFilter.put(key, "added");
+        }
+        Entry<String, String[]>[] dataArray = new Entry[filteredDetectedFriends.size()];
+        return filteredDetectedFriends.toArray(dataArray);
     }
 
     private String getMyFacebookUserId(String accessToken) {
@@ -140,12 +158,10 @@
         return user.getId();
     }
 
-    private String[] getFacebookUserDataTuple(String accessToken, String uid, String gender, String confidence) {
+    private String[] getFacebookUserDataTuple(FacebookClient facebookClient, String uid, String gender, String confidence) {
         String name = "";
         String link = "";
         String userId = uid.trim().split("@")[0];
-
-        FacebookClient facebookClient = new DefaultFacebookClient(accessToken);
         User user = facebookClient.fetchObject(userId, User.class);
         name = user.getName();
         link = user.getLink();
diff --git a/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/generic/GenericFriendFinder.java b/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/generic/GenericFriendFinder.java
index 75690ac..c600813 100644
--- a/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/generic/GenericFriendFinder.java
+++ b/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/generic/GenericFriendFinder.java
@@ -25,14 +25,39 @@
 @Remotable
 public interface GenericFriendFinder {
 
-    public String check();
 
+    /**
+     * Train the face.com index with the given picture
+     * @param imagePath  local image location
+     * @param userName username under the private namespace i.e xxx@photark.com
+     * @param label optional, a label to the tag
+     */
     public void trainUrlImage(String imagePath, String userName, String label) ;
 
+    /**
+     * Train the face.com index with the given picture
+     * @param imagePath URL image location
+     * @param userName userName username under the private namespace i.e xxx@photark.com
+     * @param label optional, a label to the tag
+     */
     public void trainLocalImage(String imagePath, String userName, String label);
 
+    /**
+     * Recognize a given one or more private namespace trained friends
+     * @param pathToFile  local image path
+     * @param uids  private namespace user ids whom should be recognized.
+     * @param photarkUid  current photark logged in user id
+     * @return
+     */
     public Entry<String, String[]>[] getAllMyFriendsFromPictureLocal(String pathToFile, String uids, String photarkUid);
 
+    /**
+     * Recognize a given one or more private namespace trained friends  
+     * @param fileUrl URL image path
+     * @param uids   private namespace user ids whom should be recognized.
+     * @param photarkUid   current photark logged in user id
+     * @return
+     */
     public Entry<String, String[]>[] getAllMyFriendsFromPictureUrl(String fileUrl,String uids, String photarkUid);
 
 }
diff --git a/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/generic/GenericFriendFinderImpl.java b/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/generic/GenericFriendFinderImpl.java
index a955afe..926a56f 100644
--- a/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/generic/GenericFriendFinderImpl.java
+++ b/photark-face-recognition/src/main/java/org/apache/photark/face/services/applications/generic/GenericFriendFinderImpl.java
@@ -57,11 +57,6 @@
         this.accessManager = accessManager;
     }
 
-    public String check() {
-        return "VVV";  //To change body of implemented methods use File | Settings | File Templates.
-    }
-
-
     public Entry<String, String[]>[] getAllMyFriendsFromPictureLocal(String pathToFile, String uids, String photarkUid) {
         return processMyFriends(pathToFile, uids, true, photarkUid);
     }
@@ -88,12 +83,15 @@
                 String confidence = "";
                 String gender = "";
                 if (face.getGuess() != null) {
-                    System.out.println("***Identified*** " + face.getGuess().toString());
+//                    System.out.println("***Identified*** " + face.getGuess().toString());
                     userName = face.getGuess().getGuessID();
                     confidence = face.getGuess().getConfidence();
                     gender = face.getGender();
-                    detectedFriends.add(new Entry<String, String[]>(userName, new String[]{userName, confidence, gender}));
-                } else {
+                    System.out.println("***Identified*** " + userName+" : "+" : "+gender+" : "+confidence);
+                    if(userName != null) {
+                    detectedFriends.add(new Entry<String, String[]>(userName, new String[]{userName, gender, confidence}));
+                    }
+                    } else {
                     System.out.println("??? Unidentified ..");
                 }
             }
diff --git a/photark-ui-admin/src/main/webapp/admin/js/face.js b/photark-ui-admin/src/main/webapp/admin/js/face.js
index 320628a..d9d5943 100644
--- a/photark-ui-admin/src/main/webapp/admin/js/face.js
+++ b/photark-ui-admin/src/main/webapp/admin/js/face.js
@@ -139,27 +139,19 @@
     });
 }
 
-function face_callback(items, exception) {
-    if (exception) {
-        alert("Error");
-    }
-
-    alert(items);
-}
-
 function facebook_ff_void_callback(items, exception) {
     if (exception) {
-        alert("Error");
+        alert("Error while Training the user..Please try again...");
     } else {
-        //      alert("CAME");
+        alert("Successfully Trained...!!");
     }
 }
 
 function facebook_gff_void_callback(items, exception) {
     if (exception) {
-        alert("Error");
+        alert("Error while Training the user..Please try again...");
     } else {
-          alert("GFF CAME");
+          alert("Successfully Trained...!!");
     }
 }
 
diff --git a/photark-ui/src/main/webapp/gallery.html b/photark-ui/src/main/webapp/gallery.html
index c543f56..b4343b0 100644
--- a/photark-ui/src/main/webapp/gallery.html
+++ b/photark-ui/src/main/webapp/gallery.html
@@ -115,7 +115,7 @@
     	 			<input id="addtag-input" style="width:20;" type="text" style="display:inline;" value="" size="20" alt="add tag..."/><input type="button" onclick="addTag();" style="display:inline;" value="Add Tag"/><input type="button" onclick="showFriends();" style="display:inline;" value="Recognize Friends"/>From
                               <select  id="faceAppType" name="faceAppType" >
                                 <option value="facebook">Facebook</option>
-                                <option value="">Private NameSpace</option>
+                                <option value="private">Private NameSpace</option>
                               </select>
 
                      <table id='faceTable' width="300" border="0" cellspacing="0" cellpadding="1">
diff --git a/photark-ui/src/main/webapp/js/gallery.js b/photark-ui/src/main/webapp/js/gallery.js
index 7a83d21..fa63113 100644
--- a/photark-ui/src/main/webapp/js/gallery.js
+++ b/photark-ui/src/main/webapp/js/gallery.js
@@ -70,6 +70,7 @@
 var albumImageToBeLoaded = null;
 
 var facebook_ff;
+var genericFaceService;
 
 dojo.addOnLoad(function() {
     dojo.require("dojo._base.xhr");
@@ -108,6 +109,8 @@
     remoteGallery = new dojo.rpc.JsonService(photark.constants.RemoteGalleryServiceEndpoint);
     faceService = new dojo.rpc.JsonService(photark.constants.FaceRecognitionService);
     facebook_ff = new dojo.rpc.JsonService(photark.constants.FacebookFriendFinder);
+    genericFaceService = new dojo.rpc.JsonService(photark.constants.GenericFriendFinder);
+
 }
 
 function initGallery() {
@@ -302,15 +305,32 @@
     if (facetype == "facebook") {
         showFacebookFriends();
     } else if (facetype == "private") {
-
-
+       showGenericFriends(textField.value);
     }
 
 
 }
 
-function showFacebookFriends() {
+function showGenericFriends(userName) {
+//  var file_path =  document.getElementById("albumImage").src;
+    var file_path = "https://lh4.googleusercontent.com/-rb_m-GQcL00/Ti8sqThvrDI/AAAAAAAAAMY/kUBurbFKJ0A/s640/friends_2.jpg";
 
+    dojo.xhrPost({
+        url:"security", //photark.constants.SecurityEndpoint,
+        content:{request:"getUser"},
+        handleAs: "json",
+        load: function(response, ioArgs) {
+            genericFaceService.getAllMyFriendsFromPictureUrl(file_path, userName, response.user.userId).addCallback(facebook_gff_callback);
+        },
+        error: function(response, ioArgs) {
+
+        }
+    });
+
+}
+
+function showFacebookFriends() {
+//  var file_path =  document.getElementById("albumImage").src;
     var file_path = "https://lh4.googleusercontent.com/-rb_m-GQcL00/Ti8sqThvrDI/AAAAAAAAAMY/kUBurbFKJ0A/s640/friends_2.jpg";
 
     dojo.xhrPost({
@@ -336,9 +356,11 @@
 
 }
 
-function face_callback1(entries, exception) {
+function facebook_gff_callback(entries, exception) {
     if (exception) {
         alert("FB AUTH Error");
+    } else {
+        viewFaceResults(entries,"gff"); // gff refers to Generic Friend Finder
     }
 
 
@@ -349,20 +371,12 @@
     if (exception) {
         alert("Error");
     } else {
-        viewFaceResults(entries);
-        //    for (var i=0; i<entries.length; i++) {
-        //        var user_data = entries[i];
-        //        var name = entries[i].data[0];
-        //        var link = entries[i].data[1];
-        //        var confidence = entries[i].data[2];
-        //    // TODO Display these data in a table
-        //        alert(name+ ":: "+link+":: "+confidence);
-        //    }
+        viewFaceResults(entries,"fff"); // fff refers to Facebook Friend Finder
     }
 
 }
 
-function viewFaceResults(entries) {
+function viewFaceResults(entries,faceAppType) {
 
     var table = document.getElementById('faceTable');
     var lastRow = 0;
@@ -376,6 +390,7 @@
     column = row.insertCell(2);column.width = 400;
     column.innerHTML = "<span style=\"color:#336633\">"+"Confidence"+"</span>";
 
+    if(faceAppType == "fff") {
     for (var i = 0; i < entries.length; i++) {
         var row = table.insertRow(i+1);
         var user_data = entries[i];
@@ -393,6 +408,25 @@
         column = row.insertCell(2);
         column.innerHTML =  "<span style=\"color:green\">"+confidence+" %"+"</span>";
     }
+    } else  if(faceAppType == "gff") {
+    for (var i = 0; i < entries.length; i++) {
+        var row = table.insertRow(i+1);
+        var user_data = entries[i];
+        var uname = entries[i].data[0];
+        var gender = entries[i].data[1];
+        var confidence = entries[i].data[2];
+
+        column = row.insertCell(0);
+        column.innerHTML = "<span style=\"color:green\">"+uname+"</span>";
+
+        column = row.insertCell(1);
+        column.innerHTML = "<span style=\"color:green\">"+gender+"</span>";
+
+        column = row.insertCell(2);
+        column.innerHTML =  "<span style=\"color:green\">"+confidence+" %"+"</span>";
+    }
+    }
+
 }