Skip to content

Commit

Permalink
Merge pull request #520 from OndraZizka/oz13-implMergeDocument
Browse files Browse the repository at this point in the history
Implement ContentStore#mergeDocument(Map<String, Object>) to update d…
  • Loading branch information
jonbullock committed Dec 6, 2018
2 parents 6eaae8f + 4809fbb commit 7621cca
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
39 changes: 39 additions & 0 deletions jbake-core/src/main/java/org/jbake/app/ContentStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.Map;
import java.util.Set;


/**
* @author jdlee
*/
Expand Down Expand Up @@ -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<String, ? extends Object> 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<ODocument> results = db.command(new OSQLSynchQuery<ODocument>(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);
Expand All @@ -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);
Expand Down
48 changes: 43 additions & 5 deletions jbake-core/src/test/java/org/jbake/app/ContentStoreTest.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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());
}

}

0 comments on commit 7621cca

Please sign in to comment.