Skip to content

Commit

Permalink
FORGE-1486: Implemented faces-project-stage
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jan 21, 2014
1 parent ab6f090 commit cb815fe
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public interface FacesFacet<DESCRIPTOR> extends JavaEEFacet, Configurable<DESCRI
{
ProjectStage getProjectStage();

void setProjectStage(ProjectStage projectStage);

List<String> getFaceletsViewMappings();

List<String> getFaceletsDefaultSuffixes();
Expand All @@ -50,4 +52,5 @@ public interface FacesFacet<DESCRIPTOR> extends JavaEEFacet, Configurable<DESCRI
List<String> getEffectiveFacesServletMappings();

List<String> getFacesServletMappings();

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.jboss.forge.parser.xml.XMLParser;
import org.jboss.shrinkwrap.descriptor.api.Descriptor;
import org.jboss.shrinkwrap.descriptor.api.DescriptorImporter;
import org.jboss.shrinkwrap.descriptor.api.javaee.ParamValueCommonType;
import org.jboss.shrinkwrap.descriptor.api.javaee5.ParamValueType;
import org.jboss.shrinkwrap.descriptor.api.webapp.WebAppCommonDescriptor;
import org.jboss.shrinkwrap.descriptor.api.webapp30.WebAppDescriptor;
Expand Down Expand Up @@ -167,8 +168,8 @@ public ProjectStage getProjectStage()
{
ServletFacet<?> servlet = getFaceted().getFacet(ServletFacet.class);
WebAppCommonDescriptor config = servlet.getConfig();
List<ParamValueType<WebAppCommonDescriptor>> params = config.getAllContextParam();
for (ParamValueType<WebAppCommonDescriptor> param : params)
List<ParamValueCommonType> params = config.getAllContextParam();
for (ParamValueCommonType param : params)
{
if (JAVAX_FACES_PROJECT_STAGE.equals(param.getParamName()))
{
Expand All @@ -180,6 +181,34 @@ public ProjectStage getProjectStage()
return ProjectStage.Production;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void setProjectStage(ProjectStage projectStage)
{
if (getFaceted().hasFacet(ServletFacet.class))
{
ServletFacet servlet = getFaceted().getFacet(ServletFacet.class);
WebAppCommonDescriptor config = (WebAppCommonDescriptor) servlet.getConfig();
List<ParamValueCommonType> params = config.getAllContextParam();
ParamValueCommonType projectStageParam = null;
for (ParamValueCommonType param : params)
{
if (JAVAX_FACES_PROJECT_STAGE.equals(param.getParamName()))
{
projectStageParam = param;
break;
}
}
if (projectStageParam == null)
{
projectStageParam = config.createContextParam();
projectStageParam.paramName(JAVAX_FACES_PROJECT_STAGE);
}
projectStageParam.paramValue(projectStage.toString());
servlet.saveConfig(config);
}
}

@Override
public List<String> getFacesServletMappings()
{
Expand Down Expand Up @@ -455,7 +484,7 @@ public List<String> getFaceletsViewMappings()

return suffixes;
}

protected abstract void createDefaultConfig(FileResource<?> descriptor);

private ServletMappingHelper helper = new ServletMappingHelper();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright 2014 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 javax.faces.application.ProjectStage;
import javax.inject.Inject;

import org.jboss.forge.addon.javaee.faces.FacesFacet;
import org.jboss.forge.addon.javaee.servlet.ServletFacet;
import org.jboss.forge.addon.javaee.ui.AbstractJavaEECommand;
import org.jboss.forge.addon.projects.Project;
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.input.UISelectOne;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
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;

/**
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public class ProjectStageCommand extends AbstractJavaEECommand
{

@Inject
@WithAttributes(label = "")
private UISelectOne<ProjectStage> set;

@Override
public void initializeUI(UIBuilder builder) throws Exception
{
builder.add(set);
}

@Override
public UICommandMetadata getMetadata(UIContext context)
{
return Metadata.from(super.getMetadata(context), getClass()).name("Faces: Project Stage")
.description("Set the project stage of this JSF project")
.category(Categories.create(super.getMetadata(context).getCategory(), "JSF"));
}

@SuppressWarnings("rawtypes")
@Override
public Result execute(UIExecutionContext context) throws Exception
{
final Result result;
Project project = getSelectedProject(context.getUIContext());
FacesFacet facesFacet = project.getFacet(FacesFacet.class);
if (set.hasValue())
{
ProjectStage projectStage = set.getValue();
facesFacet.setProjectStage(projectStage);
result = Results.success("Faces PROJECT_STAGE updated to: " + projectStage);
}
else
{
result = Results.success("Project stage is currently: " + facesFacet.getProjectStage());
}
return result;
}

@Override
public boolean isEnabled(UIContext context)
{
boolean enabled = super.isEnabled(context);
if (enabled)
{
final Project project = getSelectedProject(context);
enabled = project.hasFacet(ServletFacet.class) && project.hasFacet(FacesFacet.class);
}
return enabled;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

import org.jboss.forge.addon.facets.FacetFactory;
import org.jboss.forge.addon.javaee.ejb.EJBFacet_3_2;
import org.jboss.forge.addon.javaee.faces.FacesFacet_2_2;
import org.jboss.forge.addon.javaee.jpa.JPAFacet;
import org.jboss.forge.addon.javaee.jpa.JPAFacet_2_0;
import org.jboss.forge.addon.javaee.jpa.PersistenceOperations;
import org.jboss.forge.addon.javaee.servlet.ServletFacet_3_1;
import org.jboss.forge.addon.parser.java.projects.JavaProjectType;
import org.jboss.forge.addon.parser.java.projects.JavaWebProjectType;
import org.jboss.forge.addon.parser.java.resources.JavaResource;
Expand Down Expand Up @@ -70,6 +72,22 @@ public EJBFacet_3_2 installEJB_3_2(Project project)
return facetFactory.install(project, EJBFacet_3_2.class);
}

/**
* Installs the {@link ServletFacet_3_1} facet
*/
public ServletFacet_3_1 installServlet_3_1(Project project)
{
return facetFactory.install(project, ServletFacet_3_1.class);
}

/**
* Installs the {@link FacesFacet_2_2} facet
*/
public FacesFacet_2_2 installFaces_2_2(Project project)
{
return facetFactory.install(project, FacesFacet_2_2.class);
}

/**
* Installs the {@link JPAFacet} facet
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright 2014 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.setup;

import javax.faces.application.ProjectStage;
import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.javaee.ProjectHelper;
import org.jboss.forge.addon.javaee.faces.FacesFacet_2_2;
import org.jboss.forge.addon.javaee.faces.ui.ProjectStageCommand;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.ui.controller.CommandController;
import org.jboss.forge.arquillian.AddonDependency;
import org.jboss.forge.arquillian.Dependencies;
import org.jboss.forge.arquillian.archive.ForgeArchive;
import org.jboss.forge.furnace.repositories.AddonDependencyEntry;
import org.jboss.forge.ui.test.UITestHarness;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
@RunWith(Arquillian.class)
public class ProjectStageCommandTest
{
@Deployment
@Dependencies({
@AddonDependency(name = "org.jboss.forge.addon:ui"),
@AddonDependency(name = "org.jboss.forge.addon:ui-test-harness"),
@AddonDependency(name = "org.jboss.forge.addon:javaee"),
@AddonDependency(name = "org.jboss.forge.addon:projects"),
@AddonDependency(name = "org.jboss.forge.addon:maven")
})
public static ForgeArchive getDeployment()
{
return ShrinkWrap
.create(ForgeArchive.class)
.addClass(ProjectHelper.class)
.addBeansXML()
.addAsAddonDependencies(
AddonDependencyEntry.create("org.jboss.forge.furnace.container:cdi"),
AddonDependencyEntry.create("org.jboss.forge.addon:projects"),
AddonDependencyEntry.create("org.jboss.forge.addon:javaee"),
AddonDependencyEntry.create("org.jboss.forge.addon:maven"),
AddonDependencyEntry.create("org.jboss.forge.addon:ui"),
AddonDependencyEntry.create("org.jboss.forge.addon:ui-test-harness")
);
}

@Inject
private ProjectHelper projectHelper;

@Inject
private UITestHarness testHarness;

@Test
public void testSetProjectStage() throws Exception
{
Project project = projectHelper.createWebProject();
projectHelper.installServlet_3_1(project);
FacesFacet_2_2 facesFacet = projectHelper.installFaces_2_2(project);
Assert.assertEquals(ProjectStage.Production, facesFacet.getProjectStage());
try (CommandController controller = testHarness.createCommandController(ProjectStageCommand.class,
project.getProjectRoot()))
{
controller.initialize();
Assert.assertTrue(controller.isEnabled());
controller.setValueFor("set", ProjectStage.Development);
Assert.assertTrue(controller.canExecute());
controller.execute();
Assert.assertEquals(ProjectStage.Development, facesFacet.getProjectStage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public Project findProject(FileResource<?> target, Predicate<Project> filter)
@Override
public Project findProject(FileResource<?> target, BuildSystem buildSystem, Predicate<Project> filter)
{
Assert.notNull(target, "Target cannot be null");
if (filter == null)
{
filter = acceptsAllProjects;
Expand Down

0 comments on commit cb815fe

Please sign in to comment.