sqoop.connections.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. var connections = (function($) {
  17. var ConnectionModel = koify.Model.extend({
  18. 'id': -1,
  19. 'name': null,
  20. 'connector': [],
  21. 'connector_id': 0,
  22. 'creation_date': null,
  23. 'creation_user': null,
  24. 'update_date': null,
  25. 'update_user': null,
  26. 'framework': [],
  27. 'initialize': function(attrs) {
  28. var self = this;
  29. var _attrs = $.extend(true, {}, attrs);
  30. _attrs = transform_keys(_attrs, {
  31. 'connector-id': 'connector_id'
  32. });
  33. _attrs = transform_values(_attrs, {
  34. 'connector': to_forms,
  35. 'framework': to_forms
  36. });
  37. return _attrs;
  38. }
  39. });
  40. var Connection = koify.Node.extend({
  41. 'identifier': 'connection',
  42. 'persists': true,
  43. 'model_class': ConnectionModel,
  44. 'base_url': '/sqoop/api/connections/',
  45. 'initialize': function(options) {
  46. var self = this;
  47. self.parent.initialize.apply(self, arguments);
  48. self.selected = ko.observable();
  49. self.persisted = ko.computed(function() {
  50. return self.id() > -1;
  51. });
  52. self.connectionString = ko.computed(function() {
  53. var connection_string = null;
  54. $.each(self.connector(), function(index, form) {
  55. if (form.name() == 'connection') {
  56. $.each(form.inputs(), function(index, input) {
  57. if (input.name() == 'connection.connectionString') {
  58. connection_string = input.value();
  59. }
  60. });
  61. }
  62. });
  63. return connection_string;
  64. });
  65. self.jdbcDriver = ko.computed(function() {
  66. var jdbc_driver = null;
  67. $.each(self.connector(), function(index, form) {
  68. if (form.name() == 'connection') {
  69. $.each(form.inputs(), function(index, input) {
  70. if (input.name() == 'connection.jdbcDriver') {
  71. jdbc_driver = input.value();
  72. }
  73. });
  74. }
  75. });
  76. return jdbc_driver;
  77. });
  78. self.database = ko.computed(function() {
  79. var pattern = null;
  80. switch (self.jdbcDriver()) {
  81. case 'com.mysql.jdbc.Driver':
  82. pattern = /jdbc:mysql:\/\/.*?\/(.*)/;
  83. break;
  84. case 'org.postgresql.Driver':
  85. pattern = /jdbc:postgresql:\/\/.*?\/(.*)/;
  86. break;
  87. case 'oracle.jdbc.OracleDriver':
  88. pattern = /jdbc:oracle:thin:@.*?:.*?:(.*)/;
  89. break;
  90. }
  91. if (pattern) {
  92. var res = self.connectionString().match(pattern);
  93. if (res) {
  94. return res[1];
  95. } else {
  96. return null;
  97. }
  98. }
  99. });
  100. },
  101. 'map': function() {
  102. var self = this;
  103. var mapping_options = $.extend(true, {
  104. 'ignore': ['parent', 'initialize']
  105. }, forms.MapProperties);
  106. if ('__ko_mapping__' in self) {
  107. ko.mapping.fromJS(self.model, mapping_options, self);
  108. } else {
  109. var mapped = ko.mapping.fromJS(self.model, mapping_options);
  110. $.extend(self, mapped);
  111. }
  112. },
  113. });
  114. function fetch_connections(options) {
  115. $(document).trigger('load.connections', [options]);
  116. var request = $.extend({
  117. url: '/sqoop/api/connections/',
  118. dataType: 'json',
  119. type: 'GET',
  120. success: fetcher_success('connections', Connection, options),
  121. error: fetcher_error('connections', Connection, options)
  122. }, options || {});
  123. $.ajax(request);
  124. }
  125. return {
  126. 'ConnectionModel': ConnectionModel,
  127. 'Connection': Connection,
  128. 'fetchConnections': fetch_connections
  129. }
  130. })($);