Skip to content

Commit

Permalink
use a common ConnectionProfileDetailsPage in both the CreateConnectio…
Browse files Browse the repository at this point in the history
…nProfileCommand and the ConnectionProfileDetailsStep
  • Loading branch information
koentsje committed Sep 24, 2013
1 parent 79a3699 commit 11281c2
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 194 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package org.hibernate.forge.addon.connections;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.inject.Inject;

import org.hibernate.forge.addon.util.HibernateToolsHelper;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIValidationContext;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.metadata.WithAttributes;

public class ConnectionProfileDetailsPage
{

@Inject
@WithAttributes(
label = "JDBC URL",
description = "The jdbc url for the database tables",
required = true)
protected UIInput<String> jdbcUrl;

@Inject
@WithAttributes(
label = "User Name",
description = "The user name for the database connection",
required = true)
protected UIInput<String> userName;

@Inject
@WithAttributes(
label = "User Password",
description = "The password for the database connection",
required = false,
defaultValue = "")
protected UIInput<String> userPassword;

@Inject
@WithAttributes(
label = "Hibernate Dialect",
description = "The Hibernate dialect to use",
required = true)
protected UIInput<String> hibernateDialect;

@Inject
@WithAttributes(
label = "Driver Location",
description = "The location of the jar file that contains the JDBC driver",
required = true)
protected UIInput<FileResource<?>> driverLocation;

@Inject
@WithAttributes(
label = "Driver Class",
description = "The class name of the JDBC driver",
required = true)
protected UIInput<String> driverClass;

@Inject
private HibernateToolsHelper helper;

protected URL[] urls;
protected Driver driver;

public void initializeUI(UIBuilder builder) throws Exception
{
builder
.add(jdbcUrl)
.add(userName)
.add(userPassword)
.add(hibernateDialect)
.add(driverLocation)
.add(driverClass);

}

public void validate(UIValidationContext context)
{
File file = getDriverLocation(context);
if (file == null) return;
urls = getDriverUrls(file, context);
if (urls == null) {
return;
}
driver = getDriver(urls, context);
if (driver == null) {
return;
}
}

private File getDriverLocation(UIValidationContext context) {
FileResource<?> resource = driverLocation.getValue();
if (!resource.exists()) {
context.addValidationError(driverLocation, "The location '" + resource.getFullyQualifiedName() + "' does not exist");
return null;
}
return resource.getUnderlyingResourceObject();
}

private URL[] getDriverUrls(File file, UIValidationContext context)
{
try {
ArrayList<URL> result = new ArrayList<URL>(1);
result.add(file.toURI().toURL());
return result.toArray(new URL[1]);
} catch (MalformedURLException e) {
context.addValidationError(driverLocation,
"The location '" +
driverLocation.getValue() +
"' does not point to a valid file");
return null;
}
}

private Driver getDriver(URL[] urls, UIValidationContext context) {
Driver result = null;
String className = driverClass.getValue();
try
{
result = helper.getDriver(className, urls);
}
catch (InstantiationException e)
{
context.addValidationError(
driverClass,
"The class '" + className + "' cannot not be instantiated");
}
catch (IllegalAccessException e)
{
context.addValidationError(
driverClass,
"Illegal access for class '" + className + "'");
}
catch (ClassNotFoundException e)
{
context.addValidationError(
driverClass,
"The class '" + className + "' does not exist");
}
catch (SQLException e) {
context.addValidationError(
driverClass,
"An unexpected SQLException happened while registering class '" + className + "'");
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import javax.inject.Inject;

import org.jboss.forge.addon.ui.AbstractUICommand;
import org.jboss.forge.addon.ui.UICommand;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.input.UIInput;
Expand All @@ -14,9 +14,9 @@
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Metadata;

public class CreateConnectionProfileCommand extends AbstractUICommand
public class CreateConnectionProfileCommand extends ConnectionProfileDetailsPage implements UICommand
{

private static final String[] COMMAND_CATEGORY = { "Database", "Connections" };
private static final String COMMAND_NAME = "Connection Profile: Create";
private static final String COMMAND_DESCRIPTION = "Command to create a database connectin profile.";
Expand All @@ -31,54 +31,11 @@ public class CreateConnectionProfileCommand extends AbstractUICommand
required = true)
private UIInput<String> name;

@Inject
@WithAttributes(
label = "JDBC URL",
description = "The jdbc url for the database tables",
required = true)
private UIInput<String> jdbcUrl;

@Inject
@WithAttributes(
label = "User Name",
description = "The user name for the database connection",
required = true)
private UIInput<String> userName;

@Inject
@WithAttributes(
label = "User Password",
description = "The password for the database connection",
required = false,
defaultValue = "")
private UIInput<String> userPassword;

@Inject
@WithAttributes(
label = "Hibernate Dialect",
description = "The Hibernate dialect to use",
required = true)
private UIInput<String> hibernateDialect;

@Inject
@WithAttributes(
label = "Driver Location",
description = "The location of the jar file that contains the JDBC driver",
required = true)
private UIInput<String> driverLocation;

@Inject
@WithAttributes(
label = "Driver Class",
description = "The class name of the JDBC driver",
required = true)
private UIInput<String> driverClass;

@Override
public Metadata getMetadata(UIContext context)
{
return Metadata
.from(super.getMetadata(context), getClass())
.forCommand(getClass())
.name(COMMAND_NAME)
.description(COMMAND_DESCRIPTION)
.category(Categories.create(COMMAND_CATEGORY));
Expand All @@ -87,15 +44,8 @@ public Metadata getMetadata(UIContext context)
@Override
public void initializeUI(UIBuilder builder) throws Exception
{
builder
.add(name)
.add(jdbcUrl)
.add(userName)
.add(userPassword)
.add(hibernateDialect)
.add(driverLocation)
.add(driverClass);

builder.add(name);
super.initializeUI(builder);
}

@Override
Expand All @@ -107,7 +57,7 @@ public Result execute(UIContext context) throws Exception
connectionProfile.name = name.getValue();
connectionProfile.dialect = hibernateDialect.getValue();
connectionProfile.driver = driverClass.getValue();
connectionProfile.path = driverLocation.getValue();
connectionProfile.path = driverLocation.getValue().getFullyQualifiedName();
connectionProfile.url = jdbcUrl.getValue();
connectionProfile.user = userName.getValue();
connectionProfiles.put(name.getValue(), connectionProfile);
Expand All @@ -118,4 +68,10 @@ public Result execute(UIContext context) throws Exception
" has been saved succesfully");
}

@Override
public boolean isEnabled(UIContext context)
{
return true;
}

}

0 comments on commit 11281c2

Please sign in to comment.