Custom parameters during initialization and update
- 8th Sep 2017
- 3
- 35307
- Adding custom parameters during initialization and update Adding custom parameters during initialization process Adding custom parameters during update process Hooks for Initialization and Update Process SystemSetupParameter example in hybris Usage of Parameters during initialization and update through HAC
How to add custom parameters in HAC during initialization and update?
Some times it is required to add custom parameters in HAC during initialization and update time so that we can import appropriate impex files based on user selection.
How to add such custom parameters in HAC ?
We should add a method annotated with @SystemSetupParameterMethod inside a SystemSetup annotated class.
This method returns a list of parameters to be added
Let us see an Example
Add below method in the CoreSystemSetup class
- @SystemSetupParameterMethod
- public List<SystemSetupParameter> getCustomSystemSetupParameters()
- {
- final List<SystemSetupParameter> params = new ArrayList<SystemSetupParameter>();
- //Adding boolean field
- final SystemSetupParameter customDataParameter = new
- SystemSetupParameter("createCustomData");
- customDataParameter.setLabel("Do you want to create custom data?");
- customDataParameter.addValue("true");
- customDataParameter.addValue("false", true);
- params.add(customDataParameter);
- //Adding multi select drop down box
- final SystemSetupParameter imports = new SystemSetupParameter("imports");
- imports.setMultiSelect(true);
- imports.setLabel("Custom data to import : ");
- imports.addValues(new String[]
- { "marketingGroups", "specialUsers" });
- params.add(imports);
- return params;
- }
@SystemSetupParameterMethod public List<SystemSetupParameter> getCustomSystemSetupParameters() { final List<SystemSetupParameter> params = new ArrayList<SystemSetupParameter>(); //Adding boolean field final SystemSetupParameter customDataParameter = new SystemSetupParameter("createCustomData"); customDataParameter.setLabel("Do you want to create custom data?"); customDataParameter.addValue("true"); customDataParameter.addValue("false", true); params.add(customDataParameter); //Adding multi select drop down box final SystemSetupParameter imports = new SystemSetupParameter("imports"); imports.setMultiSelect(true); imports.setLabel("Custom data to import : "); imports.addValues(new String[] { "marketingGroups", "specialUsers" }); params.add(imports); return params; }
we have added 2 different types of custom parameters , one is single selection list box and other one is multiple selection list box.
After this, Do Build, start the server and access Initialization or Update option in HAC using below URLs
http://localhost:9001/platform/update
http://localhost:9001/platform/init
we can see that custom parameters are appeared during the initialization process.
Users can select the options from these custom parameters and accordingly we can import the corresponding impex files.
How to access the options selected by user ?
We can access the values selected by user using below code and import the appropriate impex during initialization and update process.
- @SystemSetup(type = Type.PROJECT, process = Process.ALL)
- public void createProjectData(final SystemSetupContext context) throws Exception
- {
- String createCustomData = context.getParameter(CoreConstants.EXTENSIONNAME +
- "_createCustomData"));
- if(createCustomData!=null)
- {
- importImpexFile(context, "/trainingcore/import/cockpits/cmscockpit/"+createCustomData+".impex");
- }
- for (final String selectedParam : context.getParameters(CoreConstants.EXTENSIONNAME +
- "_imports"))
- {
- importImpexFile(context, "/trainingcore/import/cockpits/cmscockpit/"+selectedParam+".impex");
- }
- }
@SystemSetup(type = Type.PROJECT, process = Process.ALL) public void createProjectData(final SystemSetupContext context) throws Exception { String createCustomData = context.getParameter(CoreConstants.EXTENSIONNAME + "_createCustomData")); if(createCustomData!=null) { importImpexFile(context, "/trainingcore/import/cockpits/cmscockpit/"+createCustomData+".impex"); } for (final String selectedParam : context.getParameters(CoreConstants.EXTENSIONNAME + "_imports")) { importImpexFile(context, "/trainingcore/import/cockpits/cmscockpit/"+selectedParam+".impex"); } }
The parameters are stored in the map with the prefix < extension>_ to avoid duplicate parameters
SystemSetupContext.getParameters(key) method returns a String[] with all the selected options
SystemSetupContext.getParameter(key) method returns a String with a single selected option
Points to Remember
@SystemSetup annotation should be used only for public methods
If the annotation is defined at the class level, then those values acts as a default values for all the annotated methods within that class.
Annotation @SystemSetup has an attribute called extension, if it is not defined then annotation code will not be executed at all.
For both process and type attributes of annotation, default value is βALLβ
We can define any number of annotated methods inside the annotated class.
Great work
So the above code without extension will never execute during build process?
During build, its just going to be compiled.
All these annotated methods will be executed during init/update process depending on the configuration.
If the annotation extension attribute is not defined for a method, It will check whether its defined in the class level.
If its not defined there also then the default value is an empty String. It means that the annotated code is not executed.
In the above case, We have placed this code on top of CoreSystemSetup.java class , since this java class is already having
We don’t need to define again in the method level as the class level value will be taken by default and hence it will be executed during mystorecore extension import.