Improvements to GetEntityDetails Node

This commit is contained in:
Andrew Shvayka 2019-04-03 13:18:00 +03:00
parent 3524b05675
commit d1c6a9073e
3 changed files with 57 additions and 95 deletions

View File

@ -17,6 +17,7 @@ package org.thingsboard.rule.engine.metadata;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -26,6 +27,8 @@ import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNode; import org.thingsboard.rule.engine.api.TbNode;
import org.thingsboard.rule.engine.api.TbNodeConfiguration; import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException; import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.rule.engine.util.EntityDetails;
import org.thingsboard.server.common.data.ContactBased;
import org.thingsboard.server.common.msg.TbMsg; import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgMetaData; import org.thingsboard.server.common.msg.TbMsgMetaData;
@ -50,7 +53,7 @@ public abstract class TbAbstractGetEntityDetailsNode<C extends TbAbstractGetEnti
} }
@Override @Override
public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException { public void onMsg(TbContext ctx, TbMsg msg) {
try { try {
ctx.tellNext(getDetails(ctx, msg), SUCCESS); ctx.tellNext(getDetails(ctx, msg), SUCCESS);
} catch (Exception e) { } catch (Exception e) {
@ -82,13 +85,50 @@ public abstract class TbAbstractGetEntityDetailsNode<C extends TbAbstractGetEnti
} }
} }
protected JsonElement addContactProperties(JsonElement data, ContactBased entity, EntityDetails entityDetails, String prefix) {
JsonObject dataAsObject = data.getAsJsonObject();
switch (entityDetails) {
case ADDRESS:
if (entity.getAddress() != null)
dataAsObject.addProperty(prefix + "address", entity.getAddress());
break;
case ADDRESS2:
if (entity.getAddress2() != null)
dataAsObject.addProperty(prefix + "address2", entity.getAddress2());
break;
case CITY:
if (entity.getCity() != null) dataAsObject.addProperty(prefix + "city", entity.getCity());
break;
case COUNTRY:
if (entity.getCountry() != null)
dataAsObject.addProperty(prefix + "country", entity.getCountry());
break;
case STATE:
if (entity.getState() != null) dataAsObject.addProperty(prefix + "state", entity.getState());
break;
case EMAIL:
if (entity.getEmail() != null) dataAsObject.addProperty(prefix + "email", entity.getEmail());
break;
case PHONE:
if (entity.getPhone() != null) dataAsObject.addProperty(prefix + "phone", entity.getPhone());
break;
case ZIP:
if (entity.getZip() != null) dataAsObject.addProperty(prefix + "zip", entity.getZip());
break;
case ADDITIONAL_INFO:
if (entity.getAdditionalInfo().hasNonNull("description")) {
dataAsObject.addProperty(prefix + "additionalInfo", entity.getAdditionalInfo().get("description").asText());
}
break;
}
return dataAsObject;
}
@Data @Data
@AllArgsConstructor @AllArgsConstructor
protected static class MessageData { protected static class MessageData {
private JsonElement data; private JsonElement data;
private String dataType; private String dataType;
} }

View File

@ -38,10 +38,10 @@ import org.thingsboard.server.common.msg.TbMsg;
@RuleNode(type = ComponentType.ENRICHMENT, @RuleNode(type = ComponentType.ENRICHMENT,
name = "customer details", name = "customer details",
configClazz = TbGetCustomerDetailsNodeConfiguration.class, configClazz = TbGetCustomerDetailsNodeConfiguration.class,
nodeDescription = "Node find the customer of the message originator and fetch his details that selected from the drop-down list and add them to the message if they exist.", nodeDescription = "Adds fields from Customer details to the message body or metadata",
nodeDetails = "If selected checkbox: <b>Add selected details to the message metadata</b>, existing fields will add to the message metadata instead of message data.<br><br>" + nodeDetails = "If checkbox: <b>Add selected details to the message metadata</b> is selected, existing fields will be added to the message metadata instead of message data.<br><br>" +
"<b>Note:</b> only Device, Asset, and Entity View type are allowed.<br><br>" + "<b>Note:</b> only Device, Asset, and Entity View type are allowed.<br><br>" +
"If the originator of the message didn't assign to Customer, or originator type is not supported - Message send via <b>Failure</b> chain, otherwise, <b>Success</b> chain will be used.", "If the originator of the message is not assigned to Customer, or originator type is not supported - Message will be forwarded to <b>Failure</b> chain, otherwise, <b>Success</b> chain will be used.",
uiResources = {"static/rulenode/rulenode-core-config.js"}, uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbEnrichmentNodeEntityDetailsConfig") configDirective = "tbEnrichmentNodeEntityDetailsConfig")
public class TbGetCustomerDetailsNode extends TbAbstractGetEntityDetailsNode<TbGetCustomerDetailsNodeConfiguration> { public class TbGetCustomerDetailsNode extends TbAbstractGetEntityDetailsNode<TbGetCustomerDetailsNodeConfiguration> {
@ -62,7 +62,7 @@ public class TbGetCustomerDetailsNode extends TbAbstractGetEntityDetailsNode<TbG
JsonElement resultObject = null; JsonElement resultObject = null;
if (!config.getDetailsList().isEmpty()) { if (!config.getDetailsList().isEmpty()) {
for (EntityDetails entityDetails : config.getDetailsList()) { for (EntityDetails entityDetails : config.getDetailsList()) {
resultObject = addCustomerProperties(messageData.getData(), getCustomer(ctx, msg), entityDetails); resultObject = addContactProperties(messageData.getData(), getCustomer(ctx, msg), entityDetails, CUSTOMER_PREFIX);
} }
return transformMsg(ctx, msg, resultObject, messageData); return transformMsg(ctx, msg, resultObject, messageData);
} else { } else {
@ -77,66 +77,25 @@ public class TbGetCustomerDetailsNode extends TbAbstractGetEntityDetailsNode<TbG
if (!device.getCustomerId().isNullUid()) { if (!device.getCustomerId().isNullUid()) {
return ctx.getCustomerService().findCustomerById(ctx.getTenantId(), device.getCustomerId()); return ctx.getCustomerService().findCustomerById(ctx.getTenantId(), device.getCustomerId());
} else { } else {
throw new RuntimeException("Device with name '" + device.getName() + "' didn't assign to Customer."); throw new RuntimeException("Device with name '" + device.getName() + "' is not assigned to Customer.");
} }
case ASSET: case ASSET:
Asset asset = ctx.getAssetService().findAssetById(ctx.getTenantId(), new AssetId(msg.getOriginator().getId())); Asset asset = ctx.getAssetService().findAssetById(ctx.getTenantId(), new AssetId(msg.getOriginator().getId()));
if (!asset.getCustomerId().isNullUid()) { if (!asset.getCustomerId().isNullUid()) {
return ctx.getCustomerService().findCustomerById(ctx.getTenantId(), asset.getCustomerId()); return ctx.getCustomerService().findCustomerById(ctx.getTenantId(), asset.getCustomerId());
} else { } else {
throw new RuntimeException("Asset with name '" + asset.getName() + "' didn't assign to Customer."); throw new RuntimeException("Asset with name '" + asset.getName() + "' is not assigned to Customer.");
} }
case ENTITY_VIEW: case ENTITY_VIEW:
EntityView entityView = ctx.getEntityViewService().findEntityViewById(ctx.getTenantId(), new EntityViewId(msg.getOriginator().getId())); EntityView entityView = ctx.getEntityViewService().findEntityViewById(ctx.getTenantId(), new EntityViewId(msg.getOriginator().getId()));
if (!entityView.getCustomerId().isNullUid()) { if (!entityView.getCustomerId().isNullUid()) {
return ctx.getCustomerService().findCustomerById(ctx.getTenantId(), entityView.getCustomerId()); return ctx.getCustomerService().findCustomerById(ctx.getTenantId(), entityView.getCustomerId());
} else { } else {
throw new RuntimeException("EntityView with name '" + entityView.getName() + "' didn't assign to Customer."); throw new RuntimeException("EntityView with name '" + entityView.getName() + "' is not assigned to Customer.");
} }
default: default:
throw new RuntimeException("Entity with entityType '" + msg.getOriginator().getEntityType() + "' can't be assigned to Customer."); throw new RuntimeException("Entity with entityType '" + msg.getOriginator().getEntityType() + "' is not supported.");
} }
} }
private JsonElement addCustomerProperties(JsonElement data, Customer customer, EntityDetails entityDetails) {
JsonObject dataAsObject = data.getAsJsonObject();
switch (entityDetails) {
case ADDRESS:
if (customer.getAddress() != null)
dataAsObject.addProperty(CUSTOMER_PREFIX + "address", customer.getAddress());
break;
case ADDRESS2:
if (customer.getAddress2() != null)
dataAsObject.addProperty(CUSTOMER_PREFIX + "address2", customer.getAddress2());
break;
case CITY:
if (customer.getCity() != null) dataAsObject.addProperty(CUSTOMER_PREFIX + "city", customer.getCity());
break;
case COUNTRY:
if (customer.getCountry() != null)
dataAsObject.addProperty(CUSTOMER_PREFIX + "country", customer.getCountry());
break;
case STATE:
if (customer.getState() != null)
dataAsObject.addProperty(CUSTOMER_PREFIX + "state", customer.getState());
break;
case EMAIL:
if (customer.getEmail() != null)
dataAsObject.addProperty(CUSTOMER_PREFIX + "email", customer.getEmail());
break;
case PHONE:
if (customer.getPhone() != null)
dataAsObject.addProperty(CUSTOMER_PREFIX + "phone", customer.getPhone());
break;
case ZIP:
if (customer.getZip() != null) dataAsObject.addProperty(CUSTOMER_PREFIX + "zip", customer.getZip());
break;
case ADDITIONAL_INFO:
if (customer.getAdditionalInfo().hasNonNull("description")) {
dataAsObject.addProperty(CUSTOMER_PREFIX + "additionalInfo", customer.getAdditionalInfo().get("description").asText());
}
break;
}
return dataAsObject;
}
} }

View File

@ -24,6 +24,7 @@ import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException; import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.rule.engine.api.util.TbNodeUtils; import org.thingsboard.rule.engine.api.util.TbNodeUtils;
import org.thingsboard.rule.engine.util.EntityDetails; import org.thingsboard.rule.engine.util.EntityDetails;
import org.thingsboard.server.common.data.ContactBased;
import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.plugin.ComponentType; import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.msg.TbMsg; import org.thingsboard.server.common.msg.TbMsg;
@ -32,13 +33,14 @@ import org.thingsboard.server.common.msg.TbMsg;
@RuleNode(type = ComponentType.ENRICHMENT, @RuleNode(type = ComponentType.ENRICHMENT,
name = "tenant details", name = "tenant details",
configClazz = TbGetTenantDetailsNodeConfiguration.class, configClazz = TbGetTenantDetailsNodeConfiguration.class,
nodeDescription = "Node fetch current Tenant details that selected from the drop-down list and add them to the message if they exist.", nodeDescription = "Adds fields from Tenant details to the message body or metadata",
nodeDetails = "If selected checkbox: <b>Add selected details to the message metadata</b>, existing fields will add to the message metadata instead of message data.", nodeDetails = "If checkbox: <b>Add selected details to the message metadata</b> is selected, existing fields will be added to the message metadata instead of message data.<br><br>" +
"<b>Note:</b> only Device, Asset, and Entity View type are allowed.<br><br>" +
"If the originator of the message is not assigned to Tenant, or originator type is not supported - Message will be forwarded to <b>Failure</b> chain, otherwise, <b>Success</b> chain will be used.",
uiResources = {"static/rulenode/rulenode-core-config.js"}, uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbEnrichmentNodeEntityDetailsConfig") configDirective = "tbEnrichmentNodeEntityDetailsConfig")
public class TbGetTenantDetailsNode extends TbAbstractGetEntityDetailsNode<TbGetTenantDetailsNodeConfiguration> { public class TbGetTenantDetailsNode extends TbAbstractGetEntityDetailsNode<TbGetTenantDetailsNodeConfiguration> {
private static final String TENANT_PREFIX = "tenant_"; private static final String TENANT_PREFIX = "tenant_";
@Override @Override
@ -56,50 +58,11 @@ public class TbGetTenantDetailsNode extends TbAbstractGetEntityDetailsNode<TbGet
Tenant tenant = ctx.getTenantService().findTenantById(ctx.getTenantId()); Tenant tenant = ctx.getTenantService().findTenantById(ctx.getTenantId());
if (!config.getDetailsList().isEmpty()) { if (!config.getDetailsList().isEmpty()) {
for (EntityDetails entityDetails : config.getDetailsList()) { for (EntityDetails entityDetails : config.getDetailsList()) {
resultObject = addTenantProperties(messageData.getData(), tenant, entityDetails); resultObject = addContactProperties(messageData.getData(), tenant, entityDetails, TENANT_PREFIX);
} }
return transformMsg(ctx, msg, resultObject, messageData); return transformMsg(ctx, msg, resultObject, messageData);
} else { } else {
return msg; return msg;
} }
} }
private JsonElement addTenantProperties(JsonElement data, Tenant tenant, EntityDetails entityDetails) {
JsonObject dataAsObject = data.getAsJsonObject();
switch (entityDetails) {
case ADDRESS:
if (tenant.getAddress() != null)
dataAsObject.addProperty(TENANT_PREFIX + "address", tenant.getAddress());
break;
case ADDRESS2:
if (tenant.getAddress2() != null)
dataAsObject.addProperty(TENANT_PREFIX + "address2", tenant.getAddress2());
break;
case CITY:
if (tenant.getCity() != null) dataAsObject.addProperty(TENANT_PREFIX + "city", tenant.getCity());
break;
case COUNTRY:
if (tenant.getCountry() != null)
dataAsObject.addProperty(TENANT_PREFIX + "country", tenant.getCountry());
break;
case STATE:
if (tenant.getState() != null) dataAsObject.addProperty(TENANT_PREFIX + "state", tenant.getState());
break;
case EMAIL:
if (tenant.getEmail() != null) dataAsObject.addProperty(TENANT_PREFIX + "email", tenant.getEmail());
break;
case PHONE:
if (tenant.getPhone() != null) dataAsObject.addProperty(TENANT_PREFIX + "phone", tenant.getPhone());
break;
case ZIP:
if (tenant.getZip() != null) dataAsObject.addProperty(TENANT_PREFIX + "zip", tenant.getZip());
break;
case ADDITIONAL_INFO:
if (tenant.getAdditionalInfo().hasNonNull("description")) {
dataAsObject.addProperty(TENANT_PREFIX + "additionalInfo", tenant.getAdditionalInfo().get("description").asText());
}
break;
}
return dataAsObject;
}
} }