sqoop.configs.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. function transform_keys(model, keys_dict) {
  17. $.each(keys_dict, function(key, new_key) {
  18. if (key in model) {
  19. model[new_key] = model[key];
  20. delete model[key];
  21. }
  22. });
  23. return model;
  24. }
  25. function transform_values(model, func_dict) {
  26. $.each(func_dict, function(key, f) {
  27. if (key in model) {
  28. model[key] = f(key, model[key]);
  29. }
  30. });
  31. return model;
  32. }
  33. function to_config(value) {
  34. return new configs.ConfigModel(value);
  35. }
  36. function to_configs(key, value) {
  37. $.each(value, function(index, form_dict) {
  38. value[index] = to_config(form_dict);
  39. });
  40. return value;
  41. }
  42. function to_input(value) {
  43. if (value.type.toLowerCase() == 'map') {
  44. return new configs.MapInputModel(value);
  45. } else {
  46. return new configs.InputModel(value);
  47. }
  48. }
  49. function to_inputs(key, value) {
  50. $.each(value, function(index, input_dict) {
  51. value[index] = to_input(input_dict);
  52. });
  53. return value;
  54. }
  55. var configs = (function($) {
  56. var map_config_properties = {
  57. 'create': function(options) {
  58. return new SqoopConfig({modelDict: options.data});
  59. },
  60. 'update': function(options) {
  61. options.target.initialize({modelDict: options.data})
  62. return options.target;
  63. }
  64. };
  65. var map_input_properties = {
  66. 'create': function(options) {
  67. switch(options.data.type.toLowerCase()) {
  68. case 'map':
  69. return new SqoopMapInput({modelDict: options.data});
  70. default:
  71. return new SqoopInput({modelDict: options.data});
  72. }
  73. },
  74. 'update': function(options) {
  75. options.target.initialize({modelDict: options.data})
  76. return options.target;
  77. }
  78. };
  79. var map_properties = {
  80. 'link-config': map_config_properties,
  81. 'from-job-config': map_config_properties,
  82. 'to-job-config': map_config_properties,
  83. 'driver-config': map_config_properties,
  84. 'inputs': map_input_properties
  85. };
  86. var ConfigModel = koify.Model.extend({
  87. 'id': -1,
  88. 'inputs': [],
  89. 'name': null,
  90. 'type': null,
  91. 'initialize': function(attrs) {
  92. var self = this;
  93. attrs = transform_values(attrs, {
  94. 'inputs': to_inputs
  95. });
  96. return attrs;
  97. }
  98. });
  99. var InputModel = koify.Model.extend({
  100. 'id': -1,
  101. 'name': null,
  102. 'type': null,
  103. 'size': -1,
  104. 'sensitive': false,
  105. 'values': null,
  106. 'value': "",
  107. 'initialize': function(attrs) {
  108. var self = this;
  109. var attrs = $.extend(true, {}, attrs);
  110. if ('values' in attrs && attrs['values']) {
  111. attrs['values'] = ($.isArray(attrs['values'])) ? attrs['values'] : attrs['values'].split(',');
  112. }
  113. if ('value' in attrs) {
  114. attrs.value = decodeURIComponent(attrs.value);
  115. }
  116. return attrs;
  117. }
  118. });
  119. var MapInputModel = InputModel.extend({
  120. 'value': {},
  121. 'initialize': function(attrs) {
  122. var self = this;
  123. if ('value' in attrs && attrs.value) {
  124. if (!$.isArray(attrs.value)) {
  125. var map = attrs['value'];
  126. attrs.value = [];
  127. $.each(map, function(key, value) {
  128. attrs.value.push({
  129. 'key': key,
  130. 'value': value
  131. });
  132. });
  133. }
  134. } else {
  135. attrs.value = [];
  136. }
  137. return attrs;
  138. }
  139. });
  140. var SqoopConfig = koify.MinimalNode.extend({
  141. 'model_class': ConfigModel,
  142. 'map': function() {
  143. var self = this;
  144. var mapping_options = $.extend(true, {
  145. 'ignore': ['parent', 'initialize']
  146. }, map_properties);
  147. if ('__ko_mapping__' in self) {
  148. ko.mapping.fromJS(self.model, mapping_options, self);
  149. } else {
  150. var mapped = ko.mapping.fromJS(self.model, mapping_options);
  151. $.extend(self, mapped);
  152. }
  153. }
  154. });
  155. var SqoopInput = koify.MinimalNode.extend({
  156. 'model_class': InputModel,
  157. 'map': function() {
  158. var self = this;
  159. var mapping_options = {
  160. 'ignore': ['parent', 'initialize']
  161. };
  162. if ('__ko_mapping__' in self) {
  163. ko.mapping.fromJS(self.model, mapping_options, self);
  164. } else {
  165. var mapped = ko.mapping.fromJS(self.model, mapping_options);
  166. $.extend(self, mapped);
  167. }
  168. }
  169. });
  170. var SqoopMapInput = SqoopInput.extend({
  171. 'model_class': MapInputModel,
  172. 'initialize': function() {
  173. var self = this;
  174. self.parent.initialize.apply(self, arguments);
  175. self.addToMap = function() {
  176. var value = {
  177. 'key': ko.observable(""),
  178. 'value': ko.observable("")
  179. };
  180. value.key.subscribe(function() {
  181. self.value.valueHasMutated();
  182. });
  183. value.value.subscribe(function() {
  184. self.value.valueHasMutated();
  185. });
  186. self.value().push(value);
  187. self.value.valueHasMutated();
  188. };
  189. self.removeFromMap = function(index) {
  190. self.value().splice(index, 1);
  191. self.value.valueHasMutated();
  192. };
  193. },
  194. fixModel: function(model) {
  195. var map = {};
  196. $.each(model.value, function(index, obj) {
  197. if (obj.key) {
  198. map[obj.key] = obj.value;
  199. }
  200. });
  201. model.value = map;
  202. return model;
  203. }
  204. });
  205. return {
  206. 'ConfigModel': ConfigModel,
  207. 'InputModel': InputModel,
  208. 'MapInputModel': MapInputModel,
  209. 'Config': SqoopConfig,
  210. 'Input': SqoopInput,
  211. 'MapInput': SqoopMapInput,
  212. 'MapProperties': map_properties
  213. };
  214. })(jQuery);