Skip to content

Commit

Permalink
Merge branch 'main' of https://git.smc.it/openk9/openk9 into 569-doc-…
Browse files Browse the repository at this point in the history
…type-sub-field
  • Loading branch information
lorenzov96 committed Oct 4, 2023
2 parents 58a3def + ef40009 commit 766c123
Show file tree
Hide file tree
Showing 69 changed files with 3,044 additions and 847 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.openk9.datasource.events;

import io.quarkus.runtime.Startup;
import io.smallrye.reactive.messaging.rabbitmq.OutgoingRabbitMQMetadata;
import org.eclipse.microprofile.reactive.messaging.Channel;
import org.eclipse.microprofile.reactive.messaging.Emitter;
import org.eclipse.microprofile.reactive.messaging.Message;
import org.eclipse.microprofile.reactive.messaging.Metadata;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
Expand All @@ -12,7 +15,16 @@
public class DatasourceEventBus {

public void sendEvent(DatasourceMessage datasourceMessage) {
quoteRequestEmitter.send(datasourceMessage);
quoteRequestEmitter.send(
Message.of(
datasourceMessage,
Metadata.of(OutgoingRabbitMQMetadata
.builder()
.withDeliveryMode(2)
.build()
)
)
);
}

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ public Uni<Connection<DocTypeField>> getDocTypeFieldsFromDocType(
docTypeId, after, before, first, last, searchText, sortByList, notEqual);
}

@Query
public Uni<Connection<DocTypeField>> getDocTypeFieldsFromDocTypeByParent(
@Id long docTypeId,
@Description("id of the parent docTypeField (0 if root )") long parentId,
@Description("fetching only nodes after this node (exclusive)") String after,
@Description("fetching only nodes before this node (exclusive)") String before,
@Description("fetching only the first certain number of nodes") Integer first,
@Description("fetching only the last certain number of nodes") Integer last,
String searchText, Set<SortBy> sortByList,
@Description("if notEqual is true, it returns unbound entities") @DefaultValue("false") boolean notEqual) {
return docTypeService.getDocTypeFieldsConnectionByParent(
docTypeId, parentId, after, before, first, last, searchText, sortByList, notEqual);
}

@Query
public Uni<DocType> getDocType(@Id long id) {
return docTypeService.findById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ public class MappingsKey {
private final String key;
private final String hashKey;

public static MappingsKey of(String key) {
return new MappingsKey(key);
}

public static MappingsKey of(String key, String hashKey) {
return new MappingsKey(key, hashKey);
}

public MappingsKey(String key) {
this(key, key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,84 +214,79 @@ public static Map<MappingsKey, Object> docTypeFieldsToMappings(
.filter(docTypeField -> docTypeField.getParentDocTypeField() == null)
.collect(Collectors.toList()),
new LinkedHashMap<>(),
"",
new MappingsKey("properties")
MappingsKey.of("properties")
);
}

private static Map<MappingsKey, Object> createMappings_(
Collection<DocTypeField> docTypeFields,
Map<MappingsKey, Object> acc, String accPath, MappingsKey nextKey) {
Map<MappingsKey, Object> acc,
MappingsKey nextKey) {

for (DocTypeField docTypeField : docTypeFields) {

String fieldName = docTypeField
.getFieldName()
.replace(accPath.isEmpty() ? "" : accPath + ".", "");

FieldType fieldType = docTypeField.getFieldType();

boolean isObject = fieldType == FieldType.OBJECT || fieldType == FieldType.I18N;

String[] fieldNamesArray = fieldName.split("\\.");

Map<MappingsKey, Object> current = acc;

for (int i = 0; i < fieldNamesArray.length; i++) {

String currentFieldName = fieldNamesArray[i];
current = visit(nextKey, current);

boolean isLast = i == fieldNamesArray.length - 1;
if (docTypeField.getParentDocTypeField() == null) {
DocType docType = docTypeField.getDocType();
String docTypeName = docType.getName();
if (!docTypeName.equals("default")) {
current = visit(MappingsKey.of(docTypeName), current);

current = (Map<MappingsKey, Object>) current.computeIfAbsent(
nextKey, k -> new LinkedHashMap<>());
current = visit(MappingsKey.of("properties"), current);
}
}

String fieldName = docTypeField.getFieldName();

current = (Map<MappingsKey, Object>) current.computeIfAbsent(
new MappingsKey(currentFieldName), k -> new LinkedHashMap<>());
FieldType fieldType = docTypeField.getFieldType();

if (isLast) {
boolean isObject = fieldType == FieldType.OBJECT || fieldType == FieldType.I18N;

if (!isObject) {
current.put(new MappingsKey("type"), fieldType.getType());
current = visit(MappingsKey.of(fieldName), current);

Analyzer analyzer = docTypeField.getAnalyzer();
if (!isObject) {
current.put(MappingsKey.of("type"), fieldType.getType());

if (analyzer != null) {
current.put(new MappingsKey("analyzer"), analyzer.getName());
}
Analyzer analyzer = docTypeField.getAnalyzer();

String fieldConfig = docTypeField.getJsonConfig();
if (analyzer != null) {
current.put(MappingsKey.of("analyzer"), analyzer.getName());
}

if (fieldConfig != null) {
JsonObject fieldConfigJson = new JsonObject(fieldConfig);
for (Map.Entry<String, Object> entry : fieldConfigJson) {
current.putIfAbsent(new MappingsKey(entry.getKey()), entry.getValue());
}
}
}
String fieldConfig = docTypeField.getJsonConfig();

Set<DocTypeField> subDocTypeFields = docTypeField.getSubDocTypeFields();

if (subDocTypeFields != null) {
createMappings_(
subDocTypeFields,
current,
accPath.isEmpty()
? fieldName
: String.join(".", accPath, fieldName),
isObject
? new MappingsKey("properties")
: new MappingsKey("fields"));
if (fieldConfig != null) {
JsonObject fieldConfigJson = new JsonObject(fieldConfig);
for (Map.Entry<String, Object> entry : fieldConfigJson) {
current.putIfAbsent(new MappingsKey(entry.getKey()), entry.getValue());
}
}
}

Set<DocTypeField> subDocTypeFields = docTypeField.getSubDocTypeFields();

if (subDocTypeFields != null) {
createMappings_(
subDocTypeFields,
current,
isObject
? MappingsKey.of("properties")
: MappingsKey.of("fields"));
}


}

return acc;

}

private static Map<MappingsKey, Object> visit(MappingsKey nextKey, Map<MappingsKey, Object> current) {
return (Map<MappingsKey, Object>) current.computeIfAbsent(
nextKey, k -> new LinkedHashMap<>());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
@Cacheable
public class DocType extends K9Entity {

public static final String DEFAULT_NAME = "default";

@Column(name = "name", nullable = false, unique = true)
private String name;

Expand All @@ -55,7 +57,8 @@ public class DocType extends K9Entity {
@OneToMany(
mappedBy = "docType",
cascade = javax.persistence.CascadeType.ALL,
fetch = FetchType.LAZY
fetch = FetchType.LAZY,
orphanRemoval = true
)
@ToString.Exclude
@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package io.openk9.datasource.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.openk9.datasource.model.util.DocTypeFieldUtils;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
Expand All @@ -30,11 +32,16 @@
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import java.util.LinkedHashSet;
import java.util.Objects;
Expand Down Expand Up @@ -62,7 +69,7 @@ public class DocTypeField extends BaseDocTypeField {
private String fieldName;

@ToString.Exclude
@ManyToOne(fetch = javax.persistence.FetchType.LAZY, cascade = javax.persistence.CascadeType.ALL)
@ManyToOne
@JoinColumn(name = "doc_type_id")
@JsonIgnore
private DocType docType;
Expand Down Expand Up @@ -111,6 +118,11 @@ public class DocTypeField extends BaseDocTypeField {
@JsonIgnore
private Set<AclMapping> aclMappings = new LinkedHashSet<>();

@Transient
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private String path;

public Set<DocTypeField> getChildren() {
return subDocTypeFields;
}
Expand All @@ -124,6 +136,14 @@ public Set<DocTypeField> getDocTypeFieldAndChildren() {
return docTypeFields;
}

public String getPath() {
if (path == null) {
refreshPath();
}

return path;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -141,4 +161,12 @@ public boolean equals(Object o) {
public int hashCode() {
return getClass().hashCode();
}

@PostLoad
@PostPersist
@PostUpdate
protected void refreshPath() {
this.path = DocTypeFieldUtils.fieldPath(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static void apply(

if (value != null && !value.isBlank()) {
boolQueryBuilder.should(
QueryBuilders.termQuery(docTypeField.getFieldName(), value));
QueryBuilders.termQuery(docTypeField.getPath(), value));
}

}
Expand All @@ -105,7 +105,7 @@ public static void apply(

if (values != null && !values.isEmpty()) {
boolQueryBuilder.should(
QueryBuilders.termsQuery(docTypeField.getFieldName(), values));
QueryBuilders.termsQuery(docTypeField.getPath(), values));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import io.openk9.datasource.model.FieldType;
import io.openk9.datasource.model.dto.util.K9EntityDTO;
import io.openk9.datasource.validation.Alnum;
import io.openk9.datasource.validation.json.Json;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down Expand Up @@ -55,6 +56,7 @@ public class DocTypeFieldDTO extends K9EntityDTO {
private Boolean exclude;

@NotNull
@Alnum
private String fieldName;

@Json
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.openk9.datasource.model.util;

import io.openk9.datasource.model.DocType;
import io.openk9.datasource.model.DocTypeField;

public class DocTypeFieldUtils {

public static String fieldPath(DocTypeField docTypeField) {
DocType docType = docTypeField.getDocType();
return fieldPath(docType != null ? docType.getName() : null, docTypeField);
}

public static String fieldPath(String docTypeName, DocTypeField docTypeField) {

String rootPath =
docTypeName != null && !docTypeName.equals("default") ? docTypeName + "." : "";

DocTypeField parent = docTypeField.getParentDocTypeField();

String fieldName = docTypeField.getFieldName();

return parent != null ? fieldPath(parent) + "." + fieldName : rootPath + fieldName;
}
}
Loading

0 comments on commit 766c123

Please sign in to comment.