Project

General

Profile

1
/*
2
 * Copyright 2006 Google Inc.
3
 * 
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
 * use this file except in compliance with the License. You may obtain a copy of
6
 * the License at
7
 * 
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 * 
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 * License for the specific language governing permissions and limitations under
14
 * the License.
15
 */
16
package org.kepler.web.client;
17

    
18
import com.google.gwt.core.client.GWT;
19
import com.google.gwt.user.client.Command;
20
import com.google.gwt.user.client.DeferredCommand;
21
import com.google.gwt.user.client.rpc.AsyncCallback;
22
import com.google.gwt.user.client.rpc.ServiceDefTarget;
23
import com.google.gwt.user.client.ui.Composite;
24

    
25
public class MetacatResultsWidget extends Composite {
26
  
27
  
28
  private final MetacatProvider metacatProvider = new MetacatProvider();
29
  private final DynaTableWidget dynaTable;
30
  private Command pendingRefresh;
31
  private boolean queryPerformed = false;
32
  private String lastSearch = null;
33

    
34
  public class MetacatProvider implements DynaTableDataProvider {
35

    
36
    public MetacatProvider() {
37
      // Initialize the service.
38
      //
39
      keplerService = (KeplerServiceAsync) GWT.create(KeplerService.class);
40

    
41
      // By default, we assume we'll make RPCs to a servlet, but see
42
      // updateRowData(). There is special support for canned RPC responses.
43
      // (Which is a totally demo hack, by the way :-)
44
      // 
45
      ServiceDefTarget target = (ServiceDefTarget) keplerService;
46
      
47
      // Use a module-relative URLs to ensure that this client code can find 
48
      // its way home, even when the URL changes (as might happen when you 
49
      // deploy this as a webapp under an external servlet container). 
50

    
51
      //String moduleRelativeURL = GWT.getModuleBaseURL() + "kepler";      
52
      //String moduleRelativeURL = "http://library.kepler-project.org/kepler/gwt";
53
      //String moduleRelativeURL = "http://offhegoes.kicks-ass.net:8080/kepler/gwt";
54
      String moduleRelativeURL = "http://library.kepler-project.org/kepler/gwt";
55
      System.out.println("connection url: " + moduleRelativeURL);
56
      target.setServiceEntryPoint(moduleRelativeURL);
57
    }
58

    
59
    public void updateRowData(final int startRow, final int maxRows,
60
        final RowDataAcceptor acceptor, String searchTerm) 
61
    {
62
      // Check the simple cache first.
63
      if(searchTerm == null)
64
      {
65
        searchTerm = "%";
66
      }
67
      
68
      if(queryPerformed && lastSearch.trim().equals(searchTerm.trim()))
69
      {
70
          // Use the cached batch.
71
          pushResults(acceptor, startRow, maxRows, lastResult);
72
          return;
73
      }
74
      else
75
      {
76
        lastSearch = searchTerm;
77
      }
78

    
79
      /*keplerService.login("uid=kepler,o=unaffiliated,dc=ecoinformatics,dc=org", 
80
        new AsyncCallback() {
81
          public void onFailure(Throwable caught) {
82
          acceptor.failed(caught);
83
        }
84

    
85
        public void onSuccess(Object result) {
86
          //handle successful login
87
        }
88
      });*/
89
      
90
      //do the query
91
      if(searchTerm == null)
92
      {
93
        lastSearch = "%";
94
      }
95
      
96
      keplerService.query(lastSearch, "", new AsyncCallback() {
97
        public void onFailure(Throwable caught) {
98
          acceptor.failed(caught);
99
        }
100

    
101
        public void onSuccess(Object result) {
102
          //handle successful query
103
          MetacatQueryResult[] queryResult = (MetacatQueryResult[])result;
104
          lastStartRow = startRow;
105
          lastMaxRows = maxRows;
106
          lastResult = queryResult;
107
          queryPerformed = true;
108
          dynaTable.setMaxRowCount(queryResult.length);
109
          pushResults(acceptor, startRow, maxRows, queryResult);
110
        }
111

    
112
      });
113

    
114
    }
115

    
116
    private void pushResults(RowDataAcceptor acceptor, int startRow,
117
        int maxRows, MetacatQueryResult[] queryResults) {
118
      int offset = (queryResults.length - 0) - startRow;
119
      if(offset > maxRows)
120
      {
121
        offset = maxRows;
122
      }
123
      
124
      String[][] rows = new String[offset][];
125
      for (int i = 0; i < offset; i++) {
126
        MetacatQueryResult result = queryResults[startRow + i];
127
        rows[i] = new String[3];
128
        rows[i][0] = " ";
129
        rows[i][1] = result.getName();
130
        rows[i][2] = result.getDocid();
131
      }
132
      acceptor.accept(startRow, rows);
133
    }
134

    
135
    private final KeplerServiceAsync keplerService;
136
    private int lastMaxRows = -1;
137
    private MetacatQueryResult[] lastResult;
138
    private int lastStartRow = -1;
139
  }
140

    
141
  public MetacatResultsWidget(int visibleRows) {
142
    String[] columns = new String[]{"", "Name", "DocId"};
143
    String[] styles = new String[]{"", "name", "docid"};
144
    dynaTable = new DynaTableWidget(metacatProvider, columns, styles, visibleRows);
145
    initWidget(dynaTable);
146
  }
147

    
148
  protected void onLoad() {
149
    dynaTable.refresh();
150
  }
151
}
(7-7/7)