Commit cdb9520ccfe9487399bf6eb6df092e9eddbaabfb

Authored by Igor Kulikov
1 parent c19adf36

Remove Entities NoSql DAO

Showing 49 changed files with 9 additions and 4767 deletions

Too many changes to show.

To preserve performance only 49 of 103 files are displayed.

1 -/**  
2 - * Copyright © 2016-2019 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.service.install;  
17 -  
18 -import org.springframework.context.annotation.Profile;  
19 -import org.springframework.stereotype.Service;  
20 -import org.thingsboard.server.dao.util.NoSqlDao;  
21 -  
22 -@Service  
23 -@NoSqlDao  
24 -@Profile("install")  
25 -public class CassandraEntityDatabaseSchemaService extends CassandraAbstractDatabaseSchemaService  
26 - implements EntityDatabaseSchemaService {  
27 - public CassandraEntityDatabaseSchemaService() {  
28 - super("schema-entities.cql");  
29 - }  
30 -}  
@@ -116,11 +116,8 @@ dashboard: @@ -116,11 +116,8 @@ dashboard:
116 116
117 database: 117 database:
118 ts_max_intervals: "${DATABASE_TS_MAX_INTERVALS:700}" # Max number of DB queries generated by single API call to fetch telemetry records 118 ts_max_intervals: "${DATABASE_TS_MAX_INTERVALS:700}" # Max number of DB queries generated by single API call to fetch telemetry records
119 - entities:  
120 - type: "${DATABASE_ENTITIES_TYPE:sql}" # cassandra OR sql  
121 ts: 119 ts:
122 - type: "${DATABASE_TS_TYPE:sql}" # cassandra OR sql (for hybrid mode, only this value should be cassandra)  
123 - 120 + type: "${DATABASE_TS_TYPE:sql}" # cassandra OR sql
124 121
125 # Cassandra driver configuration parameters 122 # Cassandra driver configuration parameters
126 cassandra: 123 cassandra:
1 -/**  
2 - * Copyright © 2016-2019 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.cassandraunit.dataset.cql.ClassPathCQLDataSet;  
19 -import org.junit.ClassRule;  
20 -import org.junit.extensions.cpsuite.ClasspathSuite;  
21 -import org.junit.runner.RunWith;  
22 -import org.thingsboard.server.dao.CustomCassandraCQLUnit;  
23 -  
24 -import java.util.Arrays;  
25 -  
26 -@RunWith(ClasspathSuite.class)  
27 -@ClasspathSuite.ClassnameFilters({  
28 - "org.thingsboard.server.controller.nosql.*Test"})  
29 -public class ControllerNoSqlTestSuite {  
30 -  
31 - @ClassRule  
32 - public static CustomCassandraCQLUnit cassandraUnit =  
33 - new CustomCassandraCQLUnit(  
34 - Arrays.asList(  
35 - new ClassPathCQLDataSet("cassandra/schema-ts.cql", false, false),  
36 - new ClassPathCQLDataSet("cassandra/schema-entities.cql", false, false),  
37 - new ClassPathCQLDataSet("cassandra/system-data.cql", false, false),  
38 - new ClassPathCQLDataSet("cassandra/system-test.cql", false, false)),  
39 - "cassandra-test.yaml", 30000l);  
40 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.nosql;  
17 -  
18 -import org.thingsboard.server.controller.BaseAdminControllerTest;  
19 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
20 -  
21 -/**  
22 - * Created by Valerii Sosliuk on 6/28/2017.  
23 - */  
24 -@DaoNoSqlTest  
25 -public class AdminControllerNoSqlTest extends BaseAdminControllerTest {  
26 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.nosql;  
17 -  
18 -import org.thingsboard.server.controller.BaseAssetControllerTest;  
19 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
20 -import org.thingsboard.server.dao.util.NoSqlDao;  
21 -  
22 -/**  
23 - * Created by Valerii Sosliuk on 6/28/2017.  
24 - */  
25 -@DaoNoSqlTest  
26 -public class AssetControllerNoSqlTest extends BaseAssetControllerTest {  
27 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.nosql;  
17 -  
18 -import org.thingsboard.server.controller.BaseAuditLogControllerTest;  
19 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
20 -  
21 -@DaoNoSqlTest  
22 -public class AuditLogControllerNoSqlTest extends BaseAuditLogControllerTest {  
23 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.nosql;  
17 -  
18 -import org.thingsboard.server.controller.BaseAuthControllerTest;  
19 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
20 -  
21 -/**  
22 - * Created by Valerii Sosliuk on 6/28/2017.  
23 - */  
24 -@DaoNoSqlTest  
25 -public class AuthControllerNoSqlTest extends BaseAuthControllerTest {  
26 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.nosql;  
17 -  
18 -import org.thingsboard.server.controller.BaseComponentDescriptorControllerTest;  
19 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
20 -  
21 -/**  
22 - * Created by Valerii Sosliuk on 6/28/2017.  
23 - */  
24 -@DaoNoSqlTest  
25 -public class ComponentDescriptorControllerNoSqlTest extends BaseComponentDescriptorControllerTest {  
26 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.nosql;  
17 -  
18 -import org.thingsboard.server.controller.BaseCustomerControllerTest;  
19 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
20 -  
21 -/**  
22 - * Created by Valerii Sosliuk on 6/28/2017.  
23 - */  
24 -@DaoNoSqlTest  
25 -public class CustomerControllerNoSqlTest extends BaseCustomerControllerTest {  
26 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.nosql;  
17 -  
18 -import org.thingsboard.server.controller.BaseDashboardControllerTest;  
19 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
20 -  
21 -/**  
22 - * Created by Valerii Sosliuk on 6/28/2017.  
23 - */  
24 -@DaoNoSqlTest  
25 -public class DashboardControllerNoSqlTest extends BaseDashboardControllerTest {  
26 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.nosql;  
17 -  
18 -import org.thingsboard.server.controller.BaseDeviceControllerTest;  
19 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
20 -  
21 -/**  
22 - * Created by Valerii Sosliuk on 6/28/2017.  
23 - */  
24 -@DaoNoSqlTest  
25 -public class DeviceControllerNoSqlTest extends BaseDeviceControllerTest {  
26 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.nosql;  
17 -  
18 -import org.thingsboard.server.controller.BaseEntityViewControllerTest;  
19 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
20 -  
21 -/**  
22 - * Created by Victor Basanets on 8/27/2017.  
23 - */  
24 -@DaoNoSqlTest  
25 -public class EntityViewControllerNoSqlTest extends BaseEntityViewControllerTest {  
26 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.nosql;  
17 -  
18 -import org.thingsboard.server.controller.BaseTenantControllerTest;  
19 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
20 -  
21 -/**  
22 - * Created by Valerii Sosliuk on 6/28/2017.  
23 - */  
24 -@DaoNoSqlTest  
25 -public class TenantControllerNoSqlTest extends BaseTenantControllerTest {  
26 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.nosql;  
17 -  
18 -import org.thingsboard.server.controller.BaseUserControllerTest;  
19 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
20 -  
21 -/**  
22 - * Created by Valerii Sosliuk on 6/28/2017.  
23 - */  
24 -@DaoNoSqlTest  
25 -public class UserControllerNoSqlTest extends BaseUserControllerTest {  
26 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.nosql;  
17 -  
18 -import org.thingsboard.server.controller.BaseWidgetTypeControllerTest;  
19 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
20 -  
21 -/**  
22 - * Created by Valerii Sosliuk on 6/28/2017.  
23 - */  
24 -@DaoNoSqlTest  
25 -public class WidgetTypeControllerNoSqlTest extends BaseWidgetTypeControllerTest {  
26 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.nosql;  
17 -  
18 -import org.thingsboard.server.controller.BaseWidgetsBundleControllerTest;  
19 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
20 -  
21 -/**  
22 - * Created by Valerii Sosliuk on 6/28/2017.  
23 - */  
24 -@DaoNoSqlTest  
25 -public class WidgetsBundleControllerNoSqlTest extends BaseWidgetsBundleControllerTest {  
26 -}  
@@ -20,6 +20,7 @@ import org.junit.ClassRule; @@ -20,6 +20,7 @@ import org.junit.ClassRule;
20 import org.junit.extensions.cpsuite.ClasspathSuite; 20 import org.junit.extensions.cpsuite.ClasspathSuite;
21 import org.junit.runner.RunWith; 21 import org.junit.runner.RunWith;
22 import org.thingsboard.server.dao.CustomCassandraCQLUnit; 22 import org.thingsboard.server.dao.CustomCassandraCQLUnit;
  23 +import org.thingsboard.server.dao.CustomSqlUnit;
23 24
24 import java.util.Arrays; 25 import java.util.Arrays;
25 26
@@ -29,11 +30,15 @@ import java.util.Arrays; @@ -29,11 +30,15 @@ import java.util.Arrays;
29 public class MqttNoSqlTestSuite { 30 public class MqttNoSqlTestSuite {
30 31
31 @ClassRule 32 @ClassRule
  33 + public static CustomSqlUnit sqlUnit = new CustomSqlUnit(
  34 + Arrays.asList("sql/schema-entities.sql", "sql/system-data.sql"),
  35 + "sql/drop-all-tables.sql",
  36 + "nosql-test.properties");
  37 +
  38 + @ClassRule
32 public static CustomCassandraCQLUnit cassandraUnit = 39 public static CustomCassandraCQLUnit cassandraUnit =
33 new CustomCassandraCQLUnit( 40 new CustomCassandraCQLUnit(
34 Arrays.asList( 41 Arrays.asList(
35 - new ClassPathCQLDataSet("cassandra/schema-ts.cql", false, false),  
36 - new ClassPathCQLDataSet("cassandra/schema-entities.cql", false, false),  
37 - new ClassPathCQLDataSet("cassandra/system-data.cql", false, false)), 42 + new ClassPathCQLDataSet("cassandra/schema-ts.cql", false, false)),
38 "cassandra-test.yaml", 30000l); 43 "cassandra-test.yaml", 30000l);
39 } 44 }
1 -/**  
2 - * Copyright © 2016-2019 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.rules;  
17 -  
18 -import org.cassandraunit.dataset.cql.ClassPathCQLDataSet;  
19 -import org.junit.ClassRule;  
20 -import org.junit.extensions.cpsuite.ClasspathSuite;  
21 -import org.junit.runner.RunWith;  
22 -import org.thingsboard.server.dao.CustomCassandraCQLUnit;  
23 -import org.thingsboard.server.dao.CustomSqlUnit;  
24 -  
25 -import java.util.Arrays;  
26 -  
27 -@RunWith(ClasspathSuite.class)  
28 -@ClasspathSuite.ClassnameFilters({  
29 - "org.thingsboard.server.rules.flow.nosql.*Test",  
30 - "org.thingsboard.server.rules.lifecycle.nosql.*Test"  
31 -})  
32 -public class RuleEngineNoSqlTestSuite {  
33 -  
34 - @ClassRule  
35 - public static CustomCassandraCQLUnit cassandraUnit =  
36 - new CustomCassandraCQLUnit(  
37 - Arrays.asList(  
38 - new ClassPathCQLDataSet("cassandra/schema-ts.cql", false, false),  
39 - new ClassPathCQLDataSet("cassandra/schema-entities.cql", false, false),  
40 - new ClassPathCQLDataSet("cassandra/system-data.cql", false, false)),  
41 - "cassandra-test.yaml", 30000l);  
42 -  
43 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.rules.flow.nosql;  
17 -  
18 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
19 -import org.thingsboard.server.rules.flow.AbstractRuleEngineFlowIntegrationTest;  
20 -  
21 -/**  
22 - * Created by Valerii Sosliuk on 8/22/2017.  
23 - */  
24 -@DaoNoSqlTest  
25 -public class RuleEngineFlowNoSqlIntegrationTest extends AbstractRuleEngineFlowIntegrationTest {  
26 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.rules.lifecycle.nosql;  
17 -  
18 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
19 -import org.thingsboard.server.rules.lifecycle.AbstractRuleEngineLifecycleIntegrationTest;  
20 -  
21 -/**  
22 - * Created by Valerii Sosliuk on 8/22/2017.  
23 - */  
24 -@DaoNoSqlTest  
25 -public class RuleEngineLifecycleNoSqlIntegrationTest extends AbstractRuleEngineLifecycleIntegrationTest {  
26 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.system;  
17 -  
18 -import org.cassandraunit.dataset.cql.ClassPathCQLDataSet;  
19 -import org.junit.ClassRule;  
20 -import org.junit.extensions.cpsuite.ClasspathSuite;  
21 -import org.junit.runner.RunWith;  
22 -import org.thingsboard.server.dao.CustomCassandraCQLUnit;  
23 -  
24 -import java.util.Arrays;  
25 -  
26 -/**  
27 - * @author Andrew Shvayka  
28 - */  
29 -@RunWith(ClasspathSuite.class)  
30 -@ClasspathSuite.ClassnameFilters({"org.thingsboard.server.system.*NoSqlTest"})  
31 -public class SystemNoSqlTestSuite {  
32 -  
33 - @ClassRule  
34 - public static CustomCassandraCQLUnit cassandraUnit =  
35 - new CustomCassandraCQLUnit(  
36 - Arrays.asList(  
37 - new ClassPathCQLDataSet("cassandra/schema-ts.cql", false, false),  
38 - new ClassPathCQLDataSet("cassandra/schema-entities.cql", false, false),  
39 - new ClassPathCQLDataSet("cassandra/system-data.cql", false, false)),  
40 - "cassandra-test.yaml", 30000l);  
41 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.system.nosql;  
17 -  
18 -import org.thingsboard.server.dao.service.DaoNoSqlTest;  
19 -import org.thingsboard.server.dao.util.NoSqlDao;  
20 -import org.thingsboard.server.system.BaseHttpDeviceApiTest;  
21 -  
22 -/**  
23 - * Created by Valerii Sosliuk on 6/27/2017.  
24 - */  
25 -@DaoNoSqlTest  
26 -public class DeviceApiNoSqlTest extends BaseHttpDeviceApiTest {  
27 -}  
@@ -17,6 +17,5 @@ package org.thingsboard.server.dao.util; @@ -17,6 +17,5 @@ package org.thingsboard.server.dao.util;
17 17
18 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 18 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
19 19
20 -@ConditionalOnProperty(prefix = "database.entities", value = "type", havingValue = "sql")  
21 public @interface SqlDao { 20 public @interface SqlDao {
22 } 21 }
1 -/**  
2 - * Copyright © 2016-2019 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.alarm;  
17 -  
18 -import com.datastax.driver.core.Statement;  
19 -import com.datastax.driver.core.querybuilder.QueryBuilder;  
20 -import com.datastax.driver.core.querybuilder.Select;  
21 -import com.google.common.util.concurrent.Futures;  
22 -import com.google.common.util.concurrent.ListenableFuture;  
23 -import lombok.extern.slf4j.Slf4j;  
24 -import org.springframework.beans.factory.annotation.Autowired;  
25 -import org.springframework.stereotype.Component;  
26 -import org.thingsboard.server.common.data.EntityType;  
27 -import org.thingsboard.server.common.data.alarm.Alarm;  
28 -import org.thingsboard.server.common.data.alarm.AlarmInfo;  
29 -import org.thingsboard.server.common.data.alarm.AlarmQuery;  
30 -import org.thingsboard.server.common.data.alarm.AlarmSearchStatus;  
31 -import org.thingsboard.server.common.data.id.EntityId;  
32 -import org.thingsboard.server.common.data.id.TenantId;  
33 -import org.thingsboard.server.common.data.relation.EntityRelation;  
34 -import org.thingsboard.server.common.data.relation.RelationTypeGroup;  
35 -import org.thingsboard.server.dao.model.ModelConstants;  
36 -import org.thingsboard.server.dao.model.nosql.AlarmEntity;  
37 -import org.thingsboard.server.dao.nosql.CassandraAbstractModelDao;  
38 -import org.thingsboard.server.dao.relation.RelationDao;  
39 -import org.thingsboard.server.dao.util.NoSqlDao;  
40 -  
41 -import java.util.ArrayList;  
42 -import java.util.List;  
43 -import java.util.UUID;  
44 -  
45 -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;  
46 -import static com.datastax.driver.core.querybuilder.QueryBuilder.select;  
47 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_BY_ID_VIEW_NAME;  
48 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_COLUMN_FAMILY_NAME;  
49 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_ORIGINATOR_ID_PROPERTY;  
50 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_ORIGINATOR_TYPE_PROPERTY;  
51 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_TENANT_ID_PROPERTY;  
52 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_TYPE_PROPERTY;  
53 -  
54 -@Component  
55 -@Slf4j  
56 -@NoSqlDao  
57 -public class CassandraAlarmDao extends CassandraAbstractModelDao<AlarmEntity, Alarm> implements AlarmDao {  
58 -  
59 - @Autowired  
60 - private RelationDao relationDao;  
61 -  
62 - @Override  
63 - protected Class<AlarmEntity> getColumnFamilyClass() {  
64 - return AlarmEntity.class;  
65 - }  
66 -  
67 - @Override  
68 - protected String getColumnFamilyName() {  
69 - return ALARM_COLUMN_FAMILY_NAME;  
70 - }  
71 -  
72 - protected boolean isDeleteOnSave() {  
73 - return false;  
74 - }  
75 -  
76 - @Override  
77 - public Alarm save(TenantId tenantId, Alarm alarm) {  
78 - log.debug("Save asset [{}] ", alarm);  
79 - return super.save(tenantId, alarm);  
80 - }  
81 -  
82 - @Override  
83 - public Boolean deleteAlarm(TenantId tenantId, Alarm alarm) {  
84 - Statement delete = QueryBuilder.delete().all().from(getColumnFamilyName()).where(eq(ModelConstants.ID_PROPERTY, alarm.getId().getId()))  
85 - .and(eq(ALARM_TENANT_ID_PROPERTY, tenantId.getId()))  
86 - .and(eq(ALARM_ORIGINATOR_ID_PROPERTY, alarm.getOriginator().getId()))  
87 - .and(eq(ALARM_ORIGINATOR_TYPE_PROPERTY, alarm.getOriginator().getEntityType()))  
88 - .and(eq(ALARM_TYPE_PROPERTY, alarm.getType()));  
89 - log.debug("Remove request: {}", delete.toString());  
90 - return executeWrite(tenantId, delete).wasApplied();  
91 - }  
92 -  
93 - @Override  
94 - public ListenableFuture<Alarm> findLatestByOriginatorAndType(TenantId tenantId, EntityId originator, String type) {  
95 - Select select = select().from(ALARM_COLUMN_FAMILY_NAME);  
96 - Select.Where query = select.where();  
97 - query.and(eq(ALARM_TENANT_ID_PROPERTY, tenantId.getId()));  
98 - query.and(eq(ALARM_ORIGINATOR_ID_PROPERTY, originator.getId()));  
99 - query.and(eq(ALARM_ORIGINATOR_TYPE_PROPERTY, originator.getEntityType()));  
100 - query.and(eq(ALARM_TYPE_PROPERTY, type));  
101 - query.limit(1);  
102 - query.orderBy(QueryBuilder.asc(ModelConstants.ALARM_TYPE_PROPERTY), QueryBuilder.desc(ModelConstants.ID_PROPERTY));  
103 - return findOneByStatementAsync(tenantId, query);  
104 - }  
105 -  
106 - @Override  
107 - public ListenableFuture<List<AlarmInfo>> findAlarms(TenantId tenantId, AlarmQuery query) {  
108 - log.trace("Try to find alarms by entity [{}], searchStatus [{}], status [{}] and pageLink [{}]", query.getAffectedEntityId(), query.getSearchStatus(), query.getStatus(), query.getPageLink());  
109 - EntityId affectedEntity = query.getAffectedEntityId();  
110 - String searchStatusName;  
111 - if (query.getSearchStatus() == null && query.getStatus() == null) {  
112 - searchStatusName = AlarmSearchStatus.ANY.name();  
113 - } else if (query.getSearchStatus() != null) {  
114 - searchStatusName = query.getSearchStatus().name();  
115 - } else {  
116 - searchStatusName = query.getStatus().name();  
117 - }  
118 - String relationType = BaseAlarmService.ALARM_RELATION_PREFIX + searchStatusName;  
119 - ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(tenantId, affectedEntity, relationType, RelationTypeGroup.ALARM, EntityType.ALARM, query.getPageLink());  
120 - return Futures.transformAsync(relations, input -> {  
121 - List<ListenableFuture<AlarmInfo>> alarmFutures = new ArrayList<>(input.size());  
122 - for (EntityRelation relation : input) {  
123 - alarmFutures.add(Futures.transform(  
124 - findAlarmByIdAsync(tenantId, relation.getTo().getId()),  
125 - AlarmInfo::new));  
126 - }  
127 - return Futures.successfulAsList(alarmFutures);  
128 - });  
129 - }  
130 -  
131 - @Override  
132 - public ListenableFuture<Alarm> findAlarmByIdAsync(TenantId tenantId, UUID key) {  
133 - log.debug("Get alarm by id {}", key);  
134 - Select.Where query = select().from(ALARM_BY_ID_VIEW_NAME).where(eq(ModelConstants.ID_PROPERTY, key));  
135 - query.limit(1);  
136 - log.trace("Execute query {}", query);  
137 - return findOneByStatementAsync(tenantId, query);  
138 - }  
139 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.asset;  
17 -  
18 -import com.datastax.driver.core.ResultSet;  
19 -import com.datastax.driver.core.ResultSetFuture;  
20 -import com.datastax.driver.core.Statement;  
21 -import com.datastax.driver.core.querybuilder.Select;  
22 -import com.datastax.driver.mapping.Result;  
23 -import com.google.common.base.Function;  
24 -import com.google.common.util.concurrent.Futures;  
25 -import com.google.common.util.concurrent.ListenableFuture;  
26 -import lombok.extern.slf4j.Slf4j;  
27 -import org.springframework.stereotype.Component;  
28 -import org.thingsboard.server.common.data.EntitySubtype;  
29 -import org.thingsboard.server.common.data.EntityType;  
30 -import org.thingsboard.server.common.data.asset.Asset;  
31 -import org.thingsboard.server.common.data.id.TenantId;  
32 -import org.thingsboard.server.common.data.page.TextPageLink;  
33 -import org.thingsboard.server.dao.DaoUtil;  
34 -import org.thingsboard.server.dao.model.EntitySubtypeEntity;  
35 -import org.thingsboard.server.dao.model.nosql.AssetEntity;  
36 -import org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao;  
37 -import org.thingsboard.server.dao.util.NoSqlDao;  
38 -  
39 -import javax.annotation.Nullable;  
40 -import java.util.ArrayList;  
41 -import java.util.Arrays;  
42 -import java.util.Collections;  
43 -import java.util.List;  
44 -import java.util.Optional;  
45 -import java.util.UUID;  
46 -  
47 -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;  
48 -import static com.datastax.driver.core.querybuilder.QueryBuilder.in;  
49 -import static com.datastax.driver.core.querybuilder.QueryBuilder.select;  
50 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_BY_CUSTOMER_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME;  
51 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_BY_CUSTOMER_BY_TYPE_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME;  
52 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_BY_TENANT_AND_NAME_VIEW_NAME;  
53 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_BY_TENANT_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME;  
54 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_BY_TENANT_BY_TYPE_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME;  
55 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_COLUMN_FAMILY_NAME;  
56 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_CUSTOMER_ID_PROPERTY;  
57 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_NAME_PROPERTY;  
58 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_TENANT_ID_PROPERTY;  
59 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_TYPE_PROPERTY;  
60 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_SUBTYPE_COLUMN_FAMILY_NAME;  
61 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_SUBTYPE_ENTITY_TYPE_PROPERTY;  
62 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_SUBTYPE_TENANT_ID_PROPERTY;  
63 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
64 -  
65 -@Component  
66 -@Slf4j  
67 -@NoSqlDao  
68 -public class CassandraAssetDao extends CassandraAbstractSearchTextDao<AssetEntity, Asset> implements AssetDao {  
69 -  
70 - @Override  
71 - protected Class<AssetEntity> getColumnFamilyClass() {  
72 - return AssetEntity.class;  
73 - }  
74 -  
75 - @Override  
76 - protected String getColumnFamilyName() {  
77 - return ASSET_COLUMN_FAMILY_NAME;  
78 - }  
79 -  
80 - @Override  
81 - public Asset save(TenantId tenantId, Asset domain) {  
82 - Asset savedAsset = super.save(tenantId, domain);  
83 - EntitySubtype entitySubtype = new EntitySubtype(savedAsset.getTenantId(), EntityType.ASSET, savedAsset.getType());  
84 - EntitySubtypeEntity entitySubtypeEntity = new EntitySubtypeEntity(entitySubtype);  
85 - Statement saveStatement = cluster.getMapper(EntitySubtypeEntity.class).saveQuery(entitySubtypeEntity);  
86 - executeWrite(tenantId, saveStatement);  
87 - return savedAsset;  
88 - }  
89 -  
90 - @Override  
91 - public List<Asset> findAssetsByTenantId(UUID tenantId, TextPageLink pageLink) {  
92 - log.debug("Try to find assets by tenantId [{}] and pageLink [{}]", tenantId, pageLink);  
93 - List<AssetEntity> assetEntities = findPageWithTextSearch(new TenantId(tenantId), ASSET_BY_TENANT_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME,  
94 - Collections.singletonList(eq(ASSET_TENANT_ID_PROPERTY, tenantId)), pageLink);  
95 -  
96 - log.trace("Found assets [{}] by tenantId [{}] and pageLink [{}]", assetEntities, tenantId, pageLink);  
97 - return DaoUtil.convertDataList(assetEntities);  
98 - }  
99 -  
100 - @Override  
101 - public List<Asset> findAssetsByTenantIdAndType(UUID tenantId, String type, TextPageLink pageLink) {  
102 - log.debug("Try to find assets by tenantId [{}], type [{}] and pageLink [{}]", tenantId, type, pageLink);  
103 - List<AssetEntity> assetEntities = findPageWithTextSearch(new TenantId(tenantId), ASSET_BY_TENANT_BY_TYPE_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME,  
104 - Arrays.asList(eq(ASSET_TYPE_PROPERTY, type),  
105 - eq(ASSET_TENANT_ID_PROPERTY, tenantId)), pageLink);  
106 - log.trace("Found assets [{}] by tenantId [{}], type [{}] and pageLink [{}]", assetEntities, tenantId, type, pageLink);  
107 - return DaoUtil.convertDataList(assetEntities);  
108 - }  
109 -  
110 - public ListenableFuture<List<Asset>> findAssetsByTenantIdAndIdsAsync(UUID tenantId, List<UUID> assetIds) {  
111 - log.debug("Try to find assets by tenantId [{}] and asset Ids [{}]", tenantId, assetIds);  
112 - Select select = select().from(getColumnFamilyName());  
113 - Select.Where query = select.where();  
114 - query.and(eq(ASSET_TENANT_ID_PROPERTY, tenantId));  
115 - query.and(in(ID_PROPERTY, assetIds));  
116 - return findListByStatementAsync(new TenantId(tenantId), query);  
117 - }  
118 -  
119 - @Override  
120 - public List<Asset> findAssetsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TextPageLink pageLink) {  
121 - log.debug("Try to find assets by tenantId [{}], customerId[{}] and pageLink [{}]", tenantId, customerId, pageLink);  
122 - List<AssetEntity> assetEntities = findPageWithTextSearch(new TenantId(tenantId), ASSET_BY_CUSTOMER_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME,  
123 - Arrays.asList(eq(ASSET_CUSTOMER_ID_PROPERTY, customerId),  
124 - eq(ASSET_TENANT_ID_PROPERTY, tenantId)),  
125 - pageLink);  
126 -  
127 - log.trace("Found assets [{}] by tenantId [{}], customerId [{}] and pageLink [{}]", assetEntities, tenantId, customerId, pageLink);  
128 - return DaoUtil.convertDataList(assetEntities);  
129 - }  
130 -  
131 - @Override  
132 - public List<Asset> findAssetsByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, TextPageLink pageLink) {  
133 - log.debug("Try to find assets by tenantId [{}], customerId [{}], type [{}] and pageLink [{}]", tenantId, customerId, type, pageLink);  
134 - List<AssetEntity> assetEntities = findPageWithTextSearch(new TenantId(tenantId), ASSET_BY_CUSTOMER_BY_TYPE_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME,  
135 - Arrays.asList(eq(ASSET_TYPE_PROPERTY, type),  
136 - eq(ASSET_CUSTOMER_ID_PROPERTY, customerId),  
137 - eq(ASSET_TENANT_ID_PROPERTY, tenantId)),  
138 - pageLink);  
139 -  
140 - log.trace("Found assets [{}] by tenantId [{}], customerId [{}], type [{}] and pageLink [{}]", assetEntities, tenantId, customerId, type, pageLink);  
141 - return DaoUtil.convertDataList(assetEntities);  
142 - }  
143 -  
144 - @Override  
145 - public ListenableFuture<List<Asset>> findAssetsByTenantIdAndCustomerIdAndIdsAsync(UUID tenantId, UUID customerId, List<UUID> assetIds) {  
146 - log.debug("Try to find assets by tenantId [{}], customerId [{}] and asset Ids [{}]", tenantId, customerId, assetIds);  
147 - Select select = select().from(getColumnFamilyName());  
148 - Select.Where query = select.where();  
149 - query.and(eq(ASSET_TENANT_ID_PROPERTY, tenantId));  
150 - query.and(eq(ASSET_CUSTOMER_ID_PROPERTY, customerId));  
151 - query.and(in(ID_PROPERTY, assetIds));  
152 - return findListByStatementAsync(new TenantId(tenantId), query);  
153 - }  
154 -  
155 - @Override  
156 - public Optional<Asset> findAssetsByTenantIdAndName(UUID tenantId, String assetName) {  
157 - Select select = select().from(ASSET_BY_TENANT_AND_NAME_VIEW_NAME);  
158 - Select.Where query = select.where();  
159 - query.and(eq(ASSET_TENANT_ID_PROPERTY, tenantId));  
160 - query.and(eq(ASSET_NAME_PROPERTY, assetName));  
161 - AssetEntity assetEntity = (AssetEntity) findOneByStatement(new TenantId(tenantId), query);  
162 - return Optional.ofNullable(DaoUtil.getData(assetEntity));  
163 - }  
164 -  
165 - @Override  
166 - public ListenableFuture<List<EntitySubtype>> findTenantAssetTypesAsync(UUID tenantId) {  
167 - Select select = select().from(ENTITY_SUBTYPE_COLUMN_FAMILY_NAME);  
168 - Select.Where query = select.where();  
169 - query.and(eq(ENTITY_SUBTYPE_TENANT_ID_PROPERTY, tenantId));  
170 - query.and(eq(ENTITY_SUBTYPE_ENTITY_TYPE_PROPERTY, EntityType.ASSET));  
171 - query.setConsistencyLevel(cluster.getDefaultReadConsistencyLevel());  
172 - ResultSetFuture resultSetFuture = executeAsyncRead(new TenantId(tenantId), query);  
173 - return Futures.transform(resultSetFuture, new Function<ResultSet, List<EntitySubtype>>() {  
174 - @Nullable  
175 - @Override  
176 - public List<EntitySubtype> apply(@Nullable ResultSet resultSet) {  
177 - Result<EntitySubtypeEntity> result = cluster.getMapper(EntitySubtypeEntity.class).map(resultSet);  
178 - if (result != null) {  
179 - List<EntitySubtype> entitySubtypes = new ArrayList<>();  
180 - result.all().forEach((entitySubtypeEntity) ->  
181 - entitySubtypes.add(entitySubtypeEntity.toEntitySubtype())  
182 - );  
183 - return entitySubtypes;  
184 - } else {  
185 - return Collections.emptyList();  
186 - }  
187 - }  
188 - });  
189 - }  
190 -  
191 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.attributes;  
17 -  
18 -import com.datastax.driver.core.BoundStatement;  
19 -import com.datastax.driver.core.PreparedStatement;  
20 -import com.datastax.driver.core.ResultSet;  
21 -import com.datastax.driver.core.Row;  
22 -import com.datastax.driver.core.Statement;  
23 -import com.datastax.driver.core.querybuilder.QueryBuilder;  
24 -import com.datastax.driver.core.querybuilder.Select;  
25 -import com.google.common.base.Function;  
26 -import com.google.common.util.concurrent.Futures;  
27 -import com.google.common.util.concurrent.ListenableFuture;  
28 -import lombok.extern.slf4j.Slf4j;  
29 -import org.springframework.stereotype.Component;  
30 -import org.thingsboard.server.common.data.id.EntityId;  
31 -import org.thingsboard.server.common.data.id.TenantId;  
32 -import org.thingsboard.server.common.data.kv.AttributeKvEntry;  
33 -import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;  
34 -import org.thingsboard.server.dao.model.ModelConstants;  
35 -import org.thingsboard.server.dao.nosql.CassandraAbstractAsyncDao;  
36 -import org.thingsboard.server.dao.timeseries.CassandraBaseTimeseriesDao;  
37 -import org.thingsboard.server.dao.util.NoSqlDao;  
38 -  
39 -import javax.annotation.PostConstruct;  
40 -import javax.annotation.PreDestroy;  
41 -import java.util.ArrayList;  
42 -import java.util.Collection;  
43 -import java.util.List;  
44 -import java.util.Optional;  
45 -import java.util.stream.Collectors;  
46 -  
47 -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;  
48 -import static com.datastax.driver.core.querybuilder.QueryBuilder.select;  
49 -import static org.thingsboard.server.dao.model.ModelConstants.ATTRIBUTES_KV_CF;  
50 -import static org.thingsboard.server.dao.model.ModelConstants.ATTRIBUTE_KEY_COLUMN;  
51 -import static org.thingsboard.server.dao.model.ModelConstants.ATTRIBUTE_TYPE_COLUMN;  
52 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_ID_COLUMN;  
53 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_TYPE_COLUMN;  
54 -import static org.thingsboard.server.dao.model.ModelConstants.LAST_UPDATE_TS_COLUMN;  
55 -  
56 -/**  
57 - * @author Andrew Shvayka  
58 - */  
59 -@Component  
60 -@Slf4j  
61 -@NoSqlDao  
62 -public class CassandraBaseAttributesDao extends CassandraAbstractAsyncDao implements AttributesDao {  
63 -  
64 - private PreparedStatement saveStmt;  
65 -  
66 - @PostConstruct  
67 - public void init() {  
68 - super.startExecutor();  
69 - }  
70 -  
71 - @PreDestroy  
72 - public void stop() {  
73 - super.stopExecutor();  
74 - }  
75 -  
76 - @Override  
77 - public ListenableFuture<Optional<AttributeKvEntry>> find(TenantId tenantId, EntityId entityId, String attributeType, String attributeKey) {  
78 - Select.Where select = select().from(ATTRIBUTES_KV_CF)  
79 - .where(eq(ENTITY_TYPE_COLUMN, entityId.getEntityType()))  
80 - .and(eq(ENTITY_ID_COLUMN, entityId.getId()))  
81 - .and(eq(ATTRIBUTE_TYPE_COLUMN, attributeType))  
82 - .and(eq(ATTRIBUTE_KEY_COLUMN, attributeKey));  
83 - log.trace("Generated query [{}] for entityId {} and key {}", select, entityId, attributeKey);  
84 - return Futures.transform(executeAsyncRead(tenantId, select), (Function<? super ResultSet, ? extends Optional<AttributeKvEntry>>) input ->  
85 - Optional.ofNullable(convertResultToAttributesKvEntry(attributeKey, input.one()))  
86 - , readResultsProcessingExecutor);  
87 - }  
88 -  
89 - @Override  
90 - public ListenableFuture<List<AttributeKvEntry>> find(TenantId tenantId, EntityId entityId, String attributeType, Collection<String> attributeKeys) {  
91 - List<ListenableFuture<Optional<AttributeKvEntry>>> entries = new ArrayList<>();  
92 - attributeKeys.forEach(attributeKey -> entries.add(find(tenantId, entityId, attributeType, attributeKey)));  
93 - return Futures.transform(Futures.allAsList(entries), (Function<List<Optional<AttributeKvEntry>>, ? extends List<AttributeKvEntry>>) input -> {  
94 - List<AttributeKvEntry> result = new ArrayList<>();  
95 - input.stream().filter(opt -> opt.isPresent()).forEach(opt -> result.add(opt.get()));  
96 - return result;  
97 - }, readResultsProcessingExecutor);  
98 - }  
99 -  
100 -  
101 - @Override  
102 - public ListenableFuture<List<AttributeKvEntry>> findAll(TenantId tenantId, EntityId entityId, String attributeType) {  
103 - Select.Where select = select().from(ATTRIBUTES_KV_CF)  
104 - .where(eq(ENTITY_TYPE_COLUMN, entityId.getEntityType()))  
105 - .and(eq(ENTITY_ID_COLUMN, entityId.getId()))  
106 - .and(eq(ATTRIBUTE_TYPE_COLUMN, attributeType));  
107 - log.trace("Generated query [{}] for entityId {} and attributeType {}", select, entityId, attributeType);  
108 - return Futures.transform(executeAsyncRead(tenantId, select), (Function<? super ResultSet, ? extends List<AttributeKvEntry>>) input ->  
109 - convertResultToAttributesKvEntryList(input)  
110 - , readResultsProcessingExecutor);  
111 - }  
112 -  
113 - @Override  
114 - public ListenableFuture<Void> save(TenantId tenantId, EntityId entityId, String attributeType, AttributeKvEntry attribute) {  
115 - BoundStatement stmt = getSaveStmt().bind();  
116 - stmt.setString(0, entityId.getEntityType().name());  
117 - stmt.setUUID(1, entityId.getId());  
118 - stmt.setString(2, attributeType);  
119 - stmt.setString(3, attribute.getKey());  
120 - stmt.setLong(4, attribute.getLastUpdateTs());  
121 - stmt.setString(5, attribute.getStrValue().orElse(null));  
122 - Optional<Boolean> booleanValue = attribute.getBooleanValue();  
123 - if (booleanValue.isPresent()) {  
124 - stmt.setBool(6, booleanValue.get());  
125 - } else {  
126 - stmt.setToNull(6);  
127 - }  
128 - Optional<Long> longValue = attribute.getLongValue();  
129 - if (longValue.isPresent()) {  
130 - stmt.setLong(7, longValue.get());  
131 - } else {  
132 - stmt.setToNull(7);  
133 - }  
134 - Optional<Double> doubleValue = attribute.getDoubleValue();  
135 - if (doubleValue.isPresent()) {  
136 - stmt.setDouble(8, doubleValue.get());  
137 - } else {  
138 - stmt.setToNull(8);  
139 - }  
140 - log.trace("Generated save stmt [{}] for entityId {} and attributeType {} and attribute", stmt, entityId, attributeType, attribute);  
141 - return getFuture(executeAsyncWrite(tenantId, stmt), rs -> null);  
142 - }  
143 -  
144 - @Override  
145 - public ListenableFuture<List<Void>> removeAll(TenantId tenantId, EntityId entityId, String attributeType, List<String> keys) {  
146 - List<ListenableFuture<Void>> futures = keys  
147 - .stream()  
148 - .map(key -> delete(tenantId, entityId, attributeType, key))  
149 - .collect(Collectors.toList());  
150 - return Futures.allAsList(futures);  
151 - }  
152 -  
153 - private ListenableFuture<Void> delete(TenantId tenantId, EntityId entityId, String attributeType, String key) {  
154 - Statement delete = QueryBuilder.delete().all().from(ModelConstants.ATTRIBUTES_KV_CF)  
155 - .where(eq(ENTITY_TYPE_COLUMN, entityId.getEntityType()))  
156 - .and(eq(ENTITY_ID_COLUMN, entityId.getId()))  
157 - .and(eq(ATTRIBUTE_TYPE_COLUMN, attributeType))  
158 - .and(eq(ATTRIBUTE_KEY_COLUMN, key));  
159 - log.debug("Remove request: {}", delete.toString());  
160 - return getFuture(executeAsyncWrite(tenantId, delete), rs -> null);  
161 - }  
162 -  
163 - private PreparedStatement getSaveStmt() {  
164 - if (saveStmt == null) {  
165 - saveStmt = prepare("INSERT INTO " + ModelConstants.ATTRIBUTES_KV_CF +  
166 - "(" + ENTITY_TYPE_COLUMN +  
167 - "," + ENTITY_ID_COLUMN +  
168 - "," + ATTRIBUTE_TYPE_COLUMN +  
169 - "," + ATTRIBUTE_KEY_COLUMN +  
170 - "," + LAST_UPDATE_TS_COLUMN +  
171 - "," + ModelConstants.STRING_VALUE_COLUMN +  
172 - "," + ModelConstants.BOOLEAN_VALUE_COLUMN +  
173 - "," + ModelConstants.LONG_VALUE_COLUMN +  
174 - "," + ModelConstants.DOUBLE_VALUE_COLUMN +  
175 - ")" +  
176 - " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)");  
177 - }  
178 - return saveStmt;  
179 - }  
180 -  
181 - private AttributeKvEntry convertResultToAttributesKvEntry(String key, Row row) {  
182 - AttributeKvEntry attributeEntry = null;  
183 - if (row != null) {  
184 - long lastUpdateTs = row.get(LAST_UPDATE_TS_COLUMN, Long.class);  
185 - attributeEntry = new BaseAttributeKvEntry(CassandraBaseTimeseriesDao.toKvEntry(row, key), lastUpdateTs);  
186 - }  
187 - return attributeEntry;  
188 - }  
189 -  
190 - private List<AttributeKvEntry> convertResultToAttributesKvEntryList(ResultSet resultSet) {  
191 - List<Row> rows = resultSet.all();  
192 - List<AttributeKvEntry> entries = new ArrayList<>(rows.size());  
193 - if (!rows.isEmpty()) {  
194 - rows.forEach(row -> {  
195 - String key = row.getString(ModelConstants.ATTRIBUTE_KEY_COLUMN);  
196 - AttributeKvEntry kvEntry = convertResultToAttributesKvEntry(key, row);  
197 - if (kvEntry != null) {  
198 - entries.add(kvEntry);  
199 - }  
200 - });  
201 - }  
202 - return entries;  
203 - }  
204 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.audit;  
17 -  
18 -import lombok.Getter;  
19 -import org.thingsboard.server.common.data.page.TimePageLink;  
20 -import org.thingsboard.server.dao.model.nosql.AuditLogEntity;  
21 -  
22 -import java.util.ArrayList;  
23 -import java.util.List;  
24 -import java.util.UUID;  
25 -  
26 -public class AuditLogQueryCursor {  
27 - @Getter  
28 - private final UUID tenantId;  
29 - @Getter  
30 - private final List<AuditLogEntity> data;  
31 - @Getter  
32 - private final TimePageLink pageLink;  
33 -  
34 - private final List<Long> partitions;  
35 -  
36 - private int partitionIndex;  
37 - private int currentLimit;  
38 -  
39 - public AuditLogQueryCursor(UUID tenantId, TimePageLink pageLink, List<Long> partitions) {  
40 - this.tenantId = tenantId;  
41 - this.partitions = partitions;  
42 - this.partitionIndex = partitions.size() - 1;  
43 - this.data = new ArrayList<>();  
44 - this.currentLimit = pageLink.getLimit();  
45 - this.pageLink = pageLink;  
46 - }  
47 -  
48 - public boolean hasNextPartition() {  
49 - return partitionIndex >= 0;  
50 - }  
51 -  
52 - public boolean isFull() {  
53 - return currentLimit <= 0;  
54 - }  
55 -  
56 - public long getNextPartition() {  
57 - long partition = partitions.get(partitionIndex);  
58 - partitionIndex--;  
59 - return partition;  
60 - }  
61 -  
62 - public int getCurrentLimit() {  
63 - return currentLimit;  
64 - }  
65 -  
66 - public void addData(List<AuditLogEntity> newData) {  
67 - currentLimit -= newData.size();  
68 - data.addAll(newData);  
69 - }  
70 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.audit;  
17 -  
18 -import com.datastax.driver.core.BoundStatement;  
19 -import com.datastax.driver.core.PreparedStatement;  
20 -import com.datastax.driver.core.ResultSet;  
21 -import com.datastax.driver.core.ResultSetFuture;  
22 -import com.datastax.driver.core.querybuilder.QueryBuilder;  
23 -import com.datastax.driver.core.querybuilder.Select;  
24 -import com.google.common.base.Function;  
25 -import com.google.common.util.concurrent.Futures;  
26 -import com.google.common.util.concurrent.ListenableFuture;  
27 -import lombok.extern.slf4j.Slf4j;  
28 -import org.springframework.beans.factory.annotation.Autowired;  
29 -import org.springframework.beans.factory.annotation.Value;  
30 -import org.springframework.core.env.Environment;  
31 -import org.springframework.stereotype.Component;  
32 -import org.thingsboard.server.common.data.audit.AuditLog;  
33 -import org.thingsboard.server.common.data.id.CustomerId;  
34 -import org.thingsboard.server.common.data.id.EntityId;  
35 -import org.thingsboard.server.common.data.id.TenantId;  
36 -import org.thingsboard.server.common.data.id.UserId;  
37 -import org.thingsboard.server.common.data.page.TimePageLink;  
38 -import org.thingsboard.server.dao.DaoUtil;  
39 -import org.thingsboard.server.dao.model.ModelConstants;  
40 -import org.thingsboard.server.dao.model.nosql.AuditLogEntity;  
41 -import org.thingsboard.server.dao.nosql.CassandraAbstractSearchTimeDao;  
42 -import org.thingsboard.server.dao.timeseries.TsPartitionDate;  
43 -import org.thingsboard.server.dao.util.NoSqlDao;  
44 -  
45 -import javax.annotation.Nullable;  
46 -import javax.annotation.PostConstruct;  
47 -import javax.annotation.PreDestroy;  
48 -import java.time.Instant;  
49 -import java.time.LocalDate;  
50 -import java.time.LocalDateTime;  
51 -import java.time.ZoneOffset;  
52 -import java.util.ArrayList;  
53 -import java.util.Arrays;  
54 -import java.util.List;  
55 -import java.util.Optional;  
56 -import java.util.StringJoiner;  
57 -import java.util.UUID;  
58 -import java.util.concurrent.ExecutorService;  
59 -import java.util.concurrent.Executors;  
60 -import java.util.stream.Collectors;  
61 -  
62 -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;  
63 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_BY_CUSTOMER_ID_CF;  
64 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_BY_ENTITY_ID_CF;  
65 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_BY_TENANT_ID_CF;  
66 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_BY_USER_ID_CF;  
67 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_COLUMN_FAMILY_NAME;  
68 -  
69 -@Component  
70 -@Slf4j  
71 -@NoSqlDao  
72 -public class CassandraAuditLogDao extends CassandraAbstractSearchTimeDao<AuditLogEntity, AuditLog> implements AuditLogDao {  
73 -  
74 - private static final String INSERT_INTO = "INSERT INTO ";  
75 -  
76 - @Autowired  
77 - private Environment environment;  
78 -  
79 - @Override  
80 - protected Class<AuditLogEntity> getColumnFamilyClass() {  
81 - return AuditLogEntity.class;  
82 - }  
83 -  
84 - @Override  
85 - protected String getColumnFamilyName() {  
86 - return AUDIT_LOG_COLUMN_FAMILY_NAME;  
87 - }  
88 -  
89 - protected ExecutorService readResultsProcessingExecutor;  
90 -  
91 - @Value("${audit-log.by_tenant_partitioning}")  
92 - private String partitioning;  
93 - private TsPartitionDate tsFormat;  
94 -  
95 - @Value("${audit-log.default_query_period}")  
96 - private Integer defaultQueryPeriodInDays;  
97 -  
98 - private PreparedStatement partitionInsertStmt;  
99 - private PreparedStatement saveByTenantStmt;  
100 - private PreparedStatement saveByTenantIdAndUserIdStmt;  
101 - private PreparedStatement saveByTenantIdAndEntityIdStmt;  
102 - private PreparedStatement saveByTenantIdAndCustomerIdStmt;  
103 -  
104 - private boolean isInstall() {  
105 - return environment.acceptsProfiles("install");  
106 - }  
107 -  
108 - @PostConstruct  
109 - public void init() {  
110 - if (!isInstall()) {  
111 - Optional<TsPartitionDate> partition = TsPartitionDate.parse(partitioning);  
112 - if (partition.isPresent()) {  
113 - tsFormat = partition.get();  
114 - } else {  
115 - log.warn("Incorrect configuration of partitioning {}", partitioning);  
116 - throw new RuntimeException("Failed to parse partitioning property: " + partitioning + "!");  
117 - }  
118 - }  
119 - readResultsProcessingExecutor = Executors.newCachedThreadPool();  
120 - }  
121 -  
122 - @PreDestroy  
123 - public void stopExecutor() {  
124 - if (readResultsProcessingExecutor != null) {  
125 - readResultsProcessingExecutor.shutdownNow();  
126 - }  
127 - }  
128 -  
129 - private <T> ListenableFuture<T> getFuture(ResultSetFuture future, java.util.function.Function<ResultSet, T> transformer) {  
130 - return Futures.transform(future, new Function<ResultSet, T>() {  
131 - @Nullable  
132 - @Override  
133 - public T apply(@Nullable ResultSet input) {  
134 - return transformer.apply(input);  
135 - }  
136 - }, readResultsProcessingExecutor);  
137 - }  
138 -  
139 - @Override  
140 - public ListenableFuture<Void> saveByTenantId(AuditLog auditLog) {  
141 - log.debug("Save saveByTenantId [{}] ", auditLog);  
142 -  
143 - long partition = toPartitionTs(LocalDate.now().atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli());  
144 - BoundStatement stmt = getSaveByTenantStmt().bind();  
145 - stmt = setSaveStmtVariables(stmt, auditLog, partition);  
146 - return getFuture(executeAsyncWrite(auditLog.getTenantId(), stmt), rs -> null);  
147 - }  
148 -  
149 - @Override  
150 - public ListenableFuture<Void> saveByTenantIdAndEntityId(AuditLog auditLog) {  
151 - log.debug("Save saveByTenantIdAndEntityId [{}] ", auditLog);  
152 -  
153 - BoundStatement stmt = getSaveByTenantIdAndEntityIdStmt().bind();  
154 - stmt = setSaveStmtVariables(stmt, auditLog, -1);  
155 - return getFuture(executeAsyncWrite(auditLog.getTenantId(), stmt), rs -> null);  
156 - }  
157 -  
158 - @Override  
159 - public ListenableFuture<Void> saveByTenantIdAndCustomerId(AuditLog auditLog) {  
160 - log.debug("Save saveByTenantIdAndCustomerId [{}] ", auditLog);  
161 -  
162 - BoundStatement stmt = getSaveByTenantIdAndCustomerIdStmt().bind();  
163 - stmt = setSaveStmtVariables(stmt, auditLog, -1);  
164 - return getFuture(executeAsyncWrite(auditLog.getTenantId(), stmt), rs -> null);  
165 - }  
166 -  
167 - @Override  
168 - public ListenableFuture<Void> saveByTenantIdAndUserId(AuditLog auditLog) {  
169 - log.debug("Save saveByTenantIdAndUserId [{}] ", auditLog);  
170 -  
171 - BoundStatement stmt = getSaveByTenantIdAndUserIdStmt().bind();  
172 - stmt = setSaveStmtVariables(stmt, auditLog, -1);  
173 - return getFuture(executeAsyncWrite(auditLog.getTenantId(), stmt), rs -> null);  
174 - }  
175 -  
176 - private BoundStatement setSaveStmtVariables(BoundStatement stmt, AuditLog auditLog, long partition) {  
177 - stmt.setUUID(0, auditLog.getId().getId())  
178 - .setUUID(1, auditLog.getTenantId().getId())  
179 - .setUUID(2, auditLog.getCustomerId().getId())  
180 - .setUUID(3, auditLog.getEntityId().getId())  
181 - .setString(4, auditLog.getEntityId().getEntityType().name())  
182 - .setString(5, auditLog.getEntityName())  
183 - .setUUID(6, auditLog.getUserId().getId())  
184 - .setString(7, auditLog.getUserName())  
185 - .setString(8, auditLog.getActionType().name())  
186 - .setString(9, auditLog.getActionData() != null ? auditLog.getActionData().toString() : null)  
187 - .setString(10, auditLog.getActionStatus().name())  
188 - .setString(11, auditLog.getActionFailureDetails());  
189 - if (partition > -1) {  
190 - stmt.setLong(12, partition);  
191 - }  
192 - return stmt;  
193 - }  
194 -  
195 - @Override  
196 - public ListenableFuture<Void> savePartitionsByTenantId(AuditLog auditLog) {  
197 - log.debug("Save savePartitionsByTenantId [{}] ", auditLog);  
198 -  
199 - long partition = toPartitionTs(LocalDate.now().atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli());  
200 -  
201 - BoundStatement stmt = getPartitionInsertStmt().bind();  
202 - stmt = stmt.setUUID(0, auditLog.getTenantId().getId())  
203 - .setLong(1, partition);  
204 - return getFuture(executeAsyncWrite(auditLog.getTenantId(), stmt), rs -> null);  
205 - }  
206 -  
207 - private PreparedStatement getSaveByTenantStmt() {  
208 - if (saveByTenantStmt == null) {  
209 - saveByTenantStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_TENANT_ID_CF, true);  
210 - }  
211 - return saveByTenantStmt;  
212 - }  
213 -  
214 - private PreparedStatement getSaveByTenantIdAndEntityIdStmt() {  
215 - if (saveByTenantIdAndEntityIdStmt == null) {  
216 - saveByTenantIdAndEntityIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_ENTITY_ID_CF, false);  
217 - }  
218 - return saveByTenantIdAndEntityIdStmt;  
219 - }  
220 -  
221 - private PreparedStatement getSaveByTenantIdAndCustomerIdStmt() {  
222 - if (saveByTenantIdAndCustomerIdStmt == null) {  
223 - saveByTenantIdAndCustomerIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_CUSTOMER_ID_CF, false);  
224 - }  
225 - return saveByTenantIdAndCustomerIdStmt;  
226 - }  
227 -  
228 - private PreparedStatement getSaveByTenantIdAndUserIdStmt() {  
229 - if (saveByTenantIdAndUserIdStmt == null) {  
230 - saveByTenantIdAndUserIdStmt = getSaveByTenantIdAndCFName(ModelConstants.AUDIT_LOG_BY_USER_ID_CF, false);  
231 - }  
232 - return saveByTenantIdAndUserIdStmt;  
233 - }  
234 -  
235 - private PreparedStatement getSaveByTenantIdAndCFName(String cfName, boolean hasPartition) {  
236 - List columnsList = new ArrayList();  
237 - columnsList.add(ModelConstants.AUDIT_LOG_ID_PROPERTY);  
238 - columnsList.add(ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY);  
239 - columnsList.add(ModelConstants.AUDIT_LOG_CUSTOMER_ID_PROPERTY);  
240 - columnsList.add(ModelConstants.AUDIT_LOG_ENTITY_ID_PROPERTY);  
241 - columnsList.add(ModelConstants.AUDIT_LOG_ENTITY_TYPE_PROPERTY);  
242 - columnsList.add(ModelConstants.AUDIT_LOG_ENTITY_NAME_PROPERTY);  
243 - columnsList.add(ModelConstants.AUDIT_LOG_USER_ID_PROPERTY);  
244 - columnsList.add(ModelConstants.AUDIT_LOG_USER_NAME_PROPERTY);  
245 - columnsList.add(ModelConstants.AUDIT_LOG_ACTION_TYPE_PROPERTY);  
246 - columnsList.add(ModelConstants.AUDIT_LOG_ACTION_DATA_PROPERTY);  
247 - columnsList.add(ModelConstants.AUDIT_LOG_ACTION_STATUS_PROPERTY);  
248 - columnsList.add(ModelConstants.AUDIT_LOG_ACTION_FAILURE_DETAILS_PROPERTY);  
249 - if (hasPartition) {  
250 - columnsList.add(ModelConstants.AUDIT_LOG_PARTITION_PROPERTY);  
251 - }  
252 - StringJoiner values = new StringJoiner(",");  
253 - for (int i = 0; i < columnsList.size(); i++) {  
254 - values.add("?");  
255 - }  
256 - String statementString = INSERT_INTO + cfName + " (" + String.join(",", columnsList) + ") VALUES (" + values.toString() + ")";  
257 - return prepare(statementString);  
258 - }  
259 -  
260 - private PreparedStatement getPartitionInsertStmt() {  
261 - if (partitionInsertStmt == null) {  
262 - partitionInsertStmt = prepare(INSERT_INTO + ModelConstants.AUDIT_LOG_BY_TENANT_ID_PARTITIONS_CF +  
263 - "(" + ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY +  
264 - "," + ModelConstants.AUDIT_LOG_PARTITION_PROPERTY + ")" +  
265 - " VALUES(?, ?)");  
266 - }  
267 - return partitionInsertStmt;  
268 - }  
269 -  
270 - private long toPartitionTs(long ts) {  
271 - LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneOffset.UTC);  
272 - return tsFormat.truncatedTo(time).toInstant(ZoneOffset.UTC).toEpochMilli();  
273 - }  
274 -  
275 - @Override  
276 - public List<AuditLog> findAuditLogsByTenantIdAndEntityId(UUID tenantId, EntityId entityId, TimePageLink pageLink) {  
277 - log.trace("Try to find audit logs by tenant [{}], entity [{}] and pageLink [{}]", tenantId, entityId, pageLink);  
278 - List<AuditLogEntity> entities = findPageWithTimeSearch(new TenantId(tenantId), AUDIT_LOG_BY_ENTITY_ID_CF,  
279 - Arrays.asList(eq(ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY, tenantId),  
280 - eq(ModelConstants.AUDIT_LOG_ENTITY_TYPE_PROPERTY, entityId.getEntityType()),  
281 - eq(ModelConstants.AUDIT_LOG_ENTITY_ID_PROPERTY, entityId.getId())),  
282 - pageLink);  
283 - log.trace("Found audit logs by tenant [{}], entity [{}] and pageLink [{}]", tenantId, entityId, pageLink);  
284 - return DaoUtil.convertDataList(entities);  
285 - }  
286 -  
287 - @Override  
288 - public List<AuditLog> findAuditLogsByTenantIdAndCustomerId(UUID tenantId, CustomerId customerId, TimePageLink pageLink) {  
289 - log.trace("Try to find audit logs by tenant [{}], customer [{}] and pageLink [{}]", tenantId, customerId, pageLink);  
290 - List<AuditLogEntity> entities = findPageWithTimeSearch(new TenantId(tenantId), AUDIT_LOG_BY_CUSTOMER_ID_CF,  
291 - Arrays.asList(eq(ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY, tenantId),  
292 - eq(ModelConstants.AUDIT_LOG_CUSTOMER_ID_PROPERTY, customerId.getId())),  
293 - pageLink);  
294 - log.trace("Found audit logs by tenant [{}], customer [{}] and pageLink [{}]", tenantId, customerId, pageLink);  
295 - return DaoUtil.convertDataList(entities);  
296 - }  
297 -  
298 - @Override  
299 - public List<AuditLog> findAuditLogsByTenantIdAndUserId(UUID tenantId, UserId userId, TimePageLink pageLink) {  
300 - log.trace("Try to find audit logs by tenant [{}], user [{}] and pageLink [{}]", tenantId, userId, pageLink);  
301 - List<AuditLogEntity> entities = findPageWithTimeSearch(new TenantId(tenantId), AUDIT_LOG_BY_USER_ID_CF,  
302 - Arrays.asList(eq(ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY, tenantId),  
303 - eq(ModelConstants.AUDIT_LOG_USER_ID_PROPERTY, userId.getId())),  
304 - pageLink);  
305 - log.trace("Found audit logs by tenant [{}], user [{}] and pageLink [{}]", tenantId, userId, pageLink);  
306 - return DaoUtil.convertDataList(entities);  
307 - }  
308 -  
309 - @Override  
310 - public List<AuditLog> findAuditLogsByTenantId(UUID tenantId, TimePageLink pageLink) {  
311 - log.trace("Try to find audit logs by tenant [{}] and pageLink [{}]", tenantId, pageLink);  
312 -  
313 - long minPartition;  
314 - if (pageLink.getStartTime() != null && pageLink.getStartTime() != 0) {  
315 - minPartition = toPartitionTs(pageLink.getStartTime());  
316 - } else {  
317 - minPartition = toPartitionTs(LocalDate.now().minusDays(defaultQueryPeriodInDays).atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli());  
318 - }  
319 -  
320 - long maxPartition;  
321 - if (pageLink.getEndTime() != null && pageLink.getEndTime() != 0) {  
322 - maxPartition = toPartitionTs(pageLink.getEndTime());  
323 - } else {  
324 - maxPartition = toPartitionTs(LocalDate.now().atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli());  
325 - }  
326 -  
327 - List<Long> partitions = fetchPartitions(tenantId, minPartition, maxPartition)  
328 - .all()  
329 - .stream()  
330 - .map(row -> row.getLong(ModelConstants.PARTITION_COLUMN))  
331 - .collect(Collectors.toList());  
332 -  
333 - AuditLogQueryCursor cursor = new AuditLogQueryCursor(tenantId, pageLink, partitions);  
334 - List<AuditLogEntity> entities = fetchSequentiallyWithLimit(cursor);  
335 - log.trace("Found audit logs by tenant [{}] and pageLink [{}]", tenantId, pageLink);  
336 - return DaoUtil.convertDataList(entities);  
337 - }  
338 -  
339 - private List<AuditLogEntity> fetchSequentiallyWithLimit(AuditLogQueryCursor cursor) {  
340 - if (cursor.isFull() || !cursor.hasNextPartition()) {  
341 - return cursor.getData();  
342 - } else {  
343 - cursor.addData(findPageWithTimeSearch(new TenantId(cursor.getTenantId()), AUDIT_LOG_BY_TENANT_ID_CF,  
344 - Arrays.asList(eq(ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY, cursor.getTenantId()),  
345 - eq(ModelConstants.AUDIT_LOG_PARTITION_PROPERTY, cursor.getNextPartition())),  
346 - cursor.getPageLink()));  
347 - return fetchSequentiallyWithLimit(cursor);  
348 - }  
349 - }  
350 -  
351 - private ResultSet fetchPartitions(UUID tenantId, long minPartition, long maxPartition) {  
352 - Select.Where select = QueryBuilder.select(ModelConstants.AUDIT_LOG_PARTITION_PROPERTY).from(ModelConstants.AUDIT_LOG_BY_TENANT_ID_PARTITIONS_CF)  
353 - .where(eq(ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY, tenantId));  
354 - select.and(QueryBuilder.gte(ModelConstants.PARTITION_COLUMN, minPartition));  
355 - select.and(QueryBuilder.lte(ModelConstants.PARTITION_COLUMN, maxPartition));  
356 - return executeRead(new TenantId(tenantId), select);  
357 - }  
358 -  
359 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.component;  
17 -  
18 -import com.datastax.driver.core.ResultSet;  
19 -import com.datastax.driver.core.Statement;  
20 -import com.datastax.driver.core.querybuilder.QueryBuilder;  
21 -import com.datastax.driver.core.querybuilder.Select;  
22 -import com.datastax.driver.core.utils.UUIDs;  
23 -import lombok.extern.slf4j.Slf4j;  
24 -import org.springframework.stereotype.Component;  
25 -import org.thingsboard.server.common.data.id.ComponentDescriptorId;  
26 -import org.thingsboard.server.common.data.id.TenantId;  
27 -import org.thingsboard.server.common.data.page.TextPageLink;  
28 -import org.thingsboard.server.common.data.plugin.ComponentDescriptor;  
29 -import org.thingsboard.server.common.data.plugin.ComponentScope;  
30 -import org.thingsboard.server.common.data.plugin.ComponentType;  
31 -import org.thingsboard.server.dao.DaoUtil;  
32 -import org.thingsboard.server.dao.model.ModelConstants;  
33 -import org.thingsboard.server.dao.model.nosql.ComponentDescriptorEntity;  
34 -import org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao;  
35 -import org.thingsboard.server.dao.util.NoSqlDao;  
36 -  
37 -import java.util.Arrays;  
38 -import java.util.List;  
39 -import java.util.Optional;  
40 -import java.util.UUID;  
41 -  
42 -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;  
43 -import static com.datastax.driver.core.querybuilder.QueryBuilder.select;  
44 -  
45 -/**  
46 - * @author Andrew Shvayka  
47 - */  
48 -@Component  
49 -@Slf4j  
50 -@NoSqlDao  
51 -public class CassandraBaseComponentDescriptorDao extends CassandraAbstractSearchTextDao<ComponentDescriptorEntity, ComponentDescriptor> implements ComponentDescriptorDao {  
52 -  
53 - public static final String SEARCH_RESULT = "Search result: [{}]";  
54 -  
55 - @Override  
56 - protected Class<ComponentDescriptorEntity> getColumnFamilyClass() {  
57 - return ComponentDescriptorEntity.class;  
58 - }  
59 -  
60 - @Override  
61 - protected String getColumnFamilyName() {  
62 - return ModelConstants.COMPONENT_DESCRIPTOR_COLUMN_FAMILY_NAME;  
63 - }  
64 -  
65 - @Override  
66 - public Optional<ComponentDescriptor> saveIfNotExist(TenantId tenantId, ComponentDescriptor component) {  
67 - ComponentDescriptorEntity entity = new ComponentDescriptorEntity(component);  
68 - log.debug("Save component entity [{}]", entity);  
69 - Optional<ComponentDescriptor> result = saveIfNotExist(tenantId, entity);  
70 - if (log.isTraceEnabled()) {  
71 - log.trace("Saved result: [{}] for component entity [{}]", result.isPresent(), result.orElse(null));  
72 - } else {  
73 - log.debug("Saved result: [{}]", result.isPresent());  
74 - }  
75 - return result;  
76 - }  
77 -  
78 - @Override  
79 - public ComponentDescriptor findById(TenantId tenantId, ComponentDescriptorId componentId) {  
80 - log.debug("Search component entity by id [{}]", componentId);  
81 - ComponentDescriptor componentDescriptor = super.findById(tenantId, componentId.getId());  
82 - if (log.isTraceEnabled()) {  
83 - log.trace("Search result: [{}] for component entity [{}]", componentDescriptor != null, componentDescriptor);  
84 - } else {  
85 - log.debug(SEARCH_RESULT, componentDescriptor != null);  
86 - }  
87 - return componentDescriptor;  
88 - }  
89 -  
90 - @Override  
91 - public ComponentDescriptor findByClazz(TenantId tenantId, String clazz) {  
92 - log.debug("Search component entity by clazz [{}]", clazz);  
93 - Select.Where query = select().from(getColumnFamilyName()).where(eq(ModelConstants.COMPONENT_DESCRIPTOR_CLASS_PROPERTY, clazz));  
94 - log.trace("Execute query [{}]", query);  
95 - ComponentDescriptorEntity entity = findOneByStatement(tenantId, query);  
96 - if (log.isTraceEnabled()) {  
97 - log.trace("Search result: [{}] for component entity [{}]", entity != null, entity);  
98 - } else {  
99 - log.debug(SEARCH_RESULT, entity != null);  
100 - }  
101 - return DaoUtil.getData(entity);  
102 - }  
103 -  
104 - @Override  
105 - public List<ComponentDescriptor> findByTypeAndPageLink(TenantId tenantId, ComponentType type, TextPageLink pageLink) {  
106 - log.debug("Try to find component by type [{}] and pageLink [{}]", type, pageLink);  
107 - List<ComponentDescriptorEntity> entities = findPageWithTextSearch(tenantId, ModelConstants.COMPONENT_DESCRIPTOR_BY_TYPE_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME,  
108 - Arrays.asList(eq(ModelConstants.COMPONENT_DESCRIPTOR_TYPE_PROPERTY, type)), pageLink);  
109 - if (log.isTraceEnabled()) {  
110 - log.trace(SEARCH_RESULT, Arrays.toString(entities.toArray()));  
111 - } else {  
112 - log.debug(SEARCH_RESULT, entities.size());  
113 - }  
114 - return DaoUtil.convertDataList(entities);  
115 - }  
116 -  
117 - @Override  
118 - public List<ComponentDescriptor> findByScopeAndTypeAndPageLink(TenantId tenantId, ComponentScope scope, ComponentType type, TextPageLink pageLink) {  
119 - log.debug("Try to find component by scope [{}] and type [{}] and pageLink [{}]", scope, type, pageLink);  
120 - List<ComponentDescriptorEntity> entities = findPageWithTextSearch(tenantId, ModelConstants.COMPONENT_DESCRIPTOR_BY_SCOPE_TYPE_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME,  
121 - Arrays.asList(eq(ModelConstants.COMPONENT_DESCRIPTOR_TYPE_PROPERTY, type),  
122 - eq(ModelConstants.COMPONENT_DESCRIPTOR_SCOPE_PROPERTY, scope.name())), pageLink);  
123 - if (log.isTraceEnabled()) {  
124 - log.trace(SEARCH_RESULT, Arrays.toString(entities.toArray()));  
125 - } else {  
126 - log.debug(SEARCH_RESULT, entities.size());  
127 - }  
128 - return DaoUtil.convertDataList(entities);  
129 - }  
130 -  
131 - public boolean removeById(TenantId tenantId, UUID key) {  
132 - Statement delete = QueryBuilder.delete().all().from(ModelConstants.COMPONENT_DESCRIPTOR_BY_ID).where(eq(ModelConstants.ID_PROPERTY, key));  
133 - log.debug("Remove request: {}", delete.toString());  
134 - return executeWrite(tenantId, delete).wasApplied();  
135 - }  
136 -  
137 - @Override  
138 - public void deleteById(TenantId tenantId, ComponentDescriptorId id) {  
139 - log.debug("Delete plugin meta-data entity by id [{}]", id);  
140 - boolean result = removeById(tenantId, id.getId());  
141 - log.debug("Delete result: [{}]", result);  
142 - }  
143 -  
144 - @Override  
145 - public void deleteByClazz(TenantId tenantId, String clazz) {  
146 - log.debug("Delete plugin meta-data entity by id [{}]", clazz);  
147 - Statement delete = QueryBuilder.delete().all().from(getColumnFamilyName()).where(eq(ModelConstants.COMPONENT_DESCRIPTOR_CLASS_PROPERTY, clazz));  
148 - log.debug("Remove request: {}", delete.toString());  
149 - ResultSet resultSet = executeWrite(tenantId, delete);  
150 - log.debug("Delete result: [{}]", resultSet.wasApplied());  
151 - }  
152 -  
153 - private Optional<ComponentDescriptor> saveIfNotExist(TenantId tenantId, ComponentDescriptorEntity entity) {  
154 - if (entity.getId() == null) {  
155 - entity.setId(UUIDs.timeBased());  
156 - }  
157 -  
158 - ResultSet rs = executeRead(tenantId, QueryBuilder.insertInto(getColumnFamilyName())  
159 - .value(ModelConstants.ID_PROPERTY, entity.getId())  
160 - .value(ModelConstants.COMPONENT_DESCRIPTOR_NAME_PROPERTY, entity.getName())  
161 - .value(ModelConstants.COMPONENT_DESCRIPTOR_CLASS_PROPERTY, entity.getClazz())  
162 - .value(ModelConstants.COMPONENT_DESCRIPTOR_TYPE_PROPERTY, entity.getType())  
163 - .value(ModelConstants.COMPONENT_DESCRIPTOR_SCOPE_PROPERTY, entity.getScope())  
164 - .value(ModelConstants.COMPONENT_DESCRIPTOR_CONFIGURATION_DESCRIPTOR_PROPERTY, entity.getConfigurationDescriptor())  
165 - .value(ModelConstants.COMPONENT_DESCRIPTOR_ACTIONS_PROPERTY, entity.getActions())  
166 - .value(ModelConstants.SEARCH_TEXT_PROPERTY, entity.getSearchText())  
167 - .ifNotExists()  
168 - );  
169 - if (rs.wasApplied()) {  
170 - return Optional.of(DaoUtil.getData(entity));  
171 - } else {  
172 - return Optional.empty();  
173 - }  
174 - }  
175 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.customer;  
17 -  
18 -import com.datastax.driver.core.querybuilder.Select;  
19 -import lombok.extern.slf4j.Slf4j;  
20 -import org.springframework.stereotype.Component;  
21 -import org.thingsboard.server.common.data.Customer;  
22 -import org.thingsboard.server.common.data.id.TenantId;  
23 -import org.thingsboard.server.common.data.page.TextPageLink;  
24 -import org.thingsboard.server.dao.DaoUtil;  
25 -import org.thingsboard.server.dao.model.ModelConstants;  
26 -import org.thingsboard.server.dao.model.nosql.CustomerEntity;  
27 -import org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao;  
28 -import org.thingsboard.server.dao.util.NoSqlDao;  
29 -  
30 -import java.util.Arrays;  
31 -import java.util.List;  
32 -import java.util.Optional;  
33 -import java.util.UUID;  
34 -  
35 -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;  
36 -import static com.datastax.driver.core.querybuilder.QueryBuilder.select;  
37 -import static org.thingsboard.server.dao.model.ModelConstants.CUSTOMER_BY_TENANT_AND_TITLE_VIEW_NAME;  
38 -import static org.thingsboard.server.dao.model.ModelConstants.CUSTOMER_TENANT_ID_PROPERTY;  
39 -import static org.thingsboard.server.dao.model.ModelConstants.CUSTOMER_TITLE_PROPERTY;  
40 -  
41 -@Component  
42 -@Slf4j  
43 -@NoSqlDao  
44 -public class CassandraCustomerDao extends CassandraAbstractSearchTextDao<CustomerEntity, Customer> implements CustomerDao {  
45 -  
46 - @Override  
47 - protected Class<CustomerEntity> getColumnFamilyClass() {  
48 - return CustomerEntity.class;  
49 - }  
50 -  
51 - @Override  
52 - protected String getColumnFamilyName() {  
53 - return ModelConstants.CUSTOMER_COLUMN_FAMILY_NAME;  
54 - }  
55 -  
56 - @Override  
57 - public List<Customer> findCustomersByTenantId(UUID tenantId, TextPageLink pageLink) {  
58 - log.debug("Try to find customers by tenantId [{}] and pageLink [{}]", tenantId, pageLink);  
59 - List<CustomerEntity> customerEntities = findPageWithTextSearch(new TenantId(tenantId), ModelConstants.CUSTOMER_BY_TENANT_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME,  
60 - Arrays.asList(eq(ModelConstants.CUSTOMER_TENANT_ID_PROPERTY, tenantId)),  
61 - pageLink);  
62 - log.trace("Found customers [{}] by tenantId [{}] and pageLink [{}]", customerEntities, tenantId, pageLink);  
63 - return DaoUtil.convertDataList(customerEntities);  
64 - }  
65 -  
66 - @Override  
67 - public Optional<Customer> findCustomersByTenantIdAndTitle(UUID tenantId, String title) {  
68 - Select select = select().from(CUSTOMER_BY_TENANT_AND_TITLE_VIEW_NAME);  
69 - Select.Where query = select.where();  
70 - query.and(eq(CUSTOMER_TENANT_ID_PROPERTY, tenantId));  
71 - query.and(eq(CUSTOMER_TITLE_PROPERTY, title));  
72 - CustomerEntity customerEntity = findOneByStatement(new TenantId(tenantId), query);  
73 - Customer customer = DaoUtil.getData(customerEntity);  
74 - return Optional.ofNullable(customer);  
75 - }  
76 -  
77 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.dashboard;  
17 -  
18 -import org.springframework.stereotype.Component;  
19 -import org.thingsboard.server.common.data.Dashboard;  
20 -import org.thingsboard.server.dao.model.nosql.DashboardEntity;  
21 -import org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao;  
22 -import org.thingsboard.server.dao.util.NoSqlDao;  
23 -  
24 -import static org.thingsboard.server.dao.model.ModelConstants.DASHBOARD_COLUMN_FAMILY_NAME;  
25 -  
26 -@Component  
27 -@NoSqlDao  
28 -public class CassandraDashboardDao extends CassandraAbstractSearchTextDao<DashboardEntity, Dashboard> implements DashboardDao {  
29 -  
30 - @Override  
31 - protected Class<DashboardEntity> getColumnFamilyClass() {  
32 - return DashboardEntity.class;  
33 - }  
34 -  
35 - @Override  
36 - protected String getColumnFamilyName() {  
37 - return DASHBOARD_COLUMN_FAMILY_NAME;  
38 - }  
39 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.dashboard;  
17 -  
18 -import com.google.common.util.concurrent.Futures;  
19 -import com.google.common.util.concurrent.ListenableFuture;  
20 -import lombok.extern.slf4j.Slf4j;  
21 -import org.springframework.beans.factory.annotation.Autowired;  
22 -import org.springframework.stereotype.Component;  
23 -import org.thingsboard.server.common.data.DashboardInfo;  
24 -import org.thingsboard.server.common.data.EntityType;  
25 -import org.thingsboard.server.common.data.id.CustomerId;  
26 -import org.thingsboard.server.common.data.id.TenantId;  
27 -import org.thingsboard.server.common.data.page.TextPageLink;  
28 -import org.thingsboard.server.common.data.page.TimePageLink;  
29 -import org.thingsboard.server.common.data.relation.EntityRelation;  
30 -import org.thingsboard.server.common.data.relation.RelationTypeGroup;  
31 -import org.thingsboard.server.dao.DaoUtil;  
32 -import org.thingsboard.server.dao.model.nosql.DashboardInfoEntity;  
33 -import org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao;  
34 -import org.thingsboard.server.dao.relation.RelationDao;  
35 -import org.thingsboard.server.dao.util.NoSqlDao;  
36 -  
37 -import java.util.ArrayList;  
38 -import java.util.Collections;  
39 -import java.util.List;  
40 -import java.util.UUID;  
41 -  
42 -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;  
43 -import static org.thingsboard.server.dao.model.ModelConstants.DASHBOARD_BY_TENANT_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME;  
44 -import static org.thingsboard.server.dao.model.ModelConstants.DASHBOARD_COLUMN_FAMILY_NAME;  
45 -import static org.thingsboard.server.dao.model.ModelConstants.DASHBOARD_TENANT_ID_PROPERTY;  
46 -  
47 -@Component  
48 -@Slf4j  
49 -@NoSqlDao  
50 -public class CassandraDashboardInfoDao extends CassandraAbstractSearchTextDao<DashboardInfoEntity, DashboardInfo> implements DashboardInfoDao {  
51 -  
52 - @Autowired  
53 - private RelationDao relationDao;  
54 -  
55 - @Override  
56 - protected Class<DashboardInfoEntity> getColumnFamilyClass() {  
57 - return DashboardInfoEntity.class;  
58 - }  
59 -  
60 - @Override  
61 - protected String getColumnFamilyName() {  
62 - return DASHBOARD_COLUMN_FAMILY_NAME;  
63 - }  
64 -  
65 - @Override  
66 - public List<DashboardInfo> findDashboardsByTenantId(UUID tenantId, TextPageLink pageLink) {  
67 - log.debug("Try to find dashboards by tenantId [{}] and pageLink [{}]", tenantId, pageLink);  
68 - List<DashboardInfoEntity> dashboardEntities = findPageWithTextSearch(new TenantId(tenantId), DASHBOARD_BY_TENANT_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME,  
69 - Collections.singletonList(eq(DASHBOARD_TENANT_ID_PROPERTY, tenantId)),  
70 - pageLink);  
71 -  
72 - log.trace("Found dashboards [{}] by tenantId [{}] and pageLink [{}]", dashboardEntities, tenantId, pageLink);  
73 - return DaoUtil.convertDataList(dashboardEntities);  
74 - }  
75 -  
76 - @Override  
77 - public ListenableFuture<List<DashboardInfo>> findDashboardsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TimePageLink pageLink) {  
78 - log.debug("Try to find dashboards by tenantId [{}], customerId[{}] and pageLink [{}]", tenantId, customerId, pageLink);  
79 -  
80 - ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(new TenantId(tenantId), new CustomerId(customerId), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.DASHBOARD, EntityType.DASHBOARD, pageLink);  
81 -  
82 - return Futures.transformAsync(relations, input -> {  
83 - List<ListenableFuture<DashboardInfo>> dashboardFutures = new ArrayList<>(input.size());  
84 - for (EntityRelation relation : input) {  
85 - dashboardFutures.add(findByIdAsync(new TenantId(tenantId), relation.getTo().getId()));  
86 - }  
87 - return Futures.successfulAsList(dashboardFutures);  
88 - });  
89 - }  
90 -  
91 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.device;  
17 -  
18 -import com.datastax.driver.core.querybuilder.Select.Where;  
19 -import lombok.extern.slf4j.Slf4j;  
20 -import org.springframework.stereotype.Component;  
21 -import org.thingsboard.server.common.data.id.TenantId;  
22 -import org.thingsboard.server.common.data.security.DeviceCredentials;  
23 -import org.thingsboard.server.dao.DaoUtil;  
24 -import org.thingsboard.server.dao.model.ModelConstants;  
25 -import org.thingsboard.server.dao.model.nosql.DeviceCredentialsEntity;  
26 -import org.thingsboard.server.dao.nosql.CassandraAbstractModelDao;  
27 -import org.thingsboard.server.dao.util.NoSqlDao;  
28 -  
29 -import java.util.UUID;  
30 -  
31 -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;  
32 -import static com.datastax.driver.core.querybuilder.QueryBuilder.select;  
33 -  
34 -@Component  
35 -@Slf4j  
36 -@NoSqlDao  
37 -public class CassandraDeviceCredentialsDao extends CassandraAbstractModelDao<DeviceCredentialsEntity, DeviceCredentials> implements DeviceCredentialsDao {  
38 -  
39 - @Override  
40 - protected Class<DeviceCredentialsEntity> getColumnFamilyClass() {  
41 - return DeviceCredentialsEntity.class;  
42 - }  
43 -  
44 - @Override  
45 - protected String getColumnFamilyName() {  
46 - return ModelConstants.DEVICE_CREDENTIALS_COLUMN_FAMILY_NAME;  
47 - }  
48 -  
49 - @Override  
50 - public DeviceCredentials findByDeviceId(TenantId tenantId, UUID deviceId) {  
51 - log.debug("Try to find device credentials by deviceId [{}] ", deviceId);  
52 - Where query = select().from(ModelConstants.DEVICE_CREDENTIALS_BY_DEVICE_COLUMN_FAMILY_NAME)  
53 - .where(eq(ModelConstants.DEVICE_CREDENTIALS_DEVICE_ID_PROPERTY, deviceId));  
54 - log.trace("Execute query {}", query);  
55 - DeviceCredentialsEntity deviceCredentialsEntity = findOneByStatement(tenantId, query);  
56 - log.trace("Found device credentials [{}] by deviceId [{}]", deviceCredentialsEntity, deviceId);  
57 - return DaoUtil.getData(deviceCredentialsEntity);  
58 - }  
59 -  
60 - @Override  
61 - public DeviceCredentials findByCredentialsId(TenantId tenantId, String credentialsId) {  
62 - log.debug("Try to find device credentials by credentialsId [{}] ", credentialsId);  
63 - Where query = select().from(ModelConstants.DEVICE_CREDENTIALS_BY_CREDENTIALS_ID_COLUMN_FAMILY_NAME)  
64 - .where(eq(ModelConstants.DEVICE_CREDENTIALS_CREDENTIALS_ID_PROPERTY, credentialsId));  
65 - log.trace("Execute query {}", query);  
66 - DeviceCredentialsEntity deviceCredentialsEntity = findOneByStatement(tenantId, query);  
67 - log.trace("Found device credentials [{}] by credentialsId [{}]", deviceCredentialsEntity, credentialsId);  
68 - return DaoUtil.getData(deviceCredentialsEntity);  
69 - }  
70 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.device;  
17 -  
18 -import com.datastax.driver.core.ResultSet;  
19 -import com.datastax.driver.core.ResultSetFuture;  
20 -import com.datastax.driver.core.Statement;  
21 -import com.datastax.driver.core.querybuilder.Select;  
22 -import com.datastax.driver.mapping.Result;  
23 -import com.google.common.base.Function;  
24 -import com.google.common.util.concurrent.Futures;  
25 -import com.google.common.util.concurrent.ListenableFuture;  
26 -import lombok.extern.slf4j.Slf4j;  
27 -import org.springframework.stereotype.Component;  
28 -import org.thingsboard.server.common.data.Device;  
29 -import org.thingsboard.server.common.data.EntitySubtype;  
30 -import org.thingsboard.server.common.data.EntityType;  
31 -import org.thingsboard.server.common.data.id.TenantId;  
32 -import org.thingsboard.server.common.data.page.TextPageLink;  
33 -import org.thingsboard.server.dao.DaoUtil;  
34 -import org.thingsboard.server.dao.model.EntitySubtypeEntity;  
35 -import org.thingsboard.server.dao.model.nosql.DeviceEntity;  
36 -import org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao;  
37 -import org.thingsboard.server.dao.util.NoSqlDao;  
38 -  
39 -import javax.annotation.Nullable;  
40 -import java.util.ArrayList;  
41 -import java.util.Arrays;  
42 -import java.util.Collections;  
43 -import java.util.List;  
44 -import java.util.Optional;  
45 -import java.util.UUID;  
46 -  
47 -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;  
48 -import static com.datastax.driver.core.querybuilder.QueryBuilder.in;  
49 -import static com.datastax.driver.core.querybuilder.QueryBuilder.select;  
50 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_BY_CUSTOMER_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME;  
51 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_BY_CUSTOMER_BY_TYPE_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME;  
52 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_BY_TENANT_AND_NAME_VIEW_NAME;  
53 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_BY_TENANT_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME;  
54 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_BY_TENANT_BY_TYPE_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME;  
55 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_COLUMN_FAMILY_NAME;  
56 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_CUSTOMER_ID_PROPERTY;  
57 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_NAME_PROPERTY;  
58 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_TENANT_ID_PROPERTY;  
59 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_TYPE_PROPERTY;  
60 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_SUBTYPE_COLUMN_FAMILY_NAME;  
61 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_SUBTYPE_ENTITY_TYPE_PROPERTY;  
62 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_SUBTYPE_TENANT_ID_PROPERTY;  
63 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
64 -  
65 -@Component  
66 -@Slf4j  
67 -@NoSqlDao  
68 -public class CassandraDeviceDao extends CassandraAbstractSearchTextDao<DeviceEntity, Device> implements DeviceDao {  
69 -  
70 - @Override  
71 - protected Class<DeviceEntity> getColumnFamilyClass() {  
72 - return DeviceEntity.class;  
73 - }  
74 -  
75 - @Override  
76 - protected String getColumnFamilyName() {  
77 - return DEVICE_COLUMN_FAMILY_NAME;  
78 - }  
79 -  
80 - @Override  
81 - public Device save(TenantId tenantId, Device domain) {  
82 - Device savedDevice = super.save(tenantId, domain);  
83 - EntitySubtype entitySubtype = new EntitySubtype(savedDevice.getTenantId(), EntityType.DEVICE, savedDevice.getType());  
84 - EntitySubtypeEntity entitySubtypeEntity = new EntitySubtypeEntity(entitySubtype);  
85 - Statement saveStatement = cluster.getMapper(EntitySubtypeEntity.class).saveQuery(entitySubtypeEntity);  
86 - executeWrite(tenantId, saveStatement);  
87 - return savedDevice;  
88 - }  
89 -  
90 - @Override  
91 - public List<Device> findDevicesByTenantId(UUID tenantId, TextPageLink pageLink) {  
92 - log.debug("Try to find devices by tenantId [{}] and pageLink [{}]", tenantId, pageLink);  
93 - List<DeviceEntity> deviceEntities = findPageWithTextSearch(new TenantId(tenantId), DEVICE_BY_TENANT_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME,  
94 - Collections.singletonList(eq(DEVICE_TENANT_ID_PROPERTY, tenantId)), pageLink);  
95 -  
96 - log.trace("Found devices [{}] by tenantId [{}] and pageLink [{}]", deviceEntities, tenantId, pageLink);  
97 - return DaoUtil.convertDataList(deviceEntities);  
98 - }  
99 -  
100 - @Override  
101 - public List<Device> findDevicesByTenantIdAndType(UUID tenantId, String type, TextPageLink pageLink) {  
102 - log.debug("Try to find devices by tenantId [{}], type [{}] and pageLink [{}]", tenantId, type, pageLink);  
103 - List<DeviceEntity> deviceEntities = findPageWithTextSearch(new TenantId(tenantId), DEVICE_BY_TENANT_BY_TYPE_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME,  
104 - Arrays.asList(eq(DEVICE_TYPE_PROPERTY, type),  
105 - eq(DEVICE_TENANT_ID_PROPERTY, tenantId)), pageLink);  
106 - log.trace("Found devices [{}] by tenantId [{}], type [{}] and pageLink [{}]", deviceEntities, tenantId, type, pageLink);  
107 - return DaoUtil.convertDataList(deviceEntities);  
108 - }  
109 -  
110 - @Override  
111 - public ListenableFuture<List<Device>> findDevicesByTenantIdAndIdsAsync(UUID tenantId, List<UUID> deviceIds) {  
112 - log.debug("Try to find devices by tenantId [{}] and device Ids [{}]", tenantId, deviceIds);  
113 - Select select = select().from(getColumnFamilyName());  
114 - Select.Where query = select.where();  
115 - query.and(eq(DEVICE_TENANT_ID_PROPERTY, tenantId));  
116 - query.and(in(ID_PROPERTY, deviceIds));  
117 - return findListByStatementAsync(new TenantId(tenantId), query);  
118 - }  
119 -  
120 - @Override  
121 - public List<Device> findDevicesByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TextPageLink pageLink) {  
122 - log.debug("Try to find devices by tenantId [{}], customerId[{}] and pageLink [{}]", tenantId, customerId, pageLink);  
123 - List<DeviceEntity> deviceEntities = findPageWithTextSearch(new TenantId(tenantId), DEVICE_BY_CUSTOMER_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME,  
124 - Arrays.asList(eq(DEVICE_CUSTOMER_ID_PROPERTY, customerId),  
125 - eq(DEVICE_TENANT_ID_PROPERTY, tenantId)),  
126 - pageLink);  
127 -  
128 - log.trace("Found devices [{}] by tenantId [{}], customerId [{}] and pageLink [{}]", deviceEntities, tenantId, customerId, pageLink);  
129 - return DaoUtil.convertDataList(deviceEntities);  
130 - }  
131 -  
132 - @Override  
133 - public List<Device> findDevicesByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, TextPageLink pageLink) {  
134 - log.debug("Try to find devices by tenantId [{}], customerId [{}], type [{}] and pageLink [{}]", tenantId, customerId, type, pageLink);  
135 - List<DeviceEntity> deviceEntities = findPageWithTextSearch(new TenantId(tenantId), DEVICE_BY_CUSTOMER_BY_TYPE_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME,  
136 - Arrays.asList(eq(DEVICE_TYPE_PROPERTY, type),  
137 - eq(DEVICE_CUSTOMER_ID_PROPERTY, customerId),  
138 - eq(DEVICE_TENANT_ID_PROPERTY, tenantId)),  
139 - pageLink);  
140 -  
141 - log.trace("Found devices [{}] by tenantId [{}], customerId [{}], type [{}] and pageLink [{}]", deviceEntities, tenantId, customerId, type, pageLink);  
142 - return DaoUtil.convertDataList(deviceEntities);  
143 - }  
144 -  
145 - @Override  
146 - public ListenableFuture<List<Device>> findDevicesByTenantIdCustomerIdAndIdsAsync(UUID tenantId, UUID customerId, List<UUID> deviceIds) {  
147 - log.debug("Try to find devices by tenantId [{}], customerId [{}] and device Ids [{}]", tenantId, customerId, deviceIds);  
148 - Select select = select().from(getColumnFamilyName());  
149 - Select.Where query = select.where();  
150 - query.and(eq(DEVICE_TENANT_ID_PROPERTY, tenantId));  
151 - query.and(eq(DEVICE_CUSTOMER_ID_PROPERTY, customerId));  
152 - query.and(in(ID_PROPERTY, deviceIds));  
153 - return findListByStatementAsync(new TenantId(tenantId), query);  
154 - }  
155 -  
156 - @Override  
157 - public Optional<Device> findDeviceByTenantIdAndName(UUID tenantId, String deviceName) {  
158 - Select select = select().from(DEVICE_BY_TENANT_AND_NAME_VIEW_NAME);  
159 - Select.Where query = select.where();  
160 - query.and(eq(DEVICE_TENANT_ID_PROPERTY, tenantId));  
161 - query.and(eq(DEVICE_NAME_PROPERTY, deviceName));  
162 - return Optional.ofNullable(DaoUtil.getData(findOneByStatement(new TenantId(tenantId), query)));  
163 - }  
164 -  
165 - @Override  
166 - public ListenableFuture<List<EntitySubtype>> findTenantDeviceTypesAsync(UUID tenantId) {  
167 - Select select = select().from(ENTITY_SUBTYPE_COLUMN_FAMILY_NAME);  
168 - Select.Where query = select.where();  
169 - query.and(eq(ENTITY_SUBTYPE_TENANT_ID_PROPERTY, tenantId));  
170 - query.and(eq(ENTITY_SUBTYPE_ENTITY_TYPE_PROPERTY, EntityType.DEVICE));  
171 - query.setConsistencyLevel(cluster.getDefaultReadConsistencyLevel());  
172 - ResultSetFuture resultSetFuture = executeAsyncRead(new TenantId(tenantId), query);  
173 - return Futures.transform(resultSetFuture, new Function<ResultSet, List<EntitySubtype>>() {  
174 - @Nullable  
175 - @Override  
176 - public List<EntitySubtype> apply(@Nullable ResultSet resultSet) {  
177 - Result<EntitySubtypeEntity> result = cluster.getMapper(EntitySubtypeEntity.class).map(resultSet);  
178 - if (result != null) {  
179 - List<EntitySubtype> entitySubtypes = new ArrayList<>();  
180 - result.all().forEach((entitySubtypeEntity) ->  
181 - entitySubtypes.add(entitySubtypeEntity.toEntitySubtype())  
182 - );  
183 - return entitySubtypes;  
184 - } else {  
185 - return Collections.emptyList();  
186 - }  
187 - }  
188 - });  
189 - }  
190 -  
191 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.entityview;  
17 -  
18 -import com.datastax.driver.core.ResultSet;  
19 -import com.datastax.driver.core.ResultSetFuture;  
20 -import com.datastax.driver.core.Statement;  
21 -import com.datastax.driver.core.querybuilder.Select;  
22 -import com.datastax.driver.mapping.Result;  
23 -import com.google.common.base.Function;  
24 -import com.google.common.util.concurrent.Futures;  
25 -import com.google.common.util.concurrent.ListenableFuture;  
26 -import lombok.extern.slf4j.Slf4j;  
27 -import org.springframework.stereotype.Component;  
28 -import org.thingsboard.server.common.data.EntitySubtype;  
29 -import org.thingsboard.server.common.data.EntityType;  
30 -import org.thingsboard.server.common.data.EntityView;  
31 -import org.thingsboard.server.common.data.id.TenantId;  
32 -import org.thingsboard.server.common.data.page.TextPageLink;  
33 -import org.thingsboard.server.dao.DaoUtil;  
34 -import org.thingsboard.server.dao.model.EntitySubtypeEntity;  
35 -import org.thingsboard.server.dao.model.nosql.EntityViewEntity;  
36 -import org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao;  
37 -import org.thingsboard.server.dao.util.NoSqlDao;  
38 -  
39 -import javax.annotation.Nullable;  
40 -import java.util.ArrayList;  
41 -import java.util.Arrays;  
42 -import java.util.Collections;  
43 -import java.util.List;  
44 -import java.util.Optional;  
45 -import java.util.UUID;  
46 -  
47 -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;  
48 -import static com.datastax.driver.core.querybuilder.QueryBuilder.select;  
49 -import static org.thingsboard.server.dao.model.ModelConstants.CUSTOMER_ID_PROPERTY;  
50 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_TYPE_PROPERTY;  
51 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_ID_COLUMN;  
52 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_SUBTYPE_COLUMN_FAMILY_NAME;  
53 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_SUBTYPE_ENTITY_TYPE_PROPERTY;  
54 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_SUBTYPE_TENANT_ID_PROPERTY;  
55 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_BY_TENANT_AND_CUSTOMER_AND_TYPE_CF;  
56 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_BY_TENANT_AND_CUSTOMER_CF;  
57 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_BY_TENANT_AND_ENTITY_ID_CF;  
58 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_BY_TENANT_AND_NAME;  
59 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_BY_TENANT_AND_SEARCH_TEXT_CF;  
60 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_BY_TENANT_BY_TYPE_AND_SEARCH_TEXT_CF;  
61 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_NAME_PROPERTY;  
62 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_TABLE_FAMILY_NAME;  
63 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_TENANT_ID_PROPERTY;  
64 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_TYPE_PROPERTY;  
65 -import static org.thingsboard.server.dao.model.ModelConstants.TENANT_ID_PROPERTY;  
66 -  
67 -/**  
68 - * Created by Victor Basanets on 9/06/2017.  
69 - */  
70 -@Component  
71 -@Slf4j  
72 -@NoSqlDao  
73 -public class CassandraEntityViewDao extends CassandraAbstractSearchTextDao<EntityViewEntity, EntityView> implements EntityViewDao {  
74 -  
75 - @Override  
76 - protected Class<EntityViewEntity> getColumnFamilyClass() {  
77 - return EntityViewEntity.class;  
78 - }  
79 -  
80 - @Override  
81 - protected String getColumnFamilyName() {  
82 - return ENTITY_VIEW_TABLE_FAMILY_NAME;  
83 - }  
84 -  
85 - @Override  
86 - public EntityView save(TenantId tenantId, EntityView domain) {  
87 - EntityView savedEntityView = super.save(domain.getTenantId(), domain);  
88 - EntitySubtype entitySubtype = new EntitySubtype(savedEntityView.getTenantId(), EntityType.ENTITY_VIEW, savedEntityView.getType());  
89 - EntitySubtypeEntity entitySubtypeEntity = new EntitySubtypeEntity(entitySubtype);  
90 - Statement saveStatement = cluster.getMapper(EntitySubtypeEntity.class).saveQuery(entitySubtypeEntity);  
91 - executeWrite(tenantId, saveStatement);  
92 - return savedEntityView;  
93 - }  
94 -  
95 - @Override  
96 - public List<EntityView> findEntityViewsByTenantId(UUID tenantId, TextPageLink pageLink) {  
97 - log.debug("Try to find entity views by tenantId [{}] and pageLink [{}]", tenantId, pageLink);  
98 - List<EntityViewEntity> entityViewEntities =  
99 - findPageWithTextSearch(new TenantId(tenantId), ENTITY_VIEW_BY_TENANT_AND_SEARCH_TEXT_CF,  
100 - Collections.singletonList(eq(TENANT_ID_PROPERTY, tenantId)), pageLink);  
101 - log.trace("Found entity views [{}] by tenantId [{}] and pageLink [{}]",  
102 - entityViewEntities, tenantId, pageLink);  
103 - return DaoUtil.convertDataList(entityViewEntities);  
104 - }  
105 -  
106 - @Override  
107 - public List<EntityView> findEntityViewsByTenantIdAndType(UUID tenantId, String type, TextPageLink pageLink) {  
108 - log.debug("Try to find entity views by tenantId [{}], type [{}] and pageLink [{}]", tenantId, type, pageLink);  
109 - List<EntityViewEntity> entityViewEntities =  
110 - findPageWithTextSearch(new TenantId(tenantId), ENTITY_VIEW_BY_TENANT_BY_TYPE_AND_SEARCH_TEXT_CF,  
111 - Arrays.asList(eq(ENTITY_VIEW_TYPE_PROPERTY, type),  
112 - eq(TENANT_ID_PROPERTY, tenantId)), pageLink);  
113 - log.trace("Found entity views [{}] by tenantId [{}], type [{}] and pageLink [{}]",  
114 - entityViewEntities, tenantId, type, pageLink);  
115 - return DaoUtil.convertDataList(entityViewEntities);  
116 - }  
117 -  
118 - @Override  
119 - public Optional<EntityView> findEntityViewByTenantIdAndName(UUID tenantId, String name) {  
120 - Select.Where query = select().from(ENTITY_VIEW_BY_TENANT_AND_NAME).where();  
121 - query.and(eq(ENTITY_VIEW_TENANT_ID_PROPERTY, tenantId));  
122 - query.and(eq(ENTITY_VIEW_NAME_PROPERTY, name));  
123 - return Optional.ofNullable(DaoUtil.getData(findOneByStatement(new TenantId(tenantId), query)));  
124 - }  
125 -  
126 - @Override  
127 - public List<EntityView> findEntityViewsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TextPageLink pageLink) {  
128 - log.debug("Try to find entity views by tenantId [{}], customerId[{}] and pageLink [{}]",  
129 - tenantId, customerId, pageLink);  
130 - List<EntityViewEntity> entityViewEntities = findPageWithTextSearch(new TenantId(tenantId),  
131 - ENTITY_VIEW_BY_TENANT_AND_CUSTOMER_CF,  
132 - Arrays.asList(eq(CUSTOMER_ID_PROPERTY, customerId), eq(TENANT_ID_PROPERTY, tenantId)),  
133 - pageLink);  
134 - log.trace("Found find entity views [{}] by tenantId [{}], customerId [{}] and pageLink [{}]",  
135 - entityViewEntities, tenantId, customerId, pageLink);  
136 - return DaoUtil.convertDataList(entityViewEntities);  
137 - }  
138 -  
139 - @Override  
140 - public List<EntityView> findEntityViewsByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, TextPageLink pageLink) {  
141 - log.debug("Try to find entity views by tenantId [{}], customerId[{}], type [{}] and pageLink [{}]",  
142 - tenantId, customerId, type, pageLink);  
143 - List<EntityViewEntity> entityViewEntities = findPageWithTextSearch(new TenantId(tenantId),  
144 - ENTITY_VIEW_BY_TENANT_AND_CUSTOMER_AND_TYPE_CF,  
145 - Arrays.asList(eq(DEVICE_TYPE_PROPERTY, type), eq(CUSTOMER_ID_PROPERTY, customerId), eq(TENANT_ID_PROPERTY, tenantId)),  
146 - pageLink);  
147 - log.trace("Found find entity views [{}] by tenantId [{}], customerId [{}], type [{}] and pageLink [{}]",  
148 - entityViewEntities, tenantId, customerId, type, pageLink);  
149 - return DaoUtil.convertDataList(entityViewEntities);  
150 - }  
151 -  
152 - @Override  
153 - public ListenableFuture<List<EntityView>> findEntityViewsByTenantIdAndEntityIdAsync(UUID tenantId, UUID entityId) {  
154 - log.debug("Try to find entity views by tenantId [{}] and entityId [{}]", tenantId, entityId);  
155 - Select.Where query = select().from(ENTITY_VIEW_BY_TENANT_AND_ENTITY_ID_CF).where();  
156 - query.and(eq(TENANT_ID_PROPERTY, tenantId));  
157 - query.and(eq(ENTITY_ID_COLUMN, entityId));  
158 - return findListByStatementAsync(new TenantId(tenantId), query);  
159 - }  
160 -  
161 - @Override  
162 - public ListenableFuture<List<EntitySubtype>> findTenantEntityViewTypesAsync(UUID tenantId) {  
163 - Select select = select().from(ENTITY_SUBTYPE_COLUMN_FAMILY_NAME);  
164 - Select.Where query = select.where();  
165 - query.and(eq(ENTITY_SUBTYPE_TENANT_ID_PROPERTY, tenantId));  
166 - query.and(eq(ENTITY_SUBTYPE_ENTITY_TYPE_PROPERTY, EntityType.ENTITY_VIEW));  
167 - query.setConsistencyLevel(cluster.getDefaultReadConsistencyLevel());  
168 - ResultSetFuture resultSetFuture = executeAsyncRead(new TenantId(tenantId), query);  
169 - return Futures.transform(resultSetFuture, new Function<ResultSet, List<EntitySubtype>>() {  
170 - @Nullable  
171 - @Override  
172 - public List<EntitySubtype> apply(@Nullable ResultSet resultSet) {  
173 - Result<EntitySubtypeEntity> result = cluster.getMapper(EntitySubtypeEntity.class).map(resultSet);  
174 - if (result != null) {  
175 - List<EntitySubtype> entitySubtypes = new ArrayList<>();  
176 - result.all().forEach((entitySubtypeEntity) ->  
177 - entitySubtypes.add(entitySubtypeEntity.toEntitySubtype())  
178 - );  
179 - return entitySubtypes;  
180 - } else {  
181 - return Collections.emptyList();  
182 - }  
183 - }  
184 - });  
185 - }  
186 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.event;  
17 -  
18 -import com.datastax.driver.core.ResultSetFuture;  
19 -import com.datastax.driver.core.querybuilder.Insert;  
20 -import com.datastax.driver.core.querybuilder.QueryBuilder;  
21 -import com.datastax.driver.core.querybuilder.Select;  
22 -import com.datastax.driver.core.utils.UUIDs;  
23 -import com.google.common.util.concurrent.Futures;  
24 -import com.google.common.util.concurrent.ListenableFuture;  
25 -import lombok.extern.slf4j.Slf4j;  
26 -import org.apache.commons.lang3.StringUtils;  
27 -import org.springframework.beans.factory.annotation.Value;  
28 -import org.springframework.stereotype.Component;  
29 -import org.thingsboard.server.common.data.DataConstants;  
30 -import org.thingsboard.server.common.data.Event;  
31 -import org.thingsboard.server.common.data.id.EntityId;  
32 -import org.thingsboard.server.common.data.id.EventId;  
33 -import org.thingsboard.server.common.data.id.TenantId;  
34 -import org.thingsboard.server.common.data.page.TimePageLink;  
35 -import org.thingsboard.server.dao.DaoUtil;  
36 -import org.thingsboard.server.dao.model.ModelConstants;  
37 -import org.thingsboard.server.dao.model.nosql.EventEntity;  
38 -import org.thingsboard.server.dao.nosql.CassandraAbstractSearchTimeDao;  
39 -import org.thingsboard.server.dao.util.NoSqlDao;  
40 -  
41 -import java.util.Arrays;  
42 -import java.util.List;  
43 -import java.util.Optional;  
44 -import java.util.UUID;  
45 -import java.util.concurrent.ExecutionException;  
46 -  
47 -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;  
48 -import static com.datastax.driver.core.querybuilder.QueryBuilder.in;  
49 -import static com.datastax.driver.core.querybuilder.QueryBuilder.select;  
50 -import static com.datastax.driver.core.querybuilder.QueryBuilder.ttl;  
51 -import static org.thingsboard.server.dao.model.ModelConstants.*;  
52 -  
53 -@Component  
54 -@Slf4j  
55 -@NoSqlDao  
56 -public class CassandraBaseEventDao extends CassandraAbstractSearchTimeDao<EventEntity, Event> implements EventDao {  
57 -  
58 - private final TenantId systemTenantId = new TenantId(NULL_UUID);  
59 -  
60 - @Override  
61 - protected Class<EventEntity> getColumnFamilyClass() {  
62 - return EventEntity.class;  
63 - }  
64 -  
65 - @Override  
66 - protected String getColumnFamilyName() {  
67 - return EVENT_COLUMN_FAMILY_NAME;  
68 - }  
69 -  
70 - @Value("${cassandra.query.events_ttl:0}")  
71 - private int eventsTtl;  
72 -  
73 - @Value("${cassandra.query.debug_events_ttl:0}")  
74 - private int debugEventsTtl;  
75 -  
76 - @Override  
77 - public Event save(TenantId tenantId, Event event) {  
78 - try {  
79 - return saveAsync(event).get();  
80 - } catch (InterruptedException | ExecutionException e) {  
81 - throw new IllegalStateException("Could not save EventEntity", e);  
82 - }  
83 - }  
84 -  
85 - @Override  
86 - public ListenableFuture<Event> saveAsync(Event event) {  
87 - log.debug("Save event [{}] ", event);  
88 - if (event.getTenantId() == null) {  
89 - log.trace("Save system event with predefined id {}", systemTenantId);  
90 - event.setTenantId(systemTenantId);  
91 - }  
92 - if (event.getId() == null) {  
93 - event.setId(new EventId(UUIDs.timeBased()));  
94 - }  
95 - if (StringUtils.isEmpty(event.getUid())) {  
96 - event.setUid(event.getId().toString());  
97 - }  
98 - ListenableFuture<Optional<Event>> optionalSave = saveAsync(event.getTenantId(), new EventEntity(event), false, eventsTtl);  
99 - return Futures.transform(optionalSave, opt -> opt.orElse(null));  
100 - }  
101 -  
102 - @Override  
103 - public Optional<Event> saveIfNotExists(Event event) {  
104 - if (event.getTenantId() == null) {  
105 - log.trace("Save system event with predefined id {}", systemTenantId);  
106 - event.setTenantId(systemTenantId);  
107 - }  
108 - if (event.getId() == null) {  
109 - event.setId(new EventId(UUIDs.timeBased()));  
110 - }  
111 - return save(event.getTenantId(), new EventEntity(event), true, eventsTtl);  
112 - }  
113 -  
114 - @Override  
115 - public Event findEvent(UUID tenantId, EntityId entityId, String eventType, String eventUid) {  
116 - log.debug("Search event entity by [{}][{}][{}][{}]", tenantId, entityId, eventType, eventUid);  
117 - Select.Where query = select().from(getColumnFamilyName()).where(  
118 - eq(ModelConstants.EVENT_TENANT_ID_PROPERTY, tenantId))  
119 - .and(eq(ModelConstants.EVENT_ENTITY_TYPE_PROPERTY, entityId.getEntityType()))  
120 - .and(eq(ModelConstants.EVENT_ENTITY_ID_PROPERTY, entityId.getId()))  
121 - .and(eq(ModelConstants.EVENT_TYPE_PROPERTY, eventType))  
122 - .and(eq(ModelConstants.EVENT_UID_PROPERTY, eventUid));  
123 - log.trace("Execute query [{}]", query);  
124 - EventEntity entity = findOneByStatement(new TenantId(tenantId), query);  
125 - if (log.isTraceEnabled()) {  
126 - log.trace("Search result: [{}] for event entity [{}]", entity != null, entity);  
127 - } else {  
128 - log.debug("Search result: [{}]", entity != null);  
129 - }  
130 - return DaoUtil.getData(entity);  
131 - }  
132 -  
133 - @Override  
134 - public List<Event> findEvents(UUID tenantId, EntityId entityId, TimePageLink pageLink) {  
135 - log.trace("Try to find events by tenant [{}], entity [{}]and pageLink [{}]", tenantId, entityId, pageLink);  
136 - List<EventEntity> entities = findPageWithTimeSearch(new TenantId(tenantId), EVENT_BY_ID_VIEW_NAME,  
137 - Arrays.asList(eq(ModelConstants.EVENT_TENANT_ID_PROPERTY, tenantId),  
138 - eq(ModelConstants.EVENT_ENTITY_TYPE_PROPERTY, entityId.getEntityType()),  
139 - eq(ModelConstants.EVENT_ENTITY_ID_PROPERTY, entityId.getId())),  
140 - pageLink);  
141 - log.trace("Found events by tenant [{}], entity [{}] and pageLink [{}]", tenantId, entityId, pageLink);  
142 - return DaoUtil.convertDataList(entities);  
143 - }  
144 -  
145 - @Override  
146 - public List<Event> findEvents(UUID tenantId, EntityId entityId, String eventType, TimePageLink pageLink) {  
147 - log.trace("Try to find events by tenant [{}], entity [{}], type [{}] and pageLink [{}]", tenantId, entityId, eventType, pageLink);  
148 - List<EventEntity> entities = findPageWithTimeSearch(new TenantId(tenantId), EVENT_BY_TYPE_AND_ID_VIEW_NAME,  
149 - Arrays.asList(eq(ModelConstants.EVENT_TENANT_ID_PROPERTY, tenantId),  
150 - eq(ModelConstants.EVENT_ENTITY_TYPE_PROPERTY, entityId.getEntityType()),  
151 - eq(ModelConstants.EVENT_ENTITY_ID_PROPERTY, entityId.getId()),  
152 - eq(ModelConstants.EVENT_TYPE_PROPERTY, eventType)),  
153 - pageLink.isAscOrder() ? QueryBuilder.asc(ModelConstants.EVENT_TYPE_PROPERTY) :  
154 - QueryBuilder.desc(ModelConstants.EVENT_TYPE_PROPERTY),  
155 - pageLink);  
156 - log.trace("Found events by tenant [{}], entity [{}], type [{}] and pageLink [{}]", tenantId, entityId, eventType, pageLink);  
157 - return DaoUtil.convertDataList(entities);  
158 - }  
159 -  
160 - @Override  
161 - public List<Event> findLatestEvents(UUID tenantId, EntityId entityId, String eventType, int limit) {  
162 - log.trace("Try to find latest events by tenant [{}], entity [{}], type [{}] and limit [{}]", tenantId, entityId, eventType, limit);  
163 - Select select = select().from(EVENT_BY_TYPE_AND_ID_VIEW_NAME);  
164 - Select.Where query = select.where();  
165 - query.and(eq(ModelConstants.EVENT_TENANT_ID_PROPERTY, tenantId));  
166 - query.and(eq(ModelConstants.EVENT_ENTITY_TYPE_PROPERTY, entityId.getEntityType()));  
167 - query.and(eq(ModelConstants.EVENT_ENTITY_ID_PROPERTY, entityId.getId()));  
168 - query.and(eq(ModelConstants.EVENT_TYPE_PROPERTY, eventType));  
169 - query.limit(limit);  
170 - query.orderBy(QueryBuilder.desc(ModelConstants.EVENT_TYPE_PROPERTY), QueryBuilder.desc(ModelConstants.ID_PROPERTY));  
171 - List<EventEntity> entities = findListByStatement(new TenantId(tenantId), query);  
172 - return DaoUtil.convertDataList(entities);  
173 - }  
174 -  
175 - private Optional<Event> save(TenantId tenantId, EventEntity entity, boolean ifNotExists, int ttl) {  
176 - try {  
177 - return saveAsync(tenantId, entity, ifNotExists, ttl).get();  
178 - } catch (InterruptedException | ExecutionException e) {  
179 - throw new IllegalStateException("Could not save EventEntity", e);  
180 - }  
181 - }  
182 -  
183 - private ListenableFuture<Optional<Event>> saveAsync(TenantId tenantId, EventEntity entity, boolean ifNotExists, int ttl) {  
184 - if (entity.getId() == null) {  
185 - entity.setId(UUIDs.timeBased());  
186 - }  
187 - Insert insert = QueryBuilder.insertInto(getColumnFamilyName())  
188 - .value(ModelConstants.ID_PROPERTY, entity.getId())  
189 - .value(ModelConstants.EVENT_TENANT_ID_PROPERTY, entity.getTenantId())  
190 - .value(ModelConstants.EVENT_ENTITY_TYPE_PROPERTY, entity.getEntityType())  
191 - .value(ModelConstants.EVENT_ENTITY_ID_PROPERTY, entity.getEntityId())  
192 - .value(ModelConstants.EVENT_TYPE_PROPERTY, entity.getEventType())  
193 - .value(ModelConstants.EVENT_UID_PROPERTY, entity.getEventUid())  
194 - .value(ModelConstants.EVENT_BODY_PROPERTY, entity.getBody());  
195 -  
196 - if (ifNotExists) {  
197 - insert = insert.ifNotExists();  
198 - }  
199 -  
200 - int selectedTtl = (entity.getEventType().equals(DataConstants.DEBUG_RULE_NODE) ||  
201 - entity.getEventType().equals(DataConstants.DEBUG_RULE_CHAIN)) ? debugEventsTtl : ttl;  
202 -  
203 - if (selectedTtl > 0) {  
204 - insert.using(ttl(selectedTtl));  
205 - }  
206 - ResultSetFuture resultSetFuture = executeAsyncWrite(tenantId, insert);  
207 - return Futures.transform(resultSetFuture, rs -> {  
208 - if (rs.wasApplied()) {  
209 - return Optional.of(DaoUtil.getData(entity));  
210 - } else {  
211 - return Optional.empty();  
212 - }  
213 - });  
214 - }  
215 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.AdminSettings;  
26 -import org.thingsboard.server.common.data.id.AdminSettingsId;  
27 -import org.thingsboard.server.dao.model.BaseEntity;  
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.ADMIN_SETTINGS_COLUMN_FAMILY_NAME;  
33 -import static org.thingsboard.server.dao.model.ModelConstants.ADMIN_SETTINGS_JSON_VALUE_PROPERTY;  
34 -import static org.thingsboard.server.dao.model.ModelConstants.ADMIN_SETTINGS_KEY_PROPERTY;  
35 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
36 -  
37 -@Table(name = ADMIN_SETTINGS_COLUMN_FAMILY_NAME)  
38 -@EqualsAndHashCode  
39 -@ToString  
40 -public final class AdminSettingsEntity implements BaseEntity<AdminSettings> {  
41 -  
42 - @PartitionKey(value = 0)  
43 - @Column(name = ID_PROPERTY)  
44 - private UUID id;  
45 -  
46 - @Column(name = ADMIN_SETTINGS_KEY_PROPERTY)  
47 - private String key;  
48 -  
49 - @Column(name = ADMIN_SETTINGS_JSON_VALUE_PROPERTY, codec = JsonCodec.class)  
50 - private JsonNode jsonValue;  
51 -  
52 - public AdminSettingsEntity() {  
53 - super();  
54 - }  
55 -  
56 - public AdminSettingsEntity(AdminSettings adminSettings) {  
57 - if (adminSettings.getId() != null) {  
58 - this.id = adminSettings.getId().getId();  
59 - }  
60 - this.key = adminSettings.getKey();  
61 - this.jsonValue = adminSettings.getJsonValue();  
62 - }  
63 -  
64 - public UUID getId() {  
65 - return id;  
66 - }  
67 -  
68 - public void setId(UUID id) {  
69 - this.id = id;  
70 - }  
71 -  
72 - public String getKey() {  
73 - return key;  
74 - }  
75 -  
76 - public void setKey(String key) {  
77 - this.key = key;  
78 - }  
79 -  
80 - public JsonNode getJsonValue() {  
81 - return jsonValue;  
82 - }  
83 -  
84 - public void setJsonValue(JsonNode jsonValue) {  
85 - this.jsonValue = jsonValue;  
86 - }  
87 -  
88 - @Override  
89 - public AdminSettings toData() {  
90 - AdminSettings adminSettings = new AdminSettings(new AdminSettingsId(id));  
91 - adminSettings.setCreatedTime(UUIDs.unixTimestamp(id));  
92 - adminSettings.setKey(key);  
93 - adminSettings.setJsonValue(jsonValue);  
94 - return adminSettings;  
95 - }  
96 -  
97 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.EntityType;  
27 -import org.thingsboard.server.common.data.alarm.Alarm;  
28 -import org.thingsboard.server.common.data.alarm.AlarmId;  
29 -import org.thingsboard.server.common.data.alarm.AlarmSeverity;  
30 -import org.thingsboard.server.common.data.alarm.AlarmStatus;  
31 -import org.thingsboard.server.common.data.id.EntityIdFactory;  
32 -import org.thingsboard.server.common.data.id.TenantId;  
33 -import org.thingsboard.server.dao.model.BaseEntity;  
34 -import org.thingsboard.server.dao.model.type.AlarmSeverityCodec;  
35 -import org.thingsboard.server.dao.model.type.AlarmStatusCodec;  
36 -import org.thingsboard.server.dao.model.type.EntityTypeCodec;  
37 -import org.thingsboard.server.dao.model.type.JsonCodec;  
38 -  
39 -import java.util.UUID;  
40 -  
41 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_ACK_TS_PROPERTY;  
42 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_CLEAR_TS_PROPERTY;  
43 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_COLUMN_FAMILY_NAME;  
44 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_DETAILS_PROPERTY;  
45 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_END_TS_PROPERTY;  
46 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_ORIGINATOR_ID_PROPERTY;  
47 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_ORIGINATOR_TYPE_PROPERTY;  
48 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_PROPAGATE_PROPERTY;  
49 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_SEVERITY_PROPERTY;  
50 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_START_TS_PROPERTY;  
51 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_STATUS_PROPERTY;  
52 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_TENANT_ID_PROPERTY;  
53 -import static org.thingsboard.server.dao.model.ModelConstants.ALARM_TYPE_PROPERTY;  
54 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
55 -  
56 -@Table(name = ALARM_COLUMN_FAMILY_NAME)  
57 -@EqualsAndHashCode  
58 -@ToString  
59 -public final class AlarmEntity implements BaseEntity<Alarm> {  
60 -  
61 - @ClusteringColumn(value = 1)  
62 - @Column(name = ID_PROPERTY)  
63 - private UUID id;  
64 -  
65 - @PartitionKey(value = 0)  
66 - @Column(name = ALARM_TENANT_ID_PROPERTY)  
67 - private UUID tenantId;  
68 -  
69 - @PartitionKey(value = 1)  
70 - @Column(name = ALARM_ORIGINATOR_ID_PROPERTY)  
71 - private UUID originatorId;  
72 -  
73 - @PartitionKey(value = 2)  
74 - @Column(name = ALARM_ORIGINATOR_TYPE_PROPERTY, codec = EntityTypeCodec.class)  
75 - private EntityType originatorType;  
76 -  
77 - @ClusteringColumn(value = 0)  
78 - @Column(name = ALARM_TYPE_PROPERTY)  
79 - private String type;  
80 -  
81 - @Column(name = ALARM_SEVERITY_PROPERTY, codec = AlarmSeverityCodec.class)  
82 - private AlarmSeverity severity;  
83 -  
84 - @Column(name = ALARM_STATUS_PROPERTY, codec = AlarmStatusCodec.class)  
85 - private AlarmStatus status;  
86 -  
87 - @Column(name = ALARM_START_TS_PROPERTY)  
88 - private Long startTs;  
89 -  
90 - @Column(name = ALARM_END_TS_PROPERTY)  
91 - private Long endTs;  
92 -  
93 - @Column(name = ALARM_ACK_TS_PROPERTY)  
94 - private Long ackTs;  
95 -  
96 - @Column(name = ALARM_CLEAR_TS_PROPERTY)  
97 - private Long clearTs;  
98 -  
99 - @Column(name = ALARM_DETAILS_PROPERTY, codec = JsonCodec.class)  
100 - private JsonNode details;  
101 -  
102 - @Column(name = ALARM_PROPAGATE_PROPERTY)  
103 - private Boolean propagate;  
104 -  
105 - public AlarmEntity() {  
106 - super();  
107 - }  
108 -  
109 - public AlarmEntity(Alarm alarm) {  
110 - if (alarm.getId() != null) {  
111 - this.id = alarm.getId().getId();  
112 - }  
113 - if (alarm.getTenantId() != null) {  
114 - this.tenantId = alarm.getTenantId().getId();  
115 - }  
116 - this.type = alarm.getType();  
117 - this.originatorId = alarm.getOriginator().getId();  
118 - this.originatorType = alarm.getOriginator().getEntityType();  
119 - this.type = alarm.getType();  
120 - this.severity = alarm.getSeverity();  
121 - this.status = alarm.getStatus();  
122 - this.propagate = alarm.isPropagate();  
123 - this.startTs = alarm.getStartTs();  
124 - this.endTs = alarm.getEndTs();  
125 - this.ackTs = alarm.getAckTs();  
126 - this.clearTs = alarm.getClearTs();  
127 - this.details = alarm.getDetails();  
128 - }  
129 -  
130 - public UUID getId() {  
131 - return id;  
132 - }  
133 -  
134 - public void setId(UUID id) {  
135 - this.id = id;  
136 - }  
137 -  
138 - public UUID getTenantId() {  
139 - return tenantId;  
140 - }  
141 -  
142 - public void setTenantId(UUID tenantId) {  
143 - this.tenantId = tenantId;  
144 - }  
145 -  
146 - public UUID getOriginatorId() {  
147 - return originatorId;  
148 - }  
149 -  
150 - public void setOriginatorId(UUID originatorId) {  
151 - this.originatorId = originatorId;  
152 - }  
153 -  
154 - public EntityType getOriginatorType() {  
155 - return originatorType;  
156 - }  
157 -  
158 - public void setOriginatorType(EntityType originatorType) {  
159 - this.originatorType = originatorType;  
160 - }  
161 -  
162 - public String getType() {  
163 - return type;  
164 - }  
165 -  
166 - public void setType(String type) {  
167 - this.type = type;  
168 - }  
169 -  
170 - public AlarmSeverity getSeverity() {  
171 - return severity;  
172 - }  
173 -  
174 - public void setSeverity(AlarmSeverity severity) {  
175 - this.severity = severity;  
176 - }  
177 -  
178 - public AlarmStatus getStatus() {  
179 - return status;  
180 - }  
181 -  
182 - public void setStatus(AlarmStatus status) {  
183 - this.status = status;  
184 - }  
185 -  
186 - public Long getStartTs() {  
187 - return startTs;  
188 - }  
189 -  
190 - public void setStartTs(Long startTs) {  
191 - this.startTs = startTs;  
192 - }  
193 -  
194 - public Long getEndTs() {  
195 - return endTs;  
196 - }  
197 -  
198 - public void setEndTs(Long endTs) {  
199 - this.endTs = endTs;  
200 - }  
201 -  
202 - public Long getAckTs() {  
203 - return ackTs;  
204 - }  
205 -  
206 - public void setAckTs(Long ackTs) {  
207 - this.ackTs = ackTs;  
208 - }  
209 -  
210 - public Long getClearTs() {  
211 - return clearTs;  
212 - }  
213 -  
214 - public void setClearTs(Long clearTs) {  
215 - this.clearTs = clearTs;  
216 - }  
217 -  
218 - public JsonNode getDetails() {  
219 - return details;  
220 - }  
221 -  
222 - public void setDetails(JsonNode details) {  
223 - this.details = details;  
224 - }  
225 -  
226 - public Boolean getPropagate() {  
227 - return propagate;  
228 - }  
229 -  
230 - public void setPropagate(Boolean propagate) {  
231 - this.propagate = propagate;  
232 - }  
233 -  
234 - @Override  
235 - public Alarm toData() {  
236 - Alarm alarm = new Alarm(new AlarmId(id));  
237 - alarm.setCreatedTime(UUIDs.unixTimestamp(id));  
238 - if (tenantId != null) {  
239 - alarm.setTenantId(new TenantId(tenantId));  
240 - }  
241 - alarm.setOriginator(EntityIdFactory.getByTypeAndUuid(originatorType, originatorId));  
242 - alarm.setType(type);  
243 - alarm.setSeverity(severity);  
244 - alarm.setStatus(status);  
245 - alarm.setPropagate(propagate);  
246 - alarm.setStartTs(startTs);  
247 - alarm.setEndTs(endTs);  
248 - alarm.setAckTs(ackTs);  
249 - alarm.setClearTs(clearTs);  
250 - alarm.setDetails(details);  
251 - return alarm;  
252 - }  
253 -  
254 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.asset.Asset;  
26 -import org.thingsboard.server.common.data.id.AssetId;  
27 -import org.thingsboard.server.common.data.id.CustomerId;  
28 -import org.thingsboard.server.common.data.id.TenantId;  
29 -import org.thingsboard.server.dao.model.SearchTextEntity;  
30 -import org.thingsboard.server.dao.model.type.JsonCodec;  
31 -  
32 -import java.util.UUID;  
33 -  
34 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_ADDITIONAL_INFO_PROPERTY;  
35 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_COLUMN_FAMILY_NAME;  
36 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_CUSTOMER_ID_PROPERTY;  
37 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_NAME_PROPERTY;  
38 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_TENANT_ID_PROPERTY;  
39 -import static org.thingsboard.server.dao.model.ModelConstants.ASSET_TYPE_PROPERTY;  
40 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
41 -import static org.thingsboard.server.dao.model.ModelConstants.SEARCH_TEXT_PROPERTY;  
42 -  
43 -@Table(name = ASSET_COLUMN_FAMILY_NAME)  
44 -@EqualsAndHashCode  
45 -@ToString  
46 -public final class AssetEntity implements SearchTextEntity<Asset> {  
47 -  
48 - @PartitionKey(value = 0)  
49 - @Column(name = ID_PROPERTY)  
50 - private UUID id;  
51 -  
52 - @PartitionKey(value = 1)  
53 - @Column(name = ASSET_TENANT_ID_PROPERTY)  
54 - private UUID tenantId;  
55 -  
56 - @PartitionKey(value = 2)  
57 - @Column(name = ASSET_CUSTOMER_ID_PROPERTY)  
58 - private UUID customerId;  
59 -  
60 - @PartitionKey(value = 3)  
61 - @Column(name = ASSET_TYPE_PROPERTY)  
62 - private String type;  
63 -  
64 - @Column(name = ASSET_NAME_PROPERTY)  
65 - private String name;  
66 -  
67 - @Column(name = SEARCH_TEXT_PROPERTY)  
68 - private String searchText;  
69 -  
70 - @Column(name = ASSET_ADDITIONAL_INFO_PROPERTY, codec = JsonCodec.class)  
71 - private JsonNode additionalInfo;  
72 -  
73 - public AssetEntity() {  
74 - super();  
75 - }  
76 -  
77 - public AssetEntity(Asset asset) {  
78 - if (asset.getId() != null) {  
79 - this.id = asset.getId().getId();  
80 - }  
81 - if (asset.getTenantId() != null) {  
82 - this.tenantId = asset.getTenantId().getId();  
83 - }  
84 - if (asset.getCustomerId() != null) {  
85 - this.customerId = asset.getCustomerId().getId();  
86 - }  
87 - this.name = asset.getName();  
88 - this.type = asset.getType();  
89 - this.additionalInfo = asset.getAdditionalInfo();  
90 - }  
91 -  
92 - public UUID getId() {  
93 - return id;  
94 - }  
95 -  
96 - public void setId(UUID id) {  
97 - this.id = id;  
98 - }  
99 -  
100 - public UUID getTenantId() {  
101 - return tenantId;  
102 - }  
103 -  
104 - public void setTenantId(UUID tenantId) {  
105 - this.tenantId = tenantId;  
106 - }  
107 -  
108 - public UUID getCustomerId() {  
109 - return customerId;  
110 - }  
111 -  
112 - public void setCustomerId(UUID customerId) {  
113 - this.customerId = customerId;  
114 - }  
115 -  
116 - public String getName() {  
117 - return name;  
118 - }  
119 -  
120 - public void setName(String name) {  
121 - this.name = name;  
122 - }  
123 -  
124 - public String getType() {  
125 - return type;  
126 - }  
127 -  
128 - public void setType(String type) {  
129 - this.type = type;  
130 - }  
131 -  
132 - public JsonNode getAdditionalInfo() {  
133 - return additionalInfo;  
134 - }  
135 -  
136 - public void setAdditionalInfo(JsonNode additionalInfo) {  
137 - this.additionalInfo = additionalInfo;  
138 - }  
139 -  
140 - @Override  
141 - public String getSearchTextSource() {  
142 - return getName();  
143 - }  
144 -  
145 - @Override  
146 - public void setSearchText(String searchText) {  
147 - this.searchText = searchText;  
148 - }  
149 -  
150 - public String getSearchText() {  
151 - return searchText;  
152 - }  
153 -  
154 - @Override  
155 - public Asset toData() {  
156 - Asset asset = new Asset(new AssetId(id));  
157 - asset.setCreatedTime(UUIDs.unixTimestamp(id));  
158 - if (tenantId != null) {  
159 - asset.setTenantId(new TenantId(tenantId));  
160 - }  
161 - if (customerId != null) {  
162 - asset.setCustomerId(new CustomerId(customerId));  
163 - }  
164 - asset.setName(name);  
165 - asset.setType(type);  
166 - asset.setAdditionalInfo(additionalInfo);  
167 - return asset;  
168 - }  
169 -  
170 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.Table;  
21 -import com.fasterxml.jackson.databind.JsonNode;  
22 -import lombok.Data;  
23 -import lombok.NoArgsConstructor;  
24 -import org.thingsboard.server.common.data.EntityType;  
25 -import org.thingsboard.server.common.data.audit.ActionStatus;  
26 -import org.thingsboard.server.common.data.audit.ActionType;  
27 -import org.thingsboard.server.common.data.audit.AuditLog;  
28 -import org.thingsboard.server.common.data.id.AuditLogId;  
29 -import org.thingsboard.server.common.data.id.CustomerId;  
30 -import org.thingsboard.server.common.data.id.EntityIdFactory;  
31 -import org.thingsboard.server.common.data.id.TenantId;  
32 -import org.thingsboard.server.common.data.id.UserId;  
33 -import org.thingsboard.server.dao.model.BaseEntity;  
34 -import org.thingsboard.server.dao.model.type.ActionStatusCodec;  
35 -import org.thingsboard.server.dao.model.type.ActionTypeCodec;  
36 -import org.thingsboard.server.dao.model.type.EntityTypeCodec;  
37 -import org.thingsboard.server.dao.model.type.JsonCodec;  
38 -  
39 -import java.util.UUID;  
40 -  
41 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_ACTION_DATA_PROPERTY;  
42 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_ACTION_FAILURE_DETAILS_PROPERTY;  
43 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_ACTION_STATUS_PROPERTY;  
44 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_ACTION_TYPE_PROPERTY;  
45 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_BY_ENTITY_ID_CF;  
46 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_CUSTOMER_ID_PROPERTY;  
47 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_ENTITY_ID_PROPERTY;  
48 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_ENTITY_NAME_PROPERTY;  
49 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_ENTITY_TYPE_PROPERTY;  
50 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_TENANT_ID_PROPERTY;  
51 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_USER_ID_PROPERTY;  
52 -import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_USER_NAME_PROPERTY;  
53 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
54 -  
55 -@Table(name = AUDIT_LOG_BY_ENTITY_ID_CF)  
56 -@Data  
57 -@NoArgsConstructor  
58 -public class AuditLogEntity implements BaseEntity<AuditLog> {  
59 -  
60 - @Column(name = ID_PROPERTY)  
61 - private UUID id;  
62 -  
63 - @Column(name = AUDIT_LOG_TENANT_ID_PROPERTY)  
64 - private UUID tenantId;  
65 -  
66 - @Column(name = AUDIT_LOG_CUSTOMER_ID_PROPERTY)  
67 - private UUID customerId;  
68 -  
69 - @Column(name = AUDIT_LOG_ENTITY_TYPE_PROPERTY, codec = EntityTypeCodec.class)  
70 - private EntityType entityType;  
71 -  
72 - @Column(name = AUDIT_LOG_ENTITY_ID_PROPERTY)  
73 - private UUID entityId;  
74 -  
75 - @Column(name = AUDIT_LOG_ENTITY_NAME_PROPERTY)  
76 - private String entityName;  
77 -  
78 - @Column(name = AUDIT_LOG_USER_ID_PROPERTY)  
79 - private UUID userId;  
80 -  
81 - @Column(name = AUDIT_LOG_USER_NAME_PROPERTY)  
82 - private String userName;  
83 -  
84 - @Column(name = AUDIT_LOG_ACTION_TYPE_PROPERTY, codec = ActionTypeCodec.class)  
85 - private ActionType actionType;  
86 -  
87 - @Column(name = AUDIT_LOG_ACTION_DATA_PROPERTY, codec = JsonCodec.class)  
88 - private JsonNode actionData;  
89 -  
90 - @Column(name = AUDIT_LOG_ACTION_STATUS_PROPERTY, codec = ActionStatusCodec.class)  
91 - private ActionStatus actionStatus;  
92 -  
93 - @Column(name = AUDIT_LOG_ACTION_FAILURE_DETAILS_PROPERTY)  
94 - private String actionFailureDetails;  
95 -  
96 - @Override  
97 - public UUID getId() {  
98 - return id;  
99 - }  
100 -  
101 - @Override  
102 - public void setId(UUID id) {  
103 - this.id = id;  
104 - }  
105 -  
106 - public AuditLogEntity(AuditLog auditLog) {  
107 - if (auditLog.getId() != null) {  
108 - this.id = auditLog.getId().getId();  
109 - }  
110 - if (auditLog.getTenantId() != null) {  
111 - this.tenantId = auditLog.getTenantId().getId();  
112 - }  
113 - if (auditLog.getEntityId() != null) {  
114 - this.entityType = auditLog.getEntityId().getEntityType();  
115 - this.entityId = auditLog.getEntityId().getId();  
116 - }  
117 - if (auditLog.getCustomerId() != null) {  
118 - this.customerId = auditLog.getCustomerId().getId();  
119 - }  
120 - if (auditLog.getUserId() != null) {  
121 - this.userId = auditLog.getUserId().getId();  
122 - }  
123 - this.entityName = auditLog.getEntityName();  
124 - this.userName = auditLog.getUserName();  
125 - this.actionType = auditLog.getActionType();  
126 - this.actionData = auditLog.getActionData();  
127 - this.actionStatus = auditLog.getActionStatus();  
128 - this.actionFailureDetails = auditLog.getActionFailureDetails();  
129 - }  
130 -  
131 - @Override  
132 - public AuditLog toData() {  
133 - AuditLog auditLog = new AuditLog(new AuditLogId(id));  
134 - auditLog.setCreatedTime(UUIDs.unixTimestamp(id));  
135 - if (tenantId != null) {  
136 - auditLog.setTenantId(new TenantId(tenantId));  
137 - }  
138 - if (entityId != null && entityType != null) {  
139 - auditLog.setEntityId(EntityIdFactory.getByTypeAndUuid(entityType, entityId));  
140 - }  
141 - if (customerId != null) {  
142 - auditLog.setCustomerId(new CustomerId(customerId));  
143 - }  
144 - if (userId != null) {  
145 - auditLog.setUserId(new UserId(userId));  
146 - }  
147 - auditLog.setEntityName(this.entityName);  
148 - auditLog.setUserName(this.userName);  
149 - auditLog.setActionType(this.actionType);  
150 - auditLog.setActionData(this.actionData);  
151 - auditLog.setActionStatus(this.actionStatus);  
152 - auditLog.setActionFailureDetails(this.actionFailureDetails);  
153 - return auditLog;  
154 - }  
155 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.mapping.annotations.Column;  
19 -import com.datastax.driver.mapping.annotations.PartitionKey;  
20 -import com.datastax.driver.mapping.annotations.Table;  
21 -import com.fasterxml.jackson.databind.JsonNode;  
22 -import org.thingsboard.server.common.data.id.ComponentDescriptorId;  
23 -import org.thingsboard.server.common.data.plugin.ComponentDescriptor;  
24 -import org.thingsboard.server.common.data.plugin.ComponentScope;  
25 -import org.thingsboard.server.common.data.plugin.ComponentType;  
26 -import org.thingsboard.server.dao.model.SearchTextEntity;  
27 -import org.thingsboard.server.dao.model.type.JsonCodec;  
28 -  
29 -import java.util.UUID;  
30 -  
31 -import static org.thingsboard.server.dao.model.ModelConstants.COMPONENT_DESCRIPTOR_ACTIONS_PROPERTY;  
32 -import static org.thingsboard.server.dao.model.ModelConstants.COMPONENT_DESCRIPTOR_CLASS_PROPERTY;  
33 -import static org.thingsboard.server.dao.model.ModelConstants.COMPONENT_DESCRIPTOR_COLUMN_FAMILY_NAME;  
34 -import static org.thingsboard.server.dao.model.ModelConstants.COMPONENT_DESCRIPTOR_CONFIGURATION_DESCRIPTOR_PROPERTY;  
35 -import static org.thingsboard.server.dao.model.ModelConstants.COMPONENT_DESCRIPTOR_NAME_PROPERTY;  
36 -import static org.thingsboard.server.dao.model.ModelConstants.COMPONENT_DESCRIPTOR_SCOPE_PROPERTY;  
37 -import static org.thingsboard.server.dao.model.ModelConstants.COMPONENT_DESCRIPTOR_TYPE_PROPERTY;  
38 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
39 -import static org.thingsboard.server.dao.model.ModelConstants.SEARCH_TEXT_PROPERTY;  
40 -  
41 -/**  
42 - * @author Andrew Shvayka  
43 - */  
44 -@Table(name = COMPONENT_DESCRIPTOR_COLUMN_FAMILY_NAME)  
45 -public class ComponentDescriptorEntity implements SearchTextEntity<ComponentDescriptor> {  
46 -  
47 - @PartitionKey  
48 - @Column(name = ID_PROPERTY)  
49 - private UUID id;  
50 -  
51 - @Column(name = COMPONENT_DESCRIPTOR_TYPE_PROPERTY)  
52 - private ComponentType type;  
53 -  
54 - @Column(name = COMPONENT_DESCRIPTOR_SCOPE_PROPERTY)  
55 - private ComponentScope scope;  
56 -  
57 - @Column(name = COMPONENT_DESCRIPTOR_NAME_PROPERTY)  
58 - private String name;  
59 -  
60 - @Column(name = COMPONENT_DESCRIPTOR_CLASS_PROPERTY)  
61 - private String clazz;  
62 -  
63 - @Column(name = COMPONENT_DESCRIPTOR_CONFIGURATION_DESCRIPTOR_PROPERTY, codec = JsonCodec.class)  
64 - private JsonNode configurationDescriptor;  
65 -  
66 - @Column(name = COMPONENT_DESCRIPTOR_ACTIONS_PROPERTY)  
67 - private String actions;  
68 -  
69 - @Column(name = SEARCH_TEXT_PROPERTY)  
70 - private String searchText;  
71 -  
72 - public ComponentDescriptorEntity() {  
73 - }  
74 -  
75 - public ComponentDescriptorEntity(ComponentDescriptor component) {  
76 - if (component.getId() != null) {  
77 - this.id = component.getId().getId();  
78 - }  
79 - this.actions = component.getActions();  
80 - this.type = component.getType();  
81 - this.scope = component.getScope();  
82 - this.name = component.getName();  
83 - this.clazz = component.getClazz();  
84 - this.configurationDescriptor = component.getConfigurationDescriptor();  
85 - this.searchText = component.getName();  
86 - }  
87 -  
88 - @Override  
89 - public ComponentDescriptor toData() {  
90 - ComponentDescriptor data = new ComponentDescriptor(new ComponentDescriptorId(id));  
91 - data.setType(type);  
92 - data.setScope(scope);  
93 - data.setName(this.getName());  
94 - data.setClazz(this.getClazz());  
95 - data.setActions(this.getActions());  
96 - data.setConfigurationDescriptor(this.getConfigurationDescriptor());  
97 - return data;  
98 - }  
99 -  
100 - @Override  
101 - public UUID getId() {  
102 - return id;  
103 - }  
104 -  
105 - @Override  
106 - public void setId(UUID id) {  
107 - this.id = id;  
108 - }  
109 -  
110 - public String getActions() {  
111 - return actions;  
112 - }  
113 -  
114 - public void setActions(String actions) {  
115 - this.actions = actions;  
116 - }  
117 -  
118 - public ComponentType getType() {  
119 - return type;  
120 - }  
121 -  
122 - public void setType(ComponentType type) {  
123 - this.type = type;  
124 - }  
125 -  
126 - public ComponentScope getScope() {  
127 - return scope;  
128 - }  
129 -  
130 - public void setScope(ComponentScope scope) {  
131 - this.scope = scope;  
132 - }  
133 -  
134 - public String getName() {  
135 - return name;  
136 - }  
137 -  
138 - public void setName(String name) {  
139 - this.name = name;  
140 - }  
141 -  
142 - public String getClazz() {  
143 - return clazz;  
144 - }  
145 -  
146 - public void setClazz(String clazz) {  
147 - this.clazz = clazz;  
148 - }  
149 -  
150 - public JsonNode getConfigurationDescriptor() {  
151 - return configurationDescriptor;  
152 - }  
153 -  
154 - public void setConfigurationDescriptor(JsonNode configurationDescriptor) {  
155 - this.configurationDescriptor = configurationDescriptor;  
156 - }  
157 -  
158 - public String getSearchText() {  
159 - return searchText;  
160 - }  
161 -  
162 - @Override  
163 - public void setSearchText(String searchText) {  
164 - this.searchText = searchText;  
165 - }  
166 -  
167 - @Override  
168 - public String getSearchTextSource() {  
169 - return getSearchText();  
170 - }  
171 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.Customer;  
26 -import org.thingsboard.server.common.data.id.CustomerId;  
27 -import org.thingsboard.server.common.data.id.TenantId;  
28 -import org.thingsboard.server.dao.model.SearchTextEntity;  
29 -import org.thingsboard.server.dao.model.type.JsonCodec;  
30 -  
31 -import java.util.UUID;  
32 -  
33 -import static org.thingsboard.server.dao.model.ModelConstants.ADDRESS2_PROPERTY;  
34 -import static org.thingsboard.server.dao.model.ModelConstants.ADDRESS_PROPERTY;  
35 -import static org.thingsboard.server.dao.model.ModelConstants.CITY_PROPERTY;  
36 -import static org.thingsboard.server.dao.model.ModelConstants.COUNTRY_PROPERTY;  
37 -import static org.thingsboard.server.dao.model.ModelConstants.CUSTOMER_ADDITIONAL_INFO_PROPERTY;  
38 -import static org.thingsboard.server.dao.model.ModelConstants.CUSTOMER_COLUMN_FAMILY_NAME;  
39 -import static org.thingsboard.server.dao.model.ModelConstants.CUSTOMER_TENANT_ID_PROPERTY;  
40 -import static org.thingsboard.server.dao.model.ModelConstants.CUSTOMER_TITLE_PROPERTY;  
41 -import static org.thingsboard.server.dao.model.ModelConstants.EMAIL_PROPERTY;  
42 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
43 -import static org.thingsboard.server.dao.model.ModelConstants.PHONE_PROPERTY;  
44 -import static org.thingsboard.server.dao.model.ModelConstants.SEARCH_TEXT_PROPERTY;  
45 -import static org.thingsboard.server.dao.model.ModelConstants.STATE_PROPERTY;  
46 -import static org.thingsboard.server.dao.model.ModelConstants.ZIP_PROPERTY;  
47 -  
48 -@Table(name = CUSTOMER_COLUMN_FAMILY_NAME)  
49 -@EqualsAndHashCode  
50 -@ToString  
51 -public final class CustomerEntity implements SearchTextEntity<Customer> {  
52 -  
53 - @PartitionKey(value = 0)  
54 - @Column(name = ID_PROPERTY)  
55 - private UUID id;  
56 -  
57 - @PartitionKey(value = 1)  
58 - @Column(name = CUSTOMER_TENANT_ID_PROPERTY)  
59 - private UUID tenantId;  
60 -  
61 - @Column(name = CUSTOMER_TITLE_PROPERTY)  
62 - private String title;  
63 -  
64 - @Column(name = SEARCH_TEXT_PROPERTY)  
65 - private String searchText;  
66 -  
67 - @Column(name = COUNTRY_PROPERTY)  
68 - private String country;  
69 -  
70 - @Column(name = STATE_PROPERTY)  
71 - private String state;  
72 -  
73 - @Column(name = CITY_PROPERTY)  
74 - private String city;  
75 -  
76 - @Column(name = ADDRESS_PROPERTY)  
77 - private String address;  
78 -  
79 - @Column(name = ADDRESS2_PROPERTY)  
80 - private String address2;  
81 -  
82 - @Column(name = ZIP_PROPERTY)  
83 - private String zip;  
84 -  
85 - @Column(name = PHONE_PROPERTY)  
86 - private String phone;  
87 -  
88 - @Column(name = EMAIL_PROPERTY)  
89 - private String email;  
90 -  
91 - @Column(name = CUSTOMER_ADDITIONAL_INFO_PROPERTY, codec = JsonCodec.class)  
92 - private JsonNode additionalInfo;  
93 -  
94 - public CustomerEntity() {  
95 - super();  
96 - }  
97 -  
98 - public CustomerEntity(Customer customer) {  
99 - if (customer.getId() != null) {  
100 - this.id = customer.getId().getId();  
101 - }  
102 - this.tenantId = customer.getTenantId().getId();  
103 - this.title = customer.getTitle();  
104 - this.country = customer.getCountry();  
105 - this.state = customer.getState();  
106 - this.city = customer.getCity();  
107 - this.address = customer.getAddress();  
108 - this.address2 = customer.getAddress2();  
109 - this.zip = customer.getZip();  
110 - this.phone = customer.getPhone();  
111 - this.email = customer.getEmail();  
112 - this.additionalInfo = customer.getAdditionalInfo();  
113 - }  
114 -  
115 - public UUID getId() {  
116 - return id;  
117 - }  
118 -  
119 - public void setId(UUID id) {  
120 - this.id = id;  
121 - }  
122 -  
123 - public UUID getTenantId() {  
124 - return tenantId;  
125 - }  
126 -  
127 - public void setTenantId(UUID tenantId) {  
128 - this.tenantId = tenantId;  
129 - }  
130 -  
131 - public String getTitle() {  
132 - return title;  
133 - }  
134 -  
135 - public void setTitle(String title) {  
136 - this.title = title;  
137 - }  
138 -  
139 - public String getCountry() {  
140 - return country;  
141 - }  
142 -  
143 - public void setCountry(String country) {  
144 - this.country = country;  
145 - }  
146 -  
147 - public String getState() {  
148 - return state;  
149 - }  
150 -  
151 - public void setState(String state) {  
152 - this.state = state;  
153 - }  
154 -  
155 - public String getCity() {  
156 - return city;  
157 - }  
158 -  
159 - public void setCity(String city) {  
160 - this.city = city;  
161 - }  
162 -  
163 - public String getAddress() {  
164 - return address;  
165 - }  
166 -  
167 - public void setAddress(String address) {  
168 - this.address = address;  
169 - }  
170 -  
171 - public String getAddress2() {  
172 - return address2;  
173 - }  
174 -  
175 - public void setAddress2(String address2) {  
176 - this.address2 = address2;  
177 - }  
178 -  
179 - public String getZip() {  
180 - return zip;  
181 - }  
182 -  
183 - public void setZip(String zip) {  
184 - this.zip = zip;  
185 - }  
186 -  
187 - public String getPhone() {  
188 - return phone;  
189 - }  
190 -  
191 - public void setPhone(String phone) {  
192 - this.phone = phone;  
193 - }  
194 -  
195 - public String getEmail() {  
196 - return email;  
197 - }  
198 -  
199 - public void setEmail(String email) {  
200 - this.email = email;  
201 - }  
202 -  
203 - public JsonNode getAdditionalInfo() {  
204 - return additionalInfo;  
205 - }  
206 -  
207 - public void setAdditionalInfo(JsonNode additionalInfo) {  
208 - this.additionalInfo = additionalInfo;  
209 - }  
210 -  
211 - @Override  
212 - public String getSearchTextSource() {  
213 - return getTitle();  
214 - }  
215 -  
216 - @Override  
217 - public void setSearchText(String searchText) {  
218 - this.searchText = searchText;  
219 - }  
220 -  
221 - public String getSearchText() {  
222 - return searchText;  
223 - }  
224 -  
225 - @Override  
226 - public Customer toData() {  
227 - Customer customer = new Customer(new CustomerId(id));  
228 - customer.setCreatedTime(UUIDs.unixTimestamp(id));  
229 - customer.setTenantId(new TenantId(tenantId));  
230 - customer.setTitle(title);  
231 - customer.setCountry(country);  
232 - customer.setState(state);  
233 - customer.setCity(city);  
234 - customer.setAddress(address);  
235 - customer.setAddress2(address2);  
236 - customer.setZip(zip);  
237 - customer.setPhone(phone);  
238 - customer.setEmail(email);  
239 - customer.setAdditionalInfo(additionalInfo);  
240 - return customer;  
241 - }  
242 -  
243 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.core.JsonProcessingException;  
23 -import com.fasterxml.jackson.databind.JavaType;  
24 -import com.fasterxml.jackson.databind.JsonNode;  
25 -import com.fasterxml.jackson.databind.ObjectMapper;  
26 -import lombok.EqualsAndHashCode;  
27 -import lombok.ToString;  
28 -import lombok.extern.slf4j.Slf4j;  
29 -import org.springframework.util.StringUtils;  
30 -import org.thingsboard.server.common.data.Dashboard;  
31 -import org.thingsboard.server.common.data.ShortCustomerInfo;  
32 -import org.thingsboard.server.common.data.id.DashboardId;  
33 -import org.thingsboard.server.common.data.id.TenantId;  
34 -import org.thingsboard.server.dao.model.SearchTextEntity;  
35 -import org.thingsboard.server.dao.model.type.JsonCodec;  
36 -  
37 -import java.io.IOException;  
38 -import java.util.HashSet;  
39 -import java.util.UUID;  
40 -  
41 -import static org.thingsboard.server.dao.model.ModelConstants.DASHBOARD_ASSIGNED_CUSTOMERS_PROPERTY;  
42 -import static org.thingsboard.server.dao.model.ModelConstants.DASHBOARD_COLUMN_FAMILY_NAME;  
43 -import static org.thingsboard.server.dao.model.ModelConstants.DASHBOARD_CONFIGURATION_PROPERTY;  
44 -import static org.thingsboard.server.dao.model.ModelConstants.DASHBOARD_TENANT_ID_PROPERTY;  
45 -import static org.thingsboard.server.dao.model.ModelConstants.DASHBOARD_TITLE_PROPERTY;  
46 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
47 -import static org.thingsboard.server.dao.model.ModelConstants.SEARCH_TEXT_PROPERTY;  
48 -  
49 -@Table(name = DASHBOARD_COLUMN_FAMILY_NAME)  
50 -@EqualsAndHashCode  
51 -@ToString  
52 -@Slf4j  
53 -public final class DashboardEntity implements SearchTextEntity<Dashboard> {  
54 -  
55 - private static final ObjectMapper objectMapper = new ObjectMapper();  
56 - private static final JavaType assignedCustomersType =  
57 - objectMapper.getTypeFactory().constructCollectionType(HashSet.class, ShortCustomerInfo.class);  
58 -  
59 - @PartitionKey(value = 0)  
60 - @Column(name = ID_PROPERTY)  
61 - private UUID id;  
62 -  
63 - @PartitionKey(value = 1)  
64 - @Column(name = DASHBOARD_TENANT_ID_PROPERTY)  
65 - private UUID tenantId;  
66 -  
67 - @Column(name = DASHBOARD_TITLE_PROPERTY)  
68 - private String title;  
69 -  
70 - @Column(name = SEARCH_TEXT_PROPERTY)  
71 - private String searchText;  
72 -  
73 - @Column(name = DASHBOARD_ASSIGNED_CUSTOMERS_PROPERTY)  
74 - private String assignedCustomers;  
75 -  
76 - @Column(name = DASHBOARD_CONFIGURATION_PROPERTY, codec = JsonCodec.class)  
77 - private JsonNode configuration;  
78 -  
79 - public DashboardEntity() {  
80 - super();  
81 - }  
82 -  
83 - public DashboardEntity(Dashboard dashboard) {  
84 - if (dashboard.getId() != null) {  
85 - this.id = dashboard.getId().getId();  
86 - }  
87 - if (dashboard.getTenantId() != null) {  
88 - this.tenantId = dashboard.getTenantId().getId();  
89 - }  
90 - this.title = dashboard.getTitle();  
91 - if (dashboard.getAssignedCustomers() != null) {  
92 - try {  
93 - this.assignedCustomers = objectMapper.writeValueAsString(dashboard.getAssignedCustomers());  
94 - } catch (JsonProcessingException e) {  
95 - log.error("Unable to serialize assigned customers to string!", e);  
96 - }  
97 - }  
98 - this.configuration = dashboard.getConfiguration();  
99 - }  
100 -  
101 - public UUID getId() {  
102 - return id;  
103 - }  
104 -  
105 - public void setId(UUID id) {  
106 - this.id = id;  
107 - }  
108 -  
109 - public UUID getTenantId() {  
110 - return tenantId;  
111 - }  
112 -  
113 - public void setTenantId(UUID tenantId) {  
114 - this.tenantId = tenantId;  
115 - }  
116 -  
117 - public String getTitle() {  
118 - return title;  
119 - }  
120 -  
121 - public void setTitle(String title) {  
122 - this.title = title;  
123 - }  
124 -  
125 - public String getAssignedCustomers() {  
126 - return assignedCustomers;  
127 - }  
128 -  
129 - public void setAssignedCustomers(String assignedCustomers) {  
130 - this.assignedCustomers = assignedCustomers;  
131 - }  
132 -  
133 - public JsonNode getConfiguration() {  
134 - return configuration;  
135 - }  
136 -  
137 - public void setConfiguration(JsonNode configuration) {  
138 - this.configuration = configuration;  
139 - }  
140 -  
141 - @Override  
142 - public String getSearchTextSource() {  
143 - return getTitle();  
144 - }  
145 -  
146 - @Override  
147 - public void setSearchText(String searchText) {  
148 - this.searchText = searchText;  
149 - }  
150 -  
151 - public String getSearchText() {  
152 - return searchText;  
153 - }  
154 -  
155 - @Override  
156 - public Dashboard toData() {  
157 - Dashboard dashboard = new Dashboard(new DashboardId(id));  
158 - dashboard.setCreatedTime(UUIDs.unixTimestamp(id));  
159 - if (tenantId != null) {  
160 - dashboard.setTenantId(new TenantId(tenantId));  
161 - }  
162 - dashboard.setTitle(title);  
163 - if (!StringUtils.isEmpty(assignedCustomers)) {  
164 - try {  
165 - dashboard.setAssignedCustomers(objectMapper.readValue(assignedCustomers, assignedCustomersType));  
166 - } catch (IOException e) {  
167 - log.warn("Unable to parse assigned customers!", e);  
168 - }  
169 - }  
170 - dashboard.setConfiguration(configuration);  
171 - return dashboard;  
172 - }  
173 -  
174 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.core.JsonProcessingException;  
23 -import com.fasterxml.jackson.databind.JavaType;  
24 -import com.fasterxml.jackson.databind.ObjectMapper;  
25 -import lombok.EqualsAndHashCode;  
26 -import lombok.ToString;  
27 -import lombok.extern.slf4j.Slf4j;  
28 -import org.springframework.util.StringUtils;  
29 -import org.thingsboard.server.common.data.DashboardInfo;  
30 -import org.thingsboard.server.common.data.ShortCustomerInfo;  
31 -import org.thingsboard.server.common.data.id.DashboardId;  
32 -import org.thingsboard.server.common.data.id.TenantId;  
33 -import org.thingsboard.server.dao.model.SearchTextEntity;  
34 -  
35 -import java.io.IOException;  
36 -import java.util.HashSet;  
37 -import java.util.UUID;  
38 -  
39 -import static org.thingsboard.server.dao.model.ModelConstants.DASHBOARD_ASSIGNED_CUSTOMERS_PROPERTY;  
40 -import static org.thingsboard.server.dao.model.ModelConstants.DASHBOARD_COLUMN_FAMILY_NAME;  
41 -import static org.thingsboard.server.dao.model.ModelConstants.DASHBOARD_TENANT_ID_PROPERTY;  
42 -import static org.thingsboard.server.dao.model.ModelConstants.DASHBOARD_TITLE_PROPERTY;  
43 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
44 -import static org.thingsboard.server.dao.model.ModelConstants.SEARCH_TEXT_PROPERTY;  
45 -  
46 -@Table(name = DASHBOARD_COLUMN_FAMILY_NAME)  
47 -@EqualsAndHashCode  
48 -@ToString  
49 -@Slf4j  
50 -public class DashboardInfoEntity implements SearchTextEntity<DashboardInfo> {  
51 -  
52 - private static final ObjectMapper objectMapper = new ObjectMapper();  
53 - private static final JavaType assignedCustomersType =  
54 - objectMapper.getTypeFactory().constructCollectionType(HashSet.class, ShortCustomerInfo.class);  
55 -  
56 - @PartitionKey(value = 0)  
57 - @Column(name = ID_PROPERTY)  
58 - private UUID id;  
59 -  
60 - @PartitionKey(value = 1)  
61 - @Column(name = DASHBOARD_TENANT_ID_PROPERTY)  
62 - private UUID tenantId;  
63 -  
64 - @Column(name = DASHBOARD_TITLE_PROPERTY)  
65 - private String title;  
66 -  
67 - @Column(name = SEARCH_TEXT_PROPERTY)  
68 - private String searchText;  
69 -  
70 - @Column(name = DASHBOARD_ASSIGNED_CUSTOMERS_PROPERTY)  
71 - private String assignedCustomers;  
72 -  
73 - public DashboardInfoEntity() {  
74 - super();  
75 - }  
76 -  
77 - public DashboardInfoEntity(DashboardInfo dashboardInfo) {  
78 - if (dashboardInfo.getId() != null) {  
79 - this.id = dashboardInfo.getId().getId();  
80 - }  
81 - if (dashboardInfo.getTenantId() != null) {  
82 - this.tenantId = dashboardInfo.getTenantId().getId();  
83 - }  
84 - this.title = dashboardInfo.getTitle();  
85 - if (dashboardInfo.getAssignedCustomers() != null) {  
86 - try {  
87 - this.assignedCustomers = objectMapper.writeValueAsString(dashboardInfo.getAssignedCustomers());  
88 - } catch (JsonProcessingException e) {  
89 - log.error("Unable to serialize assigned customers to string!", e);  
90 - }  
91 - }  
92 - }  
93 -  
94 - public UUID getId() {  
95 - return id;  
96 - }  
97 -  
98 - public void setId(UUID id) {  
99 - this.id = id;  
100 - }  
101 -  
102 - public UUID getTenantId() {  
103 - return tenantId;  
104 - }  
105 -  
106 - public void setTenantId(UUID tenantId) {  
107 - this.tenantId = tenantId;  
108 - }  
109 -  
110 - public String getTitle() {  
111 - return title;  
112 - }  
113 -  
114 - public void setTitle(String title) {  
115 - this.title = title;  
116 - }  
117 -  
118 - public String getAssignedCustomers() {  
119 - return assignedCustomers;  
120 - }  
121 -  
122 - public void setAssignedCustomers(String assignedCustomers) {  
123 - this.assignedCustomers = assignedCustomers;  
124 - }  
125 -  
126 - @Override  
127 - public String getSearchTextSource() {  
128 - return getTitle();  
129 - }  
130 -  
131 - @Override  
132 - public void setSearchText(String searchText) {  
133 - this.searchText = searchText;  
134 - }  
135 -  
136 - public String getSearchText() {  
137 - return searchText;  
138 - }  
139 -  
140 - @Override  
141 - public DashboardInfo toData() {  
142 - DashboardInfo dashboardInfo = new DashboardInfo(new DashboardId(id));  
143 - dashboardInfo.setCreatedTime(UUIDs.unixTimestamp(id));  
144 - if (tenantId != null) {  
145 - dashboardInfo.setTenantId(new TenantId(tenantId));  
146 - }  
147 - dashboardInfo.setTitle(title);  
148 - if (!StringUtils.isEmpty(assignedCustomers)) {  
149 - try {  
150 - dashboardInfo.setAssignedCustomers(objectMapper.readValue(assignedCustomers, assignedCustomersType));  
151 - } catch (IOException e) {  
152 - log.warn("Unable to parse assigned customers!", e);  
153 - }  
154 - }  
155 - return dashboardInfo;  
156 - }  
157 -  
158 -}  
1 -/**  
2 - * Copyright © 2016-2019 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 lombok.EqualsAndHashCode;  
23 -import lombok.ToString;  
24 -import org.thingsboard.server.common.data.id.DeviceCredentialsId;  
25 -import org.thingsboard.server.common.data.id.DeviceId;  
26 -import org.thingsboard.server.common.data.security.DeviceCredentials;  
27 -import org.thingsboard.server.common.data.security.DeviceCredentialsType;  
28 -import org.thingsboard.server.dao.model.BaseEntity;  
29 -import org.thingsboard.server.dao.model.type.DeviceCredentialsTypeCodec;  
30 -  
31 -import java.util.UUID;  
32 -  
33 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_CREDENTIALS_COLUMN_FAMILY_NAME;  
34 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_CREDENTIALS_CREDENTIALS_ID_PROPERTY;  
35 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_CREDENTIALS_CREDENTIALS_TYPE_PROPERTY;  
36 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_CREDENTIALS_CREDENTIALS_VALUE_PROPERTY;  
37 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_CREDENTIALS_DEVICE_ID_PROPERTY;  
38 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
39 -  
40 -@Table(name = DEVICE_CREDENTIALS_COLUMN_FAMILY_NAME)  
41 -@EqualsAndHashCode  
42 -@ToString  
43 -public final class DeviceCredentialsEntity implements BaseEntity<DeviceCredentials> {  
44 -  
45 - @PartitionKey(value = 0)  
46 - @Column(name = ID_PROPERTY)  
47 - private UUID id;  
48 -  
49 - @Column(name = DEVICE_CREDENTIALS_DEVICE_ID_PROPERTY)  
50 - private UUID deviceId;  
51 -  
52 - @Column(name = DEVICE_CREDENTIALS_CREDENTIALS_TYPE_PROPERTY, codec = DeviceCredentialsTypeCodec.class)  
53 - private DeviceCredentialsType credentialsType;  
54 -  
55 - @Column(name = DEVICE_CREDENTIALS_CREDENTIALS_ID_PROPERTY)  
56 - private String credentialsId;  
57 -  
58 - @Column(name = DEVICE_CREDENTIALS_CREDENTIALS_VALUE_PROPERTY)  
59 - private String credentialsValue;  
60 -  
61 - public DeviceCredentialsEntity() {  
62 - super();  
63 - }  
64 -  
65 - public DeviceCredentialsEntity(DeviceCredentials deviceCredentials) {  
66 - if (deviceCredentials.getId() != null) {  
67 - this.id = deviceCredentials.getId().getId();  
68 - }  
69 - if (deviceCredentials.getDeviceId() != null) {  
70 - this.deviceId = deviceCredentials.getDeviceId().getId();  
71 - }  
72 - this.credentialsType = deviceCredentials.getCredentialsType();  
73 - this.credentialsId = deviceCredentials.getCredentialsId();  
74 - this.credentialsValue = deviceCredentials.getCredentialsValue();  
75 - }  
76 -  
77 - public UUID getId() {  
78 - return id;  
79 - }  
80 -  
81 - public void setId(UUID id) {  
82 - this.id = id;  
83 - }  
84 -  
85 - public UUID getDeviceId() {  
86 - return deviceId;  
87 - }  
88 -  
89 - public void setDeviceId(UUID deviceId) {  
90 - this.deviceId = deviceId;  
91 - }  
92 -  
93 - public DeviceCredentialsType getCredentialsType() {  
94 - return credentialsType;  
95 - }  
96 -  
97 - public void setCredentialsType(DeviceCredentialsType credentialsType) {  
98 - this.credentialsType = credentialsType;  
99 - }  
100 -  
101 - public String getCredentialsId() {  
102 - return credentialsId;  
103 - }  
104 -  
105 - public void setCredentialsId(String credentialsId) {  
106 - this.credentialsId = credentialsId;  
107 - }  
108 -  
109 - public String getCredentialsValue() {  
110 - return credentialsValue;  
111 - }  
112 -  
113 - public void setCredentialsValue(String credentialsValue) {  
114 - this.credentialsValue = credentialsValue;  
115 - }  
116 -  
117 - @Override  
118 - public DeviceCredentials toData() {  
119 - DeviceCredentials deviceCredentials = new DeviceCredentials(new DeviceCredentialsId(id));  
120 - deviceCredentials.setCreatedTime(UUIDs.unixTimestamp(id));  
121 - if (deviceId != null) {  
122 - deviceCredentials.setDeviceId(new DeviceId(deviceId));  
123 - }  
124 - deviceCredentials.setCredentialsType(credentialsType);  
125 - deviceCredentials.setCredentialsId(credentialsId);  
126 - deviceCredentials.setCredentialsValue(credentialsValue);  
127 - return deviceCredentials;  
128 - }  
129 -  
130 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.Device;  
26 -import org.thingsboard.server.common.data.id.CustomerId;  
27 -import org.thingsboard.server.common.data.id.DeviceId;  
28 -import org.thingsboard.server.common.data.id.TenantId;  
29 -import org.thingsboard.server.dao.model.SearchTextEntity;  
30 -import org.thingsboard.server.dao.model.type.JsonCodec;  
31 -  
32 -import java.util.UUID;  
33 -  
34 -import static org.thingsboard.server.dao.model.ModelConstants.*;  
35 -  
36 -@Table(name = DEVICE_COLUMN_FAMILY_NAME)  
37 -@EqualsAndHashCode  
38 -@ToString  
39 -public final class DeviceEntity implements SearchTextEntity<Device> {  
40 -  
41 - @PartitionKey(value = 0)  
42 - @Column(name = ID_PROPERTY)  
43 - private UUID id;  
44 -  
45 - @PartitionKey(value = 1)  
46 - @Column(name = DEVICE_TENANT_ID_PROPERTY)  
47 - private UUID tenantId;  
48 -  
49 - @PartitionKey(value = 2)  
50 - @Column(name = DEVICE_CUSTOMER_ID_PROPERTY)  
51 - private UUID customerId;  
52 -  
53 - @PartitionKey(value = 3)  
54 - @Column(name = DEVICE_TYPE_PROPERTY)  
55 - private String type;  
56 -  
57 - @Column(name = DEVICE_NAME_PROPERTY)  
58 - private String name;  
59 -  
60 - @Column(name = DEVICE_LABEL_PROPERTY)  
61 - private String label;  
62 -  
63 - @Column(name = SEARCH_TEXT_PROPERTY)  
64 - private String searchText;  
65 -  
66 - @Column(name = DEVICE_ADDITIONAL_INFO_PROPERTY, codec = JsonCodec.class)  
67 - private JsonNode additionalInfo;  
68 -  
69 - public DeviceEntity() {  
70 - super();  
71 - }  
72 -  
73 - public DeviceEntity(Device device) {  
74 - if (device.getId() != null) {  
75 - this.id = device.getId().getId();  
76 - }  
77 - if (device.getTenantId() != null) {  
78 - this.tenantId = device.getTenantId().getId();  
79 - }  
80 - if (device.getCustomerId() != null) {  
81 - this.customerId = device.getCustomerId().getId();  
82 - }  
83 - this.name = device.getName();  
84 - this.type = device.getType();  
85 - this.label = device.getLabel();  
86 - this.additionalInfo = device.getAdditionalInfo();  
87 - }  
88 -  
89 - public UUID getId() {  
90 - return id;  
91 - }  
92 -  
93 - public void setId(UUID id) {  
94 - this.id = id;  
95 - }  
96 -  
97 - public UUID getTenantId() {  
98 - return tenantId;  
99 - }  
100 -  
101 - public void setTenantId(UUID tenantId) {  
102 - this.tenantId = tenantId;  
103 - }  
104 -  
105 - public UUID getCustomerId() {  
106 - return customerId;  
107 - }  
108 -  
109 - public void setCustomerId(UUID customerId) {  
110 - this.customerId = customerId;  
111 - }  
112 -  
113 - public String getName() {  
114 - return name;  
115 - }  
116 -  
117 - public void setName(String name) {  
118 - this.name = name;  
119 - }  
120 -  
121 - public String getType() {  
122 - return type;  
123 - }  
124 -  
125 - public void setType(String type) {  
126 - this.type = type;  
127 - }  
128 -  
129 - public JsonNode getAdditionalInfo() {  
130 - return additionalInfo;  
131 - }  
132 -  
133 - public void setAdditionalInfo(JsonNode additionalInfo) {  
134 - this.additionalInfo = additionalInfo;  
135 - }  
136 -  
137 - @Override  
138 - public String getSearchTextSource() {  
139 - return getName();  
140 - }  
141 -  
142 - @Override  
143 - public void setSearchText(String searchText) {  
144 - this.searchText = searchText;  
145 - }  
146 -  
147 - public String getSearchText() {  
148 - return searchText;  
149 - }  
150 -  
151 - @Override  
152 - public Device toData() {  
153 - Device device = new Device(new DeviceId(id));  
154 - device.setCreatedTime(UUIDs.unixTimestamp(id));  
155 - if (tenantId != null) {  
156 - device.setTenantId(new TenantId(tenantId));  
157 - }  
158 - if (customerId != null) {  
159 - device.setCustomerId(new CustomerId(customerId));  
160 - }  
161 - device.setName(name);  
162 - device.setType(type);  
163 - device.setLabel(label);  
164 - device.setAdditionalInfo(additionalInfo);  
165 - return device;  
166 - }  
167 -  
168 -}  
1 -/**  
2 - * Copyright © 2016-2019 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 com.fasterxml.jackson.databind.ObjectMapper;  
24 -import lombok.Data;  
25 -import lombok.EqualsAndHashCode;  
26 -import lombok.ToString;  
27 -import lombok.extern.slf4j.Slf4j;  
28 -import org.hibernate.annotations.Type;  
29 -import org.thingsboard.server.common.data.EntityType;  
30 -import org.thingsboard.server.common.data.EntityView;  
31 -import org.thingsboard.server.common.data.id.CustomerId;  
32 -import org.thingsboard.server.common.data.id.EntityIdFactory;  
33 -import org.thingsboard.server.common.data.id.EntityViewId;  
34 -import org.thingsboard.server.common.data.id.TenantId;  
35 -import org.thingsboard.server.common.data.objects.TelemetryEntityView;  
36 -import org.thingsboard.server.dao.model.ModelConstants;  
37 -import org.thingsboard.server.dao.model.SearchTextEntity;  
38 -  
39 -import javax.persistence.EnumType;  
40 -import javax.persistence.Enumerated;  
41 -import java.io.IOException;  
42 -import java.util.UUID;  
43 -  
44 -import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_TYPE_PROPERTY;  
45 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_TYPE_PROPERTY;  
46 -import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_TABLE_FAMILY_NAME;  
47 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
48 -  
49 -/**  
50 - * Created by Victor Basanets on 8/31/2017.  
51 - */  
52 -@Data  
53 -@Table(name = ENTITY_VIEW_TABLE_FAMILY_NAME)  
54 -@EqualsAndHashCode  
55 -@ToString  
56 -@Slf4j  
57 -public class EntityViewEntity implements SearchTextEntity<EntityView> {  
58 -  
59 - @PartitionKey(value = 0)  
60 - @Column(name = ID_PROPERTY)  
61 - private UUID id;  
62 -  
63 - @PartitionKey(value = 1)  
64 - @Column(name = ModelConstants.ENTITY_VIEW_TENANT_ID_PROPERTY)  
65 - private UUID tenantId;  
66 -  
67 - @PartitionKey(value = 2)  
68 - @Column(name = ModelConstants.ENTITY_VIEW_CUSTOMER_ID_PROPERTY)  
69 - private UUID customerId;  
70 -  
71 - @PartitionKey(value = 3)  
72 - @Column(name = DEVICE_TYPE_PROPERTY)  
73 - private String type;  
74 -  
75 - @Enumerated(EnumType.STRING)  
76 - @Column(name = ENTITY_TYPE_PROPERTY)  
77 - private EntityType entityType;  
78 -  
79 - @Column(name = ModelConstants.ENTITY_VIEW_ENTITY_ID_PROPERTY)  
80 - private UUID entityId;  
81 -  
82 - @Column(name = ModelConstants.ENTITY_VIEW_NAME_PROPERTY)  
83 - private String name;  
84 -  
85 - @Column(name = ModelConstants.ENTITY_VIEW_KEYS_PROPERTY)  
86 - private String keys;  
87 -  
88 - @Column(name = ModelConstants.ENTITY_VIEW_START_TS_PROPERTY)  
89 - private long startTs;  
90 -  
91 - @Column(name = ModelConstants.ENTITY_VIEW_END_TS_PROPERTY)  
92 - private long endTs;  
93 -  
94 - @Column(name = ModelConstants.SEARCH_TEXT_PROPERTY)  
95 - private String searchText;  
96 -  
97 - @Type(type = "json")  
98 - @Column(name = ModelConstants.ENTITY_VIEW_ADDITIONAL_INFO_PROPERTY)  
99 - private JsonNode additionalInfo;  
100 -  
101 - private static final ObjectMapper mapper = new ObjectMapper();  
102 -  
103 - public EntityViewEntity() {  
104 - super();  
105 - }  
106 -  
107 - public EntityViewEntity(EntityView entityView) {  
108 - if (entityView.getId() != null) {  
109 - this.id = entityView.getId().getId();  
110 - }  
111 - if (entityView.getEntityId() != null) {  
112 - this.entityId = entityView.getEntityId().getId();  
113 - this.entityType = entityView.getEntityId().getEntityType();  
114 - }  
115 - if (entityView.getTenantId() != null) {  
116 - this.tenantId = entityView.getTenantId().getId();  
117 - }  
118 - if (entityView.getCustomerId() != null) {  
119 - this.customerId = entityView.getCustomerId().getId();  
120 - }  
121 - this.type = entityView.getType();  
122 - this.name = entityView.getName();  
123 - try {  
124 - this.keys = mapper.writeValueAsString(entityView.getKeys());  
125 - } catch (IOException e) {  
126 - log.error("Unable to serialize entity view keys!", e);  
127 - }  
128 - this.startTs = entityView.getStartTimeMs();  
129 - this.endTs = entityView.getEndTimeMs();  
130 - this.searchText = entityView.getSearchText();  
131 - this.additionalInfo = entityView.getAdditionalInfo();  
132 - }  
133 -  
134 - @Override  
135 - public String getSearchTextSource() {  
136 - return name;  
137 - }  
138 -  
139 - @Override  
140 - public EntityView toData() {  
141 - EntityView entityView = new EntityView(new EntityViewId(id));  
142 - entityView.setCreatedTime(UUIDs.unixTimestamp(id));  
143 - if (entityId != null) {  
144 - entityView.setEntityId(EntityIdFactory.getByTypeAndId(entityType.name(), entityId.toString()));  
145 - }  
146 - if (tenantId != null) {  
147 - entityView.setTenantId(new TenantId(tenantId));  
148 - }  
149 - if (customerId != null) {  
150 - entityView.setCustomerId(new CustomerId(customerId));  
151 - }  
152 - entityView.setType(type);  
153 - entityView.setName(name);  
154 - try {  
155 - entityView.setKeys(mapper.readValue(keys, TelemetryEntityView.class));  
156 - } catch (IOException e) {  
157 - log.error("Unable to read entity view keys!", e);  
158 - }  
159 - entityView.setStartTimeMs(startTs);  
160 - entityView.setEndTimeMs(endTs);  
161 - entityView.setAdditionalInfo(additionalInfo);  
162 - return entityView;  
163 - }  
164 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.Data;  
25 -import lombok.NoArgsConstructor;  
26 -import org.thingsboard.server.common.data.EntityType;  
27 -import org.thingsboard.server.common.data.Event;  
28 -import org.thingsboard.server.common.data.id.EntityIdFactory;  
29 -import org.thingsboard.server.common.data.id.EventId;  
30 -import org.thingsboard.server.common.data.id.TenantId;  
31 -import org.thingsboard.server.dao.model.BaseEntity;  
32 -import org.thingsboard.server.dao.model.type.EntityTypeCodec;  
33 -import org.thingsboard.server.dao.model.type.JsonCodec;  
34 -  
35 -import java.util.UUID;  
36 -  
37 -import static org.thingsboard.server.dao.model.ModelConstants.EVENT_BODY_PROPERTY;  
38 -import static org.thingsboard.server.dao.model.ModelConstants.EVENT_COLUMN_FAMILY_NAME;  
39 -import static org.thingsboard.server.dao.model.ModelConstants.EVENT_ENTITY_ID_PROPERTY;  
40 -import static org.thingsboard.server.dao.model.ModelConstants.EVENT_ENTITY_TYPE_PROPERTY;  
41 -import static org.thingsboard.server.dao.model.ModelConstants.EVENT_TENANT_ID_PROPERTY;  
42 -import static org.thingsboard.server.dao.model.ModelConstants.EVENT_TYPE_PROPERTY;  
43 -import static org.thingsboard.server.dao.model.ModelConstants.EVENT_UID_PROPERTY;  
44 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
45 -  
46 -/**  
47 - * @author Andrew Shvayka  
48 - */  
49 -@Data  
50 -@NoArgsConstructor  
51 -@Table(name = EVENT_COLUMN_FAMILY_NAME)  
52 -public class EventEntity implements BaseEntity<Event> {  
53 -  
54 - @Column(name = ID_PROPERTY)  
55 - private UUID id;  
56 -  
57 - @PartitionKey()  
58 - @Column(name = EVENT_TENANT_ID_PROPERTY)  
59 - private UUID tenantId;  
60 -  
61 - @PartitionKey(value = 1)  
62 - @Column(name = EVENT_ENTITY_TYPE_PROPERTY, codec = EntityTypeCodec.class)  
63 - private EntityType entityType;  
64 -  
65 - @PartitionKey(value = 2)  
66 - @Column(name = EVENT_ENTITY_ID_PROPERTY)  
67 - private UUID entityId;  
68 -  
69 - @ClusteringColumn()  
70 - @Column(name = EVENT_TYPE_PROPERTY)  
71 - private String eventType;  
72 -  
73 - @ClusteringColumn(value = 1)  
74 - @Column(name = EVENT_UID_PROPERTY)  
75 - private String eventUid;  
76 -  
77 - @Column(name = EVENT_BODY_PROPERTY, codec = JsonCodec.class)  
78 - private JsonNode body;  
79 -  
80 - public EventEntity(Event event) {  
81 - if (event.getId() != null) {  
82 - this.id = event.getId().getId();  
83 - }  
84 - if (event.getTenantId() != null) {  
85 - this.tenantId = event.getTenantId().getId();  
86 - }  
87 - if (event.getEntityId() != null) {  
88 - this.entityType = event.getEntityId().getEntityType();  
89 - this.entityId = event.getEntityId().getId();  
90 - }  
91 - this.eventType = event.getType();  
92 - this.eventUid = event.getUid();  
93 - this.body = event.getBody();  
94 - }  
95 -  
96 - @Override  
97 - public UUID getId() {  
98 - return id;  
99 - }  
100 -  
101 - @Override  
102 - public void setId(UUID id) {  
103 - this.id = id;  
104 - }  
105 -  
106 - @Override  
107 - public Event toData() {  
108 - Event event = new Event(new EventId(id));  
109 - event.setCreatedTime(UUIDs.unixTimestamp(id));  
110 - event.setTenantId(new TenantId(tenantId));  
111 - event.setEntityId(EntityIdFactory.getByTypeAndUuid(entityType, entityId));  
112 - event.setBody(body);  
113 - event.setType(eventType);  
114 - event.setUid(eventUid);  
115 - return event;  
116 - }  
117 -}  
1 -/**  
2 - * Copyright © 2016-2019 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.Getter;  
26 -import lombok.Setter;  
27 -import lombok.ToString;  
28 -import org.thingsboard.server.common.data.id.RuleChainId;  
29 -import org.thingsboard.server.common.data.id.RuleNodeId;  
30 -import org.thingsboard.server.common.data.id.TenantId;  
31 -import org.thingsboard.server.common.data.rule.RuleChain;  
32 -import org.thingsboard.server.dao.DaoUtil;  
33 -import org.thingsboard.server.dao.model.SearchTextEntity;  
34 -import org.thingsboard.server.dao.model.type.JsonCodec;  
35 -  
36 -import java.util.UUID;  
37 -  
38 -import static org.thingsboard.server.dao.model.ModelConstants.ADDITIONAL_INFO_PROPERTY;  
39 -import static org.thingsboard.server.dao.model.ModelConstants.DEBUG_MODE;  
40 -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY;  
41 -import static org.thingsboard.server.dao.model.ModelConstants.RULE_CHAIN_COLUMN_FAMILY_NAME;  
42 -import static org.thingsboard.server.dao.model.ModelConstants.RULE_CHAIN_CONFIGURATION_PROPERTY;  
43 -import static org.thingsboard.server.dao.model.ModelConstants.RULE_CHAIN_FIRST_RULE_NODE_ID_PROPERTY;  
44 -import static org.thingsboard.server.dao.model.ModelConstants.RULE_CHAIN_NAME_PROPERTY;  
45 -import static org.thingsboard.server.dao.model.ModelConstants.RULE_CHAIN_ROOT_PROPERTY;  
46 -import static org.thingsboard.server.dao.model.ModelConstants.RULE_CHAIN_TENANT_ID_PROPERTY;  
47 -import static org.thingsboard.server.dao.model.ModelConstants.SEARCH_TEXT_PROPERTY;  
48 -  
49 -@Table(name = RULE_CHAIN_COLUMN_FAMILY_NAME)  
50 -@EqualsAndHashCode  
51 -@ToString  
52 -public class RuleChainEntity implements SearchTextEntity<RuleChain> {  
53 -  
54 - @PartitionKey  
55 - @Column(name = ID_PROPERTY)  
56 - private UUID id;  
57 - @ClusteringColumn  
58 - @Column(name = RULE_CHAIN_TENANT_ID_PROPERTY)  
59 - private UUID tenantId;  
60 - @Column(name = RULE_CHAIN_NAME_PROPERTY)  
61 - private String name;  
62 - @Column(name = SEARCH_TEXT_PROPERTY)  
63 - private String searchText;  
64 - @Column(name = RULE_CHAIN_FIRST_RULE_NODE_ID_PROPERTY)  
65 - private UUID firstRuleNodeId;  
66 - @Column(name = RULE_CHAIN_ROOT_PROPERTY)  
67 - private boolean root;  
68 - @Getter  
69 - @Setter  
70 - @Column(name = DEBUG_MODE)  
71 - private boolean debugMode;  
72 - @Column(name = RULE_CHAIN_CONFIGURATION_PROPERTY, codec = JsonCodec.class)  
73 - private JsonNode configuration;  
74 - @Column(name = ADDITIONAL_INFO_PROPERTY, codec = JsonCodec.class)  
75 - private JsonNode additionalInfo;  
76 -  
77 - public RuleChainEntity() {  
78 - }  
79 -  
80 - public RuleChainEntity(RuleChain ruleChain) {  
81 - if (ruleChain.getId() != null) {  
82 - this.id = ruleChain.getUuidId();  
83 - }  
84 - this.tenantId = DaoUtil.getId(ruleChain.getTenantId());  
85 - this.name = ruleChain.getName();  
86 - this.searchText = ruleChain.getName();  
87 - this.firstRuleNodeId = DaoUtil.getId(ruleChain.getFirstRuleNodeId());  
88 - this.root = ruleChain.isRoot();  
89 - this.debugMode = ruleChain.isDebugMode();  
90 - this.configuration = ruleChain.getConfiguration();  
91 - this.additionalInfo = ruleChain.getAdditionalInfo();  
92 - }  
93 -  
94 - @Override  
95 - public String getSearchTextSource() {  
96 - return getSearchText();  
97 - }  
98 -  
99 - @Override  
100 - public void setSearchText(String searchText) {  
101 - this.searchText = searchText;  
102 - }  
103 -  
104 - @Override  
105 - public UUID getId() {  
106 - return id;  
107 - }  
108 -  
109 - @Override  
110 - public void setId(UUID id) {  
111 - this.id = id;  
112 - }  
113 -  
114 - public UUID getTenantId() {  
115 - return tenantId;  
116 - }  
117 -  
118 - public void setTenantId(UUID tenantId) {  
119 - this.tenantId = tenantId;  
120 - }  
121 -  
122 - public String getName() {  
123 - return name;  
124 - }  
125 -  
126 - public void setName(String name) {  
127 - this.name = name;  
128 - }  
129 -  
130 - public UUID getFirstRuleNodeId() {  
131 - return firstRuleNodeId;  
132 - }  
133 -  
134 - public void setFirstRuleNodeId(UUID firstRuleNodeId) {  
135 - this.firstRuleNodeId = firstRuleNodeId;  
136 - }  
137 -  
138 - public boolean isRoot() {  
139 - return root;  
140 - }  
141 -  
142 - public void setRoot(boolean root) {  
143 - this.root = root;  
144 - }  
145 -  
146 - public String getSearchText() {  
147 - return searchText;  
148 - }  
149 -  
150 - public JsonNode getConfiguration() {  
151 - return configuration;  
152 - }  
153 -  
154 - public void setConfiguration(JsonNode configuration) {  
155 - this.configuration = configuration;  
156 - }  
157 -  
158 - public JsonNode getAdditionalInfo() {  
159 - return additionalInfo;  
160 - }  
161 -  
162 - public void setAdditionalInfo(JsonNode additionalInfo) {  
163 - this.additionalInfo = additionalInfo;  
164 - }  
165 -  
166 - @Override  
167 - public RuleChain toData() {  
168 - RuleChain ruleChain = new RuleChain(new RuleChainId(id));  
169 - ruleChain.setCreatedTime(UUIDs.unixTimestamp(id));  
170 - ruleChain.setTenantId(new TenantId(tenantId));  
171 - ruleChain.setName(name);  
172 - if (this.firstRuleNodeId != null) {  
173 - ruleChain.setFirstRuleNodeId(new RuleNodeId(this.firstRuleNodeId));  
174 - }  
175 - ruleChain.setRoot(this.root);  
176 - ruleChain.setDebugMode(this.debugMode);  
177 - ruleChain.setConfiguration(this.configuration);  
178 - ruleChain.setAdditionalInfo(this.additionalInfo);  
179 - return ruleChain;  
180 - }  
181 -  
182 -}