/* ************************************************************************
#
#  DivConq
#
#  http://divconq.com/
#
#  Copyright:
#    Copyright 2012 eTimeline, LLC. All rights reserved.
#
#  License:
#    See the license.txt file in the project's top-level directory for details.
#
#  Authors:
#    * Andy White
#
************************************************************************ */
package divconq.db.update;

import divconq.db.IDatabaseManager;
import divconq.db.UpdateRequest;
import divconq.lang.OperationResult;
import divconq.struct.RecordStruct;
import divconq.struct.Struct;
import divconq.util.StringUtil;

/**
 * If you wish to work with values > 1MB then use this request.  It only allows one field at a time to be 
 * updated but it does handle up to 40MB of data.  This call also does not update indexes, so do not store large
 * values in indexed fields.  
 * 
 * @author Andy
 *
 */
public class UpdateBigFieldRequest extends UpdateRequest {
	protected DbField field = null;
	
	/**
	 * @param table name 
	 * @param id of record
	 * @param field to update
	 */
	public UpdateBigFieldRequest(String table, String id, DbField field) {
		this(table, id, null, null, field);
	}
	
	/**
	 * @param table name 
	 * @param id of record
	 * @param filter name
	 * @param field to update
	 */
	public UpdateBigFieldRequest(String table, String id, String filter, DbField field) {
		this(table, id, filter, null, field);
	}
	
	/**
	 * @param table name 
	 * @param id of record
	 * @param filter name
	 * @param extra parameters for filter
	 * @param field to update
	 */
	public UpdateBigFieldRequest(String table, String id, String filter, Struct extra, DbField field) {
		super("dcUpdateBigField");
		
		RecordStruct params = new RecordStruct();
		
		this.parameters = params;
		this.field = field;
	
		params.setField("Table", table);
		params.setField("Id", id);
		
		if (StringUtil.isNotEmpty(filter))
			params.setField("Filter", filter);
		
		if (extra != null)
			params.setField("Extra", extra);
	}
	
	/* (non-Javadoc)
	 * @see divconq.db.UpdateRequest#validate(divconq.lang.OperationResult)
	 */
	@Override
	public void validate(OperationResult or) {
		super.validate(or);
		
		// TODO validate, prove that all fields/tables/filters are real  
		// (check filter is for this table)
	}
	
	/* (non-Javadoc)
	 * @see divconq.db.UpdateRequest#getDatabaseParams()
	 */
	@Override
	public RecordStruct getDatabaseParams(IDatabaseManager dbm) {
		if (this.field != null) {
			this.parameters.setField("Content", this.field.convert(null, dbm));
			this.parameters.setField("Field", this.field.name);
		}
		
		return this.parameters;
	}
	
	/* (non-Javadoc)
	 * @see divconq.db.UpdateRequest#isBulkyRequest()
	 */
	@Override
	public boolean isBulkyRequest() {
		return true;
	}
}
