diff options
author | Vitaly Takmazov | 2016-07-07 11:25:23 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-07-07 11:25:23 +0300 |
commit | a93c85c5ea1bf6683f604bef2608053957577ff0 (patch) | |
tree | f80998a4cf339a2b85cd5f095f4386de9d46597b /src/main/java/com/juick/http/www/PageTemplates.java | |
parent | 3e48ba7c9cae9027b0a045aa25ae40b533518865 (diff) | |
parent | 857a930d31c425185c9b9405f5a7820e6cec9491 (diff) |
merge rebase_all
Diffstat (limited to 'src/main/java/com/juick/http/www/PageTemplates.java')
-rw-r--r-- | src/main/java/com/juick/http/www/PageTemplates.java | 105 |
1 files changed, 23 insertions, 82 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..7021ccba 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; /** @@ -45,7 +50,7 @@ import ru.sape.Sape; public class PageTemplates { public static Sape sape = null; - private static final SimpleDateFormat sdfSQL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + protected static final SimpleDateFormat sdfSQL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static SimpleDateFormat sdfSimple = new SimpleDateFormat("d MMM"); private static SimpleDateFormat sdfFull = new SimpleDateFormat("d MMM yyyy"); private static String tagsHTML = null; @@ -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) { @@ -275,7 +226,7 @@ public class PageTemplates { return ret; } - public static String formatDate(int minutes, String fulldate) { + public static String formatDate(int minutes, Date fulldate) { if (minutes < 1) { return "сейчас"; } else if (minutes < 60) { @@ -314,17 +265,16 @@ public class PageTemplates { } return days + " " + unit + " назад"; } else { - String ret = fulldate; + String ret = sdfFull.format(fulldate); synchronized (sdfSQL) { try { - Date pDate = sdfSQL.parse(fulldate); Calendar c = Calendar.getInstance(); int curyear = c.get(Calendar.YEAR); - c.setTime(pDate); + c.setTime(fulldate); if (c.get(Calendar.YEAR) == curyear) { - ret = sdfSimple.format(pDate); + ret = sdfSimple.format(fulldate); } else { - ret = sdfFull.format(pDate); + ret = sdfFull.format(fulldate); } } catch (Exception e) { System.err.println("PARSE EXCEPTION: " + fulldate); @@ -334,20 +284,11 @@ public class PageTemplates { } } - public static String formatJSLocalTime(String ts) { - String ret = ""; - synchronized (sdfSQL) { - try { - Date date = sdfSQL.parse(ts); - ret = "<script type=\"text/javascript\">" - + "var d=new Date(" + date.getTime() + ");" + public static String formatJSLocalTime(Date ts) { + return "<script type=\"text/javascript\">" + + "var d=new Date(" + ts.getTime() + ");" + "document.write((d.getDate()<10?'0':'')+d.getDate()+'.'+(d.getMonth()<9?'0':'')+(d.getMonth()+1)+'.'+d.getFullYear()+' '+(d.getHours()<10?'0':'')+d.getHours()+':'+(d.getMinutes()<10?'0':'')+d.getMinutes());" + "</script>"; - } catch (Exception e) { - System.err.println("PARSE EXCEPTION: " + ts); - } - } - return ret; } public static String formatReplies(int replies) { @@ -455,7 +396,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++) { @@ -501,7 +442,7 @@ public class PageTemplates { out.println("<article data-mid=\"" + msg.getMID() + "\">"); out.println(" <aside><a href=\"/" + msg.getUser().getUName() + "/\"><img src=\"//i.juick.com/a/" + msg.getUser().getUID() + ".png\" alt=\"" + msg.getUser().getUName() + "\"/></a></aside>"); out.println(" <header class=\"u\">@<a href=\"/" + msg.getUser().getUName() + "/\">" + msg.getUser().getUName() + "</a>:" + tagsStr + "</header>"); - out.println(" <header class=\"t\"><a href=\"/" + msg.getUser().getUName() + "/" + msg.getMID() + "\"><time datetime=\"" + msg.TimestampString + "Z\" title=\"" + msg.TimestampString + " GMT\">" + formatDate(msg.TimeAgo, msg.TimestampString) + "</time></a></header>"); + out.println(" <header class=\"t\"><a href=\"/" + msg.getUser().getUName() + "/" + msg.getMID() + "\"><time datetime=\"" + sdfSQL.format(msg.getDate()) + "Z\" title=\"" + sdfSQL.format(msg.getDate()) + " GMT\">" + formatDate(msg.TimeAgo, msg.getDate()) + "</time></a></header>"); if (msg.AttachmentType != null) { String fname = msg.getMID() + "." + msg.AttachmentType; out.println(" <p class=\"ir\"><a href=\"//i.juick.com/photos-512/" + fname + "\" onclick=\"return showPhotoDialog('" + fname + "')\"><img src=\"//i.juick.com/photos-512/" + fname + "\" alt=\"\"/></a></p>"); |