Skip to content

Commit

Permalink
Now supporting primitive types and Lob
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jun 12, 2013
1 parent 881f1b8 commit 073fde5
Showing 1 changed file with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import javax.inject.Inject;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;

import org.jboss.forge.addon.convert.Converter;
import org.jboss.forge.addon.javaee.jpa.PersistenceOperations;
Expand All @@ -24,6 +26,7 @@
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.UIValidationContext;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.input.UISelectOne;
Expand All @@ -36,6 +39,7 @@
import org.jboss.forge.addon.ui.wizard.UIWizard;
import org.jboss.forge.parser.java.Field;
import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.parser.java.util.Types;

public class NewFieldCommand extends AbstractProjectUICommand implements UIWizard
{
Expand All @@ -55,6 +59,14 @@ public class NewFieldCommand extends AbstractProjectUICommand implements UIWizar
@WithAttributes(label = "Relationship", description = "The type of the relationship", type = InputType.SELECT_ONE_RADIO)
private UISelectOne<RelationshipType> relationshipType;

@Inject
@WithAttributes(label = "Use Primitive Version?", description = "For this field type, use the primitive version")
private UIInput<Boolean> primitive;

@Inject
@WithAttributes(label = "Is LOB?", description = "If the relationship is a LOB, in this case, it will ignore the value in the Type field")
private UIInput<Boolean> lob;

@Inject
private PersistenceOperations persistenceOperations;

Expand All @@ -72,7 +84,25 @@ public void initializeUI(UIBuilder builder) throws Exception
Project project = getSelectedProject(builder.getUIContext());
setupEntities(project);
setupRelationshipType();
builder.add(entity).add(fieldName).add(typeName).add(relationshipType);
lob.setDefaultValue(Boolean.FALSE);
primitive.setDefaultValue(Boolean.FALSE);
lob.setEnabled(new Callable<Boolean>()
{
@Override
public Boolean call() throws Exception
{
return !primitive.getValue();
}
});
typeName.setEnabled(new Callable<Boolean>()
{
@Override
public Boolean call() throws Exception
{
return !lob.getValue();
}
});
builder.add(entity).add(fieldName).add(typeName).add(relationshipType).add(lob).add(primitive);
}

private void setupEntities(Project project)
Expand Down Expand Up @@ -101,6 +131,8 @@ public void visit(JavaResource resource)
});
}
entity.setValueChoices(entities);
if (!entities.isEmpty())
entity.setDefaultValue(entities.get(0));
}

private void setupRelationshipType()
Expand All @@ -120,12 +152,28 @@ public String convert(RelationshipType source)
public Result execute(UIContext context) throws Exception
{
JavaResource javaResource = entity.getValue();
String fieldType = typeName.getValue();
String fieldNameStr = fieldName.getValue();
// TODO: Improve this
String annotation = Column.class.getCanonicalName();
Field<JavaClass> field;
JavaClass targetEntity = (JavaClass) javaResource.getJavaSource();
Field<JavaClass> field = persistenceOperations.addFieldTo(targetEntity, fieldType, fieldNameStr, annotation);
if (primitive.getValue())
{
String fieldType = getPrimitiveTypeFor(typeName.getValue());
field = persistenceOperations.addFieldTo(targetEntity, fieldType, fieldNameStr,
Column.class.getCanonicalName());
}
else if (lob.getValue())
{
String fieldType = byte[].class.getName();
field = persistenceOperations.addFieldTo(targetEntity, fieldType, fieldNameStr, Lob.class.getName());
// TODO: Specify column length somewhere ?
field.addAnnotation(Column.class).setLiteralValue("length", String.valueOf(Integer.MAX_VALUE));
}
else
{
String fieldType = typeName.getValue();
field = persistenceOperations.addFieldTo(targetEntity, fieldType, fieldNameStr,
Column.class.getCanonicalName());
}
Project selectedProject = getSelectedProject(context);
if (selectedProject != null)
{
Expand All @@ -136,6 +184,36 @@ public Result execute(UIContext context) throws Exception
return Results.success("Field " + fieldName.getValue() + " created");
}

@Override
public void validate(UIValidationContext validator)
{
super.validate(validator);
if (primitive.getValue())
{
String primitiveType = getPrimitiveTypeFor(typeName.getValue());
if (primitiveType == null)
{
validator.addValidationError(typeName, "Type is not a wrapper of a primitive type");
}
}
}

private String getPrimitiveTypeFor(String value)
{
if (value == null)
return null;
String val = value.toLowerCase().replaceAll("java.lang.", "");
if (val.equals("integer"))
{
val = "int";
}
else if (val.equals("character"))
{
val = "char";
}
return (Types.isPrimitive(val)) ? val : null;
}

@Override
public NavigationResult next(UIContext context) throws Exception
{
Expand Down

0 comments on commit 073fde5

Please sign in to comment.