Commit 9764f27a1085c4d6f69d452e86eba050a61f6fba

Authored by Igor Kulikov
1 parent 016bbefa

Rule Chain and Rule Node DAO.

Showing 27 changed files with 1376 additions and 54 deletions
  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 +
  17 +CREATE TABLE IF NOT EXISTS thingsboard.msg_queue (
  18 + node_id timeuuid,
  19 + clustered_hash bigint,
  20 + partition bigint,
  21 + ts bigint,
  22 + msg blob,
  23 + PRIMARY KEY ((node_id, clustered_hash, partition), ts))
  24 +WITH CLUSTERING ORDER BY (ts DESC)
  25 +AND compaction = {
  26 + 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy',
  27 + 'min_threshold': '5',
  28 + 'base_time_seconds': '43200',
  29 + 'max_window_size_seconds': '43200',
  30 + 'tombstone_threshold': '0.9',
  31 + 'unchecked_tombstone_compaction': 'true'
  32 +};
  33 +
  34 +CREATE TABLE IF NOT EXISTS thingsboard.msg_ack_queue (
  35 + node_id timeuuid,
  36 + clustered_hash bigint,
  37 + partition bigint,
  38 + msg_id timeuuid,
  39 + PRIMARY KEY ((node_id, clustered_hash, partition), msg_id))
  40 +WITH CLUSTERING ORDER BY (msg_id DESC)
  41 +AND compaction = {
  42 + 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy',
  43 + 'min_threshold': '5',
  44 + 'base_time_seconds': '43200',
  45 + 'max_window_size_seconds': '43200',
  46 + 'tombstone_threshold': '0.9',
  47 + 'unchecked_tombstone_compaction': 'true'
  48 +};
  49 +
  50 +CREATE TABLE IF NOT EXISTS thingsboard.processed_msg_partitions (
  51 + node_id timeuuid,
  52 + clustered_hash bigint,
  53 + partition bigint,
  54 + PRIMARY KEY ((node_id, clustered_hash), partition))
  55 +WITH CLUSTERING ORDER BY (partition DESC)
  56 +AND compaction = {
  57 + 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy',
  58 + 'min_threshold': '5',
  59 + 'base_time_seconds': '43200',
  60 + 'max_window_size_seconds': '43200',
  61 + 'tombstone_threshold': '0.9',
  62 + 'unchecked_tombstone_compaction': 'true'
  63 +};
  64 +
  65 +CREATE TABLE IF NOT EXISTS thingsboard.rule_chain (
  66 + id uuid,
  67 + tenant_id uuid,
  68 + name text,
  69 + search_text text,
  70 + first_rule_node_id uuid,
  71 + is_root boolean,
  72 + configuration text,
  73 + additional_info text,
  74 + PRIMARY KEY (id, tenant_id)
  75 +);
  76 +
  77 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.rule_chain_by_tenant_and_search_text AS
  78 + SELECT *
  79 + from thingsboard.rule_chain
  80 + WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL
  81 + PRIMARY KEY ( tenant_id, search_text, id )
  82 + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC );
  83 +
  84 +CREATE TABLE IF NOT EXISTS thingsboard.rule_node (
  85 + id uuid,
  86 + type text,
  87 + name text,
  88 + search_text text,
  89 + configuration text,
  90 + additional_info text,
  91 + PRIMARY KEY (id)
  92 +);
  93 +
... ...
  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 +
  17 +CREATE TABLE IF NOT EXISTS rule_chain (
  18 + id varchar(31) NOT NULL CONSTRAINT rule_chain_pkey PRIMARY KEY,
  19 + additional_info varchar,
  20 + configuration varchar(10000000),
  21 + name varchar(255),
  22 + first_rule_node_id varchar(31),
  23 + is_root boolean,
  24 + search_text varchar(255),
  25 + tenant_id varchar(31)
  26 +);
  27 +
  28 +CREATE TABLE IF NOT EXISTS rule_node (
  29 + id varchar(31) NOT NULL CONSTRAINT rule_node_pkey PRIMARY KEY,
  30 + additional_info varchar,
  31 + configuration varchar(10000000),
  32 + type varchar(255),
  33 + name varchar(255),
  34 + search_text varchar(255)
  35 +);
\ No newline at end of file
... ...
... ... @@ -77,11 +77,16 @@ public class ThingsboardInstallService {
77 77
78 78 databaseUpgradeService.upgradeDatabase("1.3.0");
79 79
80   - case "1.3.1":
  80 + case "1.3.1": //NOSONAR, Need to execute gradual upgrade starting from upgradeFromVersion
81 81 log.info("Upgrading ThingsBoard from version 1.3.1 to 1.4.0 ...");
82 82
83 83 databaseUpgradeService.upgradeDatabase("1.3.1");
84 84
  85 + case "1.4.0":
  86 + log.info("Upgrading ThingsBoard from version 1.4.0 to 1.5.0 ...");
  87 +
  88 + databaseUpgradeService.upgradeDatabase("1.4.0");
  89 +
85 90 log.info("Updating system data...");
86 91
87 92 systemDataLoaderService.deleteSystemWidgetBundle("charts");
... ...
... ... @@ -186,6 +186,14 @@ public class CassandraDatabaseUpgradeService implements DatabaseUpgradeService {
186 186 }
187 187 log.info("Dashboards restored.");
188 188 break;
  189 + case "1.4.0":
  190 +
  191 + log.info("Updating schema ...");
  192 + schemaUpdateFile = Paths.get(this.dataDir, "upgrade", "1.5.0", SCHEMA_UPDATE_CQL);
  193 + loadCql(schemaUpdateFile);
  194 + log.info("Schema updated.");
  195 +
  196 + break;
189 197 default:
190 198 throw new RuntimeException("Unable to upgrade Cassandra database, unsupported fromVersion: " + fromVersion);
191 199 }
... ...
... ... @@ -97,6 +97,15 @@ public class SqlDatabaseUpgradeService implements DatabaseUpgradeService {
97 97 log.info("Dashboards restored.");
98 98 }
99 99 break;
  100 + case "1.4.0":
  101 + try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
  102 + log.info("Updating schema ...");
  103 + schemaUpdateFile = Paths.get(this.dataDir, "upgrade", "1.5.0", SCHEMA_UPDATE_SQL);
  104 + String sql = new String(Files.readAllBytes(schemaUpdateFile), Charset.forName("UTF-8"));
  105 + conn.createStatement().execute(sql); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script
  106 + log.info("Schema updated.");
  107 + }
  108 + break;
100 109 default:
101 110 throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion);
102 111 }
... ...
... ... @@ -19,5 +19,5 @@ package org.thingsboard.server.common.data;
19 19 * @author Andrew Shvayka
20 20 */
21 21 public enum EntityType {
22   - TENANT, CUSTOMER, USER, RULE, PLUGIN, DASHBOARD, ASSET, DEVICE, ALARM
  22 + TENANT, CUSTOMER, USER, RULE, PLUGIN, DASHBOARD, ASSET, DEVICE, ALARM, RULE_CHAIN, RULE_NODE;
23 23 }
... ...
... ... @@ -53,6 +53,10 @@ public class EntityIdFactory {
53 53 return new AssetId(uuid);
54 54 case ALARM:
55 55 return new AlarmId(uuid);
  56 + case RULE_CHAIN:
  57 + return new RuleChainId(uuid);
  58 + case RULE_NODE:
  59 + return new RuleNodeId(uuid);
56 60 }
57 61 throw new IllegalArgumentException("EntityType " + type + " is not supported!");
58 62 }
... ...
  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.common.data.id;
  17 +
  18 +import com.fasterxml.jackson.annotation.JsonCreator;
  19 +import com.fasterxml.jackson.annotation.JsonIgnore;
  20 +import com.fasterxml.jackson.annotation.JsonProperty;
  21 +import org.thingsboard.server.common.data.EntityType;
  22 +
  23 +import java.util.UUID;
  24 +
  25 +public class RuleChainId extends UUIDBased implements EntityId {
  26 +
  27 + @JsonCreator
  28 + public RuleChainId(@JsonProperty("id") UUID id) {
  29 + super(id);
  30 + }
  31 +
  32 + @JsonIgnore
  33 + @Override
  34 + public EntityType getEntityType() {
  35 + return EntityType.RULE_CHAIN;
  36 + }
  37 +}
... ...
  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.common.data.id;
  17 +
  18 +import com.fasterxml.jackson.annotation.JsonCreator;
  19 +import com.fasterxml.jackson.annotation.JsonIgnore;
  20 +import com.fasterxml.jackson.annotation.JsonProperty;
  21 +import org.thingsboard.server.common.data.EntityType;
  22 +
  23 +import java.util.UUID;
  24 +
  25 +public class RuleNodeId extends UUIDBased implements EntityId {
  26 +
  27 + @JsonCreator
  28 + public RuleNodeId(@JsonProperty("id") UUID id) {
  29 + super(id);
  30 + }
  31 +
  32 + @JsonIgnore
  33 + @Override
  34 + public EntityType getEntityType() {
  35 + return EntityType.RULE_NODE;
  36 + }
  37 +}
... ...
  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.common.data.rule;
  17 +
  18 +import com.fasterxml.jackson.annotation.JsonIgnore;
  19 +import com.fasterxml.jackson.databind.JsonNode;
  20 +import lombok.Data;
  21 +import lombok.EqualsAndHashCode;
  22 +import lombok.extern.slf4j.Slf4j;
  23 +import org.thingsboard.server.common.data.HasName;
  24 +import org.thingsboard.server.common.data.SearchTextBasedWithAdditionalInfo;
  25 +import org.thingsboard.server.common.data.id.RuleChainId;
  26 +import org.thingsboard.server.common.data.id.RuleNodeId;
  27 +import org.thingsboard.server.common.data.id.TenantId;
  28 +
  29 +@Data
  30 +@EqualsAndHashCode(callSuper = true)
  31 +@Slf4j
  32 +public class RuleChain extends SearchTextBasedWithAdditionalInfo<RuleChainId> implements HasName {
  33 +
  34 + private static final long serialVersionUID = -5656679015121935465L;
  35 +
  36 + private TenantId tenantId;
  37 + private String name;
  38 + private RuleNodeId firstRuleNodeId;
  39 + private boolean isRoot;
  40 + private transient JsonNode configuration;
  41 + @JsonIgnore
  42 + private byte[] configurationBytes;
  43 +
  44 + public RuleChain() {
  45 + super();
  46 + }
  47 +
  48 + public RuleChain(RuleChainId id) {
  49 + super(id);
  50 + }
  51 +
  52 + public RuleChain(RuleChain ruleChain) {
  53 + super(ruleChain);
  54 + this.tenantId = ruleChain.getTenantId();
  55 + this.name = ruleChain.getName();
  56 + this.firstRuleNodeId = ruleChain.getFirstRuleNodeId();
  57 + this.isRoot = ruleChain.isRoot();
  58 + this.setConfiguration(ruleChain.getConfiguration());
  59 + }
  60 +
  61 + @Override
  62 + public String getSearchText() {
  63 + return getName();
  64 + }
  65 +
  66 + @Override
  67 + public String getName() {
  68 + return name;
  69 + }
  70 +
  71 + public JsonNode getConfiguration() {
  72 + return SearchTextBasedWithAdditionalInfo.getJson(() -> configuration, () -> configurationBytes);
  73 + }
  74 +
  75 + public void setConfiguration(JsonNode data) {
  76 + setJson(data, json -> this.configuration = json, bytes -> this.configurationBytes = bytes);
  77 + }
  78 +
  79 +}
... ...
  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.common.data.rule;
  17 +
  18 +import com.fasterxml.jackson.annotation.JsonIgnore;
  19 +import com.fasterxml.jackson.databind.JsonNode;
  20 +import lombok.Data;
  21 +import lombok.EqualsAndHashCode;
  22 +import lombok.extern.slf4j.Slf4j;
  23 +import org.thingsboard.server.common.data.HasName;
  24 +import org.thingsboard.server.common.data.SearchTextBasedWithAdditionalInfo;
  25 +import org.thingsboard.server.common.data.id.RuleNodeId;
  26 +import org.thingsboard.server.common.data.id.TenantId;
  27 +
  28 +@Data
  29 +@EqualsAndHashCode(callSuper = true)
  30 +@Slf4j
  31 +public class RuleNode extends SearchTextBasedWithAdditionalInfo<RuleNodeId> implements HasName {
  32 +
  33 + private static final long serialVersionUID = -5656679015121235465L;
  34 +
  35 + private String type;
  36 + private String name;
  37 + private transient JsonNode configuration;
  38 + @JsonIgnore
  39 + private byte[] configurationBytes;
  40 +
  41 + public RuleNode() {
  42 + super();
  43 + }
  44 +
  45 + public RuleNode(RuleNodeId id) {
  46 + super(id);
  47 + }
  48 +
  49 + public RuleNode(RuleNode ruleNode) {
  50 + super(ruleNode);
  51 + this.type = ruleNode.getType();
  52 + this.name = ruleNode.getName();
  53 + this.setConfiguration(ruleNode.getConfiguration());
  54 + }
  55 +
  56 + @Override
  57 + public String getSearchText() {
  58 + return getName();
  59 + }
  60 +
  61 + @Override
  62 + public String getName() {
  63 + return name;
  64 + }
  65 +
  66 + public JsonNode getConfiguration() {
  67 + return SearchTextBasedWithAdditionalInfo.getJson(() -> configuration, () -> configurationBytes);
  68 + }
  69 +
  70 + public void setConfiguration(JsonNode data) {
  71 + setJson(data, json -> this.configuration = json, bytes -> this.configurationBytes = bytes);
  72 + }
  73 +
  74 +}
... ...
... ... @@ -333,6 +333,26 @@ public class ModelConstants {
333 333 public static final String EVENT_BY_ID_VIEW_NAME = "event_by_id";
334 334
335 335 /**
  336 + * Cassandra rule chain constants.
  337 + */
  338 + public static final String RULE_CHAIN_COLUMN_FAMILY_NAME = "rule_chain";
  339 + public static final String RULE_CHAIN_TENANT_ID_PROPERTY = TENANT_ID_PROPERTY;
  340 + public static final String RULE_CHAIN_NAME_PROPERTY = "name";
  341 + public static final String RULE_CHAIN_FIRST_RULE_NODE_ID_PROPERTY = "first_rule_node_id";
  342 + public static final String RULE_CHAIN_IS_ROOT_PROPERTY = "is_root";
  343 + public static final String RULE_CHAIN_CONFIGURATION_PROPERTY = "configuration";
  344 +
  345 + public static final String RULE_CHAIN_BY_TENANT_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME = "rule_chain_by_tenant_and_search_text";
  346 +
  347 + /**
  348 + * Cassandra rule node constants.
  349 + */
  350 + public static final String RULE_NODE_COLUMN_FAMILY_NAME = "rule_node";
  351 + public static final String RULE_NODE_TYPE_PROPERTY = "type";
  352 + public static final String RULE_NODE_NAME_PROPERTY = "name";
  353 + public static final String RULE_NODE_CONFIGURATION_PROPERTY = "configuration";
  354 +
  355 + /**
336 356 * Cassandra attributes and timeseries constants.
337 357 */
338 358 public static final String ATTRIBUTES_KV_CF = "attributes_kv_cf";
... ...
  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.dao.model.nosql;
  17 +
  18 +import com.datastax.driver.core.utils.UUIDs;
  19 +import com.datastax.driver.mapping.annotations.ClusteringColumn;
  20 +import com.datastax.driver.mapping.annotations.Column;
  21 +import com.datastax.driver.mapping.annotations.PartitionKey;
  22 +import com.datastax.driver.mapping.annotations.Table;
  23 +import com.fasterxml.jackson.databind.JsonNode;
  24 +import lombok.EqualsAndHashCode;
  25 +import lombok.ToString;
  26 +import org.thingsboard.server.common.data.id.RuleChainId;
  27 +import org.thingsboard.server.common.data.id.RuleNodeId;
  28 +import org.thingsboard.server.common.data.id.TenantId;
  29 +import org.thingsboard.server.common.data.rule.RuleChain;
  30 +import org.thingsboard.server.dao.DaoUtil;
  31 +import org.thingsboard.server.dao.model.SearchTextEntity;
  32 +import org.thingsboard.server.dao.model.type.JsonCodec;
  33 +
  34 +import java.util.UUID;
  35 +
  36 +import static org.thingsboard.server.dao.model.ModelConstants.*;
  37 +
  38 +@Table(name = RULE_CHAIN_COLUMN_FAMILY_NAME)
  39 +@EqualsAndHashCode
  40 +@ToString
  41 +public class RuleChainEntity implements SearchTextEntity<RuleChain> {
  42 +
  43 + @PartitionKey
  44 + @Column(name = ID_PROPERTY)
  45 + private UUID id;
  46 + @ClusteringColumn
  47 + @Column(name = RULE_CHAIN_TENANT_ID_PROPERTY)
  48 + private UUID tenantId;
  49 + @Column(name = RULE_CHAIN_NAME_PROPERTY)
  50 + private String name;
  51 + @Column(name = SEARCH_TEXT_PROPERTY)
  52 + private String searchText;
  53 + @Column(name = RULE_CHAIN_FIRST_RULE_NODE_ID_PROPERTY)
  54 + private UUID firstRuleNodeId;
  55 + @Column(name = RULE_CHAIN_IS_ROOT_PROPERTY)
  56 + private boolean isRoot;
  57 + @Column(name = RULE_CHAIN_CONFIGURATION_PROPERTY, codec = JsonCodec.class)
  58 + private JsonNode configuration;
  59 + @Column(name = ADDITIONAL_INFO_PROPERTY, codec = JsonCodec.class)
  60 + private JsonNode additionalInfo;
  61 +
  62 + public RuleChainEntity() {
  63 + }
  64 +
  65 + public RuleChainEntity(RuleChain ruleChain) {
  66 + if (ruleChain.getId() != null) {
  67 + this.id = ruleChain.getUuidId();
  68 + }
  69 + this.tenantId = DaoUtil.getId(ruleChain.getTenantId());
  70 + this.name = ruleChain.getName();
  71 + this.searchText = ruleChain.getName();
  72 + this.firstRuleNodeId = DaoUtil.getId(ruleChain.getFirstRuleNodeId());
  73 + this.isRoot = ruleChain.isRoot();
  74 + this.configuration = ruleChain.getConfiguration();
  75 + this.additionalInfo = ruleChain.getAdditionalInfo();
  76 + }
  77 +
  78 + @Override
  79 + public String getSearchTextSource() {
  80 + return getSearchText();
  81 + }
  82 +
  83 + @Override
  84 + public void setSearchText(String searchText) {
  85 + this.searchText = searchText;
  86 + }
  87 +
  88 + @Override
  89 + public UUID getId() {
  90 + return id;
  91 + }
  92 +
  93 + @Override
  94 + public void setId(UUID id) {
  95 + this.id = id;
  96 + }
  97 +
  98 + public UUID getTenantId() {
  99 + return tenantId;
  100 + }
  101 +
  102 + public void setTenantId(UUID tenantId) {
  103 + this.tenantId = tenantId;
  104 + }
  105 +
  106 + public String getName() {
  107 + return name;
  108 + }
  109 +
  110 + public void setName(String name) {
  111 + this.name = name;
  112 + }
  113 +
  114 + public UUID getFirstRuleNodeId() {
  115 + return firstRuleNodeId;
  116 + }
  117 +
  118 + public void setFirstRuleNodeId(UUID firstRuleNodeId) {
  119 + this.firstRuleNodeId = firstRuleNodeId;
  120 + }
  121 +
  122 + public boolean isRoot() {
  123 + return isRoot;
  124 + }
  125 +
  126 + public void setRoot(boolean isRoot) {
  127 + this.isRoot = isRoot;
  128 + }
  129 +
  130 + public String getSearchText() {
  131 + return searchText;
  132 + }
  133 +
  134 + public JsonNode getConfiguration() {
  135 + return configuration;
  136 + }
  137 +
  138 + public void setConfiguration(JsonNode configuration) {
  139 + this.configuration = configuration;
  140 + }
  141 +
  142 + public JsonNode getAdditionalInfo() {
  143 + return additionalInfo;
  144 + }
  145 +
  146 + public void setAdditionalInfo(JsonNode additionalInfo) {
  147 + this.additionalInfo = additionalInfo;
  148 + }
  149 +
  150 + @Override
  151 + public RuleChain toData() {
  152 + RuleChain ruleChain = new RuleChain(new RuleChainId(id));
  153 + ruleChain.setCreatedTime(UUIDs.unixTimestamp(id));
  154 + ruleChain.setTenantId(new TenantId(tenantId));
  155 + ruleChain.setName(name);
  156 + if (this.firstRuleNodeId != null) {
  157 + ruleChain.setFirstRuleNodeId(new RuleNodeId(this.firstRuleNodeId));
  158 + }
  159 + ruleChain.setRoot(this.isRoot);
  160 + ruleChain.setConfiguration(this.configuration);
  161 + ruleChain.setAdditionalInfo(this.additionalInfo);
  162 + return ruleChain;
  163 + }
  164 +
  165 +}
... ...
  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.dao.model.nosql;
  17 +
  18 +import com.datastax.driver.core.utils.UUIDs;
  19 +import com.datastax.driver.mapping.annotations.Column;
  20 +import com.datastax.driver.mapping.annotations.PartitionKey;
  21 +import com.datastax.driver.mapping.annotations.Table;
  22 +import com.fasterxml.jackson.databind.JsonNode;
  23 +import lombok.EqualsAndHashCode;
  24 +import lombok.ToString;
  25 +import org.thingsboard.server.common.data.id.RuleNodeId;
  26 +import org.thingsboard.server.common.data.rule.RuleNode;
  27 +import org.thingsboard.server.dao.model.SearchTextEntity;
  28 +import org.thingsboard.server.dao.model.type.JsonCodec;
  29 +
  30 +import java.util.UUID;
  31 +
  32 +import static org.thingsboard.server.dao.model.ModelConstants.*;
  33 +
  34 +@Table(name = RULE_NODE_COLUMN_FAMILY_NAME)
  35 +@EqualsAndHashCode
  36 +@ToString
  37 +public class RuleNodeEntity implements SearchTextEntity<RuleNode> {
  38 +
  39 + @PartitionKey
  40 + @Column(name = ID_PROPERTY)
  41 + private UUID id;
  42 + @Column(name = RULE_NODE_TYPE_PROPERTY)
  43 + private String type;
  44 + @Column(name = RULE_NODE_NAME_PROPERTY)
  45 + private String name;
  46 + @Column(name = SEARCH_TEXT_PROPERTY)
  47 + private String searchText;
  48 + @Column(name = RULE_NODE_CONFIGURATION_PROPERTY, codec = JsonCodec.class)
  49 + private JsonNode configuration;
  50 + @Column(name = ADDITIONAL_INFO_PROPERTY, codec = JsonCodec.class)
  51 + private JsonNode additionalInfo;
  52 +
  53 + public RuleNodeEntity() {
  54 + }
  55 +
  56 + public RuleNodeEntity(RuleNode ruleNode) {
  57 + if (ruleNode.getId() != null) {
  58 + this.id = ruleNode.getUuidId();
  59 + }
  60 + this.type = ruleNode.getType();
  61 + this.name = ruleNode.getName();
  62 + this.searchText = ruleNode.getName();
  63 + this.configuration = ruleNode.getConfiguration();
  64 + this.additionalInfo = ruleNode.getAdditionalInfo();
  65 + }
  66 +
  67 + @Override
  68 + public String getSearchTextSource() {
  69 + return getSearchText();
  70 + }
  71 +
  72 + @Override
  73 + public void setSearchText(String searchText) {
  74 + this.searchText = searchText;
  75 + }
  76 +
  77 + @Override
  78 + public UUID getId() {
  79 + return id;
  80 + }
  81 +
  82 + @Override
  83 + public void setId(UUID id) {
  84 + this.id = id;
  85 + }
  86 +
  87 + public String getType() {
  88 + return type;
  89 + }
  90 +
  91 + public void setType(String type) {
  92 + this.type = type;
  93 + }
  94 +
  95 + public String getName() {
  96 + return name;
  97 + }
  98 +
  99 + public void setName(String name) {
  100 + this.name = name;
  101 + }
  102 +
  103 + public String getSearchText() {
  104 + return searchText;
  105 + }
  106 +
  107 + public JsonNode getConfiguration() {
  108 + return configuration;
  109 + }
  110 +
  111 + public void setConfiguration(JsonNode configuration) {
  112 + this.configuration = configuration;
  113 + }
  114 +
  115 + public JsonNode getAdditionalInfo() {
  116 + return additionalInfo;
  117 + }
  118 +
  119 + public void setAdditionalInfo(JsonNode additionalInfo) {
  120 + this.additionalInfo = additionalInfo;
  121 + }
  122 +
  123 + @Override
  124 + public RuleNode toData() {
  125 + RuleNode ruleNode = new RuleNode(new RuleNodeId(id));
  126 + ruleNode.setCreatedTime(UUIDs.unixTimestamp(id));
  127 + ruleNode.setType(this.type);
  128 + ruleNode.setName(this.name);
  129 + ruleNode.setConfiguration(this.configuration);
  130 + ruleNode.setAdditionalInfo(this.additionalInfo);
  131 + return ruleNode;
  132 + }
  133 +
  134 +}
... ...
  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.dao.model.sql;
  17 +
  18 +import com.datastax.driver.core.utils.UUIDs;
  19 +import com.fasterxml.jackson.databind.JsonNode;
  20 +import lombok.Data;
  21 +import lombok.EqualsAndHashCode;
  22 +import org.hibernate.annotations.Type;
  23 +import org.hibernate.annotations.TypeDef;
  24 +import org.thingsboard.server.common.data.UUIDConverter;
  25 +import org.thingsboard.server.common.data.id.RuleChainId;
  26 +import org.thingsboard.server.common.data.id.RuleNodeId;
  27 +import org.thingsboard.server.common.data.id.TenantId;
  28 +import org.thingsboard.server.common.data.rule.RuleChain;
  29 +import org.thingsboard.server.dao.DaoUtil;
  30 +import org.thingsboard.server.dao.model.BaseSqlEntity;
  31 +import org.thingsboard.server.dao.model.ModelConstants;
  32 +import org.thingsboard.server.dao.model.SearchTextEntity;
  33 +import org.thingsboard.server.dao.util.mapping.JsonStringType;
  34 +
  35 +import javax.persistence.Column;
  36 +import javax.persistence.Entity;
  37 +import javax.persistence.Table;
  38 +
  39 +@Data
  40 +@EqualsAndHashCode(callSuper = true)
  41 +@Entity
  42 +@TypeDef(name = "json", typeClass = JsonStringType.class)
  43 +@Table(name = ModelConstants.RULE_CHAIN_COLUMN_FAMILY_NAME)
  44 +public class RuleChainEntity extends BaseSqlEntity<RuleChain> implements SearchTextEntity<RuleChain> {
  45 +
  46 + @Column(name = ModelConstants.RULE_CHAIN_TENANT_ID_PROPERTY)
  47 + private String tenantId;
  48 +
  49 + @Column(name = ModelConstants.RULE_CHAIN_NAME_PROPERTY)
  50 + private String name;
  51 +
  52 + @Column(name = ModelConstants.SEARCH_TEXT_PROPERTY)
  53 + private String searchText;
  54 +
  55 + @Column(name = ModelConstants.RULE_CHAIN_FIRST_RULE_NODE_ID_PROPERTY)
  56 + private String firstRuleNodeId;
  57 +
  58 + @Column(name = ModelConstants.RULE_CHAIN_IS_ROOT_PROPERTY)
  59 + private boolean isRoot;
  60 +
  61 + @Type(type = "json")
  62 + @Column(name = ModelConstants.RULE_CHAIN_CONFIGURATION_PROPERTY)
  63 + private JsonNode configuration;
  64 +
  65 + @Type(type = "json")
  66 + @Column(name = ModelConstants.ADDITIONAL_INFO_PROPERTY)
  67 + private JsonNode additionalInfo;
  68 +
  69 + public RuleChainEntity() {
  70 + }
  71 +
  72 + public RuleChainEntity(RuleChain ruleChain) {
  73 + if (ruleChain.getId() != null) {
  74 + this.setId(ruleChain.getUuidId());
  75 + }
  76 + this.tenantId = toString(DaoUtil.getId(ruleChain.getTenantId()));
  77 + this.name = ruleChain.getName();
  78 + this.searchText = ruleChain.getName();
  79 + if (ruleChain.getFirstRuleNodeId() != null) {
  80 + this.firstRuleNodeId = UUIDConverter.fromTimeUUID(ruleChain.getFirstRuleNodeId().getId());
  81 + }
  82 + this.isRoot = ruleChain.isRoot();
  83 + this.configuration = ruleChain.getConfiguration();
  84 + this.additionalInfo = ruleChain.getAdditionalInfo();
  85 + }
  86 +
  87 + @Override
  88 + public String getSearchTextSource() {
  89 + return searchText;
  90 + }
  91 +
  92 + @Override
  93 + public void setSearchText(String searchText) {
  94 + this.searchText = searchText;
  95 + }
  96 +
  97 + @Override
  98 + public RuleChain toData() {
  99 + RuleChain ruleChain = new RuleChain(new RuleChainId(getId()));
  100 + ruleChain.setCreatedTime(UUIDs.unixTimestamp(getId()));
  101 + ruleChain.setTenantId(new TenantId(toUUID(tenantId)));
  102 + ruleChain.setName(name);
  103 + if (firstRuleNodeId != null) {
  104 + ruleChain.setFirstRuleNodeId(new RuleNodeId(UUIDConverter.fromString(firstRuleNodeId)));
  105 + }
  106 + ruleChain.setRoot(isRoot);
  107 + ruleChain.setConfiguration(configuration);
  108 + ruleChain.setAdditionalInfo(additionalInfo);
  109 + return ruleChain;
  110 + }
  111 +}
... ...
  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.dao.model.sql;
  17 +
  18 +import com.datastax.driver.core.utils.UUIDs;
  19 +import com.fasterxml.jackson.databind.JsonNode;
  20 +import lombok.Data;
  21 +import lombok.EqualsAndHashCode;
  22 +import org.hibernate.annotations.Type;
  23 +import org.hibernate.annotations.TypeDef;
  24 +import org.thingsboard.server.common.data.id.RuleNodeId;
  25 +import org.thingsboard.server.common.data.rule.RuleNode;
  26 +import org.thingsboard.server.dao.model.BaseSqlEntity;
  27 +import org.thingsboard.server.dao.model.ModelConstants;
  28 +import org.thingsboard.server.dao.model.SearchTextEntity;
  29 +import org.thingsboard.server.dao.util.mapping.JsonStringType;
  30 +
  31 +import javax.persistence.Column;
  32 +import javax.persistence.Entity;
  33 +import javax.persistence.Table;
  34 +
  35 +@Data
  36 +@EqualsAndHashCode(callSuper = true)
  37 +@Entity
  38 +@TypeDef(name = "json", typeClass = JsonStringType.class)
  39 +@Table(name = ModelConstants.RULE_NODE_COLUMN_FAMILY_NAME)
  40 +public class RuleNodeEntity extends BaseSqlEntity<RuleNode> implements SearchTextEntity<RuleNode> {
  41 +
  42 + @Column(name = ModelConstants.RULE_NODE_TYPE_PROPERTY)
  43 + private String type;
  44 +
  45 + @Column(name = ModelConstants.RULE_NODE_NAME_PROPERTY)
  46 + private String name;
  47 +
  48 + @Column(name = ModelConstants.SEARCH_TEXT_PROPERTY)
  49 + private String searchText;
  50 +
  51 + @Type(type = "json")
  52 + @Column(name = ModelConstants.RULE_NODE_CONFIGURATION_PROPERTY)
  53 + private JsonNode configuration;
  54 +
  55 + @Type(type = "json")
  56 + @Column(name = ModelConstants.ADDITIONAL_INFO_PROPERTY)
  57 + private JsonNode additionalInfo;
  58 +
  59 + public RuleNodeEntity() {
  60 + }
  61 +
  62 + public RuleNodeEntity(RuleNode ruleNode) {
  63 + if (ruleNode.getId() != null) {
  64 + this.setId(ruleNode.getUuidId());
  65 + }
  66 + this.type = ruleNode.getType();
  67 + this.name = ruleNode.getName();
  68 + this.searchText = ruleNode.getName();
  69 + this.configuration = ruleNode.getConfiguration();
  70 + this.additionalInfo = ruleNode.getAdditionalInfo();
  71 + }
  72 +
  73 + @Override
  74 + public String getSearchTextSource() {
  75 + return searchText;
  76 + }
  77 +
  78 + @Override
  79 + public void setSearchText(String searchText) {
  80 + this.searchText = searchText;
  81 + }
  82 +
  83 + @Override
  84 + public RuleNode toData() {
  85 + RuleNode ruleNode = new RuleNode(new RuleNodeId(getId()));
  86 + ruleNode.setCreatedTime(UUIDs.unixTimestamp(getId()));
  87 + ruleNode.setType(type);
  88 + ruleNode.setName(name);
  89 + ruleNode.setConfiguration(configuration);
  90 + ruleNode.setAdditionalInfo(additionalInfo);
  91 + return ruleNode;
  92 + }
  93 +}
... ...
  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.dao.rule;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.springframework.stereotype.Component;
  20 +import org.thingsboard.server.common.data.page.TextPageLink;
  21 +import org.thingsboard.server.common.data.rule.RuleChain;
  22 +import org.thingsboard.server.dao.DaoUtil;
  23 +import org.thingsboard.server.dao.model.nosql.RuleChainEntity;
  24 +import org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao;
  25 +import org.thingsboard.server.dao.util.NoSqlDao;
  26 +
  27 +import java.util.Collections;
  28 +import java.util.List;
  29 +import java.util.UUID;
  30 +
  31 +import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
  32 +import static org.thingsboard.server.dao.model.ModelConstants.*;
  33 +
  34 +@Component
  35 +@Slf4j
  36 +@NoSqlDao
  37 +public class CassandraRuleChainDao extends CassandraAbstractSearchTextDao<RuleChainEntity, RuleChain> implements RuleChainDao {
  38 +
  39 + @Override
  40 + protected Class<RuleChainEntity> getColumnFamilyClass() {
  41 + return RuleChainEntity.class;
  42 + }
  43 +
  44 + @Override
  45 + protected String getColumnFamilyName() {
  46 + return RULE_CHAIN_COLUMN_FAMILY_NAME;
  47 + }
  48 +
  49 + @Override
  50 + public List<RuleChain> findRuleChainsByTenantId(UUID tenantId, TextPageLink pageLink) {
  51 + log.debug("Try to find rule chains by tenantId [{}] and pageLink [{}]", tenantId, pageLink);
  52 + List<RuleChainEntity> ruleChainEntities = findPageWithTextSearch(RULE_CHAIN_BY_TENANT_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME,
  53 + Collections.singletonList(eq(RULE_CHAIN_TENANT_ID_PROPERTY, tenantId)),
  54 + pageLink);
  55 +
  56 + log.trace("Found rule chains [{}] by tenantId [{}] and pageLink [{}]", ruleChainEntities, tenantId, pageLink);
  57 + return DaoUtil.convertDataList(ruleChainEntities);
  58 + }
  59 +
  60 +}
... ...
  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.dao.rule;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.springframework.stereotype.Component;
  20 +import org.thingsboard.server.common.data.rule.RuleNode;
  21 +import org.thingsboard.server.dao.model.nosql.RuleNodeEntity;
  22 +import org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao;
  23 +import org.thingsboard.server.dao.util.NoSqlDao;
  24 +
  25 +import static org.thingsboard.server.dao.model.ModelConstants.RULE_NODE_COLUMN_FAMILY_NAME;
  26 +
  27 +@Component
  28 +@Slf4j
  29 +@NoSqlDao
  30 +public class CassandraRuleNodeDao extends CassandraAbstractSearchTextDao<RuleNodeEntity, RuleNode> implements RuleNodeDao {
  31 +
  32 + @Override
  33 + protected Class<RuleNodeEntity> getColumnFamilyClass() {
  34 + return RuleNodeEntity.class;
  35 + }
  36 +
  37 + @Override
  38 + protected String getColumnFamilyName() {
  39 + return RULE_NODE_COLUMN_FAMILY_NAME;
  40 + }
  41 +
  42 +}
... ...
  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 +
  17 +package org.thingsboard.server.dao.rule;
  18 +
  19 +import org.thingsboard.server.common.data.page.TextPageLink;
  20 +import org.thingsboard.server.common.data.rule.RuleChain;
  21 +import org.thingsboard.server.dao.Dao;
  22 +
  23 +import java.util.List;
  24 +import java.util.UUID;
  25 +
  26 +/**
  27 + * Created by igor on 3/12/18.
  28 + */
  29 +public interface RuleChainDao extends Dao<RuleChain> {
  30 +
  31 + /**
  32 + * Find rule chains by tenantId and page link.
  33 + *
  34 + * @param tenantId the tenantId
  35 + * @param pageLink the page link
  36 + * @return the list of rule chain objects
  37 + */
  38 + List<RuleChain> findRuleChainsByTenantId(UUID tenantId, TextPageLink pageLink);
  39 +
  40 +}
... ...
  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 +
  17 +package org.thingsboard.server.dao.rule;
  18 +
  19 +import org.thingsboard.server.common.data.rule.RuleChain;
  20 +
  21 +/**
  22 + * Created by igor on 3/12/18.
  23 + */
  24 +public interface RuleChainService {
  25 +
  26 + RuleChain saveRuleChain(RuleChain ruleChain);
  27 +
  28 +}
... ...
  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 +
  17 +package org.thingsboard.server.dao.rule;
  18 +
  19 +import org.thingsboard.server.common.data.rule.RuleNode;
  20 +import org.thingsboard.server.dao.Dao;
  21 +
  22 +/**
  23 + * Created by igor on 3/12/18.
  24 + */
  25 +public interface RuleNodeDao extends Dao<RuleNode> {
  26 +
  27 +}
... ...
  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.dao.sql.rule;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.springframework.beans.factory.annotation.Autowired;
  20 +import org.springframework.data.domain.PageRequest;
  21 +import org.springframework.data.repository.CrudRepository;
  22 +import org.springframework.stereotype.Component;
  23 +import org.thingsboard.server.common.data.UUIDConverter;
  24 +import org.thingsboard.server.common.data.page.TextPageLink;
  25 +import org.thingsboard.server.common.data.rule.RuleChain;
  26 +import org.thingsboard.server.dao.DaoUtil;
  27 +import org.thingsboard.server.dao.model.sql.RuleChainEntity;
  28 +import org.thingsboard.server.dao.rule.RuleChainDao;
  29 +import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao;
  30 +import org.thingsboard.server.dao.util.SqlDao;
  31 +
  32 +import java.util.List;
  33 +import java.util.Objects;
  34 +import java.util.UUID;
  35 +
  36 +import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID_STR;
  37 +
  38 +@Slf4j
  39 +@Component
  40 +@SqlDao
  41 +public class JpaRuleChainDao extends JpaAbstractSearchTextDao<RuleChainEntity, RuleChain> implements RuleChainDao {
  42 +
  43 + @Autowired
  44 + private RuleChainRepository ruleChainRepository;
  45 +
  46 + @Override
  47 + protected Class getEntityClass() {
  48 + return RuleChainEntity.class;
  49 + }
  50 +
  51 + @Override
  52 + protected CrudRepository getCrudRepository() {
  53 + return ruleChainRepository;
  54 + }
  55 +
  56 + @Override
  57 + public List<RuleChain> findRuleChainsByTenantId(UUID tenantId, TextPageLink pageLink) {
  58 + return DaoUtil.convertDataList(ruleChainRepository
  59 + .findByTenantId(
  60 + UUIDConverter.fromTimeUUID(tenantId),
  61 + Objects.toString(pageLink.getTextSearch(), ""),
  62 + pageLink.getIdOffset() == null ? NULL_UUID_STR : UUIDConverter.fromTimeUUID(pageLink.getIdOffset()),
  63 + new PageRequest(0, pageLink.getLimit())));
  64 + }
  65 +
  66 +}
... ...
  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.dao.sql.rule;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.springframework.beans.factory.annotation.Autowired;
  20 +import org.springframework.data.repository.CrudRepository;
  21 +import org.springframework.stereotype.Component;
  22 +import org.thingsboard.server.common.data.rule.RuleNode;
  23 +import org.thingsboard.server.dao.model.sql.RuleNodeEntity;
  24 +import org.thingsboard.server.dao.rule.RuleNodeDao;
  25 +import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao;
  26 +import org.thingsboard.server.dao.util.SqlDao;
  27 +
  28 +@Slf4j
  29 +@Component
  30 +@SqlDao
  31 +public class JpaRuleNodeDao extends JpaAbstractSearchTextDao<RuleNodeEntity, RuleNode> implements RuleNodeDao {
  32 +
  33 + @Autowired
  34 + private RuleNodeRepository ruleNodeRepository;
  35 +
  36 + @Override
  37 + protected Class getEntityClass() {
  38 + return RuleNodeEntity.class;
  39 + }
  40 +
  41 + @Override
  42 + protected CrudRepository getCrudRepository() {
  43 + return ruleNodeRepository;
  44 + }
  45 +
  46 +}
... ...
  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.dao.sql.rule;
  17 +
  18 +import org.springframework.data.domain.Pageable;
  19 +import org.springframework.data.jpa.repository.Query;
  20 +import org.springframework.data.repository.CrudRepository;
  21 +import org.springframework.data.repository.query.Param;
  22 +import org.thingsboard.server.dao.model.sql.RuleChainEntity;
  23 +import org.thingsboard.server.dao.util.SqlDao;
  24 +
  25 +import java.util.List;
  26 +
  27 +@SqlDao
  28 +public interface RuleChainRepository extends CrudRepository<RuleChainEntity, String> {
  29 +
  30 + @Query("SELECT rc FROM RuleChainEntity rc WHERE rc.tenantId = :tenantId " +
  31 + "AND LOWER(rc.searchText) LIKE LOWER(CONCAT(:searchText, '%')) " +
  32 + "AND rc.id > :idOffset ORDER BY rc.id")
  33 + List<RuleChainEntity> findByTenantId(@Param("tenantId") String tenantId,
  34 + @Param("searchText") String searchText,
  35 + @Param("idOffset") String idOffset,
  36 + Pageable pageable);
  37 +
  38 +}
... ...
  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.dao.sql.rule;
  17 +
  18 +import org.springframework.data.repository.CrudRepository;
  19 +import org.thingsboard.server.dao.model.sql.RuleNodeEntity;
  20 +import org.thingsboard.server.dao.util.SqlDao;
  21 +
  22 +@SqlDao
  23 +public interface RuleNodeRepository extends CrudRepository<RuleNodeEntity, String> {
  24 +
  25 +}
... ...
... ... @@ -542,55 +542,6 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.event_by_id AS
542 542 PRIMARY KEY ((tenant_id, entity_type, entity_id), id, event_type, event_uid)
543 543 WITH CLUSTERING ORDER BY (id ASC, event_type ASC, event_uid ASC);
544 544
545   -CREATE TABLE IF NOT EXISTS thingsboard.msg_queue (
546   - node_id timeuuid,
547   - clustered_hash bigint,
548   - partition bigint,
549   - ts bigint,
550   - msg blob,
551   - PRIMARY KEY ((node_id, clustered_hash, partition), ts))
552   -WITH CLUSTERING ORDER BY (ts DESC)
553   -AND compaction = {
554   - 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy',
555   - 'min_threshold': '5',
556   - 'base_time_seconds': '43200',
557   - 'max_window_size_seconds': '43200',
558   - 'tombstone_threshold': '0.9',
559   - 'unchecked_tombstone_compaction': 'true'
560   -};
561   -
562   -
563   -CREATE TABLE IF NOT EXISTS thingsboard.msg_ack_queue (
564   - node_id timeuuid,
565   - clustered_hash bigint,
566   - partition bigint,
567   - msg_id timeuuid,
568   - PRIMARY KEY ((node_id, clustered_hash, partition), msg_id))
569   -WITH CLUSTERING ORDER BY (msg_id DESC)
570   -AND compaction = {
571   - 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy',
572   - 'min_threshold': '5',
573   - 'base_time_seconds': '43200',
574   - 'max_window_size_seconds': '43200',
575   - 'tombstone_threshold': '0.9',
576   - 'unchecked_tombstone_compaction': 'true'
577   -};
578   -
579   -CREATE TABLE IF NOT EXISTS thingsboard.processed_msg_partitions (
580   - node_id timeuuid,
581   - clustered_hash bigint,
582   - partition bigint,
583   - PRIMARY KEY ((node_id, clustered_hash), partition))
584   -WITH CLUSTERING ORDER BY (partition DESC)
585   -AND compaction = {
586   - 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy',
587   - 'min_threshold': '5',
588   - 'base_time_seconds': '43200',
589   - 'max_window_size_seconds': '43200',
590   - 'tombstone_threshold': '0.9',
591   - 'unchecked_tombstone_compaction': 'true'
592   -};
593   -
594 545 CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_entity_id (
595 546 tenant_id timeuuid,
596 547 id timeuuid,
... ... @@ -639,8 +590,6 @@ CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_user_id (
639 590 PRIMARY KEY ((tenant_id, user_id), id)
640 591 );
641 592
642   -
643   -
644 593 CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_tenant_id (
645 594 tenant_id timeuuid,
646 595 id timeuuid,
... ... @@ -664,3 +613,80 @@ CREATE TABLE IF NOT EXISTS thingsboard.audit_log_by_tenant_id_partitions (
664 613 PRIMARY KEY (( tenant_id ), partition)
665 614 ) WITH CLUSTERING ORDER BY ( partition ASC )
666 615 AND compaction = { 'class' : 'LeveledCompactionStrategy' };
  616 +
  617 +CREATE TABLE IF NOT EXISTS thingsboard.msg_queue (
  618 + node_id timeuuid,
  619 + clustered_hash bigint,
  620 + partition bigint,
  621 + ts bigint,
  622 + msg blob,
  623 + PRIMARY KEY ((node_id, clustered_hash, partition), ts))
  624 +WITH CLUSTERING ORDER BY (ts DESC)
  625 +AND compaction = {
  626 + 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy',
  627 + 'min_threshold': '5',
  628 + 'base_time_seconds': '43200',
  629 + 'max_window_size_seconds': '43200',
  630 + 'tombstone_threshold': '0.9',
  631 + 'unchecked_tombstone_compaction': 'true'
  632 +};
  633 +
  634 +CREATE TABLE IF NOT EXISTS thingsboard.msg_ack_queue (
  635 + node_id timeuuid,
  636 + clustered_hash bigint,
  637 + partition bigint,
  638 + msg_id timeuuid,
  639 + PRIMARY KEY ((node_id, clustered_hash, partition), msg_id))
  640 +WITH CLUSTERING ORDER BY (msg_id DESC)
  641 +AND compaction = {
  642 + 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy',
  643 + 'min_threshold': '5',
  644 + 'base_time_seconds': '43200',
  645 + 'max_window_size_seconds': '43200',
  646 + 'tombstone_threshold': '0.9',
  647 + 'unchecked_tombstone_compaction': 'true'
  648 +};
  649 +
  650 +CREATE TABLE IF NOT EXISTS thingsboard.processed_msg_partitions (
  651 + node_id timeuuid,
  652 + clustered_hash bigint,
  653 + partition bigint,
  654 + PRIMARY KEY ((node_id, clustered_hash), partition))
  655 +WITH CLUSTERING ORDER BY (partition DESC)
  656 +AND compaction = {
  657 + 'class': 'org.apache.cassandra.db.compaction.DateTieredCompactionStrategy',
  658 + 'min_threshold': '5',
  659 + 'base_time_seconds': '43200',
  660 + 'max_window_size_seconds': '43200',
  661 + 'tombstone_threshold': '0.9',
  662 + 'unchecked_tombstone_compaction': 'true'
  663 +};
  664 +
  665 +CREATE TABLE IF NOT EXISTS thingsboard.rule_chain (
  666 + id uuid,
  667 + tenant_id uuid,
  668 + name text,
  669 + search_text text,
  670 + first_rule_node_id uuid,
  671 + is_root boolean,
  672 + configuration text,
  673 + additional_info text,
  674 + PRIMARY KEY (id, tenant_id)
  675 +);
  676 +
  677 +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.rule_chain_by_tenant_and_search_text AS
  678 + SELECT *
  679 + from thingsboard.rule_chain
  680 + WHERE tenant_id IS NOT NULL AND search_text IS NOT NULL AND id IS NOT NULL
  681 + PRIMARY KEY ( tenant_id, search_text, id )
  682 + WITH CLUSTERING ORDER BY ( search_text ASC, id DESC );
  683 +
  684 +CREATE TABLE IF NOT EXISTS thingsboard.rule_node (
  685 + id uuid,
  686 + type text,
  687 + name text,
  688 + search_text text,
  689 + configuration text,
  690 + additional_info text,
  691 + PRIMARY KEY (id)
  692 +);
\ No newline at end of file
... ...
... ... @@ -254,4 +254,24 @@ CREATE TABLE IF NOT EXISTS widgets_bundle (
254 254 search_text varchar(255),
255 255 tenant_id varchar(31),
256 256 title varchar(255)
257   -);
\ No newline at end of file
  257 +);
  258 +
  259 +CREATE TABLE IF NOT EXISTS rule_chain (
  260 + id varchar(31) NOT NULL CONSTRAINT rule_chain_pkey PRIMARY KEY,
  261 + additional_info varchar,
  262 + configuration varchar(10000000),
  263 + name varchar(255),
  264 + first_rule_node_id varchar(31),
  265 + is_root boolean,
  266 + search_text varchar(255),
  267 + tenant_id varchar(31)
  268 +);
  269 +
  270 +CREATE TABLE IF NOT EXISTS rule_node (
  271 + id varchar(31) NOT NULL CONSTRAINT rule_node_pkey PRIMARY KEY,
  272 + additional_info varchar,
  273 + configuration varchar(10000000),
  274 + type varchar(255),
  275 + name varchar(255),
  276 + search_text varchar(255)
  277 +);
... ...