-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(view files of document): ui allows CDR and XML file view (#102)
* SAve changes * Remove stream * Remove imports
- Loading branch information
1 parent
748f056
commit 4642b99
Showing
15 changed files
with
617 additions
and
304 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
src/main/java/io/github/project/openubl/ublhub/builder/XMLBuilderManager.java
This file contains 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
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"); | ||
} | ||
} | ||
|
||
} |
This file contains 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
29 changes: 29 additions & 0 deletions
29
src/main/java/io/github/project/openubl/ublhub/files/camel/RouteUtils.java
This file contains 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
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; | ||
} | ||
|
||
} |
This file contains 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
106 changes: 106 additions & 0 deletions
106
src/main/java/io/github/project/openubl/ublhub/resources/DocumentFileResource.java
This file contains 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
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()); | ||
} | ||
|
||
} | ||
|
||
|
94 changes: 94 additions & 0 deletions
94
src/main/java/io/github/project/openubl/ublhub/resources/DocumentPreviewResource.java
This file contains 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
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() | ||
) | ||
); | ||
} | ||
|
||
} | ||
|
||
|
Oops, something went wrong.