aboutsummaryrefslogtreecommitdiff
path: root/juick-server-jdbc/src/main/java/com/juick/service/PushQueriesServiceImpl.java
blob: 878d10d56226c52441c74a5de3752259ddffe285 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2008-2017, Juick
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.juick.service;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created by aalexeev on 11/13/16.
 */
@Repository
public class PushQueriesServiceImpl extends BaseJdbcService implements PushQueriesService {

    @Transactional(readOnly = true)
    @Override
    public List<String> getGCMRegID(final int uid) {
        return getJdbcTemplate().queryForList(
                "SELECT regid FROM android WHERE user_id=?",
                String.class,
                uid);
    }

    @Transactional(readOnly = true)
    @Override
    public List<String> getGCMTokens(final Collection<Integer> uids) {
        if (CollectionUtils.isEmpty(uids))
            return Collections.emptyList();

        return getNamedParameterJdbcTemplate().queryForList(
                "SELECT regid FROM android INNER JOIN users ON (users.id = android.user_id) WHERE users.id IN (:ids)",
                new MapSqlParameterSource("ids", uids),
                String.class);
    }

    @Transactional
    @Override
    public boolean addGCMToken(Integer uid, String token) {
        return getJdbcTemplate().update("INSERT IGNORE INTO android(user_id,regid) VALUES (?, ?)",
                uid, token) > 0;
    }

    @Transactional
    @Override
    public boolean deleteGCMToken(String token) {
        return getJdbcTemplate().update("DELETE FROM android WHERE regid=?", token) > 0;
    }

    @Transactional(readOnly = true)
    @Override
    public List<String> getMPNSURL(final int uid) {
        return getJdbcTemplate().queryForList(
                "SELECT url FROM winphone WHERE user_id=?",
                String.class,
                uid);
    }

    @Transactional(readOnly = true)
    @Override
    public List<String> getMPNSTokens(final Collection<Integer> uids) {
        if (CollectionUtils.isEmpty(uids))
            return Collections.emptyList();

        return getNamedParameterJdbcTemplate().queryForList(
                "SELECT url FROM winphone INNER JOIN users ON (users.id=winphone.user_id) WHERE users.id IN (:ids)",
                new MapSqlParameterSource("ids", uids),
                String.class);
    }

    @Transactional
    @Override
    public boolean addMPNSToken(Integer uid, String token) {
        return getJdbcTemplate().update("INSERT IGNORE INTO winphone(user_id,url) VALUES (?, ?)",
                uid, token) > 0;
    }

    @Transactional
    @Override
    public boolean deleteMPNSToken(String token) {
        return getJdbcTemplate().update("DELETE FROM winphone WHERE url=?", token) > 0;
    }

    @Transactional(readOnly = true)
    @Override
    public List<String> getAPNSToken(final int uid) {
        return getJdbcTemplate().queryForList(
                "SELECT token from ios WHERE user_id=?",
                String.class,
                uid);
    }

    @Transactional
    @Override
    public boolean deleteAPNSToken(String token) {
        return getJdbcTemplate().update("DELETE FROM ios WHERE token=?", token) > 0;
    }

    @Transactional(readOnly = true)
    @Override
    public List<String> getAPNSTokens(final Collection<Integer> uids) {
        if (CollectionUtils.isEmpty(uids))
            return Collections.emptyList();

        return getNamedParameterJdbcTemplate().queryForList(
                "SELECT token FROM ios INNER JOIN users ON (users.id = ios.user_id) WHERE users.id IN (:ids)",
                new MapSqlParameterSource("ids", uids),
                String.class);
    }

    @Transactional
    @Override
    public boolean addAPNSToken(Integer uid, String token) {
        return getJdbcTemplate().update("INSERT IGNORE INTO ios(user_id,token) VALUES (?, ?)",
                uid, token) > 0;
    }
}