Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate out error reporting logic for testability
- Loading branch information
Showing
8 changed files
with
283 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
extensions/wikibase/src/org/openrefine/wikibase/editing/EditResultsFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
package org.openrefine.wikibase.editing; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import com.google.refine.expr.EvalError; | ||
import com.google.refine.model.Cell; | ||
import com.google.refine.model.changes.CellAtRow; | ||
|
||
import org.openrefine.wikibase.editing.EditBatchProcessor.EditResult; | ||
|
||
/** | ||
* Formats the results of Wikibase edits into cells to be included in the project's grid. | ||
*/ | ||
public class EditResultsFormatter { | ||
|
||
private final String baseUrl; | ||
private final Map<Integer, String> rowIdToError = new HashMap<>(); | ||
private final Map<Integer, String> rowIdToEditLink = new HashMap<>(); | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param mediaWikiApiEndpoint | ||
* full API endpoint, ending with .../w/api.php | ||
*/ | ||
public EditResultsFormatter(String mediaWikiApiEndpoint) { | ||
this.baseUrl = mediaWikiApiEndpoint.substring(0, mediaWikiApiEndpoint.length() - "w/api.php".length()); | ||
} | ||
|
||
/** | ||
* Log another edit result for further formatting. | ||
*/ | ||
public void add(EditResult result) { | ||
if (!result.getCorrespondingRowIds().isEmpty()) { | ||
int firstRowId = result.getCorrespondingRowIds().stream().min(Comparator.naturalOrder()).get(); | ||
if (result.getErrorMessage() != null && result.getErrorCode() != null) { | ||
String error = String.format("[%s] %s", result.getErrorCode(), result.getErrorMessage()); | ||
String existingError = rowIdToError.get(firstRowId); | ||
if (existingError == null) { | ||
rowIdToError.put(firstRowId, error); | ||
} else { | ||
rowIdToError.put(firstRowId, existingError + "; " + error); | ||
} | ||
} else if (result.getLastRevisionId().isPresent()) { | ||
String revisionLink = baseUrl + "w/index.php?diff=prev&oldid=" + result.getLastRevisionId().getAsLong(); | ||
String existingLinks = rowIdToEditLink.get(firstRowId); | ||
if (existingLinks == null) { | ||
rowIdToEditLink.put(firstRowId, revisionLink); | ||
} else { | ||
rowIdToEditLink.put(firstRowId, existingLinks + " " + revisionLink); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Generate a list of cells on specific rows representing the editing results | ||
*/ | ||
public List<CellAtRow> toCells() { | ||
List<CellAtRow> cells = new ArrayList<>(); | ||
for (Entry<Integer, String> errorCell : rowIdToError.entrySet()) { | ||
cells.add(new CellAtRow(errorCell.getKey(), new Cell(new EvalError(errorCell.getValue()), null))); | ||
} | ||
for (Entry<Integer, String> editLinkCell : rowIdToEditLink.entrySet()) { | ||
int rowId = editLinkCell.getKey(); | ||
if (rowIdToError.get(rowId) == null) { | ||
cells.add(new CellAtRow(rowId, new Cell(editLinkCell.getValue(), null))); | ||
} | ||
} | ||
return cells; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.