From b3db4da7051078cd2971b9ebb65bb236c8b04725 Mon Sep 17 00:00:00 2001
From: Sonia Zorba <zorba@oats.inaf.it>
Date: Fri, 2 Feb 2018 12:30:34 +0100
Subject: [PATCH] webapp sort columns using column index before alphabetical
 order

---
 .../ia2/tsm/webapp/TapSchemaEditingBean.java  | 43 +++++++++++++++++++
 .../webapp/resources/js/columns-sorter.js     |  2 +-
 .../src/main/webapp/tapSchemaEditing.xhtml    |  2 +-
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/TASMAN-webapp/src/main/java/it/inaf/ia2/tsm/webapp/TapSchemaEditingBean.java b/TASMAN-webapp/src/main/java/it/inaf/ia2/tsm/webapp/TapSchemaEditingBean.java
index bedb38c..82f4940 100644
--- a/TASMAN-webapp/src/main/java/it/inaf/ia2/tsm/webapp/TapSchemaEditingBean.java
+++ b/TASMAN-webapp/src/main/java/it/inaf/ia2/tsm/webapp/TapSchemaEditingBean.java
@@ -39,6 +39,8 @@ import it.inaf.ia2.tsm.UpdateOperations;
 import java.io.Serializable;
 import java.sql.SQLException;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Objects;
 import javax.faces.context.FacesContext;
@@ -517,4 +519,45 @@ public class TapSchemaEditingBean implements Serializable {
             }
         }).getString();
     }
+
+    /**
+     * Returns the columns list sorting them using {@code column_index} if
+     * available.
+     */
+    public List<Column> getColumns() {
+        Table table = getSelectedTable();
+        if (table == null) {
+            return null;
+        }
+
+        List<Column> columns = new ArrayList<>(table.getAddedOrRemovedChildren());
+
+        if (hasColumnsSorter) {
+            Collections.sort(columns, new Comparator<Column>() {
+                @Override
+                public int compare(Column column1, Column column2) {
+                    Integer columnIndex1 = column1.getValue(Column.COLUMN_INDEX, Integer.class);
+                    Integer columnIndex2 = column2.getValue(Column.COLUMN_INDEX, Integer.class);
+
+                    if (columnIndex1 == null && columnIndex2 == null) {
+                        // When column index is not specified use the column name for sorting
+                        return column1.getName().compareToIgnoreCase(column2.getName());
+                    }
+
+                    // Columns with the column index must be shown before the ones which don't have it.
+                    if (columnIndex1 != null && columnIndex2 == null) {
+                        return -1;
+                    }
+                    if (columnIndex1 == null && columnIndex2 != null) {
+                        return 1;
+                    }
+
+                    // Compare the column indexes if they are not null.
+                    return columnIndex1.compareTo(columnIndex2);
+                }
+            });
+        }
+
+        return columns;
+    }
 }
diff --git a/TASMAN-webapp/src/main/webapp/resources/js/columns-sorter.js b/TASMAN-webapp/src/main/webapp/resources/js/columns-sorter.js
index 96b4b0b..80d7dff 100644
--- a/TASMAN-webapp/src/main/webapp/resources/js/columns-sorter.js
+++ b/TASMAN-webapp/src/main/webapp/resources/js/columns-sorter.js
@@ -96,7 +96,7 @@
                 jsf.ajax.request('main', null, {
                     'javax.faces.behavior.event': 'action',
                     execute: '@none',
-                    render: 'main:column_wrapper',
+                    render: 'main:tables_wrapper',
                     onevent: function (event) {
                         if (event.status === 'success') {
                             $('#columns-sorter').modal('hide');
diff --git a/TASMAN-webapp/src/main/webapp/tapSchemaEditing.xhtml b/TASMAN-webapp/src/main/webapp/tapSchemaEditing.xhtml
index e251875..b47cb52 100644
--- a/TASMAN-webapp/src/main/webapp/tapSchemaEditing.xhtml
+++ b/TASMAN-webapp/src/main/webapp/tapSchemaEditing.xhtml
@@ -243,7 +243,7 @@
                                         <div class="columns-wrapper">
                                             <div class="columns-selector">
                                                 <ul class="nav nav-pills">
-                                                    <ui:repeat value="#{tapSchemaEditing.selectedTable.addedOrRemovedChildren}" var="column" id="columns-list">
+                                                    <ui:repeat value="#{tapSchemaEditing.columns}" var="column" id="columns-list">
                                                         <li role="presentation" class="#{tapSchemaEditing.selectedColumn.name eq column.name ? 'active': ''}">
                                                             <h:commandLink role="tab" action="#{tapSchemaEditing.setSelectedColumn(column)}" id="column-selector">
                                                                 <h:commandButton class="btn btn-link remove-btn" disabled="#{!tapSchemaEditing.isColumnRemovable(column)}" value="&#215;" onclick="TSM.stopPropagation(event)" id="column-remover">
-- 
GitLab