Skip to content
Snippets Groups Projects
Commit 38bec077 authored by Jan Zickermann's avatar Jan Zickermann
Browse files

OZG-5412 common: Resolve XTAHelper

parent 86940d16
Branches
Tags
No related merge requests found
......@@ -23,6 +23,7 @@
*/
package de.ozgcloud.eingang.intelliform;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
......@@ -32,29 +33,55 @@ import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import de.ozgcloud.common.binaryfile.TempFileUtils;
import de.ozgcloud.eingang.common.errorhandling.TechnicalException;
import de.ozgcloud.eingang.common.formdata.FormData;
import de.ozgcloud.eingang.common.formdata.IncomingFile;
import de.ozgcloud.eingang.common.formdata.IncomingFileGroup;
import de.ozgcloud.eingang.common.xml.XMLHelper;
import lombok.RequiredArgsConstructor;
@Component
@RequiredArgsConstructor
class DepositDataMapper {
// TODO Resolve code duplication (xta-adapter: de.ozgcloud.eingang.xdomea.XMLHelper)
private static final DocumentBuilder DOCUMENT_BUILDER = createDocumentBuilder();
private static DocumentBuilder createDocumentBuilder() {
var documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
return documentBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new TechnicalException("Failed to configure document builder", e);
}
}
public FormData mapToFormData(DepositData depositData) {
Map<String, IncomingFile> incomingFileMap = mapDepositAttachmentsToIncomingFiles(depositData);
Document document = parsePrimaryXmlRepresentation(depositData, incomingFileMap);
List<String> attachmentFileIds = findAttachmentFileIds(document);
var attachments = getAttachmentFileGroups(attachmentFileIds, incomingFileMap);
var representations = getRepresentations(incomingFileMap, attachmentFileIds);
return mapToFormDataWithRepresentationsAndAttachments(
getRepresentations(incomingFileMap, attachmentFileIds),
getAttachmentFileGroups(attachmentFileIds, incomingFileMap)
);
}
private FormData mapToFormDataWithRepresentationsAndAttachments(
List<IncomingFile> representations,
List<IncomingFileGroup> attachments
) {
return FormData.builder()
.attachments(attachments)
.numberOfAttachments(attachments.size())
......@@ -85,11 +112,19 @@ class DepositDataMapper {
private Document parsePrimaryXmlRepresentation(DepositData depositData, Map<String, IncomingFile> incomingFileMap) {
// Expect that the <primaryDataAttachmentId> refers to the XML file
return XMLHelper.parseDocument(
return parseDocument(
getIncomingFileById(depositData.getPrimaryDataAttachmentId(), incomingFileMap)
);
}
private static Document parseDocument(IncomingFile incomingFile) {
try (var inputStream = incomingFile.getContentStream()) {
return DOCUMENT_BUILDER.parse(inputStream);
} catch (SAXException | IOException e) {
throw new TechnicalException("Failed to parse xml document!", e);
}
}
private List<String> findAttachmentFileIds(Document document) {
return streamElements(document.getElementsByTagName("file"))
.map(element -> element.getAttribute("id"))
......
package de.ozgcloud.eingang.common.xml;
package de.ozgcloud.eingang.xdomea;
import java.io.IOException;
......
package de.ozgcloud.eingang.xdomea;
import static de.ozgcloud.eingang.common.xml.XMLHelper.*;
import static de.ozgcloud.eingang.xdomea.XMLHelper.*;
import java.util.List;
import java.util.stream.IntStream;
......@@ -17,7 +17,6 @@ import org.w3c.dom.NodeList;
import de.ozgcloud.eingang.common.errorhandling.TechnicalException;
import de.ozgcloud.eingang.common.formdata.IncomingFile;
import de.ozgcloud.eingang.common.xml.XMLHelper;
@Component
public class XdomeaXMLValueReader {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment