Creating your own Report plugin

Report plugin are used to generate your own reports to be rendered in the OpenKM UI.

You can create your own Report plugin.

Conditions:

  • The new Report Plugin class must implement the "Report" interface.
  • The new Report Plugin class must be declared under the package "com.openkm.plugin.report".
  • The new Report Plugin class must be annotated with "@PluginImplementation".
  • The new Report Plugin class must extend of "BasePlugin".

Report interface:

package com.openkm.plugin.report;

import com.openkm.bean.PageInfo;
import com.openkm.bean.form.FormElement;
import com.openkm.core.AccessDeniedException;
import com.openkm.core.DatabaseException;
import com.openkm.core.PathNotFoundException;
import com.openkm.core.RepositoryException;
import net.xeoh.plugins.base.Plugin;

import java.util.List;
import java.util.Map;

/**
* Report plugin
*/
public interface Report extends Plugin {

List<FormElement> getFormElements();

PageInfo execute(Map<String, String> params) throws AccessDeniedException, PathNotFoundException, RepositoryException, DatabaseException;

String getName();
}

The new class must be loaded into the package com.openkm.plugin.report because the application plugins 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

getFormElements()

List<FormElement>

The method returns a list of FormElement objects what will be rendered in the UI asking for report filtering values.

execute(Map<String, String> params)

PageInfo

Returns a list of elements related to the report.

The params variables contain the values of the form elements set from the UI by the user.

getName()

String

Return the name of the report.

Example of the Report plugin implementation

In this example, we return a list of documents filtered by document name as an input value, and also with a limit of results as input value (if it is empty by default the limit is 100). The list only shows 3 fields related to the document (Author, Path, Date)

package com.openkm.plugin.report;

import com.openkm.bean.Folder;
import com.openkm.bean.PageInfo;
import com.openkm.bean.form.FormElement;
import com.openkm.bean.form.Input;
import com.openkm.core.AccessDeniedException;
import com.openkm.core.DatabaseException;
import com.openkm.core.PathNotFoundException;
import com.openkm.core.RepositoryException;
import com.openkm.db.bean.NodeBase;
import com.openkm.db.bean.NodeDocument;
import com.openkm.db.service.NodeBaseSrv;
import com.openkm.module.db.DbRepositoryModule;
import com.openkm.plugin.BasePlugin;
import com.openkm.util.FormatUtil;
import com.openkm.ws.rest.util.SimpleNodeBase;
import com.openkm.ws.rest.util.SimpleRowReport;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

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

@PluginImplementation
public class TestReport extends BasePlugin implements Report {
	private static final Logger log = LoggerFactory.getLogger(TestReport.class);

	private static final String REPORTS_DEFAULT_LIMIT = "100";
	private static final String REPORT_INPUT_FILTER = "filter";
	private static final String REPORT_INPUT_LIMIT = "limit";
	private static final String REPORT_COLUMN_LABEL_1 = "Author";
	private static final String REPORT_COLUMN_LABEL_2 = "Path";
	private static final String REPORT_COLUMN_LABEL_3 = "Date";

	@Autowired
	private DbRepositoryModule dbRepositoryModule;

	@Autowired
	private NodeBaseSrv nodeBaseSrv;

	@Override
	public String getName() {
		return "Consulting documents sample";
	}

	@Override
	public List getFormElements() {
		List formElements = new ArrayList<>();

		FormElement feInputFilter = new Input();
		feInputFilter.setName(REPORT_INPUT_FILTER);
		feInputFilter.setLabel("Filter");

		FormElement feInputLimit = new Input();
		feInputLimit.setName(REPORT_INPUT_LIMIT);
		feInputLimit.setLabel("Limit");

		formElements.add(feInputFilter);
		formElements.add(feInputLimit);

		return formElements;
	}

	@Override
	public PageInfo execute(Map<String, String> params) throws AccessDeniedException, PathNotFoundException,
			RepositoryException, DatabaseException {
		PageInfo reportPageInfo = new PageInfo();
		List simpleRowReportList = new ArrayList<>();
		Folder rootFolder = dbRepositoryModule.getRootFolder(null);

		String filter = params.get(REPORT_INPUT_FILTER);
		String limit = StringUtils.isNotEmpty(params.get(REPORT_INPUT_LIMIT)) ? params.get(REPORT_INPUT_LIMIT) : REPORTS_DEFAULT_LIMIT;

		List<Class<? extends NodeBase>> filteredByNodeTypeList = new ArrayList<>();
		filteredByNodeTypeList.add(NodeDocument.class);

		PageInfo pageInfo = nodeBaseSrv.getCommonChildrenPaginated(rootFolder.getUuid(), 0, Integer.parseInt(limit), filter, "name",
				true, filteredByNodeTypeList);

		// Only fill the metadata values when results size greater than 0
		if (pageInfo.getObjects().size() > 0) {
			for (SimpleNodeBase snb : (List) pageInfo.getObjects()) {
				SimpleRowReport simpleRowReportPlugin = new SimpleRowReport();
				simpleRowReportPlugin.setUuid(snb.getUuid());
				simpleRowReportPlugin.setName(snb.getName());
				simpleRowReportPlugin.setReportColumnLabel1(REPORT_COLUMN_LABEL_1);
				simpleRowReportPlugin.setReportColumnLabel2(REPORT_COLUMN_LABEL_2);
				simpleRowReportPlugin.setReportColumnLabel3(REPORT_COLUMN_LABEL_3);
				simpleRowReportPlugin.setReportColumnValue1(snb.getAuthor());
				simpleRowReportPlugin.setReportColumnValue2(snb.getPath());
				simpleRowReportPlugin.setReportColumnValue3(FormatUtil.formatDate(snb.getVersionCreated())); // formatted from backend

				simpleRowReportList.add(simpleRowReportPlugin);
			}

			reportPageInfo.setTotalElements(pageInfo.getObjects().size());
			reportPageInfo.setObjects(simpleRowReportList);
		}

		return reportPageInfo;
	}
}