Refactoring DAO continue

This commit is contained in:
volodymyr-babak 2017-04-09 23:19:12 +03:00
parent d6965e9469
commit 9fbd7e5bc8
11 changed files with 71 additions and 65 deletions

View File

@ -15,14 +15,11 @@
*/
package org.thingsboard.server.common.data.rule;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
import lombok.ToString;
import org.thingsboard.server.common.data.SearchTextBased;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.RuleId;
import org.thingsboard.server.common.data.id.TenantId;
import com.fasterxml.jackson.databind.JsonNode;
import org.thingsboard.server.common.data.plugin.ComponentLifecycleState;
@Data

View File

@ -27,8 +27,10 @@ import com.google.common.base.Function;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.server.common.data.SearchTextBased;
import org.thingsboard.server.dao.model.BaseEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.model.SearchTextEntity;
import org.thingsboard.server.dao.model.wrapper.EntityResultSet;
import javax.annotation.Nullable;
@ -46,6 +48,10 @@ public abstract class CassandraAbstractModelDao<E extends BaseEntity<D>, D> exte
protected abstract String getColumnFamilyName();
protected boolean isSearchTextDao() {
return false;
}
protected Mapper<E> getMapper() {
return cluster.getMapper(getColumnFamilyClass());
}
@ -144,6 +150,9 @@ public abstract class CassandraAbstractModelDao<E extends BaseEntity<D>, D> exte
log.error("Can't create entity for domain object {}", domain, e);
throw new IllegalArgumentException("Can't create entity for domain object {" + domain + "}", e);
}
if (isSearchTextDao()) {
((SearchTextEntity) entity).setSearchText(((SearchTextEntity) entity).getSearchTextSource().toLowerCase());
}
log.debug("Saving entity {}", entity);
entity = saveWithResult(entity).getEntity();
return DaoUtil.getData(entity);

View File

@ -33,11 +33,9 @@ import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
@Slf4j
public abstract class CassandraAbstractSearchTextDao<E extends SearchTextEntity<D>, D> extends CassandraAbstractModelDao<E, D> {
public D save(D domain) {
entity.setSearchText(entity.getSearchTextSource().toLowerCase());
return super.save(entity);
@Override
protected boolean isSearchTextDao() {
return true;
}
protected List<E> findPageWithTextSearch(String searchView, List<Clause> clauses, TextPageLink pageLink) {

View File

@ -32,7 +32,7 @@ import java.util.UUID;
import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
public abstract class AbstractSearchTimeDao<E extends BaseEntity<D>, D> extends CassandraAbstractModelDao<E, D> {
public abstract class CassandraAbstractSearchTimeDao<E extends BaseEntity<D>, D> extends CassandraAbstractModelDao<E, D> {
protected List<E> findPageWithTimeSearch(String searchView, List<Clause> clauses, TimePageLink pageLink) {

View File

@ -45,7 +45,7 @@ import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
*/
@Component
@Slf4j
public class BaseComponentDescriptorDao extends CassandraAbstractSearchTextDao<ComponentDescriptorEntity, ComponentDescriptor> implements ComponentDescriptorDao {
public class CassandraBaseComponentDescriptorDao extends CassandraAbstractSearchTextDao<ComponentDescriptorEntity, ComponentDescriptor> implements ComponentDescriptorDao {
@Override
protected Class<ComponentDescriptorEntity> getColumnFamilyClass() {

View File

@ -23,7 +23,9 @@ import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.TenantId;
@ -33,9 +35,7 @@ import org.thingsboard.server.common.data.security.DeviceCredentials;
import org.thingsboard.server.common.data.security.DeviceCredentialsType;
import org.thingsboard.server.dao.customer.CustomerDao;
import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.model.CustomerEntity;
import org.thingsboard.server.dao.model.DeviceEntity;
import org.thingsboard.server.dao.model.TenantEntity;
import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.service.PaginatedRemover;
import org.thingsboard.server.dao.tenant.TenantDao;
@ -43,9 +43,13 @@ import org.thingsboard.server.dao.tenant.TenantDao;
import java.util.List;
import java.util.Optional;
import static org.thingsboard.server.dao.DaoUtil.*;
import static org.thingsboard.server.dao.DaoUtil.convertDataList;
import static org.thingsboard.server.dao.DaoUtil.getData;
import static org.thingsboard.server.dao.DaoUtil.toUUIDs;
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
import static org.thingsboard.server.dao.service.Validator.*;
import static org.thingsboard.server.dao.service.Validator.validateId;
import static org.thingsboard.server.dao.service.Validator.validateIds;
import static org.thingsboard.server.dao.service.Validator.validatePageLink;
@Service
@Slf4j
@ -74,17 +78,16 @@ public class DeviceServiceImpl implements DeviceService {
public ListenableFuture<Device> findDeviceByIdAsync(DeviceId deviceId) {
log.trace("Executing findDeviceById [{}]", deviceId);
validateId(deviceId, "Incorrect deviceId " + deviceId);
ListenableFuture<Device> deviceEntity = deviceDao.findByIdAsync(deviceId.getId());
return Futures.transform(deviceEntity, (Function<? super DeviceEntity, ? extends Device>) input -> getData(input));
return deviceDao.findByIdAsync(deviceId.getId());
}
@Override
public Optional<Device> findDeviceByTenantIdAndName(TenantId tenantId, String name) {
log.trace("Executing findDeviceByTenantIdAndName [{}][{}]", tenantId, name);
validateId(tenantId, "Incorrect tenantId " + tenantId);
Optional<Device> deviceEntityOpt = deviceDao.findDevicesByTenantIdAndName(tenantId.getId(), name);
if (deviceEntityOpt.isPresent()) {
return Optional.of(deviceEntityOpt.get());
Optional<Device> deviceOpt = deviceDao.findDevicesByTenantIdAndName(tenantId.getId(), name);
if (deviceOpt.isPresent()) {
return Optional.of(deviceOpt.get());
} else {
return Optional.empty();
}
@ -136,7 +139,7 @@ public class DeviceServiceImpl implements DeviceService {
validateId(tenantId, "Incorrect tenantId " + tenantId);
validatePageLink(pageLink, "Incorrect page link " + pageLink);
List<Device> devices = deviceDao.findDevicesByTenantId(tenantId.getId(), pageLink);
return new TextPageData<Device>(devices, pageLink);
return new TextPageData<>(devices, pageLink);
}
@Override
@ -144,8 +147,7 @@ public class DeviceServiceImpl implements DeviceService {
log.trace("Executing findDevicesByTenantIdAndIdsAsync, tenantId [{}], deviceIds [{}]", tenantId, deviceIds);
validateId(tenantId, "Incorrect tenantId " + tenantId);
validateIds(deviceIds, "Incorrect deviceIds " + deviceIds);
ListenableFuture<List<Device>> devices = deviceDao.findDevicesByTenantIdAndIdsAsync(tenantId.getId(), toUUIDs(deviceIds));
return Futures.transform(deviceEntities, (Function<List<DeviceEntity>, List<Device>>) input -> convertDataList(input));
return deviceDao.findDevicesByTenantIdAndIdsAsync(tenantId.getId(), toUUIDs(deviceIds));
}
@ -163,7 +165,7 @@ public class DeviceServiceImpl implements DeviceService {
validateId(customerId, "Incorrect customerId " + customerId);
validatePageLink(pageLink, "Incorrect page link " + pageLink);
List<Device> devices = deviceDao.findDevicesByTenantIdAndCustomerId(tenantId.getId(), customerId.getId(), pageLink);
return new TextPageData<Device>(devices, pageLink);
return new TextPageData<>(devices, pageLink);
}
@Override
@ -172,9 +174,8 @@ public class DeviceServiceImpl implements DeviceService {
validateId(tenantId, "Incorrect tenantId " + tenantId);
validateId(customerId, "Incorrect customerId " + customerId);
validateIds(deviceIds, "Incorrect deviceIds " + deviceIds);
ListenableFuture<List<DeviceEntity>> deviceEntities = deviceDao.findDevicesByTenantIdCustomerIdAndIdsAsync(tenantId.getId(),
return deviceDao.findDevicesByTenantIdCustomerIdAndIdsAsync(tenantId.getId(),
customerId.getId(), toUUIDs(deviceIds));
return Futures.transform(deviceEntities, (Function<List<DeviceEntity>, List<Device>>) input -> convertDataList(input));
}
@Override
@ -201,7 +202,7 @@ public class DeviceServiceImpl implements DeviceService {
protected void validateUpdate(Device device) {
deviceDao.findDevicesByTenantIdAndName(device.getTenantId().getId(), device.getName()).ifPresent(
d -> {
if (!d.getId().equals(device.getUuidId())) {
if (!d.getUuidId().equals(device.getUuidId())) {
throw new DataValidationException("Device with such name already exists!");
}
}
@ -216,7 +217,7 @@ public class DeviceServiceImpl implements DeviceService {
if (device.getTenantId() == null) {
throw new DataValidationException("Device should be assigned to tenant!");
} else {
TenantEntity tenant = tenantDao.findById(device.getTenantId().getId());
Tenant tenant = tenantDao.findById(device.getTenantId().getId());
if (tenant == null) {
throw new DataValidationException("Device is referencing to non-existent tenant!");
}
@ -224,32 +225,32 @@ public class DeviceServiceImpl implements DeviceService {
if (device.getCustomerId() == null) {
device.setCustomerId(new CustomerId(NULL_UUID));
} else if (!device.getCustomerId().getId().equals(NULL_UUID)) {
CustomerEntity customer = customerDao.findById(device.getCustomerId().getId());
Customer customer = customerDao.findById(device.getCustomerId().getId());
if (customer == null) {
throw new DataValidationException("Can't assign device to non-existent customer!");
}
if (!customer.getTenantId().equals(device.getTenantId().getId())) {
if (!customer.getTenantId().getId().equals(device.getTenantId().getId())) {
throw new DataValidationException("Can't assign device to customer from different tenant!");
}
}
}
};
private PaginatedRemover<TenantId, DeviceEntity> tenantDevicesRemover =
new PaginatedRemover<TenantId, DeviceEntity>() {
private PaginatedRemover<TenantId, Device> tenantDevicesRemover =
new PaginatedRemover<TenantId, Device>() {
@Override
protected List<DeviceEntity> findEntities(TenantId id, TextPageLink pageLink) {
protected List<Device> findEntities(TenantId id, TextPageLink pageLink) {
return deviceDao.findDevicesByTenantId(id.getId(), pageLink);
}
@Override
protected void removeEntity(DeviceEntity entity) {
deleteDevice(new DeviceId(entity.getId()));
protected void removeEntity(Device entity) {
deleteDevice(new DeviceId(entity.getUuidId()));
}
};
class CustomerDevicesUnassigner extends PaginatedRemover<CustomerId, DeviceEntity> {
private class CustomerDevicesUnassigner extends PaginatedRemover<CustomerId, Device> {
private TenantId tenantId;
@ -258,13 +259,13 @@ public class DeviceServiceImpl implements DeviceService {
}
@Override
protected List<DeviceEntity> findEntities(CustomerId id, TextPageLink pageLink) {
protected List<Device> findEntities(CustomerId id, TextPageLink pageLink) {
return deviceDao.findDevicesByTenantIdAndCustomerId(tenantId.getId(), id.getId(), pageLink);
}
@Override
protected void removeEntity(DeviceEntity entity) {
unassignDeviceFromCustomer(new DeviceId(entity.getId()));
protected void removeEntity(Device entity) {
unassignDeviceFromCustomer(new DeviceId(entity.getUuidId()));
}
}

View File

@ -25,7 +25,7 @@ import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.Event;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.page.TimePageLink;
import org.thingsboard.server.dao.AbstractSearchTimeDao;
import org.thingsboard.server.dao.CassandraAbstractSearchTimeDao;
import org.thingsboard.server.dao.DaoUtil;
import org.thingsboard.server.dao.model.EventEntity;
import org.thingsboard.server.dao.model.ModelConstants;
@ -41,7 +41,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.*;
@Component
@Slf4j
public class CassandraBaseEventDao extends AbstractSearchTimeDao<EventEntity, Event> implements EventDao {
public class CassandraBaseEventDao extends CassandraAbstractSearchTimeDao<EventEntity, Event> implements EventDao {
@Override
protected Class<EventEntity> getColumnFamilyClass() {

View File

@ -17,22 +17,22 @@ package org.thingsboard.server.dao.exception;
public class DatabaseException extends RuntimeException {
private static final long serialVersionUID = 3463762014441887103L;
private static final long serialVersionUID = 3463762014441887103L;
public DatabaseException() {
super();
}
public DatabaseException() {
super();
}
public DatabaseException(String message, Throwable cause) {
super(message, cause);
}
public DatabaseException(String message, Throwable cause) {
super(message, cause);
}
public DatabaseException(String message) {
super(message);
}
public DatabaseException(String message) {
super(message);
}
public DatabaseException(Throwable cause) {
super(cause);
}
public DatabaseException(Throwable cause) {
super(cause);
}
}

View File

@ -18,9 +18,9 @@ package org.thingsboard.server.dao.exception;
public class IncorrectParameterException extends RuntimeException {
private static final long serialVersionUID = 601995650578985289L;
private static final long serialVersionUID = 601995650578985289L;
public IncorrectParameterException(String message) {
public IncorrectParameterException(String message) {
super(message);
}

View File

@ -1,12 +1,12 @@
/**
* Copyright © 2016-2017 The Thingsboard Authors
* <p>
*
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
*
* 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.

View File

@ -36,11 +36,13 @@ import org.thingsboard.server.common.data.security.DeviceCredentialsType;
import org.thingsboard.server.dao.device.DeviceCredentialsDao;
import org.thingsboard.server.dao.device.DeviceCredentialsService;
import org.thingsboard.server.dao.device.DeviceService;
import org.thingsboard.server.dao.model.DeviceCredentialsEntity;
import java.util.UUID;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@TestPropertySource(properties = {"cache.enabled = true"})
public class DeviceCredentialsCacheTest extends AbstractServiceTest {
@ -140,9 +142,8 @@ public class DeviceCredentialsCacheTest extends AbstractServiceTest {
return null;
}
private DeviceCredentialsEntity createDummyDeviceCredentialsEntity(String deviceCredentialsId) {
DeviceCredentialsEntity result = new DeviceCredentialsEntity();
result.setId(UUIDs.timeBased());
private DeviceCredentials createDummyDeviceCredentialsEntity(String deviceCredentialsId) {
DeviceCredentials result = new DeviceCredentials(new DeviceCredentialsId(UUIDs.timeBased()));
result.setCredentialsId(deviceCredentialsId);
return result;
}