Skip to content

Commit

Permalink
Fixed statement updating bug
Browse files Browse the repository at this point in the history
  • Loading branch information
leifeld committed May 8, 2023
1 parent fa58c74 commit 64b8a1b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
4 changes: 3 additions & 1 deletion dna/src/main/java/gui/PopupMulti.java
Expand Up @@ -610,7 +610,7 @@ private void formatEntry() {
if (currentText.length() > 0 && currentText.matches("^\\s+$")) { // replace a (multiple) whitespace string by an empty string
currentText = "";
}
currentText = currentText.substring(0, Math.min(190, currentText.length()));
currentText = currentText.substring(0, Math.min(189, currentText.length()));
Entity entity = new Entity(currentText); // the new entity has an ID of -1; the SQL class needs to take care of this when writing into the database
int defaultVariableId = PopupMulti.this.roleMap.get(roleValue.getRoleId()).getDefaultVariableId();
String defaultVariableName = PopupMulti.this.variables.stream().filter(v -> v.getVariableId() == defaultVariableId).findFirst().get().getVariableName();
Expand All @@ -619,6 +619,7 @@ private void formatEntry() {
RoleValuePanel.this.ts.getRoleValues().get(finalI1).setVariableName(defaultVariableName);
RoleValuePanel.this.ts.getRoleValues().get(finalI1).setRoleVariableLinkId(defaultRoleVariableId);
RoleValuePanel.this.ts.getRoleValues().get(finalI1).setValue(entity);
box.setSelectedItem(entity);
} else {
Entity entity = (Entity) box.getSelectedItem();
RoleValuePanel.this.ts.getRoleValues().get(finalI1).setVariableId(entity.getVariableId());
Expand All @@ -629,6 +630,7 @@ private void formatEntry() {
toggleButtons();
}
});

if (coder.isPopupAutoComplete()) {
AutoCompleteDecorator.decorate(box); // auto-complete short text values; part of SwingX
}
Expand Down
11 changes: 10 additions & 1 deletion dna/src/main/java/model/TableStatement.java
Expand Up @@ -4,6 +4,7 @@
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Objects;
import java.util.stream.Collectors;

Expand All @@ -21,7 +22,15 @@ public TableStatement(int id, int start, int stop, int statementTypeId, int code
this.coderColor = coderColor;
this.statementTypeColor = statementTypeColor;
this.statementTypeLabel = statementTypeLabel;
this.roleValues = roleValues;

// make sure the role values are sorted by role name, then role ID, then value, to make sure different sorting orders from SQL do not matter for comparison
this.roleValues = roleValues
.stream()
.sorted(Comparator.comparing((RoleValue rv) -> rv.getRoleName())
.thenComparing((RoleValue rv) -> rv.getRoleId())
.thenComparing((RoleValue rv) -> rv.getValue().toString()))
.collect(Collectors.toCollection(ArrayList::new));

}

public TableStatement(TableStatement tableStatement) {
Expand Down
42 changes: 29 additions & 13 deletions dna/src/main/java/sql/Sql.java
Expand Up @@ -2623,10 +2623,10 @@ public boolean updateTableStatements(ArrayList<TableStatement> tableStatements)
PreparedStatement s8 = conn.prepareStatement("INSERT INTO DATALONGTEXT (StatementId, RoleVariableLinkId, Value) VALUES (?, ?, ?);");
PreparedStatement s9 = conn.prepareStatement("INSERT INTO DATASHORTTEXT (StatementId, RoleVariableLinkId, Entity) VALUES (?, ?, ?);");
PreparedStatement s10 = conn.prepareStatement("INSERT INTO ENTITIES (VariableId, Value) VALUES (?, ?);", PreparedStatement.RETURN_GENERATED_KEYS);
PreparedStatement s11 = conn.prepareStatement("UPDATE DATABOOLEAN SET Value = ? WHERE ID = ?;");
PreparedStatement s12 = conn.prepareStatement("UPDATE DATAINTEGER SET Value = ? WHERE ID = ?;");
PreparedStatement s13 = conn.prepareStatement("UPDATE DATALONGTEXT SET Value = ? WHERE ID = ?;");
PreparedStatement s14 = conn.prepareStatement("UPDATE DATASHORTTEXT SET Entity = ? WHERE ID = ?;");
PreparedStatement s11 = conn.prepareStatement("UPDATE DATABOOLEAN SET RoleVariableLinkId = ?, Value = ? WHERE ID = ?;");
PreparedStatement s12 = conn.prepareStatement("UPDATE DATAINTEGER SET RoleVariableLinkId = ?, Value = ? WHERE ID = ?;");
PreparedStatement s13 = conn.prepareStatement("UPDATE DATALONGTEXT SET RoleVariableLinkId = ?, Value = ? WHERE ID = ?;");
PreparedStatement s14 = conn.prepareStatement("UPDATE DATASHORTTEXT SET RoleVariableLinkId = ?, Entity = ? WHERE ID = ?;");
PreparedStatement s15 = conn.prepareStatement("DELETE FROM DATABOOLEAN WHERE ID = ?;");
PreparedStatement s16 = conn.prepareStatement("DELETE FROM DATAINTEGER WHERE ID = ?;");
PreparedStatement s17 = conn.prepareStatement("DELETE FROM DATALONGTEXT WHERE ID = ?;");
Expand Down Expand Up @@ -2748,11 +2748,22 @@ public String toString() {
for (int j = 0; j < insertionPile.size(); j++) {
if (insertionPile.get(j).getDataType().equals("short text")) {
boolean match = false;
// does the entity exist already with an identical ID? then nothing needs to be changed
for (int k = 0; k < entities.size(); k++) {
if (entities.get(k).getValue().equals(((Entity) insertionPile.get(j).getValue()).getValue()) && entities.get(k).getVariableId() == insertionPile.get(j).getVariableId()) {
if (entities.get(k).getValue().equals(((Entity) insertionPile.get(j).getValue()).getValue()) && entities.get(k).getId() == ((Entity) insertionPile.get(j).getValue()).getId() && entities.get(k).getVariableId() == insertionPile.get(j).getVariableId()) {
match = true;
break;
}
}
// does the entity with the same value exist but under a different ID? then change the ID in the insertion pile
for (int k = 0; k < entities.size(); k++) {
if (!match && entities.get(k).getValue().equals(((Entity) insertionPile.get(j).getValue()).getValue()) && entities.get(k).getId() != ((Entity) insertionPile.get(j).getValue()).getId() && entities.get(k).getVariableId() == insertionPile.get(j).getVariableId()) {
((Entity) insertionPile.get(j).getValue()).setId(entities.get(k).getId());
match = true;
}
}

// if it can't be found, create a new entity in the database and set its ID in the insertion pile
if (!match) {
s10.setInt(1, insertionPile.get(j).getVariableId());
s10.setString(2, ((Entity) insertionPile.get(j).getValue()).getValue());
Expand All @@ -2762,6 +2773,7 @@ public String toString() {
r1 = s10.getGeneratedKeys();
while (r1.next()) {
((Entity) insertionPile.get(j).getValue()).setId(r1.getInt(1));
entities.add((Entity) insertionPile.get(j).getValue()); // add the new entity to the entities list in case another role value has the same value (which, theoretically, shouldn't happen, though)
}
}
}
Expand All @@ -2774,30 +2786,34 @@ public String toString() {
if (deletionPile.get(j).dataType.equals("long text") &&
insertionPile.get(k).getDataType().equals("long text") &&
!deletionPile.get(j).valueString.equals(insertionPile.get(k).getValue())) {
s13.setString(1, (String) insertionPile.get(k).getValue());
s13.setInt(2, deletionPile.get(j).id);
s13.setInt(1, insertionPile.get(k).getRoleVariableLinkId());
s13.setString(2, (String) insertionPile.get(k).getValue());
s13.setInt(3, deletionPile.get(j).id);
s13.executeUpdate();
deletionPile.remove(j);
insertionPile.remove(k);
break;
} else if (deletionPile.get(j).dataType.equals("short text") &&
insertionPile.get(k).getDataType().equals("short text") &&
deletionPile.get(j).valueInt != ((Entity) insertionPile.get(k).getValue()).getId()) {
s14.setInt(1, ((Entity) insertionPile.get(k).getValue()).getId());
s14.setInt(2, deletionPile.get(j).id);
s14.setInt(1, insertionPile.get(k).getRoleVariableLinkId());
s14.setInt(2, ((Entity) insertionPile.get(k).getValue()).getId());
s14.setInt(3, deletionPile.get(j).id);
s14.executeUpdate();
deletionPile.remove(j);
insertionPile.remove(k);
break;
} else if (deletionPile.get(j).valueInt != (int) insertionPile.get(k).getValue() &&
deletionPile.get(j).dataType.equals(insertionPile.get(k).getDataType())) {
if (deletionPile.get(j).dataType.equals("boolean")) {
s11.setInt(1, (int) insertionPile.get(k).getValue());
s11.setInt(2, deletionPile.get(j).id);
s11.setInt(1, insertionPile.get(k).getRoleVariableLinkId());
s11.setInt(2, (int) insertionPile.get(k).getValue());
s11.setInt(3, deletionPile.get(j).id);
s11.executeUpdate();
} else if (deletionPile.get(j).dataType.equals("integer")) {
s12.setInt(1, (int) insertionPile.get(k).getValue());
s12.setInt(2, deletionPile.get(j).id);
s12.setInt(1, insertionPile.get(k).getRoleVariableLinkId());
s12.setInt(2, (int) insertionPile.get(k).getValue());
s12.setInt(3, deletionPile.get(j).id);
s12.executeUpdate();
}
deletionPile.remove(j);
Expand Down

0 comments on commit 64b8a1b

Please sign in to comment.