-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented FORGE-1235 - Commands to create new JSF Validator and Con…
…verter objects.
- Loading branch information
1 parent
eec0145
commit e5088b9
Showing
6 changed files
with
582 additions
and
0 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
javaee/impl/src/main/java/org/jboss/forge/addon/javaee/faces/FacesOperations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* 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; | ||
|
||
import java.io.FileNotFoundException; | ||
|
||
import javax.inject.Inject; | ||
|
||
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; | ||
import org.jboss.forge.addon.resource.DirectoryResource; | ||
import org.jboss.forge.parser.java.JavaClass; | ||
|
||
/** | ||
* This class contains Faces specific operations | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
* | ||
*/ | ||
public class FacesOperations | ||
{ | ||
@Inject | ||
private JavaSourceFactory javaSourceFactory; | ||
|
||
/** | ||
* Creates a new {@link JavaResource} in the specified project. If no project is available, use | ||
* {@link FacesOperations#newConverter(DirectoryResource, String, String)} | ||
* | ||
* @param project the current project to create the converter. Must not be null | ||
* @param converterName the name of the converter | ||
* @param converterPackage the package of the converter to be created | ||
* @return the created {@link JavaResource} | ||
*/ | ||
public JavaResource newConverter(Project project, String converterName, String converterPackage) | ||
throws FileNotFoundException | ||
{ | ||
final JavaSourceFacet java = project.getFacet(JavaSourceFacet.class); | ||
JavaClass javaClass = createConverter(converterName, converterPackage); | ||
return java.saveJavaSource(javaClass); | ||
} | ||
|
||
/** | ||
* Creates a new {@link JavaResource} in the specified target. If a project is available, use | ||
* {@link FacesOperations#newConverter(Project, String, String)} | ||
* | ||
* @param target the target directory resource to create this class | ||
* @param converterName the name of the converter | ||
* @param converterPackage the package of the converter to be created | ||
* @return the created {@link JavaResource} | ||
*/ | ||
public JavaResource newConverter(DirectoryResource target, String converterName, String converterPackage) | ||
{ | ||
JavaClass javaClass = createConverter(converterName, converterPackage); | ||
JavaResource javaResource = getJavaResource(target, javaClass.getName()); | ||
javaResource.setContents(javaClass); | ||
return javaResource; | ||
} | ||
|
||
private JavaClass createConverter(String converterName, String converterPackage) | ||
{ | ||
JavaClass source = javaSourceFactory.parse(JavaClass.class, getClass().getResourceAsStream("Converter.jv")); | ||
source.setName(converterName); | ||
source.setPackage(converterPackage); | ||
return source; | ||
} | ||
|
||
/** | ||
* Creates a new {@link JavaResource} in the specified project. If no project is available, use | ||
* {@link FacesOperations#newValidator(DirectoryResource, String, String)} | ||
* | ||
* @param project the current project to create the validator. Must not be null | ||
* @param validatorName the name of the validator | ||
* @param validatorPackage the package of the validator to be created | ||
* @return the created {@link JavaResource} | ||
*/ | ||
public JavaResource newValidator(Project project, String validatorName, String validatorPackage) | ||
throws FileNotFoundException | ||
{ | ||
final JavaSourceFacet java = project.getFacet(JavaSourceFacet.class); | ||
JavaClass javaClass = createValidator(validatorName, validatorPackage); | ||
return java.saveJavaSource(javaClass); | ||
} | ||
|
||
/** | ||
* Creates a new {@link JavaResource} in the specified target. If a project is available, use | ||
* {@link FacesOperations#newConverter(Project, String, String)} | ||
* | ||
* @param target the target directory resource to create the validator. Must not be null | ||
* @param validatorName the name of the validator | ||
* @param validatorPackage the package of the validator to be created | ||
* @return the created {@link JavaResource} | ||
*/ | ||
public JavaResource newValidator(DirectoryResource target, String converterName, String converterPackage) | ||
{ | ||
JavaClass javaClass = createValidator(converterName, converterPackage); | ||
JavaResource javaResource = getJavaResource(target, javaClass.getName()); | ||
javaResource.setContents(javaClass); | ||
return javaResource; | ||
} | ||
|
||
private JavaClass createValidator(String converterName, String converterPackage) | ||
{ | ||
JavaClass source = javaSourceFactory.parse(JavaClass.class, getClass().getResourceAsStream("Validator.jv")); | ||
source.setName(converterName); | ||
source.setPackage(converterPackage); | ||
return source; | ||
} | ||
|
||
private JavaResource getJavaResource(final DirectoryResource sourceDir, final String relativePath) | ||
{ | ||
String path = relativePath.trim().endsWith(".java") | ||
? relativePath.substring(0, relativePath.lastIndexOf(".java")) : relativePath; | ||
path = path.replace(".", "/") + ".java"; | ||
JavaResource target = sourceDir.getChildOfType(JavaResource.class, path); | ||
return target; | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
javaee/impl/src/main/java/org/jboss/forge/addon/javaee/faces/ui/NewConverterCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* 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.inject.Inject; | ||
import javax.persistence.Entity; | ||
|
||
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.parser.java.resources.JavaResourceVisitor; | ||
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.context.UIBuilder; | ||
import org.jboss.forge.addon.ui.context.UIContext; | ||
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.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.JavaSource; | ||
|
||
/** | ||
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> | ||
*/ | ||
public class NewConverterCommand extends AbstractJavaEECommand | ||
{ | ||
|
||
@Inject | ||
@WithAttributes(label = "Converter 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 Converter") | ||
.description("Create a new JSF Converter Type") | ||
.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.getSourceFolder()).setEnabled(false); | ||
targetPackage.setValue(calculateConverterPackage(project)); | ||
} | ||
builder.add(targetLocation); | ||
builder.add(targetPackage).add(named); | ||
} | ||
|
||
private String calculateConverterPackage(Project project) | ||
{ | ||
final String[] value = new String[1]; | ||
project.getFacet(JavaSourceFacet.class).visitJavaSources(new JavaResourceVisitor() | ||
{ | ||
@Override | ||
public void visit(JavaResource javaResource) | ||
{ | ||
try | ||
{ | ||
JavaSource<?> javaSource = javaResource.getJavaSource(); | ||
if (javaSource.hasAnnotation(Entity.class)) | ||
{ | ||
value[0] = javaSource.getPackage(); | ||
} | ||
} | ||
catch (FileNotFoundException ignore) | ||
{ | ||
} | ||
} | ||
}); | ||
if (value[0] == null) | ||
{ | ||
value[0] = project.getFacet(MetadataFacet.class).getTopLevelPackage() + ".convert"; | ||
} | ||
return value[0]; | ||
} | ||
|
||
@Override | ||
public Result execute(UIContext context) throws Exception | ||
{ | ||
String entityName = named.getValue(); | ||
String entityPackage = targetPackage.getValue(); | ||
DirectoryResource targetDir = targetLocation.getValue(); | ||
Project project = getSelectedProject(context); | ||
JavaResource javaResource; | ||
if (project == null) | ||
{ | ||
javaResource = facesOperations.newConverter(targetDir, entityName, entityPackage); | ||
} | ||
else | ||
{ | ||
javaResource = facesOperations.newConverter(project, entityName, entityPackage); | ||
} | ||
context.setSelection(javaResource); | ||
return Results.success("Entity " + javaResource + " created"); | ||
} | ||
|
||
@Override | ||
protected boolean isProjectRequired() | ||
{ | ||
return false; | ||
} | ||
} |
Oops, something went wrong.