/*
 * 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-4508
// https://issues.jboss.org/browse/TEIID-2268
// https://issues.jboss.org/browse/TEIID-3520
package org.teiid.metadata;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.teiid.core.util.NamesUtil;
import org.teiid.metadata.AbstractMetadataRecord.Modifiable;


/**
 * ProcedureRecordImpl
 */
public class Procedure extends AbstractMetadataRecord implements Modifiable {
    
	private static final long serialVersionUID = 7714869437683360834L;

	public enum Type {
		StoredProc,
		StoredQuery
	}

	public static final int AUTO_UPDATECOUNT = -1;

    private boolean isVirtual;
	private boolean isSystem;
    private int updateCount = AUTO_UPDATECOUNT;
	private List<ProcedureParameter> parameters = DVMetadataRecordHolder.initList(2);
	private ColumnSet<Procedure> resultSet;
    private String queryPlan;
    
    private Schema parent;
    private transient long lastModified;

	private String owner = "system";
	private ExecuteAs executeAs = ExecuteAs.CALLER;

    public void setParent(Schema parent) {
		this.parent = parent;
	}

    public boolean isVirtual() {
        return this.isVirtual;
    }

	public boolean isSystem() {
		return isSystem;
	}

	public Type getType() {
        if (isVirtual()) {
            return Type.StoredQuery;
        }
        return Type.StoredProc;
    }
    
    public int getUpdateCount() {
        return this.updateCount;
    }
    
	public List<ProcedureParameter> getParameters() {
		return parameters;
	}

	public ProcedureParameter getParameterByName(String param) {
		final String cName = NamesUtil.getCanonicalName(param);
    	for (ProcedureParameter p: this.parameters) {
			if (Objects.equals(cName, NamesUtil.getCanonicalName(p.getName()))) {
				return p;
			}
        }
        return null;
	}   
	 
	public void setParameters(List<ProcedureParameter> parameters) {
		this.parameters = DVMetadataRecordHolder.reInitList(this.parameters, parameters);
	}

	public String getQueryPlan() {
		return queryPlan;
	}

	public void setQueryPlan(String queryPlan) {
		this.queryPlan = queryPlan;
	}

    /**
     * @param b
     */
    public void setVirtual(boolean b) {
        isVirtual = b;
    }

	/**
	 * @param b
	 */
	public void setSystem(boolean b) {
		isSystem = b;
	}

	public void setUpdateCount(int count) {
    	this.updateCount = count;
    }

	public void setResultSet(ColumnSet<Procedure> resultSet) {
		this.resultSet = resultSet;
		if (resultSet != null) {
			resultSet.setParent(this);
		}
	}

	public ColumnSet<Procedure> getResultSet() {
		return resultSet;
	}
	
	@Override
	public Schema getParent() {
		return parent;
	}
	
	public long getLastModified() {
		return lastModified;
	}
	
	public void setLastModified(long lastModified) {
		this.lastModified = lastModified;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public ExecuteAs getExecuteAs() {
		return executeAs;
	}

	public void setExecuteAs(ExecuteAs executeAs) {
		this.executeAs = executeAs;
	}

	@Override
	public List<? extends AbstractMetadataRecord> getChildren() {
		List<AbstractMetadataRecord> children = new ArrayList<>();
		children.addAll(parameters);
		if (resultSet != null)
			children.addAll(resultSet.getChildren());
		return children;
	}
}
