QueryServiceTester.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2011
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * QueryServiceTester.java
  10. *
  11. */
  12. import java.rmi.RemoteException;
  13. import com.cognos.developer.schemas.bibus._3.AsynchDetailROLAPDataSourceState;
  14. import com.cognos.developer.schemas.bibus._3.AsynchDetailMessages;
  15. import com.cognos.developer.schemas.bibus._3.AsynchReply;
  16. import com.cognos.developer.schemas.bibus._3.AsynchReplyStatusEnum;
  17. import com.cognos.developer.schemas.bibus._3.AsynchSecondaryRequest;
  18. import com.cognos.developer.schemas.bibus._3.BaseClass;
  19. import com.cognos.developer.schemas.bibus._3.FaultDetail;
  20. import com.cognos.developer.schemas.bibus._3.GenericOptionBoolean;
  21. import com.cognos.developer.schemas.bibus._3.Option;
  22. import com.cognos.developer.schemas.bibus._3.OrderEnum;
  23. import com.cognos.developer.schemas.bibus._3.ParameterValue;
  24. import com.cognos.developer.schemas.bibus._3.PropEnum;
  25. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  26. import com.cognos.developer.schemas.bibus._3.QueryTaskOptionEnum;
  27. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  28. import com.cognos.developer.schemas.bibus._3.Sort;
  29. public class QueryServiceTester {
  30. static String queryTaskOptionEnum_stopROLAPCubesImmediately =
  31. "http://developer.cognos.com/ceba/constants/queryTaskOptionEnum#stopROLAPCubesImmediately";
  32. // get available Cubes according to the specified search path
  33. public BaseClassWrapper[] getCubes(CRNConnect Con,
  34. String searchPath) {
  35. BaseClassWrapper[] cubeList = null;
  36. if ((searchPath == null) || (searchPath.length() == 0)
  37. || (searchPath.compareTo("") == 0)) {
  38. System.out.println("Invalid searchPath"
  39. + System.getProperty("line.separator"));
  40. return null;
  41. }
  42. // Search properties: we need the defaultName and the searchPath.
  43. PropEnum[] properties = { PropEnum.defaultName, PropEnum.searchPath };
  44. // Sort options: ascending sort on the defaultName property.
  45. Sort[] sortBy = { new Sort() };
  46. sortBy[0].setOrder(OrderEnum.ascending);
  47. sortBy[0].setPropName(PropEnum.defaultName);
  48. // Query options; use the defaults.
  49. QueryOptions options = new QueryOptions();
  50. try {
  51. BaseClass[] results = Con.getCMService().query(
  52. new SearchPathMultipleObject(searchPath), properties,
  53. sortBy, options);
  54. if (results != null && results.length > 0) {
  55. cubeList = new BaseClassWrapper[results.length];
  56. for (int i = 0; i < results.length; i++) {
  57. cubeList[i] = new BaseClassWrapper(results[i]);
  58. }
  59. }
  60. } catch (RemoteException remoteEx) {
  61. System.out.println("The request threw an RMI exception:");
  62. System.out.println(remoteEx.getMessage());
  63. System.out.println("Stack trace:");
  64. remoteEx.printStackTrace();
  65. return null;
  66. }
  67. return cubeList;
  68. }
  69. // Retrieve the current state for one cube
  70. public String getCubeState(CRNConnect Con, String cubeName) {
  71. String cubeStateRequestResult = null;
  72. String[] cubeNames = {cubeName};
  73. try {
  74. ParameterValue[] parameters = new ParameterValue[0];
  75. Option[] options = new Option[0];
  76. AsynchReply getStateResult = Con.getQueryService().getCubeState(cubeNames, parameters, options);
  77. // If response is not immediately complete, call wait until complete
  78. if (!getStateResult.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  79. {
  80. while (!getStateResult.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  81. {
  82. //before calling wait, double check that it is okay
  83. if (hasSecondaryRequest(getStateResult, "wait"))
  84. {
  85. getStateResult =
  86. Con.getQueryService().wait(
  87. getStateResult.getPrimaryRequest(),
  88. new ParameterValue[] {},
  89. new Option[] {});
  90. }
  91. else
  92. {
  93. return "Error: Wait method not available as expected.";
  94. }
  95. }
  96. }
  97. if (getStateResult != null) {
  98. cubeStateRequestResult = "Query returned no state information for the requested cube.";
  99. for (int i = 0; i < getStateResult.getDetails().length; i++)
  100. {
  101. if ((getStateResult.getDetails()[i] instanceof AsynchDetailROLAPDataSourceState))
  102. {
  103. cubeStateRequestResult =((AsynchDetailROLAPDataSourceState)getStateResult.getDetails()[i]).getState();
  104. }
  105. }
  106. }
  107. } catch (RemoteException re) {
  108. System.out
  109. .println("Error getting cube state: "
  110. + re.getMessage());
  111. cubeStateRequestResult = "Error running getCubeState.";
  112. }
  113. return cubeStateRequestResult;
  114. }
  115. // Start a cube
  116. public String startSingleCube(CRNConnect Con, String cubeName) {
  117. String requestResult = null;
  118. String[] cubeNames = {cubeName};
  119. try {
  120. ParameterValue[] parameters = new ParameterValue[0];
  121. Option[] options = new Option[0];
  122. AsynchReply result = Con.getQueryService().startCubes(cubeNames, parameters, options);
  123. // If response is not immediately complete, call wait until complete
  124. if (!result.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  125. {
  126. while (!result.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  127. {
  128. //before calling wait, double check that it is okay
  129. if (hasSecondaryRequest(result, "wait"))
  130. {
  131. result =
  132. Con.getQueryService().wait(
  133. result.getPrimaryRequest(),
  134. new ParameterValue[] {},
  135. new Option[] {});
  136. }
  137. else
  138. {
  139. return "Error: Wait method not available as expected.";
  140. }
  141. }
  142. }
  143. if (result != null) {
  144. requestResult = "No messages in response to stop request.";
  145. for (int i = 0; i < result.getDetails().length; i++)
  146. {
  147. if ((result.getDetails()[i] instanceof AsynchDetailMessages))
  148. {
  149. FaultDetail[] faultMessages = ((AsynchDetailMessages)result.getDetails()[i]).getMessages();
  150. requestResult = "Response from server: ";
  151. for (int j = 0; j < faultMessages.length; j++)
  152. {
  153. requestResult = requestResult + "\n\n" +faultMessages[j].getMessage()[0].getMessage();
  154. }
  155. }
  156. }
  157. }
  158. } catch (RemoteException re) {
  159. System.out
  160. .println("Error stopping cube: "
  161. + re.getMessage());
  162. requestResult = "Error running stopCubes.";
  163. }
  164. return requestResult;
  165. }
  166. // stop a cube - if forceStop true, stop immediately, otherwise wait until cube is not in use
  167. public String stopSingleCube(CRNConnect Con, String cubeName, boolean forceStop) {
  168. String cubeStopRequestResult = null;
  169. String[] cubeNames = {cubeName};
  170. ParameterValue[] parameters = new ParameterValue[0];
  171. Option[] options = new Option[1];
  172. GenericOptionBoolean immediateStop = new GenericOptionBoolean();
  173. immediateStop.setName(queryTaskOptionEnum_stopROLAPCubesImmediately);
  174. immediateStop.setValue(forceStop);
  175. options[0] = immediateStop;
  176. try {
  177. AsynchReply result = Con.getQueryService().stopCubes(cubeNames, parameters, options);
  178. // If response is not immediately complete, call wait until complete
  179. if (!result.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  180. {
  181. while (!result.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  182. {
  183. //before calling wait, double check that it is okay
  184. if (hasSecondaryRequest(result, "wait"))
  185. {
  186. result =
  187. Con.getQueryService().wait(
  188. result.getPrimaryRequest(),
  189. new ParameterValue[] {},
  190. new Option[] {});
  191. }
  192. else
  193. {
  194. return "Error: Wait method not available as expected.";
  195. }
  196. }
  197. }
  198. if (result != null) {
  199. cubeStopRequestResult = "No messages in response to stop request.";
  200. for (int i = 0; i < result.getDetails().length; i++)
  201. {
  202. if ((result.getDetails()[i] instanceof AsynchDetailMessages))
  203. {
  204. FaultDetail[] faultMessages = ((AsynchDetailMessages)result.getDetails()[i]).getMessages();
  205. cubeStopRequestResult = "Response from server: ";
  206. for (int j = 0; j < faultMessages.length; j++)
  207. {
  208. cubeStopRequestResult = cubeStopRequestResult + "\n\n" +faultMessages[j].getMessage()[0].getMessage();
  209. }
  210. }
  211. }
  212. }
  213. } catch (RemoteException re) {
  214. System.out
  215. .println("Error stopping cube: "
  216. + re.getMessage());
  217. cubeStopRequestResult = "Error running stopCubes.";
  218. }
  219. //return "Stop Cube Request";
  220. return cubeStopRequestResult;
  221. }
  222. // send request to restart cube
  223. public String restartSingleCube(CRNConnect Con, String cubeName) {
  224. String requestResult = null;
  225. String[] cubeNames = {cubeName};
  226. try {
  227. ParameterValue[] parameters = new ParameterValue[0];
  228. Option[] options = new Option[0];
  229. AsynchReply result = Con.getQueryService().restartCubes(cubeNames, parameters, options);
  230. // If response is not immediately complete, call wait until complete
  231. if (!result.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  232. {
  233. while (!result.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  234. {
  235. //before calling wait, double check that it is okay
  236. if (hasSecondaryRequest(result, "wait"))
  237. {
  238. result =
  239. Con.getQueryService().wait(
  240. result.getPrimaryRequest(),
  241. new ParameterValue[] {},
  242. new Option[] {});
  243. }
  244. else
  245. {
  246. return "Error: Wait method not available as expected.";
  247. }
  248. }
  249. }
  250. if (result != null) {
  251. requestResult = "No messages in response to restart request.";
  252. for (int i = 0; i < result.getDetails().length; i++)
  253. {
  254. if ((result.getDetails()[i] instanceof AsynchDetailMessages))
  255. {
  256. FaultDetail[] faultMessages = ((AsynchDetailMessages)result.getDetails()[i]).getMessages();
  257. requestResult = "Response from server: ";
  258. for (int j = 0; j < faultMessages.length; j++)
  259. {
  260. requestResult = requestResult + "\n\n" +faultMessages[j].getMessage()[0].getMessage();
  261. }
  262. }
  263. }
  264. }
  265. } catch (RemoteException re) {
  266. System.out
  267. .println("Error restarting cube: "
  268. + re.getMessage());
  269. requestResult = "Error running restartCubes.";
  270. }
  271. return requestResult;
  272. }
  273. // request refresh of the data cache for a specific cube
  274. public String refreshDataCache(CRNConnect Con, String cubeName) {
  275. String requestResult = null;
  276. String[] cubeNames = {cubeName};
  277. try {
  278. ParameterValue[] parameters = new ParameterValue[0];
  279. Option[] options = new Option[0];
  280. AsynchReply result = Con.getQueryService().refreshCubeDataCache(cubeNames, parameters, options);
  281. // If response is not immediately complete, call wait until complete
  282. if (!result.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  283. {
  284. while (!result.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  285. {
  286. //before calling wait, double check that it is okay
  287. if (hasSecondaryRequest(result, "wait"))
  288. {
  289. result =
  290. Con.getQueryService().wait(
  291. result.getPrimaryRequest(),
  292. new ParameterValue[] {},
  293. new Option[] {});
  294. }
  295. else
  296. {
  297. return "Error: Wait method not available as expected.";
  298. }
  299. }
  300. }
  301. if (result != null) {
  302. requestResult = "No messages in response to refresh data cache request.";
  303. for (int i = 0; i < result.getDetails().length; i++)
  304. {
  305. if ((result.getDetails()[i] instanceof AsynchDetailMessages))
  306. {
  307. FaultDetail[] faultMessages = ((AsynchDetailMessages)result.getDetails()[i]).getMessages();
  308. requestResult = "Response from server: ";
  309. for (int j = 0; j < faultMessages.length; j++)
  310. {
  311. requestResult = requestResult + "\n\n" +faultMessages[j].getMessage()[0].getMessage();
  312. }
  313. }
  314. }
  315. }
  316. } catch (RemoteException re) {
  317. System.out
  318. .println("Error refreshing data cache: "
  319. + re.getMessage());
  320. requestResult = "Error running refreshCubeDataCache.";
  321. }
  322. return requestResult;
  323. }
  324. // request refresh of the member cache for a specific cube
  325. public String refreshMemberCache(CRNConnect Con, String cubeName) {
  326. String requestResult = null;
  327. String[] cubeNames = {cubeName};
  328. try {
  329. ParameterValue[] parameters = new ParameterValue[0];
  330. Option[] options = new Option[0];
  331. AsynchReply result = Con.getQueryService().refreshCubeMemberCache(cubeNames, parameters, options);
  332. // If response is not immediately complete, call wait until complete
  333. if (!result.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  334. {
  335. while (!result.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  336. {
  337. //before calling wait, double check that it is okay
  338. if (hasSecondaryRequest(result, "wait"))
  339. {
  340. result =
  341. Con.getQueryService().wait(
  342. result.getPrimaryRequest(),
  343. new ParameterValue[] {},
  344. new Option[] {});
  345. }
  346. else
  347. {
  348. return "Error: Wait method not available as expected.";
  349. }
  350. }
  351. }
  352. if (result != null) {
  353. requestResult = "No messages in response to refresh member cache request.";
  354. for (int i = 0; i < result.getDetails().length; i++)
  355. {
  356. if ((result.getDetails()[i] instanceof AsynchDetailMessages))
  357. {
  358. FaultDetail[] faultMessages = ((AsynchDetailMessages)result.getDetails()[i]).getMessages();
  359. requestResult = "Response from server: ";
  360. for (int j = 0; j < faultMessages.length; j++)
  361. {
  362. requestResult = requestResult + "\n\n" +faultMessages[j].getMessage()[0].getMessage();
  363. }
  364. }
  365. }
  366. }
  367. } catch (RemoteException re) {
  368. System.out
  369. .println("Error refreshing cube member cache: "
  370. + re.getMessage());
  371. requestResult = "Error running refreshCubeMemberCache.";
  372. }
  373. return requestResult;
  374. }
  375. // request refresh of security for a specific cube
  376. public String refreshCubeSecurity(CRNConnect Con, String cubeName) {
  377. String requestResult = null;
  378. String[] cubeNames = {cubeName};
  379. try {
  380. ParameterValue[] parameters = new ParameterValue[0];
  381. Option[] options = new Option[0];
  382. AsynchReply result = Con.getQueryService().refreshCubeSecurity(cubeNames, parameters, options);
  383. // If response is not immediately complete, call wait until complete
  384. if (!result.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  385. {
  386. while (!result.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  387. {
  388. //before calling wait, double check that it is okay
  389. if (hasSecondaryRequest(result, "wait"))
  390. {
  391. result =
  392. Con.getQueryService().wait(
  393. result.getPrimaryRequest(),
  394. new ParameterValue[] {},
  395. new Option[] {});
  396. }
  397. else
  398. {
  399. return "Error: Wait method not available as expected.";
  400. }
  401. }
  402. }
  403. if (result != null) {
  404. requestResult = "No messages in response to security refresh request.";
  405. for (int i = 0; i < result.getDetails().length; i++)
  406. {
  407. if ((result.getDetails()[i] instanceof AsynchDetailMessages))
  408. {
  409. FaultDetail[] faultMessages = ((AsynchDetailMessages)result.getDetails()[i]).getMessages();
  410. requestResult = "Response from server: ";
  411. for (int j = 0; j < faultMessages.length; j++)
  412. {
  413. requestResult = requestResult + "\n\n" +faultMessages[j].getMessage()[0].getMessage();
  414. }
  415. }
  416. }
  417. }
  418. } catch (RemoteException re) {
  419. System.out
  420. .println("Error refreshing cube security: "
  421. + re.getMessage());
  422. requestResult = "Error running refreshCubeSecurity.";
  423. }
  424. return requestResult;
  425. }
  426. public boolean hasSecondaryRequest(
  427. AsynchReply response,
  428. String secondaryRequest)
  429. {
  430. AsynchSecondaryRequest[] secondaryRequests =
  431. response.getSecondaryRequests();
  432. for (int i = 0; i < secondaryRequests.length; i++)
  433. {
  434. if (secondaryRequests[i].getName().compareTo(secondaryRequest)
  435. == 0)
  436. {
  437. return true;
  438. }
  439. }
  440. return false;
  441. }
  442. }