aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/juick/http/www/Tags.java
blob: 07cff58219b62ae907eed4541a7e17a6deefb651 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.juick.http.www;

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.Arrays;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ugnich
 */
public class Tags {

    protected void doGetTags(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException {
        Locale locale = request.getLocale();
        ResourceBundle rbnm = ResourceBundle.getBundle("Blogs", locale);

        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            PageTemplates.pageHead(out, rbnm.getString("Tags"), null);
            PageTemplates.pageNavigation(out, locale, visitor);

            int cnt = 30;
            com.juick.Tag tags[] = new com.juick.Tag[cnt];
            int maxUsageCnt = 0;
            PreparedStatement stmt = null;
            ResultSet rs = null;
            try {
                stmt = sql.prepareStatement("SELECT tags.name AS name,COUNT(DISTINCT messages.user_id) AS cnt FROM (messages INNER JOIN messages_tags ON (messages.ts>TIMESTAMPADD(DAY,-3,NOW()) AND messages.message_id=messages_tags.message_id)) INNER JOIN tags ON messages_tags.tag_id=tags.tag_id GROUP BY tags.tag_id ORDER BY cnt DESC LIMIT ?");
                stmt.setInt(1, cnt);
                rs = stmt.executeQuery();
                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);
            }

            Arrays.sort(tags, 0, cnt);

            for (int i = 0; i < cnt; i++) {
                String tag = Utils.encodeHTML(tags[i].Name);
                try {
                    tag = "<a href=\"/?tag=" + URLEncoder.encode(tags[i].Name, "UTF-8") + "\">" + tag + "</a>";
                } catch (UnsupportedEncodingException e) {
                }

                if (tags[i].UsageCnt > maxUsageCnt / 3 * 2) {
                    out.print("<big>" + tag + "</big> ");
                } else if (tags[i].UsageCnt > maxUsageCnt / 3) {
                    out.print("<small>" + tag + "</small> ");
                } else {
                    out.print(tag + " ");
                }
            }

            PageTemplates.pageFooter(request, out, locale, visitor);
        } finally {
            out.close();
        }
    }
}