Browse Source

Cleanup

pull/2663/head
Igor Kulikov 7 years ago
parent
commit
15a31044b2
  1. 22
      common/dao-api/src/main/java/org/thingsboard/server/dao/util/NoSqlDao.java
  2. 34
      dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java
  3. 30
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java
  4. 34
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java
  5. 12
      dao/src/main/java/org/thingsboard/server/dao/entity/AbstractEntityService.java

22
common/dao-api/src/main/java/org/thingsboard/server/dao/util/NoSqlDao.java

@ -1,22 +0,0 @@
/**
* Copyright © 2016-2020 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.util;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ConditionalOnProperty(prefix = "database.entities", value = "type", havingValue = "cassandra")
public @interface NoSqlDao {
}

34
dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java

@ -126,18 +126,14 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
log.trace("Executing saveAsset [{}]", asset);
assetValidator.validate(asset, Asset::getTenantId);
Asset savedAsset;
if (!sqlDatabaseUsed) {
try {
savedAsset = assetDao.save(asset.getTenantId(), asset);
} else {
try {
savedAsset = assetDao.save(asset.getTenantId(), asset);
} catch (Exception t) {
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("asset_name_unq_key")) {
throw new DataValidationException("Asset with such name already exists!");
} else {
throw t;
}
} catch (Exception t) {
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("asset_name_unq_key")) {
throw new DataValidationException("Asset with such name already exists!");
} else {
throw t;
}
}
return savedAsset;
@ -324,26 +320,10 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
@Override
protected void validateCreate(TenantId tenantId, Asset asset) {
if (!sqlDatabaseUsed) {
assetDao.findAssetsByTenantIdAndName(asset.getTenantId().getId(), asset.getName()).ifPresent(
d -> {
throw new DataValidationException("Asset with such name already exists!");
}
);
}
}
@Override
protected void validateUpdate(TenantId tenantId, Asset asset) {
if (!sqlDatabaseUsed) {
assetDao.findAssetsByTenantIdAndName(asset.getTenantId().getId(), asset.getName()).ifPresent(
d -> {
if (!d.getId().equals(asset.getId())) {
throw new DataValidationException("Asset with such name already exists!");
}
}
);
}
}
@Override

30
dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java

@ -80,18 +80,14 @@ public class DeviceCredentialsServiceImpl extends AbstractEntityService implemen
}
log.trace("Executing updateDeviceCredentials [{}]", deviceCredentials);
credentialsValidator.validate(deviceCredentials, id -> tenantId);
if (!sqlDatabaseUsed) {
try {
return deviceCredentialsDao.save(tenantId, deviceCredentials);
} else {
try {
return deviceCredentialsDao.save(tenantId, deviceCredentials);
} catch (Exception t) {
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_credentials_id_unq_key")) {
throw new DataValidationException("Specified credentials are already registered!");
} else {
throw t;
}
} catch (Exception t) {
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_credentials_id_unq_key")) {
throw new DataValidationException("Specified credentials are already registered!");
} else {
throw t;
}
}
}
@ -115,12 +111,6 @@ public class DeviceCredentialsServiceImpl extends AbstractEntityService implemen
@Override
protected void validateCreate(TenantId tenantId, DeviceCredentials deviceCredentials) {
if (!sqlDatabaseUsed) {
DeviceCredentials existingCredentialsEntity = deviceCredentialsDao.findByCredentialsId(tenantId, deviceCredentials.getCredentialsId());
if (existingCredentialsEntity != null) {
throw new DataValidationException("Create of existent device credentials!");
}
}
}
@Override
@ -129,12 +119,6 @@ public class DeviceCredentialsServiceImpl extends AbstractEntityService implemen
if (existingCredentials == null) {
throw new DataValidationException("Unable to update non-existent device credentials!");
}
if (!sqlDatabaseUsed) {
DeviceCredentials sameCredentialsId = deviceCredentialsDao.findByCredentialsId(tenantId, deviceCredentials.getCredentialsId());
if (sameCredentialsId != null && !sameCredentialsId.getUuidId().equals(deviceCredentials.getUuidId())) {
throw new DataValidationException("Specified credentials are already registered!");
}
}
}
@Override

34
dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java

@ -144,18 +144,14 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
log.trace("Executing saveDevice [{}]", device);
deviceValidator.validate(device, Device::getTenantId);
Device savedDevice;
if (!sqlDatabaseUsed) {
try {
savedDevice = deviceDao.save(device.getTenantId(), device);
} else {
try {
savedDevice = deviceDao.save(device.getTenantId(), device);
} catch (Exception t) {
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_name_unq_key")) {
throw new DataValidationException("Device with such name already exists!");
} else {
throw t;
}
} catch (Exception t) {
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_name_unq_key")) {
throw new DataValidationException("Device with such name already exists!");
} else {
throw t;
}
}
if (device.getId() == null) {
@ -362,26 +358,10 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
@Override
protected void validateCreate(TenantId tenantId, Device device) {
if (!sqlDatabaseUsed) {
deviceDao.findDeviceByTenantIdAndName(device.getTenantId().getId(), device.getName()).ifPresent(
d -> {
throw new DataValidationException("Device with such name already exists!");
}
);
}
}
@Override
protected void validateUpdate(TenantId tenantId, Device device) {
if (!sqlDatabaseUsed) {
deviceDao.findDeviceByTenantIdAndName(device.getTenantId().getId(), device.getName()).ifPresent(
d -> {
if (!d.getUuidId().equals(device.getUuidId())) {
throw new DataValidationException("Device with such name already exists!");
}
}
);
}
}
@Override

12
dao/src/main/java/org/thingsboard/server/dao/entity/AbstractEntityService.java

@ -18,12 +18,10 @@ package org.thingsboard.server.dao.entity;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.dao.relation.RelationService;
import javax.annotation.PostConstruct;
import java.util.Optional;
@Slf4j
@ -32,16 +30,6 @@ public abstract class AbstractEntityService {
@Autowired
protected RelationService relationService;
@Value("${database.entities.type:sql}")
private String databaseType;
protected boolean sqlDatabaseUsed;
@PostConstruct
public void init() {
sqlDatabaseUsed = "sql".equalsIgnoreCase(databaseType);
}
protected void deleteEntityRelations(TenantId tenantId, EntityId entityId) {
log.trace("Executing deleteEntityRelations [{}]", entityId);
relationService.deleteEntityRelations(tenantId, entityId);

Loading…
Cancel
Save