What is the proper way to export data from an ArrayList in moqui - moqui

I am trying to export data in XML file. I have several entity from where I retrieve all the necessary data in an ArrayList. I can utilize this ArrayList and export data by embedding Java / groovy code in the script tag. But I like to know is there any other way to export data in Moqui?
I am only aware of the writeXmlText method which can write entity data in XML.
EDIT: This is my sample service
<service verb="get" noun="ExportData" type="script" location="component://PracticeTutorial/service/ExportTutorialServices/getExportData.groovy">
<out-parameters>
<parameter name="employees" type="List">
<parameter name="employee" type="Map">
<parameter name="empId"/>
<parameter name="firstName"/>
<parameter name="lastName"/>
<parameter name="designation"/>
</parameter>
</parameter>
</out-parameters>
</service>
The sample Groovy script:
import org.moqui.entity.EntityList
import org.moqui.entity.EntityValue
employees = []
EntityList employeeList = ec.entity.makeFind("tutorial.Employee").list()
for(EntityValue ev : employeeList) {
employees.add([empId:ev.empId, firstName:ev.firstName,
lastName:ev.lastName, designation:ev.designation])
}
I called the service like this:
<transition name="export">
<actions>
<service-call name="ExportTutorialServices.get#ExportData" in-map="context" out-map="context"/>
</actions>
<default-response url="."/>
</transition>
Question 1: How should I access the employees ArrayList from the screen after calling the service?
Question 2: Is there any simpler way to export data from ArrayList employees? I want to produce output like this.
Expected output:
<employees>
<employee>
<id>001</id>
<firstName>John</firstName>
<lastName>Doe</lastName>
<designation>Developer</designation>
</employee>
</employees>
Thanks

In short it sounds like you want to create an XML document with a specific structure, ultimately based on data from a database.
There are various tools for doing this in Java, the Groovy Node and XML API being a pretty good one. Appending strings to a Writer or StringBuilder is sometimes a good way to go, because the output format is so simple (for smaller documents, or those that follow particular patterns anyway).
For more complex documents, such as if the list you mentioned is one of dozens or the elements are nested a few levels deep, then I'd recommend using an FTL template to generate the XML and a Moqui Screen with actions to call your data prep service, and a screen.widgets.render-mode element to include the FTL template.
With the FTL wrapped in a screen approach you can use the ScreenFacade.makeRender() method to create a ScreenRender object. On that object set the root screen location, a render mode of "xml", other options as desired, and then call the render(Writer) method or the render() method that returns a String. With these methods you can stream the text to a file, send it over a web request, or whatever.

Related

Is there any existing service in HotDocs tools to receive data from an external source to prepare a document?

HotDocs is a tool to generate documents and basically it carries 2 things. First is temple and second is answer file. Template carries variables and data to those variables are pushed through answer file.
Generally answer file is page where is it asks for data and further it generates a document.
Now our requirement is - instead of passing variable's values through answer file, I need to send through a API built using PHP which provides data in JSON format.
IS there any exiting service in HotDocs to serve this kind requests?. I can change the data from JSON to XML if required.
At the moment there is no off the shelf converter from JSON to HotDocs Answer XML however, at HotDocs we do this all the time. If you produce either JSON or XML from your application the data will need to be transformed into the HotDocs answer XML format - e.g.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AnswerSet title="Demo Answers" version="1.1">
<Answer name="Employee Name">
<TextValue>Graham Penman</TextValue>
</Answer>
<Answer name="Job Duty">
<RptValue>
<TextValue>make tea</TextValue>
<TextValue>make coffee</TextValue>
<TextValue>make some cake</TextValue>
</RptValue>
</Answer>
<Answer name="Annual Salary">
<NumValue>12.0000000</NumValue>
</Answer>
<Answer name="Contract Date">
<DateValue>10/10/2016</DateValue>
</Answer>
<Answer name="Paid Seminar Days">
<TFValue>false</TFValue>
</Answer>
</AnswerSet>
There are three key things you need to know to create the answer XML: The data type of your data, the data type in HotDocs and whether the data you are passing is a list or single item.
So to build the answer XML is relatively easy.
The answer XML is essentially key value pairs being contained between the opening and closing tags:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AnswerSet title="Demo Answers" version="1.1">
...Answers go here
</AnswerSet>
We then add answers in by adding the following and specifying the variable in the template the answer corresponds to, the actual value (from your data) you want to set the answer to and also the type of data it is in the template - in the example below it is text however, the type in HotDocs are: TextValue (string), NumValue (decimal), TFValue (boolean), DateValue (DateTime) and MCValue (see later on in this answer).
<Answer name="[Variable name in template]">
<TextValue>[Value from your data]</TextValue>
</Answer>
For multiple choices specifically you can select one or more answers so the answer XML format is slightly different:
<Answer name="[Variable name in template]">
<MCValue>
<SelValue>[First selected value]</SelValue>
<SelValue>[Second selected value]</SelValue>
</MCValue>
</Answer>
If you have repeated data you want to put into the document you can use the list repeat format:
<Answer name="[Variable name in template]">
<RptValue>
<[Variable Type]>[First value]</[Variable Type]>
<[Variable Type]>[Second value]</[Variable Type]>
</RptValue>
</Answer>
Once you build this XML structure you can pass this into the assemble document method on the REST services as a string with the template to assemble the corresponding documents.

Can I iterate form-lists in Moqui?

Is there a way to do the following in Moqui?
Say I have a list of parent categories (or classifications etc.)... Taking Request categories:
<entity-find entity-name="mantle.request.RequestCategory" list="parentCategoryList">
<econdition field-name="parentCategoryId" operator="is-null" />
</entity-find>
And I want to use 'parentCategoryList' to produce a sub-list for EACH parent category, to display separate form-lists on screen:
Something like:
<iterate list="parentCategoryList" entry="thisCategory" >
<entity-find entity-name="mantle.request.RequestCategory" list="categoryList">
<econdition field-name="parentCategoryId" from="thisCategory.requestCategoryId" />
</entity-find>
<!-- I include the following only to give an idea of what I am trying to do.
It is incorrect and incomplete -->
<script>listOfLists.add(categoryList)</script>
</iterate>
Then use that 'listOfLists' to iterate a form-list, supplying the form-list 'name' and 'list' sequentially for each list in the list. (I know you can't use iterate outside of actions, and you can't use forms inside of actions.)
I may well be thinking about this in the wrong way.
You can iterate within the screen.widgets element, just use section-iterate. There are limitations to how much you can nest these (the current template macros for XML Screens/Forms only support so much), but you can do quite a bit. There are example of this in SimpleScreens, like the OrderDetail.xml screen iterating over order parts.

How to map specified pattern string into a xml using Mule data mapper

I have requirement to map specified patterned string to xml using data mapper in Mule.
Input payload (String) to Data mapper:
key1:value1,key2:value2
output payload (xml) from Data Mapper:
<control>
<Parameter>
<Key>key1</Key>
<Value>value1</Value>
</Parameter>
<Parameter>
<Key>key2</Key>
<Value>value2</Value>
</Parameter>
</control>
Can someone give me the solution to achieve this using data mapper in mule?
DataMapper has a script view (upper right-hand corner of the DataMapper console).
Create a mapping of the string to the key, then edit the mapping, by default it's written in the Mulee Expression Language. With it you can just split the value in two, and set to output values rathen than one. This will leave only one visual line.
You could also draw to mappings an then split on both, just taking value 0 in one and 1 in the other, this will be represented more beatifully in the visual editor but will perform worse.

Mule datamapper: Mapping a value to an attribute of right hand side

I have an xml like:
<name>Mule</name>
<company>mulesoft</company>
How can I map using datamapper to get result like:
<details name="Mule" company="mulesoft" />
I tried but datamapper never shows the attributes to map.
I am using Mule 3.5.1
you need to either supply the schema or provide an example for both sides.
For example use the following as the sample source. DataMapper will generate the XSD based on the example file:
<enity>
<name>Mule</name>
<company>mulesoft</company>
</enity>
Right side do the same but use
<company>
<details name="Mule" company="mulesoft" />
</company>
Then click Create Mappings and you should see the mappings to your attributes.

XPath for web.config applicationSettings webdeploy parameters.xml file is not correct

I use webdeploy to deploy my web site project with a parameters.xml file I have been using a for a while. So far the parameters I've added are all element attributes and it all works well. But I am trying to get the xpath right to update an applicationSettings element value (not attributes) and am failing, badly, to work out if its my poor xpath skills to blame or a misunderstanding of the way the parameters file works.
When I do a deployment the field is not updated, it compiles fine and no errors\warnings during deployment. I want to be able to set this to True or False.
So I have following parameters field
<parameter name="ShowExceptionCallStackOnErrorView" description="Display a call stack on the UI Error view - true for debug only." defaultValue="False" tags="">
<parameterEntry kind="XmlFile" scope="\\web.config$" match="/configuration/applicationSettings/abc.123.Properties.Settings/setting[#name='ShowExceptionCallStackOnErrorView']/value" />
</parameter>
trying to match to the following application settings section
<configuration>
<applicationSettings>
<abc.123.Properties.Settings>
<setting name="ShowExceptionCallStackOnErrorView" serializeAs="String">
<value>True</value>
Any help would be much appreciated!
It's not giving you an error because it is simply not finding a match to replace. You need to add /text() to the end of your match tag if you want it to replace the contents of the value tag. As follows...
<parameter name="ShowExceptionCallStackOnErrorView" description="Display a call stack on the UI Error view - true for debug only." defaultValue="False" tags="">
<parameterEntry kind="XmlFile" scope="\\web.config$" match="/configuration/applicationSettings/abc.123.Properties.Settings/setting[#name='ShowExceptionCallStackOnErrorView']/value/text()" />
</parameter>