server_data_mapper.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. var App = require('app');
  19. var stringUtils = require('utils/string_utils');
  20. App.ServerDataMapper = Em.Object.extend({
  21. jsonKey: false,
  22. map: function (json) {
  23. if (json) {
  24. var model = this.get('model');
  25. var jsonKey = this.get('jsonKey');
  26. if (jsonKey && json[jsonKey]) { // if data come as { hdfs: {...} }
  27. json = json[jsonKey];
  28. }
  29. $.each(json, function (field, value) {
  30. model.set(field, value);
  31. })
  32. }
  33. }
  34. });
  35. App.QuickDataMapper = App.ServerDataMapper.extend({
  36. config: {},
  37. model: null,
  38. map: function (json) {
  39. if (!this.get('model')) {
  40. return;
  41. }
  42. if (json.items) {
  43. var result = [];
  44. json.items.forEach(function (item) {
  45. result.push(this.parseIt(item, this.config));
  46. }, this)
  47. //console.log(this.get('model'), result);
  48. App.store.loadMany(this.get('model'), result);
  49. }
  50. },
  51. parseIt: function (data, config) {
  52. var result = {};
  53. for ( var i in config) {
  54. if (i.substr(0, 1) === '$') {
  55. i = i.substr(1, i.length);
  56. result[i] = config['$' + i];
  57. } else {
  58. var isSpecial = false;
  59. if (i.substr(-5) == '_type') {
  60. var prefix = i.substr(0, i.length - 5);
  61. isSpecial = config[prefix + '_key'] != null;
  62. } else if (i.substr(-4) == '_key') {
  63. var prefix = i.substr(0, i.length - 4);
  64. isSpecial = config[prefix + '_type'] != null;
  65. }
  66. if (!isSpecial && typeof config[i] == 'string') {
  67. result[i] = this.getJsonProperty(data, config[i]);
  68. } else if (typeof config[i] == 'object') {
  69. result[i] = [];
  70. var _data = this.getJsonProperty(data, config[i + '_key']);
  71. var _type = config[i + '_type'];
  72. var l = _data.length;
  73. for ( var index = 0; index < l; index++) {
  74. if (_type == 'array') {
  75. result[i].push(this.getJsonProperty(_data[index], config[i].item));
  76. } else {
  77. result[i].push(this.parseIt(_data[index], config[i]));
  78. }
  79. }
  80. }
  81. }
  82. }
  83. return result;
  84. },
  85. getJsonProperty: function (json, path) {
  86. var pathArr = path.split('.');
  87. var current = json;
  88. while (pathArr.length && current) {
  89. if (pathArr[0].substr(-1) == ']') {
  90. var index = parseInt(pathArr[0].substr(-2, 1));
  91. var attr = pathArr[0].substr(0, pathArr[0].length - 3);
  92. if (attr in current) {
  93. current = current[attr][index];
  94. }
  95. } else {
  96. current = current[pathArr[0]];
  97. }
  98. pathArr.splice(0, 1);
  99. }
  100. return current;
  101. },
  102. calculateState: function (json) {
  103. var stateEqual = (json.desired_status != json.work_status);
  104. if (stateEqual) {
  105. if (json.desired_status == 'STARTED' && json.work_status == 'INSTALLED') {
  106. json.work_status = 'STARTING';
  107. } else if (json.desired_status == 'INSTALLED' && json.work_status == 'STARTED') {
  108. json.work_status = 'STOPPING';
  109. }
  110. }
  111. return json;
  112. },
  113. /**
  114. * update fields in record
  115. * @param record
  116. * @param json
  117. * @param fieldsNotUpdate
  118. */
  119. updateRecord: function (record, json, fieldsNotUpdate) {
  120. for (var field in json) {
  121. if (json[field] !== undefined && !fieldsNotUpdate.contains(field)) {
  122. if(json[field] instanceof Array){
  123. this.updateHasMany(record, stringUtils.underScoreToCamelCase(field), json[field]);
  124. } else {
  125. record.set(stringUtils.underScoreToCamelCase(field), json[field]);
  126. }
  127. }
  128. }
  129. },
  130. /**
  131. * update fields with hasMany type
  132. * @param record
  133. * @param field
  134. * @param items
  135. */
  136. updateHasMany: function(record, field, items ){
  137. var hasMany = record.get(field);
  138. hasMany.clear();
  139. items.forEach(function (item) {
  140. hasMany.pushObject(hasMany.type.find(item));
  141. });
  142. }
  143. });