Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix newly added entry not synced to db #7178

Merged
merged 7 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where the "Document Viewer" did not show the first page of the opened pdf document and did not show the correct total number of pages [#7108](https://github.com/JabRef/jabref/issues/7108)
- We fixed an issue where the context menu was not updated after a file link was changed. [#5777](https://github.com/JabRef/jabref/issues/5777)
- We fixed an issue where the password for a shared SQL database was not remembered [#6869](https://github.com/JabRef/jabref/issues/6869)
- We fixed an issue where newly added entires were not synced to a shared SQL database [#7176](https://github.com/JabRef/jabref/issues/7176)

### Removed

Expand Down
60 changes: 31 additions & 29 deletions src/main/java/org/jabref/logic/shared/DBMSProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ protected void insertIntoEntryTable(List<BibEntry> bibEntries) {
.append(escape("TYPE"))
.append(") VALUES(?)");
// Number of commas is bibEntries.size() - 1
for (int i = 0; i < bibEntries.size() - 1; i++) {
for (int i = 0; i < (bibEntries.size() - 1); i++) {
insertIntoEntryQuery.append(", (?)");
}

Expand Down Expand Up @@ -239,6 +239,7 @@ protected void insertIntoFieldTable(List<BibEntry> bibEntries) {
// Coerce to ArrayList in order to use List.get()
List<List<Field>> fields = bibEntries.stream().map(bibEntry -> new ArrayList<>(bibEntry.getFields()))
.collect(Collectors.toList());

StringBuilder insertFieldQuery = new StringBuilder()
.append("INSERT INTO ")
.append(escape("FIELD"))
Expand All @@ -253,8 +254,13 @@ protected void insertIntoFieldTable(List<BibEntry> bibEntries) {
for (List<Field> entryFields : fields) {
numFields += entryFields.size();
}

if (numFields == 0) {
return; // Prevent SQL Exception
}

// Number of commas is fields.size() - 1
for (int i = 0; i < numFields - 1; i++) {
for (int i = 0; i < (numFields - 1); i++) {
insertFieldQuery.append(", (?, ?, ?)");
}
try (PreparedStatement preparedFieldStatement = connection.prepareStatement(insertFieldQuery.toString())) {
Expand Down Expand Up @@ -493,7 +499,7 @@ public List<BibEntry> getSharedEntries(List<Integer> sharedIDs) {
.append("F.").append(escape("VALUE"))
.append(" FROM ")
.append(escape("ENTRY"))
.append(" inner join ")
.append(" left outer join ")
koppor marked this conversation as resolved.
Show resolved Hide resolved
.append(escape("FIELD"))
.append(" F on ")
.append(escape("ENTRY")).append(".").append(escape("SHARED_ID"))
Expand All @@ -508,41 +514,37 @@ public List<BibEntry> getSharedEntries(List<Integer> sharedIDs) {
query.append(" order by ")
.append(escape("SHARED_ID"));

PreparedStatement preparedStatement;
try {
preparedStatement = connection.prepareStatement(query.toString());
try (PreparedStatement preparedStatement = connection.prepareStatement(query.toString())) {
for (int i = 0; i < sharedIDs.size(); i++) {
preparedStatement.setInt(i + 1, sharedIDs.get(i));
}
} catch (SQLException e) {
LOGGER.debug("Executed >{}<", query.toString());
LOGGER.error("SQL Error", e);
return Collections.emptyList();
}
try (ResultSet selectEntryResultSet = preparedStatement.executeQuery()) {
BibEntry bibEntry = null;
int lastId = -1;
while (selectEntryResultSet.next()) {
// We get a list of field values of bib entries "grouped" by bib entries
// Thus, the first change in the shared id leads to a new BibEntry
if (selectEntryResultSet.getInt("SHARED_ID") > lastId) {
bibEntry = new BibEntry();
bibEntry.getSharedBibEntryData().setSharedID(selectEntryResultSet.getInt("SHARED_ID"));
bibEntry.setType(EntryTypeFactory.parse(selectEntryResultSet.getString("TYPE")));
bibEntry.getSharedBibEntryData().setVersion(selectEntryResultSet.getInt("VERSION"));
sharedEntries.add(bibEntry);
lastId = selectEntryResultSet.getInt("SHARED_ID");
}

// In all cases, we set the field value of the newly created BibEntry object
String value = selectEntryResultSet.getString("VALUE");
if (value != null) {
bibEntry.setField(FieldFactory.parseField(selectEntryResultSet.getString("NAME")), value, EntriesEventSource.SHARED);
try (ResultSet selectEntryResultSet = preparedStatement.executeQuery()) {
BibEntry bibEntry = null;
int lastId = -1;
while (selectEntryResultSet.next()) {
// We get a list of field values of bib entries "grouped" by bib entries
// Thus, the first change in the shared id leads to a new BibEntry
if (selectEntryResultSet.getInt("SHARED_ID") > lastId) {
bibEntry = new BibEntry();
bibEntry.getSharedBibEntryData().setSharedID(selectEntryResultSet.getInt("SHARED_ID"));
bibEntry.setType(EntryTypeFactory.parse(selectEntryResultSet.getString("TYPE")));
bibEntry.getSharedBibEntryData().setVersion(selectEntryResultSet.getInt("VERSION"));
sharedEntries.add(bibEntry);
lastId = selectEntryResultSet.getInt("SHARED_ID");
}

// In all cases, we set the field value of the newly created BibEntry object
String value = selectEntryResultSet.getString("VALUE");
if (value != null) {
bibEntry.setField(FieldFactory.parseField(selectEntryResultSet.getString("NAME")), value, EntriesEventSource.SHARED);
}
}
}
} catch (SQLException e) {
LOGGER.error("Executed >{}<", query.toString());
LOGGER.error("SQL Error", e);
return Collections.emptyList();
}

return sharedEntries;
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/org/jabref/logic/shared/DBMSProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,37 @@ void testInsertEntry() throws SQLException {
assertEquals(expectedFieldMap, actualFieldMap);
}

@Test
void testInsertEntryWithEmptyFields() throws SQLException {
BibEntry expectedEntry = new BibEntry(StandardEntryType.Article);

dbmsProcessor.insertEntry(expectedEntry);

BibEntry emptyEntry = getBibEntryExample();
emptyEntry.getSharedBibEntryData().setSharedID(1);
dbmsProcessor.insertEntry(emptyEntry); // does not insert, due to same sharedID.
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved

Map<String, String> actualFieldMap = new HashMap<>();

try (ResultSet entryResultSet = selectFrom("ENTRY", dbmsConnection, dbmsProcessor)) {
assertTrue(entryResultSet.next());
assertEquals(1, entryResultSet.getInt("SHARED_ID"));
assertEquals("article", entryResultSet.getString("TYPE"));
assertEquals(1, entryResultSet.getInt("VERSION"));
assertFalse(entryResultSet.next());

try (ResultSet fieldResultSet = selectFrom("FIELD", dbmsConnection, dbmsProcessor)) {
while (fieldResultSet.next()) {
actualFieldMap.put(fieldResultSet.getString("NAME"), fieldResultSet.getString("VALUE"));
}
}
}

Map<String, String> expectedFieldMap = expectedEntry.getFieldMap().entrySet().stream().collect(Collectors.toMap((entry) -> entry.getKey().getName(), Map.Entry::getValue));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this always an empty list (as expected entry is empty)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, seems like I need to adapt the queries a bit. Didn't think about this,
I should check for the shared entry id to be present.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I've no idea what the test is doing. You add an empty entry, then some other completely unrelated entry, and then compare against the empty entry again.


assertEquals(expectedFieldMap, actualFieldMap);
}

private static BibEntry getBibEntryExample() {
return new BibEntry(StandardEntryType.InProceedings)
.withField(StandardField.AUTHOR, "Wirthlin, Michael J and Hutchings, Brad L and Gilson, Kent L")
Expand All @@ -119,6 +150,19 @@ void testUpdateEntry() throws Exception {
assertEquals(Optional.of(expectedEntry), actualEntry);
}

@Test
void testUpdateEmptyEntry() throws Exception {
BibEntry expectedEntry = new BibEntry(StandardEntryType.Article);
dbmsProcessor.insertEntry(expectedEntry);

expectedEntry.setField(StandardField.AUTHOR, "Michael J and Hutchings");
expectedEntry.setField(new UnknownField("customField"), "custom value");
dbmsProcessor.updateEntry(expectedEntry);

Optional<BibEntry> actualEntry = dbmsProcessor.getSharedEntry(expectedEntry.getSharedBibEntryData().getSharedID());
assertEquals(Optional.of(expectedEntry), actualEntry);
}

@Test
void testGetEntriesByIdList() throws Exception {
BibEntry firstEntry = getBibEntryExample();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jabref.logic.shared;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -69,7 +68,7 @@ public void setup() throws Exception {
}

@AfterEach
public void clear() throws SQLException {
public void clear() {
dbmsSynchronizer.closeSharedDatabase();
}

Expand Down