Written by

Senior Startups and Community Programs Manager at InterSystems Corporation
Question Evgeny Shvarov · Jun 20, 2023

How to Transform a String into a Stream in Interoperability Production?

Hi folks!

I have an example where I want to write a string into a file.

I have a message with string and I want to write the string down into a file.

I took EnsLib.FilePathThrough.Operation which expects Ens.StreamContainer message.

So I perform a data transformation where I need to transfer string to a stream. Here is the DataTransformation:

And on the second step there is an ObjectScript code:

As you could notice, this is not an obvious transformation. Indeed, first I ought to create an instance of Stream object.

Then I'm running an ObjectScript which writes string to Stream. Notice a space(" ") symbol as the first symbol of the code line. Is it obvious?

And this works.

Question: is there a simpler way to perform such a transformation from the string to a stream?

What I have in mind is that I derive from EnsLib.FilePathThrough.Operation into a new message class where I add a string property which I can initialize with the string. And then the operation will take the data from string and write it to file. Or init the stream by itself with this string in once created.

Thoughts?

Product version: IRIS 2023.1

Comments

Alex Woodhead · Jun 21, 2023

Hi Evgeny,

An option is to use a FunctionSet, for example:

 Class Test.Str2Stream Extends Ens.Rule.FunctionSet
{ ClassMethod StringToStream(initValue As %String = "undefined", filename As %String = "") As %GlobalCharacterStream
{
set ret=##class(%GlobalCharacterStream).%New()
set ret.Attributes("Filename")=filename
do ret.Write(initValue)
quit ret
}
}

This will let you see the nice single line "From Source property" and "To Target Property"

Where assignment value was:

##class(Test.Str2Stream).StringToStream(source.Content,source.Filename)

The Gotcha

If you have a test form input message:

<ExampleIn>
  <Filename>ABC</Filename>
  <Content>ho ho ho</Content>
</ExampleIn>

And this is not populating output like:

<StreamContainer>
  <OriginalFilename>ABC</OriginalFilename>
  <Stream><![CDATA[ho ho ho]</Stream>
  <Type>GC</Type>
</StreamContainer>

Then go back and check the input message type extends %XML.Adaptor.

 Class Test.Str2Stream.ExampleIn Extends (%Persistent, %XML.Adaptor)

ie: It is a limitation of the Test form that it expects to be able to correlate the content from XML to Test Object.

Hope this helps.

0
Evgeny Shvarov  Jun 21, 2023 to Alex Woodhead

Neat!

@Alex Woodhead , noticed you derived the class from Ens.Rule.FunctionSet

What benefits does it give?

I was hoping to see the compiled function in the available functions list here:

But it doesn't.

0
Alex Woodhead  Jun 21, 2023 to Evgeny Shvarov

Hi Evgeny,

They should show up after being compiled.

May need to close the DTL completely and reopen to get the list to repopulate?

The benefits should be for easy reusability in Transforms and Business Rules

0
Evgeny Shvarov  Jun 21, 2023 to Alex Woodhead

Yes, indeed!

I had an issue with VSCode that didn't compile code. But VSCode window reload solved the problem and I saw the function in the list! Perfect!

Thank you, @Alex Woodhead !

0