fix #548 : Multipe default sort does not work

This commit is contained in:
yfujita 2016-07-08 16:53:12 +09:00
parent 4b945297c8
commit 526ff5e264
2 changed files with 31 additions and 14 deletions

View file

@ -19,6 +19,8 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@ -156,8 +158,24 @@ public abstract class FessSearchAction extends FessBaseAction {
// sort
if (StringUtil.isBlank(form.sort)) {
final String[] defaultSortValues = fessConfig.getDefaultSortValues(getUserBean());
if (defaultSortValues.length > 0) {
form.sort = String.join(",", defaultSortValues);
if (defaultSortValues.length == 1) {
form.sort = defaultSortValues[0];
} else if (defaultSortValues.length >= 2) {
final StringBuilder sortValueSb = new StringBuilder();
final Set<String> sortFieldNames = new HashSet<>();
for (final String defaultSortValue : defaultSortValues) {
for (final String singleValue : defaultSortValue.split(",")) {
final String sortFieldName = singleValue.split("\\.")[0];
if (!sortFieldNames.contains(sortFieldName)) {
sortFieldNames.add(sortFieldName);
if (sortValueSb.length() > 0) {
sortValueSb.append(",");
}
sortValueSb.append(singleValue);
}
}
}
form.sort = sortValueSb.toString();
}
}
}

View file

@ -132,14 +132,14 @@ public interface FessProp {
public default String[] getDefaultSortValues(final OptionalThing<FessUserBean> userBean) {
@SuppressWarnings("unchecked")
Map<String, String> map = (Map<String, String>) propMap.get(DEFAULT_SORT_VALUES);
if (map == null) {
List<Pair<String, String>> list = (List<Pair<String, String>>) propMap.get(DEFAULT_SORT_VALUES);
if (list == null) {
final String value = getSystemProperty(Constants.DEFAULT_SORT_VALUE_PROPERTY);
if (StringUtil.isBlank(value)) {
map = Collections.emptyMap();
list = Collections.emptyList();
} else {
final Set<String> keySet = new HashSet<>();
map = stream(value.split("\n")).get(stream -> (Map<String, String>) stream.filter(StringUtil::isNotBlank).map(s -> {
list = stream(value.split("\n")).get(stream -> stream.filter(StringUtil::isNotBlank).map(s -> {
final String[] pair = s.split("=");
if (pair.length == 1) {
return new Pair<>(StringUtil.EMPTY, pair[0].trim());
@ -151,23 +151,22 @@ public interface FessProp {
return new Pair<>(pair[0].trim(), sortValue);
}
return null;
}).filter(o -> o != null && keySet.add(o.getFirst())).collect(Collectors.toMap(Pair::getFirst, d -> d.getSecond())));
}).filter(o -> o != null && keySet.add(o.getFirst())).collect(Collectors.toList()));
}
propMap.put(DEFAULT_SORT_VALUES, map);
propMap.put(DEFAULT_SORT_VALUES, list);
}
return map
.entrySet()
return list
.stream()
.map(e -> {
final String key = e.getKey();
.map(p -> {
final String key = p.getFirst();
if (StringUtil.isEmpty(key)) {
return e.getValue();
return p.getSecond();
}
if (userBean.map(
user -> stream(user.getRoles()).get(stream -> stream.anyMatch(s -> key.equals(ROLE_VALUE_PREFIX + s)))
|| stream(user.getGroups()).get(stream -> stream.anyMatch(s -> key.equals(GROUP_VALUE_PREFIX + s))))
.orElse(false)) {
return e.getValue();
return p.getSecond();
}
return null;
}).filter(StringUtil::isNotBlank).toArray(n -> new String[n]);