Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 20 additions & 19 deletions src/main/java/com/coveo/pushapiclient/Document.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
package com.coveo.pushapiclient;

import java.util.Map;
import java.util.HashMap;

public class Document {
/**
* The metadata key-value pairs for a given document.
* <p>
* Each metadata in the document must be unique.
* <p>
* Metadata are case-insensitive (e.g., the Push API considers mykey, MyKey, myKey, MYKEY, etc. as identical).
* <p>
* See https://docs.coveo.com/en/115 for more information.
*/
public final HashMap<String, Object> metadata;
/**
* The list of permission sets for this item.
* <p>
* This is useful when item based security is required (i.e., when security isn't configured at the source level).
* <p>
* See https://docs.coveo.com/en/107 for more information.
*/
public final DocumentPermissions[] permissions;
/**
* The Uniform Resource Identifier (URI) that uniquely identifies the document in a Coveo index.
* <p>
Expand Down Expand Up @@ -73,24 +91,6 @@ public class Document {
* See https://docs.coveo.com/en/73 for more information.
*/
public CompressedBinaryData compressedBinaryData;
/**
* The metadata key-value pairs for a given document.
* <p>
* Each metadata in the document must be unique.
* <p>
* Metadata are case-insensitive (e.g., the Push API considers mykey, MyKey, myKey, MYKEY, etc. as identical).
* <p>
* See https://docs.coveo.com/en/115 for more information.
*/
public Map<String, Object> metadata;
/**
* The list of permission sets for this item.
* <p>
* This is useful when item based security is required (i.e., when security isn't configured at the source level).
* <p>
* See https://docs.coveo.com/en/107 for more information.
*/
public DocumentPermissions[] permissions;
/**
* The file extension of the data you're pushing.
* <p>
Expand All @@ -104,6 +104,7 @@ public class Document {

public Document() {
this.permissions = new DocumentPermissions[]{new DocumentPermissions()};
this.metadata = new HashMap<String, Object>();
}
}

28 changes: 25 additions & 3 deletions src/main/java/com/coveo/pushapiclient/DocumentBuilder.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
package com.coveo.pushapiclient;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

public class DocumentBuilder {
private Document document;

private static final ArrayList<String> reservedKeynames = new ArrayList<>() {{
add("compressedBinaryData");
add("compressedBinaryDataFileId");
add("parentId");
add("fileExtension");
add("data");
add("permissions");
add("documentId");
add("orderingId");
}};

private final Document document;

public DocumentBuilder(String uri, String title) {
this.document = new Document();
Expand Down Expand Up @@ -133,7 +147,13 @@ public DocumentBuilder withDeniedPermissions() {
}

public String marshal() {
return new Gson().toJson(this.document);
JsonObject jsonDocument = new Gson().toJsonTree(this.document).getAsJsonObject();
this.document.metadata.forEach((key, value) -> {
jsonDocument.add(key, new Gson().toJsonTree(value));
});
jsonDocument.remove("metadata");
return jsonDocument.toString();

}

private String dateFormat(DateTime dt) {
Expand All @@ -154,6 +174,8 @@ private void validateFileExtension(String fileExtension) {
}

private void validateReservedMetadataKeyNames(String key) {
// TODO
if (reservedKeynames.contains(key)) {
throw new RuntimeException(String.format("Cannot use %s as a metadata key: It is a reserved keynames. See https://docs.coveo.com/en/78/index-content/push-api-reference#json-document-reserved-key-names", key));
}
}
}
10 changes: 9 additions & 1 deletion src/main/java/com/coveo/testlocally/TestingLocally.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,21 @@ public static void main(String[] args) {

public static void testPushDocument(String sourceId, Source source) {
DocumentBuilder doc = new DocumentBuilder("https://perdu.com", "the title").withData("this is searchable").withDate(new Date());
DocumentBuilder docWithMetadata = new DocumentBuilder("https://perdu.com/3", "the title 3").withMetadata(new HashMap<>() {{
put("foo", "bar");
put("my_field_1", "1");
put("my_field_2", false);
put("my_field_3", 1234);
put("my_field_4", new String[]{"a", "b", "c"});
}});
System.out.println(doc.marshal());
System.out.println(docWithMetadata.marshal());
try {
source.addOrUpdateDocument(sourceId, doc);
source.addOrUpdateDocument(sourceId, docWithMetadata);
} catch (IOException | InterruptedException e) {
System.out.println(e);
}

}

public static void testManageIdentities(Source source) {
Expand Down