Skip to content

Commit 4642b99

Browse files
feat(view files of document): ui allows CDR and XML file view (#102)
* SAve changes * Remove stream * Remove imports
1 parent 748f056 commit 4642b99

File tree

15 files changed

+617
-304
lines changed

15 files changed

+617
-304
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.github.project.openubl.ublhub.builder;
18+
19+
import io.github.project.openubl.ublhub.idgenerator.IDGenerator;
20+
import io.github.project.openubl.ublhub.idgenerator.IDGeneratorType;
21+
import io.github.project.openubl.ublhub.idgenerator.IGGeneratorManager;
22+
import io.github.project.openubl.ublhub.idm.input.InputTemplateRepresentation;
23+
import io.github.project.openubl.ublhub.idm.input.KindRepresentation;
24+
import io.github.project.openubl.ublhub.idm.input.SpecRepresentation;
25+
import io.github.project.openubl.ublhub.models.jpa.entities.NamespaceEntity;
26+
import io.github.project.openubl.xmlbuilderlib.clock.SystemClock;
27+
import io.github.project.openubl.xmlbuilderlib.config.Config;
28+
import io.github.project.openubl.xmlbuilderlib.facade.DocumentManager;
29+
import io.github.project.openubl.xmlbuilderlib.models.input.standard.invoice.InvoiceInputModel;
30+
import io.github.project.openubl.xmlbuilderlib.models.input.standard.note.creditNote.CreditNoteInputModel;
31+
import io.github.project.openubl.xmlbuilderlib.models.input.standard.note.debitNote.DebitNoteInputModel;
32+
import io.github.project.openubl.xmlbuilderlib.models.input.sunat.SummaryDocumentInputModel;
33+
import io.github.project.openubl.xmlbuilderlib.models.input.sunat.VoidedDocumentInputModel;
34+
import io.smallrye.mutiny.Uni;
35+
import io.vertx.core.json.JsonObject;
36+
37+
import javax.enterprise.context.ApplicationScoped;
38+
import javax.inject.Inject;
39+
import java.util.Collections;
40+
import java.util.Map;
41+
42+
@ApplicationScoped
43+
public class XMLBuilderManager {
44+
45+
@Inject
46+
Config xBuilderConfig;
47+
48+
@Inject
49+
SystemClock xBuilderClock;
50+
51+
@Inject
52+
IGGeneratorManager igGeneratorManager;
53+
54+
public Uni<String> createXMLString(NamespaceEntity namespace, InputTemplateRepresentation inputTemplate, JsonObject document, boolean isPreview) {
55+
KindRepresentation kind = inputTemplate.getKind();
56+
SpecRepresentation spec = inputTemplate.getSpec();
57+
58+
IDGeneratorType idGeneratorType = IDGeneratorType.none;
59+
Map<String, String> idGeneratorConfig = Collections.emptyMap();
60+
if (spec.getIdGenerator() != null) {
61+
idGeneratorType = spec.getIdGenerator().getName();
62+
idGeneratorConfig = spec.getIdGenerator().getConfig();
63+
}
64+
65+
IDGenerator idGenerator = igGeneratorManager.selectIDGenerator(idGeneratorType);
66+
switch (kind) {
67+
case Invoice:
68+
InvoiceInputModel invoice = document.mapTo(InvoiceInputModel.class);
69+
70+
return idGenerator
71+
.enrichWithID(namespace, invoice, idGeneratorConfig, isPreview)
72+
.map(input -> DocumentManager.createXML(input, xBuilderConfig, xBuilderClock).getXml());
73+
case CreditNote:
74+
CreditNoteInputModel creditNote = document.mapTo(CreditNoteInputModel.class);
75+
return idGenerator
76+
.enrichWithID(namespace, creditNote, idGeneratorConfig, isPreview)
77+
.map(input -> DocumentManager.createXML(input, xBuilderConfig, xBuilderClock).getXml());
78+
case DebitNote:
79+
DebitNoteInputModel debitNote = document.mapTo(DebitNoteInputModel.class);
80+
return idGenerator
81+
.enrichWithID(namespace, debitNote, idGeneratorConfig, isPreview)
82+
.map(input -> DocumentManager.createXML(input, xBuilderConfig, xBuilderClock).getXml());
83+
case VoidedDocument:
84+
VoidedDocumentInputModel voidedDocument = document.mapTo(VoidedDocumentInputModel.class);
85+
return idGenerator
86+
.enrichWithID(namespace, voidedDocument, idGeneratorConfig, isPreview)
87+
.map(input -> DocumentManager.createXML(input, xBuilderConfig, xBuilderClock).getXml());
88+
case SummaryDocument:
89+
SummaryDocumentInputModel summaryDocument = document.mapTo(SummaryDocumentInputModel.class);
90+
return idGenerator
91+
.enrichWithID(namespace, summaryDocument, idGeneratorConfig, isPreview)
92+
.map(input -> DocumentManager.createXML(input, xBuilderConfig, xBuilderClock).getXml());
93+
default:
94+
throw new IllegalStateException("Kind:" + kind + " not supported");
95+
}
96+
}
97+
98+
}

src/main/java/io/github/project/openubl/ublhub/files/camel/FilesystemRoute.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import org.eclipse.microprofile.config.inject.ConfigProperty;
2222

2323
import javax.enterprise.context.ApplicationScoped;
24+
import java.io.ByteArrayInputStream;
2425
import java.nio.file.Files;
2526
import java.nio.file.Path;
2627
import java.nio.file.Paths;
28+
import java.util.Iterator;
2729
import java.util.UUID;
2830

2931
@ApplicationScoped
@@ -61,7 +63,11 @@ public void configure() throws Exception {
6163
})
6264
.choice()
6365
.when(header("shouldUnzip").isEqualTo(true))
64-
.unmarshal().zipFile()
66+
.unmarshal(RouteUtils.getZipFileDataFormat())
67+
.split(bodyAs(Iterator.class), (oldExchange, newExchange) -> newExchange)
68+
.streaming()
69+
.convertBodyTo(byte[].class)
70+
.end()
6571
.endChoice()
6672
.end();
6773

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.github.project.openubl.ublhub.files.camel;
18+
19+
import org.apache.camel.dataformat.zipfile.ZipFileDataFormat;
20+
21+
public class RouteUtils {
22+
23+
public static ZipFileDataFormat getZipFileDataFormat() {
24+
ZipFileDataFormat zipFile = new ZipFileDataFormat();
25+
zipFile.setUsingIterator(true);
26+
return zipFile;
27+
}
28+
29+
}

src/main/java/io/github/project/openubl/ublhub/files/camel/S3FilesRoute.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import javax.enterprise.context.ApplicationScoped;
3333
import java.net.URI;
34+
import java.util.Iterator;
3435
import java.util.Optional;
3536
import java.util.UUID;
3637

@@ -111,7 +112,11 @@ public void configure() throws Exception {
111112
.pollEnrich().simple("aws2-s3://" + s3Bucket + "?amazonS3Client=#s3client&deleteAfterRead=false&fileName=${body}")
112113
.setHeader("Content-Disposition", simple("$header.CamelAwsS3ContentDisposition"))
113114
.setHeader(Exchange.CONTENT_TYPE, simple("$header.CamelAwsS3ContentType"))
114-
.unmarshal().zipFile()
115+
.unmarshal(RouteUtils.getZipFileDataFormat())
116+
.split(bodyAs(Iterator.class), (oldExchange, newExchange) -> newExchange)
117+
.streaming()
118+
.convertBodyTo(byte[].class)
119+
.end()
115120
.endChoice()
116121
.otherwise()
117122
.pollEnrich().simple("aws2-s3://" + s3Bucket + "?amazonS3Client=#s3client&deleteAfterRead=false&fileName=${body}")
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.github.project.openubl.ublhub.resources;
18+
19+
/*
20+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
21+
* and other contributors as indicated by the @author tags.
22+
*
23+
* Licensed under the Eclipse Public License - v 2.0 (the "License");
24+
* you may not use this file except in compliance with the License.
25+
* You may obtain a copy of the License at
26+
*
27+
* https://www.eclipse.org/legal/epl-2.0/
28+
*
29+
* Unless required by applicable law or agreed to in writing, software
30+
* distributed under the License is distributed on an "AS IS" BASIS,
31+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32+
* See the License for the specific language governing permissions and
33+
* limitations under the License.
34+
*/
35+
36+
import io.github.project.openubl.ublhub.files.FilesMutiny;
37+
import io.github.project.openubl.ublhub.models.jpa.NamespaceRepository;
38+
import io.github.project.openubl.ublhub.models.jpa.UBLDocumentRepository;
39+
import io.quarkus.hibernate.reactive.panache.Panache;
40+
import io.smallrye.mutiny.Uni;
41+
42+
import javax.enterprise.context.ApplicationScoped;
43+
import javax.inject.Inject;
44+
import javax.validation.constraints.NotNull;
45+
import javax.ws.rs.*;
46+
import javax.ws.rs.core.HttpHeaders;
47+
import javax.ws.rs.core.MediaType;
48+
import javax.ws.rs.core.Response;
49+
50+
@Path("/namespaces")
51+
@Produces("application/json")
52+
@Consumes("application/json")
53+
@ApplicationScoped
54+
public class DocumentFileResource {
55+
56+
@Inject
57+
FilesMutiny filesMutiny;
58+
59+
@Inject
60+
NamespaceRepository namespaceRepository;
61+
62+
@Inject
63+
UBLDocumentRepository documentRepository;
64+
65+
@GET
66+
@Path("/{namespaceId}/document-files/{documentId}")
67+
@Produces({MediaType.TEXT_XML, MediaType.APPLICATION_OCTET_STREAM})
68+
public Uni<Response> getDocumentFile(
69+
@PathParam("namespaceId") @NotNull String namespaceId,
70+
@PathParam("documentId") @NotNull String documentId,
71+
@QueryParam("requestedFile") @DefaultValue("ubl") String requestedFile,
72+
@QueryParam("requestedFormat") @DefaultValue("zip") String requestedFormat
73+
) {
74+
return Panache
75+
.withTransaction(() -> namespaceRepository.findById(namespaceId)
76+
.onItem().ifNotNull().transformToUni(namespaceEntity -> documentRepository.findById(namespaceEntity, documentId))
77+
)
78+
.onItem().ifNotNull().transformToUni(documentEntity -> {
79+
String fileId;
80+
if (requestedFile.equals("ubl")) {
81+
fileId = documentEntity.storageFile;
82+
} else {
83+
fileId = documentEntity.storageCdr;
84+
}
85+
86+
boolean isZipFormatRequested = requestedFormat.equals("zip");
87+
88+
Uni<byte[]> bytesUni;
89+
if (isZipFormatRequested) {
90+
bytesUni = filesMutiny.getFileAsBytesWithoutUnzipping(fileId);
91+
} else {
92+
bytesUni = filesMutiny.getFileAsBytesAfterUnzip(fileId);
93+
}
94+
95+
return bytesUni.map(bytes -> Response.ok(bytes, isZipFormatRequested ? "application/zip" : MediaType.APPLICATION_XML)
96+
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + documentEntity.documentID + (isZipFormatRequested ? ".zip" : ".xml") + "\"")
97+
.build()
98+
);
99+
}
100+
)
101+
.onItem().ifNull().continueWith(() -> Response.status(Response.Status.NOT_FOUND).build());
102+
}
103+
104+
}
105+
106+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.github.project.openubl.ublhub.resources;
18+
19+
/*
20+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
21+
* and other contributors as indicated by the @author tags.
22+
*
23+
* Licensed under the Eclipse Public License - v 2.0 (the "License");
24+
* you may not use this file except in compliance with the License.
25+
* You may obtain a copy of the License at
26+
*
27+
* https://www.eclipse.org/legal/epl-2.0/
28+
*
29+
* Unless required by applicable law or agreed to in writing, software
30+
* distributed under the License is distributed on an "AS IS" BASIS,
31+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32+
* See the License for the specific language governing permissions and
33+
* limitations under the License.
34+
*/
35+
36+
import io.github.project.openubl.ublhub.builder.XMLBuilderManager;
37+
import io.github.project.openubl.ublhub.idm.input.InputTemplateRepresentation;
38+
import io.github.project.openubl.ublhub.models.jpa.NamespaceRepository;
39+
import io.quarkus.hibernate.reactive.panache.Panache;
40+
import io.smallrye.mutiny.Uni;
41+
import io.vertx.core.json.JsonObject;
42+
43+
import javax.enterprise.context.ApplicationScoped;
44+
import javax.inject.Inject;
45+
import javax.validation.ConstraintViolationException;
46+
import javax.validation.constraints.NotNull;
47+
import javax.ws.rs.*;
48+
import javax.ws.rs.core.Response;
49+
50+
@Path("/namespaces")
51+
@Produces("application/json")
52+
@Consumes("application/json")
53+
@ApplicationScoped
54+
public class DocumentPreviewResource {
55+
56+
@Inject
57+
NamespaceRepository namespaceRepository;
58+
59+
@Inject
60+
XMLBuilderManager xmlBuilderManager;
61+
62+
@POST
63+
@Path("/{namespaceId}/document-preview")
64+
public Uni<Response> createDocumentPreview(
65+
@PathParam("namespaceId") @NotNull String namespaceId,
66+
@NotNull JsonObject jsonObject
67+
) {
68+
InputTemplateRepresentation inputTemplate = jsonObject.mapTo(InputTemplateRepresentation.class);
69+
JsonObject documentJsonObject = jsonObject.getJsonObject("spec").getJsonObject("document");
70+
return Panache
71+
.withTransaction(() -> namespaceRepository.findById(namespaceId)
72+
.onItem().ifNotNull().transformToUni(namespaceEntity -> xmlBuilderManager.createXMLString(namespaceEntity, inputTemplate, documentJsonObject, true)
73+
// Response
74+
.map(xmlString -> Response
75+
.status(Response.Status.OK)
76+
.entity(xmlString)
77+
.build()
78+
)
79+
)
80+
81+
.onItem().ifNull().continueWith(Response.ok()
82+
.status(Response.Status.NOT_FOUND)::build
83+
)
84+
85+
.onFailure(throwable -> throwable instanceof ConstraintViolationException).recoverWithItem(throwable -> Response
86+
.status(Response.Status.BAD_REQUEST)
87+
.entity(throwable.getMessage()).build()
88+
)
89+
);
90+
}
91+
92+
}
93+
94+

0 commit comments

Comments
 (0)