/*
 * 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.
 */
package org.teiid.translator.jpa;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.teiid.language.ColumnReference;
import org.teiid.language.Command;
import org.teiid.language.NamedTable;
import org.teiid.language.SQLConstants.Tokens;
import org.teiid.language.visitor.HierarchyVisitor;
import org.teiid.language.visitor.SQLStringVisitor;
import org.teiid.metadata.AbstractMetadataRecord;
import org.teiid.translator.TranslatorException;

public class JPQLUpdateQueryVisitor extends HierarchyVisitor {
	protected ArrayList<TranslatorException> exceptions = new ArrayList<TranslatorException>();
	protected AtomicInteger aliasCounter = new AtomicInteger(0);
	protected HashMap<String, String> correlatedName = new HashMap<String, String>();
	
	public JPQLUpdateQueryVisitor() {
		super(false);
	}

    public static String getJPQLString(Command obj)  throws TranslatorException {
    	JPQLUpdateQueryVisitor visitor = new JPQLUpdateQueryVisitor();
        
    	visitor.visitNode(obj);
    	
    	if (!visitor.exceptions.isEmpty()) {
    		throw visitor.exceptions.get(0);
    	}  
    	
        return visitor.convertToQuery(obj);
    }
    
    private String convertToQuery(Command obj) {
    	JPQLUpdateStringVisitor visitor = new JPQLUpdateStringVisitor(this);
    	visitor.visitNode(obj);
    	return visitor.toString();
    }
    
    @Override
    public void visit(NamedTable obj) {
    	if (obj.getCorrelationName() == null) {
    		String aliasName = "ql_"+this.aliasCounter.getAndIncrement(); //$NON-NLS-1$
    		this.correlatedName.put(obj.getMetadataObject().getName(), aliasName);
    		obj.setCorrelationName(aliasName);
    	}
    	else {
    		this.correlatedName.put(obj.getMetadataObject().getName(), obj.getCorrelationName());
    	}
    }
    
    static class JPQLUpdateStringVisitor extends SQLStringVisitor {
    	private JPQLUpdateQueryVisitor visitor;
    	
    	public JPQLUpdateStringVisitor(JPQLUpdateQueryVisitor visitor) {
    		this.visitor = visitor;
    	}
    	
    	@Override
        public void visit(ColumnReference column) {
    		AbstractMetadataRecord record = column.getMetadataObject();
    		if (record != null) {
    			String name = record.getProperty(JPAMetadataProcessor.KEY_ASSOSIATED_WITH_FOREIGN_TABLE, false); 
    			if (name == null) {
					buffer.append(this.visitor.correlatedName.get(column.getTable().getMetadataObject().getName())).append(Tokens.DOT).append(column.getMetadataObject().getName());
    			}
    			else {
    				buffer.append(this.visitor.correlatedName.get(name)).append(Tokens.DOT).append(column.getMetadataObject().getName());
    			}
    		}   
    		else {
    			buffer.append(column.getName());
    		}
    	}    	
    }
}
