Example usage for com.fasterxml.jackson.core JsonFactory JsonFactory

List of usage examples for com.fasterxml.jackson.core JsonFactory JsonFactory

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonFactory JsonFactory.

Prototype

public JsonFactory() 

Source Link

Document

Default constructor used to create factory instances.

Usage

From source file:com.marklogic.client.functionaltest.TestBiTemporal.java

@Test
// Test Period Range Query usig ALN_CONTAINS. We use 2 axes during query
// Note that the query will be done for every axis across every period. And
// the results will be an OR of the result of each of the query done for every axis 
// across every period
public void testPeriodRangeQueryMultiplesAxesBasedOnALNContains() throws Exception {
    System.out.println("Inside testPeriodRangeQueryMultiplesAxesBasedOnALNContains");

    // Read documents based on document URI and ALN_OVERLAPS. We are just
    // looking//from   ww  w  . ja  v  a2 s .c om
    // for count of documents to be correct

    String docId = "javaSingleJSONDoc.json";

    insertJSONSingleDocument(temporalCollectionName, docId, null);
    updateJSONSingleDocument(temporalCollectionName, docId);

    // Fetch documents associated with a search term (such as XML) in Address
    // element
    QueryManager queryMgr = readerClient.newQueryManager();
    StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder();

    StructuredQueryDefinition termQuery = sqb.collection(docId);

    StructuredQueryBuilder.Axis validAxis1 = sqb.axis(axisValidName);
    Calendar start1 = DatatypeConverter.parseDateTime("2001-01-01T00:00:01");
    Calendar end1 = DatatypeConverter.parseDateTime("2011-12-31T23:59:58");
    StructuredQueryBuilder.Period period1 = sqb.period(start1, end1);

    StructuredQueryBuilder.Axis validAxis2 = sqb.axis(axisValidName);
    Calendar start2 = DatatypeConverter.parseDateTime("2003-01-01T00:00:01");
    Calendar end2 = DatatypeConverter.parseDateTime("2008-12-31T23:59:58");
    StructuredQueryBuilder.Period period2 = sqb.period(start2, end2);

    StructuredQueryBuilder.Axis[] axes = new StructuredQueryBuilder.Axis[] { validAxis1, validAxis2 };
    StructuredQueryBuilder.Period[] periods = new StructuredQueryBuilder.Period[] { period1, period2 };

    StructuredQueryDefinition periodQuery = sqb.and(termQuery,
            sqb.temporalPeriodRange(axes, TemporalOperator.ALN_CONTAINS, periods));

    // Note that the query will be done for every axis across every period. And
    // the results will be an OR of the result of each of the query done for every axis 
    // across every period
    long start = 1;
    JSONDocumentManager docMgr = readerClient.newJSONDocumentManager();
    docMgr.setMetadataCategories(Metadata.ALL); // Get all metadata
    DocumentPage termQueryResults = docMgr.search(periodQuery, start);

    long count = 0;
    while (termQueryResults.hasNext()) {
        ++count;
        DocumentRecord record = termQueryResults.next();
        System.out.println("URI = " + record.getUri());

        DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();
        record.getMetadata(metadataHandle);
        Iterator<String> resCollections = metadataHandle.getCollections().iterator();
        while (resCollections.hasNext()) {
            System.out.println("Collection = " + resCollections.next());
        }

        if (record.getFormat() != Format.JSON) {
            assertFalse("Invalid document format: " + record.getFormat(), true);
        } else {
            JacksonDatabindHandle<ObjectNode> recordHandle = new JacksonDatabindHandle<ObjectNode>(
                    ObjectNode.class);
            record.getContent(recordHandle);
            System.out.println("Content = " + recordHandle.toString());

            JsonFactory factory = new JsonFactory();
            ObjectMapper mapper = new ObjectMapper(factory);
            TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
            };

            HashMap<String, Object> docObject = mapper.readValue(recordHandle.toString(), typeRef);

            @SuppressWarnings("unchecked")
            HashMap<String, Object> validNode = (HashMap<String, Object>) (docObject.get(validNodeName));

            String validStartDate = (String) validNode.get(validStartERIName);
            String validEndDate = (String) validNode.get(validEndERIName);
            System.out.println("validStartDate = " + validStartDate);
            System.out.println("validEndDate = " + validEndDate);

            assertTrue("Valid start date check failed", (validStartDate.equals("2001-01-01T00:00:00")
                    || validStartDate.equals("2003-01-01T00:00:00")));
            assertTrue("Valid end date check failed",
                    (validEndDate.equals("2011-12-31T23:59:59") || validEndDate.equals("2008-12-31T23:59:59")));
        }
    }

    System.out.println("Number of results using SQB = " + count);
    assertEquals("Wrong number of results", 2, count);
}

From source file:com.marklogic.client.functionaltest.TestBiTemporal.java

@Test
// Test Period Compare Query using ALN_CONTAINS as the operator
public void testPeriodCompareQueryBasedOnALNContains() throws Exception {
    System.out.println("Inside testPeriodCompareQueryBasedOnALNContains");

    // Read documents based on document URI and ALN Contains. We are just
    // looking for count of documents to be correct
    String docId = "javaSingleJSONDoc.json";
    ConnectedRESTQA.updateTemporalCollectionForLSQT(dbName, temporalLsqtCollectionName, true);

    Calendar insertTime = DatatypeConverter.parseDateTime("2005-01-01T00:00:01");
    insertJSONSingleDocument(temporalLsqtCollectionName, docId, null, null, insertTime);

    Calendar updateTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01");
    updateJSONSingleDocument(temporalLsqtCollectionName, docId, null, updateTime);

    // Fetch documents associated with a search term (such as XML) in Address
    // element/*w  ww  .j  av  a  2  s. c  o  m*/
    QueryManager queryMgr = readerClient.newQueryManager();
    StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder();

    StructuredQueryBuilder.Axis validAxis = sqb.axis(axisValidName);
    StructuredQueryBuilder.Axis systemAxis = sqb.axis(axisSystemName);

    StructuredQueryDefinition termQuery = sqb.collection(docId);
    StructuredQueryDefinition periodQuery = sqb.and(termQuery,
            sqb.temporalPeriodCompare(validAxis, TemporalOperator.ALN_CONTAINS, systemAxis));

    long start = 1;
    JSONDocumentManager docMgr = readerClient.newJSONDocumentManager();
    docMgr.setMetadataCategories(Metadata.ALL); // Get all metadata
    DocumentPage termQueryResults = docMgr.search(periodQuery, start);

    long count = 0;
    while (termQueryResults.hasNext()) {
        ++count;
        DocumentRecord record = termQueryResults.next();
        System.out.println("URI = " + record.getUri());

        DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();
        record.getMetadata(metadataHandle);
        Iterator<String> resCollections = metadataHandle.getCollections().iterator();
        while (resCollections.hasNext()) {
            System.out.println("Collection = " + resCollections.next());
        }

        if (record.getFormat() == Format.XML) {
            DOMHandle recordHandle = new DOMHandle();
            record.getContent(recordHandle);
            System.out.println("Content = " + recordHandle.toString());
        } else {
            JacksonDatabindHandle<ObjectNode> recordHandle = new JacksonDatabindHandle<ObjectNode>(
                    ObjectNode.class);
            record.getContent(recordHandle);
            System.out.println("Content = " + recordHandle.toString());

            JsonFactory factory = new JsonFactory();
            ObjectMapper mapper = new ObjectMapper(factory);
            TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
            };

            HashMap<String, Object> docObject = mapper.readValue(recordHandle.toString(), typeRef);

            @SuppressWarnings("unchecked")
            HashMap<String, Object> systemNode = (HashMap<String, Object>) (docObject.get(systemNodeName));

            String systemStartDate = (String) systemNode.get(systemStartERIName);
            String systemEndDate = (String) systemNode.get(systemEndERIName);
            System.out.println("systemStartDate = " + systemStartDate);
            System.out.println("systemEndDate = " + systemEndDate);

            @SuppressWarnings("unchecked")
            HashMap<String, Object> validNode = (HashMap<String, Object>) (docObject.get(validNodeName));

            String validStartDate = (String) validNode.get(validStartERIName);
            String validEndDate = (String) validNode.get(validEndERIName);
            System.out.println("validStartDate = " + validStartDate);
            System.out.println("validEndDate = " + validEndDate);

            assertTrue("Valid start date check failed", (validStartDate.contains("2001-01-01T00:00:00")
                    && validEndDate.contains("2011-12-31T23:59:59")));

            assertTrue("System start date check failed", (systemStartDate.contains("2005-01-01T00:00:01")
                    && systemEndDate.contains("2010-01-01T00:00:01")));

        }
    }

    System.out.println("Number of results using SQB = " + count);
    assertEquals("Wrong number of results", 1, count);
}