edit_dataset_controller.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. datasetType: null,
  25. datasetTargetClusterName: null,
  26. datasetSourceDir: null,
  27. datasetTargetDir: null,
  28. datasetStartDate: null,
  29. hoursForStart: null,
  30. minutesForStart: null,
  31. middayPeriodForStart: null,
  32. datasetEndDate: null,
  33. hoursForEnd: null,
  34. minutesForEnd: null,
  35. middayPeriodForEnd: null,
  36. datasetFrequency: null,
  37. repeatOptionSelected: null
  38. }),
  39. // Messages for errors occurred during Edit DataSet form validation
  40. errorMessages: Ember.Object.create({
  41. name: '',
  42. sourceDir: '',
  43. targetDir: '',
  44. startDate: '',
  45. endDate: '',
  46. frequency: ''
  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. }),
  56. clearStep: function () {
  57. var formFields = this.get('formFields');
  58. Em.keys(formFields).forEach(function (key) {
  59. formFields.set(key, null);
  60. }, this);
  61. this.clearErrors();
  62. },
  63. clearErrors: function () {
  64. var errorMessages = this.get('errorMessages');
  65. Em.keys(errorMessages).forEach(function (key) {
  66. errorMessages.set(key, '');
  67. }, this);
  68. var errors = this.get('errors');
  69. Em.keys(errors).forEach(function (key) {
  70. errors.set(key, false);
  71. }, this);
  72. },
  73. showAddPopup: function () {
  74. this.showPopup(Em.I18n.t('mirroring.dataset.newDataset'));
  75. this.set('isEdit', false);
  76. },
  77. showEditPopup: function () {
  78. this.showPopup(Em.I18n.t('mirroring.dataset.editDataset'));
  79. this.set('isEdit', true);
  80. },
  81. showPopup: function (header) {
  82. var self = this;
  83. App.ModalPopup.show({
  84. classNames: ['sixty-percent-width-modal'],
  85. header: header,
  86. primary: Em.I18n.t('mirroring.dataset.save'),
  87. secondary: Em.I18n.t('common.cancel'),
  88. showCloseButton: false,
  89. saveDisabled: function () {
  90. return !self.get('saveDisabled');
  91. }.property('App.router.' + self.get('name') + '.saveDisabled'),
  92. enablePrimary: function () {
  93. return this.get('saveDisabled');
  94. }.property('saveDisabled'),
  95. onPrimary: function () {
  96. if (!this.get('saveDisabled')) {
  97. return false;
  98. }
  99. // Apply form validation for first click
  100. if (!this.get('primaryWasClicked')) {
  101. this.toggleProperty('primaryWasClicked');
  102. self.applyValidation();
  103. if (!this.get('saveDisabled')) {
  104. return false;
  105. }
  106. }
  107. self.save();
  108. this.hide();
  109. App.router.transitionTo('main.mirroring.index');
  110. },
  111. primaryWasClicked: false,
  112. onSecondary: function () {
  113. this.hide();
  114. App.router.send('gotoShowJobs');
  115. },
  116. bodyClass: App.MainMirroringEditDataSetView.extend({
  117. controller: self
  118. })
  119. });
  120. },
  121. // Set observer to call validate method if any property from formFields will change
  122. applyValidation: function () {
  123. Em.keys(this.get('formFields')).forEach(function (key) {
  124. this.addObserver('formFields.' + key, this, 'validate');
  125. }, this);
  126. this.validate();
  127. },
  128. // Return date object calculated from appropriate fields
  129. scheduleStartDate: function () {
  130. var startDate = this.get('formFields.datasetStartDate');
  131. var hoursForStart = this.get('formFields.hoursForStart');
  132. var minutesForStart = this.get('formFields.minutesForStart');
  133. var middayPeriodForStart = this.get('formFields.middayPeriodForStart');
  134. if (startDate && hoursForStart && minutesForStart && middayPeriodForStart) {
  135. return new Date(startDate + ' ' + hoursForStart + ':' + minutesForStart + ' ' + middayPeriodForStart);
  136. }
  137. return null;
  138. }.property('formFields.datasetStartDate', 'formFields.hoursForStart', 'formFields.minutesForStart', 'formFields.middayPeriodForStart'),
  139. // Return date object calculated from appropriate fields
  140. scheduleEndDate: function () {
  141. var endDate = this.get('formFields.datasetEndDate');
  142. var hoursForEnd = this.get('formFields.hoursForEnd');
  143. var minutesForEnd = this.get('formFields.minutesForEnd');
  144. var middayPeriodForEnd = this.get('formFields.middayPeriodForEnd');
  145. if (endDate && hoursForEnd && minutesForEnd && middayPeriodForEnd) {
  146. return new Date(endDate + ' ' + hoursForEnd + ':' + minutesForEnd + ' ' + middayPeriodForEnd);
  147. }
  148. return null;
  149. }.property('formFields.datasetEndDate', 'formFields.hoursForEnd', 'formFields.minutesForEnd', 'formFields.middayPeriodForEnd'),
  150. // Validation for every field in Edit DataSet form
  151. validate: function () {
  152. var formFields = this.get('formFields');
  153. var errors = this.get('errors');
  154. var errorMessages = this.get('errorMessages');
  155. this.clearErrors();
  156. // Check if feild is empty
  157. Em.keys(errorMessages).forEach(function (key) {
  158. if (!formFields.get('dataset' + key.capitalize())) {
  159. errors.set('is' + key.capitalize() + 'Error', true);
  160. errorMessages.set(key, Em.I18n.t('mirroring.required.error'));
  161. }
  162. }, this);
  163. // Check that endDate is after startDate
  164. var scheduleStartDate = this.get('scheduleStartDate');
  165. var scheduleEndDate = this.get('scheduleEndDate');
  166. if (scheduleStartDate && scheduleEndDate && (scheduleStartDate > scheduleEndDate)) {
  167. errors.set('isEndDateError', true);
  168. errorMessages.set('endDate', Em.I18n.t('mirroring.dateOrder.error'));
  169. }
  170. // Check that repeat field value consists only from digits
  171. if (isNaN(this.get('formFields.datasetFrequency'))) {
  172. errors.set('isFrequencyError', true);
  173. errorMessages.set('frequency', Em.I18n.t('mirroring.required.invalidNumberError'));
  174. }
  175. },
  176. // Add '0' for numbers less than 10
  177. addZero: function (number) {
  178. return ('0' + number).slice(-2);
  179. },
  180. // Convert date to TZ format
  181. toTZFormat: function (date) {
  182. return date.getFullYear() + '-' + this.addZero(date.getMonth() + 1) + '-' + this.addZero(date.getDate()) + 'T' + this.addZero(date.getHours()) + ':' + this.addZero(date.getMinutes()) + 'Z';
  183. },
  184. // Converts hours value from 24-hours format to AM/PM format
  185. toAMPMHours: function (hours) {
  186. var result = hours % 12;
  187. result = result ? result : 12;
  188. return this.addZero(result);
  189. },
  190. save: function () {
  191. var datasetName = this.get('formFields.datasetName');
  192. var sourceCluster = App.get('clusterName');
  193. var targetCluster = this.get('formFields.datasetTargetClusterName');
  194. var sourceDir = this.get('formFields.datasetSourceDir');
  195. var targetDir = this.get('formFields.datasetTargetDir');
  196. var datasetFrequency = this.get('formFields.datasetFrequency');
  197. var repeatOptionSelected = this.get('formFields.repeatOptionSelected');
  198. var startDate = this.get('scheduleStartDate');
  199. var endDate = this.get('scheduleEndDate');
  200. var scheduleStartDateFormatted = this.toTZFormat(startDate);
  201. var scheduleEndDateFormatted = this.toTZFormat(endDate);
  202. // Compose XML data, that will be sended to server
  203. var dataToSend = '<?xml version="1.0"?><feed description="" name="' + datasetName + '" xmlns="uri:falcon:feed:0.1"><frequency>' + repeatOptionSelected + '(' + datasetFrequency + ')' +
  204. '</frequency><clusters><cluster name="' + targetCluster + '" type="source"><validity start="' + scheduleStartDateFormatted + '" end="' + scheduleEndDateFormatted +
  205. '"/><retention limit="days(7)" action="delete"/></cluster><cluster name="' + targetCluster + '" type="target"><validity start="' + scheduleStartDateFormatted + '" end="' +
  206. '"/><retention limit="months(1)" action="delete"/><locations><location type="data" path="' + targetDir + '" /></locations></cluster></clusters><locations><location type="data" path="' +
  207. sourceDir + '" /></locations><ACL owner="hue" group="users" permission="0755" /><schema location="/none" provider="none"/></feed>';
  208. // Send request to server to create dataset
  209. App.ajax.send({
  210. name: 'mirroring.create_new_dataset',
  211. sender: this,
  212. data: {
  213. dataset: dataToSend
  214. },
  215. error: 'onCreateNewDatasetError'
  216. });
  217. var newDataset = {
  218. id: datasetName,
  219. name: datasetName,
  220. source_cluster_name: sourceCluster,
  221. target_cluster_name: targetCluster,
  222. source_dir: sourceDir,
  223. target_dir: targetDir,
  224. dataset_jobs: []
  225. };
  226. App.store.load(App.Dataset, newDataset);
  227. },
  228. onCreateNewDatasetError: function () {
  229. console.error('Error in sending new dataset data to server.');
  230. },
  231. saveDisabled: function () {
  232. var errors = this.get('errors');
  233. return errors.get('isNameError') || errors.get('isSourceDirError') || errors.get('isTargetDirError') || errors.get('isStartDateError') || errors.get('isEndDateError') || errors.get('isFrequencyError');
  234. }.property('errors.isNameError', 'errors.isSourceDirError', 'errors.isTargetDirError', 'errors.isStartDateError', 'errors.isEndDateError', 'errors.isFrequencyError')
  235. });