aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/server/SubscriptionsQueries.java
blob: b271435be0f946da1a427606ca24f805bb8b2b70 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.juick.server;

import com.juick.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

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

    private static final Logger logger = Logger.getLogger(SubscriptionsQueries.class.getName());

    public static List<String> getJIDSubscribedToUser(Connection sql, int uid, boolean friendsonly) {
        List<String> jids = new ArrayList<>();

        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            if (friendsonly == false) {
                stmt = sql.prepareStatement("SELECT jids.jid FROM subscr_users INNER JOIN jids ON (subscr_users.user_id=? AND subscr_users.suser_id=jids.user_id) WHERE jids.active=1");
                stmt.setInt(1, uid);
            } else {
                stmt = sql.prepareStatement("SELECT jids.jid FROM subscr_users INNER JOIN jids ON (subscr_users.user_id=? AND subscr_users.suser_id=jids.user_id) WHERE jids.active=1 AND jids.user_id IN (SELECT wl_user_id FROM wl_users WHERE user_id=?)");
                stmt.setInt(1, uid);
                stmt.setInt(2, uid);
            }
            rs = stmt.executeQuery();
            rs.beforeFirst();
            while (rs.next()) {
                jids.add(rs.getString(1));
            }
        } catch (SQLException e) {
            logger.log(Level.SEVERE, "sql exception", e);
        } finally {
            Utils.finishSQL(rs, stmt);
        }
        return jids;
    }

    public static List<User> getSubscribedUsers(JdbcTemplate sql, int uid, int mid) {
        List<Integer> userids = UserQueries.getUserRead(sql, uid);
        Set<Integer> set = new HashSet<>();
        set.addAll(userids);
        List<Integer> tags = MessagesQueries.getMessageTagsIDs(sql, mid);
        if (tags.size() > 0) {
            List<Integer> tagUsers = sql.queryForList("SELECT suser_id FROM subscr_tags " +
                    "WHERE tag_id IN (" + StringUtils.arrayToCommaDelimitedString(tags.toArray()) + ") AND suser_id!=" + uid, Integer.class);
            set.addAll(tagUsers);
        }
        return UserQueries.getUsersByID(sql, new ArrayList<>(set));
    }

    public static List<User> getUsersSubscribedToComments(JdbcTemplate sql, int mid, int ignore_uid) {
        List<Integer> userids = sql.queryForList("SELECT suser_id FROM subscr_messages WHERE message_id=? AND suser_id!=?",
                new Object[] {mid, ignore_uid}, Integer.class);
        if (userids.size() > 0) {
            return UserQueries.getUsersByID(sql, userids);
        } else {
            return new ArrayList<>();
        }
    }

    public static List<User> getUsersSubscribedToUserRecommendations(JdbcTemplate sql, int uid, int mid, int muid) {
        List<Integer> tags = MessagesQueries.getMessageTagsIDs(sql, mid);

            String query = "SELECT user_id IN (SELECT suser_id FROM subscr_users WHERE user_id=" + uid + ")";
            query += " AND user_id NOT IN (SELECT user_id FROM bl_users WHERE bl_user_id=" + muid + ")";
            query += " AND user_id NOT IN (SELECT suser_id FROM subscr_users WHERE user_id=" + muid + ")";
            query += " AND user_id NOT IN (SELECT suser_id FROM subscr_messages WHERE message_id=" + mid + ")";
            query += " AND user_id NOT IN (SELECT user_id FROM favorites WHERE message_id=" + mid + ")";
            query += " AND user_id NOT IN (SELECT subscr_users.suser_id FROM subscr_users INNER JOIN favorites ON (favorites.message_id=" + mid + " AND subscr_users.user_id=favorites.user_id AND favorites.user_id!=" + uid + "))";
            if (!tags.isEmpty()) {
                String tagsStr = Utils.convertArrayInt2String(tags);
                query += " AND user_id NOT IN (SELECT suser_id FROM subscr_tags WHERE tag_id IN (" + tagsStr + "))";
                query += " AND user_id NOT IN (SELECT user_id FROM bl_tags WHERE tag_id IN (" + tagsStr + "))";
            }
        List<Integer> userids = sql.queryForList(query, Integer.class);
        return UserQueries.getUsersByID(sql, userids);
    }

    public static boolean subscribeMessage(JdbcTemplate sql, int mid, int vuid) {
        return sql.update("INSERT IGNORE INTO subscr_messages(suser_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1;
    }
}