Adapter Development Kit 6.5 | webMethods Adapter Development Kit Documentation | webMethods Adapter Development Kit Installation and User’s Documentation | Adapter Services | Adapter Service Implementation | Code Example 1: WmAdapterService Implementation Class
 
Code Example 1: WmAdapterService Implementation Class
1. package com.mycompany.adapter.myadapter.services;
2.
3. import java.util.Hashtable;
4. import java.util.Locale;
5.
6. import javax.resource.ResourceException;
7.
8. import com.mycompany.adapter.myadapter.MyAdapter;
9. import com.wm.adk.cci.interaction.WmAdapterService;
10. import com.wm.adk.cci.record.WmRecord;
11. import com.wm.adk.cci.record.WmRecordFactory;
12. import com.wm.adk.connection.WmManagedConnection;
13. import com.wm.adk.metadata.WmTemplateDescriptor;
14. import com.wm.data.IData;
15. import com.wm.data.IDataCursor;
16. import com.wm.data.IDataFactory;
17. import com.wm.data.IDataUtil;
18.
19.
20. public class MockDbUpdate extends WmAdapterService
21. {
22.
23. public static final String SIG_IN_PARM = "sigIn";
24. public static final String SIG_OUT_PARM = "sigOut";
25. public static final String FIELD_NAMES_PARM = "fieldNames";
26. public static final String FIELD_TYPES_PARM = "fieldTypes";
27. public static final String TABLE_NAME_PARM = "tableName";
28. public static final String COLUMN_NAMES_PARM = "columnNames";
29. public static final String COLUMN_TYPES_PARM = "columnTypes";
30. public static final String REPEATING_PARM = "repeating";
31. public static final String OVERRIDE_TYPES_PARM = "overrideTypes";
32.
33. public static final String TABLES_RD = "tablesRD";
34. public static final String COLUMN_NAMES_RD = "columnNamesRD";
35. public static final String COLUMN_TYPES_RD = "columnTypesRD";
36. public static final String FIELD_NAMES_RD = "fieldNamesRD";
37. public static final String FIELD_TYPES_RD = "fieldTypesRD";
38. public static final String OVERRIDE_TYPES_RD = "overrideTypesRD";
39.
40. private String[] _fieldNames;
41. private String[] _fieldTypes;
42. private String _tableName;
43. private String[] _columnNames;
44. private String[] _columnTypes;
45. private boolean _repeating;
46. private String[] _overrideTypes;
47.
48.
49. public void setSigIn(String[] val){}
50. public void setSigOut(String[] val){}
51. public void setFieldNames(String[] val){ _fieldNames = val;}
52. public void setFieldTypes(String[] val){ _fieldTypes = val;}
53. public void setTableName(String val){ _tableName = val;}
54. public void setColumnNames(String[] val){ _columnNames = val;}
55. public void setColumnTypes(String[] val){ _columnTypes = val;}
56. public void setRepeating(boolean val){ _repeating = val;}
57. public void setOverrideTypes(String[] val){_overrideTypes = val;}
58.
59. public void fillWmTemplateDescriptor(WmTemplateDescriptor d,Locale l)
60. throws ResourceException
61. {
62. d.createGroup("Mock Settings", new String [] { TABLE_NAME_PARM,
63. REPEATING_PARM,
64. COLUMN_NAMES_PARM,
65. COLUMN_TYPES_PARM,
66. OVERRIDE_TYPES_PARM});
67.
68. d.createGroup("Signature", new String [] {FIELD_NAMES_PARM,
69. FIELD_TYPES_PARM,
70. SIG_IN_PARM,
71. SIG_OUT_PARM});
72.
73. d.createFieldMap(new String[] {COLUMN_NAMES_PARM,
74. COLUMN_TYPES_PARM,
75. OVERRIDE_TYPES_PARM},true);
76.
77. d.createTuple(new String[]{COLUMN_NAMES_PARM,COLUMN_TYPES_PARM});
78.
79. d.createFieldMap(new String [] {FIELD_NAMES_PARM,
80. FIELD_TYPES_PARM,
81. SIG_IN_PARM,
82. SIG_OUT_PARM},false);
83.
84. d.createTuple(new String[]{FIELD_NAMES_PARM,FIELD_TYPES_PARM});
85.
86. d.setResourceDomain(TABLE_NAME_PARM,TABLES_RD,null);
87. d.setResourceDomain(COLUMN_NAMES_PARM,COLUMN_NAMES_RD,
88. new String[]{TABLE_NAME_PARM});
89. d.setResourceDomain(COLUMN_TYPES_PARM,COLUMN_TYPES_RD,
90. new String[]{TABLE_NAME_PARM});
91. d.setResourceDomain(OVERRIDE_TYPES_PARM,OVERRIDE_TYPES_RD,null);
92.
93. String [] fieldTupleDependencies = {TABLE_NAME_PARM,
94. REPEATING_PARM,
95. COLUMN_NAMES_PARM,
96. COLUMN_TYPES_PARM,
97. OVERRIDE_TYPES_PARM};
98.
99. d.setResourceDomain(FIELD_NAMES_PARM,FIELD_NAMES_RD,
100. fieldTupleDependencies);
101. d.setResourceDomain(FIELD_TYPES_PARM,FIELD_TYPES_RD,
102. fieldTupleDependencies);
103.
104. d.setResourceDomain(SIG_IN_PARM,WmTemplateDescriptor.INPUT_FIELD_
NAMES,
105. new String[] {FIELD_NAMES_PARM, FIELD_TYPES_PARM});
106. d.setResourceDomain(SIG_OUT_PARM,WmTemplateDescriptor.OUTPUT_FIEL
D_NAMES,
107. new String[] {FIELD_NAMES_PARM, FIELD_TYPES_PARM});
108.
109. d.setDescriptions(MyAdapter.getInstance().
110. getAdapterResourceBundleManager(),l);
111. }
112. public WmRecord execute(WmManagedConnection connection, WmRecord input)
113. throws ResourceException
114. {
115. Hashtable[] request = this.unpackRequest(input);
116. return this.packResonse(request);
117. }
118.
119. private Hashtable[] unpackRequest(WmRecord request) throws
ResourceException
120. {
121. Hashtable data[] = null;
122. IData mainIData = request.getIData();
123. IDataCursor mainCursor = mainIData.getCursor();
124.
125. try
126. {
127. String tableName = this._tableName;
128. String[] columnNames = this._columnNames;
129.
130. if(mainCursor.first(tableName))
131. {
132. IData[] recordIData;
133. if(this._repeating)
134. {
135. recordIData = IDataUtil.getIDataArray (mainCursor,tableName);
136. data = new Hashtable[recordIData.length];
137. }
138. else
139. {
140. recordIData = new IData[] {IDataUtil.getIData(mainCursor)};
141. data = new Hashtable[1];
142. }
143. for(int rec=0;rec<recordIData.length;rec++)
144. {
145. IDataCursor recordCursor = recordIData[rec].getCursor();
146. data[rec] = new Hashtable();
147. for(int c = 0; c < columnNames.length;c++)
148. {
149. if(recordCursor.first(columnNames[c]))
150. {
151. data[rec].put(tableName + "." + columnNames[c],
152. recordCursor.getValue());
153. }
154. }
155. recordCursor.destroy();
156. }
157. }
158. else
159. {
160. throw MyAdapter.getInstance().createAdapterException(9999,
161. new String[] {"No Request Data"});
162. }
163. }
164. catch (Throwable t)
165. {
166. throw MyAdapter.getInstance().createAdapterException(9999,
167. new String[] {"Error unpacking request data"},t);
168. }
169. finally
170. {
171. mainCursor.destroy();
172. }
173. return data;
174. }
175.
176. private WmRecord packResonse(Hashtable[] response) throws
ResourceException
177. {
178. WmRecord data = null;
179. try
180. {
181. IData[] recordIData = new IData[response.length];
182. String tableName = this._tableName;
183. String[] columnNames = this._columnNames;
184.
185. for(int rec = 0; rec < response.length; rec++)
186. {
187. recordIData[rec] = IDataFactory.create();
188. IDataCursor recordCursor = recordIData[rec].getCursor();
189. for(int col = 0; col < columnNames.length;col++)
190. {
191. IDataUtil.put(recordCursor,columnNames[col],
192. response[rec].get(tableName + "." +
columnNames[col]));
193. }
194. recordCursor.destroy();
195. }
196. IData mainIData = IDataFactory.create();
197. IDataCursor mainCursor = mainIData.getCursor();
198. if(this._repeating)
199. {
200. IDataUtil.put(mainCursor,tableName,recordIData);
201. }
202. else
203. {
204. IDataUtil.put(mainCursor,tableName,recordIData[0]);
205. }
206. mainCursor.destroy();
207. data =
WmRecordFactory.getFactory().createWmRecord("nameNotUsed");
208. data.setIData(mainIData);
209. }
210. catch (Throwable t)
211. {
212. throw MyAdapter.getInstance().createAdapterException(9999,
213. new String[] {"Error packing response data"},t);
214. }
215. return data;
216. }
217. }