hit-server-manager/backend/src/main/java/com/hitcommunications/servermanager/services/ServersService.java

180 lines
6.3 KiB
Java
Raw Normal View History

2025-12-15 21:02:52 +00:00
package com.hitcommunications.servermanager.services;
import com.hitcommunications.servermanager.mappers.ServersMapper;
import com.hitcommunications.servermanager.model.Servers;
import com.hitcommunications.servermanager.model.dtos.BulkServerImportResponse;
import com.hitcommunications.servermanager.model.dtos.BulkServerImportResponse.FailedRow;
2025-12-15 21:02:52 +00:00
import com.hitcommunications.servermanager.model.dtos.NewServerDTO;
import com.hitcommunications.servermanager.model.dtos.ServerDTO;
import com.hitcommunications.servermanager.model.enums.Applications;
import com.hitcommunications.servermanager.model.enums.DatabaseType;
2025-12-15 21:02:52 +00:00
import com.hitcommunications.servermanager.model.enums.ServersType;
import com.hitcommunications.servermanager.repositories.ServersRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
2025-12-15 21:02:52 +00:00
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
2025-12-15 21:02:52 +00:00
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
2025-12-15 21:02:52 +00:00
@Service
@RequiredArgsConstructor
public class ServersService {
private final ServersMapper mapper;
private final ServersRepository repo;
public ServerDTO create(NewServerDTO createDTO) {
Servers entity = mapper.toEntity(createDTO);
entity = repo.save(entity);
return mapper.toDTO(entity);
}
public ServerDTO getById(String id) {
return repo.findById(id)
.map(mapper::toDTO)
.orElseThrow(() -> new RuntimeException("Server not found with id: " + id));
}
public ServerDTO getByName(String name) {
return repo.findByName(name)
.map(mapper::toDTO)
.orElseThrow(() -> new RuntimeException("Server not found with name: " + name));
}
public List<ServerDTO> getByType(ServersType type) {
return repo.findByType(type)
.stream()
.map(mapper::toDTO)
.toList();
}
public Map<ServersType, Integer> countAllByType() {
Map<ServersType, Integer> response = new HashMap<>();
for(ServersType type : ServersType.values()) {
response.put(type, repo.countAllByType(type));
}
return response;
}
2025-12-15 21:02:52 +00:00
public List<ServerDTO> getByApplication(Applications application) {
return repo.findByApplication(application)
.stream()
.map(mapper::toDTO)
.toList();
}
public BulkServerImportResponse bulkCreate(MultipartFile file) {
List<FailedRow> failures = new ArrayList<>();
int succeeded = 0;
int processed = 0;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8))) {
String line;
int lineNumber = 0;
while ((line = reader.readLine()) != null) {
lineNumber++;
if (line.isBlank()) {
continue;
}
String[] columns = parseColumns(line);
if (isHeaderRow(columns)) {
continue;
}
processed++;
try {
NewServerDTO dto = toNewServerDTO(columns);
create(dto);
succeeded++;
} catch (Exception ex) {
failures.add(new FailedRow(lineNumber, ex.getMessage(), line));
}
}
} catch (IOException e) {
throw new RuntimeException("Erro ao ler arquivo CSV.", e);
}
int failed = failures.size();
return new BulkServerImportResponse(processed, succeeded, failed, failures);
}
private String[] parseColumns(String line) {
String[] rawColumns = line.split(";");
String[] columns = new String[rawColumns.length];
for (int i = 0; i < rawColumns.length; i++) {
columns[i] = rawColumns[i].trim();
}
return columns;
}
private NewServerDTO toNewServerDTO(String[] columns) {
if (columns.length < 8) {
throw new IllegalArgumentException("Linha incompleta. Esperado 8 colunas.");
}
String name = columns[0];
String ip = columns[1];
Integer port = Integer.parseInt(columns[2]);
String user = columns[3];
String password = columns[4];
ServersType type = ServersType.valueOf(columns[5].toUpperCase());
Applications application = Applications.valueOf(columns[6].toUpperCase());
DatabaseType dbType = DatabaseType.valueOf(columns[7].toUpperCase());
return new NewServerDTO(name, ip, port, user, password, type, application, dbType);
}
private boolean isHeaderRow(String[] columns) {
if (columns.length < 8) {
return false;
}
return "name".equalsIgnoreCase(columns[0]) &&
"ip".equalsIgnoreCase(columns[1]) &&
"port".equalsIgnoreCase(columns[2]);
}
2025-12-15 21:02:52 +00:00
public List<ServerDTO> getAll() {
return repo.findAll()
.stream()
.map(mapper::toDTO)
.toList();
}
public List<ServerDTO> search(String query, ServersType type, Applications application, DatabaseType dbType) {
String normalizedQuery = (query == null || query.isBlank()) ? null : query.trim();
String typeFilter = type != null ? type.name() : null;
String applicationFilter = application != null ? application.name() : null;
String dbTypeFilter = dbType != null ? dbType.name() : null;
return repo.search(normalizedQuery, typeFilter, applicationFilter, dbTypeFilter)
.stream()
.map(mapper::toDTO)
.toList();
}
2025-12-15 21:02:52 +00:00
public ServerDTO update(String id, NewServerDTO updateDTO) {
Servers entity = repo.findById(id)
.orElseThrow(() -> new RuntimeException("Server not found with id: " + id));
mapper.partialUpdate(updateDTO, entity);
entity = repo.save(entity);
return mapper.toDTO(entity);
}
public void delete(String id) {
if (!repo.existsById(id)) {
throw new RuntimeException("Server not found with id: " + id);
}
repo.deleteById(id);
}
2025-12-15 21:02:52 +00:00
}