Commit 9fbd7e5bc8d73e031b5f0f130181605bc415f17b

Authored by volodymyr-babak
1 parent d6965e94

Refactoring DAO continue

... ... @@ -15,14 +15,11 @@
15 15 */
16 16 package org.thingsboard.server.common.data.rule;
17 17
  18 +import com.fasterxml.jackson.databind.JsonNode;
18 19 import lombok.Data;
19   -import lombok.ToString;
20 20 import org.thingsboard.server.common.data.SearchTextBased;
21   -import org.thingsboard.server.common.data.id.CustomerId;
22 21 import org.thingsboard.server.common.data.id.RuleId;
23 22 import org.thingsboard.server.common.data.id.TenantId;
24   -
25   -import com.fasterxml.jackson.databind.JsonNode;
26 23 import org.thingsboard.server.common.data.plugin.ComponentLifecycleState;
27 24
28 25 @Data
... ...
... ... @@ -27,8 +27,10 @@ import com.google.common.base.Function;
27 27 import com.google.common.util.concurrent.Futures;
28 28 import com.google.common.util.concurrent.ListenableFuture;
29 29 import lombok.extern.slf4j.Slf4j;
  30 +import org.thingsboard.server.common.data.SearchTextBased;
30 31 import org.thingsboard.server.dao.model.BaseEntity;
31 32 import org.thingsboard.server.dao.model.ModelConstants;
  33 +import org.thingsboard.server.dao.model.SearchTextEntity;
32 34 import org.thingsboard.server.dao.model.wrapper.EntityResultSet;
33 35
34 36 import javax.annotation.Nullable;
... ... @@ -46,6 +48,10 @@ public abstract class CassandraAbstractModelDao<E extends BaseEntity<D>, D> exte
46 48
47 49 protected abstract String getColumnFamilyName();
48 50
  51 + protected boolean isSearchTextDao() {
  52 + return false;
  53 + }
  54 +
49 55 protected Mapper<E> getMapper() {
50 56 return cluster.getMapper(getColumnFamilyClass());
51 57 }
... ... @@ -144,6 +150,9 @@ public abstract class CassandraAbstractModelDao<E extends BaseEntity<D>, D> exte
144 150 log.error("Can't create entity for domain object {}", domain, e);
145 151 throw new IllegalArgumentException("Can't create entity for domain object {" + domain + "}", e);
146 152 }
  153 + if (isSearchTextDao()) {
  154 + ((SearchTextEntity) entity).setSearchText(((SearchTextEntity) entity).getSearchTextSource().toLowerCase());
  155 + }
147 156 log.debug("Saving entity {}", entity);
148 157 entity = saveWithResult(entity).getEntity();
149 158 return DaoUtil.getData(entity);
... ...
... ... @@ -33,11 +33,9 @@ import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
33 33 @Slf4j
34 34 public abstract class CassandraAbstractSearchTextDao<E extends SearchTextEntity<D>, D> extends CassandraAbstractModelDao<E, D> {
35 35
36   - public D save(D domain) {
37   -
38   - entity.setSearchText(entity.getSearchTextSource().toLowerCase());
39   -
40   - return super.save(entity);
  36 + @Override
  37 + protected boolean isSearchTextDao() {
  38 + return true;
41 39 }
42 40
43 41 protected List<E> findPageWithTextSearch(String searchView, List<Clause> clauses, TextPageLink pageLink) {
... ...
dao/src/main/java/org/thingsboard/server/dao/CassandraAbstractSearchTimeDao.java renamed from dao/src/main/java/org/thingsboard/server/dao/AbstractSearchTimeDao.java
... ... @@ -32,7 +32,7 @@ import java.util.UUID;
32 32
33 33 import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
34 34
35   -public abstract class AbstractSearchTimeDao<E extends BaseEntity<D>, D> extends CassandraAbstractModelDao<E, D> {
  35 +public abstract class CassandraAbstractSearchTimeDao<E extends BaseEntity<D>, D> extends CassandraAbstractModelDao<E, D> {
36 36
37 37
38 38 protected List<E> findPageWithTimeSearch(String searchView, List<Clause> clauses, TimePageLink pageLink) {
... ...
dao/src/main/java/org/thingsboard/server/dao/component/CassandraBaseComponentDescriptorDao.java renamed from dao/src/main/java/org/thingsboard/server/dao/component/BaseComponentDescriptorDao.java
... ... @@ -45,7 +45,7 @@ import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
45 45 */
46 46 @Component
47 47 @Slf4j
48   -public class BaseComponentDescriptorDao extends CassandraAbstractSearchTextDao<ComponentDescriptorEntity, ComponentDescriptor> implements ComponentDescriptorDao {
  48 +public class CassandraBaseComponentDescriptorDao extends CassandraAbstractSearchTextDao<ComponentDescriptorEntity, ComponentDescriptor> implements ComponentDescriptorDao {
49 49
50 50 @Override
51 51 protected Class<ComponentDescriptorEntity> getColumnFamilyClass() {
... ...
... ... @@ -23,7 +23,9 @@ import org.apache.commons.lang3.RandomStringUtils;
23 23 import org.springframework.beans.factory.annotation.Autowired;
24 24 import org.springframework.stereotype.Service;
25 25 import org.springframework.util.StringUtils;
  26 +import org.thingsboard.server.common.data.Customer;
26 27 import org.thingsboard.server.common.data.Device;
  28 +import org.thingsboard.server.common.data.Tenant;
27 29 import org.thingsboard.server.common.data.id.CustomerId;
28 30 import org.thingsboard.server.common.data.id.DeviceId;
29 31 import org.thingsboard.server.common.data.id.TenantId;
... ... @@ -33,9 +35,7 @@ import org.thingsboard.server.common.data.security.DeviceCredentials;
33 35 import org.thingsboard.server.common.data.security.DeviceCredentialsType;
34 36 import org.thingsboard.server.dao.customer.CustomerDao;
35 37 import org.thingsboard.server.dao.exception.DataValidationException;
36   -import org.thingsboard.server.dao.model.CustomerEntity;
37 38 import org.thingsboard.server.dao.model.DeviceEntity;
38   -import org.thingsboard.server.dao.model.TenantEntity;
39 39 import org.thingsboard.server.dao.service.DataValidator;
40 40 import org.thingsboard.server.dao.service.PaginatedRemover;
41 41 import org.thingsboard.server.dao.tenant.TenantDao;
... ... @@ -43,9 +43,13 @@ import org.thingsboard.server.dao.tenant.TenantDao;
43 43 import java.util.List;
44 44 import java.util.Optional;
45 45
46   -import static org.thingsboard.server.dao.DaoUtil.*;
  46 +import static org.thingsboard.server.dao.DaoUtil.convertDataList;
  47 +import static org.thingsboard.server.dao.DaoUtil.getData;
  48 +import static org.thingsboard.server.dao.DaoUtil.toUUIDs;
47 49 import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
48   -import static org.thingsboard.server.dao.service.Validator.*;
  50 +import static org.thingsboard.server.dao.service.Validator.validateId;
  51 +import static org.thingsboard.server.dao.service.Validator.validateIds;
  52 +import static org.thingsboard.server.dao.service.Validator.validatePageLink;
49 53
50 54 @Service
51 55 @Slf4j
... ... @@ -74,17 +78,16 @@ public class DeviceServiceImpl implements DeviceService {
74 78 public ListenableFuture<Device> findDeviceByIdAsync(DeviceId deviceId) {
75 79 log.trace("Executing findDeviceById [{}]", deviceId);
76 80 validateId(deviceId, "Incorrect deviceId " + deviceId);
77   - ListenableFuture<Device> deviceEntity = deviceDao.findByIdAsync(deviceId.getId());
78   - return Futures.transform(deviceEntity, (Function<? super DeviceEntity, ? extends Device>) input -> getData(input));
  81 + return deviceDao.findByIdAsync(deviceId.getId());
79 82 }
80 83
81 84 @Override
82 85 public Optional<Device> findDeviceByTenantIdAndName(TenantId tenantId, String name) {
83 86 log.trace("Executing findDeviceByTenantIdAndName [{}][{}]", tenantId, name);
84 87 validateId(tenantId, "Incorrect tenantId " + tenantId);
85   - Optional<Device> deviceEntityOpt = deviceDao.findDevicesByTenantIdAndName(tenantId.getId(), name);
86   - if (deviceEntityOpt.isPresent()) {
87   - return Optional.of(deviceEntityOpt.get());
  88 + Optional<Device> deviceOpt = deviceDao.findDevicesByTenantIdAndName(tenantId.getId(), name);
  89 + if (deviceOpt.isPresent()) {
  90 + return Optional.of(deviceOpt.get());
88 91 } else {
89 92 return Optional.empty();
90 93 }
... ... @@ -136,7 +139,7 @@ public class DeviceServiceImpl implements DeviceService {
136 139 validateId(tenantId, "Incorrect tenantId " + tenantId);
137 140 validatePageLink(pageLink, "Incorrect page link " + pageLink);
138 141 List<Device> devices = deviceDao.findDevicesByTenantId(tenantId.getId(), pageLink);
139   - return new TextPageData<Device>(devices, pageLink);
  142 + return new TextPageData<>(devices, pageLink);
140 143 }
141 144
142 145 @Override
... ... @@ -144,8 +147,7 @@ public class DeviceServiceImpl implements DeviceService {
144 147 log.trace("Executing findDevicesByTenantIdAndIdsAsync, tenantId [{}], deviceIds [{}]", tenantId, deviceIds);
145 148 validateId(tenantId, "Incorrect tenantId " + tenantId);
146 149 validateIds(deviceIds, "Incorrect deviceIds " + deviceIds);
147   - ListenableFuture<List<Device>> devices = deviceDao.findDevicesByTenantIdAndIdsAsync(tenantId.getId(), toUUIDs(deviceIds));
148   - return Futures.transform(deviceEntities, (Function<List<DeviceEntity>, List<Device>>) input -> convertDataList(input));
  150 + return deviceDao.findDevicesByTenantIdAndIdsAsync(tenantId.getId(), toUUIDs(deviceIds));
149 151 }
150 152
151 153
... ... @@ -163,7 +165,7 @@ public class DeviceServiceImpl implements DeviceService {
163 165 validateId(customerId, "Incorrect customerId " + customerId);
164 166 validatePageLink(pageLink, "Incorrect page link " + pageLink);
165 167 List<Device> devices = deviceDao.findDevicesByTenantIdAndCustomerId(tenantId.getId(), customerId.getId(), pageLink);
166   - return new TextPageData<Device>(devices, pageLink);
  168 + return new TextPageData<>(devices, pageLink);
167 169 }
168 170
169 171 @Override
... ... @@ -172,9 +174,8 @@ public class DeviceServiceImpl implements DeviceService {
172 174 validateId(tenantId, "Incorrect tenantId " + tenantId);
173 175 validateId(customerId, "Incorrect customerId " + customerId);
174 176 validateIds(deviceIds, "Incorrect deviceIds " + deviceIds);
175   - ListenableFuture<List<DeviceEntity>> deviceEntities = deviceDao.findDevicesByTenantIdCustomerIdAndIdsAsync(tenantId.getId(),
  177 + return deviceDao.findDevicesByTenantIdCustomerIdAndIdsAsync(tenantId.getId(),
176 178 customerId.getId(), toUUIDs(deviceIds));
177   - return Futures.transform(deviceEntities, (Function<List<DeviceEntity>, List<Device>>) input -> convertDataList(input));
178 179 }
179 180
180 181 @Override
... ... @@ -201,7 +202,7 @@ public class DeviceServiceImpl implements DeviceService {
201 202 protected void validateUpdate(Device device) {
202 203 deviceDao.findDevicesByTenantIdAndName(device.getTenantId().getId(), device.getName()).ifPresent(
203 204 d -> {
204   - if (!d.getId().equals(device.getUuidId())) {
  205 + if (!d.getUuidId().equals(device.getUuidId())) {
205 206 throw new DataValidationException("Device with such name already exists!");
206 207 }
207 208 }
... ... @@ -216,7 +217,7 @@ public class DeviceServiceImpl implements DeviceService {
216 217 if (device.getTenantId() == null) {
217 218 throw new DataValidationException("Device should be assigned to tenant!");
218 219 } else {
219   - TenantEntity tenant = tenantDao.findById(device.getTenantId().getId());
  220 + Tenant tenant = tenantDao.findById(device.getTenantId().getId());
220 221 if (tenant == null) {
221 222 throw new DataValidationException("Device is referencing to non-existent tenant!");
222 223 }
... ... @@ -224,32 +225,32 @@ public class DeviceServiceImpl implements DeviceService {
224 225 if (device.getCustomerId() == null) {
225 226 device.setCustomerId(new CustomerId(NULL_UUID));
226 227 } else if (!device.getCustomerId().getId().equals(NULL_UUID)) {
227   - CustomerEntity customer = customerDao.findById(device.getCustomerId().getId());
  228 + Customer customer = customerDao.findById(device.getCustomerId().getId());
228 229 if (customer == null) {
229 230 throw new DataValidationException("Can't assign device to non-existent customer!");
230 231 }
231   - if (!customer.getTenantId().equals(device.getTenantId().getId())) {
  232 + if (!customer.getTenantId().getId().equals(device.getTenantId().getId())) {
232 233 throw new DataValidationException("Can't assign device to customer from different tenant!");
233 234 }
234 235 }
235 236 }
236 237 };
237 238
238   - private PaginatedRemover<TenantId, DeviceEntity> tenantDevicesRemover =
239   - new PaginatedRemover<TenantId, DeviceEntity>() {
  239 + private PaginatedRemover<TenantId, Device> tenantDevicesRemover =
  240 + new PaginatedRemover<TenantId, Device>() {
240 241
241 242 @Override
242   - protected List<DeviceEntity> findEntities(TenantId id, TextPageLink pageLink) {
  243 + protected List<Device> findEntities(TenantId id, TextPageLink pageLink) {
243 244 return deviceDao.findDevicesByTenantId(id.getId(), pageLink);
244 245 }
245 246
246 247 @Override
247   - protected void removeEntity(DeviceEntity entity) {
248   - deleteDevice(new DeviceId(entity.getId()));
  248 + protected void removeEntity(Device entity) {
  249 + deleteDevice(new DeviceId(entity.getUuidId()));
249 250 }
250 251 };
251 252
252   - class CustomerDevicesUnassigner extends PaginatedRemover<CustomerId, DeviceEntity> {
  253 + private class CustomerDevicesUnassigner extends PaginatedRemover<CustomerId, Device> {
253 254
254 255 private TenantId tenantId;
255 256
... ... @@ -258,13 +259,13 @@ public class DeviceServiceImpl implements DeviceService {
258 259 }
259 260
260 261 @Override
261   - protected List<DeviceEntity> findEntities(CustomerId id, TextPageLink pageLink) {
  262 + protected List<Device> findEntities(CustomerId id, TextPageLink pageLink) {
262 263 return deviceDao.findDevicesByTenantIdAndCustomerId(tenantId.getId(), id.getId(), pageLink);
263 264 }
264 265
265 266 @Override
266   - protected void removeEntity(DeviceEntity entity) {
267   - unassignDeviceFromCustomer(new DeviceId(entity.getId()));
  267 + protected void removeEntity(Device entity) {
  268 + unassignDeviceFromCustomer(new DeviceId(entity.getUuidId()));
268 269 }
269 270
270 271 }
... ...
... ... @@ -25,7 +25,7 @@ import org.springframework.stereotype.Component;
25 25 import org.thingsboard.server.common.data.Event;
26 26 import org.thingsboard.server.common.data.id.EntityId;
27 27 import org.thingsboard.server.common.data.page.TimePageLink;
28   -import org.thingsboard.server.dao.AbstractSearchTimeDao;
  28 +import org.thingsboard.server.dao.CassandraAbstractSearchTimeDao;
29 29 import org.thingsboard.server.dao.DaoUtil;
30 30 import org.thingsboard.server.dao.model.EventEntity;
31 31 import org.thingsboard.server.dao.model.ModelConstants;
... ... @@ -41,7 +41,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.*;
41 41
42 42 @Component
43 43 @Slf4j
44   -public class CassandraBaseEventDao extends AbstractSearchTimeDao<EventEntity, Event> implements EventDao {
  44 +public class CassandraBaseEventDao extends CassandraAbstractSearchTimeDao<EventEntity, Event> implements EventDao {
45 45
46 46 @Override
47 47 protected Class<EventEntity> getColumnFamilyClass() {
... ...
... ... @@ -17,22 +17,22 @@ package org.thingsboard.server.dao.exception;
17 17
18 18 public class DatabaseException extends RuntimeException {
19 19
20   - private static final long serialVersionUID = 3463762014441887103L;
  20 + private static final long serialVersionUID = 3463762014441887103L;
21 21
22   - public DatabaseException() {
23   - super();
24   - }
  22 + public DatabaseException() {
  23 + super();
  24 + }
25 25
26   - public DatabaseException(String message, Throwable cause) {
27   - super(message, cause);
28   - }
  26 + public DatabaseException(String message, Throwable cause) {
  27 + super(message, cause);
  28 + }
29 29
30   - public DatabaseException(String message) {
31   - super(message);
32   - }
  30 + public DatabaseException(String message) {
  31 + super(message);
  32 + }
33 33
34   - public DatabaseException(Throwable cause) {
35   - super(cause);
36   - }
  34 + public DatabaseException(Throwable cause) {
  35 + super(cause);
  36 + }
37 37
38 38 }
... ...
... ... @@ -18,9 +18,9 @@ package org.thingsboard.server.dao.exception;
18 18
19 19 public class IncorrectParameterException extends RuntimeException {
20 20
21   - private static final long serialVersionUID = 601995650578985289L;
  21 + private static final long serialVersionUID = 601995650578985289L;
22 22
23   - public IncorrectParameterException(String message) {
  23 + public IncorrectParameterException(String message) {
24 24 super(message);
25 25 }
26 26
... ...
1 1 /**
2 2 * Copyright © 2016-2017 The Thingsboard Authors
3   - * <p>
  3 + *
4 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 5 * you may not use this file except in compliance with the License.
6 6 * You may obtain a copy of the License at
7   - * <p>
8   - * http://www.apache.org/licenses/LICENSE-2.0
9   - * <p>
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
10 10 * Unless required by applicable law or agreed to in writing, software
11 11 * distributed under the License is distributed on an "AS IS" BASIS,
12 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
... ...
... ... @@ -36,11 +36,13 @@ import org.thingsboard.server.common.data.security.DeviceCredentialsType;
36 36 import org.thingsboard.server.dao.device.DeviceCredentialsDao;
37 37 import org.thingsboard.server.dao.device.DeviceCredentialsService;
38 38 import org.thingsboard.server.dao.device.DeviceService;
39   -import org.thingsboard.server.dao.model.DeviceCredentialsEntity;
40 39
41 40 import java.util.UUID;
42 41
43   -import static org.mockito.Mockito.*;
  42 +import static org.mockito.Mockito.mock;
  43 +import static org.mockito.Mockito.times;
  44 +import static org.mockito.Mockito.verify;
  45 +import static org.mockito.Mockito.when;
44 46
45 47 @TestPropertySource(properties = {"cache.enabled = true"})
46 48 public class DeviceCredentialsCacheTest extends AbstractServiceTest {
... ... @@ -140,9 +142,8 @@ public class DeviceCredentialsCacheTest extends AbstractServiceTest {
140 142 return null;
141 143 }
142 144
143   - private DeviceCredentialsEntity createDummyDeviceCredentialsEntity(String deviceCredentialsId) {
144   - DeviceCredentialsEntity result = new DeviceCredentialsEntity();
145   - result.setId(UUIDs.timeBased());
  145 + private DeviceCredentials createDummyDeviceCredentialsEntity(String deviceCredentialsId) {
  146 + DeviceCredentials result = new DeviceCredentials(new DeviceCredentialsId(UUIDs.timeBased()));
146 147 result.setCredentialsId(deviceCredentialsId);
147 148 return result;
148 149 }
... ...