remake init data
json dump Small bug fix
This commit is contained in:
@@ -4,7 +4,6 @@ import com.jambotronGroup.jambotron.jsonDump.JsonDumpService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -17,15 +16,11 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/json-dump")
|
||||
@@ -79,27 +74,27 @@ public class JsonDumpController {
|
||||
|
||||
// ==================== IMPORT ENDPOINTS ====================
|
||||
|
||||
@PostMapping("/import/users")
|
||||
@PostMapping("/initData/users")
|
||||
public ResponseEntity<Map<String, Object>> importUsers() {
|
||||
return performImport(() -> _jsonDumpService.importUsers(), "users");
|
||||
}
|
||||
|
||||
@PostMapping("/import/tutorials")
|
||||
@PostMapping("/initData/tutorials")
|
||||
public ResponseEntity<Map<String, Object>> importTutorials() {
|
||||
return performImport(() -> _jsonDumpService.importTutorials(), "tutorials");
|
||||
}
|
||||
|
||||
@PostMapping("/import/roles")
|
||||
@PostMapping("/initData/roles")
|
||||
public ResponseEntity<Map<String, Object>> importRoles() {
|
||||
return performImport(() -> _jsonDumpService.importRoles(), "roles");
|
||||
}
|
||||
|
||||
@PostMapping("/import/users/file")
|
||||
@PostMapping("/initData/users/file")
|
||||
public ResponseEntity<Map<String, Object>> importUsersFromFile(@RequestParam("file") MultipartFile file) {
|
||||
return importFromFile(file, (path) -> _jsonDumpService.importUsers(path), "users");
|
||||
}
|
||||
|
||||
@PostMapping("/import/tutorials/file")
|
||||
@PostMapping("/initData/tutorials/file")
|
||||
public ResponseEntity<Map<String, Object>> importTutorialsFromFile(@RequestParam("file") MultipartFile file) {
|
||||
return importFromFile(file, (path) -> _jsonDumpService.importTutorials(path), "tutorials");
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ public class InitDataConfiguration {
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
|
||||
_initDataService.importData();
|
||||
_initDataService.initData();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,11 +13,13 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashSet;
|
||||
@@ -32,7 +34,7 @@ public class InitDataService {
|
||||
|
||||
private static final Logger _logger = LoggerFactory.getLogger(InitDataService.class);
|
||||
|
||||
private ObjectMapper _mapper;
|
||||
protected ObjectMapper _mapper;
|
||||
|
||||
@Autowired
|
||||
private ResourceLoader _resources;
|
||||
@@ -42,8 +44,53 @@ public class InitDataService {
|
||||
@Autowired
|
||||
private UserRepository _userRepository;
|
||||
|
||||
|
||||
private final Path _initDataPath = Path.of("/jambotron-data/initData/");
|
||||
|
||||
public InitDataService(){
|
||||
|
||||
_mapper = new ObjectMapper();
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void extractInitData(){
|
||||
_logger.info("Extracting init data to {}", _initDataPath.toAbsolutePath());
|
||||
if(!_initDataPath.toFile().exists()) {
|
||||
_logger.info("Creating init data directory at {}", _initDataPath.toAbsolutePath());
|
||||
try {
|
||||
Files.createDirectories(_initDataPath);
|
||||
} catch (Exception e) {
|
||||
_logger.error("Failed to create init data directory", e);
|
||||
}
|
||||
}
|
||||
extractJsonFile("roles.json");
|
||||
extractJsonFile("users.json");
|
||||
}
|
||||
|
||||
private void extractJsonFile(String fileName) {
|
||||
try {
|
||||
ClassPathResource resource = new ClassPathResource("initData/"+ fileName);
|
||||
|
||||
|
||||
Path targetPath = _initDataPath.resolve(fileName);
|
||||
if (!Files.exists(targetPath)) {
|
||||
try (InputStream inputStream = resource.getInputStream()) {
|
||||
Files.copy(resource.getInputStream(), targetPath);
|
||||
}
|
||||
|
||||
_logger.info("Copied {} to {}", fileName, targetPath.toAbsolutePath());
|
||||
} else {
|
||||
try (InputStream inputStream = resource.getInputStream()) {
|
||||
Files.copy(resource.getInputStream(), targetPath, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
_logger.warn("{} already exists, REPLACE", targetPath.toAbsolutePath());
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
_logger.error("Failed to extract JSON file: {}", fileName, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -122,23 +169,31 @@ public class InitDataService {
|
||||
return _mapper.readValue(json, type);
|
||||
}
|
||||
|
||||
public void importData() {
|
||||
try {
|
||||
Resource resource = _resources.getResource("classpath:import/roles.json");
|
||||
if (resource.exists()) {
|
||||
public void initData() {
|
||||
|
||||
importRoles(resource.getFile().toPath());
|
||||
extractInitData();
|
||||
|
||||
_logger.info("Initializing data import from {}", _initDataPath.toAbsolutePath());
|
||||
|
||||
try {
|
||||
|
||||
Path rolesFile = _initDataPath.resolve("roles.json");
|
||||
|
||||
if (rolesFile.toFile().exists()) {
|
||||
|
||||
importRoles(rolesFile);
|
||||
} else {
|
||||
|
||||
_logger.warn("Roles import file not found: {}", resource.getFilename());
|
||||
_logger.warn("Roles import file not found: {}", rolesFile.getFileName());
|
||||
}
|
||||
|
||||
resource = _resources.getResource("classpath:import/users.json");
|
||||
if (resource.exists()) {
|
||||
importUsers(resource.getFile().toPath());
|
||||
Path usersFile = _initDataPath.resolve("users.json");
|
||||
|
||||
if (usersFile.toFile().exists()) {
|
||||
importUsers(usersFile);
|
||||
} else {
|
||||
|
||||
_logger.warn("Users import file not found: {}", resource.getFilename());
|
||||
_logger.warn("Users import file not found: {}", usersFile.getFileName());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
_logger.error("Error importing data", e);
|
||||
|
||||
@@ -50,16 +50,16 @@ public class JsonDumpService extends InitDataService {
|
||||
|
||||
private final Path _extractedPath = _root.resolve("extracted");
|
||||
|
||||
private final ObjectMapper _mapper;
|
||||
//private final ObjectMapper _mapper;
|
||||
|
||||
@Autowired
|
||||
private final UserRepository _userRepository;
|
||||
private UserRepository _userRepository;
|
||||
|
||||
@Autowired
|
||||
private final RoleRepository _roleRepository;
|
||||
private RoleRepository _roleRepository;
|
||||
|
||||
@Autowired
|
||||
private final TutorialRepository _tutorialRepository;
|
||||
private TutorialRepository _tutorialRepository;
|
||||
|
||||
@Value("${app.json.dump.cleanup.enabled:true}")
|
||||
private boolean _cleanupEnabled;
|
||||
@@ -70,11 +70,8 @@ public class JsonDumpService extends InitDataService {
|
||||
@Value("${app.json.dump.archive.enabled:true}")
|
||||
private boolean _archiveEnabled;
|
||||
|
||||
public JsonDumpService(ObjectMapper mapper, UserRepository userRepository, RoleRepository roleRepository, TutorialRepository tutorialRepository) {
|
||||
_mapper = mapper;
|
||||
_userRepository = userRepository;
|
||||
_roleRepository = roleRepository;
|
||||
_tutorialRepository = tutorialRepository;
|
||||
public JsonDumpService() {
|
||||
|
||||
}
|
||||
|
||||
private static String getFileNameWithTimestamp(String prefix) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.jambotronGroup.jambotron.security;
|
||||
|
||||
import com.jambotronGroup.jambotron.initData.InitDataService;
|
||||
import com.jambotronGroup.jambotron.security.jwt.AuthEntryPointJwt;
|
||||
import com.jambotronGroup.jambotron.security.jwt.AuthTokenFilter;
|
||||
import com.jambotronGroup.jambotron.security.services.UserDetailsServiceImpl;
|
||||
|
||||
Reference in New Issue
Block a user