/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *  
 */
// https://issues.jboss.org/browse/TEIID-3053
// https://issues.jboss.org/browse/TEIID-4509

/*
 */
package org.teiid.query.processor.proc;

import org.teiid.client.plan.PlanNode;
import org.teiid.core.TeiidComponentException;
import org.teiid.core.TeiidProcessingException;
import org.teiid.query.metadata.QueryMetadataInterface;
import org.teiid.query.processor.ProcessorPlan;
import org.teiid.query.sql.lang.Command;
import org.teiid.query.sql.symbol.ElementSymbol;
import org.teiid.query.sql.symbol.Expression;
import org.teiid.query.sql.symbol.Symbol;
import org.teiid.query.sql.util.VariableContext;

import java.util.ArrayList;
import java.util.List;

import static org.teiid.query.analysis.AnalysisRecord.*;


/**
 */
public class LoopInstruction extends CreateCursorResultSetInstruction implements RepeatedInstruction {
    // the loop block
    private Program loopProgram;
    
    private List<ElementSymbol> elements;
    private String label;
    private final QueryMetadataInterface metadata;
    
    public LoopInstruction(Program loopProgram, String rsName, ProcessorPlan plan, String label, Command command, QueryMetadataInterface metadata) {
        // EXTENSION
        // ISSUE: DVCORE-2441
        // FIXED BY: Roman Pedchenko
        // DATE: 2014.10.23
        // ADOPTED FROM: https://issues.jboss.org/browse/TEIID-247, 2166, 2151, 2234, 2581, 2571, 2369, 1386, 2226, 2233, 2224, 2225, 2257
        // super(rsName, plan, false);
        super(rsName, null, null, plan, Mode.NOHOLD);
        // END EXTENSION
        this.loopProgram = loopProgram;
        this.label = label;
        this.command = command;
        this.metadata = metadata;
    }
    
    @Override
    public String getLabel() {
    	return label;
    }
    
    @Override
    public void setLabel(String label) {
    	this.label = label;
    }

    public void process(ProcedurePlan procEnv) throws TeiidComponentException, TeiidProcessingException {
        List<?> currentRow = procEnv.getCurrentRow(rsName); 
        VariableContext varContext = procEnv.getCurrentVariableContext();
        //set results to the variable context(the cursor.element is treated as variable)
        if(this.elements == null){
            List schema = procEnv.getSchema(rsName);
            elements = new ArrayList<ElementSymbol>(schema.size());
            for(int i=0; i< schema.size(); i++){
                Expression element = (Expression)schema.get(i);
                // BUG: NPE thrown when dynamic SQL is executed
        		// ISSUE: DVCORE-781
        		// SOLUTION: applied solution provided in TEIID-2222
        		// FIXED by Salvatore Raunich
        		// date 2013.12.12
//                elements.add(new ElementSymbol(rsName + "." + Symbol.getShortName(element)));              //$NON-NLS-1$
                ElementSymbol e = new ElementSymbol(rsName + Symbol.SEPARATOR + Symbol.getShortName(element));
                e.setType(element.getType());
				elements.add(e);
				// BUG END
            }
        }
        for(int i=0; i< elements.size(); i++){
            varContext.setValue(elements.get(i), currentRow.get(i));               
        }
        AuthorizationUtils.validateAuthorizationInProcCommand(command, metadata, procEnv);
    }
    
    /**
     * Returns a deep clone
     */
    public LoopInstruction clone(){
        ProcessorPlan clonedPlan = this.plan.clone();
        return new LoopInstruction((Program)this.loopProgram.clone(), this.rsName, clonedPlan, label, command, metadata);
    }
    
    public String toString() {
        return "LOOP INSTRUCTION: " + this.rsName; //$NON-NLS-1$
    }
    
    public PlanNode getDescriptionProperties() {
        PlanNode props = new PlanNode("LOOP"); //$NON-NLS-1$
        props.addProperty(PROP_SQL, this.plan.getDescriptionProperties());
        props.addProperty(PROP_RESULT_SET, this.rsName);
        props.addProperty(PROP_PROGRAM, this.loopProgram.getDescriptionProperties());
        return props;
    }

    public boolean testCondition(ProcedurePlan procEnv) throws TeiidComponentException, TeiidProcessingException {
        if(!procEnv.resultSetExists(rsName)) {
            // EXTENSION
            // ISSUE: DVCORE-2441
            // FIXED BY: Roman Pedchenko
            // DATE: 2014.10.23
            // ADOPTED FROM: https://issues.jboss.org/browse/TEIID-247, 2166, 2151, 2234, 2581, 2571, 2369, 1386, 2226, 2233, 2224, 2225, 2257
            // procEnv.executePlan(plan, rsName, null, false);            
            procEnv.executePlan(plan, rsName, null, Mode.NOHOLD, false);            
            // END EXTENSION
        }
        
        return procEnv.iterateCursor(rsName);
    }

    /** 
     * @see org.teiid.query.processor.proc.RepeatedInstruction#getNestedProgram()
     */
    public Program getNestedProgram() {
        return loopProgram;
    }

    public void postInstruction(ProcedurePlan procEnv) throws TeiidComponentException {
        procEnv.removeResults(rsName);
    }
    
// EXTENSION
// ISSUE: DVCORE-2441
// FIXED BY: Roman Pedchenko
// DATE: 2014.10.23
// ADOPTED FROM: https://issues.jboss.org/browse/TEIID-247, 2166, 2151, 2234, 2581, 2571, 2369, 1386, 2226, 2233, 2224, 2225, 2257
//     @Override
//     public void getChildPlans(List<ProcessorPlan> plans) {
//     	super.getChildPlans(plans);
//     	this.loopProgram.getChildPlans(plans);
//     }
// END EXTENSION

	@Override
	public Boolean requiresTransaction(boolean transactionalReads) {
		Boolean requires = super.requiresTransaction(transactionalReads);
		if (requires != null && requires) {
			return true;
		}
		Boolean loopRequires = loopProgram.requiresTransaction(transactionalReads);
		if (loopRequires == null || loopRequires) {
			return true;
		}
		return requires;
	}

}
