edit_dataset_controller.js 11 KB

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