Skip to content
Other versions

Loading…

Creating your own Suggestion Analyzer

To create your own Suggestion Analyzer must create a new class that implements Suggestion interface:

package com.openkm.form.suggestion;
import java.util.List;
import com.openkm.bean.form.Select;
import com.openkm.core.DatabaseException;
import com.openkm.core.PathNotFoundException;
public interface Suggestion {
List<String> getSuggestions(String nodeUuid, String nodePath, Select sel) throws PathNotFoundException, SuggestionException, DatabaseException;
}

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

Method Type Description
getSuggestions(String nodeUuid, String nodePath, Select sel) List Return a list of suggested words to tag the document.
The method need the document uuid ( nodeUuid ) and the document path ( nodePath ).
Select can be used as a collection of possible values ( enclosed dictionary ).
package com.openkm.form.suggestion;
import com.openkm.bean.form.Option;
import com.openkm.bean.form.Select;
import com.openkm.core.DatabaseException;
import com.openkm.core.PathNotFoundException;
import com.openkm.dao.NodeDocumentDAO;
import com.openkm.dao.NodeDocumentUtilsDAO;
import com.openkm.dao.NodeDocumentVersionDAO;
import com.openkm.dao.SearchDAO;
import com.openkm.dao.bean.NodeDocument;
import com.openkm.dao.bean.NodeDocumentVersion;
import com.openkm.extractor.TextExtractorWork;
import com.openkm.spring.PrincipalUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DocumentTermsSuggestion implements Suggestion {
private static Logger log = LoggerFactory.getLogger(DocumentTermsSuggestion.class);
@Override
public List<String> getSuggestions(String nodeUuid, String nodePath, Select sel) throws PathNotFoundException, SuggestionException,
DatabaseException {
log.debug("getSuggestions({}, {}, {})", new Object[] { nodeUuid, nodePath, sel });
List<String> list = new ArrayList<String>();
try {
if (NodeDocumentDAO.getInstance().itemExists(nodeUuid)) {
// Get text extraction
if (!NodeDocumentUtilsDAO.getInstance().isTextExtracted(nodeUuid)) {
// Force text extraction
NodeDocumentVersion nDocVer = NodeDocumentVersionDAO.getInstance().findCurrentVersion(nodeUuid);
TextExtractorWork tew = new TextExtractorWork();
tew.setDocUuid(nodeUuid);
tew.setDocPath(nodePath);
tew.setDocVerUuid(nDocVer.getUuid());
tew.setTenant(PrincipalUtils.getTenantId());
// Execute extractor
NodeDocumentUtilsDAO.getInstance().textExtractorHelper(tew);
}
for (String term : SearchDAO.getInstance().getTerms(NodeDocument.class, nodeUuid)) {
for (Option opt : sel.getOptions()) {
if (term.equals(opt.getLabel().toLowerCase())) {
if (!list.contains(opt.getValue())) {
list.add(opt.getValue());
break;
}
}
}
}
}
} catch (IOException e) {
throw new SuggestionException("IOException: " + e.getMessage(), e);
}
return list;
}
}