/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 "apache/das/rdb/ModifiedDataObject.h"

namespace apache {
	namespace das {
		namespace rdb {
			
ModifiedDataObject::ModifiedDataObject(const Table& table, commonj::sdo::DataObjectPtr dataObject, 
			commonj::sdo::ChangeSummaryPtr changeSummary) : DASDataObject(table, dataObject, changeSummary) {
	
	bool modifiedOnGraph = changeSummary->isCreated(dataObject) || changeSummary->isDeleted(dataObject) || changeSummary->isModified(dataObject);
	const KeyDataList& columnDataList = getPrimaryKeys();
	unsigned int pkCount = 0;
	unsigned int pkColumnCount = columnDataList.size();
	bool dataTypeModified = false;
	//pkModified = false;
		
	if (modifiedOnGraph) {
		sets.append("update ").append(table.getTableName()).append(" set");
		commonj::sdo::SettingList& settings = changeSummary->getOldValues(dataObject);
		/*const KeyDataList& keyDataList = getPrimaryKeys();

		for (KeyDataList::const_iterator pkIt = keyDataList.begin() ; pkIt != keyDataList.end() ; pkIt++) {
			ColumnData actualColumnData(pkIt->second->getColumn(), dataObject);

			if (actualColumnData != *pkIt->second) {
				pkModified = true;
				break;

			}

		}*/

		for (int i = 0 ; i < settings.size() ; i++) {

			if (settings[i].getType().isDataType()) {

				if (dataTypeModified) {
					sets.append(",");
				}

				std::string propName = settings[i].getProperty().getName();
				const Column* column = table.getColumnByProperty(propName);

				if (!column->isCollision() || !column->isManaged()) {
					dataTypeModified = true;
					sets.append(" ").append(column->getName()).append("=").append(ColumnData(*column, dataObject).toSQL());

				}
		
			}

		}

		if (!dataTypeModified) {
			sets = "";
		}
		
	}

}

ModifiedDataObject::~ModifiedDataObject(void) {}

std::string ModifiedDataObject::getStatement(void) const {

	if (sets != "") {
		std::string ret = sets;
		const Column* occCol = getTable().getOCCColumn();

		if (occCol != 0 && !isOCCChecked() && occCol->isManaged()) {
			ret.append(",").append(occCol->getName()).append("=").append(StringWrapper::toString(getNewOCC() + 1));
		}

		ret.append(" ").append(getWhereStmt()).append(";");

		return ret;

	} else {
		return "";
	}

}
			
//bool ModifiedDataObject::isPKModified(void) const {
//	return pkModified;
//}


//
//void ModifiedDataObject::printStmt() {
//	DASDataObject::printStmt();
//
//	if (sets.size() != 0 || fks.size() != 0) {
//		std::string statement;
//		statement.append("update ").append(getTable().getTableName()).append(" set").append(sets);
//
//		for (std::map<std::string, const ColumnData*>::const_iterator it = fks.begin() ; it != fks.end() ; it++) {
//
//			statement.append(" ").append(it->first).append("=");
//
//			if (it->second == 0) {
//				statement.append(SQL_NULL_VALUE);
//			} else {
//				statement.append(it->second->toSQL());
//			}
//
//		}
//
//		statement.append(" ").append(whereStmt).append(";");
//
//		std::cout << statement << std::endl;
//
//	}
//
//}



		

		};
	};
};
