Skip to content

Working with Documents

In order to use the services provided by the Web Service Connector, you need to become familiar with constructing and reading values from Documents built using the provided Document Class.

The following code snippets demonstrate practical examples on how to do this.

Import Libraries
import org.apache.log4j.Logger;

import com.fieldtec.webclient.model.Document;
import com.fieldtec.webclient.util.UtilJson;
Example 1 - Construct a simple document
// Construct a simple document
Document doc = new Document();
doc.setValue("assetId", "123");
doc.setValue("name", "asset name");
doc.setValue("description", "asset description");
doc.setValue("systemHeader.templateId", "5ba9bbee5b116026f1b23dad");

logger.debug(UtilJson.prettyPrint(doc));

The above example produces the following output:

{
    "assetId": "123",
    "name": "asset name",
    "description": "asset description",
    "systemHeader": {
        "templateId": "5ba9bbee5b116026f1b23dad"
    }
}
Example 2 - Get values from a document
// Get values from a document
Document doc = new Document();
doc.setValue("assetId", "123");
doc.setValue("name", "asset name");
doc.setValue("description", "asset description");
doc.setValue("systemHeader.templateId", "5ba9bbee5b116026f1b23dad");

String description = (String) doc.getValue("description");
logger.debug("description: " + description);

String templateId = (String) doc.getValue("systemHeader.templateId");
logger.debug("systemHeader.templateId: " + templateId);

The above example produces the following output:

description: asset description
systemHeader.templateId: 5ba9bbee5b116026f1b23dad
Example 3 - Working with Dates
// Working with Dates
Date buildDate = new Date();

Document doc = new Document();
doc.setValue("assetId", "123");
doc.setValue("name", "asset name");
doc.setValue("description", "asset description");
doc.setValue("buildDate", buildDate);
doc.setValue("systemHeader.templateId", "5ba9bbee5b116026f1b23dad");

logger.debug(UtilJson.prettyPrint(doc));

The above example produces the following output:

{
    "assetId": "123",
    "name": "asset name",
    "description": "asset description",
    "buildDate" : "2018-12-10T03:05:52.169Z",
    "systemHeader": {
        "templateId": "5ba9bbee5b116026f1b23dad"
    }
}
Example 4 - Working with Geojson structures
// Construct a complex document
String keyLocGeo = "location";

List coordinate = new ArrayList();
coordinate.add(xVal));
coordinate.add(yVal));

// Construct a document containing a geojson structure
Document doc = new Document();
doc.setValue("assetId", "123");
doc.setValue("name", "asset name");
doc.setValue("description", "asset description");
doc.setValue("location.type", "FeatureCollection");
doc.setValue("location.features[0].type", "Feature");
doc.setValue("location.features[0].properties.streetNo", "1");
doc.setValue("location.features[0].properties.street", "Flinders Street);
doc.setValue("location.features[0].properties.suburb", "Melbourne");
doc.setValue("location.features[0].properties.postcode", "3004");
doc.setValue("location.features[0].properties.state", "Victoria");
doc.setValue("location.features[0].properties.type", "street");
doc.setValue("location.features[0].geometry.type", "Point");
doc.setValue("location.features[0].geometry.coordinates", coordinate);
doc.setValue("systemHeader.templateId", "5ba9bbee5b116026f1b23dad");

logger.debug(UtilJson.prettyPrint(doc));

The above example produces the following output:

{
    "assetId": "123",
    "name": "asset name",
    "description": "asset description",
    "location": {
        "type": "FeatureCollection",
        "features": [
            "type": "Feature",
            "properties": {
                "streetNo": "1",
                "street": "Flinders Street",
                "suburb": "Melbourne",
                "postcode": "3004",
                "state": "Victoria",
                "type": "street"
            },
            "geometry" : {
                "type": "Point",
                "coordinates": [ 
                    144.967059, 
                    -37.817459
                ]
            }
        ]
    },
    "systemHeader": {
        "templateId": "5ba9bbee5b116026f1b23dad"
    }
}