Java Code Examples for com.orientechnologies.orient.core.record.impl.ODocument#fromMap()

The following examples show how to use com.orientechnologies.orient.core.record.impl.ODocument#fromMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContentStore.java    From jbake with MIT License 5 votes vote down vote up
/**
 * 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.isEmpty()) {
        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;
}
 
Example 2
Source File: ContentStoreTest.java    From jbake with MIT License 5 votes vote down vote up
@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());
}
 
Example 3
Source File: OrientHttpClientConfigurationEntityAdapter.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected void writeFields(final ODocument document, final OrientHttpClientConfiguration entity) throws Exception {
  Map<String, Object> fields = objectMapper.convertValue(entity, MAP_STRING_OBJECT);
  log.trace("Writing fields: {}", fields);
  document.fromMap(fields);
}
 
Example 4
Source File: Crawler.java    From jbake with MIT License 4 votes vote down vote up
private void crawlSourceFile(final File sourceFile, final String sha1, final String uri) {
    try {
        Map<String, Object> fileContents = parser.processFile(sourceFile);
        if (fileContents != null) {
            fileContents.put(Attributes.ROOTPATH, getPathToRoot(sourceFile));
            fileContents.put(String.valueOf(DocumentAttributes.SHA1), sha1);
            fileContents.put(String.valueOf(DocumentAttributes.RENDERED), false);
            if (fileContents.get(Attributes.TAGS) != null) {
                // store them as a String[]
                String[] tags = (String[]) fileContents.get(Attributes.TAGS);
                fileContents.put(Attributes.TAGS, tags);
            }
            fileContents.put(Attributes.FILE, sourceFile.getPath());
            fileContents.put(String.valueOf(DocumentAttributes.SOURCE_URI), uri);
            fileContents.put(Attributes.URI, uri);

            String documentType = (String) fileContents.get(Attributes.TYPE);
            if (fileContents.get(Attributes.STATUS).equals(Status.PUBLISHED_DATE)) {
                if (fileContents.get(Attributes.DATE) != null && (fileContents.get(Attributes.DATE) instanceof Date)) {
                    if (new Date().after((Date) fileContents.get(Attributes.DATE))) {
                        fileContents.put(Attributes.STATUS, Status.PUBLISHED);
                    }
                }
            }

            if (config.getUriWithoutExtension()) {
                fileContents.put(Attributes.NO_EXTENSION_URI, uri.replace("/index.html", "/"));
            }

            if (config.getImgPathUpdate()) {
                // Prevent image source url's from breaking
                HtmlUtil.fixImageSourceUrls(fileContents, config);
            }

            ODocument doc = new ODocument(documentType);
            doc.fromMap(fileContents);
            boolean cached = fileContents.get(String.valueOf(DocumentAttributes.CACHED)) != null ? Boolean.valueOf((String) fileContents.get(String.valueOf(DocumentAttributes.CACHED))) : true;
            doc.field(String.valueOf(DocumentAttributes.CACHED), cached);
            doc.save();
        } else {
            LOGGER.warn("{} has an invalid header, it has been ignored!", sourceFile);
        }
    } catch (Exception ex) {
        throw new RuntimeException("Failed crawling file: " + sourceFile.getPath() + " " + ex.getMessage(), ex);
    }
}