From 3d7f60c1c0b48dad2f58bffe3352d1c5c1b93f00 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 1 Dec 2016 13:56:53 +0300 Subject: juick-api: Tags controller from perl, refactoring --- juick-www/src/main/java/com/juick/www/NewMessage.java | 17 +++++++++-------- .../src/main/java/com/juick/www/PageTemplates.java | 14 +++++++------- juick-www/src/main/java/com/juick/www/User.java | 19 ++++++++++--------- juick-www/src/main/java/com/juick/www/UserThread.java | 10 +++++----- 4 files changed, 31 insertions(+), 29 deletions(-) (limited to 'juick-www/src/main/java/com/juick') diff --git a/juick-www/src/main/java/com/juick/www/NewMessage.java b/juick-www/src/main/java/com/juick/www/NewMessage.java index c066c5b3..ca783779 100644 --- a/juick-www/src/main/java/com/juick/www/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/NewMessage.java @@ -19,6 +19,7 @@ package com.juick.www; import com.juick.Tag; import com.juick.server.*; +import com.juick.server.helpers.TagStats; import net.coobird.thumbnailator.Thumbnails; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.math.NumberUtils; @@ -100,16 +101,16 @@ public class NewMessage { } void printUserTags(JdbcTemplate sql, PrintWriter out, com.juick.User visitor) { - List tags = TagQueries.getUserTagsAll(sql, visitor.getUid()); + List tags = TagQueries.getTagsStats(sql, visitor.getUid()); if (tags.isEmpty()) { return; } - int min = tags.get(0).UsageCnt; - int max = tags.get(0).UsageCnt; + int min = tags.get(0).getUsageCount(); + int max = tags.get(0).getUsageCount(); for (int i = 1; i < tags.size(); i++) { - int usagecnt = tags.get(i).UsageCnt; + int usagecnt = tags.get(i).getUsageCount(); if (usagecnt < min) { min = usagecnt; } @@ -126,12 +127,12 @@ public class NewMessage { } String taglink = ""; try { - taglink = "" + StringEscapeUtils.escapeHtml4(tags.get(i).getName()) + ""; + taglink = "" + StringEscapeUtils.escapeHtml4(tags.get(i).getTag().getName()) + ""; } catch (UnsupportedEncodingException e) { } - int usagecnt = tags.get(i).UsageCnt; + int usagecnt = tags.get(i).getUsageCount(); if (usagecnt <= max / 5 + min) { out.print("" + taglink + ""); } else if (usagecnt <= max / 5 * 2 + min) { diff --git a/juick-www/src/main/java/com/juick/www/PageTemplates.java b/juick-www/src/main/java/com/juick/www/PageTemplates.java index 180e852f..504a4b8b 100644 --- a/juick-www/src/main/java/com/juick/www/PageTemplates.java +++ b/juick-www/src/main/java/com/juick/www/PageTemplates.java @@ -18,10 +18,10 @@ package com.juick.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 com.juick.server.helpers.TagStats; import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.slf4j.Logger; @@ -185,13 +185,13 @@ public class PageTemplates { out.println(""); } - public static String formatTags(List tags) { + public static String formatTags(List tags) { String ret = ""; - for (Tag tag : tags) { - String tagName = StringEscapeUtils.escapeHtml4(tag.getName()); + for (TagStats tag : tags) { + String tagName = StringEscapeUtils.escapeHtml4(tag.getTag().getName()); try { - ret += ""; @@ -301,7 +301,7 @@ public class PageTemplates { for (Message msg : msgs) { - List tags = MessagesQueries.getMessageTags(sql, msg.getMid()); + List tags = MessagesQueries.getMessageTags(sql, msg.getMid()); String tagsStr = formatTags(tags); if (msg.ReadOnly) { tagsStr += "readonly"; diff --git a/juick-www/src/main/java/com/juick/www/User.java b/juick-www/src/main/java/com/juick/www/User.java index 3cc38b37..088fb004 100644 --- a/juick-www/src/main/java/com/juick/www/User.java +++ b/juick-www/src/main/java/com/juick/www/User.java @@ -17,10 +17,10 @@ */ package com.juick.www; -import com.juick.Tag; import com.juick.server.MessagesQueries; import com.juick.server.TagQueries; import com.juick.server.UserQueries; +import com.juick.server.helpers.TagStats; import org.apache.commons.lang3.StringEscapeUtils; import org.springframework.jdbc.core.JdbcTemplate; @@ -32,6 +32,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -323,22 +324,22 @@ public class User { } public static String pageUserTags(JdbcTemplate sql, com.juick.User user, com.juick.User visitor, int cnt) { - List tags = TagQueries.getUserTagsAll(sql, user.getUid()).stream() - .sorted((e1, e2) -> Integer.compare(e2.UsageCnt, e1.UsageCnt)).collect(Collectors.toList()); - int maxUsageCnt = tags.stream().map(t -> t.UsageCnt).max(Integer::max).orElse(0); + List tags = TagQueries.getTagsStats(sql, user.getUid()).stream() + .sorted((e1, e2) -> Integer.compare(e2.getUsageCount(), e1.getUsageCount())).collect(Collectors.toList()); + int maxUsageCnt = tags.stream().map(TagStats::getUsageCount).max(Comparator.naturalOrder()).orElse(0); String ret = ""; int count = cnt > 0 ? Math.min(tags.size(), cnt) : tags.size(); for (int i = 0; i < count; i++) { - String tag = StringEscapeUtils.escapeHtml4(tags.get(i).getName()); + String tag = StringEscapeUtils.escapeHtml4(tags.get(i).getTag().getName()); try { - tag = "" + tag + ""; + tag = "" + tag + ""; } catch (UnsupportedEncodingException e) { } - if (tags.get(i).UsageCnt > maxUsageCnt / 3 * 2) { + if (tags.get(i).getUsageCount() > maxUsageCnt / 3 * 2) { ret += "" + tag + " "; - } else if (tags.get(i).UsageCnt > maxUsageCnt / 3) { + } else if (tags.get(i).getUsageCount() > maxUsageCnt / 3) { ret += "" + tag + " "; } else { ret += tag + " "; diff --git a/juick-www/src/main/java/com/juick/www/UserThread.java b/juick-www/src/main/java/com/juick/www/UserThread.java index d94f2156..79ec42fc 100644 --- a/juick-www/src/main/java/com/juick/www/UserThread.java +++ b/juick-www/src/main/java/com/juick/www/UserThread.java @@ -18,19 +18,19 @@ package com.juick.www; import com.juick.Message; -import com.juick.Tag; import com.juick.server.MessagesQueries; import com.juick.server.UserQueries; +import com.juick.server.helpers.TagStats; import com.juick.util.MessageUtils; import org.springframework.jdbc.core.JdbcTemplate; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; /** * @@ -95,7 +95,7 @@ public class UserThread { public static com.juick.Message printMessage(PrintWriter out, JdbcTemplate sql, com.juick.Message msg, com.juick.User visitor) { msg.VisitorCanComment = visitor.getUid() > 0; - List tags = MessagesQueries.getMessageTags(sql, msg.getMid()); + List tags = MessagesQueries.getMessageTags(sql, msg.getMid()); String tagsStr = PageTemplates.formatTags(tags); if (msg.ReadOnly) { tagsStr += "readonly"; -- cgit v1.2.3