ESPLogicalFile.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*##############################################################################
  2. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################## */
  16. define([
  17. "dojo/_base/declare",
  18. "dojo/_base/array",
  19. "dojo/_base/lang",
  20. "dojo/_base/Deferred",
  21. "dojo/data/ObjectStore",
  22. "dojo/store/util/QueryResults",
  23. "dojo/store/Observable",
  24. "dojo/Stateful",
  25. "hpcc/WsDfu",
  26. "hpcc/FileSpray",
  27. "hpcc/ESPUtil",
  28. "hpcc/ESPResult"
  29. ], function (declare, arrayUtil, lang, Deferred, ObjectStore, QueryResults, Observable, Stateful,
  30. WsDfu, FileSpray, ESPUtil, ESPResult) {
  31. var _logicalFiles = {};
  32. var Store = declare(null, {
  33. idProperty: "Name",
  34. _watched: {},
  35. constructor: function (options) {
  36. declare.safeMixin(this, options);
  37. },
  38. getIdentity: function (object) {
  39. return object[this.idProperty];
  40. },
  41. get: function (name) {
  42. if (!_logicalFiles[name]) {
  43. _logicalFiles[name] = new LogicalFile({
  44. Name: name
  45. });
  46. }
  47. return _logicalFiles[name];
  48. },
  49. remove: function (item) {
  50. if (_logicalFiles[this.getIdentity(item)]) {
  51. _logicalFiles[this.getIdentity(item)].stopMonitor();
  52. delete _logicalFiles[this.getIdentity(item)];
  53. }
  54. },
  55. query: function (query, options) {
  56. var request = {};
  57. lang.mixin(request, options.query);
  58. if (options.start)
  59. request['PageStartFrom'] = options.start;
  60. if (options.count)
  61. request['Count'] = options.count;
  62. if (options.sort) {
  63. request['Sortby'] = options.sort[0].attribute;
  64. request['Descending'] = options.sort[0].descending;
  65. }
  66. var results = WsDfu.DFUQuery({
  67. request: request
  68. });
  69. var deferredResults = new Deferred();
  70. deferredResults.total = results.then(function (response) {
  71. if (lang.exists("DFUQueryResponse.NumFiles", response)) {
  72. return response.DFUQueryResponse.NumFiles;
  73. }
  74. return 0;
  75. });
  76. var context = this;
  77. Deferred.when(results, function (response) {
  78. var logicalFiles = [];
  79. for (key in context._watched) {
  80. context._watched[key].unwatch();
  81. }
  82. this._watched = {};
  83. if (lang.exists("DFUQueryResponse.DFULogicalFiles.DFULogicalFile", response)) {
  84. arrayUtil.forEach(response.DFUQueryResponse.DFULogicalFiles.DFULogicalFile, function (item, index) {
  85. var logicalFile = context.get(item.Name);
  86. logicalFile.updateData(item);
  87. logicalFiles.push(logicalFile);
  88. context._watched[logicalFile.Name] = logicalFile.watch("changedCount", function (name, oldValue, newValue) {
  89. if (oldValue !== newValue) {
  90. context.notify(logicalFile, logicalFile.Name);
  91. }
  92. });
  93. });
  94. }
  95. deferredResults.resolve(logicalFiles);
  96. });
  97. return QueryResults(deferredResults);
  98. }
  99. });
  100. var LogicalFile = declare([ESPUtil.Singleton], {
  101. _FileDetailSetter: function(FileDetail) {
  102. this.FileDetail = FileDetail;
  103. this.result = ESPResult.Get(FileDetail);
  104. },
  105. _DirSetter: function (Dir) {
  106. this.set("Directory", Dir);
  107. },
  108. constructor: function (args) {
  109. this.inherited(arguments);
  110. declare.safeMixin(this, args);
  111. this.logicalFile = this;
  112. },
  113. save: function (description, args) {
  114. //WsDfu/DFUInfo?FileName=progguide%3A%3Aexampledata%3A%3Akeys%3A%3Apeople.lastname.firstname&UpdateDescription=true&FileDesc=%C2%A0123&Save+Description=Save+Description
  115. var context = this;
  116. WsDfu.DFUInfo({
  117. request: {
  118. FileName: this.Name,
  119. Cluster: this.Cluster,
  120. UpdateDescription: true,
  121. FileDesc: description
  122. },
  123. load: function (response) {
  124. if (lang.exists("DFUInfoResponse.FileDetail", response)) {
  125. context.updateData(response.DFUInfoResponse.FileDetail);
  126. if (args && args.onGetAll) {
  127. args.onGetAll(response.DFUInfoResponse.FileDetail);
  128. }
  129. }
  130. }
  131. });
  132. },
  133. doDelete: function (params) {
  134. var context = this;
  135. WsDfu.DFUArrayAction([this], "Delete", {
  136. load: function (response) {
  137. context.refresh();
  138. }
  139. });
  140. },
  141. despray: function (params) {
  142. var context = this;
  143. lang.mixin(params.request, {
  144. sourceLogicalName: this.Name
  145. });
  146. return FileSpray.Despray(params);
  147. },
  148. copy: function (params) {
  149. var context = this;
  150. lang.mixin(params.request, {
  151. sourceLogicalName: this.Name
  152. });
  153. return FileSpray.Copy(params);
  154. },
  155. rename: function (params) {
  156. var context = this;
  157. lang.mixin(params.request, {
  158. srcname: this.Name
  159. });
  160. return FileSpray.Rename(params).then(function (response) {
  161. context.set("Name", params.request.dstname); //TODO - need to monitor DFUWorkunit for success (After ESPDFUWorkunit has been updated to proper singleton).
  162. context.refresh();
  163. return response;
  164. });
  165. },
  166. removeSubfiles: function (subfiles, removeSuperfile) {
  167. var context = this;
  168. return WsDfu.SuperfileAction("remove", this.Name, subfiles, removeSuperfile).then(function (response) {
  169. context.refresh();
  170. return response;
  171. });
  172. },
  173. refresh: function (full) {
  174. this.getInfo();
  175. },
  176. getInfo: function (args) {
  177. //WsDfu/DFUInfo?Name=progguide::exampledata::keys::people.state.city.zip.lastname.firstname.payload&Cluster=hthor__myeclagent HTTP/1.1
  178. var context = this;
  179. WsDfu.DFUInfo({
  180. request:{
  181. Name: this.Name,
  182. Cluster: this.Cluster
  183. },
  184. load: function (response) {
  185. if (lang.exists("DFUInfoResponse.FileDetail", response)) {
  186. context.updateData(response.DFUInfoResponse.FileDetail);
  187. if (args && args.onGetAll) {
  188. args.onGetAll(response.DFUInfoResponse.FileDetail);
  189. }
  190. }
  191. }
  192. });
  193. },
  194. updateData: function (data) {
  195. this.inherited(arguments);
  196. if (!this.result) {
  197. this.result = ESPResult.Get(data);
  198. }
  199. },
  200. fetchStructure: function (format, onFetchStructure) {
  201. var context = this;
  202. WsDfu.DFUDefFile({
  203. request: {
  204. Name: this.Name,
  205. Format: format
  206. },
  207. load: function (response) {
  208. onFetchStructure(response);
  209. }
  210. });
  211. },
  212. fetchDEF: function (onFetchXML) {
  213. this.fetchStructure("def", onFetchXML);
  214. },
  215. fetchXML: function (onFetchXML) {
  216. this.fetchStructure("xml", onFetchXML);
  217. }
  218. });
  219. return {
  220. Create: function (params) {
  221. retVal = new LogicalFile(params);
  222. retVal.create();
  223. return retVal;
  224. },
  225. Get: function (name) {
  226. var store = new Store();
  227. return store.get(name);
  228. },
  229. CreateLFQueryObjectStore: function (options) {
  230. var store = new Store(options);
  231. store = Observable(store);
  232. var objStore = new ObjectStore({ objectStore: store });
  233. return objStore;
  234. }
  235. };
  236. });