package br.cefetrj.sagitarii.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import br.cefetrj.sagitarii.core.TableAttribute;
import br.cefetrj.sagitarii.persistence.services.RelationService;

@Action (value = "editTable", 
results = { 
	@Result ( location = "editTable.jsp", name = "ok")
} , interceptorRefs= { @InterceptorRef("seguranca")	 }) 

@ParentPackage("default")
public class EditTableAction extends BasicActionClass {
	private String tableName;
	private List<TableAttribute> attributes;
	private String columnName;
	private String columnType;
	private String op;
	
	public String execute(){
		
		try {
			
			if ( op != null ) {
				// Remove column
				if ( op.equals("remove") ) {
					if ( columnName != null && tableName != null ) {
						new RelationService().dropColumn( tableName, columnName ); 
						setMessageText("Column " + columnName + " deleted from table " + tableName );
					}
				} else
				// Add column
				if ( op.equals("add") ) {
					if ( (columnName != null) && (tableName != null) && (columnType != null) ) {
						new RelationService().addColumn( tableName, columnName, columnType );
						setMessageText("Column " + columnName + " added for table " + tableName );
					}
				}
			}
			
		} catch ( Exception e ) {
			setMessageText( e.getMessage() );
		}

		
		try {
			attributes = new RelationService().getAttributes( tableName );
		} catch ( Exception e ) {
			setMessageText( e.getMessage() );
		}
		
		return "ok";
	}


	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public List<TableAttribute> getAttributes() {
		return attributes;
	}
	
	public void setOp(String op) {
		this.op = op;
	}

	public String getTableName() {
		return tableName;
	}
	
	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}
	
	public void setColumnType(String columnType) {
		this.columnType = columnType;
	}
	
}

@@ -496,21 +496,15 @@
  = %22
-editTable.jsp
+$%7Bdest%7D
 %22, n
@@ -810,16 +810,40 @@
 ng op;%0D%0A
+%09private String dest; %0D%0A
 %09%0D%0A%09publ
@@ -864,16 +864,62 @@
 ute()%7B%0D%0A
+%09%09dest = %22editTable?tableName=%22 + tableName;%0D%0A
 %09%09%0D%0A%09%09tr
@@ -2263,11 +2263,59 @@
 %0A%09%7D%0D%0A%09%0D%0A
+%09public String getDest() %7B%0D%0A%09%09return dest;%0D%0A%09%7D%0D%0A
 %7D%0D%0A

Edit table screen redirect to correct page after insert new attribute.