diff options
author | Vitaly Takmazov | 2016-06-27 10:39:24 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-06-27 10:39:24 +0300 |
commit | 0d93073c6988a22246db61a59b97c2fd7c9f462b (patch) | |
tree | d5f27cdb9a4fe51690f7af067608a9bee62087e3 /src/main/java/com/juick/http/www/PageTemplates.java | |
parent | 2d7f61242de54b0c0670bac1953e45ee18d9ecec (diff) | |
parent | 53fb0b3e0e790df007a198deef21c115ed236b3e (diff) |
merge
Diffstat (limited to 'src/main/java/com/juick/http/www/PageTemplates.java')
-rw-r--r-- | src/main/java/com/juick/http/www/PageTemplates.java | 75 |
1 files changed, 13 insertions, 62 deletions
diff --git a/src/main/java/com/juick/http/www/PageTemplates.java b/src/main/java/com/juick/http/www/PageTemplates.java index fedb11bf..89b0c737 100644 --- a/src/main/java/com/juick/http/www/PageTemplates.java +++ b/src/main/java/com/juick/http/www/PageTemplates.java @@ -20,6 +20,7 @@ package com.juick.http.www; import com.juick.Message; import com.juick.Tag; import com.juick.server.MessagesQueries; +import com.juick.server.TagQueries; import com.juick.server.UserQueries; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; @@ -35,7 +36,11 @@ import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.util.StringUtils; import ru.sape.Sape; /** @@ -105,13 +110,13 @@ public class PageTemplates { out.println("</header>"); } - public static void pageHomeColumn(PrintWriter out, Connection sql, com.juick.User visitor) { + public static void pageHomeColumn(PrintWriter out, JdbcTemplate sql, com.juick.User visitor) { pageHomeColumn(out, sql, visitor, false); } - public static void pageHomeColumn(PrintWriter out, Connection sql, com.juick.User visitor, boolean showAdv) { + public static void pageHomeColumn(PrintWriter out, JdbcTemplate sql, com.juick.User visitor, boolean showAdv) { if (tagsHTML == null) { - tagsHTML = PageTemplates.getPopularTags(sql, 80); + tagsHTML = PageTemplates.formatPopularTags(sql, 80); } out.println("<aside id=\"column\">"); @@ -126,64 +131,10 @@ public class PageTemplates { out.println("</aside>"); } - public static String getPopularTags(Connection sql, int cnt) { - String ret = ""; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT name FROM tags WHERE top=1 ORDER BY name ASC"); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - if (!ret.isEmpty()) { - ret += " "; - } - try { - ret += "<a href=\"/tag/" + URLEncoder.encode(rs.getString(1), "UTF-8") + "\">" + Utils.encodeHTML(rs.getString(1)) + "</a>"; - } catch (UnsupportedEncodingException e) { - } - - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return ret; - } - - public static void printContestRating(PrintWriter out, Connection sql) { - out.println("<hr/>"); - out.println("<!--noindex-->"); - out.println("<p style=\"font-size: 14pt\">Кто <a href=\"/help/ru/contest\">выиграет iPod</a>?</p>"); - out.println("<table width=\"100%\">"); - - int i = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT users.id,users.nick,COUNT(users_refs.user_id) AS cnt FROM users INNER JOIN users_refs ON users.id=users_refs.ref WHERE users.id>2 GROUP BY users_refs.ref ORDER BY cnt DESC LIMIT 10"); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - String uname = rs.getString(2); - if (i == 0) { - out.println(" <tr><td><b><a href=\"/" + uname + "/\">" + uname + "</a></b></td><td align=\"right\"><b>" + rs.getInt(3) + "</b></td></tr>"); - } else { - out.println(" <tr><td><a href=\"/" + uname + "/\">" + uname + "</a></td><td align=\"right\">" + rs.getInt(3) + "</td></tr>"); - } - i++; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - - out.println("</table>"); - out.println("<!--/noindex-->"); + public static String formatPopularTags(JdbcTemplate sql, int cnt) { + List<String> popularTags = TagQueries.getPopularTags(sql).stream() + .map(t -> "<a href=\"/tag/" + URLEncoder.encode(t) + "\">" + Utils.encodeHTML(t) + "</a>").collect(Collectors.toList()); + return StringUtils.collectionToDelimitedString(popularTags, " "); } public static void pageFooter(HttpServletRequest request, PrintWriter out, com.juick.User visitor, boolean sapeon) { @@ -455,7 +406,7 @@ public class PageTemplates { return msg; } - public static void printMessages(PrintWriter out, Connection sql, com.juick.User user, List<Integer> mids, com.juick.User visitor, int YandexID, int ad_mid) { + public static void printMessages(PrintWriter out, JdbcTemplate sql, com.juick.User user, List<Integer> mids, com.juick.User visitor, int YandexID, int ad_mid) { List<com.juick.Message> msgs = MessagesQueries.getMessages(sql, mids); for (int i = 0; i < msgs.size(); i++) { |