Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. This Developer Center contains the guides for Commerce 6.13.0 through 7.4.1.Visit new site

This version of Elastic Path Commerce is no longer supported or maintained. To upgrade to the latest version, contact your Elastic Path representative.

Creating a State Policy

Creating a State Policy

Custom State Policies should extend the AbstractStatePolicyImpl class and implement the init and determineState methods.

The State Policy is initialized by calling the init method. This method takes a bean factory and an Object as parameters. The bean factory parameter can be used to set a custom bean factory. The dependentObject parameter is an optional dependent object, usually the model (domain object) containing the data to be displayed in the control. Use the dependentObject parameter to pass any objects you need to determine state.

Note:

If you need to pass multiple objects to the state policy, add the objects to a Collection and pass the Collection in the dependentObject parameter.

The following example shows the init method for a policy that determines the state of UI controls when creating a category in a catalog. In this case, the dependentObject parameter contains a Catalog object.

public void init(final BeanFactory beanFactory, final Object dependentObject) {
    this.catalog = (Catalog) dependentObject;
}

The determineState method contains the logic you want to apply to determine the state of the UI control. The PolicyActionContainer parameter contains the State Policy Governables to which the Policy is being applied and the State Change Targets that will be affected.

The following example shows the determineState method for a policy that determines the state of UI controls when creating a category in a catalog. If the user has the category management permission and the user has been assigned to that catalog, then determineState returns EpState.EDITABLE, which means the UI controls will be editable. Otherwise, the UI controls will be read-only.

public EpState determineState(final PolicyActionContainer targetContainer) {
    if (this.catalog != null
            && AuthorizationService.getInstance().isAuthorized(CatalogPermissions.CATEGORY_MANAGE)
            && AuthorizationService.getInstance().isAuthorized(this.catalog)) {
        return EpState.EDITABLE;
    }
    return EpState.READ_ONLY;
}