import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.broadleafcommerce.metadata.dsl.core.OperationType;
import com.broadleafcommerce.metadata.dsl.core.extension.fields.DateField;
import com.broadleafcommerce.metadata.dsl.core.utils.Actions;
import com.broadleafcommerce.metadata.dsl.core.utils.Conditionals;
import com.broadleafcommerce.metadata.dsl.core.utils.Endpoints;
import com.broadleafcommerce.metadata.dsl.core.utils.Fields;
import com.broadleafcommerce.metadata.dsl.core.utils.ValidationMethods;
import com.broadleafcommerce.metadata.dsl.core.utils.ValidationSchemas;
import com.broadleafcommerce.metadata.dsl.registry.ComponentSource;
import com.broadleafcommerce.order.metadata.autoconfigure.OrderServicesMetadataAutoConfiguration;
import com.broadleafcommerce.order.metadata.dsl.extension.views.FulfillmentView;
import com.broadleafcommerce.order.metadata.dsl.extension.views.OrderView;
import com.broadleafcommerce.order.metadata.support.OrderFulfillmentStatuses;
import com.broadleafcommerce.order.metadata.support.OrderIds;
import com.broadleafcommerce.order.metadata.support.OrderPaths;
import com.broadleafcommerce.order.metadata.support.OrderProps;
import com.broadleafcommerce.order.metadata.support.OrderScopes;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.experimental.UtilityClass;
@Configuration
@RequiredArgsConstructor
@AutoConfigureAfter(OrderServicesMetadataAutoConfiguration.class)
public class OrderMetadataAutoConfiguration {
@UtilityClass
static class ShipmentProps {
public final String SHIPMENT = "shipment";
public final String TRACKING_NUMBER = SHIPMENT + ".trackingNumber";
public final String TRACKING_URL = SHIPMENT + ".trackingUrl";
public final String SHIP_DATE = SHIPMENT + ".shipDate";
public final String SHIPPER_TYPE = SHIPMENT + ".shipperType";
}
@Bean
public ComponentSource addCustomFulfillmentAction() {
return registry -> {
FulfillmentView<?> fulfillmentView =
(FulfillmentView<?>) registry.get(OrderIds.FULFILLMENT_DETAILS);
fulfillmentView
.addAction("MODAL_FORM", Actions.action()
.label("order.actions.add-shipment.label")
.operationType(OperationType.UPDATE)
.placement("PRIMARY")
.type("MODAL_FORM")
// since actions don't normally have conditionals, they need to be
// as an attribute if needed added
.attribute("conditionals", List.of(
Conditionals.when(OrderProps.OrderFulfillment.STATUS)
.equalTo(OrderFulfillmentStatuses.FULFILLED)))
.addEndpoint("UPDATE", Endpoints.put()
.uri(OrderPaths.ORDER_FULFILLMENTS + "/${fulfillment.id}")
.scope(OrderScopes.ORDER_FULFILLMENT))
.addComponent(ShipmentProps.TRACKING_NUMBER, Fields.string()
.name(ShipmentProps.TRACKING_NUMBER)
.label("order.fields.shipment.tracking-number.label")
.order(1))
.addComponent(ShipmentProps.TRACKING_URL, Fields.string()
.name(ShipmentProps.TRACKING_URL)
.label("order.fields.shipment.tracking-url.label")
.order(2)
.validationSchema(ValidationSchemas.string()
.method(ValidationMethods.urlAbsoluteOrRelative(
"order.fields.shipment.tracking-url.valid"))))
.addComponent(ShipmentProps.SHIP_DATE, Fields.date()
.name(ShipmentProps.SHIP_DATE)
.label("order.fields.shipment.ship-date.label")
.defaultStartOf(DateField.DateStartUnit.DAY)
.order(3))
.addComponent(ShipmentProps.SHIPPER_TYPE, Fields.string()
.name(ShipmentProps.SHIPPER_TYPE)
.label("order.fields.shipment.shipper-type.label")
.order(4)));
};
}
@Bean
public ComponentSource addCustomItemOrderAction() {
return registry -> {
OrderView<?> orderView =
(OrderView<?>) registry.get(OrderIds.DETAILS);
orderView
.getItemsSection()
.addAction("MODAL_FORM", Actions.action()
.label("order.actions.add-shipment.label")
.operationType(OperationType.UPDATE)
.placement("CARD")
.type("MODAL_FORM")
// since actions don't normally have conditionals, they need to be added
// as an attribute if needed
.attribute("conditionals", List.of(
Conditionals.when(OrderProps.OrderFulfillment.STATUS)
.equalTo(OrderFulfillmentStatuses.FULFILLED)))
.addEndpoint("UPDATE", Endpoints.put()
.uri(OrderPaths.ORDER_FULFILLMENTS + "/${fulfillment.id}")
.scope(OrderScopes.ORDER_FULFILLMENT))
.addComponent(ShipmentProps.TRACKING_NUMBER, Fields.string()
.name(ShipmentProps.TRACKING_NUMBER)
.label("order.fields.shipment.tracking-number.label")
.order(1))
.addComponent(ShipmentProps.TRACKING_URL, Fields.string()
.name(ShipmentProps.TRACKING_URL)
.label("order.fields.shipment.tracking-url.label")
.order(2)
.validationSchema(ValidationSchemas.string()
.method(ValidationMethods.urlAbsoluteOrRelative(
"order.fields.shipment.tracking-url.valid"))))
.addComponent(ShipmentProps.SHIP_DATE, Fields.date()
.name(ShipmentProps.SHIP_DATE)
.label("order.fields.shipment.ship-date.label")
.defaultStartOf(DateField.DateStartUnit.DAY)
.order(3))
.addComponent(ShipmentProps.SHIPPER_TYPE, Fields.string()
.name(ShipmentProps.SHIPPER_TYPE)
.label("order.fields.shipment.shipper-type.label")
.order(4)));
};
}
}