edit_dataset_controller.js 9.4 KB

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