CommonFunctions.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. using System;
  9. using System.IO;
  10. using cognosdotnet_10_2;
  11. namespace SamplesCommon
  12. {
  13. public class CommonFunctions
  14. {
  15. public CommonFunctions()
  16. {
  17. }
  18. /*
  19. * This function deals with executing reports with charts or graphics.
  20. * the img tag contains credentials that are not valid when the report
  21. * is not run through report or query studio therefore we must extract
  22. * the img from content mgr and then save it locally, then replace the
  23. * img tag returned from CRN with a path to the local graphic.
  24. */
  25. public string setupGraphics(contentManagerService1 cBICMS, string html)
  26. {
  27. string start = null;
  28. string end = null;
  29. string imgTag = null;
  30. int idx_start = 0;
  31. int idx_end = 0;
  32. string[] graphFileName = null;
  33. baseClass[] graphicList = new baseClass[0];
  34. CommonFunctions cf = new CommonFunctions();
  35. baseClass[] bcOutputList = new baseClass[0];
  36. searchPathMultipleObject outputPath = new searchPathMultipleObject();
  37. outputPath.Value = "~~/output";
  38. // Get the list of outputs generated during this session
  39. bcOutputList = cBICMS.query(outputPath,
  40. new propEnum[] { propEnum.creationTime, propEnum.searchPath },
  41. new sort[] {},
  42. new queryOptions());
  43. if ((bcOutputList == null) || (bcOutputList.GetLength(0)<= 0) )
  44. {
  45. return html;
  46. }
  47. // Find the latest output from the list
  48. output latest_output = getLatestOutput(bcOutputList);
  49. searchPathMultipleObject latestOutputPath = new searchPathMultipleObject();
  50. latestOutputPath.Value = latest_output.searchPath.value + "/graphic";
  51. //"Found Graphic in report... saving locally.");
  52. graphicList = cBICMS.query(latestOutputPath,
  53. new propEnum[] {propEnum.name, propEnum.defaultName, propEnum.searchPath, propEnum.data, propEnum.dataType },
  54. new sort[] {},
  55. new queryOptions());
  56. if ((graphicList==null) || (graphicList.GetLength(0)<=0))
  57. {
  58. return html;
  59. }
  60. graphFileName = new string[graphicList.GetLength(0)];
  61. for(int i=0; i<graphicList.GetLength(0);i++)
  62. {
  63. // search&replace img tag with local image
  64. // (except when it's already pointing to a local image: i.e.<img src="../image.jpg">)
  65. next_imgTag:
  66. idx_start = html.IndexOf("<img", idx_start);
  67. if (idx_start == -1)
  68. {
  69. return html;
  70. }
  71. start = html.Substring(0, idx_start);
  72. idx_end = html.IndexOf(">", idx_start);
  73. imgTag = html.Substring(idx_start, idx_end-idx_start+1);
  74. if (imgTag.IndexOf("src=\"..", 0, imgTag.Length) == -1)
  75. {
  76. // dump the graphic data into a PNG file in <install_location>/webcontent/samples
  77. graphic graphObj = (graphic)graphicList[i];
  78. graphFileName[i] = cf.getSamplesPath();
  79. graphFileName[i] += "graphToDisplay_" + i.ToString() + ".png";
  80. if (File.Exists(graphFileName[i]))
  81. {
  82. File.Delete(graphFileName[i]);
  83. }
  84. FileStream fs = new FileStream(graphFileName[i], FileMode.CreateNew);
  85. BinaryWriter bw = new BinaryWriter(fs);
  86. bw.Write(graphObj.data.value);
  87. bw.Flush();
  88. bw.Close();
  89. end = html.Substring(idx_end+ 1, html.Length - (idx_end+1));
  90. html = start + "<img src='" + graphFileName[i] + "'>" + end;
  91. }
  92. else
  93. {
  94. idx_start++;
  95. goto next_imgTag;
  96. }
  97. idx_start++;
  98. }
  99. return html;
  100. }
  101. public string getSamplesPath()
  102. {
  103. string samplesPath = "";
  104. // Get the current working directory
  105. string currentDirectory = Directory.GetCurrentDirectory();
  106. // if current working directory is <install_location>\sdk\csharp\bin
  107. // webcontent path is: ..\..\..\webcontent
  108. // if current working directory is <install_location>\sdk\csharp\<SampleName>\bin\debug
  109. // webcontent path is: ..\..\..\..\..\webcontent
  110. if (currentDirectory.EndsWith("bin"))
  111. {
  112. samplesPath = currentDirectory + "\\..\\..\\..\\webcontent\\samples\\";
  113. }
  114. else if (currentDirectory.EndsWith("Debug"))
  115. {
  116. samplesPath = currentDirectory + "\\..\\..\\..\\..\\..\\webcontent\\samples\\";
  117. }
  118. return samplesPath;
  119. }
  120. public output getLatestOutput(baseClass[] bcOutputList)
  121. {
  122. output currentOutput = new output();
  123. output latest_output = new output(); // the most recent output generated
  124. latest_output = (output)bcOutputList[0];
  125. if (bcOutputList.GetLength(0) > 1)
  126. {
  127. for (int outputIdx=1; outputIdx < bcOutputList.GetLength(0); outputIdx++)
  128. {
  129. currentOutput = (output)bcOutputList[outputIdx];
  130. if (latest_output.creationTime.value < currentOutput.creationTime.value)
  131. {
  132. latest_output = currentOutput;
  133. }
  134. }
  135. }
  136. return latest_output;
  137. }
  138. }
  139. }