You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by jo...@apache.org on 2011/11/02 13:51:14 UTC

svn commit: r1196558 - in /incubator/opennlp/sandbox/caseditor-corpus-server-plugin/src/main/java/org/apache/opennlp/corpus_server/caseditor: CorpusExplorerView.java SearchCorpusServerJob.java

Author: joern
Date: Wed Nov  2 12:51:14 2011
New Revision: 1196558

URL: http://svn.apache.org/viewvc?rev=1196558&view=rev
Log:
OPENNLP-350 Corpus Explorer should not query server in the UI thread

Added:
    incubator/opennlp/sandbox/caseditor-corpus-server-plugin/src/main/java/org/apache/opennlp/corpus_server/caseditor/SearchCorpusServerJob.java   (with props)
Modified:
    incubator/opennlp/sandbox/caseditor-corpus-server-plugin/src/main/java/org/apache/opennlp/corpus_server/caseditor/CorpusExplorerView.java

Modified: incubator/opennlp/sandbox/caseditor-corpus-server-plugin/src/main/java/org/apache/opennlp/corpus_server/caseditor/CorpusExplorerView.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/sandbox/caseditor-corpus-server-plugin/src/main/java/org/apache/opennlp/corpus_server/caseditor/CorpusExplorerView.java?rev=1196558&r1=1196557&r2=1196558&view=diff
==============================================================================
--- incubator/opennlp/sandbox/caseditor-corpus-server-plugin/src/main/java/org/apache/opennlp/corpus_server/caseditor/CorpusExplorerView.java (original)
+++ incubator/opennlp/sandbox/caseditor-corpus-server-plugin/src/main/java/org/apache/opennlp/corpus_server/caseditor/CorpusExplorerView.java Wed Nov  2 12:51:14 2011
@@ -21,6 +21,8 @@ import javax.ws.rs.core.MediaType;
 
 import org.codehaus.jettison.json.JSONArray;
 import org.codehaus.jettison.json.JSONException;
+import org.eclipse.core.runtime.jobs.IJobChangeEvent;
+import org.eclipse.core.runtime.jobs.JobChangeAdapter;
 import org.eclipse.jface.layout.GridDataFactory;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.viewers.ILabelProviderListener;
@@ -39,21 +41,23 @@ import org.eclipse.swt.layout.GridLayout
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Combo;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.part.PageBook;
 import org.eclipse.ui.part.ViewPart;
 
-import com.sun.jersey.api.client.Client;
-import com.sun.jersey.api.client.ClientResponse;
-import com.sun.jersey.api.client.WebResource;
-
 public class CorpusExplorerView extends ViewPart {
 
   private static final String LUCENE_QUERY_DELIMITER = ":::";
   
+  private PageBook book;
+  
+  private Text messageText;
+  
   private Composite explorerComposite;
   private Text serverUrl;
   private Combo queryText;
@@ -131,6 +135,7 @@ public class CorpusExplorerView extends 
       @Override
       public void widgetSelected(SelectionEvent event) {
         
+        // Remember query and save last queries to the preference store
         int queryIndex = queryText.indexOf(queryText.getText());
         
         if (queryIndex != -1) {
@@ -160,53 +165,66 @@ public class CorpusExplorerView extends 
         // get query
         String query = queryText.getText();
         
-        // execute query
-        Client c = Client.create();
+        final SearchCorpusServerJob searchJob = new SearchCorpusServerJob();
         
-        WebResource r = c.resource(serverPath);
+        searchJob.setServerAddress(serverPath);
+        searchJob.setQuery(query);
         
-        ClientResponse response = r
-                .path("_search")
-                .queryParam("q", query)
-                .accept(MediaType.APPLICATION_JSON)
-                .get(ClientResponse.class);
-        
-        JSONArray searchResult = response.getEntity(JSONArray.class);
-        
-        System.out.println("Status: " + response.getStatus());
-        
-        searchResultViewer.setItemCount(0);
-        
-        for (int i = 0; i < searchResult.length(); i++) {
-            try {
-//              System.out.println("Hit: " + searchResult.getString(i));
-              
-              searchResultViewer.add(searchResult.getString(i));
-              // Write result into table ...
-              
-            } catch (JSONException e) {
-              // TODO Auto-generated catch block
-              e.printStackTrace();
-            }
-        }
+        searchJob.schedule();
+        
+        setMessage("Fetching results ...");
         
-        // add result to list ...
+        searchJob.addJobChangeListener(new JobChangeAdapter(){
+          @Override
+          public void done(final IJobChangeEvent event) {
+            
+            Display.getDefault().asyncExec(new Runnable() {
+
+              @Override
+              public void run() {
+                if (event.getResult().isOK()) {
+                  
+                  setMessage(null);
+                  
+                  searchResultViewer.setItemCount(0);
+                  JSONArray searchResult = searchJob.getSearchResult();
+                  
+                  for (int i = 0; i < searchResult.length(); i++) {
+                    try {
+                      searchResultViewer.add(searchResult.getString(i));
+                    } catch (JSONException e) {
+                      setMessage("Error, failed to parse results.");
+                    }
+                  }
+                }
+                else {
+                  setMessage("Fetching search results from server failed!");
+                }
+              }
+            });
+          }
+        }); 
       }
 
       @Override
       public void widgetDefaultSelected(SelectionEvent event) {
-        // TODO Auto-generated method stub
-
       }
     });
     
+    book = new PageBook(explorerComposite, SWT.NONE);
+    
+    GridDataFactory.swtDefaults().align(SWT.FILL, SWT.FILL).grab(true, true)
+            .span(2, 1).applyTo(book);
+    
+    messageText = new Text(book, SWT.WRAP | SWT.READ_ONLY);
+    messageText.setText("Enter the server address and a query to search a corpus on the Corpus Server.");
+
     // List with casIds in the corpus ... (might be later replaced with a title)
     // The table should later be virtual, and be able to scroll through very huge
     // lits of CASes ... might be connected to a repository with million of documents
-    searchResultViewer = new TableViewer(explorerComposite);
-
+    searchResultViewer = new TableViewer(book);
     GridDataFactory.swtDefaults().align(SWT.FILL, SWT.FILL).grab(true, true)
-        .span(2, 1).applyTo(searchResultViewer.getTable());
+        .span(2, 1).applyTo(searchResultViewer.getControl());
 
     searchResultViewer.setLabelProvider(new ITableLabelProvider() {
 
@@ -259,26 +277,25 @@ public class CorpusExplorerView extends 
         } catch (PartInitException e) {
           e.printStackTrace();
         }
-        
-        // Need to register our content provider, should be possible to have multiple,
-        // selection could be done based on editor input class ...
-        
-        // the editor has multiple content providers, and can choose one based
-        // on the editor input ...
-        // how to add all cas editors to an open with menu?
-        
-        // Document Provider ?!
-        // url based input
       }
     });
-    
-    // Need an action which can take a CAS id, as editor input ...
-    // Write document provider, which can download as CAS, open it, and upload
-    // it on save!
 
-    // Context menu should have open action ...
+    book.showPage(messageText);
+    
+    // TODO: Context menu should have open action
   }
 
+  void setMessage(String message) {
+    if (message != null) {
+      messageText.setText(message);
+      book.showPage(messageText);
+    }
+    else {
+      messageText.setText("");
+      book.showPage(searchResultViewer.getControl());
+    }
+  }
+  
   @Override
   public void setFocus() {
     explorerComposite.setFocus();

Added: incubator/opennlp/sandbox/caseditor-corpus-server-plugin/src/main/java/org/apache/opennlp/corpus_server/caseditor/SearchCorpusServerJob.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/sandbox/caseditor-corpus-server-plugin/src/main/java/org/apache/opennlp/corpus_server/caseditor/SearchCorpusServerJob.java?rev=1196558&view=auto
==============================================================================
--- incubator/opennlp/sandbox/caseditor-corpus-server-plugin/src/main/java/org/apache/opennlp/corpus_server/caseditor/SearchCorpusServerJob.java (added)
+++ incubator/opennlp/sandbox/caseditor-corpus-server-plugin/src/main/java/org/apache/opennlp/corpus_server/caseditor/SearchCorpusServerJob.java Wed Nov  2 12:51:14 2011
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.opennlp.corpus_server.caseditor;
+
+import javax.ws.rs.core.MediaType;
+
+import org.codehaus.jettison.json.JSONArray;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.WebResource;
+
+/**
+ * A job to retrieve search results from the corpus server.
+ */
+public class SearchCorpusServerJob extends Job {
+
+  private String serverAddress;
+  private String searchQuery;
+  private JSONArray searchResult;
+  
+  public SearchCorpusServerJob() {
+    super("Search Job");
+  }
+
+  public void setServerAddress(String serverPath) {
+    serverAddress = serverPath;
+  }
+  
+  public void setQuery(String query) {
+    searchQuery = query;
+  }
+  
+  @Override
+  protected IStatus run(IProgressMonitor monitor) {
+    
+    Client c = Client.create();
+    
+    WebResource r = c.resource(serverAddress);
+    
+    ClientResponse response = r
+        .path("_search")
+        .queryParam("q", searchQuery)
+        .accept(MediaType.APPLICATION_JSON)
+        .get(ClientResponse.class);
+
+    if (response.getClientResponseStatus().getStatusCode() != 200) {
+      return new Status(IStatus.OK, CorpusServerPlugin.PLUGIN_ID, "Failed to retrieve results from server!");
+    }
+    
+    searchResult = response.getEntity(JSONArray.class);
+    
+    return new Status(IStatus.OK, CorpusServerPlugin.PLUGIN_ID, "OK");
+  }
+  
+  JSONArray getSearchResult() {
+    return searchResult;
+  }
+}

Propchange: incubator/opennlp/sandbox/caseditor-corpus-server-plugin/src/main/java/org/apache/opennlp/corpus_server/caseditor/SearchCorpusServerJob.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain