diff options
author | 2016-07-07 11:25:23 +0300 | |
---|---|---|
committer | 2016-07-07 11:25:23 +0300 | |
commit | a93c85c5ea1bf6683f604bef2608053957577ff0 (patch) | |
tree | f80998a4cf339a2b85cd5f095f4386de9d46597b /src/main/java/com/juick/http/www/User.java | |
parent | 3e48ba7c9cae9027b0a045aa25ae40b533518865 (diff) | |
parent | 857a930d31c425185c9b9405f5a7820e6cec9491 (diff) |
merge rebase_all
Diffstat (limited to 'src/main/java/com/juick/http/www/User.java')
-rw-r--r-- | src/main/java/com/juick/http/www/User.java | 130 |
1 files changed, 36 insertions, 94 deletions
diff --git a/src/main/java/com/juick/http/www/User.java b/src/main/java/com/juick/http/www/User.java index 84d1ad9a..0bdd910e 100644 --- a/src/main/java/com/juick/http/www/User.java +++ b/src/main/java/com/juick/http/www/User.java @@ -17,24 +17,22 @@ */ package com.juick.http.www; +import com.juick.Tag; import com.juick.server.MessagesQueries; import com.juick.server.TagQueries; import com.juick.server.UserQueries; +import org.springframework.jdbc.core.JdbcTemplate; + +import javax.servlet.ServletException; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; 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.util.ArrayList; import java.util.Arrays; import java.util.List; -import javax.servlet.ServletException; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; /** * @@ -42,7 +40,7 @@ import javax.servlet.http.HttpServletResponse; */ public class User { - protected void doGetBlog(Connection sql, Connection sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { + protected void doGetBlog(JdbcTemplate sql, JdbcTemplate sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); List<Integer> mids; @@ -158,7 +156,7 @@ public class User { } } - protected void doGetTags(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { + protected void doGetTags(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor == null) { @@ -181,7 +179,7 @@ public class User { } } - protected void doGetFriends(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { + protected void doGetFriends(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor == null) { @@ -198,25 +196,14 @@ public class User { out.println("<section id=\"content\">"); out.println("<table class=\"users\"><tr>"); - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT users.id,users.nick FROM subscr_users INNER JOIN users ON subscr_users.user_id=users.id WHERE subscr_users.suser_id=? ORDER BY users.nick"); - stmt.setInt(1, user.getUID()); - rs = stmt.executeQuery(); - rs.beforeFirst(); - int cnt = 0; - while (rs.next()) { - if (cnt % 3 == 0 && cnt > 0) { - out.print("</tr><tr>"); - } - out.print("<td><a href=\"/" + rs.getString(2) + "/\"><img src=\"//i.juick.com/as/" + rs.getInt(1) + ".png\"/>" + rs.getString(2) + "</a></td>"); - cnt++; + List<com.juick.User> friends = UserQueries.getUserFriends(sql, user.getUID()); + for (int i = 0; i < friends.size(); i++) { + if (i % 3 == 0 && i > 0) { + out.print("</tr><tr>"); } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + out.print("<td><a href=\"/" + friends.get(i).getUName() + + "/\"><img src=\"//i.juick.com/as/" + friends.get(i).getUID() + ".png\"/>" + + friends.get(i).getUName() + "</a></td>"); } out.println("</tr></table>"); @@ -227,7 +214,7 @@ public class User { } } - protected void doGetReaders(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { + protected void doGetReaders(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor == null) { @@ -244,25 +231,14 @@ public class User { out.println("<section id=\"content\">"); out.println("<table class=\"users\"><tr>"); - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT users.id,users.nick FROM subscr_users INNER JOIN users ON subscr_users.suser_id=users.id WHERE subscr_users.user_id=? ORDER BY users.nick"); - stmt.setInt(1, user.getUID()); - rs = stmt.executeQuery(); - rs.beforeFirst(); - int cnt = 0; - while (rs.next()) { - if (cnt % 3 == 0 && cnt > 0) { - out.print("</tr><tr>"); - } - out.print("<td><a href=\"/" + rs.getString(2) + "/\"><img src=\"//i.juick.com/as/" + rs.getInt(1) + ".png\"/>" + rs.getString(2) + "</a></td>"); - cnt++; + List<com.juick.User> readers = UserQueries.getUserReaders(sql, user.getUID()); + for (int i = 0; i < readers.size(); i++) { + if (i % 3 == 0 && i > 0) { + out.print("</tr><tr>"); } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + out.print("<td><a href=\"/" + readers.get(i).getUName() + + "/\"><img src=\"//i.juick.com/as/" + readers.get(i).getUID() + ".png\"/>" + + readers.get(i).getUName() + "</a></td>"); } out.println("</tr></table>"); @@ -285,7 +261,7 @@ public class User { } } - public static void pageUserColumn(PrintWriter out, Connection sql, com.juick.User user, com.juick.User visitor) { + public static void pageUserColumn(PrintWriter out, JdbcTemplate sql, com.juick.User user, com.juick.User visitor) { out.println("<aside id=\"column\">"); out.println(" <div id=\"ctitle\"><a href=\"./\"><img src=\"//i.juick.com/as/" + user.getUID() + ".png\" alt=\"\"/>" + user.getUName() + "</a></div>"); if (visitor != null && visitor.getUID() > 0 && visitor.getUID() != user.getUID()) { @@ -342,56 +318,22 @@ public class User { out.println("</aside>"); } - public static String pageUserTags(Connection sql, com.juick.User user, com.juick.User visitor, int cnt) { - com.juick.Tag tags[] = null; - - int maxUsageCnt = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (cnt > 0) { - stmt = sql.prepareStatement("SELECT tags.name AS name,COUNT(DISTINCT messages_tags.message_id) AS cnt FROM (messages INNER JOIN messages_tags ON (messages.message_id=messages_tags.message_id)) INNER JOIN tags ON messages_tags.tag_id=tags.tag_id WHERE messages.user_id=? GROUP BY messages_tags.tag_id ORDER BY cnt DESC LIMIT ?", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); - stmt.setInt(1, user.getUID()); - stmt.setInt(2, cnt); - } else { - stmt = sql.prepareStatement("SELECT tags.name AS name,COUNT(DISTINCT messages_tags.message_id) AS cnt FROM (messages INNER JOIN messages_tags ON (messages.message_id=messages_tags.message_id)) INNER JOIN tags ON messages_tags.tag_id=tags.tag_id WHERE messages.user_id=? GROUP BY messages_tags.tag_id ORDER BY cnt DESC", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); - stmt.setInt(1, user.getUID()); - } - rs = stmt.executeQuery(); - rs.last(); - tags = new com.juick.Tag[rs.getRow()]; - rs.beforeFirst(); - cnt = 0; - while (rs.next()) { - tags[cnt] = new com.juick.Tag(); - tags[cnt].Name = rs.getString(1); - tags[cnt].UsageCnt = rs.getInt(2); - if (tags[cnt].UsageCnt > maxUsageCnt) { - maxUsageCnt = tags[cnt].UsageCnt; - } - cnt++; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - - if (tags != null && cnt > 0) { - Arrays.sort(tags, 0, cnt); - } - + public static String pageUserTags(JdbcTemplate sql, com.juick.User user, com.juick.User visitor, int cnt) { + List<Tag> tags = TagQueries.getUserTagsAll(sql, user.getUID()); + int maxUsageCnt = tags.stream().map(t -> t.UsageCnt).max(Integer::max).orElse(0); String ret = ""; - for (int i = 0; i < cnt; i++) { - String tag = Utils.encodeHTML(tags[i].Name); + int count = Math.min(tags.size(), cnt); + for (int i = 0; i < count; i++) { + String tag = Utils.encodeHTML(tags.get(i).Name); try { - tag = "<a href=\"./?tag=" + URLEncoder.encode(tags[i].Name, "UTF-8") + "\" title=\"" + tags[i].UsageCnt + "\" rel=\"nofollow\">" + tag + "</a>"; + tag = "<a href=\"./?tag=" + URLEncoder.encode(tags.get(i).Name, "UTF-8") + "\" title=\"" + + tags.get(i).UsageCnt + "\" rel=\"nofollow\">" + tag + "</a>"; } catch (UnsupportedEncodingException e) { } - if (tags[i].UsageCnt > maxUsageCnt / 3 * 2) { + if (tags.get(i).UsageCnt > maxUsageCnt / 3 * 2) { ret += "<big>" + tag + "</big> "; - } else if (tags[i].UsageCnt > maxUsageCnt / 3) { + } else if (tags.get(i).UsageCnt > maxUsageCnt / 3) { ret += "<small>" + tag + "</small> "; } else { ret += tag + " "; |