Created
September 27, 2024 14:16
-
-
Save shvyrev/129f3dc8c1aff772c6665f5b27ea99c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.bft.bftresend.business.zags.processors; | |
import lombok.RequiredArgsConstructor; | |
import lombok.extern.slf4j.Slf4j; | |
import org.apache.camel.Exchange; | |
import org.apache.camel.Processor; | |
import org.bft.bftresend.business.zags.model.ZagsProxy; | |
import org.bft.bftresend.services.ElectronicArchiveResource; | |
import org.bft.bftresend.services.MessageFormater; | |
import org.bft.bftresend.utils.MessageProcessor; | |
import org.springframework.stereotype.Component; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.UUID; | |
import java.util.function.Predicate; | |
import static java.nio.charset.StandardCharsets.UTF_8; | |
import static org.bft.bftresend.utils.Const.MESSAGE_ID_PREFIX; | |
/** | |
* Читает содержимое из файла лога. Находит идентификаторы файлов соответсвующее идентификатором сообщений. | |
*/ | |
@SuppressWarnings("unchecked") | |
@Slf4j | |
@Component | |
@RequiredArgsConstructor | |
public class ZagsReadFileByMessageIdProcessor implements Processor, MessageProcessor { | |
private final MessageFormater messageFormater; | |
private final ElectronicArchiveResource eaResource; | |
@Override | |
public void process(Exchange exchange) throws Exception { | |
List<UUID> messageIds = (List<UUID>)exchange.getProperty("messages", List.class); | |
List<ZagsProxy> proxyList = (List<ZagsProxy>)exchange.getProperty("proxies", List.class); | |
Long jobId = exchange.getProperty("jobId", Long.class); | |
var filePath = exchange.getIn().getBody().toString(); | |
Predicate<String> predicate = line -> proxyList.contains(getProxy(line)) && line.contains(MESSAGE_ID_PREFIX); | |
List<String> result = new ArrayList<>(); | |
try (var lines = Files.lines(Paths.get(filePath), UTF_8).filter(predicate)) { | |
lines.map(line -> getMessageLogInfo(jobId, line)) | |
.filter(info -> messageIds.contains(info.messageId())) | |
.forEach(messageLogInfo -> { | |
eaResource.loadFile(messageLogInfo.fileId(), fileModel -> { | |
String text = messageFormater.formatZagsForRabbit( | |
fileModel.getFileName(), messageLogInfo.messageId()); | |
result.add(text); | |
}); | |
}); | |
exchange.getIn().setBody(result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment