<commit_before>/**
 * Property Manager to store and receive the Config
 *
 * there will be 3 kinds of propertys
 * PropertyBool
 * PropertyString
 * PropertyNumber
 *
 * Copyright 2013 Thincast Technologies GmbH
 * Copyright 2013 DI (FH) <NAME> <<EMAIL>>
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "PropertyManager.h"

#include <winpr/wlog.h>
#include <appcontext/ApplicationContext.h>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <utils/StringHelpers.h>

namespace freerds
{
	namespace sessionmanager
	{
		namespace config
		{
			static wLog* logger_PropertyManager = WLog_Get("freerds.SessionManager.config.propertymanager");

			std::string gConnectionPrefix = "current.connection.";


			PropertyManager::PropertyManager()
			{

			};

			PropertyManager::~PropertyManager()
			{

			};

			bool PropertyManager::getPropertyInternal(long sessionID, std::string path, PROPERTY_STORE_HELPER & helper, std::string username) {
				// first try to resolve the sessionID
				bool found = false;
				std::string currentUserName;
				if (sessionID == 0) {
					// for no session, use username if its present
					if (username.size() == 0) {
						return -1;
					}
					currentUserName = username;
				} else {
					// for a given sessionID we try to get the username from the sessionstore
					sessionNS::SessionPtr session = APP_CONTEXT.getSessionStore()->getSession(sessionID);
					if (session == NULL) {
						return -1;
					}
					currentUserName = session->getUserName();
				}

				if(path.substr(0, gConnectionPrefix.size()) == gConnectionPrefix) {
				    // requesting session values
					std::string actualPath = path.substr(gConnectionPrefix.size());
					sessionNS::SessionPtr currentSession =APP_CONTEXT.getSessionStore()->getSession(sessionID);
					if (currentSession == NULL) {
						WLog_Print(logger_PropertyManager, WLOG_ERROR, "Cannot get Session for sessionID %lu",sessionID);
						return false;
					}
					long connectionID = APP_CONTEXT.getConnectionStore()->getConnectionIdForSessionId(currentSession->getSessionID());
					if (connectionID == 0) {
						WLog_Print(logger_PropertyManager, WLOG_ERROR, "Cannot get ConnectionId for sessionID %lu",sessionID);
						return false;
					}
					sessionNS::ConnectionPtr currentConnection = APP_CONTEXT.getConnectionStore()->getConnection(connectionID);
					if (currentConnection == NULL) {
						WLog_Print(logger_PropertyManager, WLOG_ERROR, "Cannot get Connection for connectionId %lu",connectionID);
						return false;
					}
					return currentConnection->getProperty(actualPath,helper);
				}


				if (mPropertyUserMap.find(currentUserName) != mPropertyUserMap.end()) {
					TPropertyMap * uPropMap = mPropertyUserMap[currentUserName];
					if (uPropMap->find(path) != uPropMap->end()) {
						// we found the setting ...
						helper = (*uPropMap)[path];
						return true;
					}
				}
				if ((!found) && (mPropertyGlobalMap.find(path) != mPropertyGlobalMap.end())) {
					helper = mPropertyGlobalMap[path];
					return true;
				}
				return false;
			}<commit_msg>fixed PropertyManager<commit_after>/**
 * Property Manager to store and receive the Config
 *
 * there will be 3 kinds of propertys
 * PropertyBool
 * PropertyString
 * PropertyNumber
 *
 * Copyright 2013 Thincast Technologies GmbH
 * Copyright 2013 DI (FH) <NAME> <<EMAIL>>
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "PropertyManager.h"

#include <winpr/wlog.h>
#include <appcontext/ApplicationContext.h>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <utils/StringHelpers.h>

namespace freerds
{
	namespace sessionmanager
	{
		namespace config
		{
			static wLog* logger_PropertyManager = WLog_Get("freerds.SessionManager.config.propertymanager");

			std::string gConnectionPrefix = "current.connection.";


			PropertyManager::PropertyManager()
			{

			};

			PropertyManager::~PropertyManager()
			{

			};

			bool PropertyManager::getPropertyInternal(long sessionID, std::string path, PROPERTY_STORE_HELPER & helper, std::string username) {
				// first try to resolve the sessionID
				std::string currentUserName;
				if (sessionID == 0) {
					// for no session, use username if its present
					if (username.size() == 0) {
						if ((mPropertyGlobalMap.find(path) != mPropertyGlobalMap.end())) {
							helper = mPropertyGlobalMap[path];
							return true;
						} else {
							return false;
						}
					}
					currentUserName = username;
				} else {
					// for a given sessionID we try to get the username from the sessionstore
					sessionNS::SessionPtr session = APP_CONTEXT.getSessionStore()->getSession(sessionID);
					if (session == NULL) {
						return -1;
					}
					currentUserName = session->getUserName();
				}

				if(path.substr(0, gConnectionPrefix.size()) == gConnectionPrefix) {
				    // requesting session values
					std::string actualPath = path.substr(gConnectionPrefix.size());
					sessionNS::SessionPtr currentSession =APP_CONTEXT.getSessionStore()->getSession(sessionID);
					if (currentSession == NULL) {
						WLog_Print(logger_PropertyManager, WLOG_ERROR, "Cannot get Session for sessionID %lu",sessionID);
						return false;
					}
					long connectionID = APP_CONTEXT.getConnectionStore()->getConnectionIdForSessionId(currentSession->getSessionID());
					if (connectionID == 0) {
						WLog_Print(logger_PropertyManager, WLOG_ERROR, "Cannot get ConnectionId for sessionID %lu",sessionID);
						return false;
					}
					sessionNS::ConnectionPtr currentConnection = APP_CONTEXT.getConnectionStore()->getConnection(connectionID);
					if (currentConnection == NULL) {
						WLog_Print(logger_PropertyManager, WLOG_ERROR, "Cannot get Connection for connectionId %lu",connectionID);
						return false;
					}
					return currentConnection->getProperty(actualPath,helper);
				}


				if (mPropertyUserMap.find(currentUserName) != mPropertyUserMap.end()) {
					TPropertyMap * uPropMap = mPropertyUserMap[currentUserName];
					if (uPropMap->find(path) != uPropMap->end()) {
						// we found the setting ...
						helper = (*uPropMap)[path];
						return true;
					}
				}
				if ((mPropertyGlobalMap.find(path) != mPropertyGlobalMap.end())) {
					helper = mPropertyGlobalMap[path];
					return true;
				}
				return false;
			}
{'fraction_non_alphanumeric': 0.10666148263348886, 'fraction_numerical': 0.004795230689476413, 'mean_word_length': 3.817103620474407, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7143857', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2160, 'n_words': 599}