CSHandlers.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2008
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * CSHandlers.java
  10. *
  11. * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  12. * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. *
  14. * Description: This code sample demonstrates how to add, update, query
  15. * and delete objects and properties in the content store
  16. * using the following methods:
  17. * - add (parentPath, objects, options)
  18. * Use this method to add objects, such as reports, to the content store.
  19. * - delete (objects, options)
  20. * Use this method to delete objects from the content store.
  21. * - update (objects)
  22. * Use this method to modify existing objects in the content store.
  23. * - move (objects, target, options)
  24. * Use this implementation of the method to move objects within the content store.
  25. * - copy (objects, target, options)
  26. * Use this implementation of the method to copy objects within the content store.
  27. * - query (searchPath, properties, sortBy, options)
  28. * Use this method to retrieve objects from the content store.
  29. * - getConfiguration (properties)
  30. * Use this method to retrieve global configuration data.
  31. */
  32. import com.cognos.developer.schemas.bibus._3.Account;
  33. import com.cognos.developer.schemas.bibus._3.AddOptions;
  34. import com.cognos.developer.schemas.bibus._3.AuthoredReport;
  35. import com.cognos.developer.schemas.bibus._3.BaseClass;
  36. import com.cognos.developer.schemas.bibus._3.ConfigurationData;
  37. import com.cognos.developer.schemas.bibus._3.ConfigurationDataEnum;
  38. import com.cognos.developer.schemas.bibus._3.CopyOptions;
  39. import com.cognos.developer.schemas.bibus._3.DeleteOptions;
  40. import com.cognos.developer.schemas.bibus._3.Folder;
  41. import com.cognos.developer.schemas.bibus._3.Locale;
  42. import com.cognos.developer.schemas.bibus._3.MoveOptions;
  43. import com.cognos.developer.schemas.bibus._3.PropEnum;
  44. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  45. import com.cognos.developer.schemas.bibus._3.Report;
  46. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  47. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
  48. import com.cognos.developer.schemas.bibus._3.Sort;
  49. import com.cognos.developer.schemas.bibus._3.StringProp;
  50. import com.cognos.developer.schemas.bibus._3.TokenProp;
  51. import com.cognos.developer.schemas.bibus._3.UpdateActionEnum;
  52. import com.cognos.developer.schemas.bibus._3.UpdateOptions;
  53. public class CSHandlers
  54. {
  55. /**
  56. * Add an object to the Content Store.
  57. *
  58. * @param connection Connection to Server
  59. * @param bc An object that extends baseClass, such as a Report.
  60. * @param path Search path that will contain the new object.
  61. *
  62. * @return The new object.
  63. *
  64. */
  65. public BaseClass addObjectToCS(
  66. CRNConnect connection,
  67. BaseClass bc,
  68. String path)
  69. throws java.rmi.RemoteException
  70. {
  71. // sn_dg_sdk_method_contentManagerService_add_start_1
  72. AddOptions ao = new AddOptions();
  73. ao.setUpdateAction(UpdateActionEnum.replace);
  74. return connection.getCMService().add(new SearchPathSingleObject(path), new BaseClass[] { bc }, ao)[0];
  75. // sn_dg_sdk_method_contentManagerService_add_end_1
  76. }
  77. /**
  78. * Add a Report to the Content Store.
  79. *
  80. * @param connection Connection to Server
  81. * @param rprt An AuthoredReport object.
  82. * @param path Search path that will contain the new object.
  83. *
  84. * @return The new Report object.
  85. *
  86. */
  87. public AuthoredReport addReportToCS(
  88. CRNConnect connection,
  89. Report rprt,
  90. String path)
  91. throws java.rmi.RemoteException
  92. {
  93. // sn_dg_sdk_method_reportService_add_start_1
  94. AddOptions ao = new AddOptions();
  95. ao.setUpdateAction(UpdateActionEnum.replace);
  96. return connection.getReportService().add(new SearchPathSingleObject(path), rprt, ao);
  97. // sn_dg_sdk_method_reportService_add_end_1
  98. }
  99. public BaseClass[] createDirectoryInCS(
  100. CRNConnect connection,
  101. String parentPath,
  102. String directoryName)
  103. throws java.rmi.RemoteException
  104. {
  105. TokenProp directoryNameTokenProp = new TokenProp();
  106. directoryNameTokenProp.setValue(directoryName);
  107. Folder directory = new Folder();
  108. directory.setDefaultName(directoryNameTokenProp);
  109. BaseClass[] directoryList = new BaseClass[] { directory };
  110. AddOptions addOpts = new AddOptions();
  111. addOpts.setUpdateAction(UpdateActionEnum.update);
  112. return connection.getCMService().add(new SearchPathSingleObject(parentPath), directoryList, addOpts);
  113. }
  114. /**
  115. * Delete an object from the Content Store.
  116. *
  117. * @param connection Connection to Server
  118. * @param bc Specifies the object to be deleted from the content store.
  119. * @return True if successful, false otherwise.
  120. *
  121. */
  122. public boolean deleteObjectFromCS(
  123. CRNConnect connection,
  124. BaseClass bc)
  125. throws java.rmi.RemoteException
  126. {
  127. // sn_dg_sdk_method_contentManagerService_delete_start_1
  128. DeleteOptions del = new DeleteOptions();
  129. del.setForce(true);
  130. int i = connection.getCMService().delete(new BaseClass[] { bc }, del);
  131. // sn_dg_sdk_method_contentManagerService_delete_end_1
  132. return (i > 0);
  133. }
  134. /**
  135. * Save modified object(s) to the Content Store.
  136. *
  137. * @param connection Connection to Server
  138. * @param bc An object that extends baseClass. Specifies the objects
  139. * and properties to be updated. If you do not include
  140. * a property for an object, the property is not modified
  141. * for that object. If you include a property for an object
  142. * but you don't specify a value, the value of that property
  143. * is deleted from the object. If the value of an acquired
  144. * property is deleted from an object, such as the policies
  145. * property, the value will be acquired from an ancestor of
  146. * the object.
  147. *
  148. * @return An array of BaseClass objects. An error is not returned if no objects are selected.
  149. *
  150. */
  151. public BaseClass[] updateObjectInCS(
  152. CRNConnect connection,
  153. BaseClass[] bc)
  154. throws java.rmi.RemoteException
  155. {
  156. // sn_dg_sdk_method_contentManagerService_update_start_1
  157. return connection.getCMService().update(bc, new UpdateOptions());
  158. // sn_dg_sdk_method_contentManagerService_update_end_1
  159. }
  160. /**
  161. * Move object in the Content Store.
  162. *
  163. * @param connection Connection to Server
  164. * @param bc Specifies the objects to be moved to a new location in the content store.
  165. * @param targetPath Specifies the target location for the moved objects. This parameter must
  166. * select a single container object that is writable in the current security
  167. * context.
  168. *
  169. * @return The new object. An error is not returned if no objects are selected.
  170. *
  171. */
  172. public BaseClass[] moveObjectsInCS(
  173. CRNConnect connection,
  174. BaseClass[] bc,
  175. String targetPath)
  176. {
  177. try
  178. {
  179. // sn_dg_sdk_method_contentManagerService_move_start_1
  180. return connection.getCMService().move(bc, new SearchPathSingleObject(targetPath), new MoveOptions());
  181. // sn_dg_sdk_method_contentManagerService_move_end_1
  182. }
  183. catch (java.rmi.RemoteException remoteEx)
  184. {
  185. remoteEx.printStackTrace();
  186. return null;
  187. }
  188. }
  189. /**
  190. * Move reports in the Content Store.
  191. *
  192. * @param connection Connection to Server
  193. * @param reportPath Search path to an AuthoredReport object.
  194. * @param targetPath Search path that will contain the moved object.
  195. *
  196. * @return
  197. *
  198. */
  199. public void moveReports(
  200. CRNConnect connection,
  201. String[] reportPath,
  202. String targetPath)
  203. {
  204. //This code moves the prompt report to the public package
  205. BaseClass[] obj = new BaseClass[1];
  206. obj[0] = new Report();
  207. StringProp path = new StringProp();
  208. path.setValue(reportPath[0]);
  209. obj[0].setSearchPath(path);
  210. obj = moveObjectsInCS(connection, obj, targetPath);
  211. System.out.println("");
  212. System.out.println("Here is the list of items that were moved.");
  213. if (obj != null)
  214. {
  215. for (int i = 0; i < obj.length; i++)
  216. {
  217. System.out.println(
  218. "Name:" + obj[i].getDefaultName().getValue());
  219. }
  220. }
  221. else
  222. {
  223. System.out.println("No items were moved.");
  224. }
  225. }
  226. /**
  227. * Copy an object in the Content Store.
  228. *
  229. * @param connection Connection to Server
  230. * @param bc Specifies the objects to be copied.
  231. * @param targetPath Search path that will contain the new object.
  232. * Must select a single container object that must
  233. * be writable in the current security context.
  234. *
  235. * @return The new objects. An error is not returned if no
  236. * objects are selected.
  237. *
  238. */
  239. public BaseClass[] copyObjectsInCS(
  240. CRNConnect connection,
  241. BaseClass[] bc,
  242. String targetPath)
  243. {
  244. try
  245. {
  246. // sn_dg_sdk_method_contentManagerService_copy_start_1
  247. return connection.getCMService().copy(bc, new SearchPathSingleObject(targetPath), new CopyOptions());
  248. // sn_dg_sdk_method_contentManagerService_copy_end_1
  249. }
  250. catch (java.rmi.RemoteException remoteEx)
  251. {
  252. remoteEx.printStackTrace();
  253. return null;
  254. }
  255. }
  256. /**
  257. * Copy report in the Content Store.
  258. *
  259. * @param connection Connection to Server
  260. * @param reportPath Search path to an AuthoredReport object.
  261. * @param targetPath Search path that will contain the new object.
  262. *
  263. * @return true if the reports were copied and false otherwise.
  264. *
  265. */
  266. public boolean copyReports(
  267. CRNConnect connection,
  268. String[] reportPath,
  269. String targetPath)
  270. {
  271. BaseClass[] obj = new BaseClass[1];
  272. StringProp path = new StringProp();
  273. path.setValue(reportPath[0]);
  274. obj[0] = new Report();
  275. obj[0].setSearchPath(path);
  276. obj = copyObjectsInCS(connection, obj, targetPath);
  277. if (obj != null)
  278. {
  279. System.out.println("Here is the list of items that were copied.");
  280. for (int i = 0; i < obj.length; i++)
  281. {
  282. System.out.println(
  283. "Name:" + obj[i].getDefaultName().getValue());
  284. }
  285. return true;
  286. }
  287. else
  288. {
  289. System.out.println("No items were copied.");
  290. return false;
  291. }
  292. }
  293. /**
  294. * Take ownership of an object in the Content Store.
  295. *
  296. * @param connection Connection to Server
  297. * @param bc An object that extends baseClass, such as a Report.
  298. *
  299. * @return
  300. *
  301. */
  302. public void takeOwnerShip(CRNConnect connection, BaseClass bc)
  303. {
  304. Account me = Logon.getLogonAccount(connection);
  305. bc.setOwner(me.getOwner());
  306. }
  307. /**
  308. * Take ownership of a report in the Content Store.
  309. *
  310. * @param connection Connection to Server
  311. * @param reportPath Search path to report
  312. *
  313. * @return
  314. *
  315. */
  316. public void takeOwnerShipOfReport(
  317. CRNConnect connection,
  318. String[] reportPath)
  319. {
  320. BaseClass[] bc = new BaseClass[1];
  321. StringProp path = new StringProp();
  322. path.setValue(reportPath[0]);
  323. bc[0] = new Report();
  324. bc[0].setSearchPath(path);
  325. takeOwnerShip(connection, bc[0]);
  326. }
  327. /**
  328. * Use to access objects. This is similar to a filepath on the OS.
  329. * The default properties are searchPath and defaultName.
  330. *
  331. * Use this method if you want to retrieve objects from the content store.
  332. * Examples of content store objects are Folder, Report, URL, Package and so on.
  333. * Since these are returned as an array (or BaseClasses), use the following
  334. * to determine which type of object it is:
  335. *
  336. * if(bc[i] instanceof Report) // This is a report.
  337. * if(bc[i] instanceof URL) // This is a URL.
  338. * if(bc[i] instanceof Folder) // This is a folder.
  339. * if(bc[i] instanceof Package_) // This is a package.
  340. * if(bc[i] instanceof Query) // This is a query.
  341. *
  342. * @param connection Connection to Server
  343. * @param path This is the search path.
  344. * It is needed to access objects in the Content Store
  345. * and is similar to a filepath on the OS.
  346. *
  347. * @return An array of BaseClass objects.
  348. *
  349. */
  350. public BaseClass[] queryObjectInCS(
  351. CRNConnect connection,
  352. String path)
  353. throws java.rmi.RemoteException
  354. {
  355. // Set up the properties.
  356. PropEnum properties[] =
  357. new PropEnum[] { PropEnum.defaultName, PropEnum.searchPath };
  358. // Call the other version of this method.
  359. return queryObjectInCS(connection, path, properties);
  360. }
  361. /**
  362. * Use to access objects. This is similar to a filepath on the OS.
  363. * The default properties are searchPath and defaultName.
  364. *
  365. * Use this method if you want to retrieve objects from the content store.
  366. * Examples of content store objects are Folder, Report, URL, Package and so on.
  367. * Since these are returned as an array (or BaseClasses), use the following
  368. * to determine which type of object it is:
  369. *
  370. * if(bc[i] instanceof Report) // This is a report.
  371. * if(bc[i] instanceof URL) // This is a URL.
  372. * if(bc[i] instanceof Folder) // This is a folder.
  373. * if(bc[i] instanceof Package_) // This is a package.
  374. * if(bc[i] instanceof Query) // This is a query.
  375. *
  376. * @param connection Connection to Server
  377. * @param path This is the search path.
  378. * It is needed to access objects in the Content Store
  379. * and is similar to a filepath on the OS.
  380. * @param properties A list of alternate properties you MAY wish to
  381. * ask for on each object. The default properties
  382. * are searchPath and defaultName.
  383. *
  384. * @return An array of BaseClass objects.
  385. *
  386. */
  387. public BaseClass[] queryObjectInCS(
  388. CRNConnect connection,
  389. String path,
  390. PropEnum[] properties)
  391. throws java.rmi.RemoteException
  392. {
  393. // Used to determine which property
  394. // the objects are sorted by.
  395. // The default is "defaultName".
  396. Sort sort[] = new Sort[] { new Sort()};
  397. return queryObjectInCS(connection, path, properties, sort);
  398. }
  399. /**
  400. * Use to access objects. This is similar to a filepath on the OS.
  401. * The default properties are searchPath and defaultName.
  402. *
  403. * Use this method if you want to retrieve objects from the content store.
  404. * Examples of content store objects are Folder, Report, URL, Package and so on.
  405. * Since these are returned as an array (or BaseClasses), use the following
  406. * to determine which type of object it is:
  407. *
  408. * if(bc[i] instanceof Report) // This is a report.
  409. * if(bc[i] instanceof URL) // This is a URL.
  410. * if(bc[i] instanceof Folder) // This is a folder.
  411. * if(bc[i] instanceof Package_) // This is a package.
  412. * if(bc[i] instanceof Query) // This is a query.
  413. *
  414. * @param connection Connection to Server
  415. * @param path This is the search path.
  416. * It is needed to access objects in the Content Store
  417. * and is similar to a filepath on the OS.
  418. * @param properties A list of alternate properties you MAY wish to
  419. * ask for on each object. The default properties
  420. * are searchPath and defaultName.
  421. * @param sort A list of sorting options.
  422. *
  423. * @return An array of BaseClass objects.
  424. *
  425. */
  426. public BaseClass[] queryObjectInCS(
  427. CRNConnect connection,
  428. String path,
  429. PropEnum[] properties,
  430. Sort sort[])
  431. throws java.rmi.RemoteException
  432. {
  433. // Used to give instructions such as max objects to
  434. // be returned, size of certain properties and so on.
  435. // In this case, we do nothing with it.
  436. QueryOptions qop = new QueryOptions();
  437. // Call the actual query method and return the base class array.
  438. return connection.getCMService().query(new SearchPathMultipleObject(path), properties, sort, qop);
  439. }
  440. /**
  441. * This method accepts a Report Search Path and
  442. * returns only the parent name
  443. *
  444. * @param connection Connection to Server
  445. *
  446. * @param searchPath
  447. * Search path of child object
  448. *
  449. * @return
  450. * Search path of parent object
  451. */
  452. public String getParentPath(
  453. CRNConnect connection,
  454. String searchPath)
  455. {
  456. String parentPath = "";
  457. Sort sortArray[] = { new Sort()};
  458. QueryOptions queryOptions = new QueryOptions();
  459. PropEnum props[] =
  460. new PropEnum[] {
  461. PropEnum.searchPath,
  462. PropEnum.defaultName,
  463. PropEnum.parent };
  464. try
  465. {
  466. BaseClass child[];
  467. child = connection.getCMService().query(new SearchPathMultipleObject(searchPath), props, sortArray, queryOptions);
  468. if (child != null && (child.length > 0))
  469. {
  470. String[] parents = new String[child.length];
  471. for (int i = 0; i < child.length; i++)
  472. {
  473. parents[i] =
  474. child[i]
  475. .getParent()
  476. .getValue()[0]
  477. .getSearchPath()
  478. .getValue();
  479. }
  480. parentPath = parents[0];
  481. }
  482. }
  483. catch (java.rmi.RemoteException remoteEx)
  484. {
  485. return "";
  486. }
  487. return parentPath;
  488. }
  489. /**
  490. * This method accepts a Report Name and searches amd returns a
  491. * string array of search paths of all reports with that name
  492. * If more than one package/folder contains a report with the same
  493. * name, all packages/folders will be returned
  494. *
  495. * @param connection Connection to Server
  496. *
  497. * @param reportName
  498. *
  499. * @return reportPath
  500. *
  501. */
  502. public String[] getReportPath(
  503. CRNConnect connection,
  504. String reportName)
  505. {
  506. Sort sortArray[] = { new Sort()};
  507. QueryOptions queryOptions = new QueryOptions();
  508. PropEnum props[] =
  509. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  510. try
  511. {
  512. BaseClass repPth[];
  513. String quotChar = "\'";
  514. if (reportName.indexOf(quotChar) >= 0)
  515. {
  516. quotChar = "\"";
  517. }
  518. repPth =
  519. connection.getCMService().query(
  520. new SearchPathMultipleObject
  521. ("/content//report[@name="
  522. + quotChar
  523. + reportName
  524. + quotChar
  525. + "]"),
  526. props,
  527. sortArray,
  528. queryOptions);
  529. if (repPth != null && (repPth.length > 0))
  530. {
  531. String[] reportPath = new String[repPth.length];
  532. for (int i = 0; i < repPth.length; i++)
  533. {
  534. reportPath[i] = repPth[i].getSearchPath().getValue();
  535. }
  536. return reportPath;
  537. }
  538. else
  539. {
  540. quotChar = "\'";
  541. if (reportName.indexOf(quotChar) >= 0)
  542. {
  543. quotChar = "\"";
  544. }
  545. repPth =
  546. connection.getCMService().query(
  547. new SearchPathMultipleObject
  548. ("/content//query[@name="
  549. + quotChar
  550. + reportName
  551. + quotChar
  552. + "]"),
  553. props,
  554. sortArray,
  555. queryOptions);
  556. if (repPth != null && (repPth.length > 0))
  557. {
  558. //This will delete the first occurence of the report with given
  559. //Name if there are many. If all reports have to be deleted use
  560. String[] reportPath = new String[repPth.length];
  561. for (int i = 0; i < repPth.length; i++)
  562. {
  563. reportPath[i] = repPth[i].getSearchPath().getValue();
  564. }
  565. return reportPath;
  566. }
  567. }
  568. return null;
  569. }
  570. catch (java.rmi.RemoteException remoteEx)
  571. {
  572. System.out.println(remoteEx.getMessage());
  573. return null;
  574. }
  575. }
  576. /**
  577. * Get the server locale setting.
  578. *
  579. * @param connection Connection to Server
  580. *
  581. * @return Server Locale.
  582. *
  583. */
  584. public Locale[] getConfiguration(CRNConnect connection)
  585. {
  586. ConfigurationData data = null;
  587. Locale[] locales = null;
  588. ConfigurationDataEnum[] config = new ConfigurationDataEnum[1];
  589. config[0] = ConfigurationDataEnum.fromString("serverLocale");
  590. try
  591. {
  592. // sn_dg_sdk_method_systemService_getConfiguration_start_1
  593. data = connection.getSystemService().getConfiguration(config);
  594. locales = data.getServerLocale();
  595. // sn_dg_sdk_method_systemService_getConfiguration_end_1
  596. if (locales == null)
  597. {
  598. System.out.println("No serverLocale configured!");
  599. }
  600. }
  601. catch (java.rmi.RemoteException remoteEx)
  602. {
  603. remoteEx.printStackTrace();
  604. }
  605. return locales;
  606. }
  607. }