edit_dataset_controller.js 10 KB

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