added default sorting if not exists
This commit is contained in:
parent
e733a092cb
commit
a031104a0b
@ -19,6 +19,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -66,18 +67,13 @@ public class PageLink {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Sort toSort(SortOrder sortOrder, Map<String,String> columnMap) {
|
public Sort toSort(SortOrder sortOrder, Map<String,String> columnMap) {
|
||||||
if (sortOrder == null) {
|
return toSort(new ArrayList<>(List.of(sortOrder)), columnMap);
|
||||||
return DEFAULT_SORT;
|
|
||||||
} else {
|
|
||||||
String property = sortOrder.getProperty();
|
|
||||||
if (columnMap.containsKey(property)) {
|
|
||||||
property = columnMap.get(property);
|
|
||||||
}
|
|
||||||
return Sort.by(Sort.Direction.fromString(sortOrder.getDirection().name()), property);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Sort toSort(List<SortOrder> sortOrders, Map<String,String> columnMap) {
|
public Sort toSort(List<SortOrder> sortOrders, Map<String,String> columnMap) {
|
||||||
|
if (!isDefaultSortOrderAvailable(sortOrders)) {
|
||||||
|
sortOrders.add(new SortOrder(DEFAULT_SORT_PROPERTY, SortOrder.Direction.ASC));
|
||||||
|
}
|
||||||
return Sort.by(sortOrders.stream().map(s -> toSortOrder(s, columnMap)).collect(Collectors.toList()));
|
return Sort.by(sortOrders.stream().map(s -> toSortOrder(s, columnMap)).collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,4 +85,13 @@ public class PageLink {
|
|||||||
return new Sort.Order(Sort.Direction.fromString(sortOrder.getDirection().name()), property, Sort.NullHandling.NULLS_LAST);
|
return new Sort.Order(Sort.Direction.fromString(sortOrder.getDirection().name()), property, Sort.NullHandling.NULLS_LAST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isDefaultSortOrderAvailable(List<SortOrder> sortOrders) {
|
||||||
|
for (SortOrder sortOrder : sortOrders) {
|
||||||
|
if (DEFAULT_SORT_PROPERTY.equals(sortOrder.getProperty())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -76,20 +76,4 @@ public class TimePageLink extends PageLink {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Sort toSort(List<SortOrder> sortOrders, Map<String,String> columnMap) {
|
|
||||||
if (!isDefaultSortOrderAvailable(sortOrders)) {
|
|
||||||
sortOrders.add(new SortOrder(DEFAULT_SORT_PROPERTY, SortOrder.Direction.ASC));
|
|
||||||
}
|
|
||||||
return super.toSort(sortOrders, columnMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isDefaultSortOrderAvailable(List<SortOrder> sortOrders) {
|
|
||||||
for (SortOrder sortOrder : sortOrders) {
|
|
||||||
if (DEFAULT_SORT_PROPERTY.equals(sortOrder.getProperty())) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
package org.thingsboard.server.dao.sql.widget;
|
package org.thingsboard.server.dao.sql.widget;
|
||||||
|
|
||||||
import com.datastax.oss.driver.api.core.uuid.Uuids;
|
import com.datastax.oss.driver.api.core.uuid.Uuids;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -43,6 +44,7 @@ import java.util.Comparator;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
@ -160,6 +162,29 @@ public class JpaWidgetTypeDaoTest extends AbstractJpaDaoTest {
|
|||||||
assertEquals(new WidgetTypeInfo(widgetTypeList.get(2)), widgetTypes.getData().get(0));
|
assertEquals(new WidgetTypeInfo(widgetTypeList.get(2)), widgetTypes.getData().get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindSystemWidgetTypesForSameName() throws InterruptedException {
|
||||||
|
List<WidgetTypeDetails> widgetTypeList = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < 20; i++) {
|
||||||
|
Thread.sleep(2);
|
||||||
|
var widgetType = saveWidgetType(TenantId.SYS_TENANT_ID, "widgetName");
|
||||||
|
widgetTypeList.add(widgetType);
|
||||||
|
}
|
||||||
|
widgetTypeList.sort(Comparator.comparing(BaseWidgetType::getName).thenComparing((BaseWidgetType baseWidgetType) -> baseWidgetType.getId().getId()));
|
||||||
|
List<WidgetTypeInfo> expected = widgetTypeList.stream().map(WidgetTypeInfo::new).collect(Collectors.toList());
|
||||||
|
|
||||||
|
PageData<WidgetTypeInfo> widgetTypesFirstPage = widgetTypeDao.findSystemWidgetTypes(TenantId.SYS_TENANT_ID, true, DeprecatedFilter.ALL, Collections.singletonList("static"),
|
||||||
|
new PageLink(10, 0, null, new SortOrder("name")));
|
||||||
|
assertEquals(10, widgetTypesFirstPage.getData().size());
|
||||||
|
assertThat(widgetTypesFirstPage.getData()).containsExactlyElementsOf(expected.subList(0, 10));
|
||||||
|
|
||||||
|
PageData<WidgetTypeInfo> widgetTypesSecondPage = widgetTypeDao.findSystemWidgetTypes(TenantId.SYS_TENANT_ID, true, DeprecatedFilter.ALL, Collections.singletonList("static"),
|
||||||
|
new PageLink(10, 0, null, new SortOrder("name")));
|
||||||
|
assertEquals(10, widgetTypesSecondPage.getData().size());
|
||||||
|
assertThat(widgetTypesSecondPage.getData()).containsExactlyElementsOf(expected.subList(10, 20));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTagsSearchInFindBySystemWidgetTypes() {
|
public void testTagsSearchInFindBySystemWidgetTypes() {
|
||||||
for (var entry : SHOULD_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
|
for (var entry : SHOULD_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
|
||||||
@ -355,4 +380,14 @@ public class JpaWidgetTypeDaoTest extends AbstractJpaDaoTest {
|
|||||||
assertEquals(0, result.size());
|
assertEquals(0, result.size());
|
||||||
widgetTypeDao.removeById(tenantId, details.getUuidId());
|
widgetTypeDao.removeById(tenantId, details.getUuidId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private WidgetTypeDetails saveWidgetType(TenantId tenantId, String name) {
|
||||||
|
WidgetTypeDetails widgetType = new WidgetTypeDetails();
|
||||||
|
widgetType.setTenantId(tenantId);
|
||||||
|
widgetType.setName(name);
|
||||||
|
var descriptor = JacksonUtil.newObjectNode();
|
||||||
|
descriptor.put("type","static");
|
||||||
|
widgetType.setDescriptor(descriptor);
|
||||||
|
return widgetTypeDao.save(TenantId.SYS_TENANT_ID, widgetType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user