EnvironmentValue.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. else if (m_pSchemaValue->isRequired())
  74. {
  75. status.addMsg(statusMsg::error, myId, m_name, "Required value has not been set");
  76. }
  77. }
  78. void EnvironmentValue::initialize()
  79. {
  80. //
  81. // Is there an auto generated value we should create?
  82. const std::string &type = m_pSchemaValue->getAutoGenerateType();
  83. if (!type.empty())
  84. {
  85. //
  86. // type "prefix" means to use the auto generate value as a name prefix and to append numbers until a new unique name is
  87. // found. ("prefix_" is a variation that adds an underbar (_) when appending numbers)
  88. if (type == "prefix" || type=="prefix_")
  89. {
  90. std::string connector = (type == "prefix_") ? "_" : "";
  91. std::string newName;
  92. const std::string &prefix = m_pSchemaValue->getAutoGenerateValue();
  93. std::vector<std::string> curValues;
  94. m_pMyEnvNode.lock()->getAttributeValueForAllSiblings(m_name, curValues);
  95. size_t count = curValues.size();
  96. newName = prefix;
  97. size_t n = 0;
  98. while (n <= count + 1)
  99. {
  100. bool found = false;
  101. for (auto it = curValues.begin(); it != curValues.end() && !found; ++it)
  102. {
  103. if ((*it) == newName)
  104. found = true;
  105. }
  106. if (!found)
  107. {
  108. setValue(newName, nullptr);
  109. break;
  110. }
  111. ++n;
  112. newName = prefix + connector + std::to_string(n);
  113. }
  114. }
  115. //
  116. // If type is configProperty, then the autogenerated value is the taken from the value of the node's indicated property
  117. else if (type == "configProperty")
  118. {
  119. const std::string &propertyName = m_pSchemaValue->getAutoGenerateValue();
  120. std::string value = m_pMyEnvNode.lock()->getSchemaItem()->getProperty(propertyName);
  121. setValue(value, nullptr);
  122. }
  123. //
  124. // Fixed value?
  125. else if (type == "fixedValue")
  126. {
  127. setValue(m_pSchemaValue->getAutoGenerateValue(), nullptr);
  128. }
  129. }
  130. }
  131. std::string EnvironmentValue::getNodeId() const
  132. {
  133. return m_pMyEnvNode.lock()->getId();
  134. }