Skip to content

Commit

Permalink
Merge pull request #435 from rbalent/FORGE-1450
Browse files Browse the repository at this point in the history
FORGE-1450 - Forge will correctly create enum attribute on entity.
  • Loading branch information
gastaldi committed Apr 14, 2014
2 parents 6257d51 + 8300bab commit 54194c1
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import javax.inject.Inject;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.EnumType;
import javax.persistence.Lob;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
Expand All @@ -35,6 +37,7 @@
import org.jboss.forge.addon.parser.java.resources.JavaResourceVisitor;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.resource.ResourceException;
import org.jboss.forge.addon.resource.visit.VisitContext;
import org.jboss.forge.addon.ui.command.PrerequisiteCommandsProvider;
import org.jboss.forge.addon.ui.context.UIBuilder;
Expand Down Expand Up @@ -98,6 +101,10 @@ public class NewFieldWizard extends AbstractJavaEECommand implements UIWizard, P
@WithAttributes(label = "Column Name", description = "The column name. Defaults to the field name if not informed")
private UIInput<String> columnName;

@Inject
@WithAttributes(label = "Enum Type", defaultValue = "ORDINAL", description = "Defines mapping for enumerated type. Will be ignored if the type of field is other than enum.", type = InputType.RADIO)
private UISelectOne<EnumType> enumType;

@Inject
private JPAFieldOperations fieldOperations;

Expand Down Expand Up @@ -214,9 +221,33 @@ public Boolean call() throws Exception
&& (Date.class.getName().equals(typeValue) || Calendar.class.getName().equals(typeValue));
}
});

enumType.setEnabled(new Callable<Boolean>() {

@Override
public Boolean call() throws Exception
{
JavaClassSource targetEntityType = null;
if (targetEntity.getValue() != null)
{
try
{
targetEntityType = targetEntity.getValue().getJavaType();
}
catch (FileNotFoundException | ResourceException ignored)
{
}
}

return !lob.getValue() && !transientField.getValue()
&& fieldOperations.isFieldTypeEnum(project, targetEntityType, type.getValue());
}
});

builder.add(targetEntity).add(named).add(type).add(temporalType).add(columnName).add(length)
.add(relationshipType)
.add(lob).add(transientField);
.add(lob).add(transientField)
.add(enumType);
}

private void setupEntities(UIContext context)
Expand All @@ -238,7 +269,7 @@ private void setupEntities(UIContext context)

/**
* @param project
* @param entities
* @return entities
*/
private List<JavaResource> getProjectEntities(Project project)
{
Expand All @@ -254,7 +285,8 @@ public void visit(VisitContext context, JavaResource resource)
try
{
JavaSource<?> javaSource = resource.getJavaType();
if (javaSource.hasAnnotation(Entity.class) || javaSource.hasAnnotation(MappedSuperclass.class))
if (javaSource.hasAnnotation(Entity.class) || javaSource.hasAnnotation(MappedSuperclass.class)
|| javaSource.isEnum())
{
entities.add(resource);
}
Expand Down Expand Up @@ -285,6 +317,7 @@ public String convert(RelationshipType source)
@Override
public Result execute(UIExecutionContext context) throws Exception
{
Project project = getSelectedProject(context);
JavaResource javaResource = targetEntity.getValue();
String fieldNameStr = named.getValue();
JavaClassSource targetEntity = javaResource.getJavaType();
Expand Down Expand Up @@ -319,6 +352,22 @@ else if (value == RelationshipType.BASIC)
field = fieldOperations.addFieldTo(targetEntity, fieldType, fieldNameStr, Lob.class.getName());
field.addAnnotation(Column.class).setLiteralValue("length", String.valueOf(Integer.MAX_VALUE));
}
else if (fieldOperations.isFieldTypeEnum(project, targetEntity, type.getValue()))
{
String fieldType = type.getValue();

field = fieldOperations.addFieldTo(targetEntity, fieldType, fieldNameStr,
Enumerated.class.getCanonicalName());

if (enumType.isEnabled() && enumType.getValue() != EnumType.ORDINAL) {
field.getAnnotation(Enumerated.class).setEnumArrayValue(enumType.getValue());
}

if (columnName.isEnabled() && columnName.hasValue())
{
field.addAnnotation(Column.class).setStringValue("name", columnName.getValue());
}
}
else
{
String fieldType = type.getValue();
Expand All @@ -331,7 +380,7 @@ else if (value == RelationshipType.BASIC)
}
if (columnName.isEnabled() && columnName.hasValue())
{
field.getAnnotation(Column.class).setLiteralValue("name", columnName.getValue());
field.getAnnotation(Column.class).setStringValue("name", columnName.getValue());
}
if (temporalType.isEnabled())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@
import org.jboss.forge.addon.javaee.jpa.PersistenceOperations;
import org.jboss.forge.addon.javaee.servlet.ServletFacet_3_1;
import org.jboss.forge.addon.javaee.validation.ValidationFacet;
import org.jboss.forge.addon.parser.java.facets.JavaSourceFacet;
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;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.projects.ProjectFactory;
import org.jboss.forge.addon.projects.facets.MetadataFacet;
import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.source.JavaEnumSource;

/**
* Helps with the configuration of a project
*
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public class ProjectHelper
Expand Down Expand Up @@ -128,4 +131,13 @@ public JavaResource createJPAEntity(Project project, String entityName) throws I
String packageName = project.getFacet(MetadataFacet.class).getTopLevelPackage() + ".model";
return persistenceOperations.newEntity(project, entityName, packageName, GenerationType.AUTO);
}

public JavaResource createEmptyEnum(Project project, String enumName) throws IOException
{
JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class);
JavaEnumSource enumSource = Roaster.create(JavaEnumSource.class).setName(enumName);
String packageName = project.getFacet(MetadataFacet.class).getTopLevelPackage() + ".model";
enumSource.setPackage(packageName);
return javaSourceFacet.saveJavaSource(enumSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.junit.runner.RunWith;

/**
*
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
@RunWith(Arquillian.class)
Expand Down Expand Up @@ -134,4 +134,17 @@ public void testJPAEntityCreation() throws Exception
Assert.assertNotNull(build);
Assert.assertTrue("Build artifact does not exist", build.exists());
}

@Test
public void testEnumCreation() throws Exception
{
Project project = projectHelper.createWebProject();
projectHelper.installJPA_2_0(project);
JavaResource enumEntity = projectHelper.createEmptyEnum(project, "CustomerType");
Assert.assertTrue(enumEntity.exists());
Assert.assertTrue(enumEntity.getJavaType().isEnum());
Resource<?> build = project.getFacet(PackagingFacet.class).createBuilder().runTests(false).build();
Assert.assertNotNull(build);
Assert.assertTrue("Build artifact does not exist", build.exists());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import javax.inject.Inject;
import javax.persistence.Column;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
Expand Down Expand Up @@ -232,4 +234,103 @@ public void testNewOneToManyEagerFetchField() throws Exception
Assert.assertEquals("Set", field.getType().getName());
}

@Test
public void testNewEnumField() throws Exception
{
JavaResource entity = projectHelper.createJPAEntity(project, "Customer");
JavaResource enumEntity = projectHelper.createEmptyEnum(project, "CustomerType");

try (WizardCommandController controller = uiTestHarness.createWizardController(NewFieldWizard.class,
project.getRoot()))
{
controller.initialize();
Assert.assertTrue(controller.isEnabled());
controller.setValueFor("targetEntity", entity);
Assert.assertFalse(controller.canExecute());
controller.setValueFor("named", "customerType");
controller.setValueFor("type", enumEntity.getJavaType().getCanonicalName());
Assert.assertFalse(controller.canMoveToNextStep());
Assert.assertTrue(controller.canExecute());
Result result = controller.execute();
Assert.assertFalse(result instanceof Failed);
Assert.assertEquals("Field customerType created", result.getMessage());
}

JavaClass<?> javaClass = entity.getJavaType();
Assert.assertTrue(javaClass.hasField("customerType"));
final Field<?> field = javaClass.getField("customerType");
Assert.assertEquals("CustomerType", field.getType().getName());
Assert.assertFalse(field.hasAnnotation(Column.class));
Assert.assertTrue(field.hasAnnotation(Enumerated.class));
Assert.assertTrue(field.getAnnotation(Enumerated.class).getValues().isEmpty());
}

@Test
public void testNewEnumFieldWithColumnName() throws Exception
{
JavaResource entity = projectHelper.createJPAEntity(project, "Customer");
JavaResource enumEntity = projectHelper.createEmptyEnum(project, "CustomerType");

try (WizardCommandController controller = uiTestHarness.createWizardController(NewFieldWizard.class,
project.getRoot()))
{
controller.initialize();
Assert.assertTrue(controller.isEnabled());
controller.setValueFor("targetEntity", entity);
Assert.assertFalse(controller.canExecute());
controller.setValueFor("named", "customerType");
controller.setValueFor("type", enumEntity.getJavaType().getCanonicalName());
controller.setValueFor("columnName", "CUSTOMER_TYPE_COLUMN");
Assert.assertFalse(controller.canMoveToNextStep());
Assert.assertTrue(controller.canExecute());
Result result = controller.execute();
Assert.assertFalse(result instanceof Failed);
Assert.assertEquals("Field customerType created", result.getMessage());
}

JavaClass<?> javaClass = entity.getJavaType();
Assert.assertTrue(javaClass.hasField("customerType"));
final Field<?> field = javaClass.getField("customerType");
Assert.assertEquals("CustomerType", field.getType().getName());
Assert.assertTrue(field.hasAnnotation(Column.class));
Assert.assertEquals("CUSTOMER_TYPE_COLUMN", field.getAnnotation(Column.class).getStringValue("name"));
Assert.assertTrue(field.hasAnnotation(Enumerated.class));
Assert.assertTrue(field.getAnnotation(Enumerated.class).getValues().isEmpty());
}

@Test
public void testNewEnumFieldWithType() throws Exception
{
JavaResource entity = projectHelper.createJPAEntity(project, "Customer");
JavaResource enumEntity = projectHelper.createEmptyEnum(project, "CustomerType");

try (WizardCommandController controller = uiTestHarness.createWizardController(NewFieldWizard.class,
project.getRoot()))
{
controller.initialize();
Assert.assertTrue(controller.isEnabled());
controller.setValueFor("targetEntity", entity);
Assert.assertFalse(controller.canExecute());
controller.setValueFor("named", "customerType");
controller.setValueFor("type", enumEntity.getJavaType().getCanonicalName());
controller.setValueFor("enumType", EnumType.STRING);
Assert.assertFalse(controller.canMoveToNextStep());
Assert.assertTrue(controller.canExecute());
Result result = controller.execute();
Assert.assertFalse(result instanceof Failed);
Assert.assertEquals("Field customerType created", result.getMessage());
}

JavaClass<?> javaClass = entity.getJavaType();
Assert.assertTrue(javaClass.hasField("customerType"));
final Field<?> field = javaClass.getField("customerType");
Assert.assertEquals("CustomerType", field.getType().getName());
Assert.assertFalse(field.hasAnnotation(Column.class));
Assert.assertTrue(field.hasAnnotation(Enumerated.class));
Assert.assertFalse(field.getAnnotation(Enumerated.class).getValues().isEmpty());
Assert.assertEquals(EnumType.STRING, field.getAnnotation(Enumerated.class).getEnumValue(EnumType.class));
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@

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

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

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.ResourceException;
import org.jboss.forge.furnace.util.Assert;
import org.jboss.forge.roaster.model.Field;
import org.jboss.forge.roaster.model.Visibility;
import org.jboss.forge.roaster.model.source.FieldSource;
Expand Down Expand Up @@ -127,4 +133,55 @@ protected boolean canAddFieldToToString(Field<JavaClassSource> field)
{
return !field.isStatic();
}

/**
* @param project Project in which the fieldType will be searched
* @param fieldType Full type of the field with package
* @return true if fieldType was found and is enum
* false otherwise.
* @throws IllegalArgumentException if fieldType or project is null
*/
public boolean isFieldTypeEnum(Project project, String fieldType)
{
return isFieldTypeEnum(project, null, fieldType);
}

/**
* @param project Project in which the fieldType will be searched
* @param fieldType Type of the field
* @param targetEntity Entity which package which will be used if fieldType doesn't have package specified
* @return true if fieldType was found and is enum
* false otherwise.
* @throws IllegalArgumentException if fieldType or project is null
*/
public boolean isFieldTypeEnum(Project project, JavaClassSource targetEntity, String fieldType)
{
boolean isEnum = false;

Assert.notNull(fieldType, "Field type should not be null");
Assert.notNull(project, "Field project should not be null");

try
{
isEnum = project.getFacet(JavaSourceFacet.class).getJavaResource(fieldType).getJavaType().isEnum();
}
catch (FileNotFoundException | ResourceException e1)
{
try
{
if (targetEntity != null)
{

isEnum = project.getFacet(JavaSourceFacet.class)
.getJavaResource(targetEntity.getPackage() + "." + fieldType).getJavaType().isEnum();
}
}
catch (FileNotFoundException | ResourceException e2)
{
// ignore
}
}

return isEnum;
}
}
Loading

0 comments on commit 54194c1

Please sign in to comment.