alert_notification_mapper.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. var App = require('app');
  18. App.alertNotificationMapper = App.QuickDataMapper.create({
  19. model: App.AlertNotification,
  20. config: {
  21. id: 'AlertTarget.id',
  22. name: 'AlertTarget.name',
  23. type: 'AlertTarget.notification_type',
  24. description: 'AlertTarget.description',
  25. global: 'AlertTarget.global'
  26. },
  27. map: function (json) {
  28. if (json.items) {
  29. var result = [];
  30. var notificationsProperties = {};
  31. var notificationsAlertStates = {};
  32. var groupsMap = App.cache['alertNotificationsGroupsMap'];
  33. json.items.forEach(function (item) {
  34. var notification = this.parseIt(item, this.config);
  35. var groups = groupsMap && groupsMap[notification.id];
  36. if (groups) {
  37. notification.groups = groups;
  38. }
  39. result.push(notification);
  40. notificationsProperties[item.AlertTarget.id] = item.AlertTarget.properties;
  41. notificationsAlertStates[item.AlertTarget.id] = item.AlertTarget.alert_states;
  42. }, this);
  43. App.store.loadMany(this.get('model'), result);
  44. this.setProperties('properties', notificationsProperties);
  45. this.setProperties('alertStates', notificationsAlertStates);
  46. }
  47. },
  48. /**
  49. * Set values from <code>propertyMap</code> for <code>propertyName</code> for each record in model
  50. * @param propertyName
  51. * @param propertiesMap record_id to value map
  52. */
  53. setProperties: function (propertyName, propertiesMap) {
  54. var modelRecords = this.get('model').find();
  55. for (var recordId in propertiesMap) {
  56. if (propertiesMap.hasOwnProperty(recordId)) {
  57. modelRecords.findProperty('id', +recordId).set(propertyName, propertiesMap[recordId]);
  58. }
  59. }
  60. }
  61. });