Skip to content

Commit

Permalink
Refactor layers (#103)
Browse files Browse the repository at this point in the history
* Refactor jpa layer

* Refactor jpa repository layer

* Add JSON validation
  • Loading branch information
carlosthe19916 committed Jan 10, 2022
1 parent 4642b99 commit a4350b1
Show file tree
Hide file tree
Showing 30 changed files with 584 additions and 531 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-aws2-s3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-json-validator</artifactId>
</dependency>

<!--Jdbc-->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class XMLBuilderManager {
@Inject
IGGeneratorManager igGeneratorManager;

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

Expand All @@ -62,6 +62,8 @@ public Uni<String> createXMLString(NamespaceEntity namespace, InputTemplateRepre
idGeneratorConfig = spec.getIdGenerator().getConfig();
}

JsonObject document = inputTemplate.getSpec().getDocument();

IDGenerator idGenerator = igGeneratorManager.selectIDGenerator(idGeneratorType);
switch (kind) {
case Invoice:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,34 @@
@ApplicationScoped
public class CompanyRepository implements PanacheRepositoryBase<CompanyEntity, String> {

public static final String[] SORT_BY_FIELDS = {"name", "createdOn"};
public static final String[] SORT_BY_FIELDS = {"name", "created"};

public Uni<CompanyEntity> findById(NamespaceEntity namespace, String companyId) {
return find(
"id = :companyId and namespace.id = :namespaceId",
Parameters.with("namespaceId", namespace.id).and("companyId", companyId).map()
).firstResult();
return findById(namespace.id, companyId);
}

public Uni<CompanyEntity> findByRuc(String namespaceId, String ruc) {
return find("ruc = :ruc and namespace.id = :namespaceId",
Parameters.with("namespaceId", namespaceId).and("ruc", ruc).map()
).firstResult();
public Uni<CompanyEntity> findById(String namespaceId, String companyId) {
Parameters params = Parameters.with("namespaceId", namespaceId).and("companyId", companyId);
return find("id = :companyId and namespace.id = :namespaceId", params).firstResult();
}

public Uni<CompanyEntity> findByRuc(NamespaceEntity namespace, String ruc) {
return findByRuc(namespace.id, ruc);
}

public Uni<CompanyEntity> findByRuc(String namespaceId, String ruc) {
Parameters params = Parameters.with("namespaceId", namespaceId).and("ruc", ruc);
return find("ruc = :ruc and namespace.id = :namespaceId", params).firstResult();
}

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

Parameters params = Parameters.with("namespaceId", namespace.id);

PanacheQuery<CompanyEntity> query = CompanyEntity
.find(
"From CompanyEntity as c where c.namespace.id = :namespaceId",
sort,
Parameters.with("namespaceId", namespace.id)
)
.find("From CompanyEntity as c where c.namespace.id = :namespaceId", sort, params)
.range(pageBean.getOffset(), pageBean.getOffset() + pageBean.getLimit() - 1);

return Uni.combine().all().unis(query.list(), query.count());
Expand All @@ -71,15 +70,19 @@ public UniAndGroup2<List<CompanyEntity>, Long> list(NamespaceEntity namespace, S
Sort sort = Sort.by();
sortBy.forEach(f -> sort.and(f.getFieldName(), f.isAsc() ? Sort.Direction.Ascending : Sort.Direction.Descending));

Parameters params = Parameters.with("namespaceId", namespace.id).and("filterText", "%" + filterText.toLowerCase() + "%");

PanacheQuery<CompanyEntity> query = CompanyEntity
.find(
"From CompanyEntity as c where c.namespace.id = :namespaceId and (lower(c.name) like :filterText or c.ruc like :filterText)",
sort,
Parameters.with("namespaceId", namespace.id).and("filterText", "%" + filterText.toLowerCase() + "%")
)
.find("From CompanyEntity as c where c.namespace.id = :namespaceId and (lower(c.name) like :filterText or c.ruc like :filterText)", sort, params)
.range(pageBean.getOffset(), pageBean.getOffset() + pageBean.getLimit() - 1);

return Uni.combine().all().unis(query.list(), query.count());
}

public Uni<Boolean> deleteByNamespaceIdAndId(String namespaceId, String id) {
Parameters params = Parameters.with("namespaceId", namespaceId).and("id", id);
return CompanyEntity
.delete("namespace.id = :namespaceId and id = :id", params)
.map(rowCount -> rowCount > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@
public class GeneratedIDRepository implements PanacheRepositoryBase<GeneratedIDEntity, String> {

public Uni<GeneratedIDEntity> getCurrentID(NamespaceEntity namespace, String ruc, String documentType) {
return
find("namespace.id = :namespaceId and ruc = :ruc and documentType = :documentType", Parameters
.with("namespaceId", namespace.id)
.and("ruc", ruc)
.and("documentType", documentType)
.map()
).firstResult();
Parameters params = Parameters
.with("namespaceId", namespace.id)
.and("ruc", ruc)
.and("documentType", documentType);

return find("namespace.id = :namespaceId and ruc = :ruc and documentType = :documentType", params).firstResult();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public class NamespaceRepository implements PanacheRepositoryBase<NamespaceEntit

public enum SortByField {
name,
createdOn
created
}

public static final String[] SORT_BY_FIELDS = {SortByField.name.toString(), SortByField.createdOn.toString()};
public static final String[] SORT_BY_FIELDS = {SortByField.name.toString(), SortByField.created.toString()};

public Uni<NamespaceEntity> findByName(String name) {
return find("name", name).firstResult();
Expand All @@ -48,7 +48,7 @@ public UniAndGroup2<List<NamespaceEntity>, Long> list(PageBean pageBean, List<So
sortBy.forEach(f -> sort.and(f.getFieldName(), f.isAsc() ? Sort.Direction.Ascending : Sort.Direction.Descending));

PanacheQuery<NamespaceEntity> query = NamespaceEntity
.find("From NamespaceEntity as o", sort)
.find("From NamespaceEntity as n", sort)
.range(pageBean.getOffset(), pageBean.getOffset() + pageBean.getLimit() - 1);

return Uni.combine().all().unis(query.list(), query.count());
Expand All @@ -69,4 +69,10 @@ public UniAndGroup2<List<NamespaceEntity>, Long> list(String filterText, PageBea
return Uni.combine().all().unis(query.list(), query.count());
}

@Override
public Uni<Boolean> deleteById(String id) {
return NamespaceEntity
.delete("id", id)
.map(rowCount -> rowCount > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@
@ApplicationScoped
public class UBLDocumentRepository implements PanacheRepositoryBase<UBLDocumentEntity, String> {

public static final String[] SORT_BY_FIELDS = {"createdOn"};
public static final String[] SORT_BY_FIELDS = {"created"};

public Uni<UBLDocumentEntity> findById(NamespaceEntity namespace, String id) {
Parameters queryParameters = Parameters.with("namespaceId", namespace.id)
.and("id", id);
return findById(namespace.id, id);
}

public Uni<UBLDocumentEntity> findById(String namespaceId, String id) {
Parameters params = Parameters.with("namespaceId", namespaceId).and("id", id);
return UBLDocumentEntity
.find("From UBLDocumentEntity as d where d.namespace.id = :namespaceId and d.id = :id", queryParameters)
.find("From UBLDocumentEntity as d where d.namespace.id = :namespaceId and d.id = :id", params)
.firstResult();
}

Expand All @@ -49,19 +52,19 @@ public UniAndGroup2<List<UBLDocumentEntity>, Long> list(NamespaceEntity namespac
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.id);
Parameters params = Parameters.with("namespaceId", namespace.id);

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

PanacheQuery<UBLDocumentEntity> query = UBLDocumentEntity
.find(queryBuilder.toString(), sort, queryParameters)
.find(queryBuilder.toString(), sort, params)
.range(pageBean.getOffset(), pageBean.getOffset() + pageBean.getLimit() - 1);

return Uni.combine().all().unis(query.list(), query.count());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.project.openubl.ublhub.models.jpa.entities;

import io.quarkus.hibernate.reactive.panache.PanacheEntityBase;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

@MappedSuperclass
public abstract class BaseEntity extends PanacheEntityBase {

@NotNull
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created")
public Date created;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated")
public Date updated;

@Version
@Column(name = "version")
public int version;

@PrePersist
public void prePersist() {
created = new Date();
}

@PreUpdate
public void preUpdate() {
updated = new Date();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,43 @@
*/
package io.github.project.openubl.ublhub.models.jpa.entities;

import io.quarkus.hibernate.reactive.panache.PanacheEntityBase;

import javax.persistence.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;
import javax.validation.constraints.Size;

@Entity
@Table(name = "company", uniqueConstraints = {
@UniqueConstraint(columnNames = {"namespace_id", "ruc"})
})
public class CompanyEntity extends PanacheEntityBase {
public class CompanyEntity extends BaseEntity {

@Id
@Column(name = "id")
@Access(AccessType.PROPERTY)
public String id;

@NotNull
@Size(max = 11)
@Column(name = "ruc")
public String ruc;

@NotNull
@Size(max = 255)
@Column(name = "name")
public String name;

@Size(max = 255)
public String description;

@NotNull
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_on")
public Date createdOn;

@NotNull
@Valid
@Embedded
public SunatEntity sunat;

@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(foreignKey = @ForeignKey, name = "namespace_id")
public NamespaceEntity namespace;

@Version
@Column(name = "version")
public int version;

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.hibernate.annotations.Nationalized;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Table(name = "component_config")
Expand All @@ -28,17 +30,19 @@ public class ComponentConfigEntity extends PanacheEntityBase {
@Id
@Column(name = "id", length = 36)
@Access(AccessType.PROPERTY)
// we do this because relationships often fetch id, but not entity. This avoids an extra SQL
public String id;

@ManyToOne(fetch = FetchType.LAZY)
@NotNull
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "component_id")
public ComponentEntity component;

@Size(max = 255)
@Column(name = "name")
public String name;

@Nationalized
@Size(max = 4000)
@Column(name = "value", length = 4000)
public String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import io.quarkus.hibernate.reactive.panache.PanacheEntityBase;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -29,25 +31,31 @@ public class ComponentEntity extends PanacheEntityBase {
@Id
@Column(name = "id", length = 36)
@Access(AccessType.PROPERTY)
// we do this because relationships often fetch id, but not entity. This avoids an extra SQL
public String id;

@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(foreignKey = @ForeignKey, name = "namespace_id")
public NamespaceEntity namespace;

@NotNull
@Size(max = 255)
@Column(name = "name")
public String name;

@Size(max = 255)
@Column(name = "provider_type")
public String providerType;

@Size(max = 255)
@Column(name = "provider_id")
public String providerId;

@Size(max = 255)
@Column(name = "parent_id")
public String parentId;

@Size(max = 255)
@Column(name = "sub_type")
public String subType;

Expand Down
Loading

0 comments on commit a4350b1

Please sign in to comment.