diff options
Diffstat (limited to 'src/main/java/com/juick/http/www/PageTemplates.java')
-rw-r--r-- | src/main/java/com/juick/http/www/PageTemplates.java | 125 |
1 files changed, 29 insertions, 96 deletions
diff --git a/src/main/java/com/juick/http/www/PageTemplates.java b/src/main/java/com/juick/http/www/PageTemplates.java index 2f9c8976..0682ed04 100644 --- a/src/main/java/com/juick/http/www/PageTemplates.java +++ b/src/main/java/com/juick/http/www/PageTemplates.java @@ -21,13 +21,13 @@ import com.juick.Message; import com.juick.Tag; import com.juick.server.MessagesQueries; import com.juick.server.UserQueries; +import org.springframework.jdbc.core.JdbcTemplate; +import ru.sape.Sape; + +import javax.servlet.http.HttpServletRequest; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; @@ -35,8 +35,7 @@ import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.servlet.http.HttpServletRequest; -import ru.sape.Sape; +import java.util.stream.Collectors; /** * @@ -142,11 +141,11 @@ public class PageTemplates { out.println("</script>"); } - 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); } @@ -163,64 +162,13 @@ 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); + public static String getPopularTags(JdbcTemplate sql, int cnt) { + List<String> tags = sql.queryForList("SELECT name FROM tags WHERE top=1 ORDER BY name ASC", String.class); + if (!tags.isEmpty()) { + return tags.stream().map(tag -> "<a href=\"/tag/" + URLEncoder.encode(tag) + "\">" + Utils.encodeHTML(tag) + "</a>") + .collect(Collectors.joining(" ")); } - - 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-->"); + return ""; } public static void pageFooter(HttpServletRequest request, PrintWriter out, com.juick.User visitor, boolean sapeon) { @@ -312,7 +260,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) { @@ -351,40 +299,24 @@ public class PageTemplates { } return days + " " + unit + " назад"; } else { - String ret = fulldate; - synchronized (sdfSQL) { - try { - Date pDate = sdfSQL.parse(fulldate); - Calendar c = Calendar.getInstance(); - int curyear = c.get(Calendar.YEAR); - c.setTime(pDate); - if (c.get(Calendar.YEAR) == curyear) { - ret = sdfSimple.format(pDate); - } else { - ret = sdfFull.format(pDate); - } - } catch (Exception e) { - System.err.println("PARSE EXCEPTION: " + fulldate); - } + String ret; + Calendar c = Calendar.getInstance(); + int curyear = c.get(Calendar.YEAR); + c.setTime(fulldate); + if (c.get(Calendar.YEAR) == curyear) { + ret = sdfSimple.format(fulldate); + } else { + ret = sdfFull.format(fulldate); } return ret; } } - 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) { @@ -492,7 +424,8 @@ 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++) { @@ -504,7 +437,7 @@ public class PageTemplates { } } - List<Integer> blUIDs = new ArrayList<Integer>(20); + List<Integer> blUIDs = new ArrayList<>(20); if (visitor != null) { for (Message msg : msgs) { blUIDs.add(msg.getUser().getUID()); @@ -542,7 +475,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>"); |