Multiple way to store configuration data

Multiple configuration approach in Liferay.
1.    System setting : If we would required to hold any data which can be used by multiple modules we can store them by adding configuration to system setting.
Below approach we use to add configuration in system setting:
1.1.    Create configuration bundle 
 

1.2.    Below are the sample codes how we define different configuration attributes and its provider.
MyConfiguration.java

package com.my.configuration.MyConfiguration;

import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.AttributeType;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@ObjectClassDefinition(name = "my-configuration", description = "my-configuration-desc", localization = "content/Language")
public @interface MyConfiguration {

    @AttributeDefinition(required = false, type = AttributeType.BOOLEAN, name = "my.config.field1")
    boolean myConfigField1() default false;

    @AttributeDefinition(required = false, type = AttributeType.STRING, name = "my.config.field2")
    String[] myConfigField2() default "option1|option2|option3";

    @AttributeDefinition(required = false, type = AttributeType.STRING, name = "my.config.field3")
    String myConfigField3() default "defaultValue";
}

MyConfigurationProvider.java

public interface MyConfigurationProvider {

    boolean myConfigField1Provider();
    
    String[] myConfigField2Provider();

    String myConfigField3Provider();
}

MyConfigurationProviderImpl.java

 

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.metatype.annotations.Designate;

import com.my.configuration.MyConfiguration;

@Designate(ocd = MyConfiguration.class)
@Component(configurationPid = {
        "com.my.configuration.MyConfiguration" }, immediate = true, service = MyConfigurationProvider.class)
public class MyConfigurationProviderImpl implements MyConfigurationProvider {

    private MyConfiguration configuration;

    @Activate
    @Modified
    protected void readConfig(MyConfiguration config) {
        this.configuration = config;
    }

    @Override
    public boolean ismyConfigField1() {
        return configuration.myConfigField1Provider();
    }

    @Override
    public String[] getMyConfigField2() {
        return configuration.myConfigField2Provider();
    }
    
    @Override
    public boolean getMyConfigField3() {
        return configuration.myConfigField3Provider();
    }
}

Ref: https://help.liferay.com/hc/en-us/articles/360018174611-System-Settings

2.    Theme Setting : If we would require to hold some data related which would require in theme level, we can use system setting of liferay. Below are the steps to create theme setting.
In theme under liferay-look-and-feel.xml file we can define the theme setting attribute:

<theme id="mytheme" name="mytheme">
        <template-extension>ftl</template-extension>
        <settings>
            <setting configurable="true" key="your-key" value="default value here" />
            <setting configurable="true" key="footer content id" value="" />
</settings>


These theme setting will be available in theme selection for the page. We can read its value in theme by below code:

$theme.getSetting("your-key")


Ref: https://help.liferay.com/hc/en-us/articles/360018180951-Making-Themes-Configurable-with-Settings

3.    Portlet preferences : If we require to hold some data specific to portlet only we can define theme in portlet preferences through the portlet configuration. We would require below configuration action class and configuration jsp.
3.1.    ConfigurationAction class

@Component(immediate = true, property = { "javax.portlet.name="
        + MyPortletConstant.MY_PORTLET_KEY }, service = ConfigurationAction.class)
public class MyPortletConfigurationAction extends DefaultConfigurationAction {

    @Override
    public String getJspPath(HttpServletRequest request) {
        return MyPortletConstant.EVENT_LISTING_CONFIG_JSP_PATH;
    }


    @Override
    public void processAction(PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse)
            throws Exception {


        String myKeyData = ParamUtil.getString(actionRequest, MyPortletConstant.MY_KEY);

        setPreference(actionRequest, MyPortletConstant.MY_KEY, myKeyData);

        super.processAction(portletConfig, actionRequest, actionResponse);
    }

}

3.2 Configuration jsp

<%@ include file="init.jsp" %>
<liferay-theme:defineObjects />
<portlet:defineObjects />
<%
    String    myKeyData = portletPreferences.getValue(MyPortletConstant.MY_KEY, StringPool.BLANK);
%>

<liferay-portlet:actionURL
    portletConfiguration="<%= true %>"
    var="configurationActionURL"
/>

<liferay-portlet:renderURL
    portletConfiguration="<%= true %>"
    var="configurationRenderURL"
/>

<liferay-frontend:edit-form action="<%=configurationActionURL%>"
    method="post" name="fm">

    <liferay-frontend:edit-form-body>
        <liferay-frontend:fieldset-group>

            <aui:input
            name="<%= Constants.CMD %>"
            type="hidden"
            value="<%= Constants.UPDATE %>"
        />
   
        <aui:input
            name="redirect"
            type="hidden"
            value="<%= configurationRenderURL %>"
        />
   
        <aui:fieldset>
            <aui:input name="<%= MyPortletConstant.MY_KEY%>" label="my-key"    type="text" value="<%=myKeyData %>" helpMessage="my-key-help"/>
        </aui:fieldset>

        </liferay-frontend:fieldset-group>
    </liferay-frontend:edit-form-body>

    <liferay-frontend:edit-form-footer>
        <aui:button name="saveButton" type="submit" />

        <aui:button type="cancel" />
    </liferay-frontend:edit-form-footer>
</liferay-frontend:edit-form>

4. Using portal-ext.properties : We can use portal-ext file to add property and receive value in different modules or hooks. We would require to restart the server to changes it value.

setting value in portal-ext.properties

liferay.hosting.cloud.aws=url

To read the value we can use below code:

String awsurl = PropsUtil.get(liferay.hosting.cloud.aws);

5. Custom field / Expando : If we want to store entity specific (web content, page, blogs etc.) configuration data or extra data for such entity we can use custom field or expando for those entity. We can define default value also for them.

Ref: https://help.liferay.com/hc/en-us/articles/360017877452-Custom-Fields