diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/pom.xml b/pom.xml index aa10332e..74d63750 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ org.example push-api-client.java 1.0-SNAPSHOT + + + com.google.code.gson + gson + 2.8.7 + + + joda-time + joda-time + 2.10.10 + + 16 diff --git a/src/main/java/com/coveo/pushapiclient/Document.java b/src/main/java/com/coveo/pushapiclient/Document.java index f04b1891..fa27bbf7 100644 --- a/src/main/java/com/coveo/pushapiclient/Document.java +++ b/src/main/java/com/coveo/pushapiclient/Document.java @@ -82,7 +82,7 @@ public class Document { *

* See https://docs.coveo.com/en/115 for more information. */ - public Map metadata; + public Map metadata; /** * The list of permission sets for this item. *

diff --git a/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java b/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java new file mode 100644 index 00000000..80a83399 --- /dev/null +++ b/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java @@ -0,0 +1,155 @@ +package com.coveo.pushapiclient; + +import com.google.gson.Gson; +import org.joda.time.DateTime; +import org.joda.time.format.ISODateTimeFormat; + +import java.util.Date; +import java.util.Map; + +public class DocumentBuilder { + private Document document; + + public DocumentBuilder(String uri, String title) { + this.document = new Document(); + this.document.uri = uri; + this.document.title = title; + } + + public DocumentBuilder withData(String data) { + this.document.data = data; + return this; + } + + public DocumentBuilder withDate(String date) { + DateTime dt = DateTime.parse(date); + this.document.date = this.dateFormat(dt); + return this; + } + + public DocumentBuilder withDate(Long date) { + DateTime dt = new DateTime(date); + this.document.date = this.dateFormat(dt); + return this; + } + + public DocumentBuilder withDate(Date date) { + DateTime dt = new DateTime(date); + this.document.date = this.dateFormat(dt); + return this; + } + + public DocumentBuilder withDate(DateTime date) { + this.document.date = this.dateFormat(date); + return this; + } + + public DocumentBuilder withModifiedDate(String date) { + DateTime dt = DateTime.parse(date); + this.document.modifiedDate = this.dateFormat(dt); + return this; + } + + public DocumentBuilder withModifiedDate(Long date) { + DateTime dt = new DateTime(date); + this.document.modifiedDate = this.dateFormat(dt); + return this; + } + + public DocumentBuilder withModifiedDate(DateTime date) { + this.document.modifiedDate = this.dateFormat(date); + return this; + } + + public DocumentBuilder withPermanentId(String permanentId) { + this.document.permanentId = permanentId; + return this; + } + + public DocumentBuilder withCompressedBinaryData(CompressedBinaryData compressedBinaryData) { + this.validateCompressedBinaryData(compressedBinaryData.data); + this.document.compressedBinaryData = compressedBinaryData; + return this; + } + + public DocumentBuilder withFileExtension(String fileExtension) { + this.validateFileExtension(fileExtension); + this.document.fileExtension = fileExtension; + return this; + } + + public DocumentBuilder withParentID(String parentID) { + this.document.parentId = parentID; + return this; + } + + public DocumentBuilder withClickableUri(String clickableUri) { + this.document.clickableUri = clickableUri; + return this; + } + + public DocumentBuilder withAuthor(String author) { + this.document.author = author; + return this; + } + + public DocumentBuilder withMetadataValue(String key, String metadataValue) { + this.setMetadataValue(key, metadataValue); + return this; + } + + public DocumentBuilder withMetadataValue(String key, String[] metadataValue) { + this.setMetadataValue(key, metadataValue); + return this; + } + + public DocumentBuilder withMetadataValue(String key, Integer metadataValue) { + this.setMetadataValue(key, metadataValue); + return this; + } + + public DocumentBuilder withMetadataValue(String key, Integer[] metadataValue) { + this.setMetadataValue(key, metadataValue); + return this; + } + + public DocumentBuilder withMetadata(Map metadata) { + metadata.forEach(this::setMetadataValue); + return this; + } + + public DocumentBuilder withAllowedPermissions() { + // TODO + return this; + } + + public DocumentBuilder withDeniedPermissions() { + // TODO + return this; + } + + public String marshal() { + return new Gson().toJson(this.document); + } + + private String dateFormat(DateTime dt) { + return dt.toString(ISODateTimeFormat.dateTime()); + } + + private void setMetadataValue(String key, Object metadataValue) { + this.validateReservedMetadataKeyNames(key); + this.document.metadata.put(key, metadataValue); + } + + private void validateCompressedBinaryData(String data) { + // TODO + } + + private void validateFileExtension(String fileExtension) { + // TODO + } + + private void validateReservedMetadataKeyNames(String key) { + // TODO + } +} diff --git a/src/main/java/com/coveo/testlocally/TestingLocally.java b/src/main/java/com/coveo/testlocally/TestingLocally.java new file mode 100644 index 00000000..6d5f9bce --- /dev/null +++ b/src/main/java/com/coveo/testlocally/TestingLocally.java @@ -0,0 +1,12 @@ +package com.coveo.testlocally; + +import com.coveo.pushapiclient.DocumentBuilder; + +import java.util.Date; + +public class TestingLocally { + public static void main(String[] args) { + DocumentBuilder doc = new DocumentBuilder("https://perdu.com", "the title").withData("this is searchable").withDate(new Date()); + System.out.println(doc.marshal()); + } +}