aboutsummaryrefslogtreecommitdiff
path: root/juick-server-jdbc/src/main/java/com/juick/configuration/DataConfiguration.java
blob: 9f59f9656a96e7ec85e077bf98a73aac846cce2e (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * 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.configuration;

import com.juick.service.search.SearchService;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.List;

/**
 * Created by aalexeev on 11/11/16.
 */
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.juick.service"})
public class DataConfiguration implements TransactionManagementConfigurer {
    @Value("${datasource_driver:org.h2.Driver}")
    private String datasourceDriver;
    @Value("${datasource_url:jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;mode=MYSQL}")
    private String datasourceUrl;
    @Value("${datasource_user:juick}")
    private String datasourceUser;
    @Value("${datasource_password:secret}")
    private String datasourcePassword;

    // NOTE: The close() method will be called automatically with default @Bean settings
    // But Datasource interface has no close() method
    @Bean(destroyMethod = "")
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();

        dataSource.setDriverClassName(datasourceDriver);
        dataSource.setUrl(datasourceUrl);
        dataSource.setUsername(datasourceUser);
        dataSource.setPassword(datasourcePassword);

        dataSource.setValidationQuery("select 1");

        return dataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return transactionManager();
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
        if (datasourceDriver.equals("org.h2.Driver")) {
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS bl_users (user_id int(10) unsigned NOT NULL, " +
                    "bl_user_id int(10) unsigned NOT NULL, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP )");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS bl_tags (user_id int(10) unsigned NOT NULL, " +
                    "tag_id int(10) unsigned NOT NULL)");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS wl_users (user_id int(10) unsigned NOT NULL, " +
                    "wl_user_id int(10) unsigned NOT NULL, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP )");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS messages (" +
                    "message_id int(10) unsigned NOT NULL AUTO_INCREMENT," +
                    "user_id int(10) unsigned NOT NULL," +
                    "place_id int(10) unsigned DEFAULT NULL," +
                    "lat decimal(10,7) DEFAULT NULL," +
                    "lon decimal(10,7) DEFAULT NULL," +
                    "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," +
                    "replies smallint(5) unsigned NOT NULL DEFAULT 0," +
                    "maxreplyid smallint(5) unsigned NOT NULL DEFAULT 0," +
                    "privacy tinyint(4) NOT NULL DEFAULT '1'," +
                    "attach nchar(3) check (attach in ('jpg', 'mp4', 'png'))," +
                    "readonly tinyint(1) NOT NULL DEFAULT 0," +
                    "likes smallint(6) NOT NULL DEFAULT 0," +
                    "`popular` tinyint(4) NOT NULL DEFAULT '0'," +
                    "hidden tinyint(3) unsigned NOT NULL DEFAULT 0," +
                    "updated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS messages_tags (" +
                    "message_id int(10) unsigned NOT NULL," +
                    "tag_id int(10) unsigned NOT NULL" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS users (" +
                    "id int(10) unsigned NOT NULL AUTO_INCREMENT," +
                    "nick char(64) NOT NULL," +
                    "`lang` enum('en','ru','fr','fa','__') NOT NULL DEFAULT '__'," +
                    "passw char(32) NOT NULL," +
                    "banned tinyint(3) unsigned NOT NULL DEFAULT 0, " +
                    "PRIMARY KEY(id),  UNIQUE KEY(nick))");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS useroptions (" +
                    "user_id int(10) unsigned NOT NULL," +
                    "jnotify tinyint(1) NOT NULL DEFAULT 1," +
                    "`repliesview` tinyint(1) NOT NULL DEFAULT '0'," +
                    "`subscr_notify` tinyint(1) NOT NULL DEFAULT '1'," +
                    "`recommendations` tinyint(1) NOT NULL DEFAULT '1'," +
                    "subscr_active tinyint(1) NOT NULL DEFAULT 1)");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS subscr_users (" +
                    "user_id int(10) unsigned NOT NULL," +
                    "suser_id int(10) unsigned NOT NULL," +
                    "jid char(64) DEFAULT NULL," +
                    "active tinyint(1) NOT NULL DEFAULT 1," +
                    "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, " +
                    "PRIMARY KEY (suser_id, user_id))");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS subscr_messages (" +
                    "message_id int(10) unsigned NOT NULL," +
                    "suser_id int(10) unsigned NOT NULL, PRIMARY KEY (suser_id, message_id))");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS subscr_tags (" +
                    "tag_id int(10) unsigned NOT NULL," +
                    "suser_id int(10) unsigned NOT NULL, PRIMARY KEY (suser_id, tag_id))");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS messages_txt (" +
                    "message_id int(10) unsigned NOT NULL," +
                    "tags varchar(255)," +
                    "repliesby varchar(96)," +
                    "txt TEXT)");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS tags(" +
                    "tag_id int(10) unsigned NOT NULL AUTO_INCREMENT," +
                    "synonym_id int(10) unsigned DEFAULT NULL," +
                    "name varchar_ignorecase(48) NOT NULL," +
                    "top tinyint(1) unsigned NOT NULL DEFAULT 0," +
                    "stat_messages int(10) unsigned NOT NULL DEFAULT 0," +
                    "stat_users  smallint(5) unsigned NOT NULL DEFAULT 0)");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS jids(" +
                    "user_id int(10) unsigned NOT NULL AUTO_INCREMENT," +
                    "jid char(64) NOT NULL," +
                    "active tinyint(1) unsigned NOT NULL DEFAULT '1'," +
                    "loginhash char(36) unsigned DEFAULT NULL," +
                    "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS replies (" +
                    "message_id int(10) unsigned NOT NULL," +
                    "reply_id smallint(5) unsigned NOT NULL," +
                    "user_id int(10) unsigned NOT NULL," +
                    "replyto smallint(5) unsigned NOT NULL DEFAULT 0," +
                    "ts timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," +
                    "attach nchar(3) check (attach in ('jpg', 'mp4', 'png'))," +
                    "txt text)");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS twitter(" +
                    "user_id int(10) unsigned NOT NULL," +
                    "access_token char(64) NOT NULL," +
                    "access_token_secret char(64) NOT NULL," +
                    "crosspost tinyint(1) unsigned NOT NULL DEFAULT '1'," +
                    "uname char(64) NOT NULL," +
                    "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS facebook(" +
                    "user_id int(10) unsigned NOT NULL," +
                    "fb_id int(20) unsigned NOT NULL," +
                    "access_token char(255) NOT NULL," +
                    "loginhash char(36) NOT NULL," +
                    "crosspost tinyint(1) unsigned NOT NULL DEFAULT '1'," +
                    "fb_name char(64) NOT NULL," +
                    "fb_link char(64) NOT NULL," +
                    "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS vk(" +
                    "user_id int(10) unsigned NOT NULL," +
                    "vk_id int(20) unsigned NOT NULL," +
                    "access_token char(128) NOT NULL," +
                    "loginhash char(36) NOT NULL," +
                    "crosspost tinyint(1) unsigned NOT NULL DEFAULT '1'," +
                    "vk_name char(64) NOT NULL," +
                    "vk_link char(64) NOT NULL," +
                    "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS emails (" +
                    "  user_id int(10) unsigned NOT NULL," +
                    "  email char(64) NOT NULL," +
                    "  subscr_hour tinyint(4) DEFAULT NULL," +
                    "  PRIMARY KEY (email)" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS favorites (" +
                    "  user_id int(10) unsigned NOT NULL," +
                    "  message_id int(10) unsigned NOT NULL," +
                    "  ts datetime NOT NULL" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS logins (" +
                    "  user_id int(10) unsigned NOT NULL," +
                    "  hash char(16) NOT NULL," +
                    "  PRIMARY KEY (user_id)" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `telegram` (" +
                    "  `user_id` int(10) unsigned DEFAULT NULL," +
                    "  `tg_id` bigint(20) NOT NULL," +
                    "  `tg_name` char(64) NOT NULL," +
                    "  `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," +
                    "  `loginhash` char(36) DEFAULT NULL" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `pm` (" +
                    "  `user_id` int(10) unsigned NOT NULL," +
                    "  `user_id_to` int(10) unsigned NOT NULL," +
                    "  `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," +
                    "  `txt` text NOT NULL" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `pm_streams` (" +
                    "  `user_id` int(10) unsigned NOT NULL," +
                    "  `user_id_to` int(10) unsigned NOT NULL," +
                    "  `lastmessage` datetime NOT NULL," +
                    "  `lastview` datetime DEFAULT NULL," +
                    "  `unread` smallint(5) unsigned NOT NULL DEFAULT '0'" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `android` (" +
                    "  `user_id` int(10) unsigned NOT NULL," +
                    "  `regid` char(255) NOT NULL," +
                    "  `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `ios` (" +
                    "  `user_id` int(10) unsigned NOT NULL," +
                    "  `token` char(64) NOT NULL," +
                    "  `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `winphone` (" +
                    "  `user_id` int(10) unsigned NOT NULL," +
                    "  `url` char(255) NOT NULL," +
                    "  `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `tags_ignore` (" +
                    "  `tag_id` int(10) unsigned NOT NULL" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `users_subscr` (" +
                    "  `user_id` int(10) unsigned NOT NULL," +
                    "  `cnt` smallint(5) unsigned NOT NULL DEFAULT '0'," +
                    "  PRIMARY KEY (`user_id`)" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `auth` (" +
                    "  `user_id` int(10) unsigned NOT NULL," +
                    "  `protocol` enum('xmpp','email','sms') NOT NULL," +
                    "  `account` char(64) NOT NULL," +
                    "  `authcode` char(8) NOT NULL" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `mail` (" +
                    "  `user_id` int(10) unsigned NOT NULL," +
                    "  `hash` char(16) NOT NULL," +
                    "  PRIMARY KEY (`user_id`)" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `usersinfo` (" +
                    "  `user_id` int(10) unsigned NOT NULL," +
                    "  `jid` char(32) DEFAULT NULL," +
                    "  `fullname` char(32) DEFAULT NULL," +
                    "  `country` char(32) DEFAULT NULL," +
                    "  `url` char(64) DEFAULT NULL," +
                    "  `gender` char(32) DEFAULT NULL," +
                    "  `bday` char(10) DEFAULT NULL," +
                    "  `descr` varchar(255) DEFAULT NULL," +
                    "  PRIMARY KEY (`user_id`)" +
                    ")");
            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `telegram_chats` (\n" +
                    "  `chat_id` bigint(20) DEFAULT NULL,\n" +
                    "  UNIQUE KEY `chat_id` (`chat_id`)\n" +
                    ")");
        }
        return jdbcTemplate;
    }

    @Bean
    public SearchService emptySearchService() {
        return new SearchService() {
            @Override
            public void setMaxResult(int maxResult) {
            }

            @Override
            public List<Integer> searchInAllMessages(String searchString, int messageIdBefore) {
                return Collections.emptyList();
            }

            @Override
            public List<Integer> searchByStringAndUser(String searchString, int userId, int messageIdBefore) {
                return Collections.emptyList();
            }
        };
    }
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}