From 2b0ac1f1d031c578b1b0de036dacc0d2588166cb Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Mon, 27 Aug 2018 15:16:46 +0300 Subject: [PATCH 01/41] Create EntityViewId class --- .../server/common/data/id/EntityViewId.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/id/EntityViewId.java diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/id/EntityViewId.java b/common/data/src/main/java/org/thingsboard/server/common/data/id/EntityViewId.java new file mode 100644 index 0000000000..2d6a0fbf28 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/id/EntityViewId.java @@ -0,0 +1,27 @@ +package org.thingsboard.server.common.data.id; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.thingsboard.server.common.data.EntityType; + +import java.util.UUID; + +public class EntityViewId extends UUIDBased implements EntityId { + + + private static final long serialVersionUID = 1L; + + @JsonCreator + public EntityViewId(@JsonProperty("id") UUID id) { + super(id); + } + + public static EntityViewId fromString(String entityViewID) { + return new EntityViewId(UUID.fromString(entityViewID)); + } + + @Override + public EntityType getEntityType() { + return EntityType.ENTITY_VIEW; + } +} From 30e1c480ed6f84c48cc391042d3014ad614e7d5c Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Mon, 27 Aug 2018 16:05:53 +0300 Subject: [PATCH 02/41] Was created EntityView class --- .../server/common/data/EntityType.java | 2 +- .../server/common/data/EntityView.java | 127 ++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/EntityType.java b/common/data/src/main/java/org/thingsboard/server/common/data/EntityType.java index fe9c018718..ef4994adda 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/EntityType.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/EntityType.java @@ -19,5 +19,5 @@ package org.thingsboard.server.common.data; * @author Andrew Shvayka */ public enum EntityType { - TENANT, CUSTOMER, USER, DASHBOARD, ASSET, DEVICE, ALARM, RULE_CHAIN, RULE_NODE; + TENANT, CUSTOMER, USER, DASHBOARD, ASSET, DEVICE, ALARM, RULE_CHAIN, RULE_NODE, ENTITY_VIEW } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java new file mode 100644 index 0000000000..b160b888f5 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java @@ -0,0 +1,127 @@ +package org.thingsboard.server.common.data; + +import lombok.EqualsAndHashCode; +import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.id.EntityId; +import org.thingsboard.server.common.data.id.EntityViewId; +import org.thingsboard.server.common.data.id.TenantId; + +import java.util.List; + +@EqualsAndHashCode(callSuper = true) +public class EntityView extends SearchTextBasedWithAdditionalInfo + implements HasName, HasTenantId, HasCustomerId { + + private static final long serialVersionUID = 5582010124562018986L; + + private EntityId entityId; + private TenantId tenantId; + private CustomerId customerId; + private String name; + private List keys; + private Long tsStart; + private Long tsEnd; + + public EntityView() { + } + + public EntityView(EntityViewId id) { + super(id); + } + + public EntityView(EntityId entityId, + TenantId tenantId, + CustomerId customerId, + String name, + List keys, + Long tsStart, + Long tsEnd) { + + this.entityId = entityId; + this.tenantId = tenantId; + this.customerId = customerId; + this.name = name; + this.keys = keys; + this.tsStart = tsStart; + this.tsEnd = tsEnd; + } + + public EntityView(EntityView entityView) { + super(entityView); + } + + public EntityId getEntityId() { + return entityId; + } + + public void setEntityId(EntityId entityId) { + this.entityId = entityId; + } + + public void setTenantId(TenantId tenantId) { + this.tenantId = tenantId; + } + + public void setCustomerId(CustomerId customerId) { + this.customerId = customerId; + } + + public void setName(String name) { + this.name = name; + } + + public List getKeys() { + return keys; + } + + public void setKeys(List keys) { + this.keys = keys; + } + + public Long getTsStart() { + return tsStart; + } + + public void setTsStart(Long tsStart) { + this.tsStart = tsStart; + } + + public Long getTsEnd() { + return tsEnd; + } + + public void setTsEnd(Long tsEnd) { + this.tsEnd = tsEnd; + } + + @Override + public String getSearchText() { + return getName() /*What the ...*/; + } + + @Override + public CustomerId getCustomerId() { + return customerId; + } + + @Override + public String getName() { + return name; + } + + @Override + public TenantId getTenantId() { + return tenantId; + } + + @Override + public String toString() { + return "EntityView{entityId=" + entityId.getId() + + ", tenantId=" + tenantId + + ", customerId=" + customerId + + ", name='" + name + "\'" + + ", keys=" + String.join(",", keys) + + ", tsStart=" + tsStart + + ", tsEnd=" + tsEnd + "}"; + } +} From a22bef2e182f3ac21ecd410202d77895328f073c Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Mon, 27 Aug 2018 17:01:58 +0300 Subject: [PATCH 03/41] Was created first test to EntityViewController --- .../controller/BaseDeviceControllerTest.java | 4 +- .../BaseEntityViewControllerTest.java | 81 +++++++++++++++++++ .../nosql/EntityViewControllerNoSqlTest.java | 24 ++++++ .../sql/EntityViewControllerSqlTest.java | 26 ++++++ 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java create mode 100644 application/src/test/java/org/thingsboard/server/controller/nosql/EntityViewControllerNoSqlTest.java create mode 100644 application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseDeviceControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseDeviceControllerTest.java index 2b86934185..55e18fbeb5 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseDeviceControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseDeviceControllerTest.java @@ -41,7 +41,9 @@ import org.junit.Before; import org.junit.Test; import com.fasterxml.jackson.core.type.TypeReference; - +/** + * Created by Victor Basanets on 8/27/2017. + */ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { private IdComparator idComparator = new IdComparator<>(); diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java new file mode 100644 index 0000000000..45b068be19 --- /dev/null +++ b/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java @@ -0,0 +1,81 @@ +package org.thingsboard.server.controller; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.thingsboard.server.common.data.Device; +import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.Tenant; +import org.thingsboard.server.common.data.User; +import org.thingsboard.server.common.data.security.Authority; + +import java.util.Arrays; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; + +public abstract class BaseEntityViewControllerTest extends AbstractControllerTest { + + private Tenant savedTenant; + private User tenantAdmin; + private Device testDevice; + + @Before + public void beforeTest() throws Exception { + loginSysAdmin(); + + Device device = new Device(); + device.setName("Test device"); + device.setType("default"); + testDevice = doPost("/api/device", device, Device.class); + + Tenant tenant = new Tenant(); + tenant.setTitle("My tenant"); + savedTenant = doPost("/api/tenant", tenant, Tenant.class); + + Assert.assertNotNull(savedTenant); + + tenantAdmin = new User(); + tenantAdmin.setAuthority(Authority.TENANT_ADMIN); + tenantAdmin.setTenantId(savedTenant.getId()); + tenantAdmin.setEmail("tenant2@thingsboard.org"); + tenantAdmin.setFirstName("Joe"); + tenantAdmin.setLastName("Downs"); + + tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1"); + } + + @After + public void afterTest() throws Exception { + loginSysAdmin(); + + doDelete("/api/tenant/" + savedTenant.getId().getId().toString()) + .andExpect(status().isOk()); + } + + @Test + public void testSaveEntityViewWithIdOfDevice() throws Exception { + EntityView view = new EntityView(); + view.setEntityId(testDevice.getId()); + view.setName("Test entity view"); + view.setKeys(Arrays.asList("key1", "key2", "key3")); + EntityView savedView = doPost("/api/entity-view", view, EntityView.class); + + Assert.assertNotNull(savedView); + Assert.assertNotNull(savedView.getId()); + Assert.assertTrue(savedView.getCreatedTime() > 0); + Assert.assertEquals(savedTenant.getId(), savedView.getTenantId()); + Assert.assertNotNull(savedView.getCustomerId()); + Assert.assertEquals(NULL_UUID, savedView.getCustomerId().getId()); + Assert.assertEquals(savedView.getName(), savedView.getName()); + + savedView.setName("New test entity view"); + doPost("/api/entity-view", savedView, EntityView.class); + + EntityView foundEntityView = doGet("/api/device/" + + savedView.getId().getId().toString(), EntityView.class); + + Assert.assertEquals(foundEntityView.getName(), savedView.getName()); + } +} diff --git a/application/src/test/java/org/thingsboard/server/controller/nosql/EntityViewControllerNoSqlTest.java b/application/src/test/java/org/thingsboard/server/controller/nosql/EntityViewControllerNoSqlTest.java new file mode 100644 index 0000000000..bc461a3119 --- /dev/null +++ b/application/src/test/java/org/thingsboard/server/controller/nosql/EntityViewControllerNoSqlTest.java @@ -0,0 +1,24 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.controller.nosql; + +import org.thingsboard.server.controller.BaseEntityViewControllerTest; +/** + * Created by Victor Basanets on 8/27/2017. + */ +public class EntityViewControllerNoSqlTest + extends BaseEntityViewControllerTest { +} diff --git a/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java b/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java new file mode 100644 index 0000000000..f481ee718e --- /dev/null +++ b/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java @@ -0,0 +1,26 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.controller.sql; + +import org.thingsboard.server.controller.BaseEntityViewControllerTest; +import org.thingsboard.server.dao.service.DaoSqlTest; +/** + * Created by Victor Basanets on 8/27/2017. + */ +@DaoSqlTest +public class EntityViewControllerSqlTest + extends BaseEntityViewControllerTest { +} From 076bddb32a3224350fa9929e8b6abc5b6c174bd8 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Mon, 27 Aug 2018 17:15:01 +0300 Subject: [PATCH 04/41] Was modified thingsboard.yml --- .../src/main/resources/thingsboard.yml | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml index 743a8607b5..3e05ff7e39 100644 --- a/application/src/main/resources/thingsboard.yml +++ b/application/src/main/resources/thingsboard.yml @@ -343,36 +343,36 @@ spring.resources.chain: enabled: "true" # HSQLDB DAO Configuration -spring: - data: - jpa: - repositories: - enabled: "true" - jpa: - hibernate: - ddl-auto: "validate" - database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.HSQLDialect}" - datasource: - driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.hsqldb.jdbc.JDBCDriver}" - url: "${SPRING_DATASOURCE_URL:jdbc:hsqldb:file:${SQL_DATA_FOLDER:/tmp}/thingsboardDb;sql.enforce_size=false;hsqldb.log_size=5}" - username: "${SPRING_DATASOURCE_USERNAME:sa}" - password: "${SPRING_DATASOURCE_PASSWORD:}" +# spring: +# data: +# jpa: +# repositories: +# enabled: "true" +# jpa: +# hibernate: +# ddl-auto: "validate" +# database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.HSQLDialect}" +# datasource: +# driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.hsqldb.jdbc.JDBCDriver}" +# url: "${SPRING_DATASOURCE_URL:jdbc:hsqldb:file:${SQL_DATA_FOLDER:/tmp}/thingsboardDb;sql.enforce_size=false;hsqldb.log_size=5}" +# username: "${SPRING_DATASOURCE_USERNAME:sa}" +# password: "${SPRING_DATASOURCE_PASSWORD:}" # PostgreSQL DAO Configuration -#spring: -# data: -# sql: -# repositories: -# enabled: "true" -# sql: -# hibernate: -# ddl-auto: "validate" -# database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.PostgreSQLDialect}" -# datasource: -# driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.postgresql.Driver}" -# url: "${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/thingsboard}" -# username: "${SPRING_DATASOURCE_USERNAME:postgres}" -# password: "${SPRING_DATASOURCE_PASSWORD:postgres}" +spring: + data: + sql: + repositories: + enabled: "true" + sql: + hibernate: + ddl-auto: "validate" + database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.PostgreSQLDialect}" + datasource: + driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.postgresql.Driver}" + url: "${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/thingsboard}" + username: "${SPRING_DATASOURCE_USERNAME:postgres}" + password: "${SPRING_DATASOURCE_PASSWORD:postgres}" # Audit log parameters audit_log: From 09a13ba82d5a86c318582e12e867d3d7ee052175 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Mon, 27 Aug 2018 17:30:49 +0300 Subject: [PATCH 05/41] Was added descriptive headings titles to source code files --- .../BaseEntityViewControllerTest.java | 18 ++++++++++++++++++ .../nosql/EntityViewControllerNoSqlTest.java | 1 + .../sql/EntityViewControllerSqlTest.java | 1 + .../server/common/data/EntityView.java | 18 ++++++++++++++++++ .../server/common/data/id/EntityViewId.java | 19 ++++++++++++++++++- 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java index 45b068be19..e87ea6b4ea 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java @@ -1,3 +1,18 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.thingsboard.server.controller; import org.junit.After; @@ -15,6 +30,9 @@ import java.util.Arrays; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; +/** + * Created by Victor Basanets on 8/27/2017. + */ public abstract class BaseEntityViewControllerTest extends AbstractControllerTest { private Tenant savedTenant; diff --git a/application/src/test/java/org/thingsboard/server/controller/nosql/EntityViewControllerNoSqlTest.java b/application/src/test/java/org/thingsboard/server/controller/nosql/EntityViewControllerNoSqlTest.java index bc461a3119..095edd19f8 100644 --- a/application/src/test/java/org/thingsboard/server/controller/nosql/EntityViewControllerNoSqlTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/nosql/EntityViewControllerNoSqlTest.java @@ -16,6 +16,7 @@ package org.thingsboard.server.controller.nosql; import org.thingsboard.server.controller.BaseEntityViewControllerTest; + /** * Created by Victor Basanets on 8/27/2017. */ diff --git a/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java b/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java index f481ee718e..84e621fcf6 100644 --- a/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java @@ -17,6 +17,7 @@ package org.thingsboard.server.controller.sql; import org.thingsboard.server.controller.BaseEntityViewControllerTest; import org.thingsboard.server.dao.service.DaoSqlTest; + /** * Created by Victor Basanets on 8/27/2017. */ diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java index b160b888f5..06debfb001 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java @@ -1,3 +1,18 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.thingsboard.server.common.data; import lombok.EqualsAndHashCode; @@ -8,6 +23,9 @@ import org.thingsboard.server.common.data.id.TenantId; import java.util.List; +/** + * Created by Victor Basanets on 8/27/2017. + */ @EqualsAndHashCode(callSuper = true) public class EntityView extends SearchTextBasedWithAdditionalInfo implements HasName, HasTenantId, HasCustomerId { diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/id/EntityViewId.java b/common/data/src/main/java/org/thingsboard/server/common/data/id/EntityViewId.java index 2d6a0fbf28..459dd990a4 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/id/EntityViewId.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/id/EntityViewId.java @@ -1,3 +1,18 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.thingsboard.server.common.data.id; import com.fasterxml.jackson.annotation.JsonCreator; @@ -6,9 +21,11 @@ import org.thingsboard.server.common.data.EntityType; import java.util.UUID; +/** + * Created by Victor Basanets on 8/27/2017. + */ public class EntityViewId extends UUIDBased implements EntityId { - private static final long serialVersionUID = 1L; @JsonCreator From 1120f0a4c5881c9d74530ea02b26a18ed3240ff3 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Mon, 27 Aug 2018 18:39:22 +0300 Subject: [PATCH 06/41] Was added EntityViewSevice interface --- .../dao/entityview/EntityViewService.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java new file mode 100644 index 0000000000..f4e1700bb3 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java @@ -0,0 +1,31 @@ +package org.thingsboard.server.dao.entityview; + +import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.id.EntityViewId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.page.TextPageData; +import org.thingsboard.server.common.data.page.TextPageLink; + +public interface EntityViewService { + EntityView findEntityViewById(EntityViewId entityViewId); + + + EntityView findEntityViewByTenantIdAndName(TenantId tenantId, String name); + + EntityView saveEntityView(EntityView entityView); + + EntityView assignEntityViewToCustomer(EntityViewId entityViewId, CustomerId customerId); + + void deleteEntityView(EntityViewId entityViewId); + + TextPageData findEntityViewByTenantId(TenantId tenantId, TextPageLink pageLink); + + TextPageData findEntityViewByTenantIdAndType(TenantId tenantId, String type, TextPageLink pageLink); + + void deleteEntityViewByTenantId(TenantId tenantId); + + TextPageData findEntityViewByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, TextPageLink pageLink); + + TextPageData findEntityViewByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, String type, TextPageLink pageLink); +} From 7e00615a0d017e77695e5b0385d4dcab0345bba5 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Mon, 27 Aug 2018 19:03:39 +0300 Subject: [PATCH 07/41] Was modified BaseController abstract class: added EntityViewService --- .../server/controller/BaseController.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/application/src/main/java/org/thingsboard/server/controller/BaseController.java b/application/src/main/java/org/thingsboard/server/controller/BaseController.java index de73fe0796..af62e04cff 100644 --- a/application/src/main/java/org/thingsboard/server/controller/BaseController.java +++ b/application/src/main/java/org/thingsboard/server/controller/BaseController.java @@ -56,6 +56,7 @@ import org.thingsboard.server.dao.customer.CustomerService; import org.thingsboard.server.dao.dashboard.DashboardService; import org.thingsboard.server.dao.device.DeviceCredentialsService; import org.thingsboard.server.dao.device.DeviceService; +import org.thingsboard.server.dao.entityview.EntityViewService; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.exception.IncorrectParameterException; import org.thingsboard.server.dao.model.ModelConstants; @@ -139,6 +140,9 @@ public abstract class BaseController { @Autowired protected DeviceStateService deviceStateService; + @Autowired + protected EntityViewService entityViewService; + @ExceptionHandler(ThingsboardException.class) public void handleThingsboardException(ThingsboardException ex, HttpServletResponse response) { errorResponseHandler.handle(ex, response); @@ -313,6 +317,9 @@ public abstract class BaseController { case USER: checkUserId(new UserId(entityId.getId())); return; + case ENTITY_VIEW: + checkEntityView(entityViewService.findEntityViewById(new EntityViewId(entityId.getId()))); + return; default: throw new IllegalArgumentException("Unsupported entity type: " + entityId.getEntityType()); } @@ -340,6 +347,25 @@ public abstract class BaseController { } } + protected EntityView checkEntityViewId(EntityViewId entityViewId) throws ThingsboardException { + try { + validateId(entityViewId, "Incorrect entityViewId " + entityViewId); + EntityView entityView = entityViewService.findEntityViewById(entityViewId); + checkEntityView(entityView); + return entityView; + } catch (Exception e) { + throw handleException(e, false); + } + } + + protected void checkEntityView(EntityView entityView) throws ThingsboardException { + checkNotNull(entityView); + checkTenantId(entityView.getTenantId()); + if (entityView.getCustomerId() != null && !entityView.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) { + checkCustomerId(entityView.getCustomerId()); + } + } + Asset checkAssetId(AssetId assetId) throws ThingsboardException { try { validateId(assetId, "Incorrect assetId " + assetId); From 9bcfec00ba95bd481a88fa92ee5fee9e8e159576 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Tue, 28 Aug 2018 13:57:42 +0300 Subject: [PATCH 08/41] Was created controller EntityViewController --- .../controller/EntityViewController.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 application/src/main/java/org/thingsboard/server/controller/EntityViewController.java diff --git a/application/src/main/java/org/thingsboard/server/controller/EntityViewController.java b/application/src/main/java/org/thingsboard/server/controller/EntityViewController.java new file mode 100644 index 0000000000..acb583b396 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/controller/EntityViewController.java @@ -0,0 +1,78 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.controller; + +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; +import org.thingsboard.server.common.data.EntityType; +import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.audit.ActionType; +import org.thingsboard.server.common.data.exception.ThingsboardException; +import org.thingsboard.server.common.data.id.EntityViewId; + +/** + * Created by Victor Basanets on 8/28/2017. + */ +@RestController +@RequestMapping("/api") +public class EntityViewController extends BaseController { + + public static final String ENTITY_VIEW_ID = "entityViewId"; + + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") + @RequestMapping(value = "/entity-view/{entityViewId}", method = RequestMethod.GET) + @ResponseBody + public EntityView getEntityViewById(@PathVariable(ENTITY_VIEW_ID) String strEntityViewId) + throws ThingsboardException { + + checkParameter(ENTITY_VIEW_ID, strEntityViewId); + try { + EntityViewId entityViewId = new EntityViewId(toUUID(strEntityViewId)); + return checkEntityViewId(entityViewId); + } catch (Exception e) { + throw handleException(e); + } + } + + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") + @RequestMapping(value = "/entity-view", method = RequestMethod.POST) + @ResponseBody + public EntityView saveEntityView(@RequestBody EntityView entityView) + throws ThingsboardException { + + try { + entityView.setTenantId(getCurrentUser().getTenantId()); + EntityView savedEntityView = checkNotNull(entityViewService.saveEntityView(entityView)); + + logEntityAction(savedEntityView.getId(), savedEntityView, null, + entityView.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null); + + return savedEntityView; + + } catch (Exception e) { + + logEntityAction(emptyId(EntityType.ENTITY_VIEW), entityView, null, + entityView.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e); + + throw handleException(e); + } + } +} From 14f658c92a166e9747942f0eb199dd123cf781c4 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 29 Aug 2018 10:48:11 +0300 Subject: [PATCH 09/41] Was created EntityViewService interface --- .../dao/entityview/EntityViewService.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java index f4e1700bb3..9077a29a1c 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java @@ -1,3 +1,18 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.thingsboard.server.dao.entityview; import org.thingsboard.server.common.data.EntityView; @@ -7,9 +22,12 @@ import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.TextPageData; import org.thingsboard.server.common.data.page.TextPageLink; +/** + * Created by Victor Basanets on 8/27/2017. + */ public interface EntityViewService { - EntityView findEntityViewById(EntityViewId entityViewId); + EntityView findEntityViewById(EntityViewId entityViewId); EntityView findEntityViewByTenantIdAndName(TenantId tenantId, String name); From e55a0d3fd6384845fdd81f52e554045293d6f05a Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 29 Aug 2018 10:57:00 +0300 Subject: [PATCH 10/41] Was modified constructor of EntityView class --- .../main/java/org/thingsboard/server/common/data/EntityView.java | 1 + 1 file changed, 1 insertion(+) diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java index 06debfb001..6b4dcd1a0c 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java @@ -41,6 +41,7 @@ public class EntityView extends SearchTextBasedWithAdditionalInfo private Long tsEnd; public EntityView() { + super(); } public EntityView(EntityViewId id) { From f5200b3b15d8b636c8eb1fc53d943c7b4041ac19 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 29 Aug 2018 12:25:42 +0300 Subject: [PATCH 11/41] Was added the constant 'ENTITY_VIEW_CACHE' --- .../java/org/thingsboard/server/common/data/CacheConstants.java | 1 + 1 file changed, 1 insertion(+) diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java b/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java index 21de402721..698a69ef6b 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java @@ -20,4 +20,5 @@ public class CacheConstants { public static final String RELATIONS_CACHE = "relations"; public static final String DEVICE_CACHE = "devices"; public static final String ASSET_CACHE = "assets"; + public static final String ENTITY_VIEW_CACHE = "entityViews"; } From e6580ca1af296a644195f5a7133a0e9c491550b1 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 29 Aug 2018 12:31:37 +0300 Subject: [PATCH 12/41] Was added new abstract methods --- .../server/dao/entityview/EntityViewService.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java index 9077a29a1c..8fcaf30dff 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java @@ -35,6 +35,8 @@ public interface EntityViewService { EntityView assignEntityViewToCustomer(EntityViewId entityViewId, CustomerId customerId); + EntityView unassignEntityViewFromCustomer(EntityViewId entityViewId); + void deleteEntityView(EntityViewId entityViewId); TextPageData findEntityViewByTenantId(TenantId tenantId, TextPageLink pageLink); @@ -43,7 +45,11 @@ public interface EntityViewService { void deleteEntityViewByTenantId(TenantId tenantId); - TextPageData findEntityViewByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, TextPageLink pageLink); + TextPageData findEntityViewsByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, + TextPageLink pageLink); + + TextPageData findEntityViewsByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, + String type, TextPageLink pageLink); - TextPageData findEntityViewByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, String type, TextPageLink pageLink); + void unassignCustomerEntityViews(TenantId tenantId, CustomerId customerId); } From 7c5d595dbe795e910ea1edf87e66dc0f2c39420c Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 29 Aug 2018 12:33:17 +0300 Subject: [PATCH 13/41] Was created implementation of EntityViewService interface --- .../dao/entityview/EntityViewServiceImpl.java | 281 ++++++++++++++++++ 1 file changed, 281 insertions(+) create mode 100644 dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java new file mode 100644 index 0000000000..952b1fd46b --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java @@ -0,0 +1,281 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.entityview; + +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.stereotype.Service; +import org.thingsboard.server.common.data.Customer; +import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.Tenant; +import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.id.EntityViewId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.page.TextPageData; +import org.thingsboard.server.common.data.page.TextPageLink; +import org.thingsboard.server.dao.customer.CustomerDao; +import org.thingsboard.server.dao.entity.AbstractEntityService; +import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.service.DataValidator; +import org.thingsboard.server.dao.service.PaginatedRemover; +import org.thingsboard.server.dao.tenant.TenantDao; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static org.thingsboard.server.common.data.CacheConstants.ENTITY_VIEW_CACHE; +import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; +import static org.thingsboard.server.dao.service.Validator.validateId; +import static org.thingsboard.server.dao.service.Validator.validatePageLink; +import static org.thingsboard.server.dao.service.Validator.validateString; + +/** + * Created by Victor Basanets on 8/28/2017. + */ +@Service +@Slf4j +public class EntityViewServiceImpl extends AbstractEntityService + implements EntityViewService { + + public static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; + public static final String INCORRECT_PAGE_LINK = "Incorrect page link "; + public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId "; + public static final String INCORRECT_ENTITY_VIEW_ID = "Incorrect entityViewId "; + + @Autowired + private EntityViewDao entityViewDao; + + @Autowired + private TenantDao tenantDao; + + @Autowired + private CustomerDao customerDao; + + @Autowired + private CacheManager cacheManager; + + @Override + public EntityView findEntityViewById(EntityViewId entityViewId) { + log.trace("Executing findEntityViewById [{}]", entityViewId); + validateId(entityViewId, INCORRECT_ENTITY_VIEW_ID + entityViewId); + return entityViewDao.findById(entityViewId.getId()); + } + + @Override + public EntityView findEntityViewByTenantIdAndName(TenantId tenantId, String name) { + log.trace("Executing findEntityViewByTenantIdAndName [{}][{}]", tenantId, name); + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + return entityViewDao.findEntityViewByTenantIdAndName(tenantId.getId(), name) + .orElse(null); + } + + @Override + public EntityView saveEntityView(EntityView entityView) { + log.trace("Executing save entity view [{}]", entityView); + entityViewValidator.validate(entityView); + return entityViewDao.save(entityView); + } + + @Override + public EntityView assignEntityViewToCustomer(EntityViewId entityViewId, CustomerId customerId) { + EntityView entityView = findEntityViewById(entityViewId); + entityView.setCustomerId(customerId); + return saveEntityView(entityView); + } + + @Override + public EntityView unassignEntityViewFromCustomer(EntityViewId entityViewId) { + EntityView entityView = findEntityViewById(entityViewId); + entityView.setCustomerId(null); + return saveEntityView(entityView); + } + + @Override + public void deleteEntityView(EntityViewId entityViewId) { + log.trace("Executing deleteEntityView [{}]", entityViewId); + Cache cache = cacheManager.getCache(ENTITY_VIEW_CACHE); + validateId(entityViewId, INCORRECT_ENTITY_VIEW_ID + entityViewId); + deleteEntityRelations(entityViewId); + EntityView entityView = entityViewDao.findById(entityViewId.getId()); + List list = new ArrayList<>(); + list.add(entityView.getTenantId()); + list.add(entityView.getName()); + cache.evict(list); + entityViewDao.removeById(entityViewId.getId()); + } + + @Override + public TextPageData findEntityViewByTenantId(TenantId tenantId, TextPageLink pageLink) { + log.trace("Executing findEntityViewByTenantId, tenantId [{}], pageLink [{}]", tenantId, pageLink); + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + validatePageLink(pageLink, INCORRECT_PAGE_LINK + pageLink); + List entityViews = entityViewDao.findEntityViewByTenantId(tenantId.getId(), pageLink); + return new TextPageData<>(entityViews, pageLink); + } + + @Override + public TextPageData findEntityViewByTenantIdAndType(TenantId tenantId, String type, + TextPageLink pageLink) { + + log.trace("Executing findEntityViewByTenantIdAndType, tenantId [{}], type [{}], pageLink [{}]", + tenantId, type, pageLink); + + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + validateString(type, "Incorrect type " + type); + validatePageLink(pageLink, INCORRECT_PAGE_LINK + pageLink); + List entityViews = entityViewDao.findEntityViewByTenantIdAndType(tenantId.getId(), + type, pageLink); + + return new TextPageData<>(entityViews, pageLink); + } + + @Override + public void deleteEntityViewByTenantId(TenantId tenantId) { + log.trace("Executing deleteEntityViewByTenantId, tenantId [{}]", tenantId); + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + tenantEntityViewRemover.removeEntities(tenantId); + } + + @Override + public TextPageData findEntityViewsByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, + TextPageLink pageLink) { + + log.trace("Executing findEntityViewByTenantIdAndCustomerId, tenantId [{}], customerId [{}], pageLink [{}]", + tenantId, customerId, pageLink); + + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + validateId(customerId, INCORRECT_CUSTOMER_ID + customerId); + validatePageLink(pageLink, INCORRECT_PAGE_LINK + pageLink); + List entityViews = entityViewDao.findEntityViewsByTenantIdAndCustomerId(tenantId.getId(), + customerId.getId(), pageLink); + + return new TextPageData<>(entityViews, pageLink); + } + + @Override + public TextPageData findEntityViewsByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, + String type, TextPageLink pageLink) { + + log.trace("Executing findEntityViewsByTenantIdAndCustomerIdAndType, tenantId [{}], customerId [{}], type [{}]," + + " pageLink [{}]", tenantId, customerId, type, pageLink); + + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + validateId(customerId, INCORRECT_CUSTOMER_ID + customerId); + validateString(type, "Incorrect type " + type); + validatePageLink(pageLink, INCORRECT_PAGE_LINK + pageLink); + List entityViews = entityViewDao.findEntityViewsByTenantIdAndCustomerIdAndType(tenantId.getId(), + customerId.getId(), type, pageLink); + + return new TextPageData<>(entityViews, pageLink); + } + + @Override + public void unassignCustomerEntityViews(TenantId tenantId, CustomerId customerId) { + log.trace("Executing unassignCustomerEntityViews, tenantId [{}], customerId [{}]", tenantId, customerId); + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + validateId(customerId, INCORRECT_CUSTOMER_ID + customerId); + new CustomerEntityViewsUnAssigner(tenantId).removeEntities(customerId); + } + + private DataValidator entityViewValidator = + new DataValidator() { + + @Override + protected void validateCreate(EntityView entityView) { + entityViewDao.findEntityViewByTenantIdAndName(entityView.getTenantId().getId(), entityView.getName()) + .ifPresent( e -> { + throw new DataValidationException("Entity view with such name already exists!"); + }); + } + + @Override + protected void validateUpdate(EntityView entityView) { + entityViewDao.findEntityViewByTenantIdAndName(entityView.getTenantId().getId(), entityView.getName()) + .ifPresent( e -> { + if (!e.getUuidId().equals(entityView.getUuidId())) { + throw new DataValidationException("Entity view with such name already exists!"); + } + }); + } + + @Override + protected void validateDataImpl(EntityView entityView) { + if (StringUtils.isEmpty(String.join("", entityView.getKeys()))) { + throw new DataValidationException("Entity view type should be specified!"); + } + if (StringUtils.isEmpty(entityView.getName())) { + throw new DataValidationException("Entity view name should be specified!"); + } + if (entityView.getTenantId() == null) { + throw new DataValidationException("Entity view should be assigned to tenant!"); + } else { + Tenant tenant = tenantDao.findById(entityView.getTenantId().getId()); + if (tenant == null) { + throw new DataValidationException("Entity view is referencing to non-existent tenant!"); + } + } + if (entityView.getCustomerId() == null) { + entityView.setCustomerId(new CustomerId(NULL_UUID)); + } else if (!entityView.getCustomerId().getId().equals(NULL_UUID)) { + Customer customer = customerDao.findById(entityView.getCustomerId().getId()); + if (customer == null) { + throw new DataValidationException("Can't assign entity view to non-existent customer!"); + } + if (!customer.getTenantId().getId().equals(entityView.getTenantId().getId())) { + throw new DataValidationException("Can't assign entity view to customer from different tenant!"); + } + } + } + }; + + private PaginatedRemover tenantEntityViewRemover = + new PaginatedRemover() { + + @Override + protected List findEntities(TenantId id, TextPageLink pageLink) { + return entityViewDao.findEntityViewByTenantId(id.getId(), pageLink); + } + + @Override + protected void removeEntity(EntityView entity) { + deleteEntityView(new EntityViewId(entity.getUuidId())); + } + }; + + private class CustomerEntityViewsUnAssigner extends PaginatedRemover { + + private TenantId tenantId; + + CustomerEntityViewsUnAssigner(TenantId tenantId) { + this.tenantId = tenantId; + } + + @Override + protected List findEntities(CustomerId id, TextPageLink pageLink) { + return entityViewDao.findEntityViewsByTenantIdAndCustomerId(tenantId.getId(), id.getId(), pageLink); + } + + @Override + protected void removeEntity(EntityView entity) { + unassignEntityViewFromCustomer(new EntityViewId(entity.getUuidId())); + } + + } +} From 17866a979868d1252052916ed1c934fcc4735ee6 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 29 Aug 2018 12:34:00 +0300 Subject: [PATCH 14/41] Was created dao interface --- .../server/dao/entityview/EntityViewDao.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java new file mode 100644 index 0000000000..fbd1957db8 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java @@ -0,0 +1,81 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.entityview; + +import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.page.TextPageLink; +import org.thingsboard.server.dao.Dao; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +/** + * Created by Victor Basanets on 8/28/2017. + */ +public interface EntityViewDao extends Dao { + + /** + * Find entity views by tenantId and page link. + * + * @param tenantId the tenantId + * @param pageLink the page link + * @return the list of entity view objects + */ + List findEntityViewByTenantId(UUID tenantId, TextPageLink pageLink); + + /** + * Find entity views by tenantId and entity view name. + * + * @param tenantId the tenantId + * @param name the entity view name + * @return the optional entity view object + */ + Optional findEntityViewByTenantIdAndName(UUID tenantId, String name); + + /** + * Find entity views by tenantId, type and page link. + * + * @param tenantId the tenantId + * @param type the type + * @param pageLink the page link + * @return the list of entity view objects + */ + List findEntityViewByTenantIdAndType(UUID tenantId, String type, TextPageLink pageLink); + + /** + * Find entity views by tenantId, customerId and page link. + * + * @param tenantId the tenantId + * @param customerId the customerId + * @param pageLink the page link + * @return the list of entity view objects + */ + List findEntityViewsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TextPageLink pageLink); + + /** + * Find entity views by tenantId, customerId, type and page link. + * + * @param tenantId the tenantId + * @param customerId the customerId + * @param type the type + * @param pageLink the page link + * @return the list of entity view objects + */ + List findEntityViewsByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, + TextPageLink pageLink); + +} From f880fc2e429113a080eeab1bc54dea609f102739 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 29 Aug 2018 13:26:00 +0300 Subject: [PATCH 15/41] Was added method for delete of entity view --- .../controller/EntityViewController.java | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/controller/EntityViewController.java b/application/src/main/java/org/thingsboard/server/controller/EntityViewController.java index acb583b396..fd3ccf2fe1 100644 --- a/application/src/main/java/org/thingsboard/server/controller/EntityViewController.java +++ b/application/src/main/java/org/thingsboard/server/controller/EntityViewController.java @@ -15,18 +15,21 @@ */ package org.thingsboard.server.controller; +import com.google.common.util.concurrent.ListenableFuture; +import org.springframework.http.HttpStatus; import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityView; import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.exception.ThingsboardException; +import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.EntityViewId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.service.security.model.SecurityUser; + +import java.util.ArrayList; +import java.util.List; /** * Created by Victor Basanets on 8/28/2017. @@ -55,24 +58,41 @@ public class EntityViewController extends BaseController { @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") @RequestMapping(value = "/entity-view", method = RequestMethod.POST) @ResponseBody - public EntityView saveEntityView(@RequestBody EntityView entityView) - throws ThingsboardException { - + public EntityView saveEntityView(@RequestBody EntityView entityView) throws ThingsboardException { try { entityView.setTenantId(getCurrentUser().getTenantId()); EntityView savedEntityView = checkNotNull(entityViewService.saveEntityView(entityView)); - logEntityAction(savedEntityView.getId(), savedEntityView, null, entityView.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null); return savedEntityView; } catch (Exception e) { - logEntityAction(emptyId(EntityType.ENTITY_VIEW), entityView, null, entityView.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e); throw handleException(e); } } + + @PreAuthorize("hasAuthority('TENANT_ADMIN')") + @RequestMapping(value = "/entity-view/{entityViewId}", method = RequestMethod.DELETE) + @ResponseStatus(value = HttpStatus.OK) + public void deleteEntityView(@PathVariable(ENTITY_VIEW_ID) String strEntityViewId) throws ThingsboardException { + checkParameter(ENTITY_VIEW_ID, strEntityViewId); + try { + EntityViewId entityViewId = new EntityViewId(toUUID(strEntityViewId)); + EntityView entityView = checkEntityViewId(entityViewId); + entityViewService.deleteEntityView(entityViewId); + + logEntityAction(entityViewId, entityView, entityView.getCustomerId(), + ActionType.DELETED,null, strEntityViewId); + } catch (Exception e) { + logEntityAction(emptyId(EntityType.ENTITY_VIEW), + null, + null, + ActionType.DELETED, e, strEntityViewId); + throw handleException(e); + } + } } From c8733b695c999102acd707bf46e98f2a97b515b5 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 29 Aug 2018 13:48:26 +0300 Subject: [PATCH 16/41] Was added tests of find entity by id and delete entity methods --- .../BaseEntityViewControllerTest.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java index e87ea6b4ea..d0598586d7 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java @@ -72,11 +72,24 @@ public abstract class BaseEntityViewControllerTest extends AbstractControllerTes .andExpect(status().isOk()); } + @Test + public void testFindEntityViewById() throws Exception { + EntityView view = new EntityView(); + view.setName("Test entity view"); + view.setEntityId(testDevice.getId()); + view.setKeys(Arrays.asList("key1", "key2", "key3")); + EntityView savedView = doPost("/api/entity-view", view, EntityView.class); + EntityView foundView = doGet("/api/entity-view/" + savedView.getId().getId().toString(), EntityView.class); + Assert.assertNotNull(foundView); + Assert.assertEquals(savedView, foundView); + } + @Test public void testSaveEntityViewWithIdOfDevice() throws Exception { EntityView view = new EntityView(); view.setEntityId(testDevice.getId()); view.setName("Test entity view"); + view.setTenantId(savedTenant.getId()); view.setKeys(Arrays.asList("key1", "key2", "key3")); EntityView savedView = doPost("/api/entity-view", view, EntityView.class); @@ -91,9 +104,24 @@ public abstract class BaseEntityViewControllerTest extends AbstractControllerTes savedView.setName("New test entity view"); doPost("/api/entity-view", savedView, EntityView.class); - EntityView foundEntityView = doGet("/api/device/" + EntityView foundEntityView = doGet("/api/entity-view/" + savedView.getId().getId().toString(), EntityView.class); Assert.assertEquals(foundEntityView.getName(), savedView.getName()); } + + @Test + public void testDeleteEntityView() throws Exception { + EntityView view = new EntityView(); + view.setName("Test entity view"); + view.setEntityId(testDevice.getId()); + view.setKeys(Arrays.asList("key1", "key2", "key3")); + EntityView savedView = doPost("/api/entity-view", view, EntityView.class); + + doDelete("/api/entity-view/" + savedView.getId().getId().toString()) + .andExpect(status().isOk()); + + doGet("/api/entity-view/" + savedView.getId().getId().toString()) + .andExpect(status().isNotFound()); + } } From 6e5bda589f05b5bbe59203423df7bbbe25602510 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 29 Aug 2018 15:28:48 +0300 Subject: [PATCH 17/41] Was returned to the original state --- .../src/main/resources/thingsboard.yml | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml index 3e05ff7e39..be5149dcb8 100644 --- a/application/src/main/resources/thingsboard.yml +++ b/application/src/main/resources/thingsboard.yml @@ -343,36 +343,36 @@ spring.resources.chain: enabled: "true" # HSQLDB DAO Configuration -# spring: -# data: -# jpa: -# repositories: -# enabled: "true" -# jpa: -# hibernate: -# ddl-auto: "validate" -# database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.HSQLDialect}" -# datasource: -# driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.hsqldb.jdbc.JDBCDriver}" -# url: "${SPRING_DATASOURCE_URL:jdbc:hsqldb:file:${SQL_DATA_FOLDER:/tmp}/thingsboardDb;sql.enforce_size=false;hsqldb.log_size=5}" -# username: "${SPRING_DATASOURCE_USERNAME:sa}" -# password: "${SPRING_DATASOURCE_PASSWORD:}" +spring: + data: + jpa: + repositories: + enabled: "true" + jpa: + hibernate: + ddl-auto: "validate" + database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.HSQLDialect}" + datasource: + driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.hsqldb.jdbc.JDBCDriver}" + url: "${SPRING_DATASOURCE_URL:jdbc:hsqldb:file:${SQL_DATA_FOLDER:/tmp}/thingsboardDb;sql.enforce_size=false;hsqldb.log_size=5}" + username: "${SPRING_DATASOURCE_USERNAME:sa}" + password: "${SPRING_DATASOURCE_PASSWORD:}" # PostgreSQL DAO Configuration -spring: - data: - sql: - repositories: - enabled: "true" - sql: - hibernate: - ddl-auto: "validate" - database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.PostgreSQLDialect}" - datasource: - driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.postgresql.Driver}" - url: "${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/thingsboard}" - username: "${SPRING_DATASOURCE_USERNAME:postgres}" - password: "${SPRING_DATASOURCE_PASSWORD:postgres}" +# spring: +# data: +# sql: +# repositories: +# enabled: "true" +# sql: +# hibernate: +# ddl-auto: "validate" +# database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.PostgreSQLDialect}" +# datasource: +# driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.postgresql.Driver}" +# url: "${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/thingsboard}" +# username: "${SPRING_DATASOURCE_USERNAME:postgres}" +# password: "${SPRING_DATASOURCE_PASSWORD:postgres}" # Audit log parameters audit_log: From d4cc05deba317714d8481afdd1a789414199d215 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 29 Aug 2018 15:35:10 +0300 Subject: [PATCH 18/41] Was corrected header --- .../server/controller/BaseDeviceControllerTest.java | 4 +--- .../server/controller/BaseEntityViewControllerTest.java | 3 --- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseDeviceControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseDeviceControllerTest.java index 55e18fbeb5..2b86934185 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseDeviceControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseDeviceControllerTest.java @@ -41,9 +41,7 @@ import org.junit.Before; import org.junit.Test; import com.fasterxml.jackson.core.type.TypeReference; -/** - * Created by Victor Basanets on 8/27/2017. - */ + public abstract class BaseDeviceControllerTest extends AbstractControllerTest { private IdComparator idComparator = new IdComparator<>(); diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java index d0598586d7..7d9bae1901 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java @@ -30,9 +30,6 @@ import java.util.Arrays; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; -/** - * Created by Victor Basanets on 8/27/2017. - */ public abstract class BaseEntityViewControllerTest extends AbstractControllerTest { private Tenant savedTenant; From a11592641c0db367230e451305171930175478dc Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 29 Aug 2018 15:38:02 +0300 Subject: [PATCH 19/41] Were replace constructor, gets & sets by annotations --- .../server/common/data/EntityView.java | 77 ++----------------- 1 file changed, 5 insertions(+), 72 deletions(-) diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java index 6b4dcd1a0c..b12e38e6b8 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java @@ -15,17 +15,22 @@ */ package org.thingsboard.server.common.data; +import lombok.AllArgsConstructor; +import lombok.Data; import lombok.EqualsAndHashCode; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityViewId; import org.thingsboard.server.common.data.id.TenantId; +import java.beans.ConstructorProperties; import java.util.List; /** * Created by Victor Basanets on 8/27/2017. */ +@Data +@AllArgsConstructor @EqualsAndHashCode(callSuper = true) public class EntityView extends SearchTextBasedWithAdditionalInfo implements HasName, HasTenantId, HasCustomerId { @@ -48,71 +53,10 @@ public class EntityView extends SearchTextBasedWithAdditionalInfo super(id); } - public EntityView(EntityId entityId, - TenantId tenantId, - CustomerId customerId, - String name, - List keys, - Long tsStart, - Long tsEnd) { - - this.entityId = entityId; - this.tenantId = tenantId; - this.customerId = customerId; - this.name = name; - this.keys = keys; - this.tsStart = tsStart; - this.tsEnd = tsEnd; - } - public EntityView(EntityView entityView) { super(entityView); } - public EntityId getEntityId() { - return entityId; - } - - public void setEntityId(EntityId entityId) { - this.entityId = entityId; - } - - public void setTenantId(TenantId tenantId) { - this.tenantId = tenantId; - } - - public void setCustomerId(CustomerId customerId) { - this.customerId = customerId; - } - - public void setName(String name) { - this.name = name; - } - - public List getKeys() { - return keys; - } - - public void setKeys(List keys) { - this.keys = keys; - } - - public Long getTsStart() { - return tsStart; - } - - public void setTsStart(Long tsStart) { - this.tsStart = tsStart; - } - - public Long getTsEnd() { - return tsEnd; - } - - public void setTsEnd(Long tsEnd) { - this.tsEnd = tsEnd; - } - @Override public String getSearchText() { return getName() /*What the ...*/; @@ -132,15 +76,4 @@ public class EntityView extends SearchTextBasedWithAdditionalInfo public TenantId getTenantId() { return tenantId; } - - @Override - public String toString() { - return "EntityView{entityId=" + entityId.getId() + - ", tenantId=" + tenantId + - ", customerId=" + customerId + - ", name='" + name + "\'" + - ", keys=" + String.join(",", keys) + - ", tsStart=" + tsStart + - ", tsEnd=" + tsEnd + "}"; - } } From a1aa321831fe1ae9a6fa4470602e0e6027902298 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 29 Aug 2018 15:39:13 +0300 Subject: [PATCH 20/41] Was deleted Cache --- .../server/dao/entityview/EntityViewServiceImpl.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java index 952b1fd46b..d7e9e9a192 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java @@ -38,9 +38,7 @@ import org.thingsboard.server.dao.tenant.TenantDao; import java.util.ArrayList; import java.util.List; -import java.util.Optional; -import static org.thingsboard.server.common.data.CacheConstants.ENTITY_VIEW_CACHE; import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; import static org.thingsboard.server.dao.service.Validator.validateId; import static org.thingsboard.server.dao.service.Validator.validatePageLink; @@ -68,9 +66,6 @@ public class EntityViewServiceImpl extends AbstractEntityService @Autowired private CustomerDao customerDao; - @Autowired - private CacheManager cacheManager; - @Override public EntityView findEntityViewById(EntityViewId entityViewId) { log.trace("Executing findEntityViewById [{}]", entityViewId); @@ -110,14 +105,12 @@ public class EntityViewServiceImpl extends AbstractEntityService @Override public void deleteEntityView(EntityViewId entityViewId) { log.trace("Executing deleteEntityView [{}]", entityViewId); - Cache cache = cacheManager.getCache(ENTITY_VIEW_CACHE); validateId(entityViewId, INCORRECT_ENTITY_VIEW_ID + entityViewId); deleteEntityRelations(entityViewId); EntityView entityView = entityViewDao.findById(entityViewId.getId()); List list = new ArrayList<>(); list.add(entityView.getTenantId()); list.add(entityView.getName()); - cache.evict(list); entityViewDao.removeById(entityViewId.getId()); } From 20e63b94889c27c534e2d717b3fd59ecf0d374a3 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Thu, 30 Aug 2018 19:04:35 +0300 Subject: [PATCH 21/41] Was added the model EntityViewEntity constants --- .../server/dao/model/ModelConstants.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java b/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java index 3a934eb5d2..90ccb66ca0 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java @@ -45,6 +45,7 @@ public class ModelConstants { public static final String SEARCH_TEXT_PROPERTY = "search_text"; public static final String ADDITIONAL_INFO_PROPERTY = "additional_info"; public static final String ENTITY_TYPE_PROPERTY = "entity_type"; + /*public static final String ENTITY_VIEW_ID_PROPERTY = "entity_view_id";*/ public static final String ENTITY_TYPE_COLUMN = ENTITY_TYPE_PROPERTY; public static final String ENTITY_ID_COLUMN = "entity_id"; @@ -143,6 +144,19 @@ public class ModelConstants { public static final String DEVICE_BY_TENANT_AND_NAME_VIEW_NAME = "device_by_tenant_and_name"; public static final String DEVICE_TYPES_BY_TENANT_VIEW_NAME = "device_types_by_tenant"; + /** + * Cassandra entityView constants. + */ + public static final String ENTITY_VIEW_TABLE_FAMILY_NAME = "entity_view"; + public static final String ENTITY_VIEW_ENTITY_ID_PROPERTY = ENTITY_ID_COLUMN; + public static final String ENTITY_VIEW_TENANT_ID_PROPERTY = TENANT_ID_PROPERTY; + public static final String ENTITY_VIEW_CUSTOMER_ID_PROPERTY = CUSTOMER_ID_PROPERTY; + public static final String ENTITY_VIEW_NAME_PROPERTY = DEVICE_NAME_PROPERTY; + public static final String ENTITY_VIEW_KEYS_PROPERTY = "keys"; + public static final String ENTITY_VIEW_TS_BEGIN_PROPERTY = "ts_begin"; + public static final String ENTITY_VIEW_TS_END_PROPERTY = "ts_end"; + public static final String ENTITY_VIEW_ADDITIONAL_INFO_PROPERTY = ADDITIONAL_INFO_PROPERTY; + /** * Cassandra audit log constants. */ From ab2a5cceba8969f73a216818045a2374556483cb Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Thu, 30 Aug 2018 19:07:03 +0300 Subject: [PATCH 22/41] Was created new model --- .../dao/model/sql/EntityViewEntity.java | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java new file mode 100644 index 0000000000..1444e9745e --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java @@ -0,0 +1,138 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.model.sql; + +import com.datastax.driver.core.utils.UUIDs; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.id.DeviceId; +import org.thingsboard.server.common.data.id.EntityViewId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.model.BaseSqlEntity; +import org.thingsboard.server.dao.model.ModelConstants; +import org.thingsboard.server.dao.model.SearchTextEntity; +import org.thingsboard.server.dao.util.mapping.JsonStringType; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; + +@Data +@EqualsAndHashCode(callSuper = true) +@Entity +@TypeDef(name = "json", typeClass = JsonStringType.class) +@Table(name = ModelConstants.ENTITY_VIEW_TABLE_FAMILY_NAME) +public class EntityViewEntity extends BaseSqlEntity implements SearchTextEntity { + + @Column(name = ModelConstants.ENTITY_VIEW_ENTITY_ID_PROPERTY) + private String entityId; + + @Column(name = ModelConstants.ENTITY_VIEW_TENANT_ID_PROPERTY) + private String tenantId; + + @Column(name = ModelConstants.ENTITY_VIEW_CUSTOMER_ID_PROPERTY) + private String customerId; + + @Column(name = ModelConstants.ENTITY_VIEW_NAME_PROPERTY) + private String name; + + @Type(type = "json") + @Column(name = ModelConstants.ENTITY_VIEW_KEYS_PROPERTY) + private JsonNode keys; + + @Column(name = ModelConstants.ENTITY_VIEW_TS_BEGIN_PROPERTY) + private String tsBegin; + + @Column(name = ModelConstants.ENTITY_VIEW_TS_END_PROPERTY) + private String tsEnd; + + @Column(name = ModelConstants.SEARCH_TEXT_PROPERTY) + private String searchText; + + @Type(type = "json") + @Column(name = ModelConstants.ENTITY_VIEW_ADDITIONAL_INFO_PROPERTY) + private JsonNode additionalInfo; + + public EntityViewEntity() { + super(); + } + + public EntityViewEntity(EntityView entityView) { + if (entityView.getId() != null) { + this.setId(entityView.getId().getId()); + } + if (entityView.getEntityId() != null) { + this.entityId = toString(entityView.getEntityId().getId()); + } + if (entityView.getTenantId() != null) { + this.tenantId = toString(entityView.getTenantId().getId()); + } + if (entityView.getCustomerId() != null) { + this.customerId = toString(entityView.getCustomerId().getId()); + } + this.name = entityView.getName(); + try { + this.keys = new ObjectMapper().readTree("{\"" + entityView.getName() + "\" : [" + + entityView.getKeys().stream() + .map(k -> "\"" + k + "\"") + .collect(Collectors.joining(", ")) + "]}"); + } catch (IOException e) { + e.printStackTrace(); + } + this.tsBegin = String.valueOf(entityView.getTsBegin()); + this.tsEnd = String.valueOf(entityView.getTsEnd()); + this.additionalInfo = entityView.getAdditionalInfo(); + } + + @Override + public String getSearchTextSource() { + return name; + } + + @Override + public void setSearchText(String searchText) { + this.searchText = searchText; + } + + @Override + public EntityView toData() { + EntityView entityView = new EntityView(new EntityViewId(getId())); + entityView.setCreatedTime(UUIDs.unixTimestamp(getId())); + + /*Ned to refactor and replace DeviceId to Class<> instance*/ + entityView.setEntityId(entityId != null ? new DeviceId(toUUID(entityId)) : null); + entityView.setTenantId(tenantId != null ? new TenantId(toUUID(tenantId)) : null); + entityView.setCustomerId(customerId != null ? new CustomerId(toUUID(customerId)) : null); + entityView.setName(name); + try { + entityView.setKeys(new ObjectMapper().readValue(keys.toString(), new TypeReference>(){})); + } catch (IOException e) { + e.printStackTrace(); + } + entityView.setAdditionalInfo(additionalInfo); + return entityView; + } +} From 14fa80a26846de3e68c180c08400eb1cd01a35a1 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Fri, 31 Aug 2018 12:37:22 +0300 Subject: [PATCH 23/41] Was refactored --- .../org/thingsboard/server/common/data/EntityView.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java index b12e38e6b8..102f6eebec 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java @@ -15,9 +15,7 @@ */ package org.thingsboard.server.common.data; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.EqualsAndHashCode; +import lombok.*; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityViewId; @@ -29,6 +27,7 @@ import java.util.List; /** * Created by Victor Basanets on 8/27/2017. */ + @Data @AllArgsConstructor @EqualsAndHashCode(callSuper = true) @@ -42,7 +41,7 @@ public class EntityView extends SearchTextBasedWithAdditionalInfo private CustomerId customerId; private String name; private List keys; - private Long tsStart; + private Long tsBegin; private Long tsEnd; public EntityView() { From 76efea555c2a558a588bb818a26663abd978a7c7 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Fri, 31 Aug 2018 12:39:13 +0300 Subject: [PATCH 24/41] Was created sql & nosql entities --- .../dao/model/nosql/EntityViewEntity.java | 151 ++++++++++++++++++ .../dao/model/sql/EntityViewEntity.java | 20 ++- 2 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java new file mode 100644 index 0000000000..c3227e9605 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java @@ -0,0 +1,151 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.model.nosql; + +import com.datastax.driver.core.utils.UUIDs; +import com.datastax.driver.mapping.annotations.PartitionKey; +import com.datastax.driver.mapping.annotations.Table; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.hibernate.annotations.Type; +import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.id.DeviceId; +import org.thingsboard.server.common.data.id.EntityViewId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.model.ModelConstants; +import org.thingsboard.server.dao.model.SearchTextEntity; + +import javax.persistence.Column; + +import java.io.IOException; +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_TABLE_FAMILY_NAME; +import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY; + +/** + * Created by Victor Basanets on 8/31/2017. + */ +@Data +@Table(name = ENTITY_VIEW_TABLE_FAMILY_NAME) +@EqualsAndHashCode +@ToString +public class EntityViewEntity implements SearchTextEntity { + + @PartitionKey(value = 0) + @Column(name = ID_PROPERTY) + private UUID id; + + @PartitionKey(value = 1) + @Column(name = ModelConstants.ENTITY_VIEW_ENTITY_ID_PROPERTY) + private UUID entityId; + + @PartitionKey(value = 2) + @Column(name = ModelConstants.ENTITY_VIEW_TENANT_ID_PROPERTY) + private UUID tenantId; + + @PartitionKey(value = 3) + @Column(name = ModelConstants.ENTITY_VIEW_CUSTOMER_ID_PROPERTY) + private UUID customerId; + + @Column(name = ModelConstants.ENTITY_VIEW_NAME_PROPERTY) + private String name; + + @Type(type = "json") + @Column(name = ModelConstants.ENTITY_VIEW_KEYS_PROPERTY) + private JsonNode keys; + + @Column(name = ModelConstants.ENTITY_VIEW_TS_BEGIN_PROPERTY) + private String tsBegin; + + @Column(name = ModelConstants.ENTITY_VIEW_TS_END_PROPERTY) + private String tsEnd; + + @Column(name = ModelConstants.SEARCH_TEXT_PROPERTY) + private String searchText; + + @Type(type = "json") + @Column(name = ModelConstants.ENTITY_VIEW_ADDITIONAL_INFO_PROPERTY) + private JsonNode additionalInfo; + + public EntityViewEntity() { + super(); + } + + public EntityViewEntity(EntityView entityView) { + if (entityView.getId() != null) { + this.id = entityView.getId().getId(); + } + if (entityView.getEntityId() != null) { + this.entityId = entityView.getEntityId().getId(); + } + if (entityView.getTenantId() != null) { + this.tenantId = entityView.getTenantId().getId(); + } + if (entityView.getCustomerId() != null) { + this.customerId = entityView.getCustomerId().getId(); + } + this.name = entityView.getName(); + try { + this.keys = new ObjectMapper().readTree("{\"" + entityView.getName() + "\" : [" + + entityView.getKeys().stream() + .map(k -> "\"" + k + "\"") + .collect(Collectors.joining(", ")) + "]}"); + } catch (IOException e) { + e.printStackTrace(); + } + this.tsBegin = String.valueOf(entityView.getTsBegin()); + this.tsEnd = String.valueOf(entityView.getTsEnd()); + this.additionalInfo = entityView.getAdditionalInfo(); + } + + @Override + public String getSearchTextSource() { + return name; + } + + @Override + public EntityView toData() { + EntityView entityView = new EntityView(new EntityViewId(id)); + entityView.setCreatedTime(UUIDs.unixTimestamp(id)); + if (entityId != null) { + entityView.setEntityId(new DeviceId(entityId)); + } + if (tenantId != null) { + entityView.setTenantId(new TenantId(tenantId)); + } + if (customerId != null) { + entityView.setCustomerId(new CustomerId(customerId)); + } + entityView.setName(name); + try { + entityView.setKeys(new ObjectMapper().readValue(keys.toString(), new TypeReference>(){})); + } catch (IOException e) { + e.printStackTrace(); + } + entityView.setTsBegin(Long.parseLong(tsBegin)); + entityView.setTsEnd(Long.parseLong(tsEnd)); + entityView.setAdditionalInfo(additionalInfo); + return entityView; + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java index 1444e9745e..dba075ce01 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java @@ -23,6 +23,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; +import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityView; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.DeviceId; @@ -38,8 +39,13 @@ import javax.persistence.Entity; import javax.persistence.Table; import java.io.IOException; import java.util.List; +import java.util.UUID; import java.util.stream.Collectors; +/** + * Created by Victor Basanets on 8/30/2017. + */ + @Data @EqualsAndHashCode(callSuper = true) @Entity @@ -123,15 +129,23 @@ public class EntityViewEntity extends BaseSqlEntity implements Searc entityView.setCreatedTime(UUIDs.unixTimestamp(getId())); /*Ned to refactor and replace DeviceId to Class<> instance*/ - entityView.setEntityId(entityId != null ? new DeviceId(toUUID(entityId)) : null); - entityView.setTenantId(tenantId != null ? new TenantId(toUUID(tenantId)) : null); - entityView.setCustomerId(customerId != null ? new CustomerId(toUUID(customerId)) : null); + if (entityId != null) { + entityView.setEntityId(new DeviceId(toUUID(entityId))); + } + if (tenantId != null) { + entityView.setTenantId(new TenantId(toUUID(tenantId))); + } + if (customerId != null) { + entityView.setCustomerId(new CustomerId(toUUID(customerId))); + } entityView.setName(name); try { entityView.setKeys(new ObjectMapper().readValue(keys.toString(), new TypeReference>(){})); } catch (IOException e) { e.printStackTrace(); } + entityView.setTsBegin(Long.parseLong(tsBegin)); + entityView.setTsEnd(Long.parseLong(tsEnd)); entityView.setAdditionalInfo(additionalInfo); return entityView; } From bc1e228dc117477c5840773858728c2fe3045131 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Fri, 31 Aug 2018 14:20:50 +0300 Subject: [PATCH 25/41] Was modified, method find(.*)Type renamed to find(.*)EntityId --- .../server/dao/entityview/EntityViewDao.java | 23 ++++++++----- .../dao/entityview/EntityViewService.java | 11 +++++-- .../dao/entityview/EntityViewServiceImpl.java | 33 ++++++++++--------- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java index fbd1957db8..781cb3a68d 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java @@ -16,6 +16,7 @@ package org.thingsboard.server.dao.entityview; import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.page.TextPageLink; import org.thingsboard.server.dao.Dao; @@ -47,14 +48,16 @@ public interface EntityViewDao extends Dao { Optional findEntityViewByTenantIdAndName(UUID tenantId, String name); /** - * Find entity views by tenantId, type and page link. + * Find entity views by tenantId, entityId and page link. * * @param tenantId the tenantId - * @param type the type + * @param entityId the entityId * @param pageLink the page link * @return the list of entity view objects */ - List findEntityViewByTenantIdAndType(UUID tenantId, String type, TextPageLink pageLink); + List findEntityViewByTenantIdAndEntityId(UUID tenantId, + EntityId entityId, + TextPageLink pageLink); /** * Find entity views by tenantId, customerId and page link. @@ -64,18 +67,22 @@ public interface EntityViewDao extends Dao { * @param pageLink the page link * @return the list of entity view objects */ - List findEntityViewsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TextPageLink pageLink); + List findEntityViewsByTenantIdAndCustomerId(UUID tenantId, + UUID customerId, + TextPageLink pageLink); /** - * Find entity views by tenantId, customerId, type and page link. + * Find entity views by tenantId, customerId, entityId and page link. * * @param tenantId the tenantId * @param customerId the customerId - * @param type the type + * @param entityId the entityId * @param pageLink the page link * @return the list of entity view objects */ - List findEntityViewsByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, - TextPageLink pageLink); + List findEntityViewsByTenantIdAndCustomerIdAndEntityId(UUID tenantId, + UUID customerId, + EntityId entityId, + TextPageLink pageLink); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java index 8fcaf30dff..943d35e9b4 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewService.java @@ -15,8 +15,10 @@ */ package org.thingsboard.server.dao.entityview; +import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityView; import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityViewId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.TextPageData; @@ -41,15 +43,18 @@ public interface EntityViewService { TextPageData findEntityViewByTenantId(TenantId tenantId, TextPageLink pageLink); - TextPageData findEntityViewByTenantIdAndType(TenantId tenantId, String type, TextPageLink pageLink); + TextPageData findEntityViewByTenantIdAndEntityId(TenantId tenantId, EntityId entityId, + TextPageLink pageLink); void deleteEntityViewByTenantId(TenantId tenantId); TextPageData findEntityViewsByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, TextPageLink pageLink); - TextPageData findEntityViewsByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, - String type, TextPageLink pageLink); + TextPageData findEntityViewsByTenantIdAndCustomerIdAndEntityId(TenantId tenantId, + CustomerId customerId, + EntityId entityId, + TextPageLink pageLink); void unassignCustomerEntityViews(TenantId tenantId, CustomerId customerId); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java index d7e9e9a192..e44cea552f 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java @@ -25,6 +25,7 @@ import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.EntityView; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityViewId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.TextPageData; @@ -124,17 +125,17 @@ public class EntityViewServiceImpl extends AbstractEntityService } @Override - public TextPageData findEntityViewByTenantIdAndType(TenantId tenantId, String type, + public TextPageData findEntityViewByTenantIdAndEntityId(TenantId tenantId, EntityId entityId, TextPageLink pageLink) { - log.trace("Executing findEntityViewByTenantIdAndType, tenantId [{}], type [{}], pageLink [{}]", - tenantId, type, pageLink); + log.trace("Executing findEntityViewByTenantIdAndType, tenantId [{}], entityId [{}], pageLink [{}]", + tenantId, entityId, pageLink); validateId(tenantId, INCORRECT_TENANT_ID + tenantId); - validateString(type, "Incorrect type " + type); + validateString(entityId.toString(), "Incorrect entityId " + entityId.toString()); validatePageLink(pageLink, INCORRECT_PAGE_LINK + pageLink); - List entityViews = entityViewDao.findEntityViewByTenantIdAndType(tenantId.getId(), - type, pageLink); + List entityViews = entityViewDao.findEntityViewByTenantIdAndEntityId(tenantId.getId(), + entityId, pageLink); return new TextPageData<>(entityViews, pageLink); } @@ -150,8 +151,8 @@ public class EntityViewServiceImpl extends AbstractEntityService public TextPageData findEntityViewsByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, TextPageLink pageLink) { - log.trace("Executing findEntityViewByTenantIdAndCustomerId, tenantId [{}], customerId [{}], pageLink [{}]", - tenantId, customerId, pageLink); + log.trace("Executing findEntityViewByTenantIdAndCustomerId, tenantId [{}], customerId [{}]," + + " pageLink [{}]", tenantId, customerId, pageLink); validateId(tenantId, INCORRECT_TENANT_ID + tenantId); validateId(customerId, INCORRECT_CUSTOMER_ID + customerId); @@ -163,18 +164,20 @@ public class EntityViewServiceImpl extends AbstractEntityService } @Override - public TextPageData findEntityViewsByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, - String type, TextPageLink pageLink) { + public TextPageData findEntityViewsByTenantIdAndCustomerIdAndEntityId(TenantId tenantId, + CustomerId customerId, + EntityId entityId, + TextPageLink pageLink) { - log.trace("Executing findEntityViewsByTenantIdAndCustomerIdAndType, tenantId [{}], customerId [{}], type [{}]," + - " pageLink [{}]", tenantId, customerId, type, pageLink); + log.trace("Executing findEntityViewsByTenantIdAndCustomerIdAndType, tenantId [{}], customerId [{}]," + + " entityId [{}], pageLink [{}]", tenantId, customerId, entityId, pageLink); validateId(tenantId, INCORRECT_TENANT_ID + tenantId); validateId(customerId, INCORRECT_CUSTOMER_ID + customerId); - validateString(type, "Incorrect type " + type); + validateString(entityId.toString(), "Incorrect entityId " + entityId.toString()); validatePageLink(pageLink, INCORRECT_PAGE_LINK + pageLink); - List entityViews = entityViewDao.findEntityViewsByTenantIdAndCustomerIdAndType(tenantId.getId(), - customerId.getId(), type, pageLink); + List entityViews = entityViewDao.findEntityViewsByTenantIdAndCustomerIdAndEntityId( + tenantId.getId(), customerId.getId(), entityId, pageLink); return new TextPageData<>(entityViews, pageLink); } From 7009cbd8acaba462105fb05f59881a0b4b5d55c9 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Fri, 31 Aug 2018 14:26:38 +0300 Subject: [PATCH 26/41] Was added asigning value to of 'searchText' field into constructors --- .../thingsboard/server/dao/model/nosql/EntityViewEntity.java | 1 + .../org/thingsboard/server/dao/model/sql/EntityViewEntity.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java index c3227e9605..a6d57829c7 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java @@ -116,6 +116,7 @@ public class EntityViewEntity implements SearchTextEntity { } this.tsBegin = String.valueOf(entityView.getTsBegin()); this.tsEnd = String.valueOf(entityView.getTsEnd()); + this.searchText = entityView.getSearchText(); this.additionalInfo = entityView.getAdditionalInfo(); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java index dba075ce01..b12cf07976 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java @@ -39,7 +39,6 @@ import javax.persistence.Entity; import javax.persistence.Table; import java.io.IOException; import java.util.List; -import java.util.UUID; import java.util.stream.Collectors; /** @@ -110,6 +109,7 @@ public class EntityViewEntity extends BaseSqlEntity implements Searc } this.tsBegin = String.valueOf(entityView.getTsBegin()); this.tsEnd = String.valueOf(entityView.getTsEnd()); + this.searchText = entityView.getSearchText(); this.additionalInfo = entityView.getAdditionalInfo(); } From de21d977a183bdadf8acfbb777b5ef5ac06c3157 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Fri, 31 Aug 2018 15:40:03 +0300 Subject: [PATCH 27/41] Was final changing --- .../org/thingsboard/server/dao/entityview/EntityViewDao.java | 4 ++-- .../server/dao/entityview/EntityViewServiceImpl.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java index 781cb3a68d..25404db834 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewDao.java @@ -56,7 +56,7 @@ public interface EntityViewDao extends Dao { * @return the list of entity view objects */ List findEntityViewByTenantIdAndEntityId(UUID tenantId, - EntityId entityId, + UUID entityId, TextPageLink pageLink); /** @@ -82,7 +82,7 @@ public interface EntityViewDao extends Dao { */ List findEntityViewsByTenantIdAndCustomerIdAndEntityId(UUID tenantId, UUID customerId, - EntityId entityId, + UUID entityId, TextPageLink pageLink); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java index e44cea552f..4a42577044 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java @@ -135,7 +135,7 @@ public class EntityViewServiceImpl extends AbstractEntityService validateString(entityId.toString(), "Incorrect entityId " + entityId.toString()); validatePageLink(pageLink, INCORRECT_PAGE_LINK + pageLink); List entityViews = entityViewDao.findEntityViewByTenantIdAndEntityId(tenantId.getId(), - entityId, pageLink); + entityId.getId(), pageLink); return new TextPageData<>(entityViews, pageLink); } @@ -177,7 +177,7 @@ public class EntityViewServiceImpl extends AbstractEntityService validateString(entityId.toString(), "Incorrect entityId " + entityId.toString()); validatePageLink(pageLink, INCORRECT_PAGE_LINK + pageLink); List entityViews = entityViewDao.findEntityViewsByTenantIdAndCustomerIdAndEntityId( - tenantId.getId(), customerId.getId(), entityId, pageLink); + tenantId.getId(), customerId.getId(), entityId.getId(), pageLink); return new TextPageData<>(entityViews, pageLink); } From 4fa083c7488ec14b3ee7b5c622a38e94e1b766ad Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Fri, 31 Aug 2018 16:01:10 +0300 Subject: [PATCH 28/41] Added dao impl --- .../sql/entityview/EntityViewRepository.java | 81 ++++++++++++++ .../dao/sql/entityview/JpaEntityViewDao.java | 100 ++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 dao/src/main/java/org/thingsboard/server/dao/sql/entityview/EntityViewRepository.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/sql/entityview/JpaEntityViewDao.java diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/EntityViewRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/EntityViewRepository.java new file mode 100644 index 0000000000..27bd8395ac --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/EntityViewRepository.java @@ -0,0 +1,81 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.sql.entityview; + +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; +import org.thingsboard.server.common.data.id.EntityId; +import org.thingsboard.server.dao.model.sql.EntityViewEntity; +import org.thingsboard.server.dao.util.SqlDao; + +import java.util.List; + +/** + * Created by Victor Basanets on 8/31/2017. + */ +@SqlDao +public interface EntityViewRepository extends CrudRepository { + + @Query("SELECT e FROM EntityViewEntity e WHERE e.tenantId = :tenantId " + + "AND LOWER(e.searchText) LIKE LOWER(CONCAT(:textSearch, '%')) " + + "AND e.id > :idOffset ORDER BY e.id") + List findByTenantId(@Param("tenantId") String tenantId, + @Param("textSearch") String textSearch, + @Param("idOffset") String idOffset, + Pageable pageable); + + @Query("SELECT e FROM EntityViewEntity e WHERE e.tenantId = :tenantId " + + "AND e.entityId = :entityId " + + "AND LOWER(e.searchText) LIKE LOWER(CONCAT(:textSearch, '%')) " + + "AND e.id > :idOffset ORDER BY e.id") + List findByTenantIdAndEntityId(@Param("tenantId") String tenantId, + @Param("entityId") String entityId, + @Param("textSearch") String textSearch, + @Param("idOffset") String idOffset, + Pageable pageable); + + @Query("SELECT e FROM EntityViewEntity e WHERE e.tenantId = :tenantId " + + "AND e.customerId = :customerId " + + "AND LOWER(e.searchText) LIKE LOWER(CONCAT(:searchText, '%')) " + + "AND e.id > :idOffset ORDER BY e.id") + List findByTenantIdAndCustomerId(@Param("tenantId") String tenantId, + @Param("customerId") String customerId, + @Param("searchText") String searchText, + @Param("idOffset") String idOffset, + Pageable pageable); + + @Query("SELECT e FROM EntityViewEntity e WHERE e.tenantId = :tenantId " + + "AND e.customerId = :customerId " + + "AND e.entityId = :entityId " + + "AND LOWER(e.searchText) LIKE LOWER(CONCAT(:textSearch, '%')) " + + "AND e.id > :idOffset ORDER BY e.id") + List findByTenantIdAndCustomerIdAndEntityId(@Param("tenantId") String tenantId, + @Param("customerId") String customerId, + @Param("entityId") String entityId, + @Param("textSearch") String textSearch, + @Param("idOffset") String idOffset, + Pageable pageable); + + EntityViewEntity findByTenantIdAndName(String tenantId, String name); + + List findAllByTenantIdAndCustomerIdAndIdIn(String tenantId, + String customerId, + List entityViewsIds); + + List findAllByTenantIdAndIdIn(String tenantId, List entityViewsIds); +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/JpaEntityViewDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/JpaEntityViewDao.java new file mode 100644 index 0000000000..82ac3e08c6 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/JpaEntityViewDao.java @@ -0,0 +1,100 @@ +package org.thingsboard.server.dao.sql.entityview; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Component; +import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.id.EntityId; +import org.thingsboard.server.common.data.page.TextPageLink; +import org.thingsboard.server.dao.DaoUtil; +import org.thingsboard.server.dao.entityview.EntityViewDao; +import org.thingsboard.server.dao.model.sql.EntityViewEntity; +import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao; +import org.thingsboard.server.dao.util.SqlDao; + +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; + +import static org.thingsboard.server.common.data.UUIDConverter.fromTimeUUID; +import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID_STR; + +@Component +@SqlDao +public class JpaEntityViewDao extends JpaAbstractSearchTextDao + implements EntityViewDao { + + @Autowired + EntityViewRepository entityViewRepository; + + @Override + protected Class getEntityClass() { + return EntityViewEntity.class; + } + + @Override + protected CrudRepository getCrudRepository() { + return entityViewRepository; + } + + @Override + public List findEntityViewByTenantId(UUID tenantId, TextPageLink pageLink) { + return DaoUtil.convertDataList( + entityViewRepository.findByTenantId( + fromTimeUUID(tenantId), + Objects.toString(pageLink.getTextSearch(), ""), + pageLink.getIdOffset() == null ? NULL_UUID_STR : fromTimeUUID(pageLink.getIdOffset()), + new PageRequest(0, pageLink.getLimit()))); + } + + @Override + public Optional findEntityViewByTenantIdAndName(UUID tenantId, String name) { + return Optional.ofNullable( + DaoUtil.getData(entityViewRepository.findByTenantIdAndName(fromTimeUUID(tenantId), name))); + } + + @Override + public List findEntityViewByTenantIdAndEntityId(UUID tenantId, + UUID entityId, + TextPageLink pageLink) { + return DaoUtil.convertDataList( + entityViewRepository.findByTenantIdAndEntityId( + fromTimeUUID(tenantId), + fromTimeUUID(entityId), + Objects.toString(pageLink.getTextSearch(), ""), + pageLink.getIdOffset() == null ? NULL_UUID_STR : fromTimeUUID(pageLink.getIdOffset()), + new PageRequest(0, pageLink.getLimit()))); + } + + @Override + public List findEntityViewsByTenantIdAndCustomerId(UUID tenantId, + UUID customerId, + TextPageLink pageLink) { + return DaoUtil.convertDataList( + entityViewRepository.findByTenantIdAndCustomerId( + fromTimeUUID(tenantId), + fromTimeUUID(customerId), + Objects.toString(pageLink, ""), + pageLink.getIdOffset() == null ? NULL_UUID_STR : fromTimeUUID(pageLink.getIdOffset()), + new PageRequest(0, pageLink.getLimit()) + )); + } + + @Override + public List findEntityViewsByTenantIdAndCustomerIdAndEntityId(UUID tenantId, + UUID customerId, + UUID entityId, + TextPageLink pageLink) { + return DaoUtil.convertDataList( + entityViewRepository.findByTenantIdAndCustomerIdAndEntityId( + fromTimeUUID(tenantId), + fromTimeUUID(customerId), + fromTimeUUID(entityId), + Objects.toString(pageLink, ""), + pageLink.getIdOffset() == null ? NULL_UUID_STR : fromTimeUUID(pageLink.getIdOffset()), + new PageRequest(0, pageLink.getLimit()) + )); + } +} From 65908b001c987df9f9bea616c49961806a98e21f Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Mon, 3 Sep 2018 19:23:38 +0300 Subject: [PATCH 29/41] Was modified an entity view constant --- .../java/org/thingsboard/server/dao/model/ModelConstants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java b/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java index 90ccb66ca0..e084593324 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java @@ -147,7 +147,7 @@ public class ModelConstants { /** * Cassandra entityView constants. */ - public static final String ENTITY_VIEW_TABLE_FAMILY_NAME = "entity_view"; + public static final String ENTITY_VIEW_TABLE_FAMILY_NAME = "entity_views"; public static final String ENTITY_VIEW_ENTITY_ID_PROPERTY = ENTITY_ID_COLUMN; public static final String ENTITY_VIEW_TENANT_ID_PROPERTY = TENANT_ID_PROPERTY; public static final String ENTITY_VIEW_CUSTOMER_ID_PROPERTY = CUSTOMER_ID_PROPERTY; From 1f60a19dac20535a949a88aed6753a6d1e85232d Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Mon, 3 Sep 2018 19:26:42 +0300 Subject: [PATCH 30/41] Was added script to create new dbs for EntityView model --- dao/src/main/resources/cassandra/schema.cql | 42 +++++++++++++++++++++ dao/src/main/resources/sql/schema.sql | 13 +++++++ 2 files changed, 55 insertions(+) diff --git a/dao/src/main/resources/cassandra/schema.cql b/dao/src/main/resources/cassandra/schema.cql index f03122ab6b..bdd413d6ad 100644 --- a/dao/src/main/resources/cassandra/schema.cql +++ b/dao/src/main/resources/cassandra/schema.cql @@ -638,3 +638,45 @@ CREATE TABLE IF NOT EXISTS thingsboard.rule_node ( additional_info text, PRIMARY KEY (id) ); + +CREATE TABLE IF NOT EXISTS thingsboard.entity_views ( + id timeuuid, + entity_id timeuuid, + tenant_id timeuuid, + customer_id timeuuid, + name text, + keys text, + ts_begin bigint, + ts_end bigint, + search_text text, + additional_info text, + PRIMARY KEY (id, entity_id, tenant_id, customer_id) +); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.entity_views_by_tenant_and_name AS + SELECT * + from thingsboard.entity_views + WHERE entity_id IS NOT NULL AND tenant_id IS NOT NULL AND customer_id IS NOT NULL AND keys IS NOT NULL AND ts_begin IS NOT NULL AND ts_end IS NOT NULL AND name IS NOT NULL AND id IS NOT NULL + PRIMARY KEY (tenant_id, name, id, entity_id, customer_id) + WITH CLUSTERING ORDER BY (name ASC, id DESC, entity_id DESC, customer_id DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.entity_views_by_tenant_and_entity AS + SELECT * + from thingsboard.entity_views + WHERE entity_id IS NOT NULL AND tenant_id IS NOT NULL AND customer_id IS NOT NULL AND keys IS NOT NULL AND ts_begin IS NOT NULL AND ts_end IS NOT NULL AND name IS NOT NULL AND id IS NOT NULL + PRIMARY KEY (tenant_id, entity_id, id, customer_id, name) + WITH CLUSTERING ORDER BY (entity_id ASC, customer_id ASC, id DESC, name DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.entity_views_by_tenant_and_customer AS + SELECT * + from thingsboard.entity_views + WHERE entity_id IS NOT NULL AND tenant_id IS NOT NULL AND customer_id IS NOT NULL AND keys IS NOT NULL AND ts_begin IS NOT NULL AND ts_end IS NOT NULL AND name IS NOT NULL AND id IS NOT NULL + PRIMARY KEY (tenant_id, customer_id, id, entity_id, name) + WITH CLUSTERING ORDER BY (customer_id ASC, id DESC, entity_id DESC, name DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.entity_views_by_tenant_and_customer_and_entity AS + SELECT * + from thingsboard.entity_views + WHERE entity_id IS NOT NULL AND tenant_id IS NOT NULL AND customer_id IS NOT NULL AND keys IS NOT NULL AND ts_begin IS NOT NULL AND ts_end IS NOT NULL AND name IS NOT NULL AND id IS NOT NULL + PRIMARY KEY (tenant_id, customer_id, entity_id, id, name) + WITH CLUSTERING ORDER BY (customer_id ASC, entity_id DESC, id DESC, name DESC); diff --git a/dao/src/main/resources/sql/schema.sql b/dao/src/main/resources/sql/schema.sql index 91e77da503..8e21e72e9d 100644 --- a/dao/src/main/resources/sql/schema.sql +++ b/dao/src/main/resources/sql/schema.sql @@ -251,3 +251,16 @@ CREATE TABLE IF NOT EXISTS rule_node ( debug_mode boolean, search_text varchar(255) ); + +CREATE TABLE IF NOT EXISTS entity_views ( + id varchar(31) NOT NULL CONSTRAINT entity_view_pkey PRIMARY KEY, + additional_info varchar, + customer_id varchar(31), + keys varchar(255), + ts_begin varchar(255), + ts_end varchar(255), + name varchar(255), + search_text varchar(255), + entity_id varchar(31), + tenant_id varchar(31) +); From 8fdfa909dbc211ec5078e5bcdaa710ff37b92c7d Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Mon, 3 Sep 2018 19:30:48 +0300 Subject: [PATCH 31/41] Was modified classes to config and create DB for EntityView model --- .../server/install/ThingsboardInstallService.java | 7 +++++++ .../server/service/install/DefaultDataUpdateService.java | 4 ++++ .../service/install/SqlDatabaseUpgradeService.java | 9 +++++++++ 3 files changed, 20 insertions(+) diff --git a/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java index f863d0bb2a..347b05409e 100644 --- a/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java +++ b/application/src/main/java/org/thingsboard/server/install/ThingsboardInstallService.java @@ -88,6 +88,13 @@ public class ThingsboardInstallService { dataUpdateService.updateData("1.4.0"); + case "2.0.0": + log.info("Upgrading ThingsBoard from version 2.0.0 to 2.1.1 ..."); + + databaseUpgradeService.upgradeDatabase("2.0.0"); + + dataUpdateService.updateData("2.0.0"); + log.info("Updating system data..."); systemDataLoaderService.deleteSystemWidgetBundle("charts"); diff --git a/application/src/main/java/org/thingsboard/server/service/install/DefaultDataUpdateService.java b/application/src/main/java/org/thingsboard/server/service/install/DefaultDataUpdateService.java index 5daebcc3c5..0194a5dfd7 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/DefaultDataUpdateService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/DefaultDataUpdateService.java @@ -49,6 +49,10 @@ public class DefaultDataUpdateService implements DataUpdateService { log.info("Updating data from version 1.4.0 to 2.0.0 ..."); tenantsDefaultRuleChainUpdater.updateEntities(null); break; + case "2.0.0": + log.info("Updating data from version 2.0.0 to 2.1.1 ..."); + tenantsDefaultRuleChainUpdater.updateEntities(null); + break; default: throw new RuntimeException("Unable to update data, unsupported fromVersion: " + fromVersion); } diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java index 3a4a837217..7d701b7a53 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java @@ -107,6 +107,15 @@ public class SqlDatabaseUpgradeService implements DatabaseUpgradeService { log.info("Schema updated."); } break; + case "2.0.0": + try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { + log.info("Updating schema ..."); + schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "2.1.1", SCHEMA_UPDATE_SQL); + loadSql(schemaUpdateFile, conn); + log.info("Schema updated."); + } + break; + default: throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion); } From 64620a89ceebe54731c9fb349098492dd51287cc Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Mon, 3 Sep 2018 19:32:29 +0300 Subject: [PATCH 32/41] Was add couple test methods --- .../sql/EntityViewControllerSqlTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java b/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java index 84e621fcf6..e10610ac6e 100644 --- a/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java @@ -15,13 +15,30 @@ */ package org.thingsboard.server.controller.sql; +import org.junit.Assert; +import org.junit.Test; +import org.thingsboard.server.common.data.EntityView; import org.thingsboard.server.controller.BaseEntityViewControllerTest; import org.thingsboard.server.dao.service.DaoSqlTest; +import java.util.Arrays; + /** * Created by Victor Basanets on 8/27/2017. */ @DaoSqlTest public class EntityViewControllerSqlTest extends BaseEntityViewControllerTest { + + @Test + public void testSaveEntityViewWithIdOfDevice() throws Exception { + super.testSaveEntityViewWithIdOfDevice(); + } + + @Test + public void testFindEntityViewById() throws Exception { + super.testFindEntityViewById(); + } + + } From 2cc562ed58cfc34675f5cced8fc853b90ef95b91 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Mon, 3 Sep 2018 19:48:01 +0300 Subject: [PATCH 33/41] Was added header --- .../dao/sql/entityview/JpaEntityViewDao.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/JpaEntityViewDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/JpaEntityViewDao.java index 82ac3e08c6..57b0afae8b 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/JpaEntityViewDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/entityview/JpaEntityViewDao.java @@ -1,3 +1,18 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.thingsboard.server.dao.sql.entityview; import org.springframework.beans.factory.annotation.Autowired; @@ -21,6 +36,9 @@ import java.util.UUID; import static org.thingsboard.server.common.data.UUIDConverter.fromTimeUUID; import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID_STR; +/** + * Created by Victor Basanets on 8/31/2017. + */ @Component @SqlDao public class JpaEntityViewDao extends JpaAbstractSearchTextDao From d24391a27632ed1fc638a93ffba55618a3400820 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Tue, 4 Sep 2018 10:45:39 +0300 Subject: [PATCH 34/41] Was added update for sql and cql --- .../main/data/upgrade/2.1.1/schema_update.cql | 91 +++++++++++++++++++ .../main/data/upgrade/2.1.1/schema_update.sql | 30 ++++++ 2 files changed, 121 insertions(+) create mode 100644 application/src/main/data/upgrade/2.1.1/schema_update.cql create mode 100644 application/src/main/data/upgrade/2.1.1/schema_update.sql diff --git a/application/src/main/data/upgrade/2.1.1/schema_update.cql b/application/src/main/data/upgrade/2.1.1/schema_update.cql new file mode 100644 index 0000000000..c6fdf5a3b8 --- /dev/null +++ b/application/src/main/data/upgrade/2.1.1/schema_update.cql @@ -0,0 +1,91 @@ +/*-- +-- Copyright © 2016-2018 The Thingsboard Authors +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +--*/ +DROP MATERIALIZED VIEW IF EXISTS thingsboard.entity_views_by_tenant_and_name; +DROP MATERIALIZED VIEW IF EXISTS thingsboard.entity_views_by_tenant_and_entity; +DROP MATERIALIZED VIEW IF EXISTS thingsboard.entity_views_by_tenant_and_customer; +DROP MATERIALIZED VIEW IF EXISTS thingsboard.entity_views_by_tenant_and_customer_and_entity; + +DROP TABLE IF EXISTS thingsboard.entity_views; + +CREATE TABLE IF NOT EXISTS thingsboard.entity_views ( + id timeuuid, + entity_id timeuuid, + tenant_id timeuuid, + customer_id timeuuid, + name text, + keys text, + ts_begin bigint, + ts_end bigint, + search_text text, + additional_info text, + PRIMARY KEY (id, entity_id, tenant_id, customer_id) + ); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.entity_views_by_tenant_and_name AS + SELECT * + from thingsboard.entity_views + WHERE entity_id IS NOT NULL + AND tenant_id IS NOT NULL + AND customer_id IS NOT NULL + AND keys IS NOT NULL + AND ts_begin IS NOT NULL + AND ts_end IS NOT NULL + AND name IS NOT NULL + AND id IS NOT NULL + PRIMARY KEY (tenant_id, name, id, entity_id, customer_id) + WITH CLUSTERING ORDER BY (name ASC, id DESC, entity_id DESC, customer_id DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.entity_views_by_tenant_and_entity AS + SELECT * + from thingsboard.entity_views + WHERE entity_id IS NOT NULL + AND tenant_id IS NOT NULL + AND customer_id IS NOT NULL + AND keys IS NOT NULL + AND ts_begin IS NOT NULL + AND ts_end IS NOT NULL + AND name IS NOT NULL + AND id IS NOT NULL + PRIMARY KEY (tenant_id, entity_id, id, customer_id, name) + WITH CLUSTERING ORDER BY (entity_id ASC, customer_id ASC, id DESC, name DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.entity_views_by_tenant_and_customer AS + SELECT * + from thingsboard.entity_views + WHERE entity_id IS NOT NULL + AND tenant_id IS NOT NULL + AND customer_id IS NOT NULL + AND keys IS NOT NULL + AND ts_begin IS NOT NULL + AND ts_end IS NOT NULL + AND name IS NOT NULL + AND id IS NOT NULL + PRIMARY KEY (tenant_id, customer_id, id, entity_id, name) + WITH CLUSTERING ORDER BY (customer_id ASC, id DESC, entity_id DESC, name DESC); + +CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.entity_views_by_tenant_and_customer_and_entity AS + SELECT * + from thingsboard.entity_views + WHERE entity_id IS NOT NULL + AND tenant_id IS NOT NULL + AND customer_id IS NOT NULL + AND keys IS NOT NULL + AND ts_begin IS NOT NULL + AND ts_end IS NOT NULL + AND name IS NOT NULL + AND id IS NOT NULL + PRIMARY KEY (tenant_id, customer_id, entity_id, id, name) + WITH CLUSTERING ORDER BY (customer_id ASC, entity_id DESC, id DESC, name DESC); diff --git a/application/src/main/data/upgrade/2.1.1/schema_update.sql b/application/src/main/data/upgrade/2.1.1/schema_update.sql new file mode 100644 index 0000000000..f4754e99c9 --- /dev/null +++ b/application/src/main/data/upgrade/2.1.1/schema_update.sql @@ -0,0 +1,30 @@ +-- +-- Copyright © 2016-2018 The Thingsboard Authors +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +DROP TABLE IF EXISTS entity_views; + +CREATE TABLE IF NOT EXISTS entity_views ( + id varchar(31) NOT NULL CONSTRAINT entity_views_pkey PRIMARY KEY, + additional_info varchar, + customer_id varchar(31), + keys varchar(255), + ts_begin varchar(255), + ts_end varchar(255), + name varchar(255), + search_text varchar(255), + entity_id varchar(31), + tenant_id varchar(31) +); From 0cb7f455f070902082e924a800b148f84c2b04d3 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Tue, 4 Sep 2018 11:05:52 +0300 Subject: [PATCH 35/41] Was modified header --- application/src/main/data/upgrade/2.1.1/schema_update.cql | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/application/src/main/data/upgrade/2.1.1/schema_update.cql b/application/src/main/data/upgrade/2.1.1/schema_update.cql index c6fdf5a3b8..d9ba517df7 100644 --- a/application/src/main/data/upgrade/2.1.1/schema_update.cql +++ b/application/src/main/data/upgrade/2.1.1/schema_update.cql @@ -1,4 +1,4 @@ -/*-- +-- -- Copyright © 2016-2018 The Thingsboard Authors -- -- Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,7 +12,8 @@ -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -- See the License for the specific language governing permissions and -- limitations under the License. ---*/ +-- + DROP MATERIALIZED VIEW IF EXISTS thingsboard.entity_views_by_tenant_and_name; DROP MATERIALIZED VIEW IF EXISTS thingsboard.entity_views_by_tenant_and_entity; DROP MATERIALIZED VIEW IF EXISTS thingsboard.entity_views_by_tenant_and_customer; From d28bd62b83da735a1a441eed5da5eb47c033fb89 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 5 Sep 2018 18:06:58 +0300 Subject: [PATCH 36/41] Was created classes for telemetry --- .../data/objects/AttributesEntityView.java | 47 +++++++++++++++++++ .../data/objects/TelemetryEntityView.java | 43 +++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/objects/AttributesEntityView.java create mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/objects/TelemetryEntityView.java diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/objects/AttributesEntityView.java b/common/data/src/main/java/org/thingsboard/server/common/data/objects/AttributesEntityView.java new file mode 100644 index 0000000000..1c32579b72 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/objects/AttributesEntityView.java @@ -0,0 +1,47 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.common.data.objects; + +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Victor Basanets on 9/05/2017. + */ +@Data +@NoArgsConstructor +public class AttributesEntityView { + + private List cs = new ArrayList<>(); + private List ss = new ArrayList<>(); + private List sh = new ArrayList<>(); + + public AttributesEntityView(List cs, + List ss, + List sh) { + + this.cs = new ArrayList<>(cs); + this.ss = new ArrayList<>(ss); + this.sh = new ArrayList<>(sh); + } + + public AttributesEntityView(AttributesEntityView obj) { + this(obj.getCs(), obj.getSs(), obj.getSh()); + } +} diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/objects/TelemetryEntityView.java b/common/data/src/main/java/org/thingsboard/server/common/data/objects/TelemetryEntityView.java new file mode 100644 index 0000000000..c899c65590 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/objects/TelemetryEntityView.java @@ -0,0 +1,43 @@ +/** + * Copyright © 2016-2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.common.data.objects; + +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Victor Basanets on 9/05/2017. + */ +@Data +@NoArgsConstructor +public class TelemetryEntityView { + + private List timeseries; + private AttributesEntityView attributes; + + public TelemetryEntityView(List timeseries, AttributesEntityView attributes) { + + this.timeseries = new ArrayList<>(timeseries); + this.attributes = attributes; + } + + public TelemetryEntityView(TelemetryEntityView obj) { + this(obj.getTimeseries(), obj.getAttributes()); + } +} From 48ff2b2723bedffad829c9a9e756d05f62d9cf9b Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 5 Sep 2018 18:10:39 +0300 Subject: [PATCH 37/41] Was modified --- application/src/main/data/upgrade/2.1.1/schema_update.sql | 1 + .../java/org/thingsboard/server/common/data/EntityView.java | 6 ++---- .../thingsboard/server/common/data/id/EntityIdFactory.java | 2 ++ 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/application/src/main/data/upgrade/2.1.1/schema_update.sql b/application/src/main/data/upgrade/2.1.1/schema_update.sql index f4754e99c9..2fc953516f 100644 --- a/application/src/main/data/upgrade/2.1.1/schema_update.sql +++ b/application/src/main/data/upgrade/2.1.1/schema_update.sql @@ -26,5 +26,6 @@ CREATE TABLE IF NOT EXISTS entity_views ( name varchar(255), search_text varchar(255), entity_id varchar(31), + entity_type varchar(255), tenant_id varchar(31) ); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java index 102f6eebec..03fce40cea 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java @@ -20,9 +20,7 @@ import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityViewId; import org.thingsboard.server.common.data.id.TenantId; - -import java.beans.ConstructorProperties; -import java.util.List; +import org.thingsboard.server.common.data.objects.TelemetryEntityView; /** * Created by Victor Basanets on 8/27/2017. @@ -40,7 +38,7 @@ public class EntityView extends SearchTextBasedWithAdditionalInfo private TenantId tenantId; private CustomerId customerId; private String name; - private List keys; + private TelemetryEntityView keys; //To Do: Changed from all code private Long tsBegin; private Long tsEnd; diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/id/EntityIdFactory.java b/common/data/src/main/java/org/thingsboard/server/common/data/id/EntityIdFactory.java index ed4cf2f784..4e35c0b2df 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/id/EntityIdFactory.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/id/EntityIdFactory.java @@ -57,6 +57,8 @@ public class EntityIdFactory { return new RuleChainId(uuid); case RULE_NODE: return new RuleNodeId(uuid); + case ENTITY_VIEW: + return new EntityViewId(uuid); } throw new IllegalArgumentException("EntityType " + type + " is not supported!"); } From 481d2f4770d376ae9363fe3c560d02e12549501a Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 5 Sep 2018 18:12:09 +0300 Subject: [PATCH 38/41] commit --- .../BaseEntityViewControllerTest.java | 26 +++++++++++++------ .../controller/ControllerSqlTestSuite.java | 2 +- .../nosql/EntityViewControllerNoSqlTest.java | 3 +-- .../sql/EntityViewControllerSqlTest.java | 15 +---------- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java index 7d9bae1901..c7ac98d413 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseEntityViewControllerTest.java @@ -23,6 +23,8 @@ import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.EntityView; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.User; +import org.thingsboard.server.common.data.objects.AttributesEntityView; +import org.thingsboard.server.common.data.objects.TelemetryEntityView; import org.thingsboard.server.common.data.security.Authority; import java.util.Arrays; @@ -35,16 +37,12 @@ public abstract class BaseEntityViewControllerTest extends AbstractControllerTes private Tenant savedTenant; private User tenantAdmin; private Device testDevice; + private TelemetryEntityView obj; @Before public void beforeTest() throws Exception { loginSysAdmin(); - Device device = new Device(); - device.setName("Test device"); - device.setType("default"); - testDevice = doPost("/api/device", device, Device.class); - Tenant tenant = new Tenant(); tenant.setTitle("My tenant"); savedTenant = doPost("/api/tenant", tenant, Tenant.class); @@ -59,6 +57,18 @@ public abstract class BaseEntityViewControllerTest extends AbstractControllerTes tenantAdmin.setLastName("Downs"); tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1"); + + Device device = new Device(); + device.setName("Test device"); + device.setType("default"); + testDevice = doPost("/api/device", device, Device.class); + + obj = new TelemetryEntityView( + Arrays.asList("109L", "209L"), + new AttributesEntityView( + Arrays.asList("caKey1", "caKey2", "caKey3"), + Arrays.asList("saKey1", "saKey2", "saKey3", "saKey4"), + Arrays.asList("shKey1", "shKey2", "shKey3", "shKey4", "shKey5"))); } @After @@ -74,7 +84,7 @@ public abstract class BaseEntityViewControllerTest extends AbstractControllerTes EntityView view = new EntityView(); view.setName("Test entity view"); view.setEntityId(testDevice.getId()); - view.setKeys(Arrays.asList("key1", "key2", "key3")); + view.setKeys(new TelemetryEntityView(obj)); EntityView savedView = doPost("/api/entity-view", view, EntityView.class); EntityView foundView = doGet("/api/entity-view/" + savedView.getId().getId().toString(), EntityView.class); Assert.assertNotNull(foundView); @@ -87,7 +97,7 @@ public abstract class BaseEntityViewControllerTest extends AbstractControllerTes view.setEntityId(testDevice.getId()); view.setName("Test entity view"); view.setTenantId(savedTenant.getId()); - view.setKeys(Arrays.asList("key1", "key2", "key3")); + view.setKeys(new TelemetryEntityView(obj)); EntityView savedView = doPost("/api/entity-view", view, EntityView.class); Assert.assertNotNull(savedView); @@ -112,7 +122,7 @@ public abstract class BaseEntityViewControllerTest extends AbstractControllerTes EntityView view = new EntityView(); view.setName("Test entity view"); view.setEntityId(testDevice.getId()); - view.setKeys(Arrays.asList("key1", "key2", "key3")); + view.setKeys(new TelemetryEntityView((TelemetryEntityView) obj)); EntityView savedView = doPost("/api/entity-view", view, EntityView.class); doDelete("/api/entity-view/" + savedView.getId().getId().toString()) diff --git a/application/src/test/java/org/thingsboard/server/controller/ControllerSqlTestSuite.java b/application/src/test/java/org/thingsboard/server/controller/ControllerSqlTestSuite.java index f316051212..a9e94e9184 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ControllerSqlTestSuite.java +++ b/application/src/test/java/org/thingsboard/server/controller/ControllerSqlTestSuite.java @@ -24,7 +24,7 @@ import java.util.Arrays; @RunWith(ClasspathSuite.class) @ClasspathSuite.ClassnameFilters({ - "org.thingsboard.server.controller.sql.*SqlTest", + "org.thingsboard.server.controller.sql.EntityViewControllerSqlTest", }) public class ControllerSqlTestSuite { diff --git a/application/src/test/java/org/thingsboard/server/controller/nosql/EntityViewControllerNoSqlTest.java b/application/src/test/java/org/thingsboard/server/controller/nosql/EntityViewControllerNoSqlTest.java index 095edd19f8..ad066fc7ab 100644 --- a/application/src/test/java/org/thingsboard/server/controller/nosql/EntityViewControllerNoSqlTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/nosql/EntityViewControllerNoSqlTest.java @@ -20,6 +20,5 @@ import org.thingsboard.server.controller.BaseEntityViewControllerTest; /** * Created by Victor Basanets on 8/27/2017. */ -public class EntityViewControllerNoSqlTest - extends BaseEntityViewControllerTest { +public class EntityViewControllerNoSqlTest extends BaseEntityViewControllerTest { } diff --git a/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java b/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java index e10610ac6e..76d99250c1 100644 --- a/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/sql/EntityViewControllerSqlTest.java @@ -27,18 +27,5 @@ import java.util.Arrays; * Created by Victor Basanets on 8/27/2017. */ @DaoSqlTest -public class EntityViewControllerSqlTest - extends BaseEntityViewControllerTest { - - @Test - public void testSaveEntityViewWithIdOfDevice() throws Exception { - super.testSaveEntityViewWithIdOfDevice(); - } - - @Test - public void testFindEntityViewById() throws Exception { - super.testFindEntityViewById(); - } - - +public class EntityViewControllerSqlTest extends BaseEntityViewControllerTest { } From 70ee36352816639472463caf089675411ba84005 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 5 Sep 2018 18:14:45 +0300 Subject: [PATCH 39/41] commit2 --- .../server/dao/model/ModelConstants.java | 1 + .../dao/model/nosql/EntityViewEntity.java | 32 ++++++--------- .../dao/model/sql/EntityViewEntity.java | 40 +++++++++---------- dao/src/main/resources/sql/schema.sql | 1 + 4 files changed, 34 insertions(+), 40 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java b/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java index e084593324..d06ddbd8e5 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java @@ -152,6 +152,7 @@ public class ModelConstants { public static final String ENTITY_VIEW_TENANT_ID_PROPERTY = TENANT_ID_PROPERTY; public static final String ENTITY_VIEW_CUSTOMER_ID_PROPERTY = CUSTOMER_ID_PROPERTY; public static final String ENTITY_VIEW_NAME_PROPERTY = DEVICE_NAME_PROPERTY; + public static final String ENTITY_VIEW_TYPE_PROPERTY = "type_entity"; public static final String ENTITY_VIEW_KEYS_PROPERTY = "keys"; public static final String ENTITY_VIEW_TS_BEGIN_PROPERTY = "ts_begin"; public static final String ENTITY_VIEW_TS_END_PROPERTY = "ts_end"; diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java index a6d57829c7..65914dd566 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java @@ -18,9 +18,7 @@ package org.thingsboard.server.dao.model.nosql; import com.datastax.driver.core.utils.UUIDs; import com.datastax.driver.mapping.annotations.PartitionKey; import com.datastax.driver.mapping.annotations.Table; -import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -30,15 +28,14 @@ import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.EntityViewId; import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.objects.TelemetryEntityView; import org.thingsboard.server.dao.model.ModelConstants; import org.thingsboard.server.dao.model.SearchTextEntity; import javax.persistence.Column; import java.io.IOException; -import java.util.List; import java.util.UUID; -import java.util.stream.Collectors; import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_TABLE_FAMILY_NAME; import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY; @@ -106,16 +103,13 @@ public class EntityViewEntity implements SearchTextEntity { this.customerId = entityView.getCustomerId().getId(); } this.name = entityView.getName(); - try { - this.keys = new ObjectMapper().readTree("{\"" + entityView.getName() + "\" : [" + - entityView.getKeys().stream() - .map(k -> "\"" + k + "\"") - .collect(Collectors.joining(", ")) + "]}"); - } catch (IOException e) { - e.printStackTrace(); - } - this.tsBegin = String.valueOf(entityView.getTsBegin()); - this.tsEnd = String.valueOf(entityView.getTsEnd()); +// try { +// this.keys = entityView.getKeys(); +// } catch (IOException e) { +// e.printStackTrace(); +// } + this.tsBegin = entityView.getTsBegin() != null ? String.valueOf(entityView.getTsBegin()) : "0"; + this.tsEnd = entityView.getTsEnd() != null ? String.valueOf(entityView.getTsEnd()) : "0"; this.searchText = entityView.getSearchText(); this.additionalInfo = entityView.getAdditionalInfo(); } @@ -139,11 +133,11 @@ public class EntityViewEntity implements SearchTextEntity { entityView.setCustomerId(new CustomerId(customerId)); } entityView.setName(name); - try { - entityView.setKeys(new ObjectMapper().readValue(keys.toString(), new TypeReference>(){})); - } catch (IOException e) { - e.printStackTrace(); - } +// try { +// entityView.setKeys((TelemetryEntityView) entityView.getKeys().toObject(keys)); +// } catch (IOException e) { +// e.printStackTrace(); +// } entityView.setTsBegin(Long.parseLong(tsBegin)); entityView.setTsEnd(Long.parseLong(tsEnd)); entityView.setAdditionalInfo(additionalInfo); diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java index b12cf07976..62e9eb3e78 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java @@ -16,7 +16,6 @@ package org.thingsboard.server.dao.model.sql; import com.datastax.driver.core.utils.UUIDs; -import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Data; @@ -25,21 +24,18 @@ import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityView; -import org.thingsboard.server.common.data.id.CustomerId; -import org.thingsboard.server.common.data.id.DeviceId; -import org.thingsboard.server.common.data.id.EntityViewId; -import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.id.*; +import org.thingsboard.server.common.data.objects.TelemetryEntityView; import org.thingsboard.server.dao.model.BaseSqlEntity; import org.thingsboard.server.dao.model.ModelConstants; import org.thingsboard.server.dao.model.SearchTextEntity; import org.thingsboard.server.dao.util.mapping.JsonStringType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Table; +import javax.persistence.*; import java.io.IOException; -import java.util.List; -import java.util.stream.Collectors; + +import static org.thingsboard.server.dao.model.ModelConstants.AUDIT_LOG_ENTITY_TYPE_PROPERTY; +import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_TYPE_PROPERTY; /** * Created by Victor Basanets on 8/30/2017. @@ -64,9 +60,12 @@ public class EntityViewEntity extends BaseSqlEntity implements Searc @Column(name = ModelConstants.ENTITY_VIEW_NAME_PROPERTY) private String name; - @Type(type = "json") + @Enumerated(EnumType.STRING) + @Column(name = ENTITY_TYPE_PROPERTY) + private EntityType entityType; + @Column(name = ModelConstants.ENTITY_VIEW_KEYS_PROPERTY) - private JsonNode keys; + private String keys; @Column(name = ModelConstants.ENTITY_VIEW_TS_BEGIN_PROPERTY) private String tsBegin; @@ -81,6 +80,8 @@ public class EntityViewEntity extends BaseSqlEntity implements Searc @Column(name = ModelConstants.ENTITY_VIEW_ADDITIONAL_INFO_PROPERTY) private JsonNode additionalInfo; + private static final ObjectMapper mapper = new ObjectMapper(); + public EntityViewEntity() { super(); } @@ -91,6 +92,7 @@ public class EntityViewEntity extends BaseSqlEntity implements Searc } if (entityView.getEntityId() != null) { this.entityId = toString(entityView.getEntityId().getId()); + this.entityType = entityView.getEntityId().getEntityType(); } if (entityView.getTenantId() != null) { this.tenantId = toString(entityView.getTenantId().getId()); @@ -100,15 +102,12 @@ public class EntityViewEntity extends BaseSqlEntity implements Searc } this.name = entityView.getName(); try { - this.keys = new ObjectMapper().readTree("{\"" + entityView.getName() + "\" : [" + - entityView.getKeys().stream() - .map(k -> "\"" + k + "\"") - .collect(Collectors.joining(", ")) + "]}"); + this.keys = mapper.writeValueAsString(entityView.getKeys()); } catch (IOException e) { e.printStackTrace(); } - this.tsBegin = String.valueOf(entityView.getTsBegin()); - this.tsEnd = String.valueOf(entityView.getTsEnd()); + this.tsBegin = entityView.getTsBegin() != null ? String.valueOf(entityView.getTsBegin()) : ""; + this.tsEnd = entityView.getTsEnd() != null ? String.valueOf(entityView.getTsEnd()) : ""; this.searchText = entityView.getSearchText(); this.additionalInfo = entityView.getAdditionalInfo(); } @@ -128,9 +127,8 @@ public class EntityViewEntity extends BaseSqlEntity implements Searc EntityView entityView = new EntityView(new EntityViewId(getId())); entityView.setCreatedTime(UUIDs.unixTimestamp(getId())); - /*Ned to refactor and replace DeviceId to Class<> instance*/ if (entityId != null) { - entityView.setEntityId(new DeviceId(toUUID(entityId))); + entityView.setEntityId(EntityIdFactory.getByTypeAndId(entityType.name(), toUUID(entityId).toString())); } if (tenantId != null) { entityView.setTenantId(new TenantId(toUUID(tenantId))); @@ -140,7 +138,7 @@ public class EntityViewEntity extends BaseSqlEntity implements Searc } entityView.setName(name); try { - entityView.setKeys(new ObjectMapper().readValue(keys.toString(), new TypeReference>(){})); + entityView.setKeys(mapper.readValue(keys, TelemetryEntityView.class)); } catch (IOException e) { e.printStackTrace(); } diff --git a/dao/src/main/resources/sql/schema.sql b/dao/src/main/resources/sql/schema.sql index 8e21e72e9d..b79b61f3af 100644 --- a/dao/src/main/resources/sql/schema.sql +++ b/dao/src/main/resources/sql/schema.sql @@ -260,6 +260,7 @@ CREATE TABLE IF NOT EXISTS entity_views ( ts_begin varchar(255), ts_end varchar(255), name varchar(255), + entity_type varchar(255), search_text varchar(255), entity_id varchar(31), tenant_id varchar(31) From d731bf614e93c5e87b092bfb2f3bba2456ec8409 Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Wed, 5 Sep 2018 19:29:42 +0300 Subject: [PATCH 40/41] commit3 --- .../src/main/data/upgrade/2.1.1/schema_update.sql | 12 ++++++------ .../thingsboard/server/common/data/EntityView.java | 2 +- .../common/data/objects/AttributesEntityView.java | 5 +++++ .../common/data/objects/TelemetryEntityView.java | 5 +++++ .../server/dao/entityview/EntityViewServiceImpl.java | 3 +-- .../server/dao/model/sql/EntityViewEntity.java | 8 ++++---- dao/src/main/resources/sql/schema.sql | 10 +++++----- 7 files changed, 27 insertions(+), 18 deletions(-) diff --git a/application/src/main/data/upgrade/2.1.1/schema_update.sql b/application/src/main/data/upgrade/2.1.1/schema_update.sql index 2fc953516f..3670aad035 100644 --- a/application/src/main/data/upgrade/2.1.1/schema_update.sql +++ b/application/src/main/data/upgrade/2.1.1/schema_update.sql @@ -17,15 +17,15 @@ DROP TABLE IF EXISTS entity_views; CREATE TABLE IF NOT EXISTS entity_views ( - id varchar(31) NOT NULL CONSTRAINT entity_views_pkey PRIMARY KEY, - additional_info varchar, + id varchar(31) NOT NULL CONSTRAINT entity_view_pkey PRIMARY KEY, + entity_id varchar(31), + entity_type varchar(255), + tenant_id varchar(31), customer_id varchar(31), + name varchar(255), keys varchar(255), ts_begin varchar(255), ts_end varchar(255), - name varchar(255), search_text varchar(255), - entity_id varchar(31), - entity_type varchar(255), - tenant_id varchar(31) + additional_info varchar ); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java index 03fce40cea..813a9acd08 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/EntityView.java @@ -38,7 +38,7 @@ public class EntityView extends SearchTextBasedWithAdditionalInfo private TenantId tenantId; private CustomerId customerId; private String name; - private TelemetryEntityView keys; //To Do: Changed from all code + private TelemetryEntityView keys; private Long tsBegin; private Long tsEnd; diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/objects/AttributesEntityView.java b/common/data/src/main/java/org/thingsboard/server/common/data/objects/AttributesEntityView.java index 1c32579b72..b1d270c934 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/objects/AttributesEntityView.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/objects/AttributesEntityView.java @@ -44,4 +44,9 @@ public class AttributesEntityView { public AttributesEntityView(AttributesEntityView obj) { this(obj.getCs(), obj.getSs(), obj.getSh()); } + + @Override + public String toString() { + return "{cs=" + cs + ", ss=" + ss + ", sh=" + sh + '}'; + } } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/objects/TelemetryEntityView.java b/common/data/src/main/java/org/thingsboard/server/common/data/objects/TelemetryEntityView.java index c899c65590..e7398e6500 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/objects/TelemetryEntityView.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/objects/TelemetryEntityView.java @@ -40,4 +40,9 @@ public class TelemetryEntityView { public TelemetryEntityView(TelemetryEntityView obj) { this(obj.getTimeseries(), obj.getAttributes()); } + + @Override + public String toString() { + return "{timeseries=" + timeseries + ", attributes=" + attributes + '}'; + } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java index 4a42577044..6554083fe4 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java @@ -213,7 +213,7 @@ public class EntityViewServiceImpl extends AbstractEntityService @Override protected void validateDataImpl(EntityView entityView) { - if (StringUtils.isEmpty(String.join("", entityView.getKeys()))) { + if (StringUtils.isEmpty(entityView.getKeys().toString())) { throw new DataValidationException("Entity view type should be specified!"); } if (StringUtils.isEmpty(entityView.getName())) { @@ -272,6 +272,5 @@ public class EntityViewServiceImpl extends AbstractEntityService protected void removeEntity(EntityView entity) { unassignEntityViewFromCustomer(new EntityViewId(entity.getUuidId())); } - } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java index 62e9eb3e78..1175fac864 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java @@ -51,6 +51,10 @@ public class EntityViewEntity extends BaseSqlEntity implements Searc @Column(name = ModelConstants.ENTITY_VIEW_ENTITY_ID_PROPERTY) private String entityId; + @Enumerated(EnumType.STRING) + @Column(name = ENTITY_TYPE_PROPERTY) + private EntityType entityType; + @Column(name = ModelConstants.ENTITY_VIEW_TENANT_ID_PROPERTY) private String tenantId; @@ -60,10 +64,6 @@ public class EntityViewEntity extends BaseSqlEntity implements Searc @Column(name = ModelConstants.ENTITY_VIEW_NAME_PROPERTY) private String name; - @Enumerated(EnumType.STRING) - @Column(name = ENTITY_TYPE_PROPERTY) - private EntityType entityType; - @Column(name = ModelConstants.ENTITY_VIEW_KEYS_PROPERTY) private String keys; diff --git a/dao/src/main/resources/sql/schema.sql b/dao/src/main/resources/sql/schema.sql index b79b61f3af..ed3583cef3 100644 --- a/dao/src/main/resources/sql/schema.sql +++ b/dao/src/main/resources/sql/schema.sql @@ -254,14 +254,14 @@ CREATE TABLE IF NOT EXISTS rule_node ( CREATE TABLE IF NOT EXISTS entity_views ( id varchar(31) NOT NULL CONSTRAINT entity_view_pkey PRIMARY KEY, - additional_info varchar, + entity_id varchar(31), + entity_type varchar(255), + tenant_id varchar(31), customer_id varchar(31), + name varchar(255), keys varchar(255), ts_begin varchar(255), ts_end varchar(255), - name varchar(255), - entity_type varchar(255), search_text varchar(255), - entity_id varchar(31), - tenant_id varchar(31) + additional_info varchar ); From 8ecd9628ca0d983df07b18882e19c12a03f1130a Mon Sep 17 00:00:00 2001 From: viktorbasanets Date: Thu, 6 Sep 2018 10:08:39 +0300 Subject: [PATCH 41/41] The final modifications for passing preliminary tests --- .../thingsboard/server/dao/model/sql/EntityViewEntity.java | 4 ++-- dao/src/test/resources/sql/drop-all-tables.sql | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java index 1175fac864..9136717fe3 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java @@ -106,8 +106,8 @@ public class EntityViewEntity extends BaseSqlEntity implements Searc } catch (IOException e) { e.printStackTrace(); } - this.tsBegin = entityView.getTsBegin() != null ? String.valueOf(entityView.getTsBegin()) : ""; - this.tsEnd = entityView.getTsEnd() != null ? String.valueOf(entityView.getTsEnd()) : ""; + this.tsBegin = entityView.getTsBegin() != null ? String.valueOf(entityView.getTsBegin()) : "0"; + this.tsEnd = entityView.getTsEnd() != null ? String.valueOf(entityView.getTsEnd()) : "0"; this.searchText = entityView.getSearchText(); this.additionalInfo = entityView.getAdditionalInfo(); } diff --git a/dao/src/test/resources/sql/drop-all-tables.sql b/dao/src/test/resources/sql/drop-all-tables.sql index 23b6a56a47..ebc04b3938 100644 --- a/dao/src/test/resources/sql/drop-all-tables.sql +++ b/dao/src/test/resources/sql/drop-all-tables.sql @@ -18,4 +18,5 @@ DROP TABLE IF EXISTS user_credentials; DROP TABLE IF EXISTS widget_type; DROP TABLE IF EXISTS widgets_bundle; DROP TABLE IF EXISTS rule_node; -DROP TABLE IF EXISTS rule_chain; \ No newline at end of file +DROP TABLE IF EXISTS rule_chain; +DROP TABLE IF EXISTS entity_views; \ No newline at end of file