|
|
1
|
+/**
|
|
|
2
|
+ * Copyright © 2016-2018 The Thingsboard Authors
|
|
|
3
|
+ *
|
|
|
4
|
+ * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5
|
+ * you may not use this file except in compliance with the License.
|
|
|
6
|
+ * You may obtain a copy of the License at
|
|
|
7
|
+ *
|
|
|
8
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9
|
+ *
|
|
|
10
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
|
11
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13
|
+ * See the License for the specific language governing permissions and
|
|
|
14
|
+ * limitations under the License.
|
|
|
15
|
+ */
|
|
|
16
|
+package org.thingsboard.server.controller;
|
|
|
17
|
+
|
|
|
18
|
+import org.springframework.http.HttpStatus;
|
|
|
19
|
+import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
20
|
+import org.springframework.web.bind.annotation.*;
|
|
|
21
|
+import org.thingsboard.server.common.data.EntityType;
|
|
|
22
|
+import org.thingsboard.server.common.data.audit.ActionType;
|
|
|
23
|
+import org.thingsboard.server.common.data.id.PluginId;
|
|
|
24
|
+import org.thingsboard.server.common.data.id.RuleChainId;
|
|
|
25
|
+import org.thingsboard.server.common.data.id.TenantId;
|
|
|
26
|
+import org.thingsboard.server.common.data.page.TextPageData;
|
|
|
27
|
+import org.thingsboard.server.common.data.page.TextPageLink;
|
|
|
28
|
+import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
|
|
|
29
|
+import org.thingsboard.server.common.data.plugin.PluginMetaData;
|
|
|
30
|
+import org.thingsboard.server.common.data.rule.RuleChain;
|
|
|
31
|
+import org.thingsboard.server.common.data.security.Authority;
|
|
|
32
|
+import org.thingsboard.server.dao.model.ModelConstants;
|
|
|
33
|
+import org.thingsboard.server.exception.ThingsboardException;
|
|
|
34
|
+
|
|
|
35
|
+import java.util.List;
|
|
|
36
|
+
|
|
|
37
|
+@RestController
|
|
|
38
|
+@RequestMapping("/api")
|
|
|
39
|
+public class RuleChainController extends BaseController {
|
|
|
40
|
+
|
|
|
41
|
+ public static final String RULE_CHAIN_ID = "ruleChainId";
|
|
|
42
|
+
|
|
|
43
|
+ @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
|
|
|
44
|
+ @RequestMapping(value = "/ruleChain/{ruleChainId}", method = RequestMethod.GET)
|
|
|
45
|
+ @ResponseBody
|
|
|
46
|
+ public RuleChain getRuleChainById(@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
|
|
47
|
+ checkParameter(RULE_CHAIN_ID, strRuleChainId);
|
|
|
48
|
+ try {
|
|
|
49
|
+ RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
|
|
|
50
|
+ return checkRuleChain(ruleChainId);
|
|
|
51
|
+ } catch (Exception e) {
|
|
|
52
|
+ throw handleException(e);
|
|
|
53
|
+ }
|
|
|
54
|
+ }
|
|
|
55
|
+
|
|
|
56
|
+ @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
|
|
|
57
|
+ @RequestMapping(value = "/ruleChain", method = RequestMethod.POST)
|
|
|
58
|
+ @ResponseBody
|
|
|
59
|
+ public RuleChain saveRuleChain(@RequestBody RuleChain ruleChain) throws ThingsboardException {
|
|
|
60
|
+ try {
|
|
|
61
|
+ boolean created = ruleChain.getId() == null;
|
|
|
62
|
+ ruleChain.setTenantId(getCurrentUser().getTenantId());
|
|
|
63
|
+ RuleChain savedRuleChain = checkNotNull(ruleChainService.saveRuleChain(ruleChain));
|
|
|
64
|
+
|
|
|
65
|
+ logEntityAction(savedRuleChain.getId(), savedRuleChain,
|
|
|
66
|
+ null,
|
|
|
67
|
+ created ? ActionType.ADDED : ActionType.UPDATED, null);
|
|
|
68
|
+
|
|
|
69
|
+ return savedRuleChain;
|
|
|
70
|
+ } catch (Exception e) {
|
|
|
71
|
+
|
|
|
72
|
+ logEntityAction(emptyId(EntityType.RULE_CHAIN), ruleChain,
|
|
|
73
|
+ null, ruleChain.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e);
|
|
|
74
|
+
|
|
|
75
|
+ throw handleException(e);
|
|
|
76
|
+ }
|
|
|
77
|
+ }
|
|
|
78
|
+
|
|
|
79
|
+ @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
|
|
|
80
|
+ @RequestMapping(value = "/ruleChains", params = {"limit"}, method = RequestMethod.GET)
|
|
|
81
|
+ @ResponseBody
|
|
|
82
|
+ public TextPageData<RuleChain> getRuleChains(
|
|
|
83
|
+ @RequestParam int limit,
|
|
|
84
|
+ @RequestParam(required = false) String textSearch,
|
|
|
85
|
+ @RequestParam(required = false) String idOffset,
|
|
|
86
|
+ @RequestParam(required = false) String textOffset) throws ThingsboardException {
|
|
|
87
|
+ try {
|
|
|
88
|
+ TextPageLink pageLink = createPageLink(limit, textSearch, idOffset, textOffset);
|
|
|
89
|
+ if (getCurrentUser().getAuthority() == Authority.SYS_ADMIN) {
|
|
|
90
|
+ return checkNotNull(ruleChainService.findSystemRuleChains(pageLink));
|
|
|
91
|
+ } else {
|
|
|
92
|
+ TenantId tenantId = getCurrentUser().getTenantId();
|
|
|
93
|
+ TextPageData<RuleChain> ruleChainsData = checkNotNull(ruleChainService.findAllTenantRuleChainsByTenantIdAndPageLink(tenantId, pageLink));
|
|
|
94
|
+ List<RuleChain> ruleChains = ruleChainsData.getData();
|
|
|
95
|
+ ruleChains.stream()
|
|
|
96
|
+ .filter(ruleChain -> ruleChain.getTenantId().getId().equals(ModelConstants.NULL_UUID))
|
|
|
97
|
+ .forEach(ruleChain -> ruleChain.setConfiguration(null));
|
|
|
98
|
+ return ruleChainsData;
|
|
|
99
|
+ }
|
|
|
100
|
+ } catch (Exception e) {
|
|
|
101
|
+ throw handleException(e);
|
|
|
102
|
+ }
|
|
|
103
|
+ }
|
|
|
104
|
+
|
|
|
105
|
+ @PreAuthorize("hasAuthority('SYS_ADMIN')")
|
|
|
106
|
+ @RequestMapping(value = "/system/ruleChains", params = {"limit"}, method = RequestMethod.GET)
|
|
|
107
|
+ @ResponseBody
|
|
|
108
|
+ public TextPageData<RuleChain> getSystemRuleChains(
|
|
|
109
|
+ @RequestParam int limit,
|
|
|
110
|
+ @RequestParam(required = false) String textSearch,
|
|
|
111
|
+ @RequestParam(required = false) String idOffset,
|
|
|
112
|
+ @RequestParam(required = false) String textOffset) throws ThingsboardException {
|
|
|
113
|
+ try {
|
|
|
114
|
+ TextPageLink pageLink = createPageLink(limit, textSearch, idOffset, textOffset);
|
|
|
115
|
+ return checkNotNull(ruleChainService.findSystemRuleChains(pageLink));
|
|
|
116
|
+ } catch (Exception e) {
|
|
|
117
|
+ throw handleException(e);
|
|
|
118
|
+ }
|
|
|
119
|
+ }
|
|
|
120
|
+
|
|
|
121
|
+ @PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
|
|
122
|
+ @RequestMapping(value = "/tenant/ruleChains", params = {"limit"}, method = RequestMethod.GET)
|
|
|
123
|
+ @ResponseBody
|
|
|
124
|
+ public TextPageData<RuleChain> getTenantRuleChains(
|
|
|
125
|
+ @RequestParam int limit,
|
|
|
126
|
+ @RequestParam(required = false) String textSearch,
|
|
|
127
|
+ @RequestParam(required = false) String idOffset,
|
|
|
128
|
+ @RequestParam(required = false) String textOffset) throws ThingsboardException {
|
|
|
129
|
+ try {
|
|
|
130
|
+ TenantId tenantId = getCurrentUser().getTenantId();
|
|
|
131
|
+ TextPageLink pageLink = createPageLink(limit, textSearch, idOffset, textOffset);
|
|
|
132
|
+ return checkNotNull(ruleChainService.findTenantRuleChains(tenantId, pageLink));
|
|
|
133
|
+ } catch (Exception e) {
|
|
|
134
|
+ throw handleException(e);
|
|
|
135
|
+ }
|
|
|
136
|
+ }
|
|
|
137
|
+
|
|
|
138
|
+ @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
|
|
|
139
|
+ @RequestMapping(value = "/ruleChain/{ruleChainId}", method = RequestMethod.DELETE)
|
|
|
140
|
+ @ResponseStatus(value = HttpStatus.OK)
|
|
|
141
|
+ public void deleteRuleChain(@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
|
|
142
|
+ checkParameter(RULE_CHAIN_ID, strRuleChainId);
|
|
|
143
|
+ try {
|
|
|
144
|
+ RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
|
|
|
145
|
+ RuleChain ruleChain = checkRuleChain(ruleChainId);
|
|
|
146
|
+ ruleChainService.deleteRuleChainById(ruleChainId);
|
|
|
147
|
+
|
|
|
148
|
+ logEntityAction(ruleChainId, ruleChain,
|
|
|
149
|
+ null,
|
|
|
150
|
+ ActionType.DELETED, null, strRuleChainId);
|
|
|
151
|
+
|
|
|
152
|
+ } catch (Exception e) {
|
|
|
153
|
+ logEntityAction(emptyId(EntityType.RULE_CHAIN),
|
|
|
154
|
+ null,
|
|
|
155
|
+ null,
|
|
|
156
|
+ ActionType.DELETED, e, strRuleChainId);
|
|
|
157
|
+ throw handleException(e);
|
|
|
158
|
+ }
|
|
|
159
|
+ }
|
|
|
160
|
+
|
|
|
161
|
+} |