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://offhegoes.kicks-ass.net:8080/kepler/gwt";
52
      System.out.println("connection url: " + moduleRelativeURL);
53
      target.setServiceEntryPoint(moduleRelativeURL);
54
    }
55

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

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

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

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

    
109
      });
110

    
111
    }
112

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

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

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

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