ahulme

Bb DevCon 2017 Starter B2 Hands on Session Part 2 B2 Configs

Blog Post created by ahulme on Mar 17, 2017

In part one we build a basic course tool.  In this part we will use the config utility to set dome value for our B2.  These can be used for things like Global user/pass.

 

The B2 stub uses ATD's Configuration Utilities Library to provide configuration services to the building block. This building block handles the saving, loading and caching of a configuration bean.

 

 

The Configuration.java contain the list of Getters and Setters for this page. More can be added of modified.

 

package com.devday.config;

import net.sourceforge.stripes.validation.Validate;

/**
 * Created by wiley on 19/11/14.
 */
public class Configuration {

    private String settingOne;
    private int settingTwo;

    public String getSettingOne() {
        return settingOne;
    }

    public void setSettingOne(String settingOne) {
        this.settingOne = settingOne;
    }

    public int getSettingTwo() {
        return settingTwo;
    }

    public void setSettingTwo(int settingTwo) {
        this.settingTwo = settingTwo;
    }
}

 

 

Stripes config.action

 

@LoginRequired
@EntitlementRestrictions(entitlements = "bbs.yourb2name.admin.MODIFY", errorPage = "/error.jsp")
@UrlBinding("/config")
public class ConfigAction implements ActionBean {

    private BlackboardActionBeanContext context;

    @ValidateNestedProperties({@Validate(field = "settingOne", required = true),
            @Validate(field = "settingTwo", required = true, minvalue = 0, maxvalue = 100)})
    private Configuration config;

    @SpringBean
    private ConfigurationService<Configuration> configService;

    @Before(stages = LifecycleStage.BindingAndValidation)
    public void loadConfiguration() {
        config = configService.loadConfiguration();
    }


    @DefaultHandler
    @DontValidate
    public Resolution displayConfigPage() {
        return new ForwardResolution("/WEB-INF/jsp/config.jsp");
    }

    public Resolution saveConfiguration() {
        configService.persistConfiguration(config);
        return new RedirectResolution(PlugInUtil.getPlugInManagerURL(), false);
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = (BlackboardActionBeanContext)context;
    }

    public Configuration getConfig() {
        return config;
    }

    public void setConfig(Configuration config) {
        this.config = config;
    }
}

 

 

Note the Form Validation, if you make a change to the methods for example making the int a String you will need to update this validation to match the type.

 

@ValidateNestedProperties({@Validate(field = "settingOne", required = true),     @Validate(field = "settingTwo", required = true, minvalue = 0, maxvalue = 100)})

 

Default Values:

 

If you want to pre-populate these configs with default values then edit the defultconfig.xml under src/main/resources

 

<com.devday.config.Configuration>
  <settingOne>A default string value</settingOne>
  <settingTwo>42</settingTwo>
</com.devday.config.Configuration>

 

Config JSP.

 

This uses a simple Form to collect the data and pass via the Stripes Action

 

 

 

<stripes:form beanclass="com.devday.stripes.ConfigAction">

        <stripes:param name="saveConfiguration"/>

 

        <bbNG:dataCollection>

            <bbNG:step title="${toolSettingsStepTitle}" instructions="${toolSettingsStepInstructions}">

                <bbNG:dataElement isRequired="true" label="${settingOneLabel}">

                    <stripes:text name="config.settingOne"></stripes:text>

                    <stripes:errors field="config.settingOne"></stripes:errors>

                </bbNG:dataElement>

                <bbNG:dataElement isRequired="true" label="${settingTwoLabel}">

                    <stripes:text name="config.settingTwo"></stripes:text>

                    <stripes:errors field="config.settingTwo"></stripes:errors>

                </bbNG:dataElement>

            </bbNG:step>

            <bbNG:stepSubmit></bbNG:stepSubmit>

        </bbNG:dataCollection>

    </stripes:form>

Outcomes