Question

I am writting a java service where I am building the document for output. But My structure should be : OutPut Doc is the top level doc. Inside that i want to have another Doc say Intermediate doc and in this intermediate doc i want to have Key values.

But My question is how can i insert one doc to another. I see the IDataUtil has put method which ask for key as string and value can be object.

My code is IDataUtil.put(idcvalueDoc, "Body", FullValue.toString());

But this Body should not be string it should be document .I want to insert one Doc to another.

Please help me

Image of output doc structure

Was it helpful?

Solution

To accomplish what you're after, you will need to do the following:

  1. Create an intermediateDoc IData object
  2. Add key value tuples to the intermediateDoc as required
  3. Create an outputDoc IData object
  4. Add the intermediateDoc as a key value tuple to the outputDoc
  5. Add the outputDoc to the pipeline

The following is an example Java service that demonstrates this (note the key value tuples added to the intermediateDoc are hard-coded here for convenience):

public static final void exampleService(IData pipeline) throws ServiceException {
  IDataCursor pipelineCursor = pipeline.getCursor();

  try {
    // create an intermediateDoc IData object
    IData intermediateDoc = IDataFactory.create();
    // create a cursor to use to add key value tuples to the intermediateDoc
    IDataCursor intermediateCursor = intermediateDoc.getCursor();
    // add key value tuples as required to the intermediateDoc
    IDataUtil.put(intermediateCursor, "key1", "value1");
    IDataUtil.put(intermediateCursor, "key2", "value2");
    // ...
    // destroy the intermediateCursor when done adding key value tuples
    intermediateCursor.destroy();

    // create an outputDoc IData object
    IData outputDoc = IDataFactory.create();
    // create a cursor to use to add key value tuples to the outputDoc
    IDataCursor outputCursor = outputDoc.getCursor();
    // add the intermediateDoc to the outputDoc
    IDataUtil.put(outputCursor, "intermediateDoc", intermediateDoc);
    // destroy the outputCursor when done adding key value tuples
    outputCursor.destroy();

    // add the outputDoc to the pipeline
    IDataUtil.put(pipelineCursor, "outputDoc", outputDoc);
  } finally {
    // destroy the pipelineCursor
    pipelineCursor.destroy();
  }
}

OTHER TIPS

Assumption and Design Input/Output

Assuming the

  1. Document of ValuesInput[] are the input, which are same as Values[] the output.
  2. Document of columnValue under Document of ValuesInput[] has string variable named additionalString (which it would be makes no sense if there are no variable inside/under the Document)

So overall would be like this:

Input/Output Documents

Of course you could generate the code after designing the input/output by right-clicking your mouse and generate code -> For implementing this service.

Code Generation

But, instead of using the generated code, I'm trying to give an example with IDataMap which you can find in webMethods Javadoc com.softwareag.util.IDataMap. Which is very convenient to use

IDataMap

IDataMap combines the functionality of IData, IDataCursor, IDataUtil, and IDataFactory. IDataMap implements the java.util.Map interface from the Java Collections Framework, providing a familiar and simple interface. IDataMap expands the Map interface, adding getAs<Type> methods, which convert the returned value to a specific type.

And it goes like this:

public static final void mapDocument(IData pipeline) throws ServiceException
{
    // pipeline input by IDataMap
    IDataMap pipelineMap = new IDataMap(pipeline);

    // extracting Values input into IData[] variable array
    IData[] ValuesInput = pipelineMap.getAsIDataArray("ValuesInput");

    // Initiate OutDoc.Values length based on ValuesInput length
    IData[] Values = new IData[ValuesInput.length];

    // OutDoc.Values
    // Iterate and copying all ValuesInputDoc into OutDoc.Values
    for (int i = 0; i < ValuesInput.length; i++)
    {
        Values[i] = IDataUtil.clone(ValuesInput[i]);
    }

    // OutDoc
    IData OutDoc = IDataFactory.create();
    IDataMap outDocMap = new IDataMap(OutDoc);

    // OutDoc IDataMap
    String TableName = "TableName is Never assigned";
    outDocMap.put("TableName", TableName);

    // OutDoc.Values
    outDocMap.put("Values", Values);

    // Wrap the OutDoc into pipeline
    pipelineMap.put("OutDoc", OutDoc);

}

And the result

Result

With the wmboost-data library you can write this:

public static final void exampleService(IData pipeline) throws ServiceException {

    Document outputDoc = Documents.create();

    Document intermediateDoc = outputDoc.docEntry("intermediateDoc").putNew();      
    intermediateDoc.entry("key1").put("value1");
    intermediateDoc.entry("key2").put("value2");

    Documents.wrap(pipeline).entry("outputDoc").put(outputDoc);
}

The code:

  1. Creates the top-level document, outputDoc
  2. Creates intermediateDoc as nested document (an alternative is to create it independently and to attach it to its parent later)
  3. Assigns entry values to intermediateDoc
  4. Adds outputDoc to the pipeline

Disclaimer: I'm the author of wmboost-data.

Just check if any of the following in the WmPublic package would help:

pub.list:appendToDocumentList

or

pub.document:insertDocument
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top