Skip to content

Commit

Permalink
Implement ContentStore#mergeDocument(Map<String, Object>) to update d…
Browse files Browse the repository at this point in the history
…ocs.
  • Loading branch information
OndraZizka committed Oct 30, 2018
1 parent 629c63b commit 00ff5bd
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions jbake-core/src/main/java/org/jbake/app/ContentStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,16 @@
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import org.jbake.model.DocumentAttributes;
import org.jbake.model.DocumentTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jbake.model.DocumentAttributes;
import org.jbake.model.DocumentTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author jdlee
Expand Down Expand Up @@ -173,6 +172,36 @@ private void activateOnCurrentThread() {
db.activateOnCurrentThread();
}


/**
* Get a document by sourceUri and update it from the given map.
* @return the saved document.
* @throws Exception if sourceUri or docType are null, or if the document doesn't exist.
*/
public ODocument mergeDocument(Map<String, 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 RuntimeException("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 Down

0 comments on commit 00ff5bd

Please sign in to comment.