workspace-sharing-widget/src/main/java/org/gcube/portlets/widgets/workspacesharingwidget/client/view/sharing/multisuggest/ServerMultiWordSuggestOracl...

91 lines
2.8 KiB
Java

package org.gcube.portlets.widgets.workspacesharingwidget.client.view.sharing.multisuggest;
import java.util.ArrayList;
import java.util.List;
import org.gcube.portlets.widgets.workspacesharingwidget.client.rpc.WorkspaceSharingServiceAsync;
import org.gcube.portlets.widgets.workspacesharingwidget.shared.InfoContactModel;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
/**
*
* @author Giancarlo Panichi
*
*/
public class ServerMultiWordSuggestOracle extends MultiWordSuggestOracle {
private static final int DEFAULT_QUERY_LENGHT = 1;
//private int startQueryLength;
private ArrayList<InfoContactModelSuggestion> suggestions = new ArrayList<>();
//private boolean isMoreSuggestions = false;
//private int previousQueryLength = 0;
public ServerMultiWordSuggestOracle() {
this(DEFAULT_QUERY_LENGHT);
}
public ServerMultiWordSuggestOracle(int startQueryLength) {
super();
//this.startQueryLength = startQueryLength;
}
@Override
public void requestSuggestions(final Request request, final Callback callback) {
// start the backend call only if the user types in more than
// startQueryLength characters.
//if (request.getQuery().length() < startQueryLength)
// return;
// if the user expands the search or a search hasn't been carried out,
// call the backend. Otherwise use the existing list
//if (isMoreSuggestions || previousQueryLength > request.getQuery().length() || suggestions.size() == 0) {
String keyword = request.getQuery();
keyword = keyword.toLowerCase();
WorkspaceSharingServiceAsync.INSTANCE.getUsersByKeyword(keyword,
new AsyncCallback<List<InfoContactModel>>() {
@Override
public void onSuccess(List<InfoContactModel> result) {
suggestions.clear();
for (int i = 0; i < result.size(); i++) {
InfoContactModelSuggestion suggestion = new InfoContactModelSuggestion(result.get(i));
suggestions.add(suggestion);
}
MultiWordSuggestOracle.Response response = new MultiWordSuggestOracle.Response(suggestions);
// isMoreSuggestions = isMore;
/*if (result == null || result.isEmpty()) {
response.setMoreSuggestions(false);
} else {
int count = result.size() - request.getLimit();
if (count > 0) {
response.setMoreSuggestionsCount(count);
} else {
response.setMoreSuggestions(false);
}
}*/
//previousQueryLength = request.getQuery().length();
callback.onSuggestionsReady(request, response);
}
@Override
public void onFailure(Throwable caught) {
GWT.log("Error retrieving users in ServerMultiWordSuggest", caught);
}
});
//} else {
// super.requestSuggestions(request, callback);
//}
}
}