adminCommands.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: pps
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2017
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. //This JavascriptFile wraps the admin tool command syntax used for administrative requests
  9. var ADM_GET = 100;
  10. var ADM_SET = 101;
  11. var ADM_RESET_BELOW = 114;
  12. var ADM_GETOBJECTCHILDREN = 126;
  13. var ADM_COM_errorMessage = "Communication failure";
  14. var ADM_COM_ERROR_NODE = "error";
  15. var ADM_RETURN_TYPE_ATTTREE = 0;
  16. function AdminCommandRec(command,returnFunc,returnType) {
  17. this.command = command;
  18. this.returnFunc = returnFunc;
  19. this.returnType = returnType;
  20. }
  21. //This class provides a communication API to the ppes server
  22. function AdminCommands() {
  23. var xmlCom = new xmlCommunicator(this);
  24. var attStreamer = new AttributeTreeStreamer();
  25. var resultXML = null;
  26. var commandQueue= new Array();
  27. var commandInProgress = false;
  28. var commandQueueLength = 0;
  29. var commandInterval = null;
  30. //Public Methods
  31. this.GetObjProperties = function(objName, returnFunc) {
  32. var commandAttTree = new AttributeTree();
  33. commandAttTree.addInt("type",ADM_GET);
  34. commandAttTree.addString("object",objName);
  35. if (returnFunc)
  36. //Non blocking
  37. sendAsyncCommand(commandAttTree, returnFunc, ADM_RETURN_TYPE_ATTTREE);
  38. else {
  39. //Blocking
  40. return sendRequest(commandAttTree, true);
  41. }
  42. }
  43. this.GetObjChildren = function(objName, sortType, returnFunc, isSearch) {
  44. var commandAttTree = new AttributeTree();
  45. commandAttTree.addInt("type",ADM_GETOBJECTCHILDREN);
  46. commandAttTree.addInt("sortType", sortType);
  47. commandAttTree.addString("object",objName);
  48. if (isSearch)
  49. commandAttTree.addString("isSearch", "true");
  50. if (returnFunc)
  51. //Non blocking
  52. sendAsyncCommand(commandAttTree, returnFunc, ADM_RETURN_TYPE_ATTTREE);
  53. else {
  54. //Blocking
  55. return sendRequest(commandAttTree, true);
  56. }
  57. }
  58. this.SetObjProperties = function(objName, objectClass, attTree, returnFunc) {
  59. var commandAttTree = new AttributeTree();
  60. commandAttTree.addInt("type",ADM_SET);
  61. commandAttTree.addString("object",objName);
  62. commandAttTree.addString("objectClass",objectClass);
  63. var props = attTree.getRoot();
  64. props.name = "value";
  65. commandAttTree.insertNode("value",props);
  66. if (returnFunc)
  67. //Non blocking
  68. sendAsyncCommand(commandAttTree, returnFunc, ADM_RETURN_TYPE_ATTTREE);
  69. else {
  70. //Blocking
  71. return sendRequest(commandAttTree, true);
  72. }
  73. }
  74. this.resetDescendants = function(objName, returnFunc) {
  75. var commandAttTree = new AttributeTree();
  76. commandAttTree.addInt("type",ADM_RESET_BELOW);
  77. commandAttTree.addString("object",objName);
  78. if (returnFunc)
  79. //Non blocking
  80. sendAsyncCommand(commandAttTree, returnFunc, ADM_RETURN_TYPE_ATTTREE);
  81. else {
  82. //Blocking
  83. return sendRequest(commandAttTree, true);
  84. }
  85. }
  86. //Private Methods
  87. function sendAsyncCommand(command, returnFunc, returnType) {
  88. //Put this command at the start of the command Queue;
  89. var commandInfo = new AdminCommandRec(command, returnFunc, returnType);
  90. for (var i = commandQueueLength - 1; i >= 0; i--) {
  91. commandQueue[i + 1] = commandQueue[i];
  92. }
  93. commandQueue[0] = commandInfo;
  94. commandQueueLength++;
  95. //If no command is in progress, send one
  96. if (!commandInProgress)
  97. sendNextCommand();
  98. }
  99. function requestCompleted() {
  100. //The request completed, so we handle the response
  101. var result;
  102. var success = getXMLResponse();
  103. if (commandQueue[commandQueueLength - 1].returnType == ADM_RETURN_TYPE_ATTTREE) {
  104. if (success) {
  105. result = attStreamer.getAttributeTreeFromXML(resultXML);
  106. } else {
  107. if (resultXML)
  108. result = generateErrorAttTree(resultXML);
  109. else
  110. result = generateErrorAttTree(ADM_COM_errorMessage);
  111. }
  112. }
  113. //Debug only;
  114. var debugDump = document.getElementById("attResponseDump");
  115. if (debugDump) {
  116. debugDump.value += "\n**************************\n";
  117. if (success)
  118. debugDump.value += result;
  119. else
  120. debugDump.value += "Unable to produce an attribute tree";
  121. }
  122. commandQueue[commandQueueLength - 1].returnFunc(result);
  123. commandQueueLength--;
  124. sendNextCommand();
  125. }
  126. this.requestCompleted = requestCompleted;
  127. function sendNextCommand() {
  128. if (commandQueueLength > 0) {
  129. if (send(commandQueue[commandQueueLength - 1].command,false))
  130. commandInProgress = true;
  131. else {
  132. //Bad command, decriment the array
  133. commandQueueLength--;
  134. commandInProgress = false;
  135. sendNextCommand();
  136. }
  137. } else {
  138. commandInProgress = false;
  139. }
  140. return commandInProgress;
  141. }
  142. function generateErrorAttTree(errorMessage) {
  143. var attTree = new AttributeTree();
  144. attTree.addString(ADM_COM_ERROR_NODE,errorMessage);
  145. return attTree;
  146. }
  147. function getXMLResponse() {
  148. resultXML = null;
  149. if (xmlCom.requestSuceeded()) {
  150. //Debug only
  151. var debugDump = document.getElementById("xmlResponseDump");
  152. if (debugDump) {
  153. debugDump.value += "\n**************************\n";
  154. debugDump.value += xmlCom.GetTextResponse();
  155. }
  156. if (!xmlCom.GetXMLResponse()) {
  157. resultXML = xmlCom.GetTextResponse();
  158. return false;
  159. } else {
  160. resultXML = xmlCom.GetXMLResponse();
  161. return true;
  162. }
  163. } else {
  164. return false;
  165. }
  166. }
  167. function getCamPassportCookie() {
  168. var allCookies = document.cookie;
  169. var start = allCookies.indexOf("cam_passport=");
  170. if (start != -1) {
  171. var end = allCookies.indexOf(";", start);
  172. if (end == -1)
  173. end = allCookies.length;
  174. return unescape(allCookies.substring(start + "cam_passport=".length, end));
  175. }
  176. return "";
  177. }
  178. function send(attTree, blocking) {
  179. if (document.getElementById("attRequestDump")) {
  180. document.getElementById("attRequestDump").value += "\n**************************\n";
  181. document.getElementById("attRequestDump").value += attTree;
  182. }
  183. resultXML = null;
  184. var xmlCommand = attStreamer.getXMLFromAttributeTree(attTree);
  185. var xmlText = "";
  186. if (xmlCommand.xml) {
  187. xmlText = xmlCommand.xml;
  188. } else {
  189. xmlText = (new XMLSerializer()).serializeToString(xmlCommand);
  190. }
  191. var cmdString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + xmlText;
  192. if (document.getElementById("xmlRequestDump")) {
  193. document.getElementById("xmlRequestDump").value += "\n**************************\n";
  194. document.getElementById("xmlRequestDump").value += cmdString;
  195. }
  196. var data = new dataCollection();
  197. data.addItem("b_action","powerPlayService");
  198. data.addItem("ATXR",cmdString);
  199. data.addItem("ADMPP", getCamPassportCookie()); // protect from CSRF
  200. var status = xmlCom.sendRequest(data.getDataArray(), blocking);
  201. if (blocking) {
  202. return getXMLResponse();
  203. } else {
  204. return (status == xmlCom.STATUS_ASYNC_SENT);
  205. }
  206. }
  207. function sendRequest(attTree, blocking) {
  208. var result;
  209. var success = send(attTree, blocking);
  210. if (blocking) {
  211. if (success) {
  212. result = attStreamer.getAttributeTreeFromXML(resultXML);
  213. } else {
  214. if (resultXML)
  215. result = generateErrorAttTree(resultXML);
  216. else
  217. result = generateErrorAttTree(ADM_COM_errorMessage);
  218. }
  219. //Debug only;
  220. var debugDump = document.getElementById("attResponseDump");
  221. if (debugDump) {
  222. debugDump.value += "\n**************************\n";
  223. if (success)
  224. debugDump.value += result;
  225. else
  226. debugDump.value += "Unable to produce an attribute tree";
  227. }
  228. debugDump = document.getElementById("xmlResponseDump");
  229. if (debugDump) {
  230. debugDump.value += "\n**************************\n";
  231. debugDump.value += xmlCom.GetTextResponse();
  232. }
  233. return result;
  234. } else {
  235. return success;
  236. }
  237. }
  238. }