package geisplugin.popup.actions;

import geisplugin.Tools;

import java.io.File;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * DE: 
 *
 * EN: 
 * 
 * 
 * Program changes
 * **************************************************************************************
 * Date			* Ticket	* KonzFirm	* Responsible	* programmer    *
 * Change										*
 * **************************************************************************************
 *
 * @author huellmante
 * @version 1.0
 *
 */

public class GenerateMapping extends AbstractHandler {

	@Override
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		if (Tools.isNotUpToDate()) {
			return null;
		}
		Shell shell = HandlerUtil.getActiveShell(event);

		String path = "";
		try {
			path = Tools.getCurrentSelectedProject().getLocation().toFile().toString().replace('/', '\\') + "\\";
		} catch (Exception e) {
		}

		if (path.isEmpty()) {
			MessageDialog.openInformation(shell, "Info", "Error, can't find a valid DB path, using desktop");
			path = "C:\\Users" + System.getProperty("user.name") + "\\Desktop";
		} else {
			if (new File(path + "\\src\\main\\java\\de\\geis\\datenbanken").isDirectory()) {
				path += "\\src\\main\\java\\de\\geis\\datenbanken";
			} else if (new File(path + "src\\all\\geis\\db").isDirectory()) {
				path += "src\\all\\geis\\db";
			} else
				if (new File(path + "src\\de\\geis\\db").isDirectory()) {
					path += "src\\de\\geis\\db";
				} else {
					path += "src\\de\\geis\\datenbanken";
				}
		}

		new GenerateHibernateMapping(shell, path);

		try {
			Tools.getCurrentSelectedProject().refreshLocal(IResource.DEPTH_INFINITE, null);
		} catch (final Exception e) {
			MessageDialog.openError(shell, "Info", "Error, can't refresh your workplace automatically");
		}

		return null;
	}
}
