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
      String moduleRelativeURL = GWT.getModuleBaseURL() + "kepler";      
51
      moduleRelativeURL = "http://library.kepler-project.org/kepler/gwt";
52
      target.setServiceEntryPoint(moduleRelativeURL);
53
    }
54

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

    
75
      /*keplerService.login("uid=kepler,o=unaffiliated,dc=ecoinformatics,dc=org", 
76
        new AsyncCallback() {
77
          public void onFailure(Throwable caught) {
78
          acceptor.failed(caught);
79
        }
80

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

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

    
108
      });
109

    
110
    }
111

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

    
131
    private final KeplerServiceAsync keplerService;
132
    private int lastMaxRows = -1;
133
    private MetacatQueryResult[] lastResult;
134
    private int lastStartRow = -1;
135
  }
136

    
137
  public MetacatResultsWidget(int visibleRows) {
138
    String[] columns = new String[]{"", "Name", "DocId"};
139
    String[] styles = new String[]{"", "name", "docid"};
140
    dynaTable = new DynaTableWidget(metacatProvider, columns, styles, visibleRows);
141
    initWidget(dynaTable);
142
  }
143

    
144
  protected void onLoad() {
145
    dynaTable.refresh();
146
  }
147
}
(7-7/7)