Skip to content

Commit

Permalink
Implemented [FORGE-1595] - Being able to create an empty JSF backing …
Browse files Browse the repository at this point in the history
…bean
  • Loading branch information
agoncal committed May 8, 2014
1 parent a37d7b5 commit 2b6e752
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@
*/
public class FacesOperations
{
/**
* Creates a new {@link JavaResource} in the specified project. If no project is available, use
* {@link FacesOperations#newBackingBean(DirectoryResource, String, String)}
*
* @param project the current project to create the backing bean. Must not be null
* @param backingBeanName the name of the backing bean
* @param backingBeanPackage the package of the backing bean to be created
* @return the created {@link JavaResource}
*/
public JavaResource newBackingBean(Project project, String backingBeanName, String backingBeanPackage)
throws FileNotFoundException
{
final JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
JavaClassSource javaClass = createBackingBean(backingBeanName, backingBeanPackage);
return java.saveJavaSource(javaClass);
}

/**
* Creates a new {@link JavaResource} in the specified target. If a project is available, use
* {@link FacesOperations#newBackingBean(Project, String, String)}
*
* @param target the target directory resource to create the backing bean
* @param backingBeanName the name of the backing bean
* @param backingBeanPackage the package of the backing bean to be created
* @return the created {@link JavaResource}
*/
public JavaResource newBackingBean(DirectoryResource target, String backingBeanName, String backingBeanPackage)
{
JavaClassSource javaClass = createBackingBean(backingBeanName, backingBeanPackage);
JavaResource javaResource = getJavaResource(target, javaClass.getName());
javaResource.setContents(javaClass);
return javaResource;
}

/**
* Creates a new {@link JavaResource} in the specified project. If no project is available, use
* {@link FacesOperations#newConverter(DirectoryResource, String, String)}
Expand Down Expand Up @@ -72,6 +106,14 @@ private JavaClassSource createConverter(String converterName, String converterPa
return source;
}

private JavaClassSource createBackingBean(String beanName, String beanPackage)
{
JavaClassSource source = Roaster.parse(JavaClassSource.class, getClass().getResourceAsStream("BackingBean.jv"));
source.setName(beanName);
source.setPackage(beanPackage);
return source;
}

/**
* Creates a new {@link JavaResource} in the specified project. If no project is available, use
* {@link FacesOperations#newValidator(DirectoryResource, String, String)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* 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 org.jboss.forge.addon.javaee.cdi.CDIFacet;
import org.jboss.forge.addon.javaee.cdi.ui.CDISetupCommand;
import org.jboss.forge.addon.javaee.faces.FacesFacet;
import org.jboss.forge.addon.javaee.faces.FacesOperations;
import org.jboss.forge.addon.javaee.ui.AbstractJavaEECommand;
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;
import org.jboss.forge.addon.projects.facets.MetadataFacet;
import org.jboss.forge.addon.resource.DirectoryResource;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.ui.command.PrerequisiteCommandsProvider;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.context.UISelection;
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.NavigationResult;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.result.Results;
import org.jboss.forge.addon.ui.result.navigation.NavigationResultBuilder;
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Metadata;

import javax.inject.Inject;

/**
* Creates a new JSF Backing Bean
*
* @author <a href="antonio.goncalves@gmail.com">Antonio Goncalves</a>
*/
public class FacesNewBeanCommand extends AbstractFacesCommand implements PrerequisiteCommandsProvider
{

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

@Inject
@WithAttributes(label = "Target package", type = InputType.JAVA_PACKAGE_PICKER)
private UIInput<String> targetPackage;

@Inject
@WithAttributes(label = "Target Directory", required = true)
private UIInput<DirectoryResource> targetLocation;

@Inject
private FacesOperations facesOperations;

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

@Override
public void initializeUI(UIBuilder builder) throws Exception
{
Project project = getSelectedProject(builder.getUIContext());
if (project == null)
{
UISelection<FileResource<?>> currentSelection = builder.getUIContext().getInitialSelection();
if (!currentSelection.isEmpty())
{
FileResource<?> resource = currentSelection.get();
if (resource instanceof DirectoryResource)
{
targetLocation.setDefaultValue((DirectoryResource) resource);
}
else
{
targetLocation.setDefaultValue(resource.getParent());
}
}
}
else if (project.hasFacet(JavaSourceFacet.class))
{
JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);
targetLocation.setDefaultValue(facet.getSourceDirectory()).setEnabled(false);
targetPackage.setValue(calculateBackingBeanPackage(project));
}
builder.add(targetLocation);
builder.add(targetPackage).add(named);
}

private String calculateBackingBeanPackage(Project project)
{
return project.getFacet(MetadataFacet.class).getTopLevelPackage() + ".view";
}

@Override
public Result execute(UIExecutionContext context) throws Exception
{
String beanName = named.getValue();
String beanPackage = targetPackage.getValue();
DirectoryResource targetDir = targetLocation.getValue();
UIContext uiContext = context.getUIContext();
Project project = getSelectedProject(uiContext);
JavaResource javaResource;
if (project == null)
{
javaResource = facesOperations.newBackingBean(targetDir, beanName, beanPackage);
}
else
{
javaResource = facesOperations.newBackingBean(project, beanName, beanPackage);
}
uiContext.setSelection(javaResource);
return Results.success("Backing bean " + javaResource + " created");
}

@Override
protected boolean isProjectRequired()
{
return true;
}

@Override
public NavigationResult getPrerequisiteCommands(UIContext context)
{
NavigationResultBuilder builder = NavigationResultBuilder.create();
Project project = getSelectedProject(context);
if (project != null)
{
if (!project.hasFacet(CDIFacet.class))
{
builder.add(CDISetupCommand.class);
}
if (!project.hasFacet(FacesFacet.class))
{
builder.add(FacesSetupWizard.class);
}
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.test.view;

import javax.inject.Named;

@Named
public class ValueBean
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,37 @@ public static ForgeArchive getDeployment()
@Inject
private FacesOperations operations;

@Test
public void testCreateBackingBeanInDirectory() throws Exception
{
DirectoryResource dir = (DirectoryResource) resourceFactory.create(OperatingSystemUtils.createTempDir());
JavaResource bean = operations.newConverter(dir, "SampleBean", "org.example");

Assert.assertEquals("SampleBean.java", bean.getName());
Assert.assertEquals("SampleBean", bean.getJavaType().getName());
Assert.assertEquals("org.example", bean.getJavaType().getPackage());
Assert.assertTrue(bean.exists());
}

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

JavaResource bean = operations.newConverter(project, "SampleBean", "org.example");
Assert.assertTrue(bean.exists());

Assert.assertEquals("SampleBean.java", bean.getName());
Resource<?> child = project.getRootDirectory().getChild("src/main/java/org/example/SampleBean.java");
Assert.assertTrue(child.exists());
Assert.assertTrue(child instanceof JavaResource);
Assert.assertEquals("SampleBean", ((JavaResource) child).getJavaType().getName());
Assert.assertEquals("org.example", ((JavaResource) child).getJavaType().getPackage());
}

@Test
public void testCreateConverterInDirectory() throws Exception
{
Expand Down

0 comments on commit 2b6e752

Please sign in to comment.