...
|
...
|
@@ -17,7 +17,6 @@ package org.thingsboard.rule.engine.action; |
17
|
17
|
|
18
|
18
|
import com.google.common.util.concurrent.Futures;
|
19
|
19
|
import com.google.common.util.concurrent.ListenableFuture;
|
20
|
|
-import com.google.common.util.concurrent.MoreExecutors;
|
21
|
20
|
import lombok.extern.slf4j.Slf4j;
|
22
|
21
|
import org.thingsboard.rule.engine.api.RuleNode;
|
23
|
22
|
import org.thingsboard.rule.engine.api.TbContext;
|
...
|
...
|
@@ -57,8 +56,6 @@ import java.util.List; |
57
|
56
|
)
|
58
|
57
|
public class TbCreateRelationNode extends TbAbstractRelationActionNode<TbCreateRelationNodeConfiguration> {
|
59
|
58
|
|
60
|
|
- private String relationType;
|
61
|
|
-
|
62
|
59
|
@Override
|
63
|
60
|
protected TbCreateRelationNodeConfiguration loadEntityNodeActionConfig(TbNodeConfiguration configuration) throws TbNodeException {
|
64
|
61
|
return TbNodeUtils.convert(configuration, TbCreateRelationNodeConfiguration.class);
|
...
|
...
|
@@ -70,8 +67,8 @@ public class TbCreateRelationNode extends TbAbstractRelationActionNode<TbCreateR |
70
|
67
|
}
|
71
|
68
|
|
72
|
69
|
@Override
|
73
|
|
- protected ListenableFuture<RelationContainer> doProcessEntityRelationAction(TbContext ctx, TbMsg msg, EntityContainer entity) {
|
74
|
|
- ListenableFuture<Boolean> future = createIfAbsent(ctx, msg, entity);
|
|
70
|
+ protected ListenableFuture<RelationContainer> doProcessEntityRelationAction(TbContext ctx, TbMsg msg, EntityContainer entity, String relationType) {
|
|
71
|
+ ListenableFuture<Boolean> future = createIfAbsent(ctx, msg, entity, relationType);
|
75
|
72
|
return Futures.transform(future, result -> {
|
76
|
73
|
RelationContainer container = new RelationContainer();
|
77
|
74
|
if (result && config.isChangeOriginatorToRelatedEntity()) {
|
...
|
...
|
@@ -82,16 +79,15 @@ public class TbCreateRelationNode extends TbAbstractRelationActionNode<TbCreateR |
82
|
79
|
}
|
83
|
80
|
container.setResult(result);
|
84
|
81
|
return container;
|
85
|
|
- }, MoreExecutors.directExecutor());
|
|
82
|
+ }, ctx.getDbCallbackExecutor());
|
86
|
83
|
}
|
87
|
84
|
|
88
|
|
- private ListenableFuture<Boolean> createIfAbsent(TbContext ctx, TbMsg msg, EntityContainer entityContainer) {
|
89
|
|
- relationType = processPattern(msg, config.getRelationType());
|
|
85
|
+ private ListenableFuture<Boolean> createIfAbsent(TbContext ctx, TbMsg msg, EntityContainer entityContainer, String relationType) {
|
90
|
86
|
SearchDirectionIds sdId = processSingleSearchDirection(msg, entityContainer);
|
91
|
87
|
ListenableFuture<Boolean> checkRelationFuture = Futures.transformAsync(ctx.getRelationService().checkRelation(ctx.getTenantId(), sdId.getFromId(), sdId.getToId(), relationType, RelationTypeGroup.COMMON), result -> {
|
92
|
88
|
if (!result) {
|
93
|
89
|
if (config.isRemoveCurrentRelations()) {
|
94
|
|
- return processDeleteRelations(ctx, processFindRelations(ctx, msg, sdId));
|
|
90
|
+ return processDeleteRelations(ctx, processFindRelations(ctx, msg, sdId, relationType));
|
95
|
91
|
}
|
96
|
92
|
return Futures.immediateFuture(false);
|
97
|
93
|
}
|
...
|
...
|
@@ -100,14 +96,14 @@ public class TbCreateRelationNode extends TbAbstractRelationActionNode<TbCreateR |
100
|
96
|
|
101
|
97
|
return Futures.transformAsync(checkRelationFuture, result -> {
|
102
|
98
|
if (!result) {
|
103
|
|
- return processCreateRelation(ctx, entityContainer, sdId);
|
|
99
|
+ return processCreateRelation(ctx, entityContainer, sdId, relationType);
|
104
|
100
|
}
|
105
|
101
|
return Futures.immediateFuture(true);
|
106
|
102
|
}, ctx.getDbCallbackExecutor());
|
107
|
103
|
}
|
108
|
104
|
|
109
|
|
- private ListenableFuture<List<EntityRelation>> processFindRelations(TbContext ctx, TbMsg msg, SearchDirectionIds sdId) {
|
110
|
|
- if (sdId.isOrignatorDirectionFrom()) {
|
|
105
|
+ private ListenableFuture<List<EntityRelation>> processFindRelations(TbContext ctx, TbMsg msg, SearchDirectionIds sdId, String relationType) {
|
|
106
|
+ if (sdId.isOriginatorDirectionFrom()) {
|
111
|
107
|
return ctx.getRelationService().findByFromAndTypeAsync(ctx.getTenantId(), msg.getOriginator(), relationType, RelationTypeGroup.COMMON);
|
112
|
108
|
} else {
|
113
|
109
|
return ctx.getRelationService().findByToAndTypeAsync(ctx.getTenantId(), msg.getOriginator(), relationType, RelationTypeGroup.COMMON);
|
...
|
...
|
@@ -121,91 +117,91 @@ public class TbCreateRelationNode extends TbAbstractRelationActionNode<TbCreateR |
121
|
117
|
for (EntityRelation relation : entityRelations) {
|
122
|
118
|
list.add(ctx.getRelationService().deleteRelationAsync(ctx.getTenantId(), relation));
|
123
|
119
|
}
|
124
|
|
- return Futures.transform(Futures.allAsList(list), result -> false, MoreExecutors.directExecutor());
|
|
120
|
+ return Futures.transform(Futures.allAsList(list), result -> false, ctx.getDbCallbackExecutor());
|
125
|
121
|
}
|
126
|
122
|
return Futures.immediateFuture(false);
|
127
|
123
|
}, ctx.getDbCallbackExecutor());
|
128
|
124
|
}
|
129
|
125
|
|
130
|
|
- private ListenableFuture<Boolean> processCreateRelation(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId) {
|
|
126
|
+ private ListenableFuture<Boolean> processCreateRelation(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId, String relationType) {
|
131
|
127
|
switch (entityContainer.getEntityType()) {
|
132
|
128
|
case ASSET:
|
133
|
|
- return processAsset(ctx, entityContainer, sdId);
|
|
129
|
+ return processAsset(ctx, entityContainer, sdId, relationType);
|
134
|
130
|
case DEVICE:
|
135
|
|
- return processDevice(ctx, entityContainer, sdId);
|
|
131
|
+ return processDevice(ctx, entityContainer, sdId, relationType);
|
136
|
132
|
case CUSTOMER:
|
137
|
|
- return processCustomer(ctx, entityContainer, sdId);
|
|
133
|
+ return processCustomer(ctx, entityContainer, sdId, relationType);
|
138
|
134
|
case DASHBOARD:
|
139
|
|
- return processDashboard(ctx, entityContainer, sdId);
|
|
135
|
+ return processDashboard(ctx, entityContainer, sdId, relationType);
|
140
|
136
|
case ENTITY_VIEW:
|
141
|
|
- return processView(ctx, entityContainer, sdId);
|
|
137
|
+ return processView(ctx, entityContainer, sdId, relationType);
|
142
|
138
|
case TENANT:
|
143
|
|
- return processTenant(ctx, entityContainer, sdId);
|
|
139
|
+ return processTenant(ctx, entityContainer, sdId, relationType);
|
144
|
140
|
}
|
145
|
141
|
return Futures.immediateFuture(true);
|
146
|
142
|
}
|
147
|
143
|
|
148
|
|
- private ListenableFuture<Boolean> processView(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId) {
|
|
144
|
+ private ListenableFuture<Boolean> processView(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId, String relationType) {
|
149
|
145
|
return Futures.transformAsync(ctx.getEntityViewService().findEntityViewByIdAsync(ctx.getTenantId(), new EntityViewId(entityContainer.getEntityId().getId())), entityView -> {
|
150
|
146
|
if (entityView != null) {
|
151
|
|
- return processSave(ctx, sdId);
|
|
147
|
+ return processSave(ctx, sdId, relationType);
|
152
|
148
|
} else {
|
153
|
149
|
return Futures.immediateFuture(true);
|
154
|
150
|
}
|
155
|
151
|
}, ctx.getDbCallbackExecutor());
|
156
|
152
|
}
|
157
|
153
|
|
158
|
|
- private ListenableFuture<Boolean> processDevice(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId) {
|
|
154
|
+ private ListenableFuture<Boolean> processDevice(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId, String relationType) {
|
159
|
155
|
return Futures.transformAsync(ctx.getDeviceService().findDeviceByIdAsync(ctx.getTenantId(), new DeviceId(entityContainer.getEntityId().getId())), device -> {
|
160
|
156
|
if (device != null) {
|
161
|
|
- return processSave(ctx, sdId);
|
|
157
|
+ return processSave(ctx, sdId, relationType);
|
162
|
158
|
} else {
|
163
|
159
|
return Futures.immediateFuture(true);
|
164
|
160
|
}
|
165
|
|
- }, MoreExecutors.directExecutor());
|
|
161
|
+ }, ctx.getDbCallbackExecutor());
|
166
|
162
|
}
|
167
|
163
|
|
168
|
|
- private ListenableFuture<Boolean> processAsset(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId) {
|
|
164
|
+ private ListenableFuture<Boolean> processAsset(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId, String relationType) {
|
169
|
165
|
return Futures.transformAsync(ctx.getAssetService().findAssetByIdAsync(ctx.getTenantId(), new AssetId(entityContainer.getEntityId().getId())), asset -> {
|
170
|
166
|
if (asset != null) {
|
171
|
|
- return processSave(ctx, sdId);
|
|
167
|
+ return processSave(ctx, sdId, relationType);
|
172
|
168
|
} else {
|
173
|
169
|
return Futures.immediateFuture(true);
|
174
|
170
|
}
|
175
|
171
|
}, ctx.getDbCallbackExecutor());
|
176
|
172
|
}
|
177
|
173
|
|
178
|
|
- private ListenableFuture<Boolean> processCustomer(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId) {
|
|
174
|
+ private ListenableFuture<Boolean> processCustomer(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId, String relationType) {
|
179
|
175
|
return Futures.transformAsync(ctx.getCustomerService().findCustomerByIdAsync(ctx.getTenantId(), new CustomerId(entityContainer.getEntityId().getId())), customer -> {
|
180
|
176
|
if (customer != null) {
|
181
|
|
- return processSave(ctx, sdId);
|
|
177
|
+ return processSave(ctx, sdId, relationType);
|
182
|
178
|
} else {
|
183
|
179
|
return Futures.immediateFuture(true);
|
184
|
180
|
}
|
185
|
181
|
}, ctx.getDbCallbackExecutor());
|
186
|
182
|
}
|
187
|
183
|
|
188
|
|
- private ListenableFuture<Boolean> processDashboard(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId) {
|
|
184
|
+ private ListenableFuture<Boolean> processDashboard(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId, String relationType) {
|
189
|
185
|
return Futures.transformAsync(ctx.getDashboardService().findDashboardByIdAsync(ctx.getTenantId(), new DashboardId(entityContainer.getEntityId().getId())), dashboard -> {
|
190
|
186
|
if (dashboard != null) {
|
191
|
|
- return processSave(ctx, sdId);
|
|
187
|
+ return processSave(ctx, sdId, relationType);
|
192
|
188
|
} else {
|
193
|
189
|
return Futures.immediateFuture(true);
|
194
|
190
|
}
|
195
|
191
|
}, ctx.getDbCallbackExecutor());
|
196
|
192
|
}
|
197
|
193
|
|
198
|
|
- private ListenableFuture<Boolean> processTenant(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId) {
|
|
194
|
+ private ListenableFuture<Boolean> processTenant(TbContext ctx, EntityContainer entityContainer, SearchDirectionIds sdId, String relationType) {
|
199
|
195
|
return Futures.transformAsync(ctx.getTenantService().findTenantByIdAsync(ctx.getTenantId(), new TenantId(entityContainer.getEntityId().getId())), tenant -> {
|
200
|
196
|
if (tenant != null) {
|
201
|
|
- return processSave(ctx, sdId);
|
|
197
|
+ return processSave(ctx, sdId, relationType);
|
202
|
198
|
} else {
|
203
|
199
|
return Futures.immediateFuture(true);
|
204
|
200
|
}
|
205
|
201
|
}, ctx.getDbCallbackExecutor());
|
206
|
202
|
}
|
207
|
203
|
|
208
|
|
- private ListenableFuture<Boolean> processSave(TbContext ctx, SearchDirectionIds sdId) {
|
|
204
|
+ private ListenableFuture<Boolean> processSave(TbContext ctx, SearchDirectionIds sdId, String relationType) {
|
209
|
205
|
return ctx.getRelationService().saveRelationAsync(ctx.getTenantId(), new EntityRelation(sdId.getFromId(), sdId.getToId(), relationType, RelationTypeGroup.COMMON));
|
210
|
206
|
}
|
211
|
207
|
|
...
|
...
|
|