updated GET /resources api to filter resources by type
This commit is contained in:
parent
07ff1ab44a
commit
b083ab98e0
@ -140,6 +140,9 @@ public class ControllerConstants {
|
||||
|
||||
protected static final String RESOURCE_TEXT_SEARCH_DESCRIPTION = "The case insensitive 'substring' filter based on the resource title.";
|
||||
protected static final String RESOURCE_SORT_PROPERTY_ALLOWABLE_VALUES = "createdTime, title, resourceType, tenantId";
|
||||
protected static final String RESOURCE_TYPE_PROPERTY_ALLOWABLE_VALUES = "LWM2M_MODEL, JKS, PKCS_12, JS_MODULE";
|
||||
protected static final String RESOURCE_TYPE = "A string value representing the resource type.";
|
||||
|
||||
protected static final String LWM2M_OBJECT_DESCRIPTION = "LwM2M Object is a object that includes information about the LwM2M model which can be used in transport configuration for the LwM2M device profile. ";
|
||||
protected static final String LWM2M_OBJECT_SORT_PROPERTY_ALLOWABLE_VALUES = "id, name";
|
||||
|
||||
|
||||
@ -31,6 +31,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.thingsboard.server.common.data.ResourceType;
|
||||
import org.thingsboard.server.common.data.StringUtils;
|
||||
import org.thingsboard.server.common.data.TbResource;
|
||||
import org.thingsboard.server.common.data.TbResourceInfo;
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
|
||||
@ -57,6 +59,8 @@ import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_ID_
|
||||
import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_INFO_DESCRIPTION;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_SORT_PROPERTY_ALLOWABLE_VALUES;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_TEXT_SEARCH_DESCRIPTION;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_TYPE;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_TYPE_PROPERTY_ALLOWABLE_VALUES;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_ALLOWABLE_VALUES;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_DESCRIPTION;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.SORT_PROPERTY_DESCRIPTION;
|
||||
@ -153,6 +157,8 @@ public class TbResourceController extends BaseController {
|
||||
@RequestParam int pageSize,
|
||||
@ApiParam(value = PAGE_NUMBER_DESCRIPTION, required = true)
|
||||
@RequestParam int page,
|
||||
@ApiParam(value = RESOURCE_TYPE, allowableValues = RESOURCE_TYPE_PROPERTY_ALLOWABLE_VALUES)
|
||||
@RequestParam(required = false) String resourceType,
|
||||
@ApiParam(value = RESOURCE_TEXT_SEARCH_DESCRIPTION)
|
||||
@RequestParam(required = false) String textSearch,
|
||||
@ApiParam(value = SORT_PROPERTY_DESCRIPTION, allowableValues = RESOURCE_SORT_PROPERTY_ALLOWABLE_VALUES)
|
||||
@ -161,11 +167,19 @@ public class TbResourceController extends BaseController {
|
||||
@RequestParam(required = false) String sortOrder) throws ThingsboardException {
|
||||
PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder);
|
||||
if (Authority.SYS_ADMIN.equals(getCurrentUser().getAuthority())) {
|
||||
if (StringUtils.isNotEmpty(resourceType)){
|
||||
return checkNotNull(resourceService.findTenantResourcesByType(getTenantId(), ResourceType.valueOf(resourceType), pageLink));
|
||||
} else {
|
||||
return checkNotNull(resourceService.findTenantResourcesByTenantId(getTenantId(), pageLink));
|
||||
}
|
||||
} else {
|
||||
if (StringUtils.isNotEmpty(resourceType)){
|
||||
return checkNotNull(resourceService.findAllTenantResourcesByType(getTenantId(), ResourceType.valueOf(resourceType), pageLink));
|
||||
} else {
|
||||
return checkNotNull(resourceService.findAllTenantResourcesByTenantId(getTenantId(), pageLink));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ApiOperation(value = "Get LwM2M Objects (getLwm2mListObjectsPage)",
|
||||
notes = "Returns a page of LwM2M objects parsed from Resources with type 'LWM2M_MODEL' owned by tenant or sysadmin. " +
|
||||
|
||||
@ -85,6 +85,16 @@ public class DefaultTbResourceService extends AbstractTbEntityService implements
|
||||
return resourceService.findTenantResourcesByTenantId(tenantId, pageLink);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<TbResourceInfo> findAllTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink) {
|
||||
return resourceService.findAllTenantResourcesByType(tenantId, resourceType, pageLink);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<TbResourceInfo> findTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink) {
|
||||
return resourceService.findTenantResourcesByType(tenantId, resourceType, pageLink);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LwM2mObject> findLwM2mObject(TenantId tenantId, String sortOrder, String sortProperty, String[] objectIds) {
|
||||
log.trace("Executing findByTenantId [{}]", tenantId);
|
||||
|
||||
@ -39,6 +39,10 @@ public interface TbResourceService extends SimpleTbEntityService<TbResource> {
|
||||
|
||||
PageData<TbResourceInfo> findTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink);
|
||||
|
||||
PageData<TbResourceInfo> findAllTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink);
|
||||
|
||||
PageData<TbResourceInfo> findTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink);
|
||||
|
||||
List<LwM2mObject> findLwM2mObject(TenantId tenantId,
|
||||
String sortOrder,
|
||||
String sortProperty,
|
||||
|
||||
@ -47,6 +47,7 @@ public class TbResourceControllerTest extends AbstractControllerTest {
|
||||
private IdComparator<TbResourceInfo> idComparator = new IdComparator<>();
|
||||
|
||||
private static final String DEFAULT_FILE_NAME = "test.jks";
|
||||
private static final String DEFAULT_FILE_NAME_2 = "test2.jks";
|
||||
|
||||
private Tenant savedTenant;
|
||||
private User tenantAdmin;
|
||||
@ -242,6 +243,54 @@ public class TbResourceControllerTest extends AbstractControllerTest {
|
||||
Assert.assertEquals(resources, loadedResources);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindTenantTbResourcesByType() throws Exception {
|
||||
Mockito.reset(tbClusterService, auditLogService);
|
||||
|
||||
List<TbResourceInfo> resources = new ArrayList<>();
|
||||
int jksCntEntity = 17;
|
||||
for (int i = 0; i < jksCntEntity; i++) {
|
||||
TbResource resource = new TbResource();
|
||||
resource.setTitle("JKS Resource" + i);
|
||||
resource.setResourceType(ResourceType.JKS);
|
||||
resource.setFileName(i + DEFAULT_FILE_NAME);
|
||||
resource.setData("Test Data");
|
||||
resources.add(new TbResourceInfo(save(resource)));
|
||||
}
|
||||
|
||||
int lwm2mCntEntity = 19;
|
||||
for (int i = 0; i < lwm2mCntEntity; i++) {
|
||||
TbResource resource = new TbResource();
|
||||
resource.setTitle("LWM2M Resource" + i);
|
||||
resource.setResourceType(ResourceType.PKCS_12);
|
||||
resource.setFileName(i + DEFAULT_FILE_NAME_2);
|
||||
resource.setData("Test Data");
|
||||
save(resource);
|
||||
}
|
||||
|
||||
List<TbResourceInfo> loadedResources = new ArrayList<>();
|
||||
PageLink pageLink = new PageLink(5);
|
||||
PageData<TbResourceInfo> pageData;
|
||||
do {
|
||||
pageData = doGetTypedWithPageLink("/api/resource?resourceType=" + ResourceType.JKS.name() + "&",
|
||||
new TypeReference<>() {
|
||||
}, pageLink);
|
||||
loadedResources.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageLink.nextPageLink();
|
||||
}
|
||||
} while (pageData.hasNext());
|
||||
|
||||
testNotifyManyEntityManyTimeMsgToEdgeServiceNever(new TbResource(), new TbResource(),
|
||||
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(),
|
||||
ActionType.ADDED, jksCntEntity + lwm2mCntEntity);
|
||||
|
||||
Collections.sort(resources, idComparator);
|
||||
Collections.sort(loadedResources, idComparator);
|
||||
|
||||
Assert.assertEquals(resources, loadedResources);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindSystemTbResources() throws Exception {
|
||||
loginSysAdmin();
|
||||
@ -300,6 +349,76 @@ public class TbResourceControllerTest extends AbstractControllerTest {
|
||||
Assert.assertTrue(loadedResources.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindSystemTbResourcesByType() throws Exception {
|
||||
loginSysAdmin();
|
||||
|
||||
List<TbResourceInfo> resources = new ArrayList<>();
|
||||
int jksCntEntity = 17;
|
||||
for (int i = 0; i < jksCntEntity; i++) {
|
||||
TbResource resource = new TbResource();
|
||||
resource.setTitle("JKS Resource" + i);
|
||||
resource.setResourceType(ResourceType.JKS);
|
||||
resource.setFileName(i + DEFAULT_FILE_NAME);
|
||||
resource.setData("Test Data");
|
||||
resources.add(new TbResourceInfo(save(resource)));
|
||||
}
|
||||
|
||||
int lwm2mCntEntity = 19;
|
||||
for (int i = 0; i < lwm2mCntEntity; i++) {
|
||||
TbResource resource = new TbResource();
|
||||
resource.setTitle("LWM2M Resource" + i);
|
||||
resource.setResourceType(ResourceType.PKCS_12);
|
||||
resource.setFileName(i + DEFAULT_FILE_NAME_2);
|
||||
resource.setData("Test Data");
|
||||
save(resource);
|
||||
}
|
||||
|
||||
List<TbResourceInfo> loadedResources = new ArrayList<>();
|
||||
PageLink pageLink = new PageLink(30);
|
||||
PageData<TbResourceInfo> pageData;
|
||||
do {
|
||||
pageData = doGetTypedWithPageLink("/api/resource?resourceType=" + ResourceType.JKS + "&",
|
||||
new TypeReference<>() {
|
||||
}, pageLink);
|
||||
loadedResources.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageLink.nextPageLink();
|
||||
}
|
||||
} while (pageData.hasNext());
|
||||
|
||||
Collections.sort(resources, idComparator);
|
||||
Collections.sort(loadedResources, idComparator);
|
||||
|
||||
Assert.assertEquals(resources, loadedResources);
|
||||
|
||||
Mockito.reset(tbClusterService, auditLogService);
|
||||
|
||||
int cntEntity = resources.size();
|
||||
for (TbResourceInfo resource : resources) {
|
||||
doDelete("/api/resource/" + resource.getId().getId().toString())
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
testNotifyManyEntityManyTimeMsgToEdgeServiceNeverAdditionalInfoAny(new TbResource(), new TbResource(),
|
||||
resources.get(0).getTenantId(), null, null, SYS_ADMIN_EMAIL,
|
||||
ActionType.DELETED, cntEntity, 1);
|
||||
|
||||
pageLink = new PageLink(27);
|
||||
loadedResources.clear();
|
||||
do {
|
||||
pageData = doGetTypedWithPageLink("/api/resource?resourceType=" + ResourceType.JKS + "&",
|
||||
new TypeReference<>() {
|
||||
}, pageLink);
|
||||
loadedResources.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageLink.nextPageLink();
|
||||
}
|
||||
} while (pageData.hasNext());
|
||||
|
||||
Assert.assertTrue(loadedResources.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindSystemAndTenantTbResources() throws Exception {
|
||||
List<TbResourceInfo> systemResources = new ArrayList<>();
|
||||
|
||||
@ -43,6 +43,10 @@ public interface ResourceService extends EntityDaoService {
|
||||
|
||||
PageData<TbResourceInfo> findTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink);
|
||||
|
||||
PageData<TbResourceInfo> findAllTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink);
|
||||
|
||||
PageData<TbResourceInfo> findTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink);
|
||||
|
||||
List<TbResource> findTenantResourcesByResourceTypeAndObjectIds(TenantId tenantId, ResourceType lwm2mModel, String[] objectIds);
|
||||
|
||||
PageData<TbResource> findTenantResourcesByResourceTypeAndPageLink(TenantId tenantId, ResourceType lwm2mModel, PageLink pageLink);
|
||||
|
||||
@ -16,5 +16,5 @@
|
||||
package org.thingsboard.server.common.data;
|
||||
|
||||
public enum ResourceType {
|
||||
LWM2M_MODEL, JKS, PKCS_12
|
||||
LWM2M_MODEL, JKS, PKCS_12, JS_MODULE
|
||||
}
|
||||
|
||||
@ -116,6 +116,20 @@ public class BaseResourceService implements ResourceService {
|
||||
return resourceInfoDao.findTenantResourcesByTenantId(tenantId.getId(), pageLink);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<TbResourceInfo> findAllTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink) {
|
||||
log.trace("Executing findAllTenantResourcesByType [{}]", tenantId);
|
||||
validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
|
||||
return resourceInfoDao.findAllTenantResourcesByType(tenantId.getId(), resourceType.name(), pageLink);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<TbResourceInfo> findTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink) {
|
||||
log.trace("Executing findTenantResourcesByType [{}]", tenantId);
|
||||
validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
|
||||
return resourceInfoDao.findTenantResourcesByType(tenantId.getId(), resourceType.name(), pageLink);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TbResource> findTenantResourcesByResourceTypeAndObjectIds(TenantId tenantId, ResourceType resourceType, String[] objectIds) {
|
||||
log.trace("Executing findTenantResourcesByResourceTypeAndObjectIds [{}][{}][{}]", tenantId, resourceType, objectIds);
|
||||
|
||||
@ -28,4 +28,8 @@ public interface TbResourceInfoDao extends Dao<TbResourceInfo> {
|
||||
|
||||
PageData<TbResourceInfo> findTenantResourcesByTenantId(UUID tenantId, PageLink pageLink);
|
||||
|
||||
PageData<TbResourceInfo> findAllTenantResourcesByType(UUID tenantId, String resourceType, PageLink pageLink);
|
||||
|
||||
PageData<TbResourceInfo> findTenantResourcesByType(UUID tenantId, String resourceType, PageLink pageLink);
|
||||
|
||||
}
|
||||
|
||||
@ -68,4 +68,25 @@ public class JpaTbResourceInfoDao extends JpaAbstractSearchTextDao<TbResourceInf
|
||||
Objects.toString(pageLink.getTextSearch(), ""),
|
||||
DaoUtil.toPageable(pageLink)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<TbResourceInfo> findAllTenantResourcesByType(UUID tenantId, String resourceType, PageLink pageLink) {
|
||||
return DaoUtil.toPageData(resourceInfoRepository
|
||||
.findAllTenantResourcesByType(
|
||||
tenantId,
|
||||
TenantId.NULL_UUID,
|
||||
resourceType,
|
||||
Objects.toString(pageLink.getTextSearch(), ""),
|
||||
DaoUtil.toPageable(pageLink)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<TbResourceInfo> findTenantResourcesByType(UUID tenantId, String resourceType, PageLink pageLink) {
|
||||
return DaoUtil.toPageData(resourceInfoRepository
|
||||
.findTenantResourcesByType(
|
||||
tenantId,
|
||||
resourceType,
|
||||
Objects.toString(pageLink.getTextSearch(), ""),
|
||||
DaoUtil.toPageable(pageLink)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,4 +46,29 @@ public interface TbResourceInfoRepository extends JpaRepository<TbResourceInfoEn
|
||||
Page<TbResourceInfoEntity> findTenantResourcesByTenantId(@Param("tenantId") UUID tenantId,
|
||||
@Param("searchText") String searchText,
|
||||
Pageable pageable);
|
||||
|
||||
@Query("SELECT tr FROM TbResourceInfoEntity tr WHERE " +
|
||||
"LOWER(tr.title) LIKE LOWER(CONCAT('%', :searchText, '%'))" +
|
||||
"AND (tr.tenantId = :tenantId " +
|
||||
"OR (tr.tenantId = :systemAdminId " +
|
||||
"AND NOT EXISTS " +
|
||||
"(SELECT sr FROM TbResourceEntity sr " +
|
||||
"WHERE sr.tenantId = :tenantId " +
|
||||
"AND tr.resourceType = sr.resourceType " +
|
||||
"AND tr.resourceKey = sr.resourceKey)))" +
|
||||
"AND tr.resourceType = :resourceType")
|
||||
Page<TbResourceInfoEntity> findAllTenantResourcesByType(@Param("tenantId") UUID tenantId,
|
||||
@Param("systemAdminId") UUID sysadminId,
|
||||
@Param("resourceType") String resourceType,
|
||||
@Param("searchText") String searchText,
|
||||
Pageable pageable);
|
||||
|
||||
@Query("SELECT ri FROM TbResourceInfoEntity ri WHERE " +
|
||||
"ri.tenantId = :tenantId " +
|
||||
"AND ri.resourceType = :resourceType " +
|
||||
"AND LOWER(ri.title) LIKE LOWER(CONCAT('%', :searchText, '%'))")
|
||||
Page<TbResourceInfoEntity> findTenantResourcesByType(@Param("tenantId") UUID tenantId,
|
||||
@Param("resourceType") String resourceType,
|
||||
@Param("searchText") String searchText,
|
||||
Pageable pageable);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user