Skip to content

Commit

Permalink
FORGEPLUGINS-172 Fix generation of widgets for boolean properties
Browse files Browse the repository at this point in the history
Also canonicalized Boolean wrapper type with the boolean primitive.

This commit ensures that search input fields for boolean types are
displayed as a dropdown. But, the same boolean property is
displayed as a checkbox in the create and edit pages.
  • Loading branch information
VineetReynolds committed Nov 26, 2014
1 parent 2586a4e commit 2b52e3d
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 9 deletions.
Expand Up @@ -8,10 +8,7 @@

import static org.jboss.forge.addon.angularjs.AngularJSInspectionResultConstants.JS_IDENTIFIER;
import static org.jboss.forge.addon.scaffold.metawidget.inspector.ForgeInspectionResultConstants.PRIMARY_KEY;
import static org.metawidget.inspector.InspectionResultConstants.DATETIME_TYPE;
import static org.metawidget.inspector.InspectionResultConstants.LABEL;
import static org.metawidget.inspector.InspectionResultConstants.NAME;
import static org.metawidget.inspector.InspectionResultConstants.TYPE;
import static org.metawidget.inspector.InspectionResultConstants.*;

import java.io.FileNotFoundException;
import java.util.ArrayList;
Expand Down Expand Up @@ -87,7 +84,7 @@ public List<Map<String, String>> enhanceResults(JavaClassSource entity, List<Map
{
createJavaScriptIdentifiers(propertyAttributes);
populateLabelStrings(propertyAttributes);
canonicalizeNumberTypes(propertyAttributes);
canonicalizeTypes(propertyAttributes);
canonicalizeTemporalTypes(propertyAttributes);
chooseRelationshipOptionLabels(entity, propertyAttributes);
}
Expand Down Expand Up @@ -159,7 +156,7 @@ private void populateLabelStrings(Map<String, String> propertyAttributes)
}
}

private void canonicalizeNumberTypes(Map<String, String> propertyAttributes)
private void canonicalizeTypes(Map<String, String> propertyAttributes)
{
// Canonicalize all numerical types in Java to "number" for HTML5 form input type support
String propertyType = propertyAttributes.get(TYPE);
Expand All @@ -171,6 +168,11 @@ private void canonicalizeNumberTypes(Map<String, String> propertyAttributes)
{
propertyAttributes.put(TYPE, "number");
}
if (propertyType.equals(boolean.class.getName()) || propertyType.equals(Boolean.class.getName()))
{
propertyAttributes.put(TYPE, "boolean");
propertyAttributes.remove(LOOKUP);
}
}

private void canonicalizeTemporalTypes(Map<String, String> propertyAttributes)
Expand Down Expand Up @@ -303,7 +305,7 @@ private List<Map<String, String>> getDisplayableProperties(List<Map<String, Stri
List<Map<String, String>> displayableProperties = new ArrayList<Map<String, String>>();
for (Map<String, String> propertyAttributes : inspectionResults)
{
canonicalizeNumberTypes(propertyAttributes);
canonicalizeTypes(propertyAttributes);
canonicalizeTemporalTypes(propertyAttributes);
boolean isManyToOneRel = Boolean.parseBoolean(propertyAttributes.get("many-to-one"));
boolean isOneToOneRel = Boolean.parseBoolean(propertyAttributes.get("one-to-one"));
Expand Down
Expand Up @@ -138,6 +138,13 @@ angular.module('${angularApp}').controller('${angularController}', function($sco
});
}
});
<#elseif property.type == "boolean">
<#assign
lookupCollection = "$scope.${property.identifier}List">
${lookupCollection} = [
"true",
"false"
];
<#elseif property["lookup"]??>
<#assign
lookupCollection = "$scope.${property.identifier}List">
Expand Down
Expand Up @@ -69,7 +69,15 @@ angular.module('${angularApp}').controller('${angularController}', function ($sc
});
}
});


<#elseif property.type == "boolean">
<#assign
lookupCollection = "$scope.${property.identifier}List">
${lookupCollection} = [
"true",
"false"
];

<#elseif property["lookup"]??>
<#assign
lookupCollection ="$scope.${property.identifier}List">
Expand Down
Expand Up @@ -37,6 +37,13 @@ angular.module('${angularApp}').controller('${angularController}', function($sco
relatedCollection = "$scope.${property.identifier}List"
relatedResource = "${property.simpleType}Resource">
${relatedCollection} = ${relatedResource}.queryAll();
<#elseif property.type == "boolean">
<#assign
lookupCollection = "$scope.${property.identifier}List">
${lookupCollection} = [
"true",
"false"
];
<#elseif property["lookup"]??>
<#assign
lookupCollection = "$scope.${property.identifier}List">
Expand Down
Expand Up @@ -26,7 +26,7 @@
<#elseif property.type == "boolean"> type="checkbox"<#t/>
<#else> type="text"</#if><#t/>
<#if (property.required!"false") == "true"> required</#if><#t/>
<#if property["maximum-length"]??> ng-maxlength="${property["maximum-length"]}"</#if><#if property["minimum-length"]??> ng-minlength="${property["minimum-length"]}"</#if><#lt/> class="form-control" ng-model="${modelProperty}" placeholder="Enter the ${entityName} ${propertyLabel}"></input>
<#if property["maximum-length"]??> ng-maxlength="${property["maximum-length"]}"</#if><#if property["minimum-length"]??> ng-minlength="${property["minimum-length"]}"</#if><#lt/><#if property.type == "boolean"> class="checkbox"<#else>class="form-control"</#if><#lt/> ng-model="${modelProperty}" placeholder="Enter the ${entityName} ${propertyLabel}"></input>
</#if>
<#if (property.required!) == "true">
<span class="help-block error" ng-show="${formProperty}.$error.required">required</span>
Expand Down
Expand Up @@ -10,6 +10,10 @@
<select id="${property.name}" name="${property.name}" class="form-control" ng-model="search.${property.name}" ng-options="${collectionVar} as ${collectionVar}.${property.optionLabel} for ${collectionVar} in ${collection}">
<option value="">Choose a ${propertyLabel}</option>
</select>
<#elseif property.type == "boolean">
<select id="${property.name}" name="${property.name}" class="form-control" ng-model="search.${property.name}" ng-options="${collectionVar} as ${collectionVar} for ${collectionVar} in ${collection}">
<option value="">Choose a ${propertyLabel}</option>
</select>
<#elseif property["lookup"]??>
<select id="${property.name}" name="${property.name}" class="form-control" ng-model="search.${property.name}" ng-options="${collectionVar} as ${collectionVar} for ${collectionVar} in ${collection}">
<option value="">Choose a ${propertyLabel}</option>
Expand Down
Expand Up @@ -169,6 +169,12 @@ public FieldSource<JavaClassSource> createBooleanField(JavaClassSource entityCla
return fieldOperations.addFieldTo(entityClass, boolean.class.getSimpleName(), fieldName);
}

public FieldSource<JavaClassSource> createBooleanWrapperField(JavaClassSource entityClass, String fieldName)
throws FileNotFoundException
{
return fieldOperations.addFieldTo(entityClass, Boolean.class.getSimpleName(), fieldName);
}

public Field<JavaClassSource> createTemporalField(JavaClassSource entityClass, String fieldName, TemporalType type)
throws FileNotFoundException
{
Expand Down
Expand Up @@ -139,6 +139,24 @@ public void testGenerateBasicNumberProperty() throws Exception {
assertThat(formInputElement.attr("ng-model"), equalTo("search" + "." + "score"));
}

@Test
public void testGenerateBasicBooleanProperty() throws Exception {
Map<String, Object> root = TestHelpers.createInspectionResultWrapper(ENTITY_NAME, BOOLEAN_PROP);

Resource<URL> templateResource = resourceFactory.create(getClass().getResource(Deployments.BASE_PACKAGE_PATH + Deployments.SEARCH_FORM_INPUT));
Template processor = processorFactory.create(templateResource, FreemarkerTemplate.class);
String output = processor.process(root);
Document html = Jsoup.parseBodyFragment(output);
assertThat(output.trim(), not(equalTo("")));

Elements container = html.select("div.form-group");
assertThat(container, notNullValue());

Elements formInputElement = container.select("div.col-sm-10 > select");
assertThat(formInputElement.attr("id"), equalTo("optForMail"));
assertThat(formInputElement.attr("ng-model"), equalTo("search" + "." + "optForMail"));
}

@Test
public void testGenerateBasicDateProperty() throws Exception {
Map<String, Object> root = TestHelpers.createInspectionResultWrapper(ENTITY_NAME, DATE_PROP);
Expand Down
Expand Up @@ -178,6 +178,22 @@ public void testInspectBooleanField() throws Exception
assertThat(inspectionResult, hasItemWithEntry("type", "boolean"));
}

@Test
public void testInspectBooleanWrapperField() throws Exception
{
String entityName = "Customer";
String fieldName = "optForMail";
generateSimpleEntity(entityName);
generateBooleanWrapperField(fieldName);

JavaClassSource klass = getJavaClassFor(entityName);
List<Map<String, String>> inspectionResult = metawidgetInspectorFacade.inspect(klass);
inspectionResult = angularResultEnhancer.enhanceResults(klass, inspectionResult);

assertThat(inspectionResult, hasItemWithEntry("name", fieldName));
assertThat(inspectionResult, hasItemWithEntry("type", "boolean"));
}

@Test
public void testInspectDateField() throws Exception
{
Expand Down Expand Up @@ -546,6 +562,12 @@ private void generateBooleanField(String fieldName) throws Exception
saveJavaSource();
}

private void generateBooleanWrapperField(String fieldName) throws Exception
{
projectHelper.createBooleanWrapperField(entityClass, fieldName);
saveJavaSource();
}

private void generateTemporalField(String fieldName, TemporalType type) throws Exception
{
projectHelper.createTemporalField(entityClass, fieldName, type);
Expand Down

0 comments on commit 2b52e3d

Please sign in to comment.