Adjust some command to query the Gerrit server

Change-Id: I021f013deac6f38f9986553325ab471c1249499c
Signed-off-by: jbouthillie <lmcbout@gmail.com>
diff --git a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/all/AllAbandonnedReviewsHandler.java b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/all/AllAbandonnedReviewsHandler.java
index 365d7e6..4c0e858 100644
--- a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/all/AllAbandonnedReviewsHandler.java
+++ b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/all/AllAbandonnedReviewsHandler.java
@@ -18,9 +18,9 @@
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.commands.common.NotDefinedException;
+import org.eclipse.mylyn.internal.gerrit.core.GerritQuery;
 import org.eclipse.mylyn.reviews.r4e_gerrit.ui.R4EGerritUi;
-import org.eclipse.mylyn.reviews.r4e_gerrit.ui.internal.utils.UIUtils;
+import org.eclipse.mylyn.reviews.r4egerrit.ui.views.R4EGerritTableView;
 
 /**
  * @author Jacques Bouthillier
@@ -35,15 +35,17 @@
 	 */
 	@Override
 	public Object execute(ExecutionEvent aEvent) throws ExecutionException {
-		// TODO Auto-generated method stub
 		R4EGerritUi.Ftracer.traceInfo("Search the Gerrit reviews for All Abandonned  " ); //$NON-NLS-1$
-		try {
-			UIUtils.notInplementedDialog(aEvent.getCommand().getName());
-		} catch (NotDefinedException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
-		return null;
+
+		R4EGerritTableView reviewTableView = R4EGerritTableView
+				.getActiveView();
+
+		// see http://gerrit-documentation.googlecode.com/svn/Documentation/2.5.2/user-search.html
+		//for All > AbandonedOpen--> status:abandoned
+		R4EGerritUi.Ftracer.traceInfo("Execute command :   "  + GerritQuery.ALL_ABANDONED_CHANGES ); //$NON-NLS-1$
+		reviewTableView.processCommands(GerritQuery.ALL_ABANDONED_CHANGES);
+
+		return null;	
 	}
 
 }
diff --git a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/all/AllMergedReviewsHandler.java b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/all/AllMergedReviewsHandler.java
index b8a5a64..3c27ae6 100644
--- a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/all/AllMergedReviewsHandler.java
+++ b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/all/AllMergedReviewsHandler.java
@@ -18,9 +18,9 @@
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.commands.common.NotDefinedException;
+import org.eclipse.mylyn.internal.gerrit.core.GerritQuery;
 import org.eclipse.mylyn.reviews.r4e_gerrit.ui.R4EGerritUi;
-import org.eclipse.mylyn.reviews.r4e_gerrit.ui.internal.utils.UIUtils;
+import org.eclipse.mylyn.reviews.r4egerrit.ui.views.R4EGerritTableView;
 
 /**
  * @author Jacques Bouthillier
@@ -35,15 +35,17 @@
 	 */
 	@Override
 	public Object execute(ExecutionEvent aEvent) throws ExecutionException {
-		// TODO Auto-generated method stub
+
 		R4EGerritUi.Ftracer.traceInfo("Search the Gerrit reviews for All Merged  " ); //$NON-NLS-1$
-		try {
-			UIUtils.notInplementedDialog(aEvent.getCommand().getName());
-		} catch (NotDefinedException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
-		return null;
+		R4EGerritTableView reviewTableView = R4EGerritTableView
+				.getActiveView();
+
+		// see http://gerrit-documentation.googlecode.com/svn/Documentation/2.5.2/user-search.html
+		//for All > AbandonedOpen--> status:merged
+		R4EGerritUi.Ftracer.traceInfo("Execute command :   "  + GerritQuery.ALL_MERGED_CHANGES ); //$NON-NLS-1$
+		reviewTableView.processCommands(GerritQuery.ALL_MERGED_CHANGES);
+
+		return null;	
 	}
 
 }
diff --git a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/my/MyDraftCommentsReviewsHandler.java b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/my/MyDraftCommentsReviewsHandler.java
index df5e3fe..e33f211 100644
--- a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/my/MyDraftCommentsReviewsHandler.java
+++ b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/my/MyDraftCommentsReviewsHandler.java
@@ -18,10 +18,8 @@
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.commands.common.NotDefinedException;
 import org.eclipse.mylyn.internal.gerrit.core.GerritQuery;
 import org.eclipse.mylyn.reviews.r4e_gerrit.ui.R4EGerritUi;
-import org.eclipse.mylyn.reviews.r4e_gerrit.ui.internal.utils.UIUtils;
 import org.eclipse.mylyn.reviews.r4egerrit.ui.views.R4EGerritTableView;
 
 /**
@@ -38,23 +36,14 @@
 	@Override
 	public Object execute(ExecutionEvent aEvent) throws ExecutionException {
 		R4EGerritUi.Ftracer.traceInfo("Search the Gerrit reviews for My Drafts Comments  " ); //$NON-NLS-1$
-//
-//		R4EGerritUi.Ftracer.traceInfo("Execute:   "  ); //$NON-NLS-1$
-//		try {
-//			UIUtils.notInplementedDialog(aEvent.getCommand().getName());
-//		} catch (NotDefinedException e) {
-//			// TODO Auto-generated catch block
-//			e.printStackTrace();
-//		}
 		R4EGerritTableView reviewTableView = R4EGerritTableView
 				.getActiveView();
 
 		// see http://gerrit-documentation.googlecode.com/svn/Documentation/2.5.2/user-search.html
 		//for MY > Drafts Comments--> has:draft
 		R4EGerritUi.Ftracer.traceInfo("Execute command :   "  +  "My > Draft Comments"); //$NON-NLS-1$
-		reviewTableView.processCommands("has:draft");
-	
-		 
+		reviewTableView.processCommands(GerritQuery.MY_DRAFTS_COMMENTS_CHANGES);
+	 
 		return null;
 	}
 
diff --git a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/my/MyDraftsReviewsHandler.java b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/my/MyDraftsReviewsHandler.java
index 58a198c..db93feb 100644
--- a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/my/MyDraftsReviewsHandler.java
+++ b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/my/MyDraftsReviewsHandler.java
@@ -18,9 +18,8 @@
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.commands.common.NotDefinedException;
+import org.eclipse.mylyn.internal.gerrit.core.GerritQuery;
 import org.eclipse.mylyn.reviews.r4e_gerrit.ui.R4EGerritUi;
-import org.eclipse.mylyn.reviews.r4e_gerrit.ui.internal.utils.UIUtils;
 import org.eclipse.mylyn.reviews.r4egerrit.ui.views.R4EGerritTableView;
 
 /**
@@ -44,16 +43,8 @@
 		// see http://gerrit-documentation.googlecode.com/svn/Documentation/2.5.2/user-search.html
 		//for MY > Drafts --> is:draft
 		R4EGerritUi.Ftracer.traceInfo("Execute command :   "  +  "My > Drafts"); //$NON-NLS-1$
-		reviewTableView.processCommands("is:draft");
+		reviewTableView.processCommands(GerritQuery.MY_DRAFTS_CHANGES);
 
-//		R4EGerritUi.Ftracer.traceInfo("Execute:   "  ); //$NON-NLS-1$
-//		try {
-//			UIUtils.notInplementedDialog(aEvent.getCommand().getName());
-//		} catch (NotDefinedException e) {
-//			// TODO Auto-generated catch block
-//			e.printStackTrace();
-//		}
-		
 		return null;
 	}
 
diff --git a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/my/MyStarredChangesReviewsHandler.java b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/my/MyStarredChangesReviewsHandler.java
index 71cf7c2..49c0ba4 100644
--- a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/my/MyStarredChangesReviewsHandler.java
+++ b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/my/MyStarredChangesReviewsHandler.java
@@ -18,6 +18,7 @@
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.mylyn.internal.gerrit.core.GerritQuery;
 import org.eclipse.mylyn.reviews.r4e_gerrit.ui.R4EGerritUi;
 import org.eclipse.mylyn.reviews.r4egerrit.ui.views.R4EGerritTableView;
 
@@ -42,16 +43,9 @@
 		// see http://gerrit-documentation.googlecode.com/svn/Documentation/2.5.2/user-search.html
 		//for My > Starred Changess--> is:starred
 		R4EGerritUi.Ftracer.traceInfo("Execute command :   "  +  "My > Starred Changes"); //$NON-NLS-1$
-		reviewTableView.processCommands("is:starred");
-
-//		R4EGerritUi.Ftracer.traceInfo("Execute:   "  ); //$NON-NLS-1$
-//		try {
-//			UIUtils.notInplementedDialog(aEvent.getCommand().getName());
-//		} catch (NotDefinedException e) {
-//			// TODO Auto-generated catch block
-//			e.printStackTrace();
-//		}
 		
+		reviewTableView.processCommands(GerritQuery.MY_STARRED_CHANGES);
+
 		return null;
 	}
 
diff --git a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/projects/ProjectListHandler.java b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/projects/ProjectListHandler.java
index 13c3c85..2ba4305 100644
--- a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/projects/ProjectListHandler.java
+++ b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4e_gerrit/ui/internal/commands/projects/ProjectListHandler.java
@@ -19,8 +19,10 @@
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.commands.common.NotDefinedException;
+import org.eclipse.mylyn.internal.gerrit.core.GerritQuery;
 import org.eclipse.mylyn.reviews.r4e_gerrit.ui.R4EGerritUi;
 import org.eclipse.mylyn.reviews.r4e_gerrit.ui.internal.utils.UIUtils;
+import org.eclipse.mylyn.reviews.r4egerrit.ui.views.R4EGerritTableView;
 
 /**
  * @author Jacques Bouthillier
@@ -35,17 +37,25 @@
 	 */
 	@Override
 	public Object execute(ExecutionEvent aEvent) throws ExecutionException {
-		// TODO Auto-generated method stub
 		R4EGerritUi.Ftracer.traceInfo("Search the Gerrit reviews for Projects List  " ); //$NON-NLS-1$
 
 		R4EGerritUi.Ftracer.traceInfo("Execute:  ProjectList() "  ); //$NON-NLS-1$
-		try {
-			UIUtils.notInplementedDialog(aEvent.getCommand().getName());
-		} catch (NotDefinedException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
+//		try {
+//			UIUtils.notInplementedDialog(aEvent.getCommand().getName());
+//		} catch (NotDefinedException e) {
+//			// TODO Auto-generated catch block
+//			e.printStackTrace();
+//		}
+//		return null;
+		R4EGerritTableView reviewTableView = R4EGerritTableView
+				.getActiveView();
+
+		// see http://gerrit-documentation.googlecode.com/svn/Documentation/2.5.2/user-search.html
+		//for Project list
+		reviewTableView.processCommands(GerritQuery.OPEN_CHANGES_BY_PROJECT);
+	 
 		return null;
+
 	}
 
 }
diff --git a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4egerrit/ui/views/R4EGerritTableView.java b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4egerrit/ui/views/R4EGerritTableView.java
index 09d3361..70b7b6d 100644
--- a/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4egerrit/ui/views/R4EGerritTableView.java
+++ b/org.eclipse.mylyn.reviews.r4e-gerrit.ui/src/org/eclipse/mylyn/reviews/r4egerrit/ui/views/R4EGerritTableView.java
@@ -319,7 +319,7 @@
 
 			@Override
 			public void handleEvent(Event event) {
-				UIUtils.notInplementedDialog("Search Button");
+				processCommands(GerritQuery.CUSTOM);
 			}});
 
 	}
@@ -546,10 +546,10 @@
 	
 	/**
 	 * @param aTaskRepo
-	 * @param aQuery
+	 * @param aQueryType
 	 * @return
 	 */
-	private Object updateTable(final TaskRepository aTaskRepo, final String aQuery)  {
+	private Object updateTable(final TaskRepository aTaskRepo, final String aQueryType)  {
 		
 		final Job job = new Job(COMMAND_MESSAGE) {
 
@@ -565,18 +565,20 @@
 				aMonitor.beginTask(COMMAND_MESSAGE, IProgressMonitor.UNKNOWN);
 						
 				R4EGerritPlugin.Ftracer.traceInfo("repository:   " + aTaskRepo.getUrl() +
-						"\t query: " + aQuery); //$NON-NLS-1$
+						"\t query: " + aQueryType); //$NON-NLS-1$
 				
 				// If there is only have one Gerrit server, we can proceed as if it was already used before
 				IStatus status = null;
 				try {
-	                fReviewTable.createReviewItem(aQuery, aTaskRepo);
-	                getReviews(aTaskRepo, aQuery);
+	                fReviewTable.createReviewItem(aQueryType, aTaskRepo);
+	                getReviews(aTaskRepo, aQueryType);
 	                Display.getDefault().syncExec(new Runnable() {
 	                    @Override
 	                    public void run() {
-	                        setSearchLabel(aQuery);
-	                        setSearchText(aQuery);
+	                        setSearchLabel(aQueryType);
+	                        if (aQueryType != GerritQuery.CUSTOM ) {
+		                        setSearchText(aQueryType);	                        	
+	                        }
 	                        setRepositoryLabel(aTaskRepo.getRepositoryLabel());
 	                    }
 	                });
@@ -610,6 +612,21 @@
 		}
 	}
 	
+	private String getSearchText () {
+		if (!fSearchRequestText.isDisposed() ) {
+			final String[] str  = new String[1];
+			Display.getDefault().syncExec(new Runnable() {
+				public void run() {
+					str[0] = fSearchRequestText.getText().trim();
+					R4EGerritUi.Ftracer.traceInfo( "Custom string: " + str[0]);
+				}
+			});
+			return str[0];
+		}
+		return null;
+	}
+	
+
 	private void setRepositoryLabel(String aSt) {
 		if (!fRepositoryResulLabel.isDisposed() ) {
 			fRepositoryResulLabel.setText(aSt);
@@ -636,6 +653,13 @@
     	// Retrieve the query from the repository (if previously defined)
     	Set<RepositoryQuery> queries = TasksUiInternal.getTaskList().getQueries();
     	RepositoryQuery query = null;
+
+		//Look to a regular query, otherwise, need to create the proper custom query
+    	if (queryType == GerritQuery.CUSTOM ) {
+    		//For Custom query, need to get the extra data
+    		queryId = rtv.getTitle() + " - " + queryType + "- " + getSearchText();
+    	}
+    	
     	for (RepositoryQuery rquery : queries) {
     		if (rquery.getRepositoryUrl().equals(repository.getRepositoryUrl()) && rquery.getSummary().equals(queryId)) {
     			query = rquery;
@@ -650,7 +674,11 @@
         	query.setSummary(queryId);
             query.setAttribute(GerritQuery.TYPE, queryType);
     		query.setAttribute(GerritQuery.PROJECT, null);
-    		query.setAttribute(GerritQuery.QUERY_STRING, null);
+    		if (queryType == GerritQuery.CUSTOM ) {
+        		query.setAttribute(GerritQuery.QUERY_STRING, getSearchText());
+    		} else {
+        		query.setAttribute(GerritQuery.QUERY_STRING, null);    			
+    		}
     		TasksUiPlugin.getTaskList().addQuery(query);
     	}
 
@@ -658,7 +686,8 @@
     	fCurrentQuery = query;
     	
     	// Fetch the list of reviews and pre-populate the table
-        R4EGerritTask[] reviews = getReviewList(repository, queryType);
+        R4EGerritTask[] reviews = getReviewList(repository, query);
+        
         fReviewTable.init(reviews);
         refresh();
     	
@@ -675,16 +704,11 @@
 		}
     }
 	
-    private R4EGerritTask[] getReviewList(TaskRepository repository, String queryType) throws R4EQueryException {
-
-        // Format the query
-        IRepositoryQuery query = new RepositoryQuery(repository.getConnectorKind(), "query"); //$NON-NLS-1$
-        query.setAttribute(GerritQuery.TYPE, queryType);
-
+    private R4EGerritTask[] getReviewList(TaskRepository repository, RepositoryQuery aQuery) throws R4EQueryException {
+    	
         // Execute the query
-//        GerritConnector connector = GerritCorePlugin.getDefault().getConnector();
         R4EGerritTaskDataCollector resultCollector = new R4EGerritTaskDataCollector();
-        IStatus status = fConnector.performQuery(repository, query, resultCollector, null, new NullProgressMonitor());
+        IStatus status = fConnector.performQuery(repository, aQuery, resultCollector, null, new NullProgressMonitor());
         if (!status.isOK()) {
             String msg = "Unable to read the Gerrit server.";
             throw new R4EQueryException(status, msg);
@@ -695,9 +719,11 @@
         List<TaskData> tasksData = resultCollector.getResults();
         for (TaskData taskData : tasksData) {
             R4EGerritTask review = new R4EGerritTask(taskData);
-            if (review.getAttribute(R4EGerritTask.DATE_COMPLETION) == null) {
+//            R4EGerritUi.Ftracer.traceInfo("Review: " + review.getAttribute(R4EGerritTask.TASK_ID) +
+//           		"\t Completion date: " + review.getAttribute(R4EGerritTask.DATE_COMPLETION));
+//            if (review.getAttribute(R4EGerritTask.DATE_COMPLETION) == null) {
                 reviews.add(review);
-            }
+ //           }
         }
         return reviews.toArray(new R4EGerritTask[0]);
     }