CLOB and int-jdbc:stored-proc-outbound-gateway - spring-integration

I'm trying to invoke a stored procedure using the following component:
int-jdbc:stored-proc-outbound-gateway
It works fine, it calls the procedure but I can't seem to be able to log the CLOB in a proper CLOB format.
I've tried the following xml:
<int-jdbc:stored-proc-outbound-gateway
id="auditGatewayProcedure" request-channel="auditGatewayInbound"
data-source="dataSource" stored-procedure-name="PKG_TEMP.PR_AUDIT"
return-value-required="false" ignore-column-meta-data="true">
<int-jdbc:sql-parameter-definition
name="IN_MSG_ID" />
<int-jdbc:sql-parameter-definition
name="IN_GUID" />
<int-jdbc:sql-parameter-definition
name="IN_CLOB" />
<int-jdbc:parameter name="IN_MSG_ID" expression="payload.msgId" />
<int-jdbc:parameter name="IN_CLOB" expression="payload.xmlPayload" />
<int-jdbc:parameter name="IN_GUID" expression="payload.guid" />
</int-jdbc:stored-proc-outbound-gateway>
With that in mind, I'm passing the following payload (with the proper getters and setters):
private long id;
private String msgId;
private Clob xmlPayload;
private String guid;
The CLOB is of type: java.sql.Clob
The procedure I'm calling is this simple stuff:
procedure PR_BRIDGE_AUDIT(in_msg_id IN varchar2,
in_guid IN varchar2,
in_clob IN Clob) is begin
insert into tb_temp_all_messages(id,msg_id,xml_payload,guid) values (TB_TEMP_ALL_MESSAGES_SEQ.NEXTVAL,in_msg_id, in_clob, in_guid);
end;
What happens is that the clob column is inserted with the following value:
org.hibernate.lob.SerializableClob#186fdd6
The second try I did was forcing the type in the spring component, like this:
<int-jdbc:sql-parameter-definition
name="IN_CLOB" type="CLOB"/>
But the following exception is thrown: java.lang.ClassCastException: org.hibernate.lob.SerializableClob cannot be cast to oracle.sql.CLOB
Am I doing something wrong?
I tried to google it but nothing of value (as far as I saw) appeared.
Thanks in advance!
UPDATE
So, after some tries and the answer from Artem, the solutions are two, it seems:
given this XML:
<int-jdbc:stored-proc-outbound-gateway
id="auditGatewayProcedure" request-channel="auditGatewayInbound"
data-source="dataSource" stored-procedure-name="PKG_TGT_BRIDGE.PR_BRIDGE_AUDIT"
return-value-required="false" ignore-column-meta-data="true">
<int-jdbc:sql-parameter-definition
name="IN_MSG_ID" />
<int-jdbc:sql-parameter-definition
name="IN_GUID" />
<int-jdbc:sql-parameter-definition
name="IN_CLOB" type="CLOB"/>
<int-jdbc:parameter name="IN_MSG_ID" expression="payload.msgId" />
<int-jdbc:parameter name="IN_CLOB" expression="payload.stringClob" />
<int-jdbc:parameter name="IN_GUID" expression="payload.guid" />
</int-jdbc:stored-proc-outbound-gateway>
In the EEM class you can either pass a string containing the Clob (stringClob).
Or create a properly oracle.sql.CLOB type with what has been said by Artem and pass into the IN_CLOB parameter the oracle.sql.CLOB created.
Hope this helped,
Thanks

The issue that you really must create oracle.sql.CLOB yourself and within the same transaction as for that <int-jdbc:stored-proc-outbound-gateway>.
The code to create a CLOB may look like:
public CLOB convertToClob(String value) {
CLOB c = CLOB.createTemporary(getNativeConnection(), false, CLOB.DURATION_SESSION);
c.setString(1L, value);
return c;
}
private Connection getNativeConnection() {
return DataSourceUtils.getConnection(this.dataSource).getMetaData().getConnection();
}

Related

Spring Integration - Scheduling Job from configuration file

I'm using Spring Integration to parse XML file and i will need to create a thread (and each one have a different rate) for each tag.
Right now (with the help of many users here :)) i'm able to split XML by tag and then route it to the appropiate service-activator.
This works great but i'm not able to redirect to a channel that create "a thread" and then execute the operations. Right now i have the following configuration and in my mind (that i dont know if it is correct...)
Split tag -> Route to the appropiate channel -> Start a thread(from tag configuration) -> Execute the operation
This is my actual configuration that split tag and redirect to the channel.
The router should redirect not toward a channel directly, but schedule them.
In first instance will be enought to redirect it in a pool with fixed rate and later i will use XPATH to get the attribute and then replace this "fixed" rate with the correct value.
I've tried many solutions to create this flow but each one fails or do not compile :(
<context:component-scan base-package="it.mypkg" />
<si:channel id="rootChannel" />
<si-xml:xpath-splitter id="mySplitter" input-channel="rootChannel" output-channel="routerChannel" create-documents="true">
<si-xml:xpath-expression expression="//service" />
</si-xml:xpath-splitter>
<si-xml:xpath-router id="router" input-channel="routerChannel" evaluate-as-string="true">
<si-xml:xpath-expression expression="concat(name(./node()), 'Channel')" />
</si-xml:xpath-router>
<si:service-activator input-channel="serviceChannel" output-channel="endChannel">
<bean class="it.mypkg.Service" />
</si:service-activator>
UPDATE:
Using this configuration for the service this should run a task every 10 seconds (the id=service1) and every 5 seconds the other (the id=service2). In the same way i can have another tag that is handle by another class (because this will have another behaviour)
<root>
<service id="service1" interval="10000" />
<service id="service2" interval="5000" />
<activity id="activity1" interval="50000" />
<root>
I will have a classe (Service) that is general to handle Service tag and this complete some operation and then "return me" the value so i can redirect to another channel.
public class Service {
public int execute() {
// Execute the task and return the value to continue the "chain"
}
}
It's not at all clear what you mean; you split a tag; route it but want to "schedule" it at a rate in the XML. It's not clear what you mean by "schedule" here - normally each message is processed once not multiple times on a schedule.
As I said, I don't understand what you need to do, but a smart poller might be suitable.
Another possibility is the delayer where the amount of the delay can be derived from the message.
EDIT
Since your "services" don't seem to take any input data, it looks like you simply need to configure/start an <inbound-channel-adapter/> for each service, and then start it, based on the arguments in the XML.
<int:inbound-channel-adapter id="service1" channel="foo"
auto-startup="false"
ref="service1Bean" method="execute">
<poller fixed-delay="1000" />
</int:inbound-channel-adapter/>
Note auto-startup="false".
Now, in the code that receives the split
#Autowired
SourcePollingChannelAdapter service1;
...
public void startService1(Node node) {
...
service1.setTrigger(new PeridicTrigger(...));
service1.start();
...
}
I dont know if this is the right way to implement the flow, but i've write the follow code:
applicationContext.xml
<context:component-scan base-package="it.mypkg" />
<!-- Expression to extract interval from XML tag -->
<si-xml:xpath-expression id="selectIntervalXpath" expression="//*/#interval" />
<si:channel id="rootChannel" />
<!-- Split each tag to redirect on router -->
<si-xml:xpath-splitter id="mySplitter" input-channel="rootChannel" output-channel="routerChannel" create-documents="true">
<si-xml:xpath-expression expression="//service|//activity" />
</si-xml:xpath-splitter>
<!-- Route each tag to the appropiate channel -->
<si-xml:xpath-router id="router" input-channel="routerChannel" evaluate-as-string="true">
<si-xml:xpath-expression expression="concat(name(./node()), 'Channel')" />
</si-xml:xpath-router>
<!-- Activator for Service Tag -->
<si:service-activator input-channel="serviceChannel" method="schedule">
<bean class="it.mypkg.Service" />
</si:service-activator>
<!-- Activator for Activity Tag -->
<si:service-activator input-channel="activityChannel" method="schedule">
<bean class="it.mypkg.Activity" />
</si:service-activator>
<!-- Task scheduler -->
<task:scheduler id="taskScheduler" pool-size="10"/>
Each tag will extend an Operation class (to avoid code duplication on bean injection)
Operation.java
public abstract class Operation {
protected TaskScheduler taskScheduler;
protected XPathExpression selectIntervalXpath;
abstract public void schedule(Node document);
#Autowired
public void setTaskScheduler(TaskScheduler taskScheduler) {
this.taskScheduler= taskScheduler;
}
public TaskScheduler getTaskScheduler() {
return this.taskScheduler;
}
#Autowired
public void setSelectIntervalXpath(XPathExpression selectIntervalXpath) {
this.selectIntervalXpath = selectIntervalXpath;
}
public XPathExpression getSelectIntervalXPath() {
return this.selectIntervalXpath;
}
}
And an example of Service class (that handle all tags service provided on .xml)
public class Service extends Operation {
private static final Logger log = Logger.getLogger(Service.class);
#Override
public void schedule(Node document) {
log.debug("Scheduling Service");
long interval = Long.parseLong(this.selectIntervalXpath.evaluateAsString(document));
this.taskScheduler.scheduleAtFixedRate(new ServiceRunner(), interval);
}
private class ServiceRunner implements Runnable {
public void run() {
log.debug("Running...");
}
}
}
Now to continue my flow i will need to find a way to redirect the output of each job to Spring Integration (applicationContext.xml).

The given key is not present in the dictionary

Hi I am trying to fetch the accounts from CRM 2011. I am fetching the data in the EntityCollection . But when I am trying to read or access data from entityCollection it displayed first record but throwing an error after that record. Kindly have a look to below code and suggest me.
string fetch2 = #"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='account'>
<attribute name='name' />
<attribute name='address1_city' />
<attribute name='primarycontactid' />
<attribute name='telephone1' />
<attribute name='accountid' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='accounttype' operator='eq' value='01' />
</filter>
</entity>
</fetch>";
try
{
EntityCollection fxResult = _service.RetrieveMultiple(new FetchExpression(fetch2));
foreach (var e in fxResult.Entities)
{
Console.WriteLine("Id:{0},Name:{1},City:{2}", e.Attributes ["accountid"].ToString(), e.Attributes["name"].ToString(), e.Attributes["address1_city"].ToString());
// Console.WriteLine("Id:{0},Name:{1},City:{2}", e.ToEntity["accountid"]);
}
}
catch (Exception e)
{
Console.WriteLine("Error:==" + e.Message);
}
Before access an attribute you need to ask if it is in the context:
e.Attributes.Contains("address1_city")
If the collection contains the attribute, then you can access it safe.
string accountid = (string)e.Attributes["address1_city"]
The reason the attribute doesn't come in the collection it's because it is null or you are not retrieving it. In this case maybe, one of your attributes is null. Maybe address1_city.
When retrieving attribute values of late-bound Entity objects, the recommended approach is to use method getAttributeValue<T>. When the attribute is not present in the entity's attribute collection, it returns default(T).
The primary key ('id') of the record is always present when it is returned by the OrganizationService.
So your code should look like this:
EntityCollection fxResult = _service.RetrieveMultiple(new FetchExpression(fetch2));
foreach (var e in fxResult.Entities)
{
Console.WriteLine(
"Id:{0},Name:{1},City:{2}",
e.Id,
e.GetAttributeValue<string>("name"),
e.GetAttributeValue<string>("address1_city"));
}
You can safely use the item selector when you need to assign a value to an attribute, regardless if it is already present or not.
E.g. the following code line is valid:
e["name"] = "Demo Accountname";

Incorrect / redundant where clause

I'm not the best a Linq and I have written the below query and it just seems my where clause is redundant and there has to be a better way?
Given the following XML structure:
<Views>
<Fulfillment>
<SecurityRoleName>ABCD</SecurityRoleName>
<SecurityRoleViews>
<RoleView name="A" />
<RoleView name="B" />
<RoleView name="C" />
<RoleView name="D" />
<RoleView name="E" />
<RoleView name="F" />
</SecurityRoleViews>
<PublicRoleViews>
<RoleView name="Z" />
<RoleView name="Y" />
<RoleView name="X" />
<RoleView name="W" />
<RoleView name="V" />
<RoleView name="U" />
</PublicRoleViews>
</Fulfillment>
</Views>
I wrote the following to get a single value (FulfillmentRoleName) and two List (SecuredViews, PublicViews) objects.
FulfillmentRoleName = configParms.Descendants("Fulfillment")
.Where(node => (string)node.Element("SecurityRoleName") == "SecurityRoleName")
.Select(node => node.Value.ToString())
.First();
SecuredViews = configParms.Descendants("SecurityRoleViews")
.Where(node => (string)node.Element("RoleView") == "RoleView")
.Select(node => node.Attribute("name").Value.ToString())
.ToList();
PublicViews = configParms.Descendants("PublicRoleViews")
.Where(node => (string)node.Element("RoleView") == "RoleView")
.Select(node => node.Attribute("name").Value.ToString())
.ToList();
I would want the following values:
FulfillmentRoleName = ABCD
SecuredViews = List of names A,B,C...
PublicViews = List of names Z,Y,X....
It is the where clause that I am unsure of:
.Where(node => (string)node.Element("RoleView") == "RoleView")
Seems there has to be a more elegant way to locate that node?
Thank you for taking the time to help
As per Ahmad Mageed recommendation I am using the more elegant Element("") approach. However I am getting an instantiation error...as if the element collection is not built yet??
However if I use configParms.Root.Value I get my SecurityRoleName value???
I would have thought that views would be the root....or is it the first node that has a value???
You can use the Elements method and provide the name to match. This would allow you to replace the where query with Elements("RoleView").
Some other observations:
You can simplify the first query by grabbing the "Fulfillment" element directly by using the Element method, instead of Descendants.
The Value property returns a string. The ToString() calls are redundant.
Here's an updated version of your queries:
// if configParms is an XDocument use configParms.Root
var securityRoleName = configParms.Element("Fulfillment")
.Element("SecurityRoleName").Value;
var securedViews = configParms.Descendants("SecurityRoleViews")
.Elements("RoleView")
.Select(node => node.Attribute("name").Value)
.ToList();
var publicViews = configParms.Descendants("PublicRoleViews")
.Elements("RoleView")
.Select(node => node.Attribute("name").Value)
.ToList();

How to call facade from the command

In IBM WebSphere Commerce How the facade can be called from the command Instead of calling it from jsp. we call it from jsp like
<wcf:getData type="com.ibm.commerce.catalog.facade.datatypes.CatalogNavigationViewType" var="catalogNavigationView"
expressionBuilder="${navigationView}" varShowVerb="showCatalogNavigationView"
maxItems="${pageSize}" recordSetStartNumber="${WCParam.beginIndex}">
<wcf:param name="searchProfile" value="${searchProfile}" />
<wcf:param name="searchTerm" value="${newSearchTerm}" />
<wcf:param name="intentSearchTerm" value="${intentSearchTerm}" />
<wcf:param name="searchType" value="${searchType}" />
<wcf:param name="searchSource" value="${WCParam.searchSource}" />
<wcf:param name="metaData" value="${WCParam.metaData}" />
<wcf:param name="orderBy" value="${WCParam.orderBy}" />
<c:forEach var="facetValue" items="${param.facet}">
<c:if test="${fn:contains(facetValue , '|')}">
<c:set var="facetValue" value="${fn:replace(facetValue,'|',',')}"/>
</c:if>
<wcf:param name="facet" value="${facetValue}" />
</c:forEach>
<wcf:param name="advancedFacetList" value="${newAdvancedFacetList}"/>
<wcf:param name="categoryId" value="${currentCategoryId}" />
<wcf:param name="filterTerm" value="${newFilterTerm}" />
<wcf:param name="filterType" value="${WCParam.filterType}" />
<wcf:param name="filterFacet" value="${removeFacet}" />
<wcf:param name="manufacturer" value="${newManufacturer}" />
<wcf:param name="minPrice" value="${WCParam.minPrice}" />
<wcf:param name="maxPrice" value="${WCParam.maxPrice}" />
<wcf:contextData name="storeId" data="${WCParam.storeId}" />
<wcf:contextData name="catalogId" data="${WCParam.catalogId}" />
</wcf:getData>
This is how we can call it from jsp but I want to call this facade from the command (from the java code)
Please give me any suggestion
Thanks
Ankit
You can do it like this :
public someMethod(){
....
CatalogFacadeClient catalogFacadeClient = new CatalogFacadeClient(getBusinessContextType(), null);
GetType getVerb = CatalogFacadeClient.createGetVerb("_wcf:XPath", getXPathExpressionString());
ShowCatalogNavigationViewDataAreaType showDataArea = catalogFacadeClient.getCatalogNavigationView(getVerb);
response = showDataArea.getCatalogNavigationView();
....
}
public String getXPathExpressionString() {
StringBuffer expression = new StringBuffer();
expression.append("{_wcf.ap='IBM_Store_CatalogEntrySearch';");
expression.append("_wcf.search.term='" + toto+ "';");
expression.append("_wcf.search.spellcheck='false';");
expression.append("_wcf.search.type='1000';");
expression.append("_wcf.search.source='Q'}");
expression.append("/CatalogNavigationView");
return expression.toString();
}
protected BusinessContextType getBusinessContextType() {
BusinessContextType businessContext = CommerceFoundationFactory.eINSTANCE.createBusinessContextType();
ContextDataType storeId = CommerceFoundationFactory.eINSTANCE.createContextDataType();
storeId.setName("storeId");
storeId.setValue(currentStoreId);
ContextDataType catalogId = CommerceFoundationFactory.eINSTANCE.createContextDataType();
catalogId.setName("catalogId");
catalogId.setValue(currentCatalogId);
ContextDataType langIdContext = CommerceFoundationFactory.eINSTANCE.createContextDataType();
langIdContext.setName("langId");
langIdContext.setValue(langId.toString());
businessContext.getContextData().add(storeId);
businessContext.getContextData().add(catalogId);
businessContext.getContextData().add(langIdContext);
return businessContext;
}
Your code works!! Thank you so much!
Some Remarks about getXPathExpressionString() method:
1.- In order to prepare the expression builder correctly please check get-data-config.xml and get the expression builder by name. Name is the expressionBuilder param in wcf:getData
expressionBuilder="${navigationView}"
<wcf:getData type="com.ibm.commerce.catalog.facade.datatypes.CatalogNavigationViewType" var="catalogNavigationView"
expressionBuilder="${navigationView}" varShowVerb="showCatalogNavigationView"
maxItems="${pageSize}" recordSetStartNumber="${WCParam.beginIndex}">
Value of ${navigationView} is = "getCatalogNavigationView"
Search this value in get-data-config.xml file and look like this:
<expression-builder>
<name>getCatalogNavigationView</name>
<data-type-name>CatalogNavigationView</data-type-name>
<expression-template>{_wcf.ap='$accessProfile$';_wcf.search.profile='$searchProfile$';_wcf.search.facet.field.limit='$facetLimit$';_wcf.search.term='$searchTerm$';_wcf.search.intent.term='$intentSearchTerm$';_wcf.search.originalterm='$originalSearchTerm$';_wcf.search.category='$categoryId$';_wcf.search.type='$searchType$';_wcf.search.exclude.term='$filterTerm$';_wcf.search.exclude.type='$filterType$';_wcf.search.manufacturer='$manufacturer$';_wcf.search.price.minimum='$minPrice$';_wcf.search.price.maximum='$maxPrice$';_wcf.search.facet='$facet$';_wcf.search.advanced.facet='$advancedFacetList$';_wcf.search.exclude.facet='$filterFacet$';_wcf.search.sort='$orderBy$';_wcf.search.meta='$metaData$';_wcf.search.source='$searchSource$';_wcf.search.store='$physicalStoreIds$'}/CatalogNavigationView</expression-template>
<param>
<name>accessProfile</name>
<value>IBM_Store_CatalogEntrySearch</value>
</param>
<param>
<name>searchType</name>
<value>0</value>
</param>
<param>
<name>searchSource</name>
<value>O</value>
</param>
<param>
<name>searchProfile</name>
<value>IBM_findCatalogEntryByNameAndShortDescription</value>
</param>
</expression-builder>
source: http://158.85.49.234/WEB-INF/config/com.ibm.commerce.catalog-fep/get-data-config.xml
So this tag : <expression-template> contains the treasure :), i.e the solr query expression.
2.- With this expression-template we could can create a correct expression:
public String getXPathExpressionString() throws JspException, AbstractBusinessObjectDocumentException {
String expressionTemplate = "{_wcf.ap='$accessProfile$';_wcf.search.profile='$searchProfile$';_wcf.search.category='$categoryId$';_wcf.search.type='$searchType$';_wcf.search.sort='$orderBy$';_wcf.search.source='$searchSource$';_wcf.search.facet='$facet$';_wcf.search.exclude.facet='$filterFacet$';_wcf.search.meta='$metaData$';_wcf.search.price.minimum='$minPrice$';_wcf.search.price.maximum='$maxPrice$'}/CatalogNavigationView";
ExpressionBuilderConfig expressionBuilderConfig = new ExpressionBuilderConfig();
expressionBuilderConfig.setName("getCatalogNavigationView");
expressionBuilderConfig.setDataTypeName("CatalogNavigationView");
expressionBuilderConfig.setExpressionLanguage("_wcf:XPath");
expressionBuilderConfig.setExpressionTemplate(expressionTemplate);
HashMap<String,String[]> parameters = new HashMap<String, String[]>();
parameters.put("accessProfile", new String[]{"IBM_Store_CatalogEntrySearch"});
parameters.put("searchProfile", new String[]{"IBM_findCatalogEntryByNameAndShortDescription"});
parameters.put("searchType", new String[]{"xyz"});
parameters.put("categoryId",new String[]{ "123"});
parameters.put("minPrice",new String[]{"0"});
ExpressionType expressionType = expressionBuilderConfig.buildExpression(parameters);
return expressionType.getValue();
}
-------------------------------*-------------------------------------
This can be used to convert websphere ecommerce in a back-end or service provider. With that we can stop using jsp, scriplets, dojo 1.5 and use whatever in front-end layer like current js frameworks.

Display custom conversion error message with <rich:beanValidator />

I'm using hibernate validators with JSF.
How can I set my conversion error messages? I am using them in the following way, but it didn't work:
<h:inputText id="input" value="#{myBean.number}" converterMessage="#{msgs.convertError}">
<f:convertNumber />
<rich:beanValidator />
<rich:ajaxValidator event="onblur"/>
</h:inputText>
I can set custom validator messages in my bean like this:
#NotNull
#Min(value = 1, message="{greateThanOne}")
public long getNumber() {
return number;
}
But what to do with conversion errors?
You may want to look into Faces resource file (jsf-api.jar, javax/faces/Messages.properties).
Just define the following keys in your own resource file and configure it in faces-config.xml.
javax.faces.converter.NumberConverter.NUMBER={2}: ''{0}'' is not a number.
javax.faces.converter.NumberConverter.NUMBER_detail={2}: ''{0}'' is not a number. Example: {1}

Resources