Skip to content

Commit

Permalink
Bug fix: saving changed PopupMulti contents
Browse files Browse the repository at this point in the history
  • Loading branch information
leifeld committed May 7, 2023
1 parent 0557464 commit 70de29b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
2 changes: 1 addition & 1 deletion dna/src/main/java/dna/Dna.java
Expand Up @@ -27,7 +27,7 @@ public class Dna {
public static Dna dna;
public static Logger logger;
public static Sql sql;
public static final String date = "2023-05-05";
public static final String date = "2023-05-07";
public static final String version = "3.1.0";
public static final String operatingSystem = System.getProperty("os.name");
public static File workingDirectory = null;
Expand Down
21 changes: 11 additions & 10 deletions dna/src/main/java/gui/PopupMulti.java
Expand Up @@ -57,7 +57,6 @@ public class PopupMulti extends JDialog {
PopupMulti(double X, double Y, TableStatement tableStatement, Point location, Coder coder, ArrayList<Coder> eligibleCoders) {

// TODO:
// debug #Sql.updateTableStatements() function;
// take care of permissions and check nothing is done that isn't permitted
// clean up the code and write javadoc annotations

Expand Down Expand Up @@ -368,10 +367,12 @@ boolean saveContents() {
if (changed) {
this.tableStatement = statementCopy;
}
saveButton.setEnabled(!changed);
// saveButton.setEnabled(!changed);
toggleButtons();
return changed;
} else {
saveButton.setEnabled(false);
// saveButton.setEnabled(false);
toggleButtons();
return true;
}
}
Expand Down Expand Up @@ -499,23 +500,23 @@ public void removeUpdate(DocumentEvent arg0) {
private void formatEntry() {
Color fg = javax.swing.UIManager.getColor("TextField.foreground"); // default unselected foreground color of JTextField
String currentText = ((JTextField) box.getEditor().getEditorComponent()).getText();
boolean foundEntity = false;
for (int i = 0; i < box.getModel().getSize(); i++) {
if (box.getModel().getElementAt(i).getValue().equals(currentText)) {
box.setSelectedIndex(i);
fg = box.getModel().getElementAt(i).getColor();
foundEntity = true;
}
}
((JTextField) box.getEditor().getEditorComponent()).setSelectedTextColor(fg);
box.getEditor().getEditorComponent().setForeground(fg);

Object object = box.getSelectedItem();
if (object.getClass().getName().endsWith("String")) { // if not an existing entity, the editor returns a String
String s1 = (String) object;
if (s1.length() > 0 && s1.matches("^\\s+$")) { // replace a (multiple) whitespace string by an empty string
s1 = "";
if (!foundEntity) { // if not an existing entity, the editor returns a String
if (currentText.length() > 0 && currentText.matches("^\\s+$")) { // replace a (multiple) whitespace string by an empty string
currentText = "";
}
s1 = s1.substring(0, Math.min(190, s1.length()));
Entity entity = new Entity(s1); // the new entity has an ID of -1; the SQL class needs to take care of this when writing into the database
currentText = currentText.substring(0, Math.min(190, 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();
int defaultRoleVariableId = PopupMulti.this.roleVariableLinks.stream().filter(r -> r.getVariableId() == defaultVariableId && r.getRoleId() == roleValue.getRoleId()).findFirst().get().getId();
Expand Down
4 changes: 3 additions & 1 deletion dna/src/main/java/model/Entity.java
Expand Up @@ -107,7 +107,9 @@ public Entity(Entity entity) {
this.color = entity.getColor();
this.inDatabase = entity.isInDatabase();
this.attributeValues = new HashMap<String, String>();
this.attributeValues.putAll(entity.getAttributeValues());
if (entity.getAttributeValues() != null) {
this.attributeValues.putAll(entity.getAttributeValues());
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions dna/src/main/java/model/RoleValue.java
Expand Up @@ -16,6 +16,16 @@ public RoleValue(int variableId, String variableName, String dataType, Object va
this.statementTypeId = statementTypeId;
}

public String toString() {
if (this.getDataType().equals("short text")) {
return ((Entity) this.getValue()).getValue();
} else if (this.getDataType().equals("long text")) {
return ((String) this.getValue());
} else {
return String.valueOf((int) this.getValue());
}
}

/**
* Copy constructor. Creates a deep copy of an existing role value object.
*
Expand Down
9 changes: 9 additions & 0 deletions dna/src/main/java/model/TableStatement.java
Expand Up @@ -5,6 +5,7 @@
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Objects;
import java.util.stream.Collectors;

public class TableStatement extends Statement {
private String text, coderName, statementTypeLabel;
Expand Down Expand Up @@ -114,6 +115,14 @@ public void setRoleValues(ArrayList<RoleValue> roleValues) {
this.roleValues = roleValues;
}

public String toString() {
String values = this.getRoleValues()
.stream()
.map(v -> v.getRoleName() + ": " + v.getValue().toString())
.collect(Collectors.joining("; "));
return "[ID: " + this.getId() + "; Coder: " + this.getCoderId() + "; Doc: " + this.getDocumentId() + "; " + values + "]";
}

@Override
public boolean equals(Object o) {
// self check
Expand Down
35 changes: 22 additions & 13 deletions dna/src/main/java/sql/Sql.java
Expand Up @@ -13,13 +13,10 @@
import java.util.*;
import java.util.regex.Matcher;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.sql.DataSource;

import export.DataFrame;
import gui.DocumentEditor;
import me.tongfei.progressbar.ProgressBar;
import model.*;
import org.jasypt.util.password.StrongPasswordEncryptor;
import org.sqlite.SQLiteDataSource;
Expand Down Expand Up @@ -415,9 +412,9 @@ public boolean createTables(String encryptedAdminPassword) {
+ "ID INTEGER PRIMARY KEY NOT NULL, "
+ "VariableId INTEGER NOT NULL, "
+ "Value TEXT NOT NULL DEFAULT '' CHECK (LENGTH(Value) < 191), "
+ "Red INTEGER CHECK (Red BETWEEN 0 AND 255), "
+ "Green INTEGER CHECK (Green BETWEEN 0 AND 255), "
+ "Blue INTEGER CHECK (Blue BETWEEN 0 AND 255), "
+ "Red INTEGER DEFAULT 0 CHECK (Red BETWEEN 0 AND 255), "
+ "Green INTEGER DEFAULT 0 CHECK (Green BETWEEN 0 AND 255), "
+ "Blue INTEGER DEFAULT 0 CHECK (Blue BETWEEN 0 AND 255), "
+ "ChildOf INTEGER CHECK(ChildOf > 0), "
+ "UNIQUE (VariableId, Value), "
+ "FOREIGN KEY(ChildOf) REFERENCES ENTITIES(ID) ON DELETE CASCADE, "
Expand Down Expand Up @@ -2597,6 +2594,14 @@ public boolean updateTableStatements(ArrayList<TableStatement> tableStatements)
class DataResult {
int id, roleVariableLinkId, valueInt;
String valueString, dataType;

public String toString() {
if (this.dataType.equals("short text") || this.dataType.equals("long text")) {
return this.valueString;
} else {
return "" + this.valueInt;
}
}
}

ResultSet r1;
Expand Down Expand Up @@ -2671,7 +2676,8 @@ class DataResult {
insertionPile = s.getRoleValues()
.stream()
.filter(v -> finalData
.stream().noneMatch(d -> d.dataType.equals(v.getDataType()) &&
.stream()
.noneMatch(d -> d.dataType.equals(v.getDataType()) &&
d.roleVariableLinkId == v.getRoleVariableLinkId() &&
((d.dataType.equals("long text") && d.valueString.equals((String) v.getValue())) ||
(d.dataType.equals("short text") && d.valueInt == ((Entity) v.getValue()).getId()) ||
Expand Down Expand Up @@ -2703,11 +2709,12 @@ class DataResult {
s10.setInt(1, insertionPile.get(j).getVariableId());
s10.setString(2, ((Entity) insertionPile.get(j).getValue()).getValue());
s10.executeUpdate();
}
// get generated primary key and save in entity in insertion pile as entity ID
r1 = s10.getGeneratedKeys();
while (r1.next()) {
((Entity) insertionPile.get(j).getValue()).setId(r1.getInt(1));

// get generated primary key and save in entity in insertion pile as entity ID
r1 = s10.getGeneratedKeys();
while (r1.next()) {
((Entity) insertionPile.get(j).getValue()).setId(r1.getInt(1));
}
}
}
}
Expand All @@ -2730,7 +2737,9 @@ class DataResult {
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.executeUpdate(); // TODO: There is a bug somewhere in here because the foreign key constraint is not always met!
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())) {
Expand Down

0 comments on commit 70de29b

Please sign in to comment.