ConfigSrv

ConfigSrv is a Spring service that provides typed read and write access to the OpenKM configuration parameter store. Configuration parameters are persisted in the database and identified by a string key. Each parameter has a declared type (string, text, boolean, integer, long, html, password, hidden, list, or file) that controls how it is displayed and stored in the administration UI.

The typed getter methods (getString, getBoolean, getInteger, etc.) auto-create the parameter with the supplied default value if the key does not exist yet in the database. This is the recommended pattern for registering default configuration values for plugins.

Methods

save

Description:

MethodReturn valuesDescription

save(Config cfg)

void

Creates or updates a configuration parameter.

Example:

package com.openkm;

import com.openkm.db.bean.Config;
import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            Config cfg = new Config();
            cfg.setKey("plugin.myPlugin.enabled");
            cfg.setType(Config.BOOLEAN);
            cfg.setValue("true");
            configSrv.save(cfg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

exists

Description:

MethodReturn valuesDescription

exists(String key)

boolean

Returns true if a configuration parameter with the given key exists in the database.

Example:

package com.openkm;

import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            boolean exists = configSrv.exists("plugin.myPlugin.enabled");
            System.out.println("Exists: " + exists);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

delete

Description:

MethodReturn valuesDescription

delete(String key)

void

Deletes the configuration parameter with the given key.

Example:

package com.openkm;

import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            configSrv.delete("plugin.myPlugin.enabled");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

findByPk

Description:

MethodReturn valuesDescription

findByPk(String key)

Config

Returns the full Config entity for the given key, or null if it does not exist.

Example:

package com.openkm;

import com.openkm.db.bean.Config;
import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            Config cfg = configSrv.findByPk("plugin.myPlugin.enabled");
            if (cfg != null) {
                System.out.println("Value: " + cfg.getValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getHidden

Description:

MethodReturn valuesDescription

getHidden(String key, String defaultValue)

String

Returns the configuration parameter value as a String with the hidden type. Hidden parameters are stored like string parameters but are not displayed in the administration UI.

Example:

package com.openkm;

import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            String value = configSrv.getHidden("plugin.myPlugin.internalKey", "defaultValue");
            System.out.println("Config parameter: " + value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getString

Description:

MethodReturn valuesDescription

getString(String key, String defaultValue)

String

Returns the configuration parameter value as a String with the string type. If the key does not exist it is created with the default value.

Example:

package com.openkm;

import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            String value = configSrv.getString("plugin.myPlugin.serverUrl", "http://localhost:8080");
            System.out.println("Config parameter: " + value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getText

Description:

MethodReturn valuesDescription

getText(String key, String defaultValue)

String

Returns the configuration parameter value as a String with the text type. Use for multi-line string values.

Example:

package com.openkm;

import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            String value = configSrv.getText("plugin.myPlugin.template", "default template text");
            System.out.println("Config parameter: " + value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getBoolean

Description:

MethodReturn valuesDescription

getBoolean(String key, boolean defaultValue)

boolean

Returns the configuration parameter value as a boolean. If the key does not exist it is created with the default value.

Example:

package com.openkm;

import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            boolean enabled = configSrv.getBoolean("plugin.myPlugin.enabled", false);
            System.out.println("Config parameter: " + enabled);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getInteger

Description:

MethodReturn valuesDescription

getInteger(String key, int defaultValue)

int

Returns the configuration parameter value as an int. If the key does not exist it is created with the default value.

Example:

package com.openkm;

import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            int maxRetries = configSrv.getInteger("plugin.myPlugin.maxRetries", 3);
            System.out.println("Config parameter: " + maxRetries);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getLong

Description:

MethodReturn valuesDescription

getLong(String key, long defaultValue)

long

Returns the configuration parameter value as a long. If the key does not exist it is created with the default value.

Example:

package com.openkm;

import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            long maxSize = configSrv.getLong("plugin.myPlugin.maxFileSize", 10485760L);
            System.out.println("Config parameter: " + maxSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getHtml

Description:

MethodReturn valuesDescription

getHtml(String key, String defaultValue)

String

Returns the configuration parameter value as a String with the html type. Use for parameters that contain HTML markup.

Example:

package com.openkm;

import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            String html = configSrv.getHtml("plugin.myPlugin.footerHtml", "<p>Default footer</p>");
            System.out.println("Config parameter: " + html);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getPassword

Description:

MethodReturn valuesDescription

getPassword(String key, String defaultValue)

String

Returns the configuration parameter value as a plain-text String with the password type. The value is stored DES-encrypted in the database and automatically decrypted on retrieval.

Example:

package com.openkm;

import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            String password = configSrv.getPassword("plugin.myPlugin.apiSecret", "changeme");
            System.out.println("Config parameter: " + password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getFile

Description:

MethodReturn valuesDescription

getFile(String key, String path)

ConfigStoredFile

Returns the configuration parameter as a ConfigStoredFile object. The path parameter is a classpath resource path used to populate the default value when the key is first created. The file content is stored Base64-encoded in the database.

Example:

package com.openkm;

import com.openkm.bean.ConfigStoredFile;
import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            ConfigStoredFile storedFile = configSrv.getFile("plugin.myPlugin.logo", "img/logo.png");
            System.out.println("File name: " + storedFile.getName());
            System.out.println("MIME type: " + storedFile.getMime());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getList

Description:

MethodReturn valuesDescription

getList(String key, String defaultValue)

List<String>

Returns the configuration parameter as a list of strings. The stored value is a newline-delimited string; this method parses it and returns each non-empty line as a list entry.

Example:

package com.openkm;

import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            for (String item : configSrv.getList("plugin.myPlugin.allowedRoles", "ROLE_ADMIN")) {
                System.out.println(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

findAll

Description:

MethodReturn valuesDescription

findAll()

List<Config>

Returns all configuration parameters stored in the database.

Example:

package com.openkm;

import com.openkm.db.bean.Config;
import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            for (Config cfg : configSrv.findAll()) {
                System.out.println(cfg.getKey() + " = " + cfg.getValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

findAllFiltered

Description:

MethodReturn valuesDescription

findAllFiltered(String txt)

List<Config>

Returns all configuration parameters whose key contains the given filter text.

Example:

package com.openkm;

import com.openkm.db.bean.Config;
import com.openkm.db.service.ConfigSrv;
import com.openkm.util.ContextWrapper;

public class Test {

    public static void main(String[] args) {
        try {
            ConfigSrv configSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
            for (Config cfg : configSrv.findAllFiltered("plugin.myPlugin")) {
                System.out.println(cfg.getKey() + " = " + cfg.getValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}