javaspringspring-mvcbroadleaf-commerce

How to override the @AdminPresentation for existing attributes [Broadleaf Commerce]


I am trying to override the @AdminPresentation of the following attribute defined in ProductImpl:

@Column(name = "DISPLAY_TEMPLATE")
@AdminPresentation(friendlyName = "ProductImpl_Product_Display_Template",
        group = GroupName.Advanced)
protected String displayTemplate;

Currently, it is displayed as a text field by default as there is no fieldType attribute provided. But I want to display a dropdown select menu with predefined values such as Product and Plan. Here is what I've tried so far:

I've created a class DisplayTemplateType that implements BroadleafEnumerationType and defined PLAN and PRODUCT enums. Here is the code of that class:

public class DisplayTemplateType implements Serializable, BroadleafEnumerationType {

    private static final long serialVersionUID = 7761108654549553693L;

    private static final Map<String, DisplayTemplateType> TYPES = new LinkedHashMap<String, DisplayTemplateType>();

    public static final DisplayTemplateType PLAN = new DisplayTemplateType("PLAN", "PLAN");
    public static final DisplayTemplateType PRODUCT = new DisplayTemplateType("PRODUCT", "PRODUCT");

    public static DisplayTemplateType getInstance(final String type) {
        return TYPES.get(type);
    }

    private String type;
    private String friendlyType;

    public DisplayTemplateType() {
        //do nothing
    }

    public DisplayTemplateType(final String type, final String friendlyType) {
        this.friendlyType = friendlyType;
        setType(type);
    }

    @Override
    public String getType() {
        return type;
    }

    @Override
    public String getFriendlyType() {
        return friendlyType;
    }

    private void setType(final String type) {
        this.type = type;
        if (!TYPES.containsKey(type)) {
            TYPES.put(type, this);
        } else {
            throw new RuntimeException("Cannot add the type: (" + type + "). It already exists as a type via " + getInstance(type).getClass().getName());
        }
    }
    // equals() and hashCode() implementation is removed for readability
}

Then in applicationContext-admin.xml file, I have added the following override properties:

<mo:override id="blMetadataOverrides">
        <mo:overrideItem ceilingEntity="org.broadleafcommerce.core.catalog.domain.Product">
        <mo:field name="displayTemplate">
                <mo:property name="explicitFieldType" value="BROADLEAF_ENUMERATION"/>
                <mo:property name="broadleafEnumeration" value="com.community.core.domain.DisplayTemplateType"/> 
            </mo:field>
        </mo:overrideItem>
</mo:override>

But it didn't change anything. Am I missing something here?


Solution

  • Finally, after trying many things, I came up with a workaround. Instead of going with the XML based approach, I had to extend the ProductImpl class to override @AdminPresentation of its attributes. But for extending I needed to define an @Entity and as a result, I needed to create a useless table to bind to that entity. I know this is not the perfect approach but I couldn't find any better solution for this. Here is my code, so that someone might get help from it in the future:

    @Entity
    @Immutable
    @AdminPresentationMergeOverrides({
            @AdminPresentationMergeOverride(name = "displayTemplate", mergeEntries = {
                    @AdminPresentationMergeEntry(propertyType = PropertyType.AdminPresentation.FIELDTYPE, overrideValue = "BROADLEAF_ENUMERATION"),
                    @AdminPresentationMergeEntry(propertyType = PropertyType.AdminPresentation.BROADLEAFENUMERATION, overrideValue = "com.community.core.domain.DisplayTemplateType"),
                    @AdminPresentationMergeEntry(propertyType = PropertyType.AdminPresentation.REQUIREDOVERRIDE, overrideValue = "REQUIRED"),
                    @AdminPresentationMergeEntry(propertyType = PropertyType.AdminPresentation.DEFAULTVALUE, overrideValue = "PLAN")
            })
    })
    public class CustomProduct extends ProductImpl {
    
        private static final long serialVersionUID = -5745207984235258075L;
    }
    

    This is how it is displayed now:

    This is how it is displayed now