edit_dataset_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. App.MainMirroringEditDataSetController = Ember.Controller.extend({
  19. name: 'mainMirroringEditDataSetController',
  20. isEdit: false,
  21. // Fields values from Edit DataSet form
  22. formFields: Ember.Object.create({
  23. datasetName: null,
  24. datasetTargetClusterName: null,
  25. datasetSourceDir: null,
  26. datasetTargetDir: null,
  27. datasetStartDate: null,
  28. hoursForStart: null,
  29. minutesForStart: null,
  30. middayPeriodForStart: null,
  31. datasetEndDate: null,
  32. hoursForEnd: null,
  33. minutesForEnd: null,
  34. middayPeriodForEnd: null,
  35. datasetFrequency: null,
  36. repeatOptionSelected: null
  37. }),
  38. // Messages for errors occurred during Edit DataSet form validation
  39. errorMessages: Ember.Object.create({
  40. name: '',
  41. sourceDir: '',
  42. targetDir: '',
  43. startDate: '',
  44. endDate: '',
  45. frequency: '',
  46. targetClusterName: ''
  47. }),
  48. errors: Ember.Object.create({
  49. isNameError: false,
  50. isSourceDirError: false,
  51. isTargetDirError: false,
  52. isStartDateError: false,
  53. isEndDateError: false,
  54. isFrequencyError: false,
  55. isTargetClusterNameError: false
  56. }),
  57. clearStep: function () {
  58. var formFields = this.get('formFields');
  59. Em.keys(formFields).forEach(function (key) {
  60. formFields.set(key, null);
  61. }, this);
  62. this.clearErrors();
  63. },
  64. clearErrors: function () {
  65. var errorMessages = this.get('errorMessages');
  66. Em.keys(errorMessages).forEach(function (key) {
  67. errorMessages.set(key, '');
  68. }, this);
  69. var errors = this.get('errors');
  70. Em.keys(errors).forEach(function (key) {
  71. errors.set(key, false);
  72. }, this);
  73. },
  74. showAddPopup: function () {
  75. this.showPopup(Em.I18n.t('mirroring.dataset.newDataset'));
  76. this.set('isEdit', false);
  77. },
  78. showEditPopup: function () {
  79. this.showPopup(Em.I18n.t('mirroring.dataset.editDataset'));
  80. this.set('isEdit', true);
  81. },
  82. showPopup: function (header) {
  83. var self = this;
  84. App.ModalPopup.show({
  85. classNames: ['sixty-percent-width-modal'],
  86. header: header,
  87. primary: Em.I18n.t('mirroring.dataset.save'),
  88. secondary: Em.I18n.t('common.cancel'),
  89. showCloseButton: false,
  90. saveDisabled: function () {
  91. return self.get('saveDisabled');
  92. }.property('App.router.' + self.get('name') + '.saveDisabled'),
  93. disablePrimary: function () {
  94. return this.get('saveDisabled');
  95. }.property('saveDisabled'),
  96. onPrimary: function () {
  97. // Apply form validation for first click
  98. if (!this.get('primaryWasClicked')) {
  99. this.toggleProperty('primaryWasClicked');
  100. self.applyValidation();
  101. if (this.get('saveDisabled')) {
  102. return false;
  103. }
  104. }
  105. self.save();
  106. this.hide();
  107. App.router.transitionTo('main.mirroring.index');
  108. },
  109. primaryWasClicked: false,
  110. onSecondary: function () {
  111. this.hide();
  112. App.router.send('gotoShowJobs');
  113. },
  114. bodyClass: App.MainMirroringEditDataSetView.extend({
  115. controller: self
  116. })
  117. });
  118. },
  119. // Set observer to call validate method if any property from formFields will change
  120. applyValidation: function () {
  121. Em.keys(this.get('formFields')).forEach(function (key) {
  122. this.addObserver('formFields.' + key, this, 'validate');
  123. }, this);
  124. this.validate();
  125. },
  126. // Return date object calculated from appropriate fields
  127. scheduleStartDate: function () {
  128. var startDate = this.get('formFields.datasetStartDate');
  129. var hoursForStart = this.get('formFields.hoursForStart');
  130. var minutesForStart = this.get('formFields.minutesForStart');
  131. var middayPeriodForStart = this.get('formFields.middayPeriodForStart');
  132. if (startDate && hoursForStart && minutesForStart && middayPeriodForStart) {
  133. return new Date(startDate + ' ' + hoursForStart + ':' + minutesForStart + ' ' + middayPeriodForStart);
  134. }
  135. return null;
  136. }.property('formFields.datasetStartDate', 'formFields.hoursForStart', 'formFields.minutesForStart', 'formFields.middayPeriodForStart'),
  137. // Return date object calculated from appropriate fields
  138. scheduleEndDate: function () {
  139. var endDate = this.get('formFields.datasetEndDate');
  140. var hoursForEnd = this.get('formFields.hoursForEnd');
  141. var minutesForEnd = this.get('formFields.minutesForEnd');
  142. var middayPeriodForEnd = this.get('formFields.middayPeriodForEnd');
  143. if (endDate && hoursForEnd && minutesForEnd && middayPeriodForEnd) {
  144. return new Date(endDate + ' ' + hoursForEnd + ':' + minutesForEnd + ' ' + middayPeriodForEnd);
  145. }
  146. return null;
  147. }.property('formFields.datasetEndDate', 'formFields.hoursForEnd', 'formFields.minutesForEnd', 'formFields.middayPeriodForEnd'),
  148. // Validation for every field in Edit DataSet form
  149. validate: function () {
  150. var formFields = this.get('formFields');
  151. var errors = this.get('errors');
  152. var errorMessages = this.get('errorMessages');
  153. this.clearErrors();
  154. // Check if feild is empty
  155. Em.keys(errorMessages).forEach(function (key) {
  156. if (!formFields.get('dataset' + key.capitalize())) {
  157. errors.set('is' + key.capitalize() + 'Error', true);
  158. errorMessages.set(key, Em.I18n.t('mirroring.required.error'));
  159. }
  160. }, this);
  161. // Check that endDate is after startDate
  162. var scheduleStartDate = this.get('scheduleStartDate');
  163. var scheduleEndDate = this.get('scheduleEndDate');
  164. if (scheduleStartDate && scheduleEndDate && (scheduleStartDate > scheduleEndDate)) {
  165. errors.set('isEndDateError', true);
  166. errorMessages.set('endDate', Em.I18n.t('mirroring.dateOrder.error'));
  167. }
  168. // Check that startDate is after current date
  169. if (!this.get('isEdit') && new Date(App.dateTime()) > scheduleStartDate) {
  170. errors.set('isStartDateError', true);
  171. errorMessages.set('startDate', Em.I18n.t('mirroring.startDate.error'));
  172. }
  173. // Check that repeat field value consists only from digits
  174. if (isNaN(this.get('formFields.datasetFrequency'))) {
  175. errors.set('isFrequencyError', true);
  176. errorMessages.set('frequency', Em.I18n.t('mirroring.required.invalidNumberError'));
  177. }
  178. },
  179. // Add '0' for numbers less than 10
  180. addZero: function (number) {
  181. return ('0' + number).slice(-2);
  182. },
  183. // Convert date to TZ format
  184. toTZFormat: function (date) {
  185. return date.toISOString().replace(/\:\d{2}\.\d{3}/,'');
  186. },
  187. // Converts hours value from 24-hours format to AM/PM format
  188. toAMPMHours: function (hours) {
  189. var result = hours % 12;
  190. result = result ? result : 12;
  191. return this.addZero(result);
  192. },
  193. save: function () {
  194. var datasetName = this.get('formFields.datasetName');
  195. var sourceCluster = App.get('clusterName');
  196. var targetCluster = this.get('formFields.datasetTargetClusterName');
  197. var sourceDir = this.get('formFields.datasetSourceDir');
  198. var targetDir = this.get('formFields.datasetTargetDir');
  199. var datasetFrequency = this.get('formFields.datasetFrequency');
  200. var repeatOptionSelected = this.get('formFields.repeatOptionSelected');
  201. var startDate = this.get('scheduleStartDate');
  202. var endDate = this.get('scheduleEndDate');
  203. var scheduleStartDateFormatted = this.toTZFormat(startDate);
  204. var scheduleEndDateFormatted = this.toTZFormat(endDate);
  205. // Compose XML data, that will be sended to server
  206. var dataToSend = '<?xml version="1.0"?><feed description="" name="' + datasetName + '" xmlns="uri:falcon:feed:0.1"><frequency>' + repeatOptionSelected + '(' + datasetFrequency + ')' +
  207. '</frequency><clusters><cluster name="' + sourceCluster + '" type="source"><validity start="' + scheduleStartDateFormatted + '" end="' + scheduleEndDateFormatted +
  208. '"/><retention limit="days(7)" action="delete"/></cluster><cluster name="' + targetCluster + '" type="target"><validity start="' + scheduleStartDateFormatted + '" end="' + scheduleEndDateFormatted +
  209. '"/><retention limit="months(1)" action="delete"/><locations><location type="data" path="' + targetDir + '" /></locations></cluster></clusters><locations><location type="data" path="' +
  210. sourceDir + '" /></locations><ACL owner="hue" group="users" permission="0755" /><schema location="/none" provider="none"/></feed>';
  211. if (this.get('isEdit')) {
  212. App.ajax.send({
  213. name: 'mirroring.update_entity',
  214. sender: this,
  215. data: {
  216. name: datasetName,
  217. type: 'feed',
  218. entity: dataToSend,
  219. falconServer: App.get('falconServerURL')
  220. },
  221. success: 'onSaveSuccess',
  222. error: 'onSaveError'
  223. });
  224. } else {
  225. // Send request to server to create dataset
  226. App.ajax.send({
  227. name: 'mirroring.create_new_dataset',
  228. sender: this,
  229. data: {
  230. dataset: dataToSend,
  231. falconServer: App.get('falconServerURL')
  232. },
  233. success: 'onSaveSuccess',
  234. error: 'onSaveError'
  235. });
  236. }
  237. var newDataset = {
  238. id: datasetName,
  239. name: datasetName,
  240. source_cluster_name: sourceCluster,
  241. target_cluster_name: targetCluster,
  242. source_dir: sourceDir,
  243. target_dir: targetDir,
  244. dataset_jobs: []
  245. };
  246. App.store.load(App.Dataset, newDataset);
  247. },
  248. onSaveSuccess: function () {
  249. App.router.send('gotoShowJobs');
  250. App.router.get('mainMirroringController').loadData();
  251. },
  252. onSaveError: function () {
  253. console.error('Error in sending new dataset data to server.');
  254. },
  255. saveDisabled: function () {
  256. var errors = this.get('errors');
  257. return errors.get('isNameError') || errors.get('isSourceDirError') || errors.get('isTargetDirError') || errors.get('isStartDateError') || errors.get('isEndDateError') || errors.get('isFrequencyError') || errors.get('isTargetClusterNameError');
  258. }.property('errors.isNameError', 'errors.isSourceDirError', 'errors.isTargetDirError', 'errors.isStartDateError', 'errors.isEndDateError', 'errors.isFrequencyError', 'errors.isTargetClusterNameError')
  259. });