lesson 13 dz · AlbertoJava/basejava@8296687 · GitHub
Skip to content

Commit 8296687

Browse files
committed
lesson 13 dz
before refactoring
1 parent c45e13f commit 8296687

11 files changed

Lines changed: 250 additions & 6 deletions

File tree

config/resumes.properties

Lines changed: 4 additions & 0 deletions

lib/postgresql-42.2.1.jar

776 KB
Binary file not shown.

src/com/urise/webapp/Config.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.urise.webapp;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.nio.file.Files;
8+
import java.util.Properties;
9+
10+
public class Config {
11+
protected static final File PROPS = new File(".\\storage\\resume.properties");
12+
private static final Config INSTANCE = new Config();
13+
private Properties props = new Properties();
14+
private File storageDir;
15+
16+
private Config() {
17+
try (InputStream is = new FileInputStream("./config/resumes.properties")) {
18+
props.load(is);
19+
storageDir = new File (props.getProperty("storage.dir"));
20+
21+
} catch (IOException e) {
22+
throw new IllegalStateException("Invalid config file " +PROPS.getAbsolutePath());
23+
}
24+
}
25+
26+
public static Config getInstance() {
27+
return INSTANCE;
28+
}
29+
30+
public File getStorageDir() {
31+
return storageDir;
32+
}
33+
public String getDbUrl (){
34+
return props.getProperty("db.url");
35+
}
36+
public String getDbUser (){
37+
return props.getProperty("db.user");
38+
}
39+
public String getDbPassword (){
40+
return props.getProperty("db.password");
41+
}
42+
}

src/com/urise/webapp/exception/StorageException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public StorageException(String message, Exception e) {
1616
this(message, null, e);
1717
}
1818

19+
public StorageException(Exception e) {
20+
this(e.getMessage(),e);
21+
}
1922
public StorageException(String message, String uuid, Exception e) {
2023
super(message, e);
2124
this.uuid = uuid;

src/com/urise/webapp/model/Resume.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Resume(String fullName) {
3333
public Resume(String uuid, String fullName) {
3434
Objects.requireNonNull(fullName, "fullName must not be null");
3535
Objects.requireNonNull(uuid, "uuid must not be null");
36-
this.uuid = uuid;
36+
this.uuid = uuid.trim();
3737
this.fullName = fullName;
3838
}
3939

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.urise.webapp.sql;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
6+
public interface ConnectionFactory {
7+
Connection getConnection() throws SQLException;
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.urise.webapp.storage;
2+
3+
import com.urise.webapp.sql.ConnectionFactory;
4+
5+
import java.sql.PreparedStatement;
6+
import java.sql.SQLException;
7+
8+
public interface ABlockOfCode<T> {
9+
T execute(PreparedStatement ps) throws SQLException;
10+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package com.urise.webapp.storage;
2+
3+
import com.urise.webapp.exception.ExistStorageException;
4+
import com.urise.webapp.exception.NotExistStorageException;
5+
import com.urise.webapp.exception.StorageException;
6+
import com.urise.webapp.model.Resume;
7+
import com.urise.webapp.sql.ConnectionFactory;
8+
9+
import java.sql.*;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
public class SqlStorage implements Storage {
14+
15+
public final ConnectionFactory connectionFactory;
16+
17+
public SqlStorage(String dbUrl, String dbUser, String dbPassword) {
18+
connectionFactory = () -> DriverManager.getConnection(dbUrl, dbUser, dbPassword);
19+
}
20+
21+
@Override
22+
public void clear() {
23+
/* try (Connection conn = connectionFactory.getConnection();
24+
PreparedStatement ps = conn.prepareStatement("DELETE FROM RESUME")
25+
) {
26+
ps.execute();
27+
} catch (SQLException e) {
28+
e.printStackTrace();
29+
}*/
30+
transactionExecute(ps->ps.execute(),"DELETE FROM RESUME");
31+
}
32+
33+
@Override
34+
public void save(Resume resume) {
35+
/* try (Connection conn = connectionFactory.getConnection();
36+
PreparedStatement ps = conn.prepareStatement("INSERT INTO RESUME (uuid, full_name) values (?,?)")
37+
) {
38+
ps.setString(1, resume.getUuid());
39+
ps.setString(2, resume.getFullName());
40+
ps.execute();
41+
} catch (SQLException e) {
42+
throw new StorageException(e);
43+
}
44+
*/
45+
transactionExecute(ps->{
46+
ps.setString(1, resume.getUuid());
47+
ps.setString(2, resume.getFullName());
48+
ps.execute();
49+
return null;
50+
},"INSERT INTO RESUME (uuid, full_name) values (?,?)" );
51+
}
52+
53+
@Override
54+
public Resume get(String uuid) {
55+
/* try (Connection conn = connectionFactory.getConnection();
56+
PreparedStatement ps = conn.prepareStatement("SELECT * FROM RESUME where uuid=?")
57+
) {
58+
ps.setString(1, uuid);
59+
ResultSet rs = ps.executeQuery();
60+
if (rs.next()) {
61+
throw new NotExistStorageException(uuid);
62+
}
63+
Resume r = new Resume(uuid, rs.getString("full_name"));
64+
return r;
65+
} catch (SQLException e) {
66+
throw new StorageException(e);
67+
}*/
68+
return transactionExecute(ps->{
69+
ps.setString(1, uuid);
70+
ResultSet rs = ps.executeQuery();
71+
if (!rs.next()) {
72+
throw new NotExistStorageException(uuid);
73+
}
74+
return new Resume(uuid, rs.getString( "full_name"));
75+
},"SELECT * FROM RESUME where uuid=?");
76+
77+
}
78+
79+
@Override
80+
public void delete(String uuid) {
81+
/* try (Connection conn=connectionFactory.getConnection();
82+
PreparedStatement ps=conn.prepareStatement("DELETE from RESUME where UUID=?")
83+
){
84+
ps.setString(1,uuid);
85+
ps.execute();
86+
} catch (SQLException e) {
87+
throw new StorageException(e);
88+
}*/
89+
transactionExecute(p -> {
90+
p.setString(1, uuid);
91+
if (p.executeUpdate()==0){throw new NotExistStorageException(uuid);}
92+
return null;
93+
}, "DELETE from RESUME where UUID=?");
94+
}
95+
96+
@Override
97+
public List<Resume> getAllSorted() {
98+
/* try (Connection conn=connectionFactory.getConnection();
99+
PreparedStatement ps=conn.prepareStatement("SELECT * FROM RESUME ORDER BY full_name")
100+
){
101+
ResultSet rs=ps.executeQuery();
102+
List<Resume> list = new ArrayList<>();
103+
while (rs.next()){
104+
list.add(new Resume(rs.getString("uuid"), rs.getString("full_name")));
105+
}
106+
return list;
107+
} catch (SQLException e) {
108+
throw new StorageException(e);
109+
}*/
110+
return transactionExecute(p -> {
111+
ResultSet rs = p.executeQuery();
112+
List<Resume> list = new ArrayList<>();
113+
while (rs.next()) {
114+
list.add(new Resume(rs.getString("uuid"), rs.getString("full_name")));
115+
}
116+
return list;
117+
}, "SELECT * FROM RESUME ORDER BY full_name");
118+
119+
}
120+
121+
@Override
122+
public int size() {
123+
/* try (Connection conn=connectionFactory.getConnection();
124+
PreparedStatement ps=conn.prepareStatement("SELECT count (uuid) FROM RESUME")
125+
){
126+
ResultSet rs=ps.executeQuery();
127+
return rs.getInt(1);
128+
} catch (SQLException e) {
129+
throw new StorageException(e);
130+
}*/
131+
return transactionExecute(ps -> {ResultSet rs= ps.executeQuery();rs.next(); return rs.getInt(1);}, "SELECT count (uuid) FROM RESUME");
132+
}
133+
134+
@Override
135+
public void update(Resume resume) {
136+
/* try (Connection conn=connectionFactory.getConnection();
137+
PreparedStatement ps=conn.prepareStatement("UPDATE RESUME set full_name=? where uuid=?")
138+
){ ps.setString(1,resume.getFullName());
139+
ps.setString(2,resume.getUuid());
140+
ps.execute();
141+
} catch (SQLException e) {
142+
throw new StorageException(e);
143+
}*/
144+
145+
transactionExecute(ps -> {
146+
ps.setString(1, resume.getFullName());
147+
ps.setString(2, resume.getUuid());
148+
if (ps.executeUpdate()==0) {throw new NotExistStorageException(resume.getUuid());}
149+
return null;
150+
}, "UPDATE RESUME set full_name=? where uuid=?");
151+
}
152+
153+
private <T> T transactionExecute(ABlockOfCode<T> aBlockOfCode, String sqlQery) {
154+
try (Connection conn = connectionFactory.getConnection();
155+
PreparedStatement ps = conn.prepareStatement(sqlQery)
156+
) {
157+
return aBlockOfCode.execute(ps);
158+
} catch (SQLException e) {
159+
throw new StorageException(e);
160+
}
161+
}
162+
}

test/com/urise/webapp/storage/AbstractStorageTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.urise.webapp.storage;
22

3+
import com.urise.webapp.Config;
34
import com.urise.webapp.exception.ExistStorageException;
45
import com.urise.webapp.exception.NotExistStorageException;
6+
import com.urise.webapp.exception.StorageException;
57
import com.urise.webapp.model.Resume;
68
import org.junit.Before;
79
import org.junit.Test;
@@ -13,7 +15,7 @@
1315
import static org.junit.Assert.assertNotSame;
1416

1517
public abstract class AbstractStorageTest {
16-
protected static final File STORAGE_DIR = new File("C:\\Java\\FileStorage");
18+
protected static final File STORAGE_DIR = Config.getInstance().getStorageDir();
1719
private static final String UUID_1 = "UUID_1";
1820
private static final String UUID_2 = "UUID_2";
1921
private static final String UUID_3 = "UUID_3";
@@ -24,11 +26,11 @@ public abstract class AbstractStorageTest {
2426
private static final Resume R_4 = new Resume(UUID_4, "FullName_4");
2527

2628
static {
27-
ResumeDataTest.fillResumeSections(R_1, 3, 3, 3, 3);
29+
/* ResumeDataTest.fillResumeSections(R_1, 3, 3, 3, 3);
2830
ResumeDataTest.fillResumeSections(R_2, 4, 4, 4, 4);
2931
ResumeDataTest.fillResumeSections(R_3, 5, 5, 5, 5);
3032
ResumeDataTest.fillResumeSections(R_4, 6, 6, 6, 6);
31-
}
33+
*/}
3234

3335
protected Storage storage;
3436

@@ -51,7 +53,7 @@ public void clear() {
5153
assertEquals(0, storage.size());
5254
}
5355

54-
@Test(expected = ExistStorageException.class)
56+
@Test(expected = StorageException.class)
5557
public void saveExist() {
5658
storage.save(R_3);
5759
}

test/com/urise/webapp/storage/AllStorageTest.java

Lines changed: 2 additions & 1 deletion

0 commit comments

Comments
 (0)