Commit d1c6a9073e19c4f5b3f2877413c85a0eda552934

Authored by Andrew Shvayka
1 parent 3524b056

Improvements to GetEntityDetails Node

... ... @@ -17,6 +17,7 @@ package org.thingsboard.rule.engine.metadata;
17 17
18 18 import com.google.gson.Gson;
19 19 import com.google.gson.JsonElement;
  20 +import com.google.gson.JsonObject;
20 21 import com.google.gson.JsonParser;
21 22 import com.google.gson.reflect.TypeToken;
22 23 import lombok.AllArgsConstructor;
... ... @@ -26,6 +27,8 @@ import org.thingsboard.rule.engine.api.TbContext;
26 27 import org.thingsboard.rule.engine.api.TbNode;
27 28 import org.thingsboard.rule.engine.api.TbNodeConfiguration;
28 29 import org.thingsboard.rule.engine.api.TbNodeException;
  30 +import org.thingsboard.rule.engine.util.EntityDetails;
  31 +import org.thingsboard.server.common.data.ContactBased;
29 32 import org.thingsboard.server.common.msg.TbMsg;
30 33 import org.thingsboard.server.common.msg.TbMsgMetaData;
31 34
... ... @@ -50,7 +53,7 @@ public abstract class TbAbstractGetEntityDetailsNode<C extends TbAbstractGetEnti
50 53 }
51 54
52 55 @Override
53   - public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
  56 + public void onMsg(TbContext ctx, TbMsg msg) {
54 57 try {
55 58 ctx.tellNext(getDetails(ctx, msg), SUCCESS);
56 59 } catch (Exception e) {
... ... @@ -82,13 +85,50 @@ public abstract class TbAbstractGetEntityDetailsNode<C extends TbAbstractGetEnti
82 85 }
83 86 }
84 87
  88 + protected JsonElement addContactProperties(JsonElement data, ContactBased entity, EntityDetails entityDetails, String prefix) {
  89 + JsonObject dataAsObject = data.getAsJsonObject();
  90 + switch (entityDetails) {
  91 + case ADDRESS:
  92 + if (entity.getAddress() != null)
  93 + dataAsObject.addProperty(prefix + "address", entity.getAddress());
  94 + break;
  95 + case ADDRESS2:
  96 + if (entity.getAddress2() != null)
  97 + dataAsObject.addProperty(prefix + "address2", entity.getAddress2());
  98 + break;
  99 + case CITY:
  100 + if (entity.getCity() != null) dataAsObject.addProperty(prefix + "city", entity.getCity());
  101 + break;
  102 + case COUNTRY:
  103 + if (entity.getCountry() != null)
  104 + dataAsObject.addProperty(prefix + "country", entity.getCountry());
  105 + break;
  106 + case STATE:
  107 + if (entity.getState() != null) dataAsObject.addProperty(prefix + "state", entity.getState());
  108 + break;
  109 + case EMAIL:
  110 + if (entity.getEmail() != null) dataAsObject.addProperty(prefix + "email", entity.getEmail());
  111 + break;
  112 + case PHONE:
  113 + if (entity.getPhone() != null) dataAsObject.addProperty(prefix + "phone", entity.getPhone());
  114 + break;
  115 + case ZIP:
  116 + if (entity.getZip() != null) dataAsObject.addProperty(prefix + "zip", entity.getZip());
  117 + break;
  118 + case ADDITIONAL_INFO:
  119 + if (entity.getAdditionalInfo().hasNonNull("description")) {
  120 + dataAsObject.addProperty(prefix + "additionalInfo", entity.getAdditionalInfo().get("description").asText());
  121 + }
  122 + break;
  123 + }
  124 + return dataAsObject;
  125 + }
  126 +
85 127 @Data
86 128 @AllArgsConstructor
87 129 protected static class MessageData {
88   -
89 130 private JsonElement data;
90 131 private String dataType;
91   -
92 132 }
93 133
94 134
... ...
... ... @@ -38,10 +38,10 @@ import org.thingsboard.server.common.msg.TbMsg;
38 38 @RuleNode(type = ComponentType.ENRICHMENT,
39 39 name = "customer details",
40 40 configClazz = TbGetCustomerDetailsNodeConfiguration.class,
41   - 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.",
42   - 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>" +
  41 + nodeDescription = "Adds fields from Customer details to the message body or metadata",
  42 + 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>" +
43 43 "<b>Note:</b> only Device, Asset, and Entity View type are allowed.<br><br>" +
44   - "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.",
  44 + "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.",
45 45 uiResources = {"static/rulenode/rulenode-core-config.js"},
46 46 configDirective = "tbEnrichmentNodeEntityDetailsConfig")
47 47 public class TbGetCustomerDetailsNode extends TbAbstractGetEntityDetailsNode<TbGetCustomerDetailsNodeConfiguration> {
... ... @@ -62,7 +62,7 @@ public class TbGetCustomerDetailsNode extends TbAbstractGetEntityDetailsNode<TbG
62 62 JsonElement resultObject = null;
63 63 if (!config.getDetailsList().isEmpty()) {
64 64 for (EntityDetails entityDetails : config.getDetailsList()) {
65   - resultObject = addCustomerProperties(messageData.getData(), getCustomer(ctx, msg), entityDetails);
  65 + resultObject = addContactProperties(messageData.getData(), getCustomer(ctx, msg), entityDetails, CUSTOMER_PREFIX);
66 66 }
67 67 return transformMsg(ctx, msg, resultObject, messageData);
68 68 } else {
... ... @@ -77,66 +77,25 @@ public class TbGetCustomerDetailsNode extends TbAbstractGetEntityDetailsNode<TbG
77 77 if (!device.getCustomerId().isNullUid()) {
78 78 return ctx.getCustomerService().findCustomerById(ctx.getTenantId(), device.getCustomerId());
79 79 } else {
80   - throw new RuntimeException("Device with name '" + device.getName() + "' didn't assign to Customer.");
  80 + throw new RuntimeException("Device with name '" + device.getName() + "' is not assigned to Customer.");
81 81 }
82 82 case ASSET:
83 83 Asset asset = ctx.getAssetService().findAssetById(ctx.getTenantId(), new AssetId(msg.getOriginator().getId()));
84 84 if (!asset.getCustomerId().isNullUid()) {
85 85 return ctx.getCustomerService().findCustomerById(ctx.getTenantId(), asset.getCustomerId());
86 86 } else {
87   - throw new RuntimeException("Asset with name '" + asset.getName() + "' didn't assign to Customer.");
  87 + throw new RuntimeException("Asset with name '" + asset.getName() + "' is not assigned to Customer.");
88 88 }
89 89 case ENTITY_VIEW:
90 90 EntityView entityView = ctx.getEntityViewService().findEntityViewById(ctx.getTenantId(), new EntityViewId(msg.getOriginator().getId()));
91 91 if (!entityView.getCustomerId().isNullUid()) {
92 92 return ctx.getCustomerService().findCustomerById(ctx.getTenantId(), entityView.getCustomerId());
93 93 } else {
94   - throw new RuntimeException("EntityView with name '" + entityView.getName() + "' didn't assign to Customer.");
  94 + throw new RuntimeException("EntityView with name '" + entityView.getName() + "' is not assigned to Customer.");
95 95 }
96 96 default:
97   - throw new RuntimeException("Entity with entityType '" + msg.getOriginator().getEntityType() + "' can't be assigned to Customer.");
  97 + throw new RuntimeException("Entity with entityType '" + msg.getOriginator().getEntityType() + "' is not supported.");
98 98 }
99 99 }
100 100
101   - private JsonElement addCustomerProperties(JsonElement data, Customer customer, EntityDetails entityDetails) {
102   - JsonObject dataAsObject = data.getAsJsonObject();
103   - switch (entityDetails) {
104   - case ADDRESS:
105   - if (customer.getAddress() != null)
106   - dataAsObject.addProperty(CUSTOMER_PREFIX + "address", customer.getAddress());
107   - break;
108   - case ADDRESS2:
109   - if (customer.getAddress2() != null)
110   - dataAsObject.addProperty(CUSTOMER_PREFIX + "address2", customer.getAddress2());
111   - break;
112   - case CITY:
113   - if (customer.getCity() != null) dataAsObject.addProperty(CUSTOMER_PREFIX + "city", customer.getCity());
114   - break;
115   - case COUNTRY:
116   - if (customer.getCountry() != null)
117   - dataAsObject.addProperty(CUSTOMER_PREFIX + "country", customer.getCountry());
118   - break;
119   - case STATE:
120   - if (customer.getState() != null)
121   - dataAsObject.addProperty(CUSTOMER_PREFIX + "state", customer.getState());
122   - break;
123   - case EMAIL:
124   - if (customer.getEmail() != null)
125   - dataAsObject.addProperty(CUSTOMER_PREFIX + "email", customer.getEmail());
126   - break;
127   - case PHONE:
128   - if (customer.getPhone() != null)
129   - dataAsObject.addProperty(CUSTOMER_PREFIX + "phone", customer.getPhone());
130   - break;
131   - case ZIP:
132   - if (customer.getZip() != null) dataAsObject.addProperty(CUSTOMER_PREFIX + "zip", customer.getZip());
133   - break;
134   - case ADDITIONAL_INFO:
135   - if (customer.getAdditionalInfo().hasNonNull("description")) {
136   - dataAsObject.addProperty(CUSTOMER_PREFIX + "additionalInfo", customer.getAdditionalInfo().get("description").asText());
137   - }
138   - break;
139   - }
140   - return dataAsObject;
141   - }
142 101 }
... ...
... ... @@ -24,6 +24,7 @@ import org.thingsboard.rule.engine.api.TbNodeConfiguration;
24 24 import org.thingsboard.rule.engine.api.TbNodeException;
25 25 import org.thingsboard.rule.engine.api.util.TbNodeUtils;
26 26 import org.thingsboard.rule.engine.util.EntityDetails;
  27 +import org.thingsboard.server.common.data.ContactBased;
27 28 import org.thingsboard.server.common.data.Tenant;
28 29 import org.thingsboard.server.common.data.plugin.ComponentType;
29 30 import org.thingsboard.server.common.msg.TbMsg;
... ... @@ -32,13 +33,14 @@ import org.thingsboard.server.common.msg.TbMsg;
32 33 @RuleNode(type = ComponentType.ENRICHMENT,
33 34 name = "tenant details",
34 35 configClazz = TbGetTenantDetailsNodeConfiguration.class,
35   - nodeDescription = "Node fetch current Tenant details that selected from the drop-down list and add them to the message if they exist.",
36   - 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.",
  36 + nodeDescription = "Adds fields from Tenant details to the message body or metadata",
  37 + 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>" +
  38 + "<b>Note:</b> only Device, Asset, and Entity View type are allowed.<br><br>" +
  39 + "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.",
37 40 uiResources = {"static/rulenode/rulenode-core-config.js"},
38 41 configDirective = "tbEnrichmentNodeEntityDetailsConfig")
39 42 public class TbGetTenantDetailsNode extends TbAbstractGetEntityDetailsNode<TbGetTenantDetailsNodeConfiguration> {
40 43
41   -
42 44 private static final String TENANT_PREFIX = "tenant_";
43 45
44 46 @Override
... ... @@ -56,50 +58,11 @@ public class TbGetTenantDetailsNode extends TbAbstractGetEntityDetailsNode<TbGet
56 58 Tenant tenant = ctx.getTenantService().findTenantById(ctx.getTenantId());
57 59 if (!config.getDetailsList().isEmpty()) {
58 60 for (EntityDetails entityDetails : config.getDetailsList()) {
59   - resultObject = addTenantProperties(messageData.getData(), tenant, entityDetails);
  61 + resultObject = addContactProperties(messageData.getData(), tenant, entityDetails, TENANT_PREFIX);
60 62 }
61 63 return transformMsg(ctx, msg, resultObject, messageData);
62 64 } else {
63 65 return msg;
64 66 }
65 67 }
66   -
67   - private JsonElement addTenantProperties(JsonElement data, Tenant tenant, EntityDetails entityDetails) {
68   - JsonObject dataAsObject = data.getAsJsonObject();
69   - switch (entityDetails) {
70   - case ADDRESS:
71   - if (tenant.getAddress() != null)
72   - dataAsObject.addProperty(TENANT_PREFIX + "address", tenant.getAddress());
73   - break;
74   - case ADDRESS2:
75   - if (tenant.getAddress2() != null)
76   - dataAsObject.addProperty(TENANT_PREFIX + "address2", tenant.getAddress2());
77   - break;
78   - case CITY:
79   - if (tenant.getCity() != null) dataAsObject.addProperty(TENANT_PREFIX + "city", tenant.getCity());
80   - break;
81   - case COUNTRY:
82   - if (tenant.getCountry() != null)
83   - dataAsObject.addProperty(TENANT_PREFIX + "country", tenant.getCountry());
84   - break;
85   - case STATE:
86   - if (tenant.getState() != null) dataAsObject.addProperty(TENANT_PREFIX + "state", tenant.getState());
87   - break;
88   - case EMAIL:
89   - if (tenant.getEmail() != null) dataAsObject.addProperty(TENANT_PREFIX + "email", tenant.getEmail());
90   - break;
91   - case PHONE:
92   - if (tenant.getPhone() != null) dataAsObject.addProperty(TENANT_PREFIX + "phone", tenant.getPhone());
93   - break;
94   - case ZIP:
95   - if (tenant.getZip() != null) dataAsObject.addProperty(TENANT_PREFIX + "zip", tenant.getZip());
96   - break;
97   - case ADDITIONAL_INFO:
98   - if (tenant.getAdditionalInfo().hasNonNull("description")) {
99   - dataAsObject.addProperty(TENANT_PREFIX + "additionalInfo", tenant.getAdditionalInfo().get("description").asText());
100   - }
101   - break;
102   - }
103   - return dataAsObject;
104   - }
105 68 }
... ...