forked from sh-vasily/java-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryImpl.java
More file actions
240 lines (222 loc) · 9.54 KB
/
LibraryImpl.java
File metadata and controls
240 lines (222 loc) · 9.54 KB
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
package ru.msu.vmk;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
public class LibraryImpl implements Library {
private final Logger logger = Logger.getLogger(LibraryImpl.class.getName());
private final String jdbcUrl;
private final String user;
private final String password;
public LibraryImpl(String jdbcUrl, String user, String password) {
this.jdbcUrl = jdbcUrl;
this.user = user;
this.password = password;
}
@Override
public void addNewBook(Book book) throws SQLException {
Book bookInLibrary = getBookByTitle(book.getTitle());
if (bookInLibrary == null) {
String sql = "insert into LIBRARY.BOOK (TITLE, IS_RESERVED) values (?,?)";
try (Connection connection = getConnection(); PreparedStatement pr = connection.prepareStatement(sql)) {
pr.setString(1, book.getTitle());
pr.setInt(2, 0);
int row = pr.executeUpdate();
if (row > 0) {
logger.info("Книга добавлена.");
}
}
} else {
logger.info("Книга уже есть в библиотеке!");
}
}
@Override
public void borrowBook(Book book, String student) throws SQLException {
Book bookInLibrary = getBookByTitle(book.getTitle());
if (bookInLibrary != null && bookInLibrary.getIsReserved() != 1) {
updateIsReservedFlag(bookInLibrary.getBookId(), 1);
Student studentInDB = getStudentByName(student);
if (studentInDB == null) {
insertNewStudent(student);
studentInDB = getStudentByName(student);
}
List<Relation> relations = getRelationByStudentId(studentInDB.getStudentId());
if (!relations.isEmpty()) {
if (relations.stream().anyMatch(rel -> rel.getBookId() != bookInLibrary.getBookId())) {
insertNewRelation(bookInLibrary.getBookId(), studentInDB.getStudentId());
}
} else {
insertNewRelation(bookInLibrary.getBookId(), studentInDB.getStudentId());
}
logger.info("Книга выдана.");
} else {
logger.info("Книги нет в библиотеки.");
}
}
@Override
public void returnBook(Book book, String student) throws SQLException {
Student studentInDB = getStudentByName(student);
if (studentInDB != null) {
List<Relation> relations = getRelationByStudentId(studentInDB.getStudentId());
if (!relations.isEmpty()) {
relations.stream().filter(rel -> rel.getBookId() == book.getBookId()).forEach(relation -> {
try {
updateIsReservedFlag(relation.getBookId(), 0);
deleteRelation(relation.getRelationId());
logger.info("Книгу вернули.");
} catch (SQLException e) {
logger.severe(e.getMessage());
}
});
} else {
logger.info("За студентом не числится эта книга!");
}
} else {
logger.info("Студент не найден!");
}
}
@Override
public List<Book> findAvailableBooks() throws SQLException {
List<Book> out = new ArrayList<>();
String sql = "select * from LIBRARY.BOOK where is_reserved = 0";
try (Connection connection = getConnection(); PreparedStatement pr = connection.prepareStatement(sql)) {
ResultSet resultSet = pr.executeQuery();
while (resultSet.next()) {
Book book = new Book();
book.setBookId(resultSet.getInt("BOOK_ID"));
book.setTitle(resultSet.getString("TITLE"));
book.setIsReserved(resultSet.getInt("IS_RESERVED"));
out.add(book);
}
}
return out;
}
@Override
public void init() throws SQLException {
try (Statement stmt = getConnection().createStatement()) {
stmt.execute("create schema LIBRARY");
stmt.execute("set schema LIBRARY");
stmt.execute("CREATE TABLE LIBRARY.BOOK " +
" (BOOK_ID INT NOT NULL GENERATED ALWAYS AS IDENTITY " +
" (START WITH 1, INCREMENT BY 1), TITLE VARCHAR(1000), IS_RESERVED INT)");
stmt.execute("CREATE TABLE LIBRARY.STUDENT " +
" (STUDENT_ID INT NOT NULL GENERATED ALWAYS AS IDENTITY " +
" (START WITH 1, INCREMENT BY 1), NAME VARCHAR(100))");
stmt.execute("CREATE TABLE LIBRARY.RELATION " +
" (RELATION_ID INT NOT NULL GENERATED ALWAYS AS IDENTITY " +
" (START WITH 1, INCREMENT BY 1), STUDENT_ID INT NOT NULL, BOOK_ID INT NOT NULL)");
}
}
@Override
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(jdbcUrl, user, password);
}
@Override
public Book getBookByTitle(String title) throws SQLException {
Book out = null;
String sql = "select * from LIBRARY.BOOK where title = ?";
try (Connection connection = getConnection(); PreparedStatement pr = connection.prepareStatement(sql)) {
pr.setString(1, title);
ResultSet resultSet = pr.executeQuery();
while (resultSet.next()) {
out = new Book();
out.setBookId(resultSet.getInt("BOOK_ID"));
out.setTitle(resultSet.getString("TITLE"));
out.setIsReserved(resultSet.getInt("IS_RESERVED"));
}
}
return out;
}
@Override
public void updateIsReservedFlag(int bookId, int isReserved) throws SQLException {
String sql = "update LIBRARY.BOOK set is_reserved = ? where book_id = ?";
try (Connection connection = getConnection(); PreparedStatement pr = connection.prepareStatement(sql)) {
pr.setInt(1, isReserved);
pr.setInt(2, bookId);
pr.executeUpdate();
}
}
@Override
public Student getStudentByName(String name) throws SQLException {
Student out = null;
String sql = "select * from LIBRARY.STUDENT where name = ?";
try (Connection connection = getConnection(); PreparedStatement pr = connection.prepareStatement(sql)) {
pr.setString(1, name);
ResultSet resultSet = pr.executeQuery();
while (resultSet.next()) {
out = new Student();
out.setStudentId(resultSet.getInt("STUDENT_ID"));
out.setStudentName(resultSet.getString("NAME"));
}
}
return out;
}
@Override
public List<Relation> getRelationByStudentId(int studentId) throws SQLException {
List<Relation> out = new ArrayList<>();
String sql = "select * from LIBRARY.RELATION where student_id = ?";
try (Connection connection = getConnection(); PreparedStatement pr = connection.prepareStatement(sql)) {
pr.setInt(1, studentId);
ResultSet resultSet = pr.executeQuery();
while (resultSet.next()) {
Relation relation = new Relation();
relation.setRelationId(resultSet.getInt("RELATION_ID"));
relation.setStudentId(resultSet.getInt("STUDENT_ID"));
relation.setBookId(resultSet.getInt("BOOK_ID"));
out.add(relation);
}
}
return out;
}
@Override
public void insertNewStudent(String student) throws SQLException {
String sql = "insert into LIBRARY.STUDENT (NAME) " +
"values (?)";
try (Connection connection = getConnection()) {
connection.setAutoCommit(false);
try (PreparedStatement pr = connection.prepareStatement(sql)) {
pr.setString(1, student);
pr.execute();
connection.commit();
} catch (SQLException e) {
logger.severe(e.getMessage());
getConnection().rollback();
}
}
}
@Override
public void insertNewRelation(int bookId, int studentId) throws SQLException {
String sql = "insert into LIBRARY.RELATION (STUDENT_ID, BOOK_ID) " +
"values (?, ?)";
try (Connection connection = getConnection()) {
connection.setAutoCommit(false);
try (PreparedStatement pr = connection.prepareStatement(sql)) {
pr.setInt(1, studentId);
pr.setInt(2, bookId);
pr.execute();
connection.commit();
} catch (SQLException e) {
logger.severe(e.getMessage());
getConnection().rollback();
}
}
}
@Override
public boolean deleteRelation(int relationId) throws SQLException {
boolean flag = false;
String sql = "delete from LIBRARY.RELATION where relation_id = ?";
try (Connection connection = getConnection()) {
try (PreparedStatement pr = connection.prepareStatement(sql)) {
pr.setInt(1, relationId);
int row = pr.executeUpdate();
if (row > 0) {
flag = true;
}
} catch (SQLException e) {
logger.severe(e.getMessage());
getConnection().rollback();
}
}
return flag;
}
}