Creating your own Suggestbox plugin

You can create your own Suggestbox

Conditions:

  • The new Suggestbox class must implement the "SuggestBoxValues" interface.
  • The new Suggestbox class must be declared under the package "com.openkm.plugin.form.values".
  • The new Suggestbox class must be annotated with "@PluginImplementation".
  • The new Suggestbox class must extend of "BasePlugin".

Suggestbox interface:

package com.openkm.plugin.form.values;

import com.openkm.db.bean.KeyValue;
import net.xeoh.plugins.base.Plugin;

import java.util.List;

public interface SuggestBoxValues extends Plugin {

    String getName();

    List<KeyValue> getSuggestBoxValuesByFilter(String filterValue) throws SuggestBoxException;

    KeyValue getExactKeyValueByClassName(String value) throws SuggestBoxException;

}

The new class must be loaded into the package com.openkm.plugin.form.values because application plugin system will try to load from there.

Do not miss the tag @PluginImplementation otherwise the application plugin system will not be able to retrieve the new class.

More information at: Register a new plugin.

Methods description

MethodTypeDescription

getName()

String

Return the name what will be shown at plugins table.

getSuggestBoxValuesByFilter(String filterValue)

List<KeyValue>

Return a list with the object KeyValue.

getExactKeyValueByClassName(String value)

 KeyValue  Return a object KeyValue.

Example of implementation

package com.openkm.plugin.form.values;

import net.xeoh.plugins.base.annotations.PluginImplementation;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.openkm.api.OKMAuth;
import com.openkm.bean.CommonUser;
import com.openkm.db.bean.KeyValue;
import com.openkm.plugin.BasePlugin;
import com.openkm.principal.PrincipalAdapterException;

@PluginImplementation
public class SuggestBoxUserList extends BasePlugin implements SuggestBoxValues {

    @Autowired
    private OKMAuth okmAuth;

    @Override
    public String getName() {
        return "User list suggest box";
    }

    @Override
    public List<KeyValue> getSuggestBoxValuesByFilter(String filterValue) throws SuggestBoxException {
        List<KeyValue> list = new ArrayList<>();

        try {
            for (CommonUser user : okmAuth.getUsers(null)) {
                if (user.getName().toLowerCase().contains(filterValue.toLowerCase())) {
                    KeyValue value = new KeyValue();
                    value.setKey(user.getId());
                    value.setValue(user.getName());
                    list.add(value);
                }
            }
        } catch (PrincipalAdapterException e) {
            throw new SuggestBoxException(e);
        }

        return list;
    }

    @Override
    public KeyValue getExactKeyValueByClassName(String value) throws SuggestBoxException {
        KeyValue keyValue = null;

        try {
            for (CommonUser user : okmAuth.getUsers(null)) {
                if (user.equals(value)) {
                    keyValue = new KeyValue();
                    keyValue.setKey(user.getId());
                    keyValue.setValue(user.getName());
                    break;
                }
            }
        } catch (PrincipalAdapterException e) {
            throw new SuggestBoxException(e);
        }

        return keyValue;
    }
}