EnvironmentEventHandlers.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2018 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "EnvironmentEventHandlers.hpp"
  14. #include "EnvironmentNode.hpp"
  15. #include "EnvironmentValue.hpp"
  16. void MatchEnvironmentEventHandler::processEvent(const std::string &eventType, std::shared_ptr<EnvironmentNode> pEventNode)
  17. {
  18. if (m_eventType == eventType && pEventNode->getSchemaItem()->getItemType() == m_itemType)
  19. {
  20. //
  21. // If an event node attribute was defined, go check it
  22. if (!m_eventNodeAttribute.empty())
  23. {
  24. //
  25. // We need to check aginst an attribute in the event node. Build a list of comparison nodes using the
  26. // target path
  27. std::vector<std::shared_ptr<EnvironmentNode>> matchNodes;
  28. pEventNode->fetchNodes(m_targetPath, matchNodes);
  29. for (auto &nodeIt: matchNodes)
  30. {
  31. std::shared_ptr<EnvironmentValue> pItemAttr = pEventNode->getAttribute(m_targetAttribute);
  32. if (pItemAttr)
  33. {
  34. std::shared_ptr<EnvironmentValue> pMatchAttr = nodeIt->getAttribute(m_targetAttribute);
  35. if (pMatchAttr)
  36. {
  37. if (pMatchAttr->getValue() == pItemAttr->getValue())
  38. {
  39. doHandleEvent(pEventNode);
  40. }
  41. }
  42. }
  43. }
  44. }
  45. else
  46. {
  47. doHandleEvent(pEventNode);
  48. }
  49. }
  50. }
  51. void MatchEnvironmentEventHandler::setEventNodeAttributeName(const std::string &name)
  52. {
  53. m_eventNodeAttribute = name;
  54. if (m_targetAttribute.empty())
  55. {
  56. m_targetAttribute = name;
  57. }
  58. }
  59. void AttributeDependencyCreateEventHandler::addDependency(const std::string &attrName, const std::string &attrVal, const std::string &depAttr, const std::string &depVal)
  60. {
  61. auto valSetMapIt = m_depAttrVals.find(attrName);
  62. if (valSetMapIt == m_depAttrVals.end())
  63. {
  64. std::map<std::string, std::pair<std::string, std::string>> keyValMap;
  65. m_depAttrVals[attrName] = keyValMap;
  66. }
  67. m_depAttrVals[attrName][attrVal] = std::pair<std::string, std::string>(depAttr, depVal);
  68. }
  69. void AttributeDependencyCreateEventHandler::doHandleEvent(std::shared_ptr<EnvironmentNode> pEventNode)
  70. {
  71. for (auto attrIt = m_depAttrVals.begin(); attrIt != m_depAttrVals.end(); ++attrIt)
  72. {
  73. std::shared_ptr<EnvironmentValue> pAttr = pEventNode->getAttribute(attrIt->first);
  74. if (pAttr && pAttr->getSchemaValue()->getType()->isEnumerated())
  75. {
  76. for (auto valueIt = attrIt->second.begin(); valueIt != attrIt->second.end(); ++valueIt)
  77. {
  78. pAttr->getSchemaValue()->getType()->getLimits()->addDependentAttributeValue(valueIt->first, valueIt->second.first, valueIt->second.second);
  79. }
  80. }
  81. }
  82. }
  83. void InsertEnvironmentDataCreateEventHandler::doHandleEvent(std::shared_ptr<EnvironmentNode> pEventNode)
  84. {
  85. pEventNode->addEnvironmentInsertData(m_envData);
  86. }
  87. void AttributeSetValueCreateEventHandler::addAttributeValue(const std::string &attrName, const std::string &attrVal)
  88. {
  89. m_attrVals.push_back(NameValue(attrName, attrVal));
  90. }
  91. void AttributeSetValueCreateEventHandler::doHandleEvent(std::shared_ptr<EnvironmentNode> pEventNode)
  92. {
  93. for (auto &attrValPair : m_attrVals)
  94. {
  95. std::shared_ptr<EnvironmentValue> pAttr = pEventNode->getAttribute(attrValPair.name);
  96. if (pAttr)
  97. {
  98. pAttr->setValue(attrValPair.value, nullptr);
  99. }
  100. }
  101. }