Skip to content

Commit

Permalink
FORGE-1603: AbstractJavaSourceCommand should have an overwrite flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Matej Briskar authored and gastaldi committed Apr 16, 2014
1 parent 8b8d27a commit 5806341
Showing 1 changed file with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

package org.jboss.forge.addon.parser.java.ui;

import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Callable;

import javax.inject.Inject;

Expand Down Expand Up @@ -61,8 +63,12 @@ public abstract class AbstractJavaSourceCommand extends AbstractProjectCommand
@WithAttributes(label = "Type Name", required = true)
private UIInput<String> named;

@Inject
@WithAttributes(label = "Overwrite", description = "The overwrite flag that is used if the class already exists.", defaultValue = "false")
private UIInput<Boolean> overwrite;

@Override
public void initializeUI(UIBuilder builder) throws Exception
public void initializeUI(final UIBuilder builder) throws Exception
{
// Setup named
named.addValidator(new UIValidator()
Expand All @@ -75,6 +81,18 @@ public void validate(UIValidationContext context)
}
});

overwrite.setEnabled(new Callable<Boolean>()
{
@Override
public Boolean call()
{
UIContext uiContext = builder.getUIContext();
Project project = getSelectedProject(uiContext);
JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class);
return classExists(javaSourceFacet);
}
});

// Setup targetPackage
Project project = getSelectedProject(builder);
if (project != null && project.hasFacet(JavaSourceFacet.class))
Expand Down Expand Up @@ -110,7 +128,7 @@ public Iterable<String> getCompletionProposals(UIContext context, InputComponent
});
}
targetPackage.setDefaultValue(calculateDefaultPackage(builder.getUIContext()));
builder.add(targetPackage).add(named);
builder.add(targetPackage).add(named).add(overwrite);
}

@Override
Expand All @@ -131,14 +149,30 @@ public UICommandMetadata getMetadata(UIContext context)
*/
protected abstract Class<? extends JavaSource<?>> getSourceType();

@Override
public Result execute(UIExecutionContext context) throws Exception
private boolean classExists(JavaSourceFacet javaSourceFacet)
{
UIContext uiContext = context.getUIContext();
Project project = getSelectedProject(uiContext);
JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class);
JavaSource<?> source = buildJavaSource(javaSourceFacet);
if(source==null) {
return true;
}
boolean classAlreadyExists;
try
{
JavaResource parsedJavaResource = javaSourceFacet.getJavaResource(source);
classAlreadyExists = parsedJavaResource.exists();
}
catch (FileNotFoundException ex)
{
classAlreadyExists = false;
}
return classAlreadyExists;
}

private JavaSource<?> buildJavaSource(JavaSourceFacet javaSourceFacet)
{
try{
JavaSource<?> source = Roaster.create(getSourceType()).setName(named.getValue());
JavaResource javaResource;

if (targetPackage.hasValue() || targetPackage.hasDefaultValue())
{
source.setPackage(targetPackage.getValue());
Expand All @@ -147,6 +181,21 @@ public Result execute(UIExecutionContext context) throws Exception
{
source.setPackage(javaSourceFacet.getBasePackage());
}

return source;
} catch(IllegalStateException ex) {
return null;
}
}

@Override
public Result execute(UIExecutionContext context) throws Exception
{
UIContext uiContext = context.getUIContext();
Project project = getSelectedProject(uiContext);
JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class);
JavaSource<?> source = buildJavaSource(javaSourceFacet);
JavaResource javaResource;
if (source.hasSyntaxErrors())
{
UIOutput output = uiContext.getProvider().getOutput();
Expand All @@ -161,6 +210,11 @@ public Result execute(UIExecutionContext context) throws Exception
}
else
{
boolean classAlreadyExists = classExists(javaSourceFacet);
if (classAlreadyExists && overwrite.isEnabled() && overwrite.getValue() == false)
{
return Results.fail("The Java class already exists and the --overwrite flag was not set.");
}
javaResource = javaSourceFacet.saveJavaSource(source);
}
uiContext.setSelection(javaResource);
Expand All @@ -183,6 +237,11 @@ protected UIInput<String> getNamed()
return named;
}

protected UIInput<Boolean> getOverwrite()
{
return overwrite;
}

protected String calculateDefaultPackage(UIContext context)
{
String packageName;
Expand Down

0 comments on commit 5806341

Please sign in to comment.