diff --git a/jbake-core/src/main/java/org/jbake/app/ContentStore.java b/jbake-core/src/main/java/org/jbake/app/ContentStore.java index 90485b4d3..7fbbd48ab 100644 --- a/jbake-core/src/main/java/org/jbake/app/ContentStore.java +++ b/jbake-core/src/main/java/org/jbake/app/ContentStore.java @@ -47,6 +47,7 @@ import java.util.Map; import java.util.Set; + /** * @author jdlee */ @@ -173,6 +174,37 @@ private void activateOnCurrentThread() { db.activateOnCurrentThread(); } + + /** + * Get a document by sourceUri and update it from the given map. + * @param incomingDocMap The document's db columns. + * @return The saved document. + * @throws IllegalArgumentException if sourceUri or docType are null, or if the document doesn't exist. + */ + public ODocument mergeDocument(Map incomingDocMap) + { + String sourceUri = (String) incomingDocMap.get(DocumentAttributes.SOURCE_URI.toString()); + if (null == sourceUri) + throw new IllegalArgumentException("Document sourceUri is null."); + String docType = (String) incomingDocMap.get(Crawler.Attributes.TYPE); + if (null == docType) + throw new IllegalArgumentException("Document docType is null."); + + // Get a document by sourceUri + String sql = "SELECT * FROM " + docType + " WHERE sourceuri=?"; + activateOnCurrentThread(); + List results = db.command(new OSQLSynchQuery(sql)).execute(sourceUri); + if (results.size() == 0) + throw new JBakeException("No document with sourceUri '"+sourceUri+"'."); + + // Update it from the given map. + ODocument incomingDoc = new ODocument(docType); + incomingDoc.fromMap(incomingDocMap); + ODocument merged = results.get(0).merge(incomingDoc, true, false); + return merged; + } + + public long getDocumentCount(String docType) { activateOnCurrentThread(); return db.countClass(docType); @@ -183,6 +215,13 @@ public long getPublishedCount(String docType) { return (Long) query(statement).get(0).get("count"); } + /* + * In fact, the URI should be the only input as there can only be one document at given URI; but the DB is split per document type for some reason. + */ + public DocumentList getDocumentByUri(String docType, String uri) { + return query("select * from " + docType + " where sourceuri=?", uri); + } + public DocumentList getDocumentStatus(String docType, String uri) { String statement = String.format(STATEMENT_GET_DOCUMENT_STATUS_BY_DOCTYPE_AND_URI, docType); return query(statement, uri); diff --git a/jbake-core/src/test/java/org/jbake/app/ContentStoreTest.java b/jbake-core/src/test/java/org/jbake/app/ContentStoreTest.java index f6092da24..39915cf54 100644 --- a/jbake-core/src/test/java/org/jbake/app/ContentStoreTest.java +++ b/jbake-core/src/test/java/org/jbake/app/ContentStoreTest.java @@ -1,31 +1,69 @@ package org.jbake.app; +import com.orientechnologies.orient.core.record.impl.ODocument; +import java.util.HashMap; +import java.util.Map; import org.jbake.FakeDocumentBuilder; +import org.jbake.model.DocumentAttributes; import static org.junit.Assert.assertEquals; -import org.junit.BeforeClass; import org.junit.Test; public class ContentStoreTest extends ContentStoreIntegrationTest { + public static final String DOC_TYPE_POST = "post"; + @Test public void shouldGetCountForPublishedDocuments() throws Exception { for (int i = 0; i < 5; i++) { - FakeDocumentBuilder builder = new FakeDocumentBuilder("post"); + FakeDocumentBuilder builder = new FakeDocumentBuilder(DOC_TYPE_POST); builder.withName("dummyfile" + i) .withStatus("published") .withRandomSha1() .build(); } - FakeDocumentBuilder builder = new FakeDocumentBuilder("post"); + FakeDocumentBuilder builder = new FakeDocumentBuilder(DOC_TYPE_POST); builder.withName("draftdummy") .withStatus("draft") .withRandomSha1() .build(); - assertEquals(6, db.getDocumentCount("post")); - assertEquals(5, db.getPublishedCount("post")); + assertEquals(6, db.getDocumentCount(DOC_TYPE_POST)); + assertEquals(5, db.getPublishedCount(DOC_TYPE_POST)); + } + + @Test + public void testMergeDocument() { + final String uri = "test/testMergeDocument"; + + ODocument doc = new ODocument(DOC_TYPE_POST); + Map values = new HashMap(); + values.put(Crawler.Attributes.TYPE, DOC_TYPE_POST); + values.put(DocumentAttributes.SOURCE_URI.toString(), uri); + values.put("foo", "originalValue"); + doc.fromMap(values); + doc.save(); + + // 1st + values.put("foo", "newValue"); + db.mergeDocument(values); + + DocumentList docs = db.getDocumentByUri(DOC_TYPE_POST, uri); + assertEquals(1, docs.size()); + assertEquals("newValue", docs.get(0).get("foo")); + + // 2nd + values.put("foo", "anotherValue"); + db.mergeDocument(values); + + docs = db.getDocumentByUri(DOC_TYPE_POST, uri); + assertEquals(1, docs.size()); + assertEquals("anotherValue", docs.get(0).get("foo")); + + db.deleteContent(DOC_TYPE_POST, uri); + docs = db.getDocumentByUri(DOC_TYPE_POST, uri); + assertEquals(0, docs.size()); } }