PdSpecStore.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*******************************************************************************
  2. * IBM Confidential
  3. *
  4. * OCO Source Materials
  5. *
  6. * A and PM: PD
  7. *
  8. * (c) Copyright IBM Corp. 2014
  9. *
  10. * The source code for this program is not published or otherwise divested of
  11. * its trade secrets, irrespective of what has been deposited with the U.S.
  12. * Copyright Office.
  13. ******************************************************************************/
  14. define([
  15. "dojo/_base/declare",
  16. "dojo/store/Memory",
  17. "dojox/xml/parser",
  18. "dojo/_base/array",
  19. "dojo/query",
  20. "pd/model/pdSpec"
  21. ],function(declare, store, xmlParser, array, query, model){
  22. return declare("pd/data/PdSpecStore", [store], {
  23. constructor: function(args) {
  24. if (!args.xmlInput){
  25. throw new Error(PDMSG.ERR.IDS_ERR_MISSING_XMLDOC_PARAM);
  26. }
  27. if (typeof(args.xmlInput) == "string"){
  28. this.xmlDoc = xmlParser.parse(dojo.trim(args.xmlInput));
  29. } else {
  30. this.xmlDoc = args.xmlInput;
  31. }
  32. this.data = this.generateDataFromXml();
  33. this.inherited(arguments);
  34. },
  35. generateDataFromXml: function() {
  36. var data = {
  37. identifier: 'id',
  38. items: []
  39. };
  40. var dataList = [];
  41. var xmlDoc = this.xmlDoc.selectSingleNode("/pdSpec/personalData");
  42. if (xmlDoc){
  43. array.forEach(
  44. query("item", xmlDoc), function(node, index, arr){
  45. var dataItem = {};
  46. array.forEach(model, function(item, idx){
  47. var value;
  48. if (node.getAttribute(item.itemName) != null){
  49. value = node.getAttribute(item.itemName);
  50. } else {
  51. value = item.defaultValue;
  52. node.setAttribute(item.itemName, value);
  53. }
  54. dataItem[item.itemName] = value;
  55. });
  56. dataList.push(dataItem);
  57. }
  58. );
  59. for(var i=0; i<dataList.length; i++){
  60. data.items.push(dojo.mixin({ id: i }, dataList[i]));
  61. }
  62. }
  63. return data;
  64. },
  65. generateXmlFromData: function(selected) {
  66. var xmlDoc = this.xmlDoc.selectSingleNode("/pdSpec/personalData");
  67. for (var i=0; i<selected.length; i++){
  68. //since "hidden" is from indirection-selector, it doesn't affect isDirty attribute.
  69. xmlDoc.childNodes[i].setAttribute("hidden", this.data[i].hidden);
  70. if (this.data[i].__isDirty){
  71. xmlDoc.childNodes[i].setAttribute("name", this.data[i].name);
  72. xmlDoc.childNodes[i].setAttribute("regularAggregate", this.data[i].regularAggregate);
  73. xmlDoc.childNodes[i].setAttribute("semiAggregate", this.data[i].regularAggregate);
  74. if (this.data[i].regularAggregate == "unsupported"){
  75. xmlDoc.childNodes[i].setAttribute("usage", "attribute");
  76. } else {
  77. xmlDoc.childNodes[i].setAttribute("usage", "fact");
  78. }
  79. }
  80. }
  81. // Trim out the preview data blob.
  82. var dataNode = this.xmlDoc.selectSingleNode("/pdSpec/personalPreview");
  83. if (dataNode){
  84. dataNode.parentNode.removeChild(dataNode);
  85. }
  86. return xmlParser.parse("<addLOBData><input><pdCollection>"
  87. + xmlParser.innerXML(this.xmlDoc)
  88. + "</pdCollection></input></addLOBData>");
  89. }
  90. });
  91. });