TIBCO BW6 custom function with dateTime return type - tibco-business-works

I'm building a custom function for BW6 that should parse ISO 8601 formatted string to a dateTime object.
So far I have built this function:
#XPathFunction(helpText = "Parse a ISO 8601 datetime string",
parameters = {
#XPathFunctionParameter(name = "isoDateTimeString", optional = false)
},
returnType = "dateTime")
public Date parseIsoDateTime(String isoDateTimeString) throws ParseException {
StringBuilder dateFormatBuilder = new StringBuilder();
dateFormatBuilder.append("yyyy-MM-dd'T'HH:mm:ss");
if (isoDateTimeString.contains(".")) {
dateFormatBuilder.append(".SSS");
}
DateFormat dateFormat = new SimpleDateFormat(dateFormatBuilder.toString());
return dateFormat.parse(isoDateTimeString);
}
When I run this function an exception is thrown by BW
09:02:42.412 ERROR [bwEngThread:In-Memory Process Worker-1] com.tibco.bw.core - TIBCO-BW-CORE-500050: The BW process [demo.parse-datetime.module.Process] instance faulted, JobId [bw0a100], ProcessInstanceId [bw0a100], ParentProcessInstanceId [-], Module [demo.parse-datetime.module:1.0.0.qualifier], Application [demo.parse-datetime:1.0].
<CausedBy> TIBCO-BW-CORE-500058: Activity [SetDateTimeValue] XML related error.
<CausedBy> com.tibco.pvm.dataexch.xml.util.exceptions.PmxException: PVM-XML-106017: Expression Evaluation Error: 'bxcom:parseIsoDateTime($dateTimeAsString)'
<CausedBy> java.lang.ClassCastException: java.util.Date cannot be cast to com.tibco.xml.data.primitive.XmlAtomicValue
So how can I return an XML dateTime object from a custom xslt function in BW6.

You need to create a Plug-in Project to allow Custom XPath Function. (You will need the ActiveMatrix BusinessWorks™ Plug-in Development Kit)
Create Custom XPath Functions
If you want to use them Design time, you'll need to export the Plugin Project and include the jar as a dependency.
Using Custom XPath Function at Design Time and Run Time

Related

Initialize SNSEvent.java with SNS Event String

I'm working with an in-house framework to consume S3Events. The current framework can handle plain S3EventNotifications by getting the events InputStream, and passing it like below...
String inputStream = CharStreams.toString( new InputStreamReader(eventInputStream, StandardCharsets.UTF_8));
S3EventNotification s3EventNotification = S3EventNotification.parseJson(inputStream);
For technical reasons, I now need to update this framework to use SNS Events, which are quite different from a raw JSON perspective. I'm hoping to find an SNSEvent equivelant of S3EventNotification so I can do something like below...
String inputStream = CharStreams.toString( new InputStreamReader(eventInputStream, StandardCharsets.UTF_8));
SNSEvent snsEvent = SomethingGoesHere.parseJson(inputStream);
Is there anything like that out there? I've looked through a few of the aws-java-sdk's as well as the aws-lambda-java-events artifacts out there in Maven world but can't seem to find anything suitable to this. Any ideas?
EDIT With Solution
Ended up settling with the SNSEvent.java object located in the aws-lambda-java-events jar from Maven. I used Gson with a DateTime adapter to handle the DateTime Timestamp within that object as well as the UPPER_CAMEL_CASE naming policy since the SNS Event from amazon is camel cased, seems to work fine!
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
#Override
public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return new DateTime(json.getAsString());
}
}).create();
SNSEvent snsEvent = gson.fromJson(event, SNSEvent.class);

How to fix 'The string was not recognized as a valid DateTime.' error in BizTalk Test map?

I'm working on a BizTalk orchestrations with a map which contains an XSLT script. When I launch the orchestration I got this error
Error encountered while executing the transform. Error: Unable to create the transform.
So I go back to the map
XSL transform error:
Unable to write output instance. Exception has been thrown by the target of an invocation. The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.
I already tried to do a ParseExact but got the same error
Here is my code to convert DateTime:
public string FormatDate(string inputDate)
{
System.DateTime date = System.DateTime.Parse(inputDate);
return date.ToString("yyyy-MM-dd");
}
...and the code I tried with ParseExact:
public string FormatDate(string inputDate)
{
System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.InvariantCulture;
System.DateTime date = System.DateTime.ParseExact(inputDate, "yyyyMMdd", culture);
return date.ToString("yyyy-MM-dd");
}
The expected result is to have the date with "yyyy-MM-dd" format.
The problem came from several scripting functoid with the same method name inside. Just gave each methods a different name and the errors has gone.
Thank you to all users for your time.

How to write decoder from date to date in smooks java to java

I am preparing Java object from json using ObjectMapper. Here is the json data
"dateTimeSent" : "LongValue"
Source and target both java classes have field is java.util.Date type.
I tried mapping of like this
<jb:value property="dtSent" data="dateTimeSent" decoder="Date">
<jb:decodeParam name="format">EEE MMM dd HH:mm:ss z yyyy</jb:decodeParam>
</jb:value>
In documentation it is mentioned that this decoder used to encode/decode from String to java.util.Date. Is that i need to write custom decoder for that. If yes please let me know how to write. I am new to smooks.
As smooks encode/decode from String to java.util.Date/java.sql.Date/ java.util.Calendar/java.sql.Time/java.sql.Timestamp. My use case, i have to decode from date to date. So i had created one more variable in source class with setter and getter like - private String modeifiedDateTimeSent
I am mapping with modifiedDateTimeSent variable in smooks-config.xml
<jb:value property="dtSent" data="modeifiedDateTimeSent" decoder="Date">
<jb:decodeParam name="format">yyyy-MM-dd HH:mm:ss</jb:decodeParam>
</jb:value>
Next, I have to set the value in the variable before mapping java class A to Class B.
Date modifiedDtTimeSent = order.getLr().getAdminSection().getDateTimeSent();
String modifiedDtTimeSentString = getDateAsString(modifiedDtTimeSent,"yyyy-MM-dd HH:mm:ss");
object.setModifieddatetimesent(modifiedDtTimeSentString);
Then finally, do your smooks java to java convertion -
Smooks smooks = new Smooks("smooks-config.xml");
ExecutionContext executionContext = smooks.createExecutionContext();
JavaSource source = new JavaSource(object);
JavaResult result = new JavaResult();
smooks.filterSource(executionContext, source, result);
ConvertedClass cc = (IimLocalResponse) result.getBean("xyz");
Hope this will help.

SpringFox Date format to milliseconds

I'm using SpringFox (Swagger) to document my REST APIs. One of the nice features in Swagger is to show example model and the data type format.
Now, I configured my APIs to produce and consume dates in milliseconds but when I'm looking at the sample JSON model the date format is following: "2015-09-21T00:51:32.617Z". See screenshot below. Is it possible to tell SpringFox (Swagger) how to format the date properly?
You could try:
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.directModelSubstitute(YourDateTimeClass.class, Integer.class);
Basically you are telling Swagger to replace the date class with an Integer which could represent the milliseconds.
This is related to the underlying Jackson serialiser. You have to set the correct date format for it otherwise by default it's using timestamps.
Here is an example configuration
#Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {
#Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter jsonMessageConverter = (MappingJackson2HttpMessageConverter) converter;
ObjectMapper objectMapper = jsonMessageConverter.getObjectMapper();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
break;
}
}
}
}
This way it will use an ISO-8601 date-time format. Here is another blog post that explains how to set your own preferred date format: http://yysource.com/2016/06/change-default-date-for-jackson-in-spring-boot-application/
I got a similar problem as yours, and I resolved it by adding the following config in my Spring Boot's application.properties file:
spring.jackson.date-format=com.fasterxml.jackson.databind.util.ISO8601DateFormat

axis2 xsd:date format issue

Merged with axis2 xsd:date format issue.
In WSDL I have the following format:
USER_ACT_STRDT is a date.
When I generate the STUB (using Axis2 1.5.3), the generated stub (ADB Data Binding) has the following source code :
public void setUSER_ACT_STRDT_TypeDef(Date param) {
if (ConverterUtil.convertToString(param).matches("\d{4}-\d{2}-\d{2}")) {
this.localUSER_ACT_STRDT_TypeDef=param; } else { throw new java.lang.RuntimeException();
} }
This method always throws RuntimeException because the ConverterUtil.convertToString() method returns a String in a different format than "yyyy-mm-dd". It returns the date by appending +5.30 as 2011-03-21+05:30.
I tried passing the date in different formats but same result for all.
Can any one suggest how to resolve this issue.