EnvironmentValue.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. EnvironmentValue::~EnvironmentValue()
  16. {
  17. //
  18. // Tell the schema vallue that we are going away
  19. m_pSchemaValue->removeEnvironmentValue(shared_from_this());
  20. }
  21. bool EnvironmentValue::setValue(const std::string &value, Status *pStatus, bool forceSet)
  22. {
  23. bool rc = true;
  24. std::string oldValue = m_value;
  25. if (m_pSchemaValue)
  26. {
  27. m_forcedSet = false;
  28. if (m_pSchemaValue->isValueValid(value))
  29. {
  30. m_value = value;
  31. }
  32. else if (forceSet)
  33. {
  34. m_value = value;
  35. m_forcedSet = true;
  36. if (pStatus != nullptr)
  37. {
  38. std::string msg = "Attribute forced to invalid value (" + m_pSchemaValue->getType()->getLimitString() + ")";
  39. pStatus->addMsg(statusMsg::info, m_pMyEnvNode.lock()->getId(), m_name, msg);
  40. }
  41. }
  42. else
  43. {
  44. rc = false;
  45. if (pStatus != nullptr)
  46. {
  47. std::string msg = "Value not set. New value(" + value + ") not valid (" + m_pSchemaValue->getType()->getLimitString() + ")";
  48. pStatus->addMsg(statusMsg::error, m_pMyEnvNode.lock()->getId(), m_name, msg);
  49. }
  50. }
  51. if (rc)
  52. m_pSchemaValue->mirrorValueToEnvironment(oldValue, value, pStatus);
  53. }
  54. return rc;
  55. }
  56. void EnvironmentValue::getAllValuesForSiblings(std::vector<std::string> &result) const
  57. {
  58. std::shared_ptr<EnvironmentNode> pEnvNode = m_pMyEnvNode.lock();
  59. return pEnvNode->getAttributeValueForAllSiblings(m_pSchemaValue->getName(), result);
  60. }
  61. bool EnvironmentValue::isValueValid(const std::string &value) const
  62. {
  63. bool rc = m_pSchemaValue->isValueValid(value, this);
  64. return rc;
  65. }
  66. void EnvironmentValue::validate(Status &status, const std::string &myId) const
  67. {
  68. //
  69. // Only validate if value is set, otherwise, it's valid
  70. if (isValueSet())
  71. {
  72. if (!m_pSchemaValue->isDefined())
  73. status.addMsg(statusMsg::warning, myId, m_name, "No type information exists");
  74. if (m_forcedSet)
  75. status.addMsg(statusMsg::warning, myId, m_name, "Current value was force set to an invalid value");
  76. // Will generate status based on current value and type
  77. m_pSchemaValue->validate(status, myId, this);
  78. }
  79. else
  80. {
  81. if (m_pSchemaValue->isRequired())
  82. {
  83. status.addMsg(statusMsg::error, myId, m_name, "Required value has not been set");
  84. }
  85. if (m_pSchemaValue->hasForcedValue())
  86. {
  87. std::string msg = "No value provided, default value of " + m_pSchemaValue->getForcedValue() + " will be used.";
  88. status.addMsg(statusMsg::warning, myId, m_name, msg);
  89. }
  90. }
  91. }
  92. void EnvironmentValue::initialize()
  93. {
  94. //
  95. // Is there an auto generated value we should create?
  96. const std::string &type = m_pSchemaValue->getAutoGenerateType();
  97. if (!type.empty())
  98. {
  99. //
  100. // type "prefix" means to use the auto generate value as a name prefix and to append numbers until a new unique name is
  101. // found. ("prefix_" is a variation that adds an underbar (_) when appending numbers)
  102. if (type == "prefix" || type=="prefix_")
  103. {
  104. std::string connector = (type == "prefix_") ? "_" : "";
  105. std::string newName;
  106. const std::string &prefix = m_pSchemaValue->getAutoGenerateValue();
  107. std::vector<std::string> curValues;
  108. m_pMyEnvNode.lock()->getAttributeValueForAllSiblings(m_name, curValues);
  109. size_t count = curValues.size();
  110. newName = prefix;
  111. size_t n = 0;
  112. while (n <= count + 1)
  113. {
  114. bool found = false;
  115. for (auto it = curValues.begin(); it != curValues.end() && !found; ++it)
  116. {
  117. if ((*it) == newName)
  118. found = true;
  119. }
  120. if (!found)
  121. {
  122. setValue(newName, nullptr);
  123. break;
  124. }
  125. ++n;
  126. newName = prefix + connector + std::to_string(n);
  127. }
  128. }
  129. //
  130. // If type is configProperty, then the autogenerated value is the taken from the value of the node's indicated property
  131. else if (type == "configProperty")
  132. {
  133. const std::string &propertyName = m_pSchemaValue->getAutoGenerateValue();
  134. std::string value = m_pMyEnvNode.lock()->getSchemaItem()->getProperty(propertyName);
  135. setValue(value, nullptr);
  136. }
  137. //
  138. // Fixed value?
  139. else if (type == "fixedValue")
  140. {
  141. setValue(m_pSchemaValue->getAutoGenerateValue(), nullptr);
  142. }
  143. }
  144. }
  145. std::string EnvironmentValue::getNodeId() const
  146. {
  147. return m_pMyEnvNode.lock()->getId();
  148. }