Repository: Wikidata/Wikidata-Toolkit (java)

Issue:
Generic method to create new EntityDocument in WikibaseDataEditor
We currently have generic methods to perform an EntityUpdate on the target Wikibase, but to create new entities we only have methods which depend on the entity type. We could add a generic version of it:
`WikibaseDataEditor::createEntityDocument(EntityDocument entityDocument, String summary, List<String> tags)`.

This would fail for entity types which cannot be created in this way (such as `MediaInfoDocument`).

Pull request:
Method to create an entity document uniformly, regardless of its type
Closes #651.

Patch:
diff --git a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WikibaseDataEditor.java b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WikibaseDataEditor.java
index e10d14ed..60f5c3a2 100644
--- a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WikibaseDataEditor.java
+++ b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WikibaseDataEditor.java
@@ -300,6 +300,48 @@ public class WikibaseDataEditor {
 		return this.wbEditingAction.wbEditEntity(
 				null, null, null, type, data, false, editAsBot, 0, summary, tags);
 	}
+	
+	/**
+	 * Creates new entity document. Provided entity document must use a local item ID,
+	 * such as {@link ItemIdValue#NULL}, and its revision ID must be 0.
+	 * <p>
+	 * The newly created document is returned. It will contain the new item ID and
+	 * revision ID. Note that the site IRI used in the item ID is not part of the
+	 * API response. The site IRI given when constructing this object is used in its
+	 * place.
+	 * <p>
+	 * Statements in the provided document must not have IDs.
+	 * <p>
+	 * Summary message will be prepended by an automatically generated comment. The
+	 * length limit of the autocomment together with the summary is 260 characters.
+	 * Everything above that limit will be cut off.
+	 *
+	 * @param document
+	 *            document that contains the data to be written
+	 * @param summary
+	 *            summary for the edit
+	 * @param tags
+	 *            string identifiers of the tags to apply to the edit, {@code null}
+	 *            or empty for no tags
+	 * @return newly created item document or {@code null} for simulated edit (see
+	 *         {@link #disableEditing()}
+	 * @throws IOException
+	 *             if there was an IO problem, such as missing network connection
+	 * @throws MediaWikiApiErrorException
+	 *             if MediaWiki API returned an error response
+	 */
+	public EntityDocument createEntityDocument(
+			EntityDocument document, String summary, List<String> tags) throws IOException, MediaWikiApiErrorException {
+		if (document instanceof ItemDocument) {
+			return createItemDocument((ItemDocument) document, summary, tags);
+		} else if (document instanceof PropertyDocument) {
+			return createPropertyDocument((PropertyDocument) document, summary, tags);
+		} else if (document instanceof LexemeDocument) {
+			return createLexemeDocument((LexemeDocument) document, summary, tags);
+		} else {
+			throw new UnsupportedOperationException("Creation of entities of this type is not supported");
+		}
+	}
 
 	/**
 	 * Creates new item document. Provided item document must use a local item ID,


Test patch:
diff --git a/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WikibaseDataEditorTest.java b/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WikibaseDataEditorTest.java
index fb13724f..01183161 100644
--- a/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WikibaseDataEditorTest.java
+++ b/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WikibaseDataEditorTest.java
@@ -57,6 +57,7 @@ import org.wikidata.wdtk.datamodel.helpers.StatementBuilder;
 import org.wikidata.wdtk.datamodel.helpers.StatementUpdateBuilder;
 import org.wikidata.wdtk.datamodel.helpers.TermUpdateBuilder;
 import org.wikidata.wdtk.datamodel.interfaces.DatatypeIdValue;
+import org.wikidata.wdtk.datamodel.interfaces.EntityDocument;
 import org.wikidata.wdtk.datamodel.interfaces.EntityUpdate;
 import org.wikidata.wdtk.datamodel.interfaces.FormUpdate;
 import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
@@ -217,8 +218,8 @@ public class WikibaseDataEditorTest {
 		params.put("data", data);
 		con.setWebResource(params, expectedResult);
 
-		ItemDocument result = wde
-				.createItemDocument(itemDocument, "My summary", Collections.singletonList("my-tag"));
+		EntityDocument result = wde
+				.createEntityDocument(itemDocument, "My summary", Collections.singletonList("my-tag"));
 
 		assertEquals(expectedResultDocument, result);
 		assertEquals(-1, wde.getRemainingEdits());
@@ -395,7 +396,7 @@ public class WikibaseDataEditorTest {
 		params.put("data", data);
 		con.setWebResource(params, expectedResult);
 
-		PropertyDocument result = wde.createPropertyDocument(propertyDocument,
+		EntityDocument result = wde.createEntityDocument(propertyDocument,
 				null, null);
 
 		assertTrue(wde.editAsBot());
@@ -436,6 +437,17 @@ public class WikibaseDataEditorTest {
 
 		assertEquals(expectedResultDocument, result);
 	}
+	
+	@Test
+	public void testCreateMediaInfo() {
+		WikibaseDataEditor wde = new WikibaseDataEditor(this.con,
+				Datamodel.SITE_WIKIMEDIA_COMMONS);
+
+		MediaInfoDocument mediaInfoDocument = Datamodel.makeMediaInfoDocument(MediaInfoIdValue.NULL)
+				.withLabel(Datamodel.makeMonolingualTextValue("test", "en"));
+		
+		assertThrows(UnsupportedOperationException.class, () -> wde.createEntityDocument(mediaInfoDocument, "summary", Collections.emptyList()));
+	}
 
 	@Test
 	@Deprecated