Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* The MIT License
*
* Copyright (c) 2009-2023 PrimeTek Informatics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.primefaces.component.datatable.feature;
import java.io.IOException;
import java.text.Collator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;
import org.primefaces.PrimeFaces;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.component.datatable.DataTableRenderer;
import org.primefaces.component.datatable.DataTableState;
import org.primefaces.event.data.PostSortEvent;
import org.primefaces.model.SortMeta;
import org.primefaces.model.SortOrder;
import org.primefaces.util.ComponentUtils;
public class SortFeature implements DataTableFeature {
private boolean isSortRequest(FacesContext context, DataTable table) {
return context.getExternalContext().getRequestParameterMap().containsKey(table.getClientId(context) + "_sorting");
}
@Override
public void decode(FacesContext context, DataTable table) {
String clientId = table.getClientId(context);
Map params = context.getExternalContext().getRequestParameterMap();
String sortKey = params.get(clientId + "_sortKey");
String sortDir = params.get(clientId + "_sortDir");
String[] sortKeys = sortKey.split(",");
String[] sortOrders = sortDir.split(",");
if (sortKeys.length != sortOrders.length) {
throw new FacesException("sortKeys != sortDirs");
}
Map sortByMap = table.getSortByAsMap();
Map sortKeysIndexes = IntStream.range(0, sortKeys.length).boxed()
.collect(Collectors.toMap(i -> sortKeys[i], i -> i));
for (Map.Entry entry : sortByMap.entrySet()) {
SortMeta sortMeta = entry.getValue();
if (sortMeta.isHeaderRow()) {
continue;
}
Integer index = sortKeysIndexes.get(entry.getKey());
if (index != null) {
sortMeta.setOrder(SortOrder.of(sortOrders[index]));
sortMeta.setPriority(index);
}
else {
sortMeta.setOrder(SortOrder.UNSORTED);
sortMeta.setPriority(SortMeta.MIN_PRIORITY);
}
}
}
@Override
public void encode(FacesContext context, DataTableRenderer renderer, DataTable table) throws IOException {
table.setFirst(0);
if (!table.loadLazyDataIfEnabled()) {
//reset the value given in the filter feature property before sorting
if (table.isFullUpdateRequest(context)) {
table.setValue(null);
}
sort(context, table);
if (table.isPaginator() && ComponentUtils.isRequestSource(table, context)) {
PrimeFaces.current().ajax().addCallbackParam("totalRecords", table.getRowCount());
}
//update filtered value accordingly to take account sorting
if (table.isFilteringCurrentlyActive()) {
if (table.isFullUpdateRequest(context)) {
DataTableFeatures.filterFeature().filter(context, table);
}
else {
table.setFilteredValue(resolveList(table.getValue()));
}
}
}
context.getApplication().publishEvent(context, PostSortEvent.class, table);
if (!table.isFullUpdateRequest(context)) {
renderer.encodeTbody(context, table, true);
}
if (table.isMultiViewState()) {
Map sortMeta = table.getSortByAsMap();
if (!sortMeta.isEmpty()) {
DataTableState ts = table.getMultiViewState(true);
ts.setSortBy(sortMeta);
if (table.isPaginator()) {
ts.setFirst(table.getFirst());
ts.setRows(table.getRows());
}
}
}
}
public void sort(FacesContext context, DataTable table) {
Object value = table.getValue();
if (value == null) {
return;
}
List list = resolveList(value);
Locale locale = table.resolveDataLocale();
String var = table.getVar();
Collator collator = Collator.getInstance(locale);
AtomicInteger comparisonResult = new AtomicInteger();
Map sortBy = table.getActiveSortMeta();
Object varBackup = context.getExternalContext().getRequestMap().get(var);
list.sort((o1, o2) -> {
for (SortMeta sortMeta : sortBy.values()) {
comparisonResult.set(0);
if (sortMeta.isHeaderRow()) {
int result = compare(context, var, sortMeta, o1, o2, collator, locale);
comparisonResult.set(result);
}
else {
// Currently ColumnGrouping supports ui:repeat, therefore we have to use a callback
// and can't use sortMeta.getComponent()
// Later when we refactored ColumnGrouping, we may remove #invokeOnColumn as we dont support ui:repeat in other cases
table.invokeOnColumn(sortMeta.getColumnKey(), column -> {
int result = compare(context, var, sortMeta, o1, o2, collator, locale);
comparisonResult.set(result);
});
}
if (comparisonResult.get() != 0) {
return comparisonResult.get();
}
}
return 0;
});
if (varBackup == null) {
context.getExternalContext().getRequestMap().remove(var);
}
else {
context.getExternalContext().getRequestMap().put(var, varBackup);
}
}
public static int compare(FacesContext context, String var, SortMeta sortMeta, Object o1, Object o2,
Collator collator, Locale locale) {
ValueExpression ve = sortMeta.getSortBy();
context.getExternalContext().getRequestMap().put(var, o1);
Object value1 = ve.getValue(context.getELContext());
context.getExternalContext().getRequestMap().put(var, o2);
Object value2 = ve.getValue(context.getELContext());
return compare(context, sortMeta, value1, value2, collator, locale);
}
public static int compare(FacesContext context, SortMeta sortMeta, Object value1, Object value2,
Collator collator, Locale locale) {
try {
int result;
if (sortMeta.getFunction() == null) {
//Empty check
if (value1 == null && value2 == null) {
result = 0;
}
else if (value1 == null) {
result = sortMeta.getNullSortOrder();
}
else if (value2 == null) {
result = -1 * sortMeta.getNullSortOrder();
}
else if (value1 instanceof String && value2 instanceof String) {
if (sortMeta.isCaseSensitiveSort()) {
result = collator.compare(value1, value2);
}
else {
String str1 = (((String) value1).toLowerCase(locale));
String str2 = (((String) value2).toLowerCase(locale));
result = collator.compare(str1, str2);
}
}
else {
result = ((Comparable