Search in sources :

Example 1 with SortCriterion

use of com.liferay.faces.util.model.SortCriterion in project liferay-faces-alloy by liferay.

the class DataTableRenderer method decodeSortCriteria.

protected void decodeSortCriteria(FacesContext facesContext, Map<String, String> requestParameterMap, DataTable dataTable, String dataTableClientId) {
    Object dataTableValue = dataTable.getValue();
    if ((dataTableValue != null) && (dataTableValue instanceof Sortable)) {
        String sortColumnClientIdParamName = dataTableClientId.concat("_sortColumnClientId");
        String sortColumnClientId = requestParameterMap.get(sortColumnClientIdParamName);
        if (sortColumnClientId != null) {
            List<Column> alloySortColumns = new ArrayList<Column>();
            String eventMetaKeyParamName = dataTableClientId.concat("_eventMetaKey");
            boolean eventMetaKey = BooleanHelper.toBoolean(requestParameterMap.get(eventMetaKeyParamName));
            boolean multiColumnSort = dataTable.isMultiColumnSort();
            List<UIComponent> children = dataTable.getChildren();
            for (UIComponent child : children) {
                if (child instanceof Column) {
                    Column alloyColumn = (Column) child;
                    String alloyColumndId = alloyColumn.getClientId(facesContext);
                    String alloyColumnSortOrder = alloyColumn.getSortOrder();
                    Map<String, Object> alloyColumnAttributes = alloyColumn.getAttributes();
                    if (alloyColumndId.equals(sortColumnClientId)) {
                        // Toggle the value from ascending->descending or from descending->ascending.
                        SortCriterion.Order sortCriterionOrder;
                        if ("ASCENDING".equals(alloyColumnSortOrder)) {
                            sortCriterionOrder = SortCriterion.Order.DESCENDING;
                        } else {
                            sortCriterionOrder = SortCriterion.Order.ASCENDING;
                        }
                        // Set the state of the column so that the sort indicator will appear correctly.
                        alloyColumn.setSortOrder(sortCriterionOrder.toString());
                        alloyColumnAttributes.put("sortTime", System.currentTimeMillis());
                        // Add the sort criterion to the list of sort criteria.
                        alloySortColumns.add(alloyColumn);
                    } else {
                        if (multiColumnSort && eventMetaKey) {
                            if (alloyColumnSortOrder != null) {
                                alloySortColumns.add(alloyColumn);
                            }
                        } else {
                            alloyColumn.setSortOrder(null);
                            alloyColumnAttributes.remove("sortTime");
                        }
                    }
                }
            }
            Collections.sort(alloySortColumns, new ColumnSortTimeComparator());
            List<SortCriterion> sortCriteria = new ArrayList<SortCriterion>();
            for (Column alloyColumn : alloySortColumns) {
                String alloyColumnFieldName = getAlloyColumnFieldName(alloyColumn);
                String alloyColumnSortOrder = alloyColumn.getSortOrder();
                SortCriterion.Order sortCriterionOrder;
                if ("ASCENDING".equals(alloyColumnSortOrder)) {
                    sortCriterionOrder = SortCriterion.Order.ASCENDING;
                } else {
                    sortCriterionOrder = SortCriterion.Order.DESCENDING;
                }
                SortCriterion sortCriterion = new SortCriterion(alloyColumnFieldName, sortCriterionOrder);
                sortCriteria.add(sortCriterion);
            }
            if (logger.isDebugEnabled()) {
                for (SortCriterion sortCriterion : sortCriteria) {
                    logger.debug("sortCriterion columnId=[{0}], order=[{1}]", sortCriterion.getColumnId(), sortCriterion.getOrder());
                }
            }
            Sortable sortable = (Sortable) dataTableValue;
            sortable.setSortCriteria(sortCriteria);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) UIComponent(javax.faces.component.UIComponent) Sortable(com.liferay.faces.util.model.Sortable) HtmlColumn(javax.faces.component.html.HtmlColumn) UIColumn(javax.faces.component.UIColumn) Column(com.liferay.faces.alloy.component.column.Column) SortCriterion(com.liferay.faces.util.model.SortCriterion)

Aggregations

Column (com.liferay.faces.alloy.component.column.Column)1 SortCriterion (com.liferay.faces.util.model.SortCriterion)1 Sortable (com.liferay.faces.util.model.Sortable)1 ArrayList (java.util.ArrayList)1 UIColumn (javax.faces.component.UIColumn)1 UIComponent (javax.faces.component.UIComponent)1 HtmlColumn (javax.faces.component.html.HtmlColumn)1