Commit ca62616cccc77feccd69eb7c3526437b112dd11d
1 parent
02895bf3
Fixes for cases when asset/device deleted but has entity view assigned
Showing
2 changed files
with
36 additions
and
3 deletions
... | ... | @@ -30,6 +30,7 @@ import org.springframework.util.StringUtils; |
30 | 30 | import org.thingsboard.server.common.data.Customer; |
31 | 31 | import org.thingsboard.server.common.data.EntitySubtype; |
32 | 32 | import org.thingsboard.server.common.data.EntityType; |
33 | +import org.thingsboard.server.common.data.EntityView; | |
33 | 34 | import org.thingsboard.server.common.data.Tenant; |
34 | 35 | import org.thingsboard.server.common.data.asset.Asset; |
35 | 36 | import org.thingsboard.server.common.data.asset.AssetSearchQuery; |
... | ... | @@ -43,6 +44,7 @@ import org.thingsboard.server.common.data.relation.EntityRelation; |
43 | 44 | import org.thingsboard.server.common.data.relation.EntitySearchDirection; |
44 | 45 | import org.thingsboard.server.dao.customer.CustomerDao; |
45 | 46 | import org.thingsboard.server.dao.entity.AbstractEntityService; |
47 | +import org.thingsboard.server.dao.entityview.EntityViewService; | |
46 | 48 | import org.thingsboard.server.dao.exception.DataValidationException; |
47 | 49 | import org.thingsboard.server.dao.service.DataValidator; |
48 | 50 | import org.thingsboard.server.dao.service.PaginatedRemover; |
... | ... | @@ -77,6 +79,9 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ |
77 | 79 | private CustomerDao customerDao; |
78 | 80 | |
79 | 81 | @Autowired |
82 | + private EntityViewService entityViewService; | |
83 | + | |
84 | + @Autowired | |
80 | 85 | private CacheManager cacheManager; |
81 | 86 | |
82 | 87 | @Override |
... | ... | @@ -130,11 +135,21 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ |
130 | 135 | validateId(assetId, INCORRECT_ASSET_ID + assetId); |
131 | 136 | deleteEntityRelations(assetId); |
132 | 137 | |
133 | - Cache cache = cacheManager.getCache(ASSET_CACHE); | |
134 | 138 | Asset asset = assetDao.findById(assetId.getId()); |
139 | + try { | |
140 | + List<EntityView> entityViews = entityViewService.findEntityViewsByTenantIdAndEntityIdAsync(asset.getTenantId(), assetId).get(); | |
141 | + if (entityViews != null && !entityViews.isEmpty()) { | |
142 | + throw new DataValidationException("Can't delete asset that is assigned to entity views!"); | |
143 | + } | |
144 | + } catch (Exception e) { | |
145 | + log.error("Exception while finding entity views for assetId [{}]", assetId, e); | |
146 | + throw new RuntimeException("Exception while finding entity views for assetId [" + assetId + "]", e); | |
147 | + } | |
148 | + | |
135 | 149 | List<Object> list = new ArrayList<>(); |
136 | 150 | list.add(asset.getTenantId()); |
137 | 151 | list.add(asset.getName()); |
152 | + Cache cache = cacheManager.getCache(ASSET_CACHE); | |
138 | 153 | cache.evict(list); |
139 | 154 | |
140 | 155 | assetDao.removeById(assetId.getId()); | ... | ... |
... | ... | @@ -31,6 +31,7 @@ import org.thingsboard.server.common.data.Customer; |
31 | 31 | import org.thingsboard.server.common.data.Device; |
32 | 32 | import org.thingsboard.server.common.data.EntitySubtype; |
33 | 33 | import org.thingsboard.server.common.data.EntityType; |
34 | +import org.thingsboard.server.common.data.EntityView; | |
34 | 35 | import org.thingsboard.server.common.data.Tenant; |
35 | 36 | import org.thingsboard.server.common.data.device.DeviceSearchQuery; |
36 | 37 | import org.thingsboard.server.common.data.id.CustomerId; |
... | ... | @@ -45,6 +46,7 @@ import org.thingsboard.server.common.data.security.DeviceCredentials; |
45 | 46 | import org.thingsboard.server.common.data.security.DeviceCredentialsType; |
46 | 47 | import org.thingsboard.server.dao.customer.CustomerDao; |
47 | 48 | import org.thingsboard.server.dao.entity.AbstractEntityService; |
49 | +import org.thingsboard.server.dao.entityview.EntityViewService; | |
48 | 50 | import org.thingsboard.server.dao.exception.DataValidationException; |
49 | 51 | import org.thingsboard.server.dao.service.DataValidator; |
50 | 52 | import org.thingsboard.server.dao.service.PaginatedRemover; |
... | ... | @@ -87,6 +89,9 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
87 | 89 | private DeviceCredentialsService deviceCredentialsService; |
88 | 90 | |
89 | 91 | @Autowired |
92 | + private EntityViewService entityViewService; | |
93 | + | |
94 | + @Autowired | |
90 | 95 | private CacheManager cacheManager; |
91 | 96 | |
92 | 97 | @Override |
... | ... | @@ -145,18 +150,31 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
145 | 150 | @Override |
146 | 151 | public void deleteDevice(DeviceId deviceId) { |
147 | 152 | log.trace("Executing deleteDevice [{}]", deviceId); |
148 | - Cache cache = cacheManager.getCache(DEVICE_CACHE); | |
149 | 153 | validateId(deviceId, INCORRECT_DEVICE_ID + deviceId); |
154 | + | |
155 | + Device device = deviceDao.findById(deviceId.getId()); | |
156 | + try { | |
157 | + List<EntityView> entityViews = entityViewService.findEntityViewsByTenantIdAndEntityIdAsync(device.getTenantId(), deviceId).get(); | |
158 | + if (entityViews != null && !entityViews.isEmpty()) { | |
159 | + throw new DataValidationException("Can't delete device that is assigned to entity views!"); | |
160 | + } | |
161 | + } catch (Exception e) { | |
162 | + log.error("Exception while finding entity views for deviceId [{}]", deviceId, e); | |
163 | + throw new RuntimeException("Exception while finding entity views for deviceId [" + deviceId + "]", e); | |
164 | + } | |
165 | + | |
150 | 166 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(deviceId); |
151 | 167 | if (deviceCredentials != null) { |
152 | 168 | deviceCredentialsService.deleteDeviceCredentials(deviceCredentials); |
153 | 169 | } |
154 | 170 | deleteEntityRelations(deviceId); |
155 | - Device device = deviceDao.findById(deviceId.getId()); | |
171 | + | |
156 | 172 | List<Object> list = new ArrayList<>(); |
157 | 173 | list.add(device.getTenantId()); |
158 | 174 | list.add(device.getName()); |
175 | + Cache cache = cacheManager.getCache(DEVICE_CACHE); | |
159 | 176 | cache.evict(list); |
177 | + | |
160 | 178 | deviceDao.removeById(deviceId.getId()); |
161 | 179 | } |
162 | 180 | ... | ... |