Skip to content

Commit

Permalink
Implement FORGE-1285: Create faces commands to add validator methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Oct 24, 2013
1 parent d5ef80b commit 5f3593e
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 23 deletions.
Expand Up @@ -9,6 +9,8 @@

import java.io.FileNotFoundException;

import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;

import org.jboss.forge.addon.parser.java.JavaSourceFactory;
Expand All @@ -17,6 +19,7 @@
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.resource.DirectoryResource;
import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.parser.java.Method;

/**
* This class contains Faces specific operations
Expand Down Expand Up @@ -121,4 +124,17 @@ private JavaResource getJavaResource(final DirectoryResource sourceDir, final St
JavaResource target = sourceDir.getChildOfType(JavaResource.class, path);
return target;
}

public Method<JavaClass> addValidatorMethod(JavaResource target, String name) throws FileNotFoundException
{
JavaClass source = (JavaClass) target.getJavaSource();
Method<JavaClass> method = source.addMethod().setName(name)
.setParameters("final FacesContext context, final UIComponent component, final Object value")
.setBody("throw new ValidatorException(new FacesMessage(\"Validator not yet implemented.\"));")
.addThrows(ValidatorException.class);
method.getOrigin().addImport(ValidatorException.class);
method.getOrigin().addImport(FacesMessage.class);
target.setContents(source);
return method;
}
}
Expand Up @@ -136,7 +136,7 @@ public Result execute(UIContext context) throws Exception
javaResource = facesOperations.newConverter(project, entityName, entityPackage);
}
context.setSelection(javaResource);
return Results.success("Entity " + javaResource + " created");
return Results.success("Converter " + javaResource + " created");
}

@Override
Expand Down
Expand Up @@ -136,7 +136,7 @@ public Result execute(UIContext context) throws Exception
javaResource = facesOperations.newValidator(project, entityName, entityPackage);
}
context.setSelection(javaResource);
return Results.success("Entity " + javaResource + " created");
return Results.success("Validator " + javaResource + " created");
}

@Override
Expand Down
@@ -0,0 +1,101 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.addon.javaee.faces.ui;

import java.io.FileNotFoundException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import org.jboss.forge.addon.javaee.faces.FacesOperations;
import org.jboss.forge.addon.javaee.ui.AbstractJavaEECommand;
import org.jboss.forge.addon.parser.java.resources.JavaResource;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIValidationContext;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.metadata.WithAttributes;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.result.Results;
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Metadata;
import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.parser.java.Method;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class NewValidatorMethodCommand extends AbstractJavaEECommand
{

@Inject
@WithAttributes(label = "Target Java class", description = "The Java class in which the method will be created", required = true, type = InputType.JAVA_CLASS_PICKER)
private UIInput<JavaResource> target;

@Inject
@WithAttributes(label = "Validator method name", required = true)
private UIInput<String> named;

@Inject
private FacesOperations operations;

@Override
public Metadata getMetadata(UIContext context)
{
return Metadata.from(super.getMetadata(context), getClass()).name("Faces: New Validator Method")
.description("Create a new JSF validator method")
.category(Categories.create(super.getMetadata(context).getCategory(), "JSF"));
}

@Override
public void initializeUI(UIBuilder builder) throws Exception
{
Object selection = builder.getUIContext().getInitialSelection().get();
if (selection instanceof JavaResource)
{
target.setDefaultValue((JavaResource) selection);
}
builder.add(target).add(named);
}

@Override
public void validate(UIValidationContext validator)
{
try
{
JavaClass source = (JavaClass) target.getValue().reify(JavaResource.class).getJavaSource();
Method<JavaClass> method = source.getMethod(named.getValue(), FacesContext.class, UIComponent.class,
Object.class);

if (method != null)
validator.addValidationError(named, "A validator with that name already exists in '"
+ target.getValue().getJavaSource().getQualifiedName() + "'");

super.validate(validator);
}
catch (FileNotFoundException e)
{
validator.addValidationError(target, "Target Java class not found.");
}
}

@Override
public Result execute(UIContext context) throws Exception
{
Method<JavaClass> method = operations.addValidatorMethod(target.getValue().reify(JavaResource.class),
named.getValue());
return Results.success("Validator method '" + method.toSignature() + "' created");
}

@Override
protected boolean isProjectRequired()
{
return false;
}
}
Expand Up @@ -7,13 +7,13 @@ import javax.faces.convert.FacesConverter;
public class ValueConverter implements Converter
{
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value)
public Object getAsObject(final FacesContext context, final UIComponent component, final String value)
{
throw new UnsupportedOperationException("not yet implemented");
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value)
public String getAsString(final FacesContext context, final UIComponent component, final Object value)
{
return value.toString();
}
Expand Down
Expand Up @@ -9,7 +9,7 @@ import javax.faces.validator.ValidatorException;
public class ValueValidator implements Validator
{
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
public void validate(final FacesContext context, final UIComponent component, final Object value) throws ValidatorException
{
throw new ValidatorException(new FacesMessage("Validator not yet implemented."));
}
Expand Down
Expand Up @@ -12,6 +12,7 @@
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.facets.FacetFactory;
import org.jboss.forge.addon.parser.java.JavaSourceFactory;
import org.jboss.forge.addon.parser.java.facets.JavaSourceFacet;
import org.jboss.forge.addon.parser.java.resources.JavaResource;
import org.jboss.forge.addon.projects.Project;
Expand All @@ -25,6 +26,8 @@
import org.jboss.forge.arquillian.archive.ForgeArchive;
import org.jboss.forge.furnace.repositories.AddonDependencyEntry;
import org.jboss.forge.furnace.util.OperatingSystemUtils;
import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.parser.java.Method;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -65,6 +68,9 @@ public static ForgeArchive getDeployment()
@Inject
private FacesOperations operations;

@Inject
private JavaSourceFactory sourceFactory;

@Test
public void testCreateConverterInDirectory() throws Exception
{
Expand Down Expand Up @@ -125,4 +131,23 @@ public void testCreateValidatorInProject() throws Exception
Assert.assertEquals("org.example", ((JavaResource) child).getJavaSource().getPackage());
}

@Test
public void testCreateValidatorMethod() throws Exception
{
Project project = projectFactory.createTempProject();
facetFactory.install(project, ResourcesFacet.class);
facetFactory.install(project, JavaSourceFacet.class);

JavaSourceFacet sourceFacet = project.getFacet(JavaSourceFacet.class);
JavaResource resource = sourceFacet.saveJavaSource(sourceFactory
.parse("package org.example; public class DemoBean {}"));

Method<JavaClass> method = operations.addValidatorMethod(resource, "validateUsername");
Assert.assertEquals(3, method.getParameters().size());

JavaClass source = (JavaClass) resource.getJavaSource();
Assert.assertEquals(1, source.getMethods().size());
Assert.assertEquals(method.toSignature(), source.getMethods().get(0).toSignature());
}

}

0 comments on commit 5f3593e

Please sign in to comment.