/*
 * Copyright (C) 2005 NTT DATA Corporation
 * 
 */
package org.postgresforest.tool.view.action;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IViewPart;
import org.postgresforest.tool.action.DbAction;
import org.postgresforest.tool.action.DbInstanceAction;
import org.postgresforest.tool.bean.AddDbInfo;
import org.postgresforest.tool.db.GSCdata;
import org.postgresforest.tool.dialog.adddb.MakeDbDialog;
import org.postgresforest.tool.util.ErrorLog;
import org.postgresforest.tool.util.MsgDialogUtil;
import org.postgresforest.tool.util.SessionProperty;
import org.postgresforest.tool.view.ViewUtil;
import org.postgresforest.tool.view.menu.AdminMenuView;

/**
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class AddDbAction extends ActionBase {
    public static final String ID = "org.postgresforest.tool.view.action.AddDbAction";
	private MakeDbDialog m_dialog;
	
	public void init(IViewPart view) {
		super.init(view);
		m_dialog = new MakeDbDialog();

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	public void run(IAction action) {
		GSCdata gsc = (GSCdata)SessionProperty.get("gsc");

		//セッション･プロパティからログイン情報を取得
		DbInstanceAction dbInstance = new DbInstanceAction(gsc);
		AddDbInfo addDbInfo;
		try {
			addDbInfo = dbInstance.getAddDbInfo();
		} catch (Exception e) {
			ErrorLog.log("", e);
		    MsgDialogUtil.err(e.getLocalizedMessage());
			return;
		}

		
		//データベース作成ダイアログを表示する
		m_dialog.setAddDb(addDbInfo);
		if (m_dialog.open() == Window.OK) {
			addDbInfo = m_dialog.getAddDb();
			//DB作成
			DbAction addDb = new DbAction(gsc);
			if( !addDb.add(addDbInfo) ){
				return;
			}
			
			//データベースビューを更新
			m_view.update();
			
			//管理者メニューを更新
			ViewUtil.updateView(AdminMenuView.ID);

		}		
	}


}
