Skip to content

Commit b57e076

Browse files
Add enhancements (#21)
* Add enhancements * Fix license * upgrade testcontainer
1 parent b230f2c commit b57e076

File tree

9 files changed

+146
-16
lines changed

9 files changed

+146
-16
lines changed

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@
3939
<!--Quarkus-->
4040
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
4141
<quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
42-
<quarkus.platform.version>1.10.5.Final</quarkus.platform.version>
43-
<quarkus-plugin.version>1.10.5.Final</quarkus-plugin.version>
42+
<quarkus.platform.version>1.11.2.Final</quarkus.platform.version>
43+
<quarkus-plugin.version>1.11.2.Final</quarkus-plugin.version>
4444

4545
<!--plugings-->
4646
<compiler-plugin.version>3.8.1</compiler-plugin.version>
4747
<surefire-plugin.version>2.22.1</surefire-plugin.version>
4848

4949
<!--libs-->
5050
<xsender-lib.version>3.0.1.Final</xsender-lib.version>
51-
<testcontainers.version>1.12.5</testcontainers.version>
52-
<awaitility.version>3.0.0</awaitility.version>
51+
<testcontainers.version>1.15.2</testcontainers.version>
52+
<awaitility.version>4.0.3</awaitility.version>
5353
<keycloak.version>12.0.1</keycloak.version>
5454
<postgresql.version>13.1</postgresql.version>
5555

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class CompanyRepresentation {
3131
@NotNull
3232
private String name;
3333

34+
private String description;
35+
3436
@NotNull
3537
@Valid
3638
private SunatUrlsRepresentation webServices;
@@ -55,6 +57,14 @@ public void setName(String name) {
5557
this.name = name;
5658
}
5759

60+
public String getDescription() {
61+
return description;
62+
}
63+
64+
public void setDescription(String description) {
65+
this.description = description;
66+
}
67+
5868
public SunatUrlsRepresentation getWebServices() {
5969
return webServices;
6070
}
@@ -73,6 +83,7 @@ public void setCredentials(SunatCredentialsRepresentation credentials) {
7383

7484
public static final class Builder {
7585
private String name;
86+
private String description;
7687
private SunatUrlsRepresentation webServices;
7788
private SunatCredentialsRepresentation credentials;
7889

@@ -88,6 +99,11 @@ public Builder withName(String name) {
8899
return this;
89100
}
90101

102+
public Builder withDescription(String description) {
103+
this.description = description;
104+
return this;
105+
}
106+
91107
public Builder withWebServices(SunatUrlsRepresentation webServices) {
92108
this.webServices = webServices;
93109
return this;
@@ -101,6 +117,7 @@ public Builder withCredentials(SunatCredentialsRepresentation credentials) {
101117
public CompanyRepresentation build() {
102118
CompanyRepresentation companyRepresentation = new CompanyRepresentation();
103119
companyRepresentation.setName(name);
120+
companyRepresentation.setDescription(description);
104121
companyRepresentation.setWebServices(webServices);
105122
companyRepresentation.setCredentials(credentials);
106123
return companyRepresentation;

src/main/java/io/github/project/openubl/xsender/managers/CompanyManager.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public CompanyEntity createCompany(String owner, CompanyRepresentation rep) {
4545

4646
companyEntity.setOwner(owner);
4747
companyEntity.setName(rep.getName().toLowerCase());
48+
companyEntity.setDescription(rep.getDescription());
4849

4950
if (rep.getWebServices() != null) {
5051
SunatUrlsEntity sunatUrlsEntity = new SunatUrlsEntity();
@@ -67,16 +68,20 @@ public CompanyEntity createCompany(String owner, CompanyRepresentation rep) {
6768
/**
6869
* Shouldn't update 'name'
6970
*/
70-
public CompanyEntity updateCompany(CompanyRepresentation rep, CompanyEntity CompanyEntity) {
71+
public CompanyEntity updateCompany(CompanyRepresentation rep, CompanyEntity entity) {
72+
if (rep.getDescription() != null) {
73+
entity.setDescription(rep.getDescription());
74+
}
75+
7176
if (rep.getWebServices() != null) {
72-
if (CompanyEntity.getSunatUrls() == null) {
73-
CompanyEntity.setSunatUrls(new SunatUrlsEntity());
77+
if (entity.getSunatUrls() == null) {
78+
entity.setSunatUrls(new SunatUrlsEntity());
7479
}
75-
updateSunatUrls(rep.getWebServices(), CompanyEntity.getSunatUrls());
80+
updateSunatUrls(rep.getWebServices(), entity.getSunatUrls());
7681
}
7782

78-
companyRepository.persist(CompanyEntity);
79-
return CompanyEntity;
83+
companyRepository.persist(entity);
84+
return entity;
8085
}
8186

8287
private void updateSunatUrls(SunatUrlsRepresentation rep, SunatUrlsEntity entity) {

src/main/java/io/github/project/openubl/xsender/managers/DocumentsManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.io.ByteArrayInputStream;
4646
import java.io.IOException;
4747
import java.util.Optional;
48+
import java.util.UUID;
4849

4950
@Transactional
5051
@ApplicationScoped
@@ -96,6 +97,7 @@ public UBLDocumentEntity createDocumentAndScheduleDelivery(
9697

9798
// Create Entity in DB
9899
UBLDocumentEntity documentEntity = UBLDocumentEntity.Builder.anUBLDocumentEntity()
100+
.withId(UUID.randomUUID().toString())
99101
.withStorageFile(fileID)
100102
.withFilename(fileNameWithoutExtension)
101103
.withRuc(xmlContentModel.getRuc())

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,24 @@
1717
package io.github.project.openubl.xsender.models.jpa;
1818

1919
import io.github.project.openubl.xsender.models.DeliveryStatusType;
20+
import io.github.project.openubl.xsender.models.PageBean;
21+
import io.github.project.openubl.xsender.models.PageModel;
22+
import io.github.project.openubl.xsender.models.SortBean;
23+
import io.github.project.openubl.xsender.models.jpa.entities.CompanyEntity;
2024
import io.github.project.openubl.xsender.models.jpa.entities.UBLDocumentEntity;
25+
import io.quarkus.hibernate.orm.panache.PanacheQuery;
2126
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
27+
import io.quarkus.panache.common.Parameters;
28+
import io.quarkus.panache.common.Sort;
2229

2330
import javax.enterprise.context.ApplicationScoped;
2431
import java.util.List;
2532

2633
@ApplicationScoped
2734
public class UBLDocumentRepository implements PanacheRepositoryBase<UBLDocumentEntity, String> {
2835

36+
public static final String[] SORT_BY_FIELDS = {"documentID"};
37+
2938
public List<UBLDocumentEntity> findAllScheduledToDeliver() {
3039
return list("deliveryStatus", DeliveryStatusType.SCHEDULED_TO_DELIVER);
3140
}
@@ -34,4 +43,38 @@ public List<UBLDocumentEntity> findAllSheduledToCheckTicket() {
3443
return list("deliveryStatus", DeliveryStatusType.SCHEDULED_CHECK_TICKET);
3544
}
3645

46+
public PageModel<UBLDocumentEntity> list(CompanyEntity company, PageBean pageBean, List<SortBean> sortBy) {
47+
Sort sort = Sort.by();
48+
sortBy.forEach(f -> sort.and(f.getFieldName(), f.isAsc() ? Sort.Direction.Ascending : Sort.Direction.Descending));
49+
50+
PanacheQuery<UBLDocumentEntity> query = CompanyEntity
51+
.find(
52+
"From UBLDocumentEntity as d where d.company =:company",
53+
sort,
54+
Parameters.with("company", company)
55+
)
56+
.range(pageBean.getOffset(), pageBean.getOffset() + pageBean.getLimit() - 1);
57+
58+
long count = query.count();
59+
List<UBLDocumentEntity> list = query.list();
60+
return new PageModel<>(pageBean, count, list);
61+
}
62+
63+
public PageModel<UBLDocumentEntity> list(CompanyEntity company, String filterText, PageBean pageBean, List<SortBean> sortBy) {
64+
Sort sort = Sort.by();
65+
sortBy.forEach(f -> sort.and(f.getFieldName(), f.isAsc() ? Sort.Direction.Ascending : Sort.Direction.Descending));
66+
67+
PanacheQuery<UBLDocumentEntity> query = CompanyEntity
68+
.find(
69+
"From UBLDocumentEntity as d where d.company =:company and lower(d.documentID) like :filterText",
70+
sort,
71+
Parameters.with("company", company)
72+
.and("filterText", "%" + filterText.toLowerCase() + "%")
73+
)
74+
.range(pageBean.getOffset(), pageBean.getOffset() + pageBean.getLimit() - 1);
75+
76+
long count = query.count();
77+
List<UBLDocumentEntity> list = query.list();
78+
return new PageModel<>(pageBean, count, list);
79+
}
3780
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public class CompanyEntity extends PanacheEntityBase {
4444
@Column(name = "NAME")
4545
private String name;
4646

47+
private String description;
48+
4749
@NotNull
4850
@Valid
4951
@Embedded
@@ -82,6 +84,14 @@ public void setName(String name) {
8284
this.name = name;
8385
}
8486

87+
public String getDescription() {
88+
return description;
89+
}
90+
91+
public void setDescription(String description) {
92+
this.description = description;
93+
}
94+
8595
public SunatCredentialsEntity getSunatCredentials() {
8696
return sunatCredentials;
8797
}
@@ -123,6 +133,7 @@ public static final class Builder {
123133
private String id;
124134
private String owner;
125135
private String name;
136+
private String description;
126137
private SunatCredentialsEntity sunatCredentials;
127138
private SunatUrlsEntity sunatUrls;
128139
private int version;
@@ -149,6 +160,11 @@ public Builder withName(String name) {
149160
return this;
150161
}
151162

163+
public Builder withDescription(String description) {
164+
this.description = description;
165+
return this;
166+
}
167+
152168
public Builder withSunatCredentials(SunatCredentialsEntity sunatCredentials) {
153169
this.sunatCredentials = sunatCredentials;
154170
return this;
@@ -169,6 +185,7 @@ public CompanyEntity build() {
169185
companyEntity.setId(id);
170186
companyEntity.setOwner(owner);
171187
companyEntity.setName(name);
188+
companyEntity.setDescription(description);
172189
companyEntity.setSunatCredentials(sunatCredentials);
173190
companyEntity.setSunatUrls(sunatUrls);
174191
companyEntity.setVersion(version);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public static CompanyRepresentation toRepresentation(CompanyEntity entity) {
4040

4141
rep.setId(entity.getId());
4242
rep.setName(entity.getName());
43+
rep.setDescription(entity.getDescription());
4344

4445
if (entity.getSunatUrls() != null) {
4546
SunatUrlsRepresentation sunatUrlsRep = new SunatUrlsRepresentation();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public interface CompanyResource {
4343
/**
4444
* Update an org
4545
*/
46-
@PATCH
46+
@PUT
4747
@Path("/{company}")
4848
CompanyRepresentation updateCompany(
4949
@PathParam("company") @NotNull String company,
50-
@NotNull @Valid CompanyRepresentation rep
50+
@NotNull CompanyRepresentation rep
5151
);
5252

5353
@DELETE
@@ -57,7 +57,7 @@ CompanyRepresentation updateCompany(
5757
/**
5858
* Change SUNAT credentials of a company
5959
*/
60-
@PATCH
60+
@PUT
6161
@Path("/{company}/sunat-credentials")
6262
void updateCompanySUNATCredentials(
6363
@PathParam("company") @NotNull String company,

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

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@
2323
import io.github.project.openubl.xsender.idm.*;
2424
import io.github.project.openubl.xsender.managers.CompanyManager;
2525
import io.github.project.openubl.xsender.managers.DocumentsManager;
26+
import io.github.project.openubl.xsender.models.ContextBean;
27+
import io.github.project.openubl.xsender.models.PageBean;
28+
import io.github.project.openubl.xsender.models.PageModel;
29+
import io.github.project.openubl.xsender.models.SortBean;
2630
import io.github.project.openubl.xsender.models.jpa.CompanyRepository;
2731
import io.github.project.openubl.xsender.models.jpa.UBLDocumentRepository;
2832
import io.github.project.openubl.xsender.models.jpa.entities.CompanyEntity;
2933
import io.github.project.openubl.xsender.models.jpa.entities.UBLDocumentEntity;
3034
import io.github.project.openubl.xsender.models.utils.EntityToRepresentation;
35+
import io.github.project.openubl.xsender.resources.utils.ResourceUtils;
3136
import io.github.project.openubl.xsender.security.UserIdentity;
3237
import org.apache.commons.io.IOUtils;
38+
import org.apache.http.NameValuePair;
39+
import org.apache.http.message.BasicNameValuePair;
3340
import org.jboss.logging.Logger;
3441
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
3542
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
@@ -39,11 +46,15 @@
3946
import javax.transaction.Transactional;
4047
import javax.validation.constraints.NotNull;
4148
import javax.ws.rs.BadRequestException;
49+
import javax.ws.rs.InternalServerErrorException;
4250
import javax.ws.rs.NotFoundException;
51+
import javax.ws.rs.core.Context;
4352
import javax.ws.rs.core.HttpHeaders;
4453
import javax.ws.rs.core.Response;
54+
import javax.ws.rs.core.UriInfo;
4555
import java.io.IOException;
4656
import java.io.InputStream;
57+
import java.net.URISyntaxException;
4758
import java.util.List;
4859
import java.util.Map;
4960

@@ -53,6 +64,9 @@ public class DefaultCompanyResource implements CompanyResource {
5364

5465
private static final Logger LOG = Logger.getLogger(DefaultCompanyResource.class);
5566

67+
@Context
68+
UriInfo uriInfo;
69+
5670
@Inject
5771
CompanyRepository companyRepository;
5872

@@ -84,8 +98,8 @@ public CompanyRepresentation updateCompany(String company, CompanyRepresentation
8498

8599
@Override
86100
public void deleteCompany(@NotNull String company) {
87-
CompanyEntity organizationEntity = companyRepository.findByNameAndOwner(company, userIdentity.getUsername()).orElseThrow(NotFoundException::new);
88-
companyRepository.deleteById(organizationEntity.getId());
101+
CompanyEntity companyEntity = companyRepository.findByNameAndOwner(company, userIdentity.getUsername()).orElseThrow(NotFoundException::new);
102+
companyRepository.deleteById(companyEntity.getId());
89103
}
90104

91105
public void updateCompanySUNATCredentials(String org, SunatCredentialsRepresentation rep) {
@@ -95,7 +109,38 @@ public void updateCompanySUNATCredentials(String org, SunatCredentialsRepresenta
95109

96110
@Override
97111
public PageRepresentation<DocumentRepresentation> listDocuments(@NotNull String company, String filterText, Integer offset, Integer limit, List<String> sortBy) {
98-
return null;
112+
CompanyEntity companyEntity = companyRepository.findByNameAndOwner(company, userIdentity.getUsername()).orElseThrow(NotFoundException::new);
113+
114+
ContextBean contextBean = ContextBean.Builder.aContextBean()
115+
.withUsername(userIdentity.getUsername())
116+
.withUriInfo(uriInfo)
117+
.build();
118+
119+
PageBean pageBean = ResourceUtils.getPageBean(offset, limit);
120+
List<SortBean> sortBeans = ResourceUtils.getSortBeans(sortBy, UBLDocumentRepository.SORT_BY_FIELDS);
121+
122+
PageModel<UBLDocumentEntity> pageModel;
123+
if (filterText != null && !filterText.trim().isEmpty()) {
124+
pageModel = documentRepository.list(companyEntity, filterText, pageBean, sortBeans);
125+
} else {
126+
pageModel = documentRepository.list(companyEntity, pageBean, sortBeans);
127+
}
128+
129+
List<NameValuePair> queryParameters = ResourceUtils.buildNameValuePairs(offset, limit, sortBeans);
130+
if (filterText != null) {
131+
queryParameters.add(new BasicNameValuePair("name", filterText));
132+
}
133+
134+
try {
135+
return EntityToRepresentation.toRepresentation(
136+
pageModel,
137+
EntityToRepresentation::toRepresentation,
138+
contextBean.getUriInfo(),
139+
queryParameters
140+
);
141+
} catch (URISyntaxException e) {
142+
throw new InternalServerErrorException();
143+
}
99144
}
100145

101146
@Override

0 commit comments

Comments
 (0)