Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ public class DocumentRepresentation {
private String id;
private Long createdOn;
private boolean inProgress;

private int retries;
private Date willRetryOn;
private String error;

private Boolean fileContentValid;
private String fileContentValidationError;
private DocumentContentRepresentation fileContent;

private String sunatDeliveryStatus;
private DocumentSunatStatusRepresentation sunat;
private List<DocumentSunatEventRepresentation> sunatEvents;

public String getId() {
return id;
Expand All @@ -63,20 +59,12 @@ public void setInProgress(boolean inProgress) {
this.inProgress = inProgress;
}

public int getRetries() {
return retries;
}

public void setRetries(int retries) {
this.retries = retries;
public String getError() {
return error;
}

public Date getWillRetryOn() {
return willRetryOn;
}

public void setWillRetryOn(Date willRetryOn) {
this.willRetryOn = willRetryOn;
public void setError(String error) {
this.error = error;
}

public Boolean getFileContentValid() {
Expand All @@ -103,27 +91,11 @@ public void setFileContent(DocumentContentRepresentation fileContent) {
this.fileContent = fileContent;
}

public String getSunatDeliveryStatus() {
return sunatDeliveryStatus;
}

public void setSunatDeliveryStatus(String sunatDeliveryStatus) {
this.sunatDeliveryStatus = sunatDeliveryStatus;
}

public DocumentSunatStatusRepresentation getSunat() {
return sunat;
}

public void setSunat(DocumentSunatStatusRepresentation sunat) {
this.sunat = sunat;
}

public List<DocumentSunatEventRepresentation> getSunatEvents() {
return sunatEvents;
}

public void setSunatEvents(List<DocumentSunatEventRepresentation> sunatEvents) {
this.sunatEvents = sunatEvents;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public class DocumentEvents {

static final int MAX_STRING = 250;

public static final String INVALID_FILE_MSG = "Not supported document type";
public static final String INVALID_FILE_MSG = "Documento no soportado";

public static final String NS_NOT_FOUND = "Namespace not found";
public static final String RUC_IN_COMPANY_NOT_FOUND = "Could not find a company with RUC";
public static final String NS_NOT_FOUND = "Namespace no encontrado";
public static final String RUC_IN_COMPANY_NOT_FOUND = "No se pudo encontrar una empresa para el RUC especificado";

@Inject
FilesManager filesManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.
*/
package io.github.project.openubl.xsender.models;

public class DocumentFilterModel {

private String ruc;
private String documentType;

public String getRuc() {
return ruc;
}

public void setRuc(String ruc) {
this.ruc = ruc;
}

public String getDocumentType() {
return documentType;
}

public void setDocumentType(String documentType) {
this.documentType = documentType;
}

public static final class DocumentFilterModelBuilder {
private String ruc;
private String documentType;

private DocumentFilterModelBuilder() {
}

public static DocumentFilterModelBuilder aDocumentFilterModel() {
return new DocumentFilterModelBuilder();
}

public DocumentFilterModelBuilder withRuc(String ruc) {
this.ruc = ruc;
return this;
}

public DocumentFilterModelBuilder withDocumentType(String documentType) {
this.documentType = documentType;
return this;
}

public DocumentFilterModel build() {
DocumentFilterModel documentFilterModel = new DocumentFilterModel();
documentFilterModel.setRuc(ruc);
documentFilterModel.setDocumentType(documentType);
return documentFilterModel;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
*/
package io.github.project.openubl.xsender.models.jpa;

import io.github.project.openubl.xsender.models.DocumentFilterModel;
import io.github.project.openubl.xsender.models.PageBean;
import io.github.project.openubl.xsender.models.PageModel;
import io.github.project.openubl.xsender.models.SortBean;
import io.github.project.openubl.xsender.models.jpa.entities.CompanyEntity;
import io.github.project.openubl.xsender.models.jpa.entities.NamespaceEntity;
import io.github.project.openubl.xsender.models.jpa.entities.UBLDocumentEntity;
import io.quarkus.hibernate.orm.panache.PanacheQuery;
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
Expand All @@ -40,34 +42,49 @@ public class UBLDocumentRepository implements PanacheRepositoryBase<UBLDocumentE
// return list("deliveryStatus", DeliveryStatusType.COULD_NOT_BE_DELIVERED);
// }

public PageModel<UBLDocumentEntity> list(CompanyEntity company, PageBean pageBean, List<SortBean> sortBy) {
public PageModel<UBLDocumentEntity> list(NamespaceEntity namespace, DocumentFilterModel filters, PageBean pageBean, List<SortBean> sortBy) {
Sort sort = Sort.by();
sortBy.forEach(f -> sort.and(f.getFieldName(), f.isAsc() ? Sort.Direction.Ascending : Sort.Direction.Descending));

StringBuilder queryBuilder = new StringBuilder("From UBLDocumentEntity as c where c.namespace.id = :namespaceId");
Parameters queryParameters = Parameters.with("namespaceId", namespace.getId());

if (filters.getRuc() != null) {
queryBuilder.append(" and c.ruc = :ruc");
queryParameters = queryParameters.and("ruc", filters.getRuc());
}
if (filters.getDocumentType() != null) {
queryBuilder.append(" and c.documentType = :documentType");
queryParameters = queryParameters.and("documentType", filters.getDocumentType());
}

PanacheQuery<UBLDocumentEntity> query = CompanyEntity
.find(
"From UBLDocumentEntity as d where d.company =:company",
sort,
Parameters.with("company", company)
)
.find(queryBuilder.toString(), sort, queryParameters)
.range(pageBean.getOffset(), pageBean.getOffset() + pageBean.getLimit() - 1);

long count = query.count();
List<UBLDocumentEntity> list = query.list();
return new PageModel<>(pageBean, count, list);
}

public PageModel<UBLDocumentEntity> list(CompanyEntity company, String filterText, PageBean pageBean, List<SortBean> sortBy) {
public PageModel<UBLDocumentEntity> list(NamespaceEntity namespace, String filterText, DocumentFilterModel filters, PageBean pageBean, List<SortBean> sortBy) {
Sort sort = Sort.by();
sortBy.forEach(f -> sort.and(f.getFieldName(), f.isAsc() ? Sort.Direction.Ascending : Sort.Direction.Descending));

StringBuilder queryBuilder = new StringBuilder("From UBLDocumentEntity as c where c.namespace.id = :namespaceId and lower(c.documentID) like :filterText");
Parameters queryParameters = Parameters.with("namespaceId", namespace.getId()).and("filterText", filterText);

if (filters.getRuc() != null) {
queryBuilder.append(" and c.ruc = :ruc");
queryParameters = queryParameters.and("ruc", filters.getRuc());
}
if (filters.getDocumentType() != null) {
queryBuilder.append(" and c.documentType = :documentType");
queryParameters = queryParameters.and("documentType", filters.getDocumentType());
}

PanacheQuery<UBLDocumentEntity> query = CompanyEntity
.find(
"From UBLDocumentEntity as d where d.company =:company and lower(d.documentID) like :filterText",
sort,
Parameters.with("company", company)
.and("filterText", "%" + filterText.toLowerCase() + "%")
)
.find(queryBuilder.toString(), sort, queryParameters)
.range(pageBean.getOffset(), pageBean.getOffset() + pageBean.getLimit() - 1);

long count = query.count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public static DocumentRepresentation toRepresentation(UBLDocumentEntity entity)
rep.setInProgress(entity.isInProgress());

rep.setCreatedOn(entity.getCreatedOn().getTime());
rep.setRetries(entity.getRetries());
rep.setWillRetryOn(entity.getWillRetryOn());
rep.setError(entity.getError());

// File

Expand All @@ -93,7 +92,6 @@ public static DocumentRepresentation toRepresentation(UBLDocumentEntity entity)

// Sunat

rep.setSunatDeliveryStatus(entity.getSunatStatus());
rep.setSunat(new DocumentSunatStatusRepresentation());

rep.getSunat().setCode(entity.getSunatCode());
Expand All @@ -103,15 +101,15 @@ public static DocumentRepresentation toRepresentation(UBLDocumentEntity entity)

// Events

List<DocumentSunatEventRepresentation> eventsRepresentation = entity.getSunatEvents().stream().map(f -> {
DocumentSunatEventRepresentation e = new DocumentSunatEventRepresentation();
e.setDescription(f.getDescription());
e.setStatus(f.getStatus().toString());
e.setCreatedOn(f.getCreatedOn().getTime());
return e;
}).collect(Collectors.toList());

rep.setSunatEvents(eventsRepresentation);
// List<DocumentSunatEventRepresentation> eventsRepresentation = entity.getSunatEvents().stream().map(f -> {
// DocumentSunatEventRepresentation e = new DocumentSunatEventRepresentation();
// e.setDescription(f.getDescription());
// e.setStatus(f.getStatus().toString());
// e.setCreatedOn(f.getCreatedOn().getTime());
// return e;
// }).collect(Collectors.toList());
//
// rep.setSunatEvents(eventsRepresentation);

return rep;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@
import io.github.project.openubl.xsender.kafka.producers.EventType;
import io.github.project.openubl.xsender.kafka.utils.EventEntityToRepresentation;
import io.github.project.openubl.xsender.managers.DocumentsManager;
import io.github.project.openubl.xsender.models.DocumentFilterModel;
import io.github.project.openubl.xsender.models.PageBean;
import io.github.project.openubl.xsender.models.PageModel;
import io.github.project.openubl.xsender.models.SortBean;
import io.github.project.openubl.xsender.models.jpa.NamespaceRepository;
import io.github.project.openubl.xsender.models.jpa.UBLDocumentRepository;
import io.github.project.openubl.xsender.models.jpa.entities.CompanyEntity;
import io.github.project.openubl.xsender.models.jpa.entities.NamespaceEntity;
import io.github.project.openubl.xsender.models.jpa.entities.UBLDocumentEntity;
import io.github.project.openubl.xsender.models.utils.EntityToRepresentation;
import io.github.project.openubl.xsender.resources.utils.ResourceUtils;
import io.github.project.openubl.xsender.security.UserIdentity;
import org.apache.commons.io.IOUtils;
import org.jboss.logging.Logger;
Expand Down Expand Up @@ -68,6 +75,9 @@ public class DocumentResource {
@Inject
NamespaceRepository namespaceRepository;

@Inject
UBLDocumentRepository documentRepository;

@Inject
DocumentsManager documentsManager;

Expand Down Expand Up @@ -139,12 +149,31 @@ public Response uploadXML(
@Path("/")
public PageRepresentation<DocumentRepresentation> getDocuments(
@PathParam("namespaceId") @NotNull String namespaceId,
@QueryParam("ruc") String ruc,
@QueryParam("documentType") String documentType,
@QueryParam("filterText") String filterText,
@QueryParam("offset") @DefaultValue("0") Integer offset,
@QueryParam("limit") @DefaultValue("10") Integer limit,
@QueryParam("sort_by") @DefaultValue("createdOn:desc") List<String> sortBy
) {
return null;
NamespaceEntity namespaceEntity = namespaceRepository.findByIdAndOwner(namespaceId, userIdentity.getUsername()).orElseThrow(NotFoundException::new);

PageBean pageBean = ResourceUtils.getPageBean(offset, limit);
List<SortBean> sortBeans = ResourceUtils.getSortBeans(sortBy, UBLDocumentRepository.SORT_BY_FIELDS);

DocumentFilterModel filters = DocumentFilterModel.DocumentFilterModelBuilder.aDocumentFilterModel()
.withRuc(ruc)
.withDocumentType(documentType)
.build();

PageModel<UBLDocumentEntity> pageModel;
if (filterText != null && !filterText.trim().isEmpty()) {
pageModel = documentRepository.list(namespaceEntity, filterText, filters, pageBean, sortBeans);
} else {
pageModel = documentRepository.list(namespaceEntity, filters, pageBean, sortBeans);
}

return EntityToRepresentation.toRepresentation(pageModel, EntityToRepresentation::toRepresentation);
}


Expand Down
Loading