Skip to content

Commit

Permalink
Add support to data type as mapping from entities (#3476)
Browse files Browse the repository at this point in the history
Pull Request: #3476
  • Loading branch information
yamelsenih committed Jun 29, 2021
1 parent e339aa2 commit 3c04d6e
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public String modelChange(PO po, int type) throws Exception {
} else if(type == TYPE_AFTER_DELETE) {
if(po instanceof MProduct) {
MProduct product = (MProduct) po;
ElasticSearchHelper.getInstance().connect().index(Product.newInstance().withProduct(product)).close();
ElasticSearchHelper.getInstance().connect().delete(Product.newInstance().withProduct(product)).close();
} else if(po instanceof MWCategory) {
MWCategory group = (MWCategory) po;
ElasticSearchHelper.getInstance().connect().delete(Category.newInstance().withCategoy(group)).close();
Expand All @@ -210,19 +210,19 @@ public String modelChange(PO po, int type) throws Exception {
} else if(po instanceof MTax) {
MTax tax = (MTax) po;
MTaxCategory taxCategory = (MTaxCategory) tax.getC_TaxCategory();
ElasticSearchHelper.getInstance().connect().index(TaxCategory.newInstance().withTaxCategory(taxCategory)).close();
ElasticSearchHelper.getInstance().connect().delete(TaxCategory.newInstance().withTaxCategory(taxCategory)).close();
} else if(po instanceof MAttribute) {
MAttribute attribute = (MAttribute) po;
ElasticSearchHelper.getInstance().connect().delete(Attribute.newInstance().withAttribute(attribute)).close();
} else if(po instanceof MAttributeValue) {
MAttributeValue attributeValue = (MAttributeValue) po;
MAttribute attribute = (MAttribute) attributeValue.getM_Attribute();
ElasticSearchHelper.getInstance().connect().index(Attribute.newInstance().withAttribute(attribute)).close();
ElasticSearchHelper.getInstance().connect().delete(Attribute.newInstance().withAttribute(attribute)).close();
} else if(po instanceof MAttachment) {
MAttachment attachment = (MAttachment) po;
if(attachment.getAD_Table_ID() == MProduct.Table_ID) {
MProduct product = MProduct.get(attachment.getCtx(), attachment.getRecord_ID());
ElasticSearchHelper.getInstance().connect().index(Product.newInstance().withProduct(product)).close();
ElasticSearchHelper.getInstance().connect().delete(Product.newInstance().withProduct(product)).close();
}
} else if(po.get_TableName().equals(I_W_Basket.Table_Name)) {
X_W_Basket basket = (X_W_Basket) po;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.compiere.model.MAttribute;
import org.compiere.model.MAttributeValue;
Expand Down Expand Up @@ -78,7 +79,9 @@ public Map<String, Object> getMap() {
// For list of values
if(attribute.getAttributeValueType().equals(MAttribute.ATTRIBUTEVALUETYPE_List)) {
List<Map<String, Object>> attributeValuesList = new ArrayList<Map<String,Object>>();
Arrays.asList(attribute.getMAttributeValues()).forEach(attributeValue -> {
Arrays.asList(attribute.getMAttributeValues())
.stream().filter(attributeValue -> Optional.ofNullable(attributeValue).isPresent())
.forEach(attributeValue -> {
Map<String, Object> attributeValueMap = new HashMap<String, Object>();
attributeValueMap.put("value", String.valueOf(attributeValue.getM_AttributeValue_ID()));
attributeValueMap.put("label", attributeValue.getName());
Expand Down Expand Up @@ -136,7 +139,9 @@ public boolean isValid() {
return false;
}
//
if(attributeValues == null) {
if(attributeValues == null
&& attribute.getMAttributeValues() != null
&& attribute.getMAttributeValues().length > (attribute.isMandatory()? 0: 1)) {
attributeValues = Arrays.asList(attribute.getMAttributeValues());
}
return attributeValues != null && attributeValues.size() > 0;
Expand All @@ -146,4 +151,9 @@ public boolean isValid() {
public IPersistenceWrapper withWebStoreId(int webStoreId) {
return this;
}

@Override
public Map<String, Object> getMapping() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ public Map<String, Object> getMap() {
map.put("level", getLevel());
// Product Count
map.put("product_count", category.getProductCount());
// TODO: Add from ADempiere
// map.put("include_in_menu", true);
// Default
map.put("is_active", category.isActive());
map.put("is_active", category.isActive() && category.isWebStoreFeatured());
map.put("created_at", ElasticSearch.convertedDate(category.getCreated().getTime()));
map.put("updated_at", ElasticSearch.convertedDate(category.getUpdated().getTime()));

Expand Down Expand Up @@ -175,13 +177,9 @@ private String getValidValue(String value) {
*/
public int getLevel() {
loadParents();
int level = parents.size();
if(level == 0) {
level = 1;
}
// Validate if is a main searched category
if(category.isWebStoreFeatured()) {
level = 0;
int level = parents.size() + 2;
if(category.getW_Category_Parent_ID() == 0) {
level = 2;
}
return level;
}
Expand Down Expand Up @@ -234,12 +232,7 @@ private Map<String, Object> getChild(MWCategory category) {
Map<String, Object> mapOfChild = new HashMap<String, Object>();
List<Map<String, Object>> currentChildList = new ArrayList<Map<String,Object>>();
category.getChildList().forEach(child -> {
Map<String, Object> childMap = new HashMap<String, Object>();
childMap.put("id", child.getW_Category_ID());
Map<String, Object> childOfChild = getChild(child);
if(childOfChild.containsKey("children_data")) {
childMap.put("children_data", childOfChild.get("children_data"));
}
Map<String, Object> childMap = Category.newInstance().withCategoy(child).getMap();
currentChildList.add(childMap);
});
mapOfChild.put("children_data", currentChildList);
Expand Down Expand Up @@ -269,4 +262,30 @@ public boolean isValid() {
public IPersistenceWrapper withWebStoreId(int webStoreId) {
return this;
}

@Override
public Map<String, Object> getMapping() {
Map<String, Object> createdAt = new HashMap<>();
createdAt.put("type", "date");
createdAt.put("format", ElasticSearch.DATE_FORMAT);
Map<String, Object> updatedAt = new HashMap<>();
updatedAt.put("type", "date");
updatedAt.put("format", ElasticSearch.DATE_FORMAT);
Map<String, Object> properties = new HashMap<>();
properties.put("created_at", createdAt);
properties.put("updated_at", updatedAt);
Map<String, Object> slug = new HashMap<>();
slug.put("type", "keyword");
properties.put("slug", slug);
Map<String, Object> urlPath = new HashMap<>();
urlPath.put("type", "keyword");
properties.put("url_path", urlPath);
Map<String, Object> urlKey = new HashMap<>();
urlKey.put("type", "keyword");
properties.put("url_key", urlKey);
// Return mapping properties
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
return mapping;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.spin.model.MADAppRegistration;
import org.spin.support.IExternalCache;
Expand Down Expand Up @@ -189,6 +191,20 @@ public ElasticSearch index(IPersistenceWrapper entity) throws IOException {
if(exist(entity)) {
return update(entity);
}
try {
GetIndexRequest getRequest = new GetIndexRequest(getCatalogName(entity.getCatalogName()));
if(!client.indices().exists(getRequest, RequestOptions.DEFAULT)) {
CreateIndexRequest indexDefinition = new CreateIndexRequest(getCatalogName(entity.getCatalogName()));
Map<String, Object> mapping = entity.getMapping();
if(Optional.ofNullable(mapping).isPresent()) {
indexDefinition.mapping(mapping);
client.indices().create(indexDefinition, RequestOptions.DEFAULT);
}
}
} catch (Exception e) {
throw new AdempiereException(e);
}
// Create
IndexRequest indexRequest = new IndexRequest(getCatalogName(entity.getCatalogName())).id(entity.getKeyValue()).source(entity.getMap());
indexRequest.opType(DocWriteRequest.OpType.CREATE);
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
Expand Down Expand Up @@ -314,6 +330,11 @@ public boolean isValid() {
public IPersistenceWrapper withWebStoreId(int webStoreId) {
return null;
}

@Override
public Map<String, Object> getMapping() {
return null;
}
});
} catch (Exception e) {
throw new AdempiereException(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public interface IPersistenceWrapper {
*/
public Map<String, Object> getMap();

/**
* Get Mapping of entity using data type
* @return
*/
public Map<String, Object> getMapping();

/**
* Get value of key for index
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ public Map<String, Object> getMap() {
map.put("image", getImageName(product));
map.put("sku", product.getSKU());
map.put("url_key", getValidValue(product.getName()));
map.put("url_path", product.getDescriptionURL());
map.put("type_id", getProductTypeFromProduct(product));
map.put("price", getPrice(product));
map.put("short_description", product.getDescription());
map.put("description", product.getHelp());
// "special_price": 0,
// "price_incl_tax": null,
// "special_price_incl_tax": null,
Expand Down Expand Up @@ -126,20 +127,27 @@ public Map<String, Object> getMap() {
List<String> categoriesIds = new ArrayList<>();
// Groups
List<MWCategory> productGroups = MWCategory.getOfProduct(product.getCtx(), product.getM_Product_ID(), product.get_TrxName());
StringBuffer urlPath = new StringBuffer();
if(productGroups != null
&& productGroups.size() > 0) {
productGroups.forEach(group -> {
Category wrapperOfGroup = Category.newInstance().withCategoy(group);
Map<String, Object> categoryMap = new HashMap<String, Object>();
categoryMap.put("id", group.getW_Category_ID());
categoryMap.put("name", group.getName());
categoryMap.put("slug", wrapperOfGroup.getURLKey(true));
categoryMap.put("path", wrapperOfGroup.getURLPath());
// categoryMap.put("id", group.getW_Category_ID());
// categoryMap.put("category_id", group.getW_Category_ID());
// categoryMap.put("name", group.getName());
// categoryMap.put("slug", wrapperOfGroup.getURLKey(true));
// categoryMap.put("path", wrapperOfGroup.getURLPath());
// Add
categories.add(categoryMap);
categories.add(wrapperOfGroup.getMap());
categoriesIds.add(wrapperOfGroup.getKeyValue());
if(urlPath.length() == 0) {
urlPath.append(wrapperOfGroup.getURLPath());
}
});
}
urlPath.append("/").append(getValidValue(product.getName() + "-" + product.getM_Product_ID())).append(".html");
map.put("slug", getValidValue(product.getName() + "-" + product.getM_Product_ID()));
map.put("url_path", urlPath.toString());
// Categories
map.put("category_ids", categoriesIds);
map.put("category", categories);
Expand Down Expand Up @@ -414,4 +422,42 @@ public IPersistenceWrapper withWebStoreId(int webStoreId) {
}
return this;
}

@Override
public Map<String, Object> getMapping() {
Map<String, Object> createdAt = new HashMap<>();
createdAt.put("type", "date");
createdAt.put("format", ElasticSearch.DATE_FORMAT);
Map<String, Object> updatedAt = new HashMap<>();
updatedAt.put("type", "date");
updatedAt.put("format", ElasticSearch.DATE_FORMAT);
Map<String, Object> properties = new HashMap<>();
properties.put("created_at", createdAt);
properties.put("updated_at", updatedAt);
Map<String, Object> slug = new HashMap<>();
slug.put("type", "keyword");
properties.put("slug", slug);
Map<String, Object> urlPath = new HashMap<>();
urlPath.put("type", "keyword");
properties.put("url_path", urlPath);
Map<String, Object> urlKey = new HashMap<>();
urlKey.put("type", "keyword");
properties.put("url_key", urlKey);
Map<String, Object> sku = new HashMap<>();
sku.put("type", "keyword");
properties.put("sku", sku);
Map<String, Object> price = new HashMap<>();
price.put("type", "float");
properties.put("price", price);
Map<String, Object> finalPrice = new HashMap<>();
finalPrice.put("type", "float");
properties.put("final_price", finalPrice);
Map<String, Object> sizeOptions = new HashMap<>();
sizeOptions.put("type", "integer");
properties.put("size_options", sizeOptions);
// Return mapping properties
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
return mapping;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public String getKeyValue() {

@Override
public String getCatalogName() {
return "tax_rule";
return "taxrule";
}

@Override
Expand All @@ -147,4 +147,9 @@ public IPersistenceWrapper withWebStoreId(int webStoreId) {
}
return this;
}

@Override
public Map<String, Object> getMapping() {
return null;
}
}

0 comments on commit 3c04d6e

Please sign in to comment.