Commit f1d89ace5d62409a2c433bfa47721df724e8ee84

Authored by Andrii Shvaika
1 parent 936171b8

Removed invalid dependency

@@ -110,7 +110,6 @@ import org.thingsboard.server.dao.model.ModelConstants; @@ -110,7 +110,6 @@ import org.thingsboard.server.dao.model.ModelConstants;
110 import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService; 110 import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService;
111 import org.thingsboard.server.dao.oauth2.OAuth2Service; 111 import org.thingsboard.server.dao.oauth2.OAuth2Service;
112 import org.thingsboard.server.dao.relation.RelationService; 112 import org.thingsboard.server.dao.relation.RelationService;
113 -import org.thingsboard.server.dao.resource.TbResourceService;  
114 import org.thingsboard.server.dao.rule.RuleChainService; 113 import org.thingsboard.server.dao.rule.RuleChainService;
115 import org.thingsboard.server.dao.tenant.TbTenantProfileCache; 114 import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
116 import org.thingsboard.server.dao.tenant.TenantProfileService; 115 import org.thingsboard.server.dao.tenant.TenantProfileService;
@@ -130,6 +129,7 @@ import org.thingsboard.server.service.edge.rpc.init.SyncEdgeService; @@ -130,6 +129,7 @@ import org.thingsboard.server.service.edge.rpc.init.SyncEdgeService;
130 import org.thingsboard.server.service.lwm2m.LwM2MModelsRepository; 129 import org.thingsboard.server.service.lwm2m.LwM2MModelsRepository;
131 import org.thingsboard.server.service.profile.TbDeviceProfileCache; 130 import org.thingsboard.server.service.profile.TbDeviceProfileCache;
132 import org.thingsboard.server.service.queue.TbClusterService; 131 import org.thingsboard.server.service.queue.TbClusterService;
  132 +import org.thingsboard.server.service.resource.TbResourceService;
133 import org.thingsboard.server.service.security.model.SecurityUser; 133 import org.thingsboard.server.service.security.model.SecurityUser;
134 import org.thingsboard.server.service.security.permission.AccessControlService; 134 import org.thingsboard.server.service.security.permission.AccessControlService;
135 import org.thingsboard.server.service.security.permission.Operation; 135 import org.thingsboard.server.service.security.permission.Operation;
@@ -36,8 +36,8 @@ import org.thingsboard.server.common.data.lwm2m.LwM2mObject; @@ -36,8 +36,8 @@ import org.thingsboard.server.common.data.lwm2m.LwM2mObject;
36 import org.thingsboard.server.common.data.page.PageData; 36 import org.thingsboard.server.common.data.page.PageData;
37 import org.thingsboard.server.common.data.page.PageLink; 37 import org.thingsboard.server.common.data.page.PageLink;
38 import org.thingsboard.server.common.data.security.Authority; 38 import org.thingsboard.server.common.data.security.Authority;
39 -import org.thingsboard.server.dao.resource.TbResourceService;  
40 import org.thingsboard.server.queue.util.TbCoreComponent; 39 import org.thingsboard.server.queue.util.TbCoreComponent;
  40 +import org.thingsboard.server.service.resource.TbResourceService;
41 import org.thingsboard.server.service.security.permission.Operation; 41 import org.thingsboard.server.service.security.permission.Operation;
42 import org.thingsboard.server.service.security.permission.Resource; 42 import org.thingsboard.server.service.security.permission.Resource;
43 43
@@ -52,12 +52,6 @@ public class TbResourceController extends BaseController { @@ -52,12 +52,6 @@ public class TbResourceController extends BaseController {
52 52
53 public static final String RESOURCE_ID = "resourceId"; 53 public static final String RESOURCE_ID = "resourceId";
54 54
55 - private final TbResourceService resourceService;  
56 -  
57 - public TbResourceController(TbResourceService resourceService) {  
58 - this.resourceService = resourceService;  
59 - }  
60 -  
61 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") 55 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
62 @RequestMapping(value = "/resource/{resourceId}/download", method = RequestMethod.GET) 56 @RequestMapping(value = "/resource/{resourceId}/download", method = RequestMethod.GET)
63 @ResponseBody 57 @ResponseBody
@@ -35,7 +35,7 @@ import org.thingsboard.server.common.data.widget.WidgetsBundle; @@ -35,7 +35,7 @@ import org.thingsboard.server.common.data.widget.WidgetsBundle;
35 import org.thingsboard.server.dao.dashboard.DashboardService; 35 import org.thingsboard.server.dao.dashboard.DashboardService;
36 import org.thingsboard.server.dao.exception.DataValidationException; 36 import org.thingsboard.server.dao.exception.DataValidationException;
37 import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService; 37 import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService;
38 -import org.thingsboard.server.dao.resource.TbResourceService; 38 +import org.thingsboard.server.dao.resource.ResourceService;
39 import org.thingsboard.server.dao.rule.RuleChainService; 39 import org.thingsboard.server.dao.rule.RuleChainService;
40 import org.thingsboard.server.dao.widget.WidgetTypeService; 40 import org.thingsboard.server.dao.widget.WidgetTypeService;
41 import org.thingsboard.server.dao.widget.WidgetsBundleService; 41 import org.thingsboard.server.dao.widget.WidgetsBundleService;
@@ -97,7 +97,7 @@ public class InstallScripts { @@ -97,7 +97,7 @@ public class InstallScripts {
97 private OAuth2ConfigTemplateService oAuth2TemplateService; 97 private OAuth2ConfigTemplateService oAuth2TemplateService;
98 98
99 @Autowired 99 @Autowired
100 - private TbResourceService resourceService; 100 + private ResourceService resourceService;
101 101
102 private Path getTenantRuleChainsDir() { 102 private Path getTenantRuleChainsDir() {
103 return Paths.get(getDataDir(), JSON_DIR, TENANT_DIR, RULE_CHAINS_DIR); 103 return Paths.get(getDataDir(), JSON_DIR, TENANT_DIR, RULE_CHAINS_DIR);
application/src/main/java/org/thingsboard/server/service/resource/DefaultTbResourceService.java renamed from dao/src/main/java/org/thingsboard/server/dao/resource/BaseTbResourceService.java
@@ -13,7 +13,7 @@ @@ -13,7 +13,7 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 -package org.thingsboard.server.dao.resource; 16 +package org.thingsboard.server.service.resource;
17 17
18 import lombok.extern.slf4j.Slf4j; 18 import lombok.extern.slf4j.Slf4j;
19 import org.apache.commons.lang3.StringUtils; 19 import org.apache.commons.lang3.StringUtils;
@@ -21,12 +21,12 @@ import org.eclipse.leshan.core.model.DDFFileParser; @@ -21,12 +21,12 @@ import org.eclipse.leshan.core.model.DDFFileParser;
21 import org.eclipse.leshan.core.model.DefaultDDFFileValidator; 21 import org.eclipse.leshan.core.model.DefaultDDFFileValidator;
22 import org.eclipse.leshan.core.model.InvalidDDFFileException; 22 import org.eclipse.leshan.core.model.InvalidDDFFileException;
23 import org.eclipse.leshan.core.model.ObjectModel; 23 import org.eclipse.leshan.core.model.ObjectModel;
24 -import org.hibernate.exception.ConstraintViolationException;  
25 import org.springframework.stereotype.Service; 24 import org.springframework.stereotype.Service;
26 import org.thingsboard.server.common.data.ResourceType; 25 import org.thingsboard.server.common.data.ResourceType;
27 import org.thingsboard.server.common.data.TbResource; 26 import org.thingsboard.server.common.data.TbResource;
28 import org.thingsboard.server.common.data.TbResourceInfo; 27 import org.thingsboard.server.common.data.TbResourceInfo;
29 -import org.thingsboard.server.common.data.Tenant; 28 +import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
  29 +import org.thingsboard.server.common.data.exception.ThingsboardException;
30 import org.thingsboard.server.common.data.id.TbResourceId; 30 import org.thingsboard.server.common.data.id.TbResourceId;
31 import org.thingsboard.server.common.data.id.TenantId; 31 import org.thingsboard.server.common.data.id.TenantId;
32 import org.thingsboard.server.common.data.lwm2m.LwM2mInstance; 32 import org.thingsboard.server.common.data.lwm2m.LwM2mInstance;
@@ -35,11 +35,7 @@ import org.thingsboard.server.common.data.lwm2m.LwM2mResourceObserve; @@ -35,11 +35,7 @@ import org.thingsboard.server.common.data.lwm2m.LwM2mResourceObserve;
35 import org.thingsboard.server.common.data.page.PageData; 35 import org.thingsboard.server.common.data.page.PageData;
36 import org.thingsboard.server.common.data.page.PageLink; 36 import org.thingsboard.server.common.data.page.PageLink;
37 import org.thingsboard.server.dao.exception.DataValidationException; 37 import org.thingsboard.server.dao.exception.DataValidationException;
38 -import org.thingsboard.server.dao.model.ModelConstants;  
39 -import org.thingsboard.server.dao.service.DataValidator;  
40 -import org.thingsboard.server.dao.service.PaginatedRemover;  
41 -import org.thingsboard.server.dao.service.Validator;  
42 -import org.thingsboard.server.dao.tenant.TenantDao; 38 +import org.thingsboard.server.dao.resource.ResourceService;
43 39
44 import java.io.ByteArrayInputStream; 40 import java.io.ByteArrayInputStream;
45 import java.io.IOException; 41 import java.io.IOException;
@@ -47,7 +43,6 @@ import java.util.ArrayList; @@ -47,7 +43,6 @@ import java.util.ArrayList;
47 import java.util.Base64; 43 import java.util.Base64;
48 import java.util.Comparator; 44 import java.util.Comparator;
49 import java.util.List; 45 import java.util.List;
50 -import java.util.Optional;  
51 import java.util.stream.Collectors; 46 import java.util.stream.Collectors;
52 47
53 import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_KEY; 48 import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_KEY;
@@ -55,139 +50,120 @@ import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPA @@ -55,139 +50,120 @@ import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPA
55 import static org.thingsboard.server.dao.device.DeviceServiceImpl.INCORRECT_TENANT_ID; 50 import static org.thingsboard.server.dao.device.DeviceServiceImpl.INCORRECT_TENANT_ID;
56 import static org.thingsboard.server.dao.service.Validator.validateId; 51 import static org.thingsboard.server.dao.service.Validator.validateId;
57 52
58 -@Service  
59 @Slf4j 53 @Slf4j
60 -public class BaseTbResourceService implements TbResourceService { 54 +@Service
  55 +public class DefaultTbResourceService implements TbResourceService {
61 56
62 - public static final String INCORRECT_RESOURCE_ID = "Incorrect resourceId ";  
63 - private final TbResourceDao resourceDao;  
64 - private final TbResourceInfoDao resourceInfoDao;  
65 - private final TenantDao tenantDao; 57 + private final ResourceService resourceService;
66 private final DDFFileParser ddfFileParser; 58 private final DDFFileParser ddfFileParser;
67 59
68 - public BaseTbResourceService(TbResourceDao resourceDao, TbResourceInfoDao resourceInfoDao, TenantDao tenantDao) {  
69 - this.resourceDao = resourceDao;  
70 - this.resourceInfoDao = resourceInfoDao;  
71 - this.tenantDao = tenantDao; 60 + public DefaultTbResourceService(ResourceService resourceService) {
  61 + this.resourceService = resourceService;
72 this.ddfFileParser = new DDFFileParser(new DefaultDDFFileValidator()); 62 this.ddfFileParser = new DDFFileParser(new DefaultDDFFileValidator());
73 } 63 }
74 64
75 @Override 65 @Override
76 - public TbResource saveResource(TbResource resource) throws InvalidDDFFileException, IOException { 66 + public TbResource saveResource(TbResource resource) throws ThingsboardException {
77 log.trace("Executing saveResource [{}]", resource); 67 log.trace("Executing saveResource [{}]", resource);
78 if (StringUtils.isEmpty(resource.getData())) { 68 if (StringUtils.isEmpty(resource.getData())) {
79 throw new DataValidationException("Resource data should be specified!"); 69 throw new DataValidationException("Resource data should be specified!");
80 } 70 }
81 if (ResourceType.LWM2M_MODEL.equals(resource.getResourceType())) { 71 if (ResourceType.LWM2M_MODEL.equals(resource.getResourceType())) {
82 - List<ObjectModel> objectModels =  
83 - ddfFileParser.parseEx(new ByteArrayInputStream(Base64.getDecoder().decode(resource.getData())), resource.getSearchText());  
84 - if (!objectModels.isEmpty()) {  
85 - ObjectModel objectModel = objectModels.get(0);  
86 -  
87 - String resourceKey = objectModel.id + LWM2M_SEPARATOR_KEY + objectModel.getVersion();  
88 - String name = objectModel.name;  
89 - resource.setResourceKey(resourceKey);  
90 - if (resource.getId() == null) {  
91 - resource.setTitle(name + " id=" + objectModel.id + " v" + objectModel.getVersion()); 72 + try {
  73 + List<ObjectModel> objectModels =
  74 + ddfFileParser.parseEx(new ByteArrayInputStream(Base64.getDecoder().decode(resource.getData())), resource.getSearchText());
  75 + if (!objectModels.isEmpty()) {
  76 + ObjectModel objectModel = objectModels.get(0);
  77 +
  78 + String resourceKey = objectModel.id + LWM2M_SEPARATOR_KEY + objectModel.getVersion();
  79 + String name = objectModel.name;
  80 + resource.setResourceKey(resourceKey);
  81 + if (resource.getId() == null) {
  82 + resource.setTitle(name + " id=" + objectModel.id + " v" + objectModel.getVersion());
  83 + }
  84 + resource.setSearchText(resourceKey + LWM2M_SEPARATOR_SEARCH_TEXT + name);
  85 + } else {
  86 + throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText()));
92 } 87 }
93 - resource.setSearchText(resourceKey + LWM2M_SEPARATOR_SEARCH_TEXT + name);  
94 - } else { 88 + } catch (InvalidDDFFileException | IOException e) {
  89 + throw new ThingsboardException(e, ThingsboardErrorCode.GENERAL);
  90 + }
  91 + if (resource.getResourceType().equals(ResourceType.LWM2M_MODEL) && toLwM2mObject(resource) == null) {
95 throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText())); 92 throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText()));
96 } 93 }
97 } else { 94 } else {
98 resource.setResourceKey(resource.getFileName()); 95 resource.setResourceKey(resource.getFileName());
99 } 96 }
100 97
101 - resourceValidator.validate(resource, TbResourceInfo::getTenantId);  
102 -  
103 - try {  
104 - return resourceDao.save(resource.getTenantId(), resource);  
105 - } catch (Exception t) {  
106 - ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);  
107 - if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("resource_unq_key")) {  
108 - String field = ResourceType.LWM2M_MODEL.equals(resource.getResourceType()) ? "resourceKey" : "fileName";  
109 - throw new DataValidationException("Resource with such " + field + " already exists!");  
110 - } else {  
111 - throw t;  
112 - }  
113 - }  
114 - 98 + return resourceService.saveResource(resource);
115 } 99 }
116 100
117 @Override 101 @Override
118 - public TbResource getResource(TenantId tenantId, ResourceType resourceType, String resourceKey) {  
119 - log.trace("Executing getResource [{}] [{}] [{}]", tenantId, resourceType, resourceKey);  
120 - return resourceDao.getResource(tenantId, resourceType, resourceKey); 102 + public TbResource getResource(TenantId tenantId, ResourceType resourceType, String resourceId) {
  103 + return resourceService.getResource(tenantId, resourceType, resourceId);
121 } 104 }
122 105
123 @Override 106 @Override
124 public TbResource findResourceById(TenantId tenantId, TbResourceId resourceId) { 107 public TbResource findResourceById(TenantId tenantId, TbResourceId resourceId) {
125 - log.trace("Executing findResourceById [{}] [{}]", tenantId, resourceId);  
126 - Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);  
127 - return resourceDao.findById(tenantId, resourceId.getId()); 108 + return resourceService.findResourceById(tenantId, resourceId);
128 } 109 }
129 110
130 @Override 111 @Override
131 public TbResourceInfo findResourceInfoById(TenantId tenantId, TbResourceId resourceId) { 112 public TbResourceInfo findResourceInfoById(TenantId tenantId, TbResourceId resourceId) {
132 - log.trace("Executing findResourceInfoById [{}] [{}]", tenantId, resourceId);  
133 - Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);  
134 - return resourceInfoDao.findById(tenantId, resourceId.getId());  
135 - }  
136 -  
137 - @Override  
138 - public void deleteResource(TenantId tenantId, TbResourceId resourceId) {  
139 - log.trace("Executing deleteResource [{}] [{}]", tenantId, resourceId);  
140 - Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);  
141 - resourceDao.removeById(tenantId, resourceId.getId()); 113 + return resourceService.findResourceInfoById(tenantId, resourceId);
142 } 114 }
143 115
144 @Override 116 @Override
145 public PageData<TbResourceInfo> findAllTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink) { 117 public PageData<TbResourceInfo> findAllTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink) {
146 - log.trace("Executing findAllTenantResourcesByTenantId [{}]", tenantId);  
147 - validateId(tenantId, INCORRECT_TENANT_ID + tenantId);  
148 - return resourceInfoDao.findAllTenantResourcesByTenantId(tenantId.getId(), pageLink); 118 + return resourceService.findAllTenantResourcesByTenantId(tenantId, pageLink);
149 } 119 }
150 120
151 @Override 121 @Override
152 public PageData<TbResourceInfo> findTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink) { 122 public PageData<TbResourceInfo> findTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink) {
153 - log.trace("Executing findTenantResourcesByTenantId [{}]", tenantId);  
154 - validateId(tenantId, INCORRECT_TENANT_ID + tenantId);  
155 - return resourceInfoDao.findTenantResourcesByTenantId(tenantId.getId(), pageLink); 123 + return resourceService.findTenantResourcesByTenantId(tenantId, pageLink);
156 } 124 }
157 125
158 @Override 126 @Override
159 - public List<LwM2mObject> findLwM2mObjectPage(TenantId tenantId, String sortProperty, String sortOrder, PageLink pageLink) { 127 + public List<LwM2mObject> findLwM2mObject(TenantId tenantId, String sortOrder, String sortProperty, String[] objectIds) {
160 log.trace("Executing findByTenantId [{}]", tenantId); 128 log.trace("Executing findByTenantId [{}]", tenantId);
161 validateId(tenantId, INCORRECT_TENANT_ID + tenantId); 129 validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
162 - PageData<TbResource> resourcePageData = resourceDao.findResourcesByTenantIdAndResourceType(  
163 - tenantId,  
164 - ResourceType.LWM2M_MODEL, pageLink);  
165 - return resourcePageData.getData().stream() 130 + List<TbResource> resources = resourceService.findTenantResourcesByResourceTypeAndObjectIds(tenantId, ResourceType.LWM2M_MODEL,
  131 + objectIds);
  132 + return resources.stream()
166 .map(this::toLwM2mObject) 133 .map(this::toLwM2mObject)
167 .sorted(getComparator(sortProperty, sortOrder)) 134 .sorted(getComparator(sortProperty, sortOrder))
168 .collect(Collectors.toList()); 135 .collect(Collectors.toList());
169 } 136 }
170 137
171 @Override 138 @Override
172 - public List<LwM2mObject> findLwM2mObject(TenantId tenantId, String sortOrder,  
173 - String sortProperty,  
174 - String[] objectIds) { 139 + public List<LwM2mObject> findLwM2mObjectPage(TenantId tenantId, String sortProperty, String sortOrder, PageLink pageLink) {
175 log.trace("Executing findByTenantId [{}]", tenantId); 140 log.trace("Executing findByTenantId [{}]", tenantId);
176 validateId(tenantId, INCORRECT_TENANT_ID + tenantId); 141 validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
177 - List<TbResource> resources = resourceDao.findResourcesByTenantIdAndResourceType(tenantId, ResourceType.LWM2M_MODEL,  
178 - objectIds,  
179 - null);  
180 - return resources.stream() 142 + PageData<TbResource> resourcePageData = resourceService.findTenantResourcesByResourceTypeAndPageLink(tenantId, ResourceType.LWM2M_MODEL, pageLink);
  143 + return resourcePageData.getData().stream()
181 .map(this::toLwM2mObject) 144 .map(this::toLwM2mObject)
182 .sorted(getComparator(sortProperty, sortOrder)) 145 .sorted(getComparator(sortProperty, sortOrder))
183 .collect(Collectors.toList()); 146 .collect(Collectors.toList());
184 } 147 }
185 148
186 @Override 149 @Override
  150 + public void deleteResource(TenantId tenantId, TbResourceId resourceId) {
  151 + resourceService.deleteResource(tenantId, resourceId);
  152 + }
  153 +
  154 + @Override
187 public void deleteResourcesByTenantId(TenantId tenantId) { 155 public void deleteResourcesByTenantId(TenantId tenantId) {
188 - log.trace("Executing deleteResourcesByTenantId, tenantId [{}]", tenantId);  
189 - validateId(tenantId, INCORRECT_TENANT_ID + tenantId);  
190 - tenantResourcesRemover.removeEntities(tenantId, tenantId); 156 + resourceService.deleteResourcesByTenantId(tenantId);
  157 + }
  158 +
  159 + private Comparator<? super LwM2mObject> getComparator(String sortProperty, String sortOrder) {
  160 + Comparator<LwM2mObject> comparator;
  161 + if ("name".equals(sortProperty)) {
  162 + comparator = Comparator.comparing(LwM2mObject::getName);
  163 + } else {
  164 + comparator = Comparator.comparingLong(LwM2mObject::getId);
  165 + }
  166 + return "DESC".equals(sortOrder) ? comparator.reversed() : comparator;
191 } 167 }
192 168
193 private LwM2mObject toLwM2mObject(TbResource resource) { 169 private LwM2mObject toLwM2mObject(TbResource resource) {
@@ -223,69 +199,4 @@ public class BaseTbResourceService implements TbResourceService { @@ -223,69 +199,4 @@ public class BaseTbResourceService implements TbResourceService {
223 return null; 199 return null;
224 } 200 }
225 } 201 }
226 -  
227 - private Comparator<? super LwM2mObject> getComparator(String sortProperty, String sortOrder) {  
228 - Comparator<LwM2mObject> comparator;  
229 - if ("name".equals(sortProperty)) {  
230 - comparator = Comparator.comparing(LwM2mObject::getName);  
231 - } else {  
232 - comparator = Comparator.comparingLong(LwM2mObject::getId);  
233 - }  
234 - return "DESC".equals(sortOrder) ? comparator.reversed() : comparator;  
235 - }  
236 -  
237 - private DataValidator<TbResource> resourceValidator = new DataValidator<>() {  
238 -  
239 - @Override  
240 - protected void validateDataImpl(TenantId tenantId, TbResource resource) {  
241 - if (StringUtils.isEmpty(resource.getTitle())) {  
242 - throw new DataValidationException("Resource title should be specified!");  
243 - }  
244 - if (resource.getResourceType() == null) {  
245 - throw new DataValidationException("Resource type should be specified!");  
246 - }  
247 - if (StringUtils.isEmpty(resource.getFileName())) {  
248 - throw new DataValidationException("Resource file name should be specified!");  
249 - }  
250 - if (StringUtils.isEmpty(resource.getResourceKey())) {  
251 - throw new DataValidationException("Resource key should be specified!");  
252 - }  
253 - if (resource.getTenantId() == null) {  
254 - resource.setTenantId(new TenantId(ModelConstants.NULL_UUID));  
255 - }  
256 - if (!resource.getTenantId().getId().equals(ModelConstants.NULL_UUID)) {  
257 - Tenant tenant = tenantDao.findById(tenantId, resource.getTenantId().getId());  
258 - if (tenant == null) {  
259 - throw new DataValidationException("Resource is referencing to non-existent tenant!");  
260 - }  
261 - }  
262 - if (resource.getResourceType().equals(ResourceType.LWM2M_MODEL) && toLwM2mObject(resource) == null) {  
263 - throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText()));  
264 - }  
265 - }  
266 - };  
267 -  
268 - private PaginatedRemover<TenantId, TbResource> tenantResourcesRemover =  
269 - new PaginatedRemover<>() {  
270 -  
271 - @Override  
272 - protected PageData<TbResource> findEntities(TenantId tenantId, TenantId id, PageLink pageLink) {  
273 - return resourceDao.findAllByTenantId(id, pageLink);  
274 - }  
275 -  
276 - @Override  
277 - protected void removeEntity(TenantId tenantId, TbResource entity) {  
278 - deleteResource(tenantId, new TbResourceId(entity.getUuidId()));  
279 - }  
280 - };  
281 -  
282 - protected Optional<ConstraintViolationException> extractConstraintViolationException(Exception t) {  
283 - if (t instanceof ConstraintViolationException) {  
284 - return Optional.of((ConstraintViolationException) t);  
285 - } else if (t.getCause() instanceof ConstraintViolationException) {  
286 - return Optional.of((ConstraintViolationException) (t.getCause()));  
287 - } else {  
288 - return Optional.empty();  
289 - }  
290 - }  
291 } 202 }
application/src/main/java/org/thingsboard/server/service/resource/TbResourceService.java renamed from common/dao-api/src/main/java/org/thingsboard/server/dao/resource/TbResourceService.java
@@ -13,24 +13,23 @@ @@ -13,24 +13,23 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 -package org.thingsboard.server.dao.resource; 16 +package org.thingsboard.server.service.resource;
17 17
18 -import org.eclipse.leshan.core.model.InvalidDDFFileException; 18 +import org.thingsboard.server.common.data.ResourceType;
19 import org.thingsboard.server.common.data.TbResource; 19 import org.thingsboard.server.common.data.TbResource;
20 import org.thingsboard.server.common.data.TbResourceInfo; 20 import org.thingsboard.server.common.data.TbResourceInfo;
21 -import org.thingsboard.server.common.data.ResourceType; 21 +import org.thingsboard.server.common.data.exception.ThingsboardException;
22 import org.thingsboard.server.common.data.id.TbResourceId; 22 import org.thingsboard.server.common.data.id.TbResourceId;
23 import org.thingsboard.server.common.data.id.TenantId; 23 import org.thingsboard.server.common.data.id.TenantId;
24 import org.thingsboard.server.common.data.lwm2m.LwM2mObject; 24 import org.thingsboard.server.common.data.lwm2m.LwM2mObject;
25 import org.thingsboard.server.common.data.page.PageData; 25 import org.thingsboard.server.common.data.page.PageData;
26 import org.thingsboard.server.common.data.page.PageLink; 26 import org.thingsboard.server.common.data.page.PageLink;
27 27
28 -import java.io.IOException;  
29 import java.util.List; 28 import java.util.List;
30 29
31 -  
32 public interface TbResourceService { 30 public interface TbResourceService {
33 - TbResource saveResource(TbResource resource) throws InvalidDDFFileException, IOException; 31 +
  32 + TbResource saveResource(TbResource resource) throws ThingsboardException;
34 33
35 TbResource getResource(TenantId tenantId, ResourceType resourceType, String resourceId); 34 TbResource getResource(TenantId tenantId, ResourceType resourceType, String resourceId);
36 35
@@ -55,4 +54,5 @@ public interface TbResourceService { @@ -55,4 +54,5 @@ public interface TbResourceService {
55 void deleteResource(TenantId tenantId, TbResourceId resourceId); 54 void deleteResource(TenantId tenantId, TbResourceId resourceId);
56 55
57 void deleteResourcesByTenantId(TenantId tenantId); 56 void deleteResourcesByTenantId(TenantId tenantId);
  57 +
58 } 58 }
@@ -56,7 +56,7 @@ import org.thingsboard.server.dao.device.provision.ProvisionFailedException; @@ -56,7 +56,7 @@ import org.thingsboard.server.dao.device.provision.ProvisionFailedException;
56 import org.thingsboard.server.dao.device.provision.ProvisionRequest; 56 import org.thingsboard.server.dao.device.provision.ProvisionRequest;
57 import org.thingsboard.server.dao.device.provision.ProvisionResponse; 57 import org.thingsboard.server.dao.device.provision.ProvisionResponse;
58 import org.thingsboard.server.dao.relation.RelationService; 58 import org.thingsboard.server.dao.relation.RelationService;
59 -import org.thingsboard.server.dao.resource.TbResourceService; 59 +import org.thingsboard.server.dao.resource.ResourceService;
60 import org.thingsboard.server.dao.tenant.TbTenantProfileCache; 60 import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
61 import org.thingsboard.server.gen.transport.TransportProtos; 61 import org.thingsboard.server.gen.transport.TransportProtos;
62 import org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto; 62 import org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto;
@@ -108,7 +108,7 @@ public class DefaultTransportApiService implements TransportApiService { @@ -108,7 +108,7 @@ public class DefaultTransportApiService implements TransportApiService {
108 private final TbClusterService tbClusterService; 108 private final TbClusterService tbClusterService;
109 private final DataDecodingEncodingService dataDecodingEncodingService; 109 private final DataDecodingEncodingService dataDecodingEncodingService;
110 private final DeviceProvisionService deviceProvisionService; 110 private final DeviceProvisionService deviceProvisionService;
111 - private final TbResourceService resourceService; 111 + private final ResourceService resourceService;
112 112
113 private final ConcurrentMap<String, ReentrantLock> deviceCreationLocks = new ConcurrentHashMap<>(); 113 private final ConcurrentMap<String, ReentrantLock> deviceCreationLocks = new ConcurrentHashMap<>();
114 114
@@ -117,7 +117,7 @@ public class DefaultTransportApiService implements TransportApiService { @@ -117,7 +117,7 @@ public class DefaultTransportApiService implements TransportApiService {
117 RelationService relationService, DeviceCredentialsService deviceCredentialsService, 117 RelationService relationService, DeviceCredentialsService deviceCredentialsService,
118 DeviceStateService deviceStateService, DbCallbackExecutorService dbCallbackExecutorService, 118 DeviceStateService deviceStateService, DbCallbackExecutorService dbCallbackExecutorService,
119 TbClusterService tbClusterService, DataDecodingEncodingService dataDecodingEncodingService, 119 TbClusterService tbClusterService, DataDecodingEncodingService dataDecodingEncodingService,
120 - DeviceProvisionService deviceProvisionService, TbResourceService resourceService) { 120 + DeviceProvisionService deviceProvisionService, ResourceService resourceService) {
121 this.deviceProfileCache = deviceProfileCache; 121 this.deviceProfileCache = deviceProfileCache;
122 this.tenantProfileCache = tenantProfileCache; 122 this.tenantProfileCache = tenantProfileCache;
123 this.apiUsageStateService = apiUsageStateService; 123 this.apiUsageStateService = apiUsageStateService;
application/src/test/java/org/thingsboard/server/service/ServiceSqlTestSuite.java renamed from dao/src/test/java/org/thingsboard/server/dao/service/sql/TbResourceServiceSqlTest.java
@@ -13,11 +13,31 @@ @@ -13,11 +13,31 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 -package org.thingsboard.server.dao.service.sql; 16 +package org.thingsboard.server.service;
17 17
18 -import org.thingsboard.server.dao.service.BaseTbResourceServiceTest;  
19 -import org.thingsboard.server.dao.service.DaoSqlTest; 18 +import org.junit.BeforeClass;
  19 +import org.junit.ClassRule;
  20 +import org.junit.extensions.cpsuite.ClasspathSuite;
  21 +import org.junit.runner.RunWith;
  22 +import org.thingsboard.server.dao.CustomSqlUnit;
  23 +import org.thingsboard.server.queue.memory.InMemoryStorage;
20 24
21 -@DaoSqlTest  
22 -public class TbResourceServiceSqlTest extends BaseTbResourceServiceTest { 25 +import java.util.Arrays;
  26 +
  27 +@RunWith(ClasspathSuite.class)
  28 +@ClasspathSuite.ClassnameFilters({
  29 + "org.thingsboard.server.service.resource.*Test",
  30 + })
  31 +public class ServiceSqlTestSuite {
  32 +
  33 + @ClassRule
  34 + public static CustomSqlUnit sqlUnit = new CustomSqlUnit(
  35 + Arrays.asList("sql/schema-types-hsql.sql", "sql/schema-ts-hsql.sql", "sql/schema-entities-hsql.sql", "sql/schema-entities-idx.sql", "sql/system-data.sql"),
  36 + "sql/hsql/drop-all-tables.sql",
  37 + "sql-test.properties");
  38 +
  39 + @BeforeClass
  40 + public static void cleanupInMemStorage(){
  41 + InMemoryStorage.getInstance().cleanup();
  42 + }
23 } 43 }
application/src/test/java/org/thingsboard/server/service/resource/BaseTbResourceServiceTest.java renamed from dao/src/test/java/org/thingsboard/server/dao/service/BaseTbResourceServiceTest.java
@@ -13,28 +13,37 @@ @@ -13,28 +13,37 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 -package org.thingsboard.server.dao.service; 16 +package org.thingsboard.server.service.resource;
17 17
18 import com.datastax.oss.driver.api.core.uuid.Uuids; 18 import com.datastax.oss.driver.api.core.uuid.Uuids;
19 import org.junit.After; 19 import org.junit.After;
20 import org.junit.Assert; 20 import org.junit.Assert;
21 import org.junit.Before; 21 import org.junit.Before;
22 import org.junit.Test; 22 import org.junit.Test;
  23 +import org.springframework.beans.factory.annotation.Autowired;
23 import org.thingsboard.server.common.data.ResourceType; 24 import org.thingsboard.server.common.data.ResourceType;
24 import org.thingsboard.server.common.data.TbResource; 25 import org.thingsboard.server.common.data.TbResource;
25 import org.thingsboard.server.common.data.TbResourceInfo; 26 import org.thingsboard.server.common.data.TbResourceInfo;
26 import org.thingsboard.server.common.data.Tenant; 27 import org.thingsboard.server.common.data.Tenant;
  28 +import org.thingsboard.server.common.data.User;
27 import org.thingsboard.server.common.data.id.TenantId; 29 import org.thingsboard.server.common.data.id.TenantId;
28 import org.thingsboard.server.common.data.page.PageData; 30 import org.thingsboard.server.common.data.page.PageData;
29 import org.thingsboard.server.common.data.page.PageLink; 31 import org.thingsboard.server.common.data.page.PageLink;
  32 +import org.thingsboard.server.common.data.security.Authority;
  33 +import org.thingsboard.server.controller.AbstractControllerTest;
30 import org.thingsboard.server.dao.exception.DataValidationException; 34 import org.thingsboard.server.dao.exception.DataValidationException;
  35 +import org.thingsboard.server.dao.service.AbstractServiceTest;
  36 +import org.thingsboard.server.dao.service.DaoSqlTest;
31 37
32 import java.util.ArrayList; 38 import java.util.ArrayList;
33 import java.util.Base64; 39 import java.util.Base64;
34 import java.util.Collections; 40 import java.util.Collections;
35 import java.util.List; 41 import java.util.List;
36 42
37 -public abstract class BaseTbResourceServiceTest extends AbstractServiceTest { 43 +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  44 +
  45 +@DaoSqlTest
  46 +public class BaseTbResourceServiceTest extends AbstractControllerTest {
38 47
39 private static final String LWM2M_TEST_MODEL = "<LWM2M xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.openmobilealliance.org/tech/profiles/LWM2M-v1_1.xsd\">\n" + 48 private static final String LWM2M_TEST_MODEL = "<LWM2M xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.openmobilealliance.org/tech/profiles/LWM2M-v1_1.xsd\">\n" +
40 "<Object ObjectType=\"MODefinition\">\n" + 49 "<Object ObjectType=\"MODefinition\">\n" +
@@ -67,18 +76,38 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest { @@ -67,18 +76,38 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest {
67 76
68 private TenantId tenantId; 77 private TenantId tenantId;
69 78
  79 + @Autowired
  80 + private TbResourceService resourceService;
  81 +
  82 + private Tenant savedTenant;
  83 + private User tenantAdmin;
  84 +
70 @Before 85 @Before
71 - public void before() { 86 + public void beforeTest() throws Exception {
  87 + loginSysAdmin();
  88 +
72 Tenant tenant = new Tenant(); 89 Tenant tenant = new Tenant();
73 tenant.setTitle("My tenant"); 90 tenant.setTitle("My tenant");
74 - Tenant savedTenant = tenantService.saveTenant(tenant);  
75 - Assert.assertNotNull(savedTenant); 91 + savedTenant = doPost("/api/tenant", tenant, Tenant.class);
76 tenantId = savedTenant.getId(); 92 tenantId = savedTenant.getId();
  93 + Assert.assertNotNull(savedTenant);
  94 +
  95 + tenantAdmin = new User();
  96 + tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
  97 + tenantAdmin.setTenantId(savedTenant.getId());
  98 + tenantAdmin.setEmail("tenant2@thingsboard.org");
  99 + tenantAdmin.setFirstName("Joe");
  100 + tenantAdmin.setLastName("Downs");
  101 +
  102 + tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
77 } 103 }
78 104
79 @After 105 @After
80 - public void after() {  
81 - tenantService.deleteTenant(tenantId); 106 + public void afterTest() throws Exception {
  107 + loginSysAdmin();
  108 +
  109 + doDelete("/api/tenant/" + savedTenant.getId().getId().toString())
  110 + .andExpect(status().isOk());
82 } 111 }
83 112
84 @Test 113 @Test
@@ -239,9 +268,10 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest { @@ -239,9 +268,10 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest {
239 268
240 @Test 269 @Test
241 public void testFindTenantResourcesByTenantId() throws Exception { 270 public void testFindTenantResourcesByTenantId() throws Exception {
  271 + loginSysAdmin();
242 Tenant tenant = new Tenant(); 272 Tenant tenant = new Tenant();
243 tenant.setTitle("Test tenant"); 273 tenant.setTitle("Test tenant");
244 - tenant = tenantService.saveTenant(tenant); 274 + tenant = doPost("/api/tenant", tenant, Tenant.class);
245 275
246 TenantId tenantId = tenant.getId(); 276 TenantId tenantId = tenant.getId();
247 277
@@ -279,14 +309,17 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest { @@ -279,14 +309,17 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest {
279 Assert.assertFalse(pageData.hasNext()); 309 Assert.assertFalse(pageData.hasNext());
280 Assert.assertTrue(pageData.getData().isEmpty()); 310 Assert.assertTrue(pageData.getData().isEmpty());
281 311
282 - tenantService.deleteTenant(tenantId); 312 + doDelete("/api/tenant/" + tenantId.getId().toString())
  313 + .andExpect(status().isOk());
283 } 314 }
284 315
285 @Test 316 @Test
286 public void testFindAllTenantResourcesByTenantId() throws Exception { 317 public void testFindAllTenantResourcesByTenantId() throws Exception {
  318 + loginSysAdmin();
  319 +
287 Tenant tenant = new Tenant(); 320 Tenant tenant = new Tenant();
288 tenant.setTitle("Test tenant"); 321 tenant.setTitle("Test tenant");
289 - tenant = tenantService.saveTenant(tenant); 322 + tenant = doPost("/api/tenant", tenant, Tenant.class);
290 323
291 TenantId tenantId = tenant.getId(); 324 TenantId tenantId = tenant.getId();
292 325
@@ -344,7 +377,8 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest { @@ -344,7 +377,8 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest {
344 Assert.assertFalse(pageData.hasNext()); 377 Assert.assertFalse(pageData.hasNext());
345 Assert.assertTrue(pageData.getData().isEmpty()); 378 Assert.assertTrue(pageData.getData().isEmpty());
346 379
347 - tenantService.deleteTenant(tenantId); 380 + doDelete("/api/tenant/" + tenantId.getId().toString())
  381 + .andExpect(status().isOk());
348 } 382 }
349 383
350 } 384 }
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.dao.resource;
  17 +
  18 +import org.thingsboard.server.common.data.ResourceType;
  19 +import org.thingsboard.server.common.data.TbResource;
  20 +import org.thingsboard.server.common.data.TbResourceInfo;
  21 +import org.thingsboard.server.common.data.id.TbResourceId;
  22 +import org.thingsboard.server.common.data.id.TenantId;
  23 +import org.thingsboard.server.common.data.page.PageData;
  24 +import org.thingsboard.server.common.data.page.PageLink;
  25 +
  26 +import java.util.List;
  27 +
  28 +public interface ResourceService {
  29 + TbResource saveResource(TbResource resource);
  30 +
  31 + TbResource getResource(TenantId tenantId, ResourceType resourceType, String resourceId);
  32 +
  33 + TbResource findResourceById(TenantId tenantId, TbResourceId resourceId);
  34 +
  35 + TbResourceInfo findResourceInfoById(TenantId tenantId, TbResourceId resourceId);
  36 +
  37 + PageData<TbResourceInfo> findAllTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink);
  38 +
  39 + PageData<TbResourceInfo> findTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink);
  40 +
  41 + List<TbResource> findTenantResourcesByResourceTypeAndObjectIds(TenantId tenantId, ResourceType lwm2mModel, String[] objectIds);
  42 +
  43 + PageData<TbResource> findTenantResourcesByResourceTypeAndPageLink(TenantId tenantId, ResourceType lwm2mModel, PageLink pageLink);
  44 +
  45 + void deleteResource(TenantId tenantId, TbResourceId resourceId);
  46 +
  47 + void deleteResourcesByTenantId(TenantId tenantId);
  48 +
  49 +
  50 +}
@@ -87,10 +87,6 @@ @@ -87,10 +87,6 @@
87 <groupId>org.thingsboard</groupId> 87 <groupId>org.thingsboard</groupId>
88 <artifactId>protobuf-dynamic</artifactId> 88 <artifactId>protobuf-dynamic</artifactId>
89 </dependency> 89 </dependency>
90 - <dependency>  
91 - <groupId>org.eclipse.leshan</groupId>  
92 - <artifactId>leshan-core</artifactId>  
93 - </dependency>  
94 </dependencies> 90 </dependencies>
95 91
96 <build> 92 <build>
@@ -227,10 +227,6 @@ @@ -227,10 +227,6 @@
227 <groupId>org.elasticsearch.client</groupId> 227 <groupId>org.elasticsearch.client</groupId>
228 <artifactId>rest</artifactId> 228 <artifactId>rest</artifactId>
229 </dependency> 229 </dependency>
230 - <dependency>  
231 - <groupId>org.eclipse.leshan</groupId>  
232 - <artifactId>leshan-core</artifactId>  
233 - </dependency>  
234 </dependencies> 230 </dependencies>
235 <build> 231 <build>
236 <plugins> 232 <plugins>
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.dao.resource;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.apache.commons.lang3.StringUtils;
  20 +import org.hibernate.exception.ConstraintViolationException;
  21 +import org.springframework.stereotype.Service;
  22 +import org.thingsboard.server.common.data.ResourceType;
  23 +import org.thingsboard.server.common.data.TbResource;
  24 +import org.thingsboard.server.common.data.TbResourceInfo;
  25 +import org.thingsboard.server.common.data.Tenant;
  26 +import org.thingsboard.server.common.data.id.TbResourceId;
  27 +import org.thingsboard.server.common.data.id.TenantId;
  28 +import org.thingsboard.server.common.data.page.PageData;
  29 +import org.thingsboard.server.common.data.page.PageLink;
  30 +import org.thingsboard.server.dao.exception.DataValidationException;
  31 +import org.thingsboard.server.dao.model.ModelConstants;
  32 +import org.thingsboard.server.dao.service.DataValidator;
  33 +import org.thingsboard.server.dao.service.PaginatedRemover;
  34 +import org.thingsboard.server.dao.service.Validator;
  35 +import org.thingsboard.server.dao.tenant.TenantDao;
  36 +
  37 +import java.util.List;
  38 +import java.util.Optional;
  39 +
  40 +import static org.thingsboard.server.dao.device.DeviceServiceImpl.INCORRECT_TENANT_ID;
  41 +import static org.thingsboard.server.dao.service.Validator.validateId;
  42 +
  43 +@Service
  44 +@Slf4j
  45 +public class BaseResourceService implements ResourceService {
  46 +
  47 + public static final String INCORRECT_RESOURCE_ID = "Incorrect resourceId ";
  48 + private final TbResourceDao resourceDao;
  49 + private final TbResourceInfoDao resourceInfoDao;
  50 + private final TenantDao tenantDao;
  51 +
  52 +
  53 + public BaseResourceService(TbResourceDao resourceDao, TbResourceInfoDao resourceInfoDao, TenantDao tenantDao) {
  54 + this.resourceDao = resourceDao;
  55 + this.resourceInfoDao = resourceInfoDao;
  56 + this.tenantDao = tenantDao;
  57 + }
  58 +
  59 + @Override
  60 + public TbResource saveResource(TbResource resource) {
  61 + resourceValidator.validate(resource, TbResourceInfo::getTenantId);
  62 +
  63 + try {
  64 + return resourceDao.save(resource.getTenantId(), resource);
  65 + } catch (Exception t) {
  66 + ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
  67 + if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("resource_unq_key")) {
  68 + String field = ResourceType.LWM2M_MODEL.equals(resource.getResourceType()) ? "resourceKey" : "fileName";
  69 + throw new DataValidationException("Resource with such " + field + " already exists!");
  70 + } else {
  71 + throw t;
  72 + }
  73 + }
  74 + }
  75 +
  76 + @Override
  77 + public TbResource getResource(TenantId tenantId, ResourceType resourceType, String resourceKey) {
  78 + log.trace("Executing getResource [{}] [{}] [{}]", tenantId, resourceType, resourceKey);
  79 + return resourceDao.getResource(tenantId, resourceType, resourceKey);
  80 + }
  81 +
  82 + @Override
  83 + public TbResource findResourceById(TenantId tenantId, TbResourceId resourceId) {
  84 + log.trace("Executing findResourceById [{}] [{}]", tenantId, resourceId);
  85 + Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);
  86 + return resourceDao.findById(tenantId, resourceId.getId());
  87 + }
  88 +
  89 + @Override
  90 + public TbResourceInfo findResourceInfoById(TenantId tenantId, TbResourceId resourceId) {
  91 + log.trace("Executing findResourceInfoById [{}] [{}]", tenantId, resourceId);
  92 + Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);
  93 + return resourceInfoDao.findById(tenantId, resourceId.getId());
  94 + }
  95 +
  96 + @Override
  97 + public void deleteResource(TenantId tenantId, TbResourceId resourceId) {
  98 + log.trace("Executing deleteResource [{}] [{}]", tenantId, resourceId);
  99 + Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);
  100 + resourceDao.removeById(tenantId, resourceId.getId());
  101 + }
  102 +
  103 + @Override
  104 + public PageData<TbResourceInfo> findAllTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink) {
  105 + log.trace("Executing findAllTenantResourcesByTenantId [{}]", tenantId);
  106 + validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
  107 + return resourceInfoDao.findAllTenantResourcesByTenantId(tenantId.getId(), pageLink);
  108 + }
  109 +
  110 + @Override
  111 + public PageData<TbResourceInfo> findTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink) {
  112 + log.trace("Executing findTenantResourcesByTenantId [{}]", tenantId);
  113 + validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
  114 + return resourceInfoDao.findTenantResourcesByTenantId(tenantId.getId(), pageLink);
  115 + }
  116 +
  117 + @Override
  118 + public List<TbResource> findTenantResourcesByResourceTypeAndObjectIds(TenantId tenantId, ResourceType resourceType, String[] objectIds) {
  119 + log.trace("Executing findTenantResourcesByResourceTypeAndObjectIds [{}][{}][{}]", tenantId, resourceType, objectIds);
  120 + validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
  121 + return resourceDao.findResourcesByTenantIdAndResourceType(tenantId, resourceType, objectIds, null);
  122 + }
  123 +
  124 + @Override
  125 + public PageData<TbResource> findTenantResourcesByResourceTypeAndPageLink(TenantId tenantId, ResourceType resourceType, PageLink pageLink) {
  126 + log.trace("Executing findTenantResourcesByResourceTypeAndPageLink [{}][{}][{}]", tenantId, resourceType, pageLink);
  127 + validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
  128 + return resourceDao.findResourcesByTenantIdAndResourceType(tenantId, resourceType, pageLink);
  129 + }
  130 +
  131 + @Override
  132 + public void deleteResourcesByTenantId(TenantId tenantId) {
  133 + log.trace("Executing deleteResourcesByTenantId, tenantId [{}]", tenantId);
  134 + validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
  135 + tenantResourcesRemover.removeEntities(tenantId, tenantId);
  136 + }
  137 +
  138 + private DataValidator<TbResource> resourceValidator = new DataValidator<>() {
  139 +
  140 + @Override
  141 + protected void validateDataImpl(TenantId tenantId, TbResource resource) {
  142 + if (StringUtils.isEmpty(resource.getTitle())) {
  143 + throw new DataValidationException("Resource title should be specified!");
  144 + }
  145 + if (resource.getResourceType() == null) {
  146 + throw new DataValidationException("Resource type should be specified!");
  147 + }
  148 + if (StringUtils.isEmpty(resource.getFileName())) {
  149 + throw new DataValidationException("Resource file name should be specified!");
  150 + }
  151 + if (StringUtils.isEmpty(resource.getResourceKey())) {
  152 + throw new DataValidationException("Resource key should be specified!");
  153 + }
  154 + if (resource.getTenantId() == null) {
  155 + resource.setTenantId(new TenantId(ModelConstants.NULL_UUID));
  156 + }
  157 + if (!resource.getTenantId().getId().equals(ModelConstants.NULL_UUID)) {
  158 + Tenant tenant = tenantDao.findById(tenantId, resource.getTenantId().getId());
  159 + if (tenant == null) {
  160 + throw new DataValidationException("Resource is referencing to non-existent tenant!");
  161 + }
  162 + }
  163 + }
  164 + };
  165 +
  166 + private PaginatedRemover<TenantId, TbResource> tenantResourcesRemover =
  167 + new PaginatedRemover<>() {
  168 +
  169 + @Override
  170 + protected PageData<TbResource> findEntities(TenantId tenantId, TenantId id, PageLink pageLink) {
  171 + return resourceDao.findAllByTenantId(id, pageLink);
  172 + }
  173 +
  174 + @Override
  175 + protected void removeEntity(TenantId tenantId, TbResource entity) {
  176 + deleteResource(tenantId, new TbResourceId(entity.getUuidId()));
  177 + }
  178 + };
  179 +
  180 + protected Optional<ConstraintViolationException> extractConstraintViolationException(Exception t) {
  181 + if (t instanceof ConstraintViolationException) {
  182 + return Optional.of((ConstraintViolationException) t);
  183 + } else if (t.getCause() instanceof ConstraintViolationException) {
  184 + return Optional.of((ConstraintViolationException) (t.getCause()));
  185 + } else {
  186 + return Optional.empty();
  187 + }
  188 + }
  189 +}
@@ -35,7 +35,7 @@ import org.thingsboard.server.dao.device.DeviceService; @@ -35,7 +35,7 @@ import org.thingsboard.server.dao.device.DeviceService;
35 import org.thingsboard.server.dao.entity.AbstractEntityService; 35 import org.thingsboard.server.dao.entity.AbstractEntityService;
36 import org.thingsboard.server.dao.entityview.EntityViewService; 36 import org.thingsboard.server.dao.entityview.EntityViewService;
37 import org.thingsboard.server.dao.exception.DataValidationException; 37 import org.thingsboard.server.dao.exception.DataValidationException;
38 -import org.thingsboard.server.dao.resource.TbResourceService; 38 +import org.thingsboard.server.dao.resource.ResourceService;
39 import org.thingsboard.server.dao.rule.RuleChainService; 39 import org.thingsboard.server.dao.rule.RuleChainService;
40 import org.thingsboard.server.dao.service.DataValidator; 40 import org.thingsboard.server.dao.service.DataValidator;
41 import org.thingsboard.server.dao.service.PaginatedRemover; 41 import org.thingsboard.server.dao.service.PaginatedRemover;
@@ -90,7 +90,7 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe @@ -90,7 +90,7 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe
90 private RuleChainService ruleChainService; 90 private RuleChainService ruleChainService;
91 91
92 @Autowired 92 @Autowired
93 - private TbResourceService resourceService; 93 + private ResourceService resourceService;
94 94
95 @Override 95 @Override
96 public Tenant findTenantById(TenantId tenantId) { 96 public Tenant findTenantById(TenantId tenantId) {
@@ -54,7 +54,7 @@ import org.thingsboard.server.dao.entity.EntityService; @@ -54,7 +54,7 @@ import org.thingsboard.server.dao.entity.EntityService;
54 import org.thingsboard.server.dao.entityview.EntityViewService; 54 import org.thingsboard.server.dao.entityview.EntityViewService;
55 import org.thingsboard.server.dao.event.EventService; 55 import org.thingsboard.server.dao.event.EventService;
56 import org.thingsboard.server.dao.relation.RelationService; 56 import org.thingsboard.server.dao.relation.RelationService;
57 -import org.thingsboard.server.dao.resource.TbResourceService; 57 +import org.thingsboard.server.dao.resource.ResourceService;
58 import org.thingsboard.server.dao.rule.RuleChainService; 58 import org.thingsboard.server.dao.rule.RuleChainService;
59 import org.thingsboard.server.dao.settings.AdminSettingsService; 59 import org.thingsboard.server.dao.settings.AdminSettingsService;
60 import org.thingsboard.server.dao.tenant.TenantProfileService; 60 import org.thingsboard.server.dao.tenant.TenantProfileService;
@@ -152,9 +152,9 @@ public abstract class AbstractServiceTest { @@ -152,9 +152,9 @@ public abstract class AbstractServiceTest {
152 protected DeviceProfileService deviceProfileService; 152 protected DeviceProfileService deviceProfileService;
153 153
154 @Autowired 154 @Autowired
155 - protected TbResourceService resourceService; 155 + protected ResourceService resourceService;
156 156
157 - class IdComparator<D extends HasId> implements Comparator<D> { 157 + public class IdComparator<D extends HasId> implements Comparator<D> {
158 @Override 158 @Override
159 public int compare(D o1, D o2) { 159 public int compare(D o1, D o2) {
160 return o1.getId().getId().compareTo(o2.getId().getId()); 160 return o1.getId().getId().compareTo(o2.getId().getId());