import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.broadleafcommerce.customer.metadata.autoconfigure.CustomerServicesMetadataAutoConfiguration;
import com.broadleafcommerce.customer.metadata.segments.support.CustomerSegmentGroups;
import com.broadleafcommerce.customer.metadata.segments.support.CustomerSegmentIds;
import com.broadleafcommerce.customer.metadata.segments.support.CustomerSegmentProps;
import com.broadleafcommerce.metadata.dsl.core.extension.fields.DynamicField;
import com.broadleafcommerce.metadata.dsl.core.extension.fields.RuleBuilderField;
import com.broadleafcommerce.metadata.dsl.core.extension.views.details.EntityFormView;
import com.broadleafcommerce.metadata.dsl.core.extension.views.details.EntityView;
import com.broadleafcommerce.metadata.dsl.core.utils.Fields;
import com.broadleafcommerce.metadata.dsl.registry.ComponentSource;
import lombok.RequiredArgsConstructor;
import lombok.experimental.UtilityClass;
@Configuration
@RequiredArgsConstructor
@AutoConfigureAfter(CustomerServicesMetadataAutoConfiguration.class)
public class DemoCustomerMetadataAutoConfiguration {
@UtilityClass
public class DemoCustomerProps {
public final String CAMPAIGN_CODE = "attributes[campaign-code]";
}
@Bean
public ComponentSource customizeCustomerMetadata() {
return registry -> {
EntityView<?> create = (EntityView<?>) registry.get(CustomerSegmentIds.CREATE);
addCampaignCodeTargeting(create);
EntityView<?> update = (EntityView<?>) registry.get(CustomerSegmentIds.UPDATE);
addCampaignCodeTargeting(update);
};
}
private void addCampaignCodeTargeting(EntityView<?> view) {
EntityFormView<?> form = view.getGeneralForm();
form.findGroup(CustomerSegmentGroups.MATCH_RULES)
.flatMap(group -> group.findField(CustomerSegmentProps.SEGMENT_MATCH_RULE))
.map(matchRule -> (DynamicField<?>) matchRule)
.ifPresent(matchRule -> matchRule.configureFields(fields -> {
fields.stream()
.filter(field -> field instanceof RuleBuilderField)
.findFirst()
.map(dynamicRules -> (RuleBuilderField<?>) dynamicRules)
.ifPresent(dynamicRules -> {
dynamicRules.addField(DemoCustomerProps.CAMPAIGN_CODE, Fields.string()
.label("Campaign Tracking Code")
.order(6000));
});
return fields;
}));
}
}