urlTrends = new UrlTrendPanel("urltrends") {
@Override
protected void onUrlClick(AjaxRequestTarget target, String name) {
JetwickQuery q;
if (lastQuery != null)
q = lastQuery;
else
q = new TweetQuery(true);
if (name == null) {
q.removeFilterQueries(ElasticTweetSearch.FIRST_URL_TITLE);
} else
q.addFilterQuery(ElasticTweetSearch.FIRST_URL_TITLE, name);
doSearch(q, 0, true);
updateAfterAjax(target, false);
}
@Override
protected void onDirectUrlClick(AjaxRequestTarget target, String name) {
if (lastQuery == null || name == null || name.isEmpty())
return;
TweetQuery q = new TweetQuery(true);
q.addFilterQuery(ElasticTweetSearch.FIRST_URL_TITLE, name);
try {
List<JTweet> tweets = getTweetSearch().collectObjects(getTweetSearch().query(q.setSize(1)));
if (tweets.size() > 0 && tweets.get(0).getUrlEntries().size() > 0) {
// TODO there could be more than 1 url!
UrlEntry entry = tweets.get(0).getUrlEntries().iterator().next();
getRequestCycle().setRequestTarget(new RedirectRequestTarget(entry.getResolvedUrl()));
}
} catch (Exception ex) {
logger.error("Error while executing onDirectUrlClick", ex);
}
}
};
add(urlTrends.setOutputMarkupId(true));
ssPanel = new SavedSearchPanel("savedSearches") {
@Override
public void onClick(AjaxRequestTarget target, long ssId) {
String searchType = parameters.getString("search");
if (searchType != null && !searchType.isEmpty() && !SearchBox.ALL.equals(searchType)) {
warn("Removed user filter when executing your saved search");
searchBox.setSearchType(SearchBox.ALL);
}
JUser user = getMySession().getUser();
SavedSearch ss = user.getSavedSearch(ssId);
if (ss != null) {
doSearch(ss.getQuery(), 0, true);
uindexProvider.get().save(user, true);
}
updateSSCounts(target);
updateAfterAjax(target, true);
}
@Override
public void onRemove(AjaxRequestTarget target, long ssId) {
JUser user = getMySession().getUser();
user.removeSavedSearch(ssId);
uindexProvider.get().save(user, true);
updateSSCounts(target);
}
@Override
public void onSave(AjaxRequestTarget target, long ssId) {
if (lastQuery == null)
return;
SavedSearch ss = new SavedSearch(ssId, lastQuery);
JUser user = getMySession().getUser();
user.addSavedSearch(ss);
uindexProvider.get().save(user, true);
updateSSCounts(target);
}
@Override
public void updateSSCounts(AjaxRequestTarget target) {
try {
JUser user = getMySession().getUser();
if (user != null) {
StopWatch sw = new StopWatch().start();
update(getTweetSearch().updateSavedSearches(user.getSavedSearches()));
if (target != null)
target.addComponent(ssPanel);
logger.info("Updated saved search counts for " + user.getScreenName() + " " + sw.stop().getSeconds());
}
} catch (Exception ex) {
logger.error("Error while searching in savedSearches", ex);
}
}
@Override
public String translate(long id) {
SavedSearch ss = getMySession().getUser().getSavedSearch(id);
return ss.getName();
}
};
add(ssPanel.setOutputMarkupId(true));
add(new UserPanel("userPanel", this) {
@Override
public void onLogout() {
getMySession().logout(uindexProvider.get(), (WebResponse) getResponse(), true);
setResponsePage(TweetSearchPage.class, parameters);
}
@Override
public void updateAfterAjax(AjaxRequestTarget target) {
TweetSearchPage.this.updateAfterAjax(target, false);
}
@Override
public void onHomeline(AjaxRequestTarget target, String user) {
searchBox.setSearchType(SearchBox.FRIENDS);
doSearch(createFriendQuery(""), 0, false);
TweetSearchPage.this.updateAfterAjax(target, true);
}
@Override
public void onShowTweets(AjaxRequestTarget target, String userName) {
searchBox.setSearchType(SearchBox.USER);
doSearch(new TweetQuery(true).addUserFilter(userName).setSort(ElasticTweetSearch.DATE, "desc"), 0, false);
TweetSearchPage.this.updateAfterAjax(target, true);
}
@Override
protected Collection<String> getUserChoices(String input) {
return getTweetSearch().getUserChoices(lastQuery, input);
}
});
tagCloudPanel = new TagCloudPanel("tagcloud") {
@Override
protected void onTagClick(String name) {
if (lastQuery != null) {
lastQuery.setQuery((lastQuery.getQuery() + " " + name).trim());
doSearch(lastQuery, 0, true);
} else {
// never happens?
PageParameters pp = new PageParameters();
pp.add("q", name);
setResponsePage(TweetSearchPage.class, pp);
}
}
@Override
protected void onFindOriginClick(String tag) {
PageParameters pp = new PageParameters();
pp.add("findOrigin", tag);
doSearch(createQuery(pp), 0, true);
// this preserves parameters but cannot be context sensitive!
// setResponsePage(TweetSearchPage.class, pp);
}
};
add(tagCloudPanel.setOutputMarkupId(true));
navigationPanel = new NavigationPanel("navigation", hitsPerPage) {
@Override
public void onPageChange(AjaxRequestTarget target, int page) {
// this does not scroll to top:
// doOldSearch(page);
// updateAfterAjax(target);
doOldSearch(page);
}
};
add(navigationPanel.setOutputMarkupId(true));
facetPanel = new FacetPanel("filterPanel") {
@Override
public void onRemoveAllFilter(AjaxRequestTarget target, String key) {
if (lastQuery != null)
lastQuery.removeFilterQueries(key);
else {
logger.error("last query cannot be null but was! ... when clicking on facets!?");
return;
}
doOldSearch(0);
updateAfterAjax(target, false);
}
@Override
public void onFilterChange(AjaxRequestTarget target, String key, Object val, Boolean selected) {
if (lastQuery != null) {
if (selected == null)
lastQuery.removeFilterQuery(key, val);
else if (selected)
lastQuery.addFilterQuery("-" + key, val);
else
lastQuery.addFilterQuery(key, val);
} else {
logger.error("last query cannot be null but was! ... when clicking on facets!?");
return;
}
doOldSearch(0);
updateAfterAjax(target, false);
}
};
add(facetPanel.setOutputMarkupId(true));
dateFilterPanel = new JSDateFilter("dateFilter") {
@Override
protected void onFilterChange(AjaxRequestTarget target, String filter, Boolean selected) {
if (lastQuery != null) {
if (selected == null) {
lastQuery.removeFilterQueries(filter);
} else if (selected) {
lastQuery.replaceFilterQuery(filter);
} else
lastQuery.reduceFilterQuery(filter);
} else {
logger.error("last query cannot be null but was! ... when clicking on facets!?");
return;
}
doOldSearch(0);
updateAfterAjax(target, false);
}
@Override
protected boolean isAlreadyFiltered(String key, Object val) {
if (lastQuery != null)
return lastQuery.containsFilter(key, val);
return false;
}
@Override
public String getFilterName(String key) {
return facetPanel.getFilterName(key);
}
};
add(dateFilterPanel.setOutputMarkupId(true));
// TODO M2.1
language = getWebRequestCycle().getWebRequest().getHttpServletRequest().getLocale().getLanguage();
remoteHost = getWebRequestCycle().getWebRequest().getHttpServletRequest().getRemoteHost();
resultsPanel = new ResultsPanel("results", language) {
@Override
public void onSortClicked(AjaxRequestTarget target, String sortKey, String sortVal) {
if (lastQuery != null) {
lastQuery.setSort(sortKey, sortVal);
doSearch(lastQuery, 0, false);
updateAfterAjax(target, false);
}
}
@Override
public void onUserClick(String userName, String queryStr) {
PageParameters p = new PageParameters();
if (queryStr != null && !queryStr.isEmpty())
p.add("q", queryStr);
if (userName != null) {
p.add("user", userName.trim());
searchBox.setSearchType(SearchBox.USER);
}
doSearch(createQuery(p), 0, true);
}
@Override
public Collection<JTweet> onTweetClick(long id, boolean retweet) {
logger.info("[stats] search replies of:" + id + " retweet:" + retweet);
return getTweetSearch().searchReplies(id, retweet);
}
@Override
protected void onRetweet(JTweet tweet, AjaxRequestTarget target) {
if (getMySession().hasLoggedIn())
try {
getTwitterSearch().doRetweet(tweet.getTwitterId());
info("Retweeted " + tweet.getFromUser().getScreenName() + " by " + getTwitterSearch().getUser());
} catch (Exception ex) {
error("Cannot retweet " + tweet.getFromUser().getScreenName() + ". "
+ "Problems with twitter. Please try again.");
}
else
error("Please login.");
updateAfterAjax(target, false);
}
@Override
public void onFindSimilar(JTweet tweet, AjaxRequestTarget target) {
JetwickQuery query = new SimilarTweetQuery(tweet, true);
if (tweet.getTextTerms().size() == 0) {
warn("Try a different tweet. This tweet is too short.");
return;
}
logger.info("[stats] similar search:" + query.toString());
doSearch(query, 0, false);
updateAfterAjax(target, false);
}
@Override
public Collection<JTweet> onInReplyOfClick(long id) {
JTweet tw = getTweetSearch().findByTwitterId(id);
logger.info("[stats] search tweet:" + id + " " + tw);
if (tw != null)
return Arrays.asList(tw);
else
return new ArrayList();
}
@Override
public String getTweetsAsString() {
if (lastQuery != null)
return twindexProvider.get().getTweetsAsString(lastQuery, "\t");
return "";
}
@Override
public void onHtmlExport() {
if (lastQuery != null) {
PrinterPage printerPage = new PrinterPage();
List<JTweet> tweets = twindexProvider.get().searchTweets(lastQuery);
printerPage.setResults(tweets);
setResponsePage(printerPage);
}
}
};
add(resultsPanel.setOutputMarkupId(true));
// add(wikiPanel = new WikipediaLazyLoadPanel("wikipanel"));
String searchType = parameters.getString("search");
String tmpUserName = null;
boolean showSpacer = true;
if (getMySession().hasLoggedIn()) {
if (query.getQuery().isEmpty() && userName.isEmpty() && (searchType == null || searchType.isEmpty()))
searchType = SearchBox.FRIENDS;
tmpUserName = getMySession().getUser().getScreenName();
// showSpacer = false;
} else {
ssPanel.setVisible(false);
facetPanel.setVisible(false);
// dateFilterPanel.setVisible(false);
// so that the jetwick link on my twitter account works ;)
if (userName.isEmpty()) {
tagCloudPanel.setVisible(false);
// if (query.getQuery().isEmpty()) {
// resultsPanel.setVisible(false);
// navigationPanel.setVisible(false);
// showSpacer = false;
// }
}
}
dateFilterPanel.setVisible(false);
searchBox = new SearchBox("searchbox", tmpUserName, searchType, showSpacer) {
@Override
protected Collection<String> getQueryChoices(String input) {
return getTweetSearch().getQueryChoices(lastQuery, input);
}
@Override
protected void onSelectionChange(AjaxRequestTarget target, String newValue) {
if (lastQuery == null)
return;
JetwickQuery tmpQ = lastQuery.getCopy().setQuery(newValue);
tmpQ.removeFilterQueries(ElasticTweetSearch.DATE);
doSearch(tmpQ, 0, false, true);
updateAfterAjax(target, false);
}
@Override