Skip to content

Commit 817a3bc

Browse files
Add filters (#27)
* Add filters * Add filters
1 parent 34ca794 commit 817a3bc

File tree

9 files changed

+149
-270
lines changed

9 files changed

+149
-270
lines changed

src/main/java/io/github/project/openubl/xsender/idm/DocumentRepresentation.java

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,13 @@ public class DocumentRepresentation {
2727
private String id;
2828
private Long createdOn;
2929
private boolean inProgress;
30-
31-
private int retries;
32-
private Date willRetryOn;
30+
private String error;
3331

3432
private Boolean fileContentValid;
3533
private String fileContentValidationError;
3634
private DocumentContentRepresentation fileContent;
3735

38-
private String sunatDeliveryStatus;
3936
private DocumentSunatStatusRepresentation sunat;
40-
private List<DocumentSunatEventRepresentation> sunatEvents;
4137

4238
public String getId() {
4339
return id;
@@ -63,20 +59,12 @@ public void setInProgress(boolean inProgress) {
6359
this.inProgress = inProgress;
6460
}
6561

66-
public int getRetries() {
67-
return retries;
68-
}
69-
70-
public void setRetries(int retries) {
71-
this.retries = retries;
62+
public String getError() {
63+
return error;
7264
}
7365

74-
public Date getWillRetryOn() {
75-
return willRetryOn;
76-
}
77-
78-
public void setWillRetryOn(Date willRetryOn) {
79-
this.willRetryOn = willRetryOn;
66+
public void setError(String error) {
67+
this.error = error;
8068
}
8169

8270
public Boolean getFileContentValid() {
@@ -103,27 +91,11 @@ public void setFileContent(DocumentContentRepresentation fileContent) {
10391
this.fileContent = fileContent;
10492
}
10593

106-
public String getSunatDeliveryStatus() {
107-
return sunatDeliveryStatus;
108-
}
109-
110-
public void setSunatDeliveryStatus(String sunatDeliveryStatus) {
111-
this.sunatDeliveryStatus = sunatDeliveryStatus;
112-
}
113-
11494
public DocumentSunatStatusRepresentation getSunat() {
11595
return sunat;
11696
}
11797

11898
public void setSunat(DocumentSunatStatusRepresentation sunat) {
11999
this.sunat = sunat;
120100
}
121-
122-
public List<DocumentSunatEventRepresentation> getSunatEvents() {
123-
return sunatEvents;
124-
}
125-
126-
public void setSunatEvents(List<DocumentSunatEventRepresentation> sunatEvents) {
127-
this.sunatEvents = sunatEvents;
128-
}
129101
}

src/main/java/io/github/project/openubl/xsender/kafka/consumers/DocumentEvents.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public class DocumentEvents {
6363

6464
static final int MAX_STRING = 250;
6565

66-
public static final String INVALID_FILE_MSG = "Not supported document type";
66+
public static final String INVALID_FILE_MSG = "Documento no soportado";
6767

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

7171
@Inject
7272
FilesManager filesManager;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 Eclipse Public License - v 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+
* https://www.eclipse.org/legal/epl-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.xsender.models;
18+
19+
public class DocumentFilterModel {
20+
21+
private String ruc;
22+
private String documentType;
23+
24+
public String getRuc() {
25+
return ruc;
26+
}
27+
28+
public void setRuc(String ruc) {
29+
this.ruc = ruc;
30+
}
31+
32+
public String getDocumentType() {
33+
return documentType;
34+
}
35+
36+
public void setDocumentType(String documentType) {
37+
this.documentType = documentType;
38+
}
39+
40+
public static final class DocumentFilterModelBuilder {
41+
private String ruc;
42+
private String documentType;
43+
44+
private DocumentFilterModelBuilder() {
45+
}
46+
47+
public static DocumentFilterModelBuilder aDocumentFilterModel() {
48+
return new DocumentFilterModelBuilder();
49+
}
50+
51+
public DocumentFilterModelBuilder withRuc(String ruc) {
52+
this.ruc = ruc;
53+
return this;
54+
}
55+
56+
public DocumentFilterModelBuilder withDocumentType(String documentType) {
57+
this.documentType = documentType;
58+
return this;
59+
}
60+
61+
public DocumentFilterModel build() {
62+
DocumentFilterModel documentFilterModel = new DocumentFilterModel();
63+
documentFilterModel.setRuc(ruc);
64+
documentFilterModel.setDocumentType(documentType);
65+
return documentFilterModel;
66+
}
67+
}
68+
}

src/main/java/io/github/project/openubl/xsender/models/jpa/UBLDocumentRepository.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*/
1717
package io.github.project.openubl.xsender.models.jpa;
1818

19+
import io.github.project.openubl.xsender.models.DocumentFilterModel;
1920
import io.github.project.openubl.xsender.models.PageBean;
2021
import io.github.project.openubl.xsender.models.PageModel;
2122
import io.github.project.openubl.xsender.models.SortBean;
2223
import io.github.project.openubl.xsender.models.jpa.entities.CompanyEntity;
24+
import io.github.project.openubl.xsender.models.jpa.entities.NamespaceEntity;
2325
import io.github.project.openubl.xsender.models.jpa.entities.UBLDocumentEntity;
2426
import io.quarkus.hibernate.orm.panache.PanacheQuery;
2527
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
@@ -40,34 +42,49 @@ public class UBLDocumentRepository implements PanacheRepositoryBase<UBLDocumentE
4042
// return list("deliveryStatus", DeliveryStatusType.COULD_NOT_BE_DELIVERED);
4143
// }
4244

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

49+
StringBuilder queryBuilder = new StringBuilder("From UBLDocumentEntity as c where c.namespace.id = :namespaceId");
50+
Parameters queryParameters = Parameters.with("namespaceId", namespace.getId());
51+
52+
if (filters.getRuc() != null) {
53+
queryBuilder.append(" and c.ruc = :ruc");
54+
queryParameters = queryParameters.and("ruc", filters.getRuc());
55+
}
56+
if (filters.getDocumentType() != null) {
57+
queryBuilder.append(" and c.documentType = :documentType");
58+
queryParameters = queryParameters.and("documentType", filters.getDocumentType());
59+
}
60+
4761
PanacheQuery<UBLDocumentEntity> query = CompanyEntity
48-
.find(
49-
"From UBLDocumentEntity as d where d.company =:company",
50-
sort,
51-
Parameters.with("company", company)
52-
)
62+
.find(queryBuilder.toString(), sort, queryParameters)
5363
.range(pageBean.getOffset(), pageBean.getOffset() + pageBean.getLimit() - 1);
5464

5565
long count = query.count();
5666
List<UBLDocumentEntity> list = query.list();
5767
return new PageModel<>(pageBean, count, list);
5868
}
5969

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

74+
StringBuilder queryBuilder = new StringBuilder("From UBLDocumentEntity as c where c.namespace.id = :namespaceId and lower(c.documentID) like :filterText");
75+
Parameters queryParameters = Parameters.with("namespaceId", namespace.getId()).and("filterText", filterText);
76+
77+
if (filters.getRuc() != null) {
78+
queryBuilder.append(" and c.ruc = :ruc");
79+
queryParameters = queryParameters.and("ruc", filters.getRuc());
80+
}
81+
if (filters.getDocumentType() != null) {
82+
queryBuilder.append(" and c.documentType = :documentType");
83+
queryParameters = queryParameters.and("documentType", filters.getDocumentType());
84+
}
85+
6486
PanacheQuery<UBLDocumentEntity> query = CompanyEntity
65-
.find(
66-
"From UBLDocumentEntity as d where d.company =:company and lower(d.documentID) like :filterText",
67-
sort,
68-
Parameters.with("company", company)
69-
.and("filterText", "%" + filterText.toLowerCase() + "%")
70-
)
87+
.find(queryBuilder.toString(), sort, queryParameters)
7188
.range(pageBean.getOffset(), pageBean.getOffset() + pageBean.getLimit() - 1);
7289

7390
long count = query.count();

src/main/java/io/github/project/openubl/xsender/models/utils/EntityToRepresentation.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ public static DocumentRepresentation toRepresentation(UBLDocumentEntity entity)
7878
rep.setInProgress(entity.isInProgress());
7979

8080
rep.setCreatedOn(entity.getCreatedOn().getTime());
81-
rep.setRetries(entity.getRetries());
82-
rep.setWillRetryOn(entity.getWillRetryOn());
81+
rep.setError(entity.getError());
8382

8483
// File
8584

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

9493
// Sunat
9594

96-
rep.setSunatDeliveryStatus(entity.getSunatStatus());
9795
rep.setSunat(new DocumentSunatStatusRepresentation());
9896

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

104102
// Events
105103

106-
List<DocumentSunatEventRepresentation> eventsRepresentation = entity.getSunatEvents().stream().map(f -> {
107-
DocumentSunatEventRepresentation e = new DocumentSunatEventRepresentation();
108-
e.setDescription(f.getDescription());
109-
e.setStatus(f.getStatus().toString());
110-
e.setCreatedOn(f.getCreatedOn().getTime());
111-
return e;
112-
}).collect(Collectors.toList());
113-
114-
rep.setSunatEvents(eventsRepresentation);
104+
// List<DocumentSunatEventRepresentation> eventsRepresentation = entity.getSunatEvents().stream().map(f -> {
105+
// DocumentSunatEventRepresentation e = new DocumentSunatEventRepresentation();
106+
// e.setDescription(f.getDescription());
107+
// e.setStatus(f.getStatus().toString());
108+
// e.setCreatedOn(f.getCreatedOn().getTime());
109+
// return e;
110+
// }).collect(Collectors.toList());
111+
//
112+
// rep.setSunatEvents(eventsRepresentation);
115113

116114
return rep;
117115
}

src/main/java/io/github/project/openubl/xsender/resources/DocumentResource.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,17 @@
3030
import io.github.project.openubl.xsender.kafka.producers.EventType;
3131
import io.github.project.openubl.xsender.kafka.utils.EventEntityToRepresentation;
3232
import io.github.project.openubl.xsender.managers.DocumentsManager;
33+
import io.github.project.openubl.xsender.models.DocumentFilterModel;
34+
import io.github.project.openubl.xsender.models.PageBean;
35+
import io.github.project.openubl.xsender.models.PageModel;
36+
import io.github.project.openubl.xsender.models.SortBean;
3337
import io.github.project.openubl.xsender.models.jpa.NamespaceRepository;
38+
import io.github.project.openubl.xsender.models.jpa.UBLDocumentRepository;
39+
import io.github.project.openubl.xsender.models.jpa.entities.CompanyEntity;
3440
import io.github.project.openubl.xsender.models.jpa.entities.NamespaceEntity;
3541
import io.github.project.openubl.xsender.models.jpa.entities.UBLDocumentEntity;
3642
import io.github.project.openubl.xsender.models.utils.EntityToRepresentation;
43+
import io.github.project.openubl.xsender.resources.utils.ResourceUtils;
3744
import io.github.project.openubl.xsender.security.UserIdentity;
3845
import org.apache.commons.io.IOUtils;
3946
import org.jboss.logging.Logger;
@@ -68,6 +75,9 @@ public class DocumentResource {
6875
@Inject
6976
NamespaceRepository namespaceRepository;
7077

78+
@Inject
79+
UBLDocumentRepository documentRepository;
80+
7181
@Inject
7282
DocumentsManager documentsManager;
7383

@@ -139,12 +149,31 @@ public Response uploadXML(
139149
@Path("/")
140150
public PageRepresentation<DocumentRepresentation> getDocuments(
141151
@PathParam("namespaceId") @NotNull String namespaceId,
152+
@QueryParam("ruc") String ruc,
153+
@QueryParam("documentType") String documentType,
142154
@QueryParam("filterText") String filterText,
143155
@QueryParam("offset") @DefaultValue("0") Integer offset,
144156
@QueryParam("limit") @DefaultValue("10") Integer limit,
145157
@QueryParam("sort_by") @DefaultValue("createdOn:desc") List<String> sortBy
146158
) {
147-
return null;
159+
NamespaceEntity namespaceEntity = namespaceRepository.findByIdAndOwner(namespaceId, userIdentity.getUsername()).orElseThrow(NotFoundException::new);
160+
161+
PageBean pageBean = ResourceUtils.getPageBean(offset, limit);
162+
List<SortBean> sortBeans = ResourceUtils.getSortBeans(sortBy, UBLDocumentRepository.SORT_BY_FIELDS);
163+
164+
DocumentFilterModel filters = DocumentFilterModel.DocumentFilterModelBuilder.aDocumentFilterModel()
165+
.withRuc(ruc)
166+
.withDocumentType(documentType)
167+
.build();
168+
169+
PageModel<UBLDocumentEntity> pageModel;
170+
if (filterText != null && !filterText.trim().isEmpty()) {
171+
pageModel = documentRepository.list(namespaceEntity, filterText, filters, pageBean, sortBeans);
172+
} else {
173+
pageModel = documentRepository.list(namespaceEntity, filters, pageBean, sortBeans);
174+
}
175+
176+
return EntityToRepresentation.toRepresentation(pageModel, EntityToRepresentation::toRepresentation);
148177
}
149178

150179

0 commit comments

Comments
 (0)