1
|
package cz.zcu.kiv.backendapi.config;
|
2
|
|
3
|
import cz.zcu.kiv.backendapi.catalog.CatalogItem;
|
4
|
import cz.zcu.kiv.backendapi.catalog.CatalogItemRepository;
|
5
|
import cz.zcu.kiv.backendapi.catalog.ICatalogItemService;
|
6
|
import cz.zcu.kiv.backendapi.sources.ISourcesService;
|
7
|
import cz.zcu.kiv.backendapi.sources.Sources;
|
8
|
import cz.zcu.kiv.backendapi.titlepage.ITitlePageService;
|
9
|
import cz.zcu.kiv.backendapi.titlepage.TitlePage;
|
10
|
import cz.zcu.kiv.backendapi.type.ITypeService;
|
11
|
import cz.zcu.kiv.backendapi.type.Type;
|
12
|
import cz.zcu.kiv.backendapi.user.UserEntity;
|
13
|
import cz.zcu.kiv.backendapi.user.UserRepository;
|
14
|
import lombok.RequiredArgsConstructor;
|
15
|
import org.apache.commons.csv.CSVFormat;
|
16
|
import org.apache.commons.csv.CSVParser;
|
17
|
import org.apache.commons.csv.CSVRecord;
|
18
|
import org.springframework.context.ApplicationListener;
|
19
|
import org.springframework.context.event.ContextRefreshedEvent;
|
20
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
21
|
import org.springframework.stereotype.Component;
|
22
|
import org.springframework.transaction.annotation.Transactional;
|
23
|
|
24
|
import java.io.File;
|
25
|
import java.io.IOException;
|
26
|
import java.io.InputStream;
|
27
|
import java.nio.charset.Charset;
|
28
|
import java.nio.charset.StandardCharsets;
|
29
|
import java.util.ArrayList;
|
30
|
import java.util.List;
|
31
|
|
32
|
//TODO comment or delete
|
33
|
@Component
|
34
|
@RequiredArgsConstructor
|
35
|
public class DataInitiator implements ApplicationListener<ContextRefreshedEvent> {
|
36
|
|
37
|
private final ICatalogItemService catalogService;
|
38
|
|
39
|
private final ITypeService typeService;
|
40
|
|
41
|
private final CatalogItemRepository catalogItemRepository;
|
42
|
|
43
|
private final UserRepository userRepository;
|
44
|
|
45
|
private final ITitlePageService titlePageService;
|
46
|
private final ISourcesService sourcesService;
|
47
|
|
48
|
private final BCryptPasswordEncoder encoder;
|
49
|
|
50
|
@Override
|
51
|
@Transactional
|
52
|
public void onApplicationEvent(ContextRefreshedEvent event) {
|
53
|
List<Type> types = loadTypes();
|
54
|
for (Type type : types) {
|
55
|
typeService.saveType(type);
|
56
|
}
|
57
|
List<CatalogItem> catalog = loadCatalog();
|
58
|
catalogService.saveCatalog(catalog);
|
59
|
|
60
|
titlePageService.updateTitlePage(new TitlePage(1L, ""));
|
61
|
sourcesService.updateSources(new Sources(1L, ""));
|
62
|
|
63
|
UserEntity user1 = new UserEntity("Kateřina Šašková", "katerina.saskova@gmail.com", "$2a$10$nTJORZk6Ik.d19SlAY1UAuPPVgIwOufmWnIMx07CjqY5VKVlCpxsu", (byte) 7, true);
|
64
|
userRepository.save(user1);
|
65
|
|
66
|
UserEntity user2 = new UserEntity("normal", "normal", encoder.encode("password"), (byte) 1, false);
|
67
|
userRepository.save(user2);
|
68
|
|
69
|
UserEntity superAmin = new UserEntity("SuperAdmin", "admin@admin.com", "$2a$10$WN7iAmrlUZiHMShI85.ScOyBbGXMMondhh.7x/WopeQ8YIMVyA8O2", (byte) 7, true);
|
70
|
userRepository.save(superAmin);
|
71
|
|
72
|
}
|
73
|
|
74
|
private List<CatalogItem> loadCatalog() {
|
75
|
List<CatalogItem> catalogEntities = new ArrayList<>();
|
76
|
InputStream csvData = getClass().getClassLoader().getResourceAsStream("22-05-09-AssyrianProject-AllNoDupl.csv");
|
77
|
// File csvData = new File("AssyrianProject-AllNoDupl-22-03-28.csv");
|
78
|
CSVParser parser;
|
79
|
try {
|
80
|
parser = CSVParser.parse(csvData, StandardCharsets.UTF_8, CSVFormat.Builder.create(CSVFormat.DEFAULT)
|
81
|
.setHeader()
|
82
|
.setSkipHeaderRecord(true)
|
83
|
.build());
|
84
|
for (CSVRecord csvRecord : parser) {
|
85
|
catalogEntities.add(new CatalogItem(csvRecord.toList()));
|
86
|
}
|
87
|
} catch (IOException e) {
|
88
|
e.printStackTrace();
|
89
|
}
|
90
|
return catalogEntities;
|
91
|
|
92
|
}
|
93
|
|
94
|
private List<Type> loadTypes() {
|
95
|
List<Type> types = new ArrayList<>();
|
96
|
CSVParser parser;
|
97
|
File csvData = new File("AssyrianProject-All.csv");
|
98
|
try {
|
99
|
parser = CSVParser.parse(csvData, Charset.defaultCharset(), CSVFormat.DEFAULT.builder().setSkipHeaderRecord(true).build());
|
100
|
for (CSVRecord csvRecord : parser) {
|
101
|
if (csvRecord.get(0).equals("10000")) {
|
102
|
types.add(new Type(csvRecord.get(2)));
|
103
|
}
|
104
|
}
|
105
|
} catch (IOException e) {
|
106
|
e.printStackTrace();
|
107
|
}
|
108
|
return types;
|
109
|
}
|
110
|
}
|