EnvironmentValue.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2017 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 "EnvironmentValue.hpp"
  14. #include "EnvironmentNode.hpp"
  15. bool EnvironmentValue::setValue(const std::string &value, Status *pStatus, bool forceSet)
  16. {
  17. bool rc = true;
  18. std::string oldValue = m_value;
  19. if (m_pSchemaValue)
  20. {
  21. m_forcedSet = false;
  22. if (m_pSchemaValue->isValueValid(value))
  23. {
  24. m_value = value;
  25. }
  26. else if (forceSet)
  27. {
  28. m_value = value;
  29. m_forcedSet = true;
  30. if (pStatus != nullptr)
  31. {
  32. std::string msg = "Attribute forced to invalid value (" + m_pSchemaValue->getType()->getLimitString() + ")";
  33. pStatus->addMsg(statusMsg::info, m_pMyEnvNode.lock()->getId(), m_name, msg);
  34. }
  35. }
  36. else
  37. {
  38. rc = false;
  39. if (pStatus != nullptr)
  40. {
  41. std::string msg = "Value not set. New value(" + value + ") not valid (" + m_pSchemaValue->getType()->getLimitString() + ")";
  42. pStatus->addMsg(statusMsg::error, m_pMyEnvNode.lock()->getId(), m_name, msg);
  43. }
  44. }
  45. if (rc)
  46. m_pSchemaValue->mirrorValueToEnvironment(oldValue, value);
  47. }
  48. return rc;
  49. }
  50. void EnvironmentValue::getAllValuesForSiblings(std::vector<std::string> &result) const
  51. {
  52. std::shared_ptr<EnvironmentNode> pEnvNode = m_pMyEnvNode.lock();
  53. return pEnvNode->getAttributeValueForAllSiblings(m_pSchemaValue->getName(), result);
  54. }
  55. bool EnvironmentValue::isValueValid(const std::string &value) const
  56. {
  57. bool rc = m_pSchemaValue->isValueValid(value, this);
  58. return rc;
  59. }
  60. void EnvironmentValue::validate(Status &status, const std::string &myId) const
  61. {
  62. //
  63. // Only validate if value is set, otherwise, it's valid
  64. if (isValueSet())
  65. {
  66. if (!m_pSchemaValue->isDefined())
  67. status.addMsg(statusMsg::warning, myId, m_name, "No type information exists");
  68. if (m_forcedSet)
  69. status.addMsg(statusMsg::warning, myId, m_name, "Current value was force set to an invalid value");
  70. // Will generate status based on current value and type
  71. m_pSchemaValue->validate(status, myId, this);
  72. }
  73. }
  74. // Called when a new value has been created, not read from existing environment, but created and added
  75. void EnvironmentValue::initialize()
  76. {
  77. //
  78. // Is there an auto generated value we should process:
  79. const std::string &type = m_pSchemaValue->getAutoGenerateType();
  80. if (!type.empty())
  81. {
  82. //
  83. // type "prefix" means to use the auto generate value as a name prefix and to append numbers until a new unique name is
  84. // found
  85. if (type == "prefix")
  86. {
  87. std::string newName;
  88. const std::string &prefix = m_pSchemaValue->getAutoGenerateValue();
  89. std::vector<std::string> curValues;
  90. m_pMyEnvNode.lock()->getAttributeValueForAllSiblings(m_name, curValues);
  91. size_t count = curValues.size();
  92. newName = prefix;
  93. size_t n = 0;
  94. while (n <= count + 1)
  95. {
  96. bool found = false;
  97. for (auto it = curValues.begin(); it != curValues.end() && !found; ++it)
  98. {
  99. if ((*it) == newName)
  100. found = true;
  101. }
  102. if (!found)
  103. {
  104. setValue(newName, nullptr);
  105. break;
  106. }
  107. ++n;
  108. newName = prefix + std::to_string(n);
  109. }
  110. }
  111. //
  112. // If type is configProperty, then the autogenerate value is the name of the value's node's schema item
  113. // property. Set the value to this
  114. else if (type == "configProperty")
  115. {
  116. const std::string &propertyName = m_pSchemaValue->getAutoGenerateValue();
  117. std::string value;
  118. value = m_pMyEnvNode.lock()->getSchemaItem()->getProperty(propertyName);
  119. setValue(value, nullptr);
  120. }
  121. }
  122. }
  123. std::string EnvironmentValue::getNodeId() const
  124. {
  125. return m_pMyEnvNode.lock()->getId();
  126. }