use of org.broadleafcommerce.cms.structure.domain.StructuredContentFieldTemplate in project BroadleafCommerce by BroadleafCommerce.
the class StructuredContentServiceImpl method buildFieldValues.
/**
* Parses the given {@link StructuredContent} into its {@link StructuredContentDTO} representation. This will also
* format the values from {@link StructuredContentDTO#getValues()} into their actual data types. For instance, if the
* given {@link StructuredContent} has a DATE field, then this method will ensure that the resulting object in the values
* map of the DTO is a {@link Date} rather than just a String representing a date.
* <p/>
* Current support of parsing field types is:
* DATE - {@link Date}
* BOOLEAN - {@link Boolean}
* DECIMAL - {@link BigDecimal}
* INTEGER - {@link Integer}
* MONEY - {@link Money}
* <p/>
* All other fields are treated as strings. This will also fix URL strings that have the CMS prefix (like images) by
* prepending the standard CMS prefix with the particular environment prefix
*
* @param sc
* @param scDTO
* @param secure
* @see {@link StaticAssetService#getStaticAssetEnvironmentUrlPrefix()}
*/
protected void buildFieldValues(StructuredContent sc, StructuredContentDTO scDTO, boolean secure) {
String cmsPrefix = staticAssetPathService.getStaticAssetUrlPrefix();
Map<String, StructuredContentFieldXref> scFieldXrefs = MapUtils.emptyIfNull(sc.getStructuredContentFieldXrefs());
scDTO.getValues().put("id", sc.getId());
for (String fieldKey : scFieldXrefs.keySet()) {
StructuredContentField scf = scFieldXrefs.get(fieldKey).getStructuredContentField();
String originalValue = scf.getValue();
if (hasCmsPrefix(originalValue, cmsPrefix)) {
buildFieldValueWithCmsPrefix(originalValue, scDTO, secure, fieldKey);
} else {
FieldDefinition definition = null;
StructuredContentType scType = sc.getStructuredContentType();
StructuredContentFieldTemplate scFieldTemplate = scType.getStructuredContentFieldTemplate();
List<FieldGroup> scFieldGroups = ListUtils.emptyIfNull(scFieldTemplate.getFieldGroups());
Iterator<FieldGroup> groupIterator = scFieldGroups.iterator();
while (groupIterator.hasNext() && definition == null) {
FieldGroup group = groupIterator.next();
for (FieldDefinition def : group.getFieldDefinitions()) {
if (def.getName().equals(fieldKey)) {
definition = def;
break;
}
}
}
if (definition != null) {
Object value = null;
if (originalValue != null) {
switch(definition.getFieldType()) {
case DATE:
try {
value = FormatUtil.getDateFormat().parse(originalValue);
} catch (Exception e) {
value = originalValue;
}
break;
case BOOLEAN:
value = new Boolean(originalValue);
break;
case DECIMAL:
value = new BigDecimal(originalValue);
break;
case INTEGER:
value = Integer.parseInt(originalValue);
break;
case MONEY:
value = new Money(originalValue);
break;
case ADDITIONAL_FOREIGN_KEY:
value = FOREIGN_LOOKUP + '|' + definition.getAdditionalForeignKeyClass() + '|' + originalValue;
break;
default:
value = originalValue;
break;
}
}
scDTO.getValues().put(fieldKey, value);
} else {
scDTO.getValues().put(fieldKey, scFieldXrefs.get(fieldKey).getStructuredContentField().getValue());
}
}
}
// allow modules to contribute to the fields of the DTO
extensionManager.getProxy().populateAdditionalStructuredContentFields(sc, scDTO, secure);
}
Aggregations