/*###############################################################################
# Linux Management Providers (LMP), System Memory provider package
# Copyright (C) 2008 Guillaume BOTTEX, ETRI <guillaumebottex@etri.re.kr, guillaumebottex@gmail.com>
#
# This program is being developed under the "OpenDRIM" project.
# The "OpenDRIM" project web page: http://opendrim.sourceforge.net
# The "OpenDRIM" project mailing list: opendrim@googlegroups.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
# To contributors, please leave your contact information in this section
# AND comment your changes in the source code.
#
# Modified by <Author(s)>, <Affiliation>, <Year>
###############################################################################*/

#include "cmpiOpenDRIM_RegisteredMemoryProfile.h"
#include "OpenDRIM_RegisteredMemoryProfileAccess.h"

static const CMPIBroker * _broker;

#ifdef CMPI_VER_100
#define SystemMemory_OpenDRIM_RegisteredMemoryProfileProviderSetInstance SystemMemory_OpenDRIM_RegisteredMemoryProfileProviderModifyInstance
#endif

int SystemMemory_OpenDRIM_RegisteredMemoryProfile_init(const CMPIBroker* broker);

int SystemMemory_OpenDRIM_RegisteredMemoryProfile_finalize();

/* ---------------------------------------------------------------------------*/
/*                      Instance Provider Interface                           */
/* ---------------------------------------------------------------------------*/

CMPIStatus SystemMemory_OpenDRIM_RegisteredMemoryProfileProviderCleanup(CMPIInstanceMI * mi, const CMPIContext* ctx, CMPIBoolean terminate) {
	_E_;
	int errorCode = SystemMemory_OpenDRIM_RegisteredMemoryProfile_finalize();
	CMPIStatus rc;
	CMSetStatus(&rc, (CMPIrc) errorCode);
	_L_;
	return rc;
}

CMPIStatus SystemMemory_OpenDRIM_RegisteredMemoryProfileProviderEnumInstanceNames(CMPIInstanceMI* mi, const CMPIContext* ctx, const CMPIResult* rslt, const CMPIObjectPath* ref) {
	_E_;
	CMPIStatus rc = {CMPI_RC_OK, NULL};
	vector<OpenDRIM_RegisteredMemoryProfile> instances;
	string errorMessage;
	
	int errorCode = SystemMemory_OpenDRIM_RegisteredMemoryProfile_retrieve(_broker, ctx, instances, NULL, errorMessage, "ein");
	if (errorCode != OK) {
		errorMessage = (string) OpenDRIM_RegisteredMemoryProfile_classnames[0] + ": " + errorMessage;
		CMSetStatusWithChars(_broker, &rc, (CMPIrc) errorCode, errorMessage.c_str());
		return rc;
	}
	for (size_t i=0; i < instances.size(); i++) {
		CMPIObjectPath* op = SystemMemory_OpenDRIM_RegisteredMemoryProfile_toCMPIObjectPath(_broker, instances[i]);
		CMReturnObjectPath(rslt, op);
	}
	CMReturnDone(rslt);
	_L_;
	return rc;
}

CMPIStatus SystemMemory_OpenDRIM_RegisteredMemoryProfileProviderEnumInstances(CMPIInstanceMI* mi, const CMPIContext* ctx, const CMPIResult* rslt, const CMPIObjectPath* ref, const char** properties) {
	_E_;
	CMPIStatus rc = {CMPI_RC_OK, NULL};
	vector<OpenDRIM_RegisteredMemoryProfile> instances;
	string errorMessage;
	
	int errorCode = SystemMemory_OpenDRIM_RegisteredMemoryProfile_retrieve(_broker, ctx, instances, properties, errorMessage, "ei");
	if (errorCode != OK) {
		errorMessage = (string) OpenDRIM_RegisteredMemoryProfile_classnames[0] + ": " + errorMessage;
		CMSetStatusWithChars(_broker, &rc, (CMPIrc) errorCode, errorMessage.c_str());
		return rc;
	}
	for (size_t i=0; i < instances.size(); i++) {
		CMPIInstance* ci = SystemMemory_OpenDRIM_RegisteredMemoryProfile_toCMPIInstance(_broker, instances[i]);
		CMReturnInstance(rslt, ci);
	}
	CMReturnDone(rslt);
	_L_;
	return rc;
}

CMPIStatus SystemMemory_OpenDRIM_RegisteredMemoryProfileProviderGetInstance(CMPIInstanceMI* mi, const CMPIContext* ctx, const CMPIResult* rslt, const CMPIObjectPath* cop, const char** properties) {
	_E_;
	CMPIStatus rc = {CMPI_RC_OK, NULL};
	OpenDRIM_RegisteredMemoryProfile instance;
	SystemMemory_OpenDRIM_RegisteredMemoryProfile_toCPP(_broker, cop, instance);
	string errorMessage;
	
	int errorCode = SystemMemory_OpenDRIM_RegisteredMemoryProfile_getInstance(_broker, ctx, instance, properties, errorMessage);
	if (errorCode != OK) {
		errorMessage = (string) OpenDRIM_RegisteredMemoryProfile_classnames[0] + ": " + errorMessage;
		CMSetStatusWithChars(_broker, &rc, (CMPIrc) errorCode, errorMessage.c_str());
		return rc;
	}
	CMPIInstance* ci = SystemMemory_OpenDRIM_RegisteredMemoryProfile_toCMPIInstance(_broker, instance);
	CMReturnInstance(rslt, ci);
	CMReturnDone(rslt);
	_L_;
	return rc;
}

CMPIStatus SystemMemory_OpenDRIM_RegisteredMemoryProfileProviderSetInstance(CMPIInstanceMI* mi, const CMPIContext* ctx, const CMPIResult* rslt, const CMPIObjectPath* cop, const CMPIInstance* ci, const char** properties) {
	_E_;
	CMPIStatus rc = {CMPI_RC_OK, NULL};
	OpenDRIM_RegisteredMemoryProfile newInstance, oldInstance;
	SystemMemory_OpenDRIM_RegisteredMemoryProfile_toCPP(_broker, ci, newInstance);
	SystemMemory_OpenDRIM_RegisteredMemoryProfile_toCPP(_broker, cop, oldInstance);
	string errorMessage;
	
	int errorCode = SystemMemory_OpenDRIM_RegisteredMemoryProfile_getInstance(_broker, ctx, oldInstance, properties, errorMessage);
	if (errorCode != OK) {
		errorMessage = (string) OpenDRIM_RegisteredMemoryProfile_classnames[0] + ": " + errorMessage;
		CMSetStatusWithChars(_broker, &rc, (CMPIrc) errorCode, errorMessage.c_str());
		return rc;
	}
	errorCode = SystemMemory_OpenDRIM_RegisteredMemoryProfile_setInstance(_broker, ctx, newInstance, oldInstance, properties, errorMessage);
	if (errorCode != OK) {
		errorMessage = (string) OpenDRIM_RegisteredMemoryProfile_classnames[0] + ": " + errorMessage;
		CMSetStatusWithChars(_broker, &rc, (CMPIrc) errorCode, errorMessage.c_str());
		return rc;
	}
	CMReturnDone(rslt);
	_L_;
	return rc;
}

CMPIStatus SystemMemory_OpenDRIM_RegisteredMemoryProfileProviderCreateInstance(CMPIInstanceMI* mi, const CMPIContext* ctx, const CMPIResult* rslt, const CMPIObjectPath* cop, const CMPIInstance* ci) {
	_E_;
	CMPIStatus rc = {CMPI_RC_OK, NULL};
	OpenDRIM_RegisteredMemoryProfile instance;
	SystemMemory_OpenDRIM_RegisteredMemoryProfile_toCPP(_broker, ci, instance);
	string errorMessage;
	
	int errorCode = SystemMemory_OpenDRIM_RegisteredMemoryProfile_getInstance(_broker, ctx, instance, NULL, errorMessage);
	if (errorCode != NOT_FOUND) {
		if (errorCode == OK)
			errorCode = ALREADY_EXISTS;
		errorMessage = (string) OpenDRIM_RegisteredMemoryProfile_classnames[0] + ": " + errorMessage;
		CMSetStatusWithChars(_broker, &rc, (CMPIrc) errorCode, errorMessage.c_str());
		return rc;
	}
	errorCode = SystemMemory_OpenDRIM_RegisteredMemoryProfile_createInstance(_broker, ctx, instance, errorMessage);
	if (errorCode != OK) {
		errorMessage = (string) OpenDRIM_RegisteredMemoryProfile_classnames[0] + ": " + errorMessage;
		CMSetStatusWithChars(_broker, &rc, (CMPIrc) errorCode, errorMessage.c_str());
		return rc;
	}
	errorCode = SystemMemory_OpenDRIM_RegisteredMemoryProfile_getInstance(_broker, ctx, instance, NULL, errorMessage);
	if (errorCode != OK) {
		errorMessage = (string) OpenDRIM_RegisteredMemoryProfile_classnames[0] + ": " + errorMessage;
		CMSetStatusWithChars(_broker, &rc, (CMPIrc) errorCode, errorMessage.c_str());
		return rc;
	}
	CMPIObjectPath* _cop = SystemMemory_OpenDRIM_RegisteredMemoryProfile_toCMPIObjectPath(_broker, instance);
	CMReturnObjectPath(rslt, _cop);
	CMReturnDone(rslt);
	_L_;
	return rc;
}

CMPIStatus SystemMemory_OpenDRIM_RegisteredMemoryProfileProviderDeleteInstance(CMPIInstanceMI* mi, const CMPIContext* ctx, const CMPIResult* rslt, const CMPIObjectPath* cop) {
	_E_;
	CMPIStatus rc = {CMPI_RC_OK, NULL};
	OpenDRIM_RegisteredMemoryProfile instance;
	SystemMemory_OpenDRIM_RegisteredMemoryProfile_toCPP(_broker, cop, instance);
	string errorMessage;
	
	int errorCode = SystemMemory_OpenDRIM_RegisteredMemoryProfile_getInstance(_broker, ctx, instance, NULL, errorMessage);
	if (errorCode != OK) {
		errorMessage = (string) OpenDRIM_RegisteredMemoryProfile_classnames[0] + ": " + errorMessage;
		CMSetStatusWithChars(_broker, &rc, (CMPIrc) errorCode, errorMessage.c_str());
		return rc;
	}
	errorCode = SystemMemory_OpenDRIM_RegisteredMemoryProfile_deleteInstance(_broker, ctx, instance, errorMessage);
	if (errorCode != OK) {
		errorMessage = (string) OpenDRIM_RegisteredMemoryProfile_classnames[0] + ": " + errorMessage;
		CMSetStatusWithChars(_broker, &rc, (CMPIrc) errorCode, errorMessage.c_str());
		return rc;
	}
	CMReturnDone(rslt);
	_L_;
	return rc;
}

CMPIStatus SystemMemory_OpenDRIM_RegisteredMemoryProfileProviderExecQuery(CMPIInstanceMI* mi, const CMPIContext* ctx, const CMPIResult* rslt, const CMPIObjectPath* ref, const char* lang, const char* query) {
	_E_;
	CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

/* ---------------------------------------------------------------------------*/
/*                   End of Instance Provider Interface                       */
/* ---------------------------------------------------------------------------*/

/* ---------------------------------------------------------------------------*/
/*                              Provider Factory                              */
/* ---------------------------------------------------------------------------*/

int SystemMemory_OpenDRIM_RegisteredMemoryProfile_init(const CMPIBroker* broker) {
	_E_;
	_broker = broker;
	static bool initialized = false;
	if (!initialized) {
		string errorMessage;
		int errorCode = SystemMemory_OpenDRIM_RegisteredMemoryProfile_load(broker, errorMessage);
		if (errorCode != OK) {
			errorMessage += "SystemMemory_OpenDRIM_RegisteredMemoryProfile_init FAILED: " + (string) OpenDRIM_RegisteredMemoryProfile_classnames[0] + ": " + errorMessage;
			string cmd = "/bin/echo \"" + errorMessage + "\" >> cmpi_prov_debug.txt";
			system(cmd.c_str());
			return -1;
		}
		initialized = true;
	}
	_L_;
	return OK;
}

int SystemMemory_OpenDRIM_RegisteredMemoryProfile_finalize() {
	_E_;
	static bool finalized = false;
	if (!finalized) {
		string errorMessage;
		int errorCode = SystemMemory_OpenDRIM_RegisteredMemoryProfile_unload(errorMessage);
		if (errorCode != OK) {
			errorMessage += "SystemMemory_OpenDRIM_RegisteredMemoryProfile_unload FAILED: " + (string) OpenDRIM_RegisteredMemoryProfile_classnames[0] + ": " + errorMessage;
			string cmd = "/bin/echo \"" + errorMessage + "\" >> cmpi_prov_debug.txt";
			system(cmd.c_str());
			return errorCode;
		}
		finalized = true;
	}
	_L_;
	return OK;
}

#define SystemMemory_OpenDRIM_RegisteredMemoryProfile_INIT if (SystemMemory_OpenDRIM_RegisteredMemoryProfile_init(_broker) < 0) return NULL

CMInstanceMIStub(SystemMemory_OpenDRIM_RegisteredMemoryProfileProvider, SystemMemory_OpenDRIM_RegisteredMemoryProfileProvider, _broker, SystemMemory_OpenDRIM_RegisteredMemoryProfile_INIT);

/* ---------------------------------------------------------------------------*/
/*                          End of Provider Factory                           */
/* ---------------------------------------------------------------------------*/
