Commit 9d856c18 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Bugfix consistency checking, simplified columns list AJAX update handling

parent 59b4b3dc
......@@ -30,10 +30,8 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.sql.DataSource;
import org.slf4j.Logger;
......@@ -50,6 +48,32 @@ public class ConsistencyChecks implements Serializable {
private static final long serialVersionUID = 4412404312756740093L;
private final static Logger LOG = LoggerFactory.getLogger(ConsistencyChecks.class);
public static class UnexistingColumn implements Serializable {
private static final long serialVersionUID = -4898369878807200093L;
private final String completeTableName;
private final String columnName;
private UnexistingColumn(String completeTableName, String columnName) {
this.completeTableName = completeTableName;
this.columnName = columnName;
}
public String getCompleteTableName() {
return completeTableName;
}
public String getColumnName() {
return columnName;
}
@Override
public String toString() {
return String.format("%s.%s", completeTableName, columnName);
}
}
public static class UnexistingKey implements Serializable {
private static final long serialVersionUID = 7891439129072900628L;
......@@ -112,14 +136,14 @@ public class ConsistencyChecks implements Serializable {
private final List<InconsistentValue> inconsistencies;
private final Set<String> unexisingSchemas;
private final Set<String> unexisingTables;
private final Map<String, String> unexisingColumns;
private final List<UnexistingColumn> unexistingColumns;
private final List<UnexistingKey> unexistingKeys;
public ConsistencyChecks() {
inconsistencies = new ArrayList<>();
unexisingSchemas = new HashSet<>();
unexisingTables = new HashSet<>();
unexisingColumns = new HashMap<>();
unexistingColumns = new ArrayList<>();
unexistingKeys = new ArrayList<>();
}
......@@ -147,12 +171,12 @@ public class ConsistencyChecks implements Serializable {
unexisingTables.add(schemaName + "." + tableSimpleName);
}
public Map<String, String> getUnexisingColumns() {
return unexisingColumns;
public List<UnexistingColumn> getUnexisingColumns() {
return unexistingColumns;
}
public void addUnexistingColumn(String completeTableName, String columnName) {
unexisingColumns.put(completeTableName, columnName);
unexistingColumns.add(new UnexistingColumn(completeTableName, columnName));
}
public void addUnexistingKey(String keyId, String fromTable, String[] fromColumns, String targetTable, String[] targetColumns) {
......@@ -183,7 +207,39 @@ public class ConsistencyChecks implements Serializable {
}
public boolean isInconsistent() {
return !inconsistencies.isEmpty() || !unexisingSchemas.isEmpty() || !unexisingTables.isEmpty() || !unexisingColumns.isEmpty();
return !inconsistencies.isEmpty() || !unexisingSchemas.isEmpty() || !unexisingTables.isEmpty() || !unexistingColumns.isEmpty();
}
private void keysToRemoveFromUnexistingColumns(Connection conn, String tapSchemaNameEscaped, Set<String> keysToRemoveIds) throws SQLException {
for (UnexistingColumn unexistingColumn : unexistingColumns) {
StringBuilder sb = new StringBuilder();
sb.append("SELECT k.key_id AS key_id\n");
sb.append("FROM ");
sb.append(tapSchemaNameEscaped);
sb.append(".`keys` k\n");
sb.append("JOIN ");
sb.append(tapSchemaNameEscaped);
sb.append(".key_columns c ON k.key_id = c.key_id\n");
sb.append("WHERE (k.from_table = ? AND c.from_column = ?) OR (k.target_table = ? AND c.target_column = ?)");
String query = sb.toString();
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setString(1, unexistingColumn.getCompleteTableName());
ps.setString(2, unexistingColumn.getColumnName());
ps.setString(3, unexistingColumn.getCompleteTableName());
ps.setString(4, unexistingColumn.getColumnName());
LOG.debug("Executing query {}", query);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
keysToRemoveIds.add(rs.getString("key_id"));
}
}
}
}
}
public void amendTapSchema(DBWrapper dbWrapper, TapSchema tapSchema) throws SQLException {
......@@ -205,27 +261,7 @@ public class ConsistencyChecks implements Serializable {
keysToRemoveIds.addAll(getKeysToRemove(conn, tapSchemaNameEscaped, dbType, table));
}
for (Map.Entry<String, String> entry : unexisingColumns.entrySet()) {
query = "select k.key_id AS key_id\n"
+ "FROM `keys` k\n"
+ "JOIN key_columns c ON k.key_id = c.key_id\n"
+ "WHERE (k.from_table = ? AND c.from_column = ?) OR (k.target_table = ? AND c.target_column = ?)";
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setString(1, entry.getKey());
ps.setString(2, entry.getValue());
ps.setString(3, entry.getKey());
ps.setString(4, entry.getValue());
LOG.debug("Executing query {}", query);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
keysToRemoveIds.add(rs.getString("key_id"));
}
}
}
}
keysToRemoveFromUnexistingColumns(conn, tapSchemaNameEscaped, keysToRemoveIds);
for (UnexistingKey unexistingKey : unexistingKeys) {
keysToRemoveIds.add(unexistingKey.getKeyId());
......@@ -256,12 +292,12 @@ public class ConsistencyChecks implements Serializable {
}
// Removing all columns
for (Map.Entry<String, String> entry : unexisingColumns.entrySet()) {
for (UnexistingColumn unexistingColumn : unexistingColumns) {
query = String.format("DELETE FROM %s.%s WHERE table_name = ? AND column_name = ?", tapSchemaNameEscaped, TSMUtil.escapeName("columns", dbType));
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setString(1, entry.getKey());
ps.setString(2, entry.getValue());
LOG.debug("Executing query {} [{}, {}]", query, entry.getKey(), entry.getValue());
ps.setString(1, unexistingColumn.getCompleteTableName());
ps.setString(2, unexistingColumn.getColumnName());
LOG.debug("Executing query {} [{}, {}]", query, unexistingColumn.getCompleteTableName(), unexistingColumn.getColumnName());
ps.executeUpdate();
}
}
......
......@@ -39,6 +39,11 @@
}
}
},
reloadClicked: function (event) {
if (event.status === 'success') {
periodicCheck();
}
},
tapSchemaCreationConfirmed: function (event) {
if (event.status === 'success') {
periodicCheck();
......
......@@ -88,6 +88,8 @@
});
// Setup loading animation
// Restore scroll position after JSF AJAX update
var windowScroll, columnsScroll;
jsf.ajax.addOnEvent(function (data) {
if ($(data.source).is('input[type="text"]') ||
$(data.source).is('[data-jsf-modal]')) {
......@@ -99,6 +101,12 @@
break;
case "complete":
hideWaiting();
windowScroll = window.scrollY;
columnsScroll = $('.columns-selector').scrollTop();
break;
case "success":
window.scrollY = windowScroll;
$('.columns-selector').scrollTop(columnsScroll);
break;
}
});
......
(function ($, TSM) {
var COLUMNS_COMPONENT_ID = 'main:columns-list';
TSM.displayUpdateOperations = TSM.eventHandlerFactory(function (srcElement, jsupdate) {
$('#updateOperationsModal').modal('show');
});
......@@ -24,33 +22,6 @@
return false;
};
TSM.columnChanged = function (event) {
if (event.status === 'success') {
var $li = $(event.source).closest('li');
$li.closest('ul').find('li').removeClass('active');
$li.addClass('active');
}
};
TSM.columnRemoved = TSM.eventHandlerFactory(function (srcElement, jsupdate) {
jsupdate = JSON.parse(jsupdate);
var $ul = $(srcElement).closest('ul');
$(srcElement).closest('a').find('span').addClass('strikeout');
$(srcElement).prop('disabled', true);
if (jsupdate.selectedColumn !== undefined) {
$ul.find('li').removeClass('active');
$ul.find('li:nth-child(' + (jsupdate.selectedColumn + 1) + ')').addClass('active');
}
}, COLUMNS_COMPONENT_ID);
TSM.columnRemovalUndo = TSM.eventHandlerFactory(function (srcElement, jsupdate) {
var $a = $('#main\\:columns-list\\:' + jsupdate + '\\:column-selector');
$a.find('input').prop('disabled', false);
$a.find('.strikeout').removeClass('strikeout');
$a.removeClass('strikeout');
}, COLUMNS_COMPONENT_ID);
TSM.ucdTextKeyDown = function (event) {
if (event.keyCode === 13) {
$('#ucd_search_form\\:search_UCD_btn').click();
......
......@@ -40,7 +40,7 @@
<h:commandLink class="btn btn-info" action="#{tapSchemaEditing.reload()}" onclick="showWaiting()">
<span class="glyphicon glyphicon-refresh"></span>
Reload all
<f:ajax execute="@form" render="@form" onevent="TSM.asyncLoader.startChecking" />
<f:ajax execute="@form" render="@form" onevent="TSM.asyncLoader.reloadClicked" />
</h:commandLink>
</div>
<div class="col-sm-6 vpadding text-right">
......@@ -49,12 +49,6 @@
Back
</h:commandLink>
<h:outputText value="&#160;" />
<ui:remove>
<h:commandLink class="btn btn-danger" action="#{tapSchemaEditing.logout()}">
<span class="glyphicon glyphicon-log-out"></span>
Close session
</h:commandLink>
</ui:remove>
</div>
<div class="clearfix"></div>
<br/>
......@@ -223,7 +217,7 @@
<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.toRemove(column) or column.parent.parent.name eq tapSchemaEditing.tapSchema.name}" value="&#215;" onclick="TSM.stopPropagation(event)" id="column-remover">
<f:ajax execute="@form" render=":main:column_wrapper :main:columns_header" listener="#{tapSchemaEditing.removeColumn(column.name)}" onevent="TSM.columnRemoved" />
<f:ajax execute="@form" render=":main:tables_wrapper" listener="#{tapSchemaEditing.removeColumn(column.name)}" />
</h:commandButton>
<h:panelGroup rendered="#{column.primaryKey}">
......@@ -237,7 +231,7 @@
</h:panelGroup>
<span class="#{tapSchemaEditing.toRemove(column) ? 'strikeout':''}">#{column.name}</span>
<f:ajax execute="@form" render=":main:column_wrapper :main:columns_header" onevent="TSM.columnChanged" />
<f:ajax execute="@form" render=":main:tables_wrapper" />
</h:commandLink>
</li>
</ui:repeat>
......@@ -251,7 +245,7 @@
This column will be removed on TAP Schema Update.
<h:commandLink class="btn btn-primary pull-right" action="#{tapSchemaEditing.undoRemoveColumn()}">
Undo
<f:ajax execute="@form" render=":main:column_wrapper :main:columns_header" onevent="TSM.columnRemovalUndo" />
<f:ajax execute="@form" render=":main:tables_wrapper" />
</h:commandLink>
</div>
</h:panelGroup>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment