CopyMoveReport.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2006
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * CopyMoveReport.java
  10. *
  11. * Copyright (C) 2006 Cognos ULC, an IBM Company. All rights reserved.
  12. * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. *
  14. */
  15. import java.rmi.RemoteException;
  16. import com.cognos.developer.schemas.bibus._3.Account;
  17. import com.cognos.developer.schemas.bibus._3.BaseClass;
  18. import com.cognos.developer.schemas.bibus._3.CopyOptions;
  19. import com.cognos.developer.schemas.bibus._3.MoveOptions;
  20. import com.cognos.developer.schemas.bibus._3.OrderEnum;
  21. import com.cognos.developer.schemas.bibus._3.PropEnum;
  22. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  23. import com.cognos.developer.schemas.bibus._3.Report;
  24. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  25. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
  26. import com.cognos.developer.schemas.bibus._3.Sort;
  27. import com.cognos.developer.schemas.bibus._3.StringProp;
  28. import com.cognos.developer.schemas.bibus._3.UpdateActionEnum;
  29. public class CopyMoveReport {
  30. // This is a method for retrieving a list of the available reports to run
  31. protected BaseClassWrapper[] getListOfReports(CRNConnect connection) {
  32. BaseClassWrapper reportAndQueryList[] = null;
  33. BaseClass reports[] = new BaseClass[0];
  34. BaseClass queries[] = new BaseClass[0];
  35. int reportAndQueryIndex = 0;
  36. int reportIndex = 0;
  37. int queryIndex = 0;
  38. if (connection == null) {
  39. System.out
  40. .println("Invalid parameter passed to getListOfReports()\n");
  41. return null;
  42. }
  43. PropEnum props[] = new PropEnum[] { PropEnum.searchPath,
  44. PropEnum.defaultName };
  45. Sort sortOptions[] = { new Sort() };
  46. sortOptions[0].setOrder(OrderEnum.ascending);
  47. sortOptions[0].setPropName(PropEnum.defaultName);
  48. try {
  49. SearchPathMultipleObject reportsPath = new SearchPathMultipleObject(
  50. "/content//report");
  51. SearchPathMultipleObject queriesPath = new SearchPathMultipleObject(
  52. "/content//query");
  53. reports = connection.getCMService().query(reportsPath, props,
  54. sortOptions, new QueryOptions());
  55. queries = connection.getCMService().query(queriesPath, props,
  56. sortOptions, new QueryOptions());
  57. } catch (java.rmi.RemoteException remoteEx) {
  58. System.out.println("Caught Remote Exception:\n");
  59. remoteEx.printStackTrace();
  60. }
  61. reportAndQueryList = new BaseClassWrapper[reports.length
  62. + queries.length];
  63. if ((reports != null) && (reports.length > 0)) {
  64. for (reportIndex = 0; reportIndex < reports.length; reportIndex++) {
  65. reportAndQueryList[reportAndQueryIndex++] = new BaseClassWrapper(
  66. reports[reportIndex]);
  67. }
  68. }
  69. if ((queries != null) && (queries.length > 0)) {
  70. for (queryIndex = 0; queryIndex < queries.length; queryIndex++) {
  71. reportAndQueryList[reportAndQueryIndex++] = new BaseClassWrapper(
  72. queries[queryIndex]);
  73. }
  74. }
  75. return reportAndQueryList;
  76. }
  77. // Get current account information to assign search path to target report
  78. public Account getCurrentAccountInfo(CRNConnect myConn) {
  79. BaseClass[] bcAccountInfo = null;
  80. Account currentAccount = null;
  81. PropEnum propEnum[] = new PropEnum[] { PropEnum.searchPath,
  82. PropEnum.defaultName };
  83. SearchPathMultipleObject searchPathObject = new SearchPathMultipleObject("~");
  84. try {
  85. bcAccountInfo = myConn.getCMService().query(searchPathObject,
  86. propEnum, new Sort[] {}, new QueryOptions());
  87. if (bcAccountInfo != null && bcAccountInfo.length > 0) {
  88. currentAccount = (Account) bcAccountInfo[0];
  89. }
  90. } catch (RemoteException ex) {
  91. System.out.println("Failed to get current Account information."
  92. + "\n" + "The error: " + ex.getMessage());
  93. }
  94. return currentAccount;
  95. }
  96. // Copy or move a report to the target path
  97. public String copyMoveReport(CRNConnect myCon, BaseClassWrapper myReport,
  98. String optionName, String myTargetReportName) {
  99. String results = null;
  100. BaseClass[] bcCopyMove = null;
  101. Report targetReport = null;
  102. SearchPathSingleObject targetSearchPath = null;
  103. StringProp mySearchPath = null;
  104. // Get the source report search path and specifies the target report
  105. String myReportPath = myReport.getBaseClassObject().getSearchPath()
  106. .getValue();
  107. mySearchPath = new StringProp();
  108. mySearchPath.setValue(myReportPath);
  109. targetReport = new Report();
  110. targetReport.setSearchPath(mySearchPath);
  111. bcCopyMove = new BaseClass[1];
  112. bcCopyMove[0] = targetReport;
  113. // Specifies the target location for the copied report
  114. targetSearchPath = new SearchPathSingleObject();
  115. // get current account information
  116. Account currentAccount = this.getCurrentAccountInfo(myCon);
  117. String targetPath = currentAccount.getSearchPath().getValue()
  118. + "/folder[@name='My Folders']";
  119. targetSearchPath.set_value(targetPath);
  120. if (optionName.equalsIgnoreCase("Copy")) {
  121. results = doCopyOrMove(myCon, bcCopyMove, targetSearchPath,
  122. optionName);
  123. } else if (optionName.equalsIgnoreCase("Copy Rename")) {
  124. results = doCopyOrMoveRename(myCon, myReport, bcCopyMove,
  125. targetSearchPath, optionName, myTargetReportName);
  126. } else if (optionName.equalsIgnoreCase("Move")) {
  127. results = doCopyOrMove(myCon, bcCopyMove, targetSearchPath,
  128. optionName);
  129. } else if (optionName.equalsIgnoreCase("Move Rename")) {
  130. results = doCopyOrMoveRename(myCon, myReport, bcCopyMove,
  131. targetSearchPath, optionName, myTargetReportName);
  132. }
  133. return results;
  134. }
  135. // Use 'copyRename' or 'moveRename' method to copy or move reports to
  136. // another location in the
  137. // content store under a different name
  138. public String doCopyOrMoveRename(CRNConnect Conn,
  139. BaseClassWrapper selectedReport, BaseClass[] bcCopy,
  140. SearchPathSingleObject copyTargetSearchPath, String runOptionName,
  141. String targetReportName) {
  142. String copyMoveRenameResults = null;
  143. CopyOptions cpyOption = null;
  144. MoveOptions moveOption = null;
  145. BaseClass[] bcCopyMoveRename = null;
  146. // Specifies the target report name for the copies or moves of the
  147. // report
  148. String[] targetName = new String[1];
  149. targetName[0] = targetReportName;
  150. // Set the copy options to replace so it overwrites an existing
  151. // report
  152. cpyOption = new CopyOptions();
  153. cpyOption.setUpdateAction(UpdateActionEnum.replace);
  154. // Set the move options to replace so it overwrites an existing
  155. // report
  156. moveOption = new MoveOptions();
  157. moveOption.setUpdateAction(UpdateActionEnum.replace);
  158. try {
  159. if (runOptionName.equalsIgnoreCase("Copy Rename")) {
  160. // Copy report to target path in the content store under a
  161. // different name
  162. bcCopyMoveRename = Conn.getCMService().copyRename(bcCopy,
  163. copyTargetSearchPath, targetName, cpyOption);
  164. } else if (runOptionName.equalsIgnoreCase("Move Rename")) {
  165. // Move report to target path in the content store under a
  166. // different name
  167. bcCopyMoveRename = Conn.getCMService().moveRename(bcCopy,
  168. copyTargetSearchPath, targetName, moveOption);
  169. }
  170. if (bcCopyMoveRename != null && bcCopyMoveRename.length > 0) {
  171. copyMoveRenameResults = bcCopyMoveRename[0].getStoreID()
  172. .getValue().get_value();
  173. } else {
  174. System.out.println("Failed to " + runOptionName + " a report.");
  175. return null;
  176. }
  177. } catch (Exception e) {
  178. System.out.println("Failed to " + runOptionName + " a report"
  179. + "\n" + "The error: " + e.getMessage());
  180. return null;
  181. }
  182. return copyMoveRenameResults;
  183. }
  184. // Use 'copy' or 'move' method to copy or move objects within the content
  185. // store
  186. public String doCopyOrMove(CRNConnect Conn, BaseClass[] bcCopyMove,
  187. SearchPathSingleObject copyMoveTargetSearchPath,
  188. String runOptionName) {
  189. String copyMoveResults = null;
  190. BaseClass[] bcCopyMoveResults = null;
  191. CopyOptions copyOptions = new CopyOptions();
  192. copyOptions.setUpdateAction(UpdateActionEnum.replace);
  193. MoveOptions moveOptions = new MoveOptions();
  194. moveOptions.setUpdateAction(UpdateActionEnum.replace);
  195. try {
  196. if (runOptionName.equalsIgnoreCase("Copy")) {
  197. // copy report to target path in the content store
  198. bcCopyMoveResults = Conn.getCMService().copy(bcCopyMove,
  199. copyMoveTargetSearchPath, copyOptions);
  200. } else if (runOptionName.equalsIgnoreCase("Move")) {
  201. // Move report to target path in the content store
  202. bcCopyMoveResults = Conn.getCMService().move(bcCopyMove,
  203. copyMoveTargetSearchPath, moveOptions);
  204. }
  205. if (bcCopyMoveResults != null && bcCopyMoveResults.length > 0) {
  206. copyMoveResults = bcCopyMoveResults[0].getStoreID().getValue()
  207. .get_value();
  208. } else {
  209. System.out.println("Failed to " + runOptionName
  210. + " a report to target path.");
  211. return null;
  212. }
  213. } catch (Exception e) {
  214. System.out.println("Failed to " + runOptionName + " a report"
  215. + "\n" + "The error: " + e.getMessage());
  216. return null;
  217. }
  218. return copyMoveResults;
  219. }
  220. // Get report name
  221. public String getReportName(BaseClassWrapper aReport) {
  222. String myReportName = null;
  223. if (aReport != null) {
  224. myReportName = ((Report) aReport.getBaseClassObject())
  225. .getDefaultName().getValue();
  226. }
  227. return myReportName;
  228. }
  229. public boolean checkReportStatus(CRNConnect myConnect,
  230. BaseClassWrapper myChosenReport) {
  231. boolean checkRS = false;
  232. BaseClass[] checkResult = null;
  233. String reportSearchPath = myChosenReport.getBaseClassObject()
  234. .getSearchPath().getValue();
  235. try {
  236. checkResult = myConnect.getCMService().query(
  237. new SearchPathMultipleObject(reportSearchPath),
  238. new PropEnum[0], new Sort[0], new QueryOptions());
  239. if (checkResult != null && checkResult.length > 0) {
  240. checkRS = true;
  241. }
  242. } catch (RemoteException re) {
  243. System.out
  244. .println("Error retrieving report from the content store: "
  245. + re.getMessage());
  246. }
  247. return checkRS;
  248. }
  249. }