Skip to content

Commit

Permalink
feat(view files of document): ui allows CDR and XML file view (#102)
Browse files Browse the repository at this point in the history
* SAve changes

* Remove stream

* Remove imports
  • Loading branch information
carlosthe19916 committed Jan 6, 2022
1 parent 748f056 commit 4642b99
Show file tree
Hide file tree
Showing 15 changed files with 617 additions and 304 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.ublhub.builder;

import io.github.project.openubl.ublhub.idgenerator.IDGenerator;
import io.github.project.openubl.ublhub.idgenerator.IDGeneratorType;
import io.github.project.openubl.ublhub.idgenerator.IGGeneratorManager;
import io.github.project.openubl.ublhub.idm.input.InputTemplateRepresentation;
import io.github.project.openubl.ublhub.idm.input.KindRepresentation;
import io.github.project.openubl.ublhub.idm.input.SpecRepresentation;
import io.github.project.openubl.ublhub.models.jpa.entities.NamespaceEntity;
import io.github.project.openubl.xmlbuilderlib.clock.SystemClock;
import io.github.project.openubl.xmlbuilderlib.config.Config;
import io.github.project.openubl.xmlbuilderlib.facade.DocumentManager;
import io.github.project.openubl.xmlbuilderlib.models.input.standard.invoice.InvoiceInputModel;
import io.github.project.openubl.xmlbuilderlib.models.input.standard.note.creditNote.CreditNoteInputModel;
import io.github.project.openubl.xmlbuilderlib.models.input.standard.note.debitNote.DebitNoteInputModel;
import io.github.project.openubl.xmlbuilderlib.models.input.sunat.SummaryDocumentInputModel;
import io.github.project.openubl.xmlbuilderlib.models.input.sunat.VoidedDocumentInputModel;
import io.smallrye.mutiny.Uni;
import io.vertx.core.json.JsonObject;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.Collections;
import java.util.Map;

@ApplicationScoped
public class XMLBuilderManager {

@Inject
Config xBuilderConfig;

@Inject
SystemClock xBuilderClock;

@Inject
IGGeneratorManager igGeneratorManager;

public Uni<String> createXMLString(NamespaceEntity namespace, InputTemplateRepresentation inputTemplate, JsonObject document, boolean isPreview) {
KindRepresentation kind = inputTemplate.getKind();
SpecRepresentation spec = inputTemplate.getSpec();

IDGeneratorType idGeneratorType = IDGeneratorType.none;
Map<String, String> idGeneratorConfig = Collections.emptyMap();
if (spec.getIdGenerator() != null) {
idGeneratorType = spec.getIdGenerator().getName();
idGeneratorConfig = spec.getIdGenerator().getConfig();
}

IDGenerator idGenerator = igGeneratorManager.selectIDGenerator(idGeneratorType);
switch (kind) {
case Invoice:
InvoiceInputModel invoice = document.mapTo(InvoiceInputModel.class);

return idGenerator
.enrichWithID(namespace, invoice, idGeneratorConfig, isPreview)
.map(input -> DocumentManager.createXML(input, xBuilderConfig, xBuilderClock).getXml());
case CreditNote:
CreditNoteInputModel creditNote = document.mapTo(CreditNoteInputModel.class);
return idGenerator
.enrichWithID(namespace, creditNote, idGeneratorConfig, isPreview)
.map(input -> DocumentManager.createXML(input, xBuilderConfig, xBuilderClock).getXml());
case DebitNote:
DebitNoteInputModel debitNote = document.mapTo(DebitNoteInputModel.class);
return idGenerator
.enrichWithID(namespace, debitNote, idGeneratorConfig, isPreview)
.map(input -> DocumentManager.createXML(input, xBuilderConfig, xBuilderClock).getXml());
case VoidedDocument:
VoidedDocumentInputModel voidedDocument = document.mapTo(VoidedDocumentInputModel.class);
return idGenerator
.enrichWithID(namespace, voidedDocument, idGeneratorConfig, isPreview)
.map(input -> DocumentManager.createXML(input, xBuilderConfig, xBuilderClock).getXml());
case SummaryDocument:
SummaryDocumentInputModel summaryDocument = document.mapTo(SummaryDocumentInputModel.class);
return idGenerator
.enrichWithID(namespace, summaryDocument, idGeneratorConfig, isPreview)
.map(input -> DocumentManager.createXML(input, xBuilderConfig, xBuilderClock).getXml());
default:
throw new IllegalStateException("Kind:" + kind + " not supported");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.enterprise.context.ApplicationScoped;
import java.io.ByteArrayInputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.UUID;

@ApplicationScoped
Expand Down Expand Up @@ -61,7 +63,11 @@ public void configure() throws Exception {
})
.choice()
.when(header("shouldUnzip").isEqualTo(true))
.unmarshal().zipFile()
.unmarshal(RouteUtils.getZipFileDataFormat())
.split(bodyAs(Iterator.class), (oldExchange, newExchange) -> newExchange)
.streaming()
.convertBodyTo(byte[].class)
.end()
.endChoice()
.end();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.ublhub.files.camel;

import org.apache.camel.dataformat.zipfile.ZipFileDataFormat;

public class RouteUtils {

public static ZipFileDataFormat getZipFileDataFormat() {
ZipFileDataFormat zipFile = new ZipFileDataFormat();
zipFile.setUsingIterator(true);
return zipFile;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import javax.enterprise.context.ApplicationScoped;
import java.net.URI;
import java.util.Iterator;
import java.util.Optional;
import java.util.UUID;

Expand Down Expand Up @@ -111,7 +112,11 @@ public void configure() throws Exception {
.pollEnrich().simple("aws2-s3://" + s3Bucket + "?amazonS3Client=#s3client&deleteAfterRead=false&fileName=${body}")
.setHeader("Content-Disposition", simple("$header.CamelAwsS3ContentDisposition"))
.setHeader(Exchange.CONTENT_TYPE, simple("$header.CamelAwsS3ContentType"))
.unmarshal().zipFile()
.unmarshal(RouteUtils.getZipFileDataFormat())
.split(bodyAs(Iterator.class), (oldExchange, newExchange) -> newExchange)
.streaming()
.convertBodyTo(byte[].class)
.end()
.endChoice()
.otherwise()
.pollEnrich().simple("aws2-s3://" + s3Bucket + "?amazonS3Client=#s3client&deleteAfterRead=false&fileName=${body}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.ublhub.resources;

/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Eclipse Public License - v 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.eclipse.org/legal/epl-2.0/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import io.github.project.openubl.ublhub.files.FilesMutiny;
import io.github.project.openubl.ublhub.models.jpa.NamespaceRepository;
import io.github.project.openubl.ublhub.models.jpa.UBLDocumentRepository;
import io.quarkus.hibernate.reactive.panache.Panache;
import io.smallrye.mutiny.Uni;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import javax.ws.rs.*;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/namespaces")
@Produces("application/json")
@Consumes("application/json")
@ApplicationScoped
public class DocumentFileResource {

@Inject
FilesMutiny filesMutiny;

@Inject
NamespaceRepository namespaceRepository;

@Inject
UBLDocumentRepository documentRepository;

@GET
@Path("/{namespaceId}/document-files/{documentId}")
@Produces({MediaType.TEXT_XML, MediaType.APPLICATION_OCTET_STREAM})
public Uni<Response> getDocumentFile(
@PathParam("namespaceId") @NotNull String namespaceId,
@PathParam("documentId") @NotNull String documentId,
@QueryParam("requestedFile") @DefaultValue("ubl") String requestedFile,
@QueryParam("requestedFormat") @DefaultValue("zip") String requestedFormat
) {
return Panache
.withTransaction(() -> namespaceRepository.findById(namespaceId)
.onItem().ifNotNull().transformToUni(namespaceEntity -> documentRepository.findById(namespaceEntity, documentId))
)
.onItem().ifNotNull().transformToUni(documentEntity -> {
String fileId;
if (requestedFile.equals("ubl")) {
fileId = documentEntity.storageFile;
} else {
fileId = documentEntity.storageCdr;
}

boolean isZipFormatRequested = requestedFormat.equals("zip");

Uni<byte[]> bytesUni;
if (isZipFormatRequested) {
bytesUni = filesMutiny.getFileAsBytesWithoutUnzipping(fileId);
} else {
bytesUni = filesMutiny.getFileAsBytesAfterUnzip(fileId);
}

return bytesUni.map(bytes -> Response.ok(bytes, isZipFormatRequested ? "application/zip" : MediaType.APPLICATION_XML)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + documentEntity.documentID + (isZipFormatRequested ? ".zip" : ".xml") + "\"")
.build()
);
}
)
.onItem().ifNull().continueWith(() -> Response.status(Response.Status.NOT_FOUND).build());
}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.ublhub.resources;

/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Eclipse Public License - v 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.eclipse.org/legal/epl-2.0/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import io.github.project.openubl.ublhub.builder.XMLBuilderManager;
import io.github.project.openubl.ublhub.idm.input.InputTemplateRepresentation;
import io.github.project.openubl.ublhub.models.jpa.NamespaceRepository;
import io.quarkus.hibernate.reactive.panache.Panache;
import io.smallrye.mutiny.Uni;
import io.vertx.core.json.JsonObject;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.validation.ConstraintViolationException;
import javax.validation.constraints.NotNull;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;

@Path("/namespaces")
@Produces("application/json")
@Consumes("application/json")
@ApplicationScoped
public class DocumentPreviewResource {

@Inject
NamespaceRepository namespaceRepository;

@Inject
XMLBuilderManager xmlBuilderManager;

@POST
@Path("/{namespaceId}/document-preview")
public Uni<Response> createDocumentPreview(
@PathParam("namespaceId") @NotNull String namespaceId,
@NotNull JsonObject jsonObject
) {
InputTemplateRepresentation inputTemplate = jsonObject.mapTo(InputTemplateRepresentation.class);
JsonObject documentJsonObject = jsonObject.getJsonObject("spec").getJsonObject("document");
return Panache
.withTransaction(() -> namespaceRepository.findById(namespaceId)
.onItem().ifNotNull().transformToUni(namespaceEntity -> xmlBuilderManager.createXMLString(namespaceEntity, inputTemplate, documentJsonObject, true)
// Response
.map(xmlString -> Response
.status(Response.Status.OK)
.entity(xmlString)
.build()
)
)

.onItem().ifNull().continueWith(Response.ok()
.status(Response.Status.NOT_FOUND)::build
)

.onFailure(throwable -> throwable instanceof ConstraintViolationException).recoverWithItem(throwable -> Response
.status(Response.Status.BAD_REQUEST)
.entity(throwable.getMessage()).build()
)
);
}

}


Loading

0 comments on commit 4642b99

Please sign in to comment.