/* ************************************************************************
#
#  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.mumps;

import divconq.db.DatabaseResult;
import divconq.db.IDatabaseCallback;
import divconq.db.IDatabaseRequest;
import divconq.db.ObjectResult;
import divconq.lang.Hub;
import divconq.lang.Memory;
import divconq.lang.OperationResult;
import divconq.lang.TaskContext;
import divconq.struct.CompositeStruct;

/**
 * Internal class used to track a request and response across thread boundaries
 * (after being submitted to queue) and also assists with the post processing
 * of results and notifying the submitter via the callback.
 * 
 * @author Andy
 *
 */
public class DatabaseTask {
	protected IDatabaseRequest request = null;
	protected DatabaseResult result = null;
	protected IDatabaseCallback callback = null;
	protected Memory mem = null;
	protected TaskContext context = null;
	
	/**
	 * Called after "result" is filled.  Sets about with post processing and call backs.
	 */
	protected void complete() {
		if (this.result instanceof ObjectResult) {
			CompositeStruct res = ((ObjectResult)this.result).getResult();
			OperationResult or = Hub.instance.getSchema().validateProcResponse(this.request.getProcedure(), res);
			this.result.copyMessages(or); 
		}
		
		this.request.process(this.result);
		
		this.callback.process(this.result);
	}
}
