package com.nflabs.zeppelin.zengine.api;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.nflabs.zeppelin.driver.LazyConnection;
import com.nflabs.zeppelin.driver.ZeppelinConnection;
import com.nflabs.zeppelin.driver.ZeppelinDriverFactory;
import com.nflabs.zeppelin.result.Result;
import com.nflabs.zeppelin.zengine.Zengine;

public class ZPlan extends LinkedList<Z>{
	
	public ZPlan(){
		
	}
	public LinkedList<Result> execute(Zengine zengine) throws Exception{
		return execute(zengine, null);
	}
	
	public LinkedList<Result> execute(Zengine zengine, List<Map<String, Object>> params) throws Exception{
		LinkedList<Result> results = new LinkedList<Result>();
		
		ZeppelinDriverFactory driverFactory = zengine.getDriverFactory();

		
		for (int i=0; i<this.size(); i++) {
			Z zz = this.get(i);
			Map<String, Object> p = new HashMap<String, Object>();
			if(params!=null && params.size()>=i+1){
				p = params.get(i);
			}
			
			try {
				zz.withParams(p);
				
				ZeppelinConnection conn = zz.getConnection();
				if (conn instanceof LazyConnection) {
					((LazyConnection)conn).initialize(driverFactory);
				}
				zz.execute(conn);
				results.add(zz.result());
				zz.release();
			} catch (Exception e) {
				e.printStackTrace();
				throw e;
			} finally {
				closeAllConnections();
			}
		}
		
		return results;
	}
	
	private void closeAllConnections(){
		for (int i=0; i<this.size(); i++) {
			Z zz = this.get(i);
			zz.connection.close();
		}
	}
}

@@ -1283,25 +1283,27 @@
 %09%09%09%09
-e.printStackTrace
+closeAllConnections
 ();%0A
@@ -1323,21 +1323,13 @@
 %09%09%09%7D
- finally %7B
 %0A%09%09
+%7D%0A
 %09%09cl
@@ -1353,20 +1353,8 @@
 ();%0A
-%09%09%09%7D%0A%09%09%7D%0A%09%09%0A
 %09%09re

Fix connection closing behavior