123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- /*##############################################################################
- HPCC SYSTEMS software Copyright (C) 2015 HPCC Systems®.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ############################################################################## */
- #include "ConfiguratorAPI.hpp"
- #include "ConfiguratorMain.hpp"
- #include "BuildSet.hpp"
- #include "ConfigSchemaHelper.hpp"
- #include "ConfiguratorMain.hpp"
- #include "jstring.hpp"
- #include "SchemaMapManager.hpp"
- #include "SchemaEnumeration.hpp"
- #include "SchemaCommon.hpp"
- #include "ConfiguratorMain.hpp"
- #include "EnvironmentModel.hpp"
- #include "ConfigNotifications.hpp"
- #include <iostream>
- #include "jlog.hpp"
- static int nAllocatedTables = 0;
- static char *modelName[MAX_ARRAY_X];
- const char* getTableDataModelName(int index)
- {
- if (index < nAllocatedTables)
- return modelName[index];
- else
- {
- modelName[index] = new char[MAX_ARRAY_Y];
- sprintf(modelName[index],"tableDataModel%d", index);
- nAllocatedTables++;
- return modelName[index];
- }
- }
- void deleteTableModels()
- {
- while (nAllocatedTables > 0)
- {
- delete[] modelName[nAllocatedTables];
- nAllocatedTables--;
- }
- }
- namespace CONFIGURATOR_API
- {
- using namespace CONFIGURATOR;
- static CConfigSchemaHelper *s_pConfigSchemaHelper = NULL;
- void reload(const char *pFile)
- {
- assert(pFile != NULL && *pFile != 0);
- delete s_pConfigSchemaHelper;
- s_pConfigSchemaHelper = NULL;
- s_pConfigSchemaHelper = CConfigSchemaHelper::getInstance();
- s_pConfigSchemaHelper->populateSchema();
- CConfigSchemaHelper::getInstance()->loadEnvFromConfig(pFile);
- }
- int getNumberOfAvailableComponents()
- {
- assert(s_pConfigSchemaHelper != NULL);
- return CBuildSetManager::getInstance()->getBuildSetComponentCount();
- }
- int getNumberOfAvailableServices()
- {
- assert(s_pConfigSchemaHelper != NULL);
- return CBuildSetManager::getInstance()->getBuildSetServiceCount();
- }
- #ifdef CONFIGURATOR_LIB
- int initialize()
- {
- assert(s_pConfigSchemaHelper == NULL);
- static bool bOnce = true;
- if (bOnce == true)
- {
- bOnce = false;
- InitModuleObjects();
- }
- s_pConfigSchemaHelper = CConfigSchemaHelper::getInstance();
- s_pConfigSchemaHelper->populateSchema();
- return 1;
- }
- #else // CONFIGURATOR_LIB
- int initialize(int argc, char *argv[])
- {
- assert(s_pConfigSchemaHelper == NULL);
- InitModuleObjects();
- s_pConfigSchemaHelper = CConfigSchemaHelper::getInstance();
- return 0;
- }
- #endif // CONFIGURATOR_LIB
- int getValue(const char *pXPath, char *pValue)
- {
- // By Default, return xPath as value.
- if (pXPath == NULL || *pXPath == 0)
- return 0;
- strcpy(pValue, pXPath[0] == '#' ? &(pXPath[1]) : pXPath); // href for frontend
- CAttribute *pAttribute = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getAttributeFromXPath(pXPath);
- if(pAttribute == NULL)
- ::std::cout << "xPath: " << pXPath << "| value: " << pXPath << ::std::endl;
- else if (pAttribute->isInstanceValueValid() == true)
- {
- strcpy(pValue, pAttribute->getInstanceValue());
- ::std::cout << "xPath: " << pXPath << "| value: " << pValue << ::std::endl;
- }
- return 1;
- }
- bool setValue(const char *pXPath, const char *pValue)
- {
- assert(pXPath != NULL && pXPath[0] != 0);
- assert(pValue != NULL);
- StringBuffer strXPath(pXPath);
- strXPath.replace('_','/');
- CAttribute *pAttribute = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getAttributeFromXPath(strXPath.str());
- if (pAttribute == NULL)
- return false;
- assert(pAttribute != NULL);
- pAttribute->setEnvValueFromXML(pValue);
- /*if (strstr(pValue, "/") == NULL)
- {
- strXPath.replace('_','/');
- }*/
- CConfigSchemaHelper::getInstance()->setEnvTreeProp(strXPath.str(), pValue);
- return true;
- }
- int getIndex(const char *pXPath)
- {
- CRestriction *pRestriction = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getRestrictionFromXPath(pXPath);
- assert(pRestriction != NULL);
- assert(pRestriction->getEnumerationArray() != NULL);
- return pRestriction->getEnumerationArray()->getEnvValueNodeIndex();
- }
- void setIndex(const char *pXPath, int newIndex)
- {
- assert(newIndex >= 0);
- CRestriction *pRestriction = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getRestrictionFromXPath(pXPath);
- assert(pRestriction != NULL);
- assert(pRestriction->getEnumerationArray() != NULL);
- pRestriction->getEnumerationArray()->setEnvValueNodeIndex(newIndex);
- CConfigSchemaHelper::getInstance()->setEnvTreeProp(pXPath, pRestriction->getEnumerationArray()->item(newIndex).getValue());
- }
- const char* getTableValue(const char *pXPath, int nRow)
- {
- assert(pXPath != NULL && *pXPath != 0);
- CAttribute *pAttribute = NULL;
- CElement *pElement = NULL;
- if (CConfigSchemaHelper::isXPathTailAttribute(pXPath) == true)
- pAttribute = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getAttributeFromXPath(pXPath);
- if (pAttribute == NULL)
- {
- pElement = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getElementFromXPath(pXPath);
- assert(pElement != NULL);
- return pAttribute->getInstanceValue();
- }
- else
- {
- assert(pAttribute != NULL);
- if (nRow == 1)
- return pAttribute->getInstanceValue();
- else
- {
- StringBuffer strXPath(pXPath);
- const StringBuffer strXPathOriginal(pXPath);
- int offset = strXPathOriginal.length() - (CConfigSchemaHelper::stripXPathIndex(strXPath) + 1) ;
- CConfigSchemaHelper::stripXPathIndex(strXPath);
- strXPath.appendf("[%d]", nRow);
- strXPath.append(strXPathOriginal, offset, strXPathOriginal.length() - offset);
- //confirm the next 2 lines are sufficient
- //CConfigSchemaHelper::stripXPathIndex(strXPath);
- //strXPath.appendf("[%d]", nRow);
- pAttribute = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getAttributeFromXPath(strXPath.str());
- if (STRICTNESS_LEVEL >= DEFAULT_STRICTNESS)
- assert(pAttribute != NULL);
- if (pAttribute == NULL)
- return NULL;
- return pAttribute->getInstanceValue();
- }
- }
- }
- void setTableValue(const char *pXPath, int index, const char *pValue)
- {
- UNIMPLEMENTED;
- }
- int getNumberOfUniqueColumns()
- {
- return CConfigSchemaHelper::getInstance()->getEnvironmentXPathSize();
- }
- const char* getColumnName(int idx)
- {
- if (idx < CConfigSchemaHelper::getInstance()->getEnvironmentXPathSize())
- return CConfigSchemaHelper::getInstance()->getEnvironmentXPaths(idx);
- else
- return NULL;
- }
- int getNumberOfRows(const char* pXPath)
- {
- assert(pXPath != NULL && *pXPath != 0);
- PROGLOG("Get number of rows for %s = %d", pXPath, CConfigSchemaHelper::getInstance()->getElementArraySize(pXPath));
- return CConfigSchemaHelper::getInstance()->getElementArraySize(pXPath);
- }
- int getNumberOfTables()
- {
- return CConfigSchemaHelper::getInstance()->getNumberOfTables();
- }
- const char* getServiceName(int idx, char *pName)
- {
- if (pName != NULL)
- strcpy (pName, CBuildSetManager::getInstance()->getBuildSetServiceName(idx));
- return CBuildSetManager::getInstance()->getBuildSetServiceName(idx);
- }
- const char* getComponentName(int idx, char *pName)
- {
- if (pName != NULL)
- strcpy (pName, CBuildSetManager::getInstance()->getBuildSetComponentTypeName(idx));
- return CBuildSetManager::getInstance()->getBuildSetComponentTypeName(idx);
- }
- int openConfigurationFile(const char* pFile)
- {
- /*s_pConfigSchemaHelper = NULL;
- s_pConfigSchemaHelper = CConfigSchemaHelper::getInstance();
- StringArray arrXSDS;
- arrXSDS.append("dali.xsd");
- CBuildSetManager::getInstance()->setBuildSetArray(arrXSDS);
- s_pConfigSchemaHelper->populateSchema();*/
- CConfigSchemaHelper::getNewInstance()->loadEnvFromConfig(pFile);
- return 1;
- }
- int getNumberOfComponentsInConfiguration(void *pData)
- {
- if (pData == NULL)
- {
- return CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getNumberOfComponents();
- }
- else
- {
- CElement *pElement = static_cast<CElement*>(pData);
- assert(pElement->getNodeType() == XSD_ELEMENT);
- CElementArray *pElementArray = static_cast<CElementArray*>(pElement->getParentNode());
- assert(pElementArray->getNodeType() == XSD_ELEMENT_ARRAY);
- return pElementArray->length();
- }
- }
- void* getComponentInConfiguration(int idx)
- {
- assert(idx < CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getNumberOfComponents());
- return CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getComponent(idx);
- }
- void* getComponentInstance(int idx, void *pData)
- {
- assert(pData != NULL);
- assert(((static_cast<CElement*>(pData))->getNodeType()) == XSD_ELEMENT);
- CElement *pElement = static_cast<CElement*>(pData);
- CElementArray *pElementArray = static_cast<CElementArray*>(pElement->getParentNode());
- if (pElementArray->length() >= idx)
- idx = 0;
- return &(pElementArray->item(idx));
- }
- const char* getComponentNameInConfiguration(int idx, void *pData)
- {
- if (pData == NULL)
- return CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getComponent(idx)->getName();
- assert(!"Invalid component index");
- return NULL;
- }
- const void* getPointerToComponentInConfiguration(int idx, void *pData, int compIdx)
- {
- if (pData == NULL)
- {
- const CElement *pElement = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getComponent(idx);
- assert(pElement != NULL);
- const CXSDNodeBase *pNodeBase = pElement->getConstParentNode();
- const CElementArray *pElementArray = dynamic_cast<const CElementArray*>(pNodeBase);
- assert(pElementArray != NULL);
- return pElementArray;
- }
- else
- {
- assert( compIdx >= 0);
- CElementArray *pElementArray = static_cast<CElementArray*>(pData);
- assert(pElementArray->getNodeType() == XSD_ELEMENT_ARRAY);
- const CXSDNodeBase *pNodeBase = &(pElementArray->item(compIdx+idx));
- return(dynamic_cast<const CElement*>(pNodeBase));
- }
- }
- const void* getPointerToComponentTypeInConfiguration(void *pData)
- {
- assert (pData != NULL);
- CElement *pElement = static_cast<CElement*>(pData);
- assert (pElement->getNodeType() == XSD_ELEMENT);
- CElementArray *pElementArray = static_cast<CElementArray*>(pElement->getParentNode());
- return &(pElementArray->item(0));
- }
- int getIndexOfParent(void *pData)
- {
- assert (pData != NULL);
- assert((static_cast<CElement*>(pData))->getNodeType() == XSD_ELEMENT);
- int nIndexOfParent = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getIndexOfElement(static_cast<CElement*>(pData));
- assert(nIndexOfParent >= 0);
- return nIndexOfParent;
- }
- const void* getPointerToComponents()
- {
- assert(CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getComponent(0)->getConstParentNode()->getNodeType() == XSD_ELEMENT_ARRAY);
- return (CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getComponent(0)->getConstParentNode());
- }
- int getNumberOfChildren(void *pData)
- {
- int nRetVal = 0;
- if (pData == NULL)
- {
- assert(!"Should not be null"); // why ever null?
- return 0;
- }
- if (pData == (void*)(CEnvironmentModel::getInstance()))
- nRetVal = (static_cast<CEnvironmentModel*>(pData))->getNumberOfRootNodes();
- else // must be of type CEnvironmentModelNode*
- {
- CEnvironmentModelNode *pNode = static_cast<CEnvironmentModelNode*>(pData);
- nRetVal = pNode->getNumberOfChildren();
- }
- return nRetVal;
- }
- const char* getData(void *pData)
- {
- if (pData == NULL)
- return NULL;
- return CEnvironmentModel::getInstance()->getData(static_cast<CEnvironmentModelNode*>(pData));
- }
- const char* getName(void *pData)
- {
- if (pData == NULL)
- return NULL;
- return CEnvironmentModel::getInstance()->getInstanceName(static_cast<CEnvironmentModelNode*>(pData));
- }
- const char* getFileName(void *pData)
- {
- if (pData == NULL)
- return NULL;
- return CEnvironmentModel::getInstance()->getXSDFileName(static_cast<CEnvironmentModelNode*>(pData));
- }
- void* getParent(void *pData)
- {
- if (pData == NULL)
- return NULL;
- if (pData == (void*)(CEnvironmentModel::getInstance()->getRoot()))
- return (void*)(CEnvironmentModel::getInstance());
- else
- return (void*)(CEnvironmentModel::getInstance()->getParent(static_cast<CEnvironmentModelNode*>(pData)));
- }
- void* getChild(void *pData, int idx)
- {
- if (pData == NULL || pData == CEnvironmentModel::getInstance())
- {
- if (idx == 0)
- return (void*)(CEnvironmentModel::getInstance()->getRoot(0));
- return NULL;
- }
- else
- return (void*)(CEnvironmentModel::getInstance()->getChild(static_cast<CEnvironmentModelNode*>(pData), idx));
- }
- int getIndexFromParent(void *pData)
- {
- CEnvironmentModelNode *pNode = static_cast<CEnvironmentModelNode*>(pData);
- if (pNode->getParent() == NULL)
- return 0; // Must be 'Environment' node
- const CEnvironmentModelNode *pGrandParent = pNode->getParent();
- int nChildren = pGrandParent->getNumberOfChildren();
- for (int idx = 0; idx < nChildren; idx++)
- {
- if (pNode == pGrandParent->getChild(idx))
- return idx;
- }
- assert(!"Should not reach here");
- return 0;
- }
- void* getRootNode(int idx)
- {
- return (void*)(CEnvironmentModel::getInstance()->getRoot(idx));
- }
- void* getModel()
- {
- return (void*)(CEnvironmentModel::getInstance());
- }
- void getJSON(void *pData, char **pOutput, int nIdx)
- {
- CConfigSchemaHelper::getInstance()->printJSON(CONFIGURATOR_API::getFileName(pData), pOutput, nIdx, true);
- }
- void getNavigatorJSON(char **pOutput)
- {
- CConfigSchemaHelper::getInstance()->printNavigatorJSON(pOutput, true);
- }
- void getJSONByComponentName(const char *pComponentName, char **pOutput, int nIdx)
- {
- CConfigSchemaHelper::getInstance()->printJSON(pComponentName, pOutput, nIdx, true);
- }
- void getJSONByComponentKey(const char *pKey, char **pOutput)
- {
- CConfigSchemaHelper::getInstance()->printJSONByKey(pKey, pOutput, true);
- }
- void getDocBookByIndex(int idx, char **pOutput)
- {
- const char *pFileName = CBuildSetManager::getInstance()->getBuildSetComponentFileName(idx);
- CConfigSchemaHelper::getInstance()->printDocumentation(pFileName, pOutput);
- }
- bool saveConfigurationFile()
- {
- return CConfigSchemaHelper::getInstance()->saveConfigurationFile();
- }
- bool saveConfigurationFileAs(const char *pFilePath)
- {
- if (pFilePath == NULL || *pFilePath == 0)
- return false;
- return CConfigSchemaHelper::getInstance()->saveConfigurationFileAs(pFilePath);
- }
- int getNumberOfNotificationTypes()
- {
- return CNotificationManager::getInstance()->getNumberOfNotificationTypes();
- }
- const char* getNotificationTypeName(int type)
- {
- return CNotificationManager::getInstance()->getNotificationTypeName(type);
- }
- int getNumberOfNotifications(int type)
- {
- enum ENotificationType eType = static_cast<ENotificationType>(type);
- return CNotificationManager::getInstance()->getNumberOfNotifications(eType);
- }
- const char* getNotification(int type, int idx)
- {
- const char *pRet = NULL;
- enum ENotificationType eType = static_cast<ENotificationType>(type);
- return CNotificationManager::getInstance()->getNotification(eType, idx);
- }
- } // CONFIGURATOR_API namespace
|