· 5 years ago · Feb 19, 2020, 12:20 PM
1package com.redefocus.common.shared.permissions.user.dao;
2
3import com.google.common.collect.Maps;
4import com.google.common.collect.Sets;
5import com.redefocus.common.shared.permissions.user.data.User;
6import com.redefocus.common.shared.databases.mysql.dao.Table;
7import com.redefocus.common.shared.permissions.user.group.dao.UserGroupDao;
8import com.redefocus.common.shared.permissions.user.group.data.UserGroup;
9import com.redefocus.common.shared.permissions.user.manager.UserManager;
10
11import java.sql.Connection;
12import java.sql.PreparedStatement;
13import java.sql.ResultSet;
14import java.sql.SQLException;
15import java.util.HashMap;
16import java.util.Set;
17
18/**
19 * Created by @SrGutyerrez
20 */
21public class UserDao extends Table {
22
23 @Override
24 public String getDatabaseName() {
25 return "general";
26 }
27
28 @Override
29 public String getTableName() {
30 return "server_user";
31 }
32
33 @Override
34 public void createTable() {
35 this.execute(
36 String.format(
37 "CREATE TABLE IF NOT EXISTS %s" +
38 "(" +
39 "`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT," +
40 "`name` VARCHAR(16) NOT NULL," +
41 "`display_name` VARCHAR(16) NOT NULL," +
42 "`unique_id` VARCHAR(255) NOT NULL," +
43 "`email` VARCHAR(255)," +
44 "`password` VARCHAR(255)," +
45 "`discord_id` LONG," +
46 "`two_factor_authentication_enabled` BOOLEAN," +
47 "`two_factor_authentication_code` VARCHAR(255)," +
48 "`twitter_access_token` VARCHAR(255)," +
49 "`twitter_token_secret` VARCHAR(255)," +
50 "`created_at` LONG NOT NULL," +
51 "`first_login` LONG," +
52 "`last_login` LONG," +
53 "`last_address` VARCHAR(255)," +
54 "`last_lobby_id` INTEGER," +
55 "`language_id` INTEGER" +
56 ");",
57 this.getTableName()
58 )
59 );
60 }
61
62 public <T extends User> void insert(T user) {
63 String query = String.format(
64 "INSERT INTO %s (" +
65 "`name`," +
66 "`display_name`," +
67 "`unique_id`," +
68 "`created_at`," +
69 "`language_id`" +
70 ")" +
71 " VALUES " +
72 "(" +
73 "'%s'," +
74 "'%s'," +
75 "'%s'," +
76 "%d," +
77 "%d" +
78 ");",
79 this.getTableName(),
80 user.getName().toLowerCase(),
81 user.getDisplayName(),
82 user.getUniqueId(),
83 user.getCreatedAt(),
84 user.getLanguageId()
85 );
86
87 try (
88 Connection connection = this.getConnection();
89 PreparedStatement preparedStatement = connection.prepareStatement(query);
90 ) {
91 preparedStatement.execute();
92 } catch (SQLException exception) {
93 exception.printStackTrace();
94 }
95 }
96
97 @Override
98 public <K, V, U, I> void update(HashMap<K, V> keys, U key, I value) {
99 String where = this.generateWhere(keys);
100
101 String query = String.format(
102 "UPDATE %s SET %s WHERE `%s`=%s",
103 this.getTableName(),
104 where,
105 key,
106 value
107 );
108
109 try (
110 Connection connection = this.getConnection();
111 PreparedStatement preparedStatement = connection.prepareStatement(query);
112 ) {
113 preparedStatement.executeUpdate();
114 } catch (SQLException exception) {
115 exception.printStackTrace();
116 }
117 }
118
119 @Override
120 public <K, V> void delete(K key, V value) {
121 String query = String.format(
122 "DELETE FROM %s WHERE `%s`=%s",
123 this.getTableName(),
124 key,
125 value
126 );
127
128 this.execute(query);
129 }
130
131 public <K, V, T> T findOne(K key, V value) {
132 String query = String.format(
133 "SELECT * FROM %s WHERE `%s`='%s'",
134 this.getTableName(),
135 key,
136 value
137 );
138
139 UserGroupDao userGroupDao = new UserGroupDao();
140
141 try (
142 Connection connection = this.getConnection();
143 PreparedStatement preparedStatement = connection.prepareStatement(query);
144 ResultSet resultSet = preparedStatement.executeQuery();
145 ) {
146 if (!resultSet.next()) return null;
147
148 User user = UserManager.toUser(resultSet);
149
150 if (user.isConsole()) {
151 Set<UserGroup> groups = userGroupDao.findAll(
152 user.getId(),
153 ""
154 );
155
156 user.getGroups().addAll(groups);
157 }
158
159 return (T) user;
160 } catch (SQLException exception) {
161 exception.printStackTrace();
162 }
163
164 return null;
165 }
166
167 @Override
168 public <T> Set<T> findAll() {
169 String query = String.format(
170 "SELECT * FROM %s",
171 this.getTableName()
172 );
173
174 UserGroupDao userGroupDao = new UserGroupDao();
175
176 Set<T> users = Sets.newConcurrentHashSet();
177
178 try (
179 Connection connection = this.getConnection();
180 PreparedStatement preparedStatement = connection.prepareStatement(query);
181 ResultSet resultSet = preparedStatement.executeQuery();
182 ) {
183 while (resultSet.next()) {
184 User user = UserManager.toUser(resultSet);
185
186 HashMap<String, Object> keys = Maps.newHashMap();
187
188 keys.put("user_id", user.getId());
189
190 Set<UserGroup> groups = userGroupDao.findAll(keys);
191
192 user.getGroups().addAll(groups);
193
194 users.add((T) user);
195 }
196 } catch (SQLException exception) {
197 exception.printStackTrace();
198 }
199
200 return users;
201 }
202}