AddReport.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. * ReportAdd.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 reports using the
  15. * following methods:
  16. * - add(parentPath, object, options)
  17. * Use this method to add reports to the content store.
  18. */
  19. import java.io.*;
  20. import java.util.List;
  21. import org.dom4j.*;
  22. import org.dom4j.io.*;
  23. import com.cognos.developer.schemas.bibus._3.Account;
  24. import com.cognos.developer.schemas.bibus._3.AddOptions;
  25. import com.cognos.developer.schemas.bibus._3.AnyTypeProp;
  26. import com.cognos.developer.schemas.bibus._3.AsynchDetailReportValidation;
  27. import com.cognos.developer.schemas.bibus._3.AsynchReply;
  28. import com.cognos.developer.schemas.bibus._3.AsynchReplyStatusEnum;
  29. import com.cognos.developer.schemas.bibus._3.AsynchSecondaryRequest;
  30. import com.cognos.developer.schemas.bibus._3.BaseClass;
  31. import com.cognos.developer.schemas.bibus._3.Locale;
  32. import com.cognos.developer.schemas.bibus._3.MultilingualToken;
  33. import com.cognos.developer.schemas.bibus._3.MultilingualTokenProp;
  34. import com.cognos.developer.schemas.bibus._3.Option;
  35. import com.cognos.developer.schemas.bibus._3.ParameterValue;
  36. import com.cognos.developer.schemas.bibus._3.Report;
  37. import com.cognos.developer.schemas.bibus._3.ReportServiceSpecification;
  38. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
  39. import com.cognos.developer.schemas.bibus._3.UpdateActionEnum;
  40. import com.cognos.developer.schemas.bibus._3.XmlEncodedXML;
  41. public class AddReport {
  42. public BaseClass[] namespaceInfo;
  43. public BaseClass[] groupOrRoleInfo;
  44. public BaseClass[] memberInfo;
  45. public String[] namespaceSearchPath;
  46. public String[] groupOrRoleSearchPath;
  47. public String[] memberSearchPath;
  48. public String[] namespaceDefaultName;
  49. public String[] groupOrRoleDefaultName;
  50. public String[] memberDefaultName;
  51. public int selectedNamespace = 0;
  52. public int selectedGroupOrRole = 0;
  53. public int selectedMember = 0;
  54. private static boolean specOkay = false;
  55. CSHandlers csHandler = new CSHandlers();
  56. /**
  57. * Validate a report/query specification
  58. *
  59. * @param connection Connection to server
  60. * @param reportSpec A string containing the report specification
  61. *
  62. * @return A string indicating the results of the operation
  63. *
  64. */
  65. public String validateReportSpec(CRNConnect connection, ReportServiceSpecification reportSpec)
  66. //throws java.rmi.RemoteException
  67. {
  68. AsynchReply asynchReply = null;
  69. XmlEncodedXML ValidationDefects = null;
  70. specOkay = true;
  71. try
  72. {
  73. // sn_dg_sdk_method_reportService_validateSpecification_start_1
  74. asynchReply =
  75. connection.getReportService().validateSpecification(
  76. reportSpec,
  77. new ParameterValue[] {},
  78. new Option[] {});
  79. // sn_dg_sdk_method_reportService_validateSpecification_end_1
  80. // If response is not immediately complete, call wait until complete
  81. if (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  82. {
  83. while (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
  84. {
  85. //before calling wait, double check that it is okay
  86. if (hasSecondaryRequest(asynchReply, "wait"))
  87. {
  88. asynchReply =
  89. connection.getReportService().wait(
  90. asynchReply.getPrimaryRequest(),
  91. new ParameterValue[] {},
  92. new Option[] {});
  93. }
  94. else
  95. {
  96. return "Error: Wait method not available as expected.";
  97. }
  98. }
  99. }
  100. // sn_dg_sdk_method_reportService_validateSpecification_start_2
  101. for (int i = 0; i < asynchReply.getDetails().length; i++)
  102. {
  103. if (asynchReply.getDetails()[i] instanceof AsynchDetailReportValidation)
  104. {
  105. ValidationDefects = ((AsynchDetailReportValidation)asynchReply.getDetails()[i]).getDefects();
  106. }
  107. }
  108. // sn_dg_sdk_method_reportService_validateSpecification_end_2
  109. }
  110. catch (java.rmi.RemoteException remoteEx)
  111. {
  112. //remoteEx.printStackTrace();
  113. return "Exception caught during validation:\n" + remoteEx;
  114. }
  115. if (asynchReply.equals(null))
  116. {
  117. specOkay = false;
  118. return "Validation Failed";
  119. }
  120. Document oDocument;
  121. List defectsList = null;
  122. List layoutProblemsList = null;
  123. List queryProblemsList = null;
  124. Node defectsNode;
  125. Node queryProblemsNode;
  126. Node layoutProblemsNode;
  127. if (ValidationDefects == null){
  128. return "Report Specification validates successfully";
  129. }
  130. String defects = ValidationDefects.toString();
  131. try
  132. {
  133. SAXReader xmlReader = new SAXReader();
  134. ByteArrayInputStream bais =
  135. new ByteArrayInputStream(defects.getBytes("UTF-8"));
  136. oDocument = xmlReader.read(bais);
  137. defectsList = oDocument.selectNodes("//defects");
  138. queryProblemsList = oDocument.selectNodes("//queryProblems");
  139. layoutProblemsList = oDocument.selectNodes("//layoutProblems");
  140. String errOutput = "";
  141. for (int i = 0; i < defectsList.size(); i++)
  142. {
  143. defectsNode = (Node) defectsList.get(i);
  144. if (defectsNode.hasContent())
  145. {
  146. //For now, this may be a false error. Need to check
  147. //the queryProblems and layoutProblems to see if there
  148. //actually is one. Trakker #432753
  149. //specOkay = false;
  150. //errOutput += "defects hasContent() returns true.\n";
  151. }
  152. }
  153. for (int i = 0; i < queryProblemsList.size(); i++)
  154. {
  155. queryProblemsNode = (Node) queryProblemsList.get(i);
  156. if (queryProblemsNode.hasContent())
  157. {
  158. errOutput += "\nThe following queryProblems were found:\n";
  159. List queryProblems = queryProblemsNode.selectNodes("//message[@type='expression']/@title");
  160. for (int j = 0; j < queryProblems.size(); j++)
  161. {
  162. if (queryProblems.get(j) != null)
  163. {
  164. errOutput += "\t" + ((Node) queryProblems.get(j)).getStringValue() + "\n";
  165. }
  166. }
  167. specOkay = false;
  168. }
  169. }
  170. for (int i = 0; i < layoutProblemsList.size(); i++)
  171. {
  172. layoutProblemsNode = (Node) layoutProblemsList.get(i);
  173. if (layoutProblemsNode.hasContent())
  174. {
  175. errOutput += "\nThe following layoutProblems were found:\n";
  176. List layoutProblems = layoutProblemsNode.selectNodes("//message[@type='layout']/@title");
  177. for (int j = 0; j < layoutProblems.size(); j++)
  178. {
  179. if (layoutProblems.get(j) != null)
  180. {
  181. errOutput += "\t" + ((Node) layoutProblems.get(j)).getStringValue() + "\n";
  182. }
  183. }
  184. specOkay = false;
  185. }
  186. }
  187. if (specOkay)
  188. {
  189. return "Report Specification validates successfully";
  190. }
  191. else
  192. {
  193. return "Report Specification failed to Validate!\n" + errOutput;
  194. }
  195. }
  196. catch (DocumentException docEx)
  197. {
  198. return "Exception caught parsing validation response:\n" + docEx.getMessage();
  199. }
  200. catch (UnsupportedEncodingException unsuppEncEx)
  201. {
  202. return "Exception caught preparing to parse validation response:\n" + unsuppEncEx.getMessage();
  203. }
  204. //return validationResults.getDefects();
  205. }
  206. /**
  207. * Add a Report to the Content Store
  208. *
  209. * @param connection Connection to server.
  210. * @param reportSpec String containing the report specification in xml format
  211. * @return A string containing successful status information.
  212. *
  213. */
  214. public String addSpecToCM(CRNConnect connection, ReportServiceSpecification reportSpec, String reportName)
  215. throws java.rmi.RemoteException
  216. {
  217. String validateOutput = validateReportSpec(connection, reportSpec);
  218. if (!specOkay)
  219. {
  220. return "Add not performed.\n\n" + validateOutput;
  221. }
  222. Report newReport = new Report();
  223. AnyTypeProp reportSpecProperty = new AnyTypeProp();
  224. reportSpecProperty.setValue(reportSpec.getValue().toString());
  225. MultilingualToken[] reportNames = new MultilingualToken[1];
  226. reportNames[0] = new MultilingualToken();
  227. reportNames[0].setValue(reportName);
  228. Locale[] locales = csHandler.getConfiguration(connection);
  229. if (locales == null)
  230. {
  231. locales[0] = new Locale();
  232. locales[0].setLocale("en");
  233. }
  234. reportNames[0].setLocale(locales[0].getLocale());
  235. reportNames[0].setValue(reportName);
  236. newReport.setName(new MultilingualTokenProp());
  237. newReport.getName().setValue(reportNames);
  238. newReport.setSpecification(reportSpecProperty);
  239. AddOptions addReportOptions = new AddOptions();
  240. addReportOptions.setUpdateAction(UpdateActionEnum.replace);
  241. Account currentAccount = Logon.getLogonAccount(connection);
  242. String searchPath = currentAccount.getSearchPath().getValue() + "/folder[@name=\"My Folders\"]";
  243. try
  244. {
  245. connection.getReportService().add(new SearchPathSingleObject(searchPath), newReport, addReportOptions);
  246. }
  247. catch(java.rmi.RemoteException remoteEx)
  248. {
  249. return "Exception caught while adding report " + reportName + " to the content store.";
  250. }
  251. return "Report " + reportName + " saved in " + searchPath + " in the content store.";
  252. }
  253. public boolean hasSecondaryRequest(
  254. AsynchReply response,
  255. String secondaryRequest)
  256. {
  257. AsynchSecondaryRequest[] secondaryRequests =
  258. response.getSecondaryRequests();
  259. for (int i = 0; i < secondaryRequests.length; i++)
  260. {
  261. if (secondaryRequests[i].getName().compareTo(secondaryRequest)
  262. == 0)
  263. {
  264. return true;
  265. }
  266. }
  267. return false;
  268. }
  269. }