Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Issue #554
  • Loading branch information
rsoika committed Aug 26, 2019
1 parent 19f1b8e commit 06df19d
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import javax.annotation.Resource;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RunAs;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
Expand All @@ -22,7 +21,6 @@
import org.imixs.workflow.WorkflowKernel;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.engine.WorkflowService;
import org.imixs.workflow.engine.index.UpdateService;
import org.imixs.workflow.engine.plugins.OwnerPlugin;
import org.imixs.workflow.exceptions.AccessDeniedException;
import org.imixs.workflow.exceptions.InvalidAccessException;
Expand Down Expand Up @@ -74,9 +72,7 @@ public class JobHandlerRenameUser implements JobHandler {
@Inject
DocumentService documentService;

@Inject
UpdateService luceneService;


private static final int DEFAULT_COUNT = 100;
private static Logger logger = Logger.getLogger(JobHandlerRenameUser.class.getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,44 +39,58 @@

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.exceptions.QueryException;

/**
* The IndexSchemaService provides the index Schema.
* <p>
* The schema is defined by the following properties:
*
* <ul>
* <li>index.fields - content which will be indexed</li>
* <li>index.fields.analyse - fields indexed as analyzed keyword fields </li>
* <li>index.fields.noanalyse - fields indexed without analyze</li>
* <li>index.fields.store - fields stored in the index</li>
* <li>index.operator - default operator</li>
* <li>index.splitwhitespace - split text on whitespace prior to analysis</li>
* </ul>
*
*
* @version 1.0
* @author rsoika
*/
@Singleton
public class SchemaService {


public class SchemaService {

/*
* index.fields
* index.fields.analyse
* index.fields.noanalyse
* index.fields.store
* index.fields index.fields.analyse index.fields.noanalyse index.fields.store
*
* index.operator
* index.splitwhitespace
* index.operator index.splitwhitespace
*
*
*/

public static final String ANONYMOUS = "ANONYMOUS";

@Inject
@ConfigProperty(name = "lucence.fulltextFieldList", defaultValue = "")
private String luceneFulltextFieldList;
@ConfigProperty(name = "index.fields", defaultValue = "")
private String indexFields;

@Inject
@ConfigProperty(name = "lucence.indexFieldListAnalyze", defaultValue = "")
private String luceneIndexFieldListAnalyse;
@ConfigProperty(name = "index.fields.analyze", defaultValue = "")
private String indexFieldsAnalyse;

@Inject
@ConfigProperty(name = "lucence.indexFieldListNoAnalyze", defaultValue = "")
private String luceneIndexFieldListNoAnalyse;
@ConfigProperty(name = "index.fields.noanalyze", defaultValue = "")
private String indexFieldsNoAnalyse;

@Inject
@ConfigProperty(name = "lucence.indexFieldListStore", defaultValue = "")
private String luceneIndexFieldListStore;
@ConfigProperty(name = "index.fields.store", defaultValue = "")
private String indexFieldsStore;

@Inject
private DocumentService documentService;

private List<String> fieldList = null;
private List<String> fieldListAnalyse = null;
Expand All @@ -94,8 +108,6 @@ public class SchemaService {
"$workflowsummary", "$workflowabstract", "$workflowgroup", "$workflowstatus", "$modified", "$created",
"$lasteventdate", "$creator", "$editor", "$lasteditor", "$owner", "namowner");



private static Logger logger = Logger.getLogger(SchemaService.class.getName());

/**
Expand All @@ -106,17 +118,17 @@ public class SchemaService {
*/
@PostConstruct
void init() {
logger.finest("......lucene FulltextFieldList=" + luceneFulltextFieldList);
logger.finest("......lucene IndexFieldListAnalyse=" + luceneIndexFieldListAnalyse);
logger.finest("......lucene IndexFieldListNoAnalyse=" + luceneIndexFieldListNoAnalyse);

logger.finest("......lucene FulltextFieldList=" + indexFields);
logger.finest("......lucene IndexFieldListAnalyse=" + indexFieldsAnalyse);
logger.finest("......lucene IndexFieldListNoAnalyse=" + indexFieldsNoAnalyse);

// compute search field list
fieldList = new ArrayList<String>();
// add all static default field list
fieldList.addAll(DEFAULT_SEARCH_FIELD_LIST);
if (luceneFulltextFieldList != null && !luceneFulltextFieldList.isEmpty()) {
StringTokenizer st = new StringTokenizer(luceneFulltextFieldList, ",");
if (indexFields != null && !indexFields.isEmpty()) {
StringTokenizer st = new StringTokenizer(indexFields, ",");
while (st.hasMoreElements()) {
String sName = st.nextToken().toLowerCase().trim();
// do not add internal fields
Expand All @@ -127,8 +139,8 @@ void init() {

// compute Index field list (Analyze)
fieldListAnalyse = new ArrayList<String>();
if (luceneIndexFieldListAnalyse != null && !luceneIndexFieldListAnalyse.isEmpty()) {
StringTokenizer st = new StringTokenizer(luceneIndexFieldListAnalyse, ",");
if (indexFieldsAnalyse != null && !indexFieldsAnalyse.isEmpty()) {
StringTokenizer st = new StringTokenizer(indexFieldsAnalyse, ",");
while (st.hasMoreElements()) {
String sName = st.nextToken().toLowerCase().trim();
// do not add internal fields
Expand All @@ -141,9 +153,9 @@ void init() {
fieldListNoAnalyse = new ArrayList<String>();
// add all static default field list
fieldListNoAnalyse.addAll(DEFAULT_NOANALYSE_FIELD_LIST);
if (luceneIndexFieldListNoAnalyse != null && !luceneIndexFieldListNoAnalyse.isEmpty()) {
if (indexFieldsNoAnalyse != null && !indexFieldsNoAnalyse.isEmpty()) {
// add additional field list from imixs.properties
StringTokenizer st = new StringTokenizer(luceneIndexFieldListNoAnalyse, ",");
StringTokenizer st = new StringTokenizer(indexFieldsNoAnalyse, ",");
while (st.hasMoreElements()) {
String sName = st.nextToken().toLowerCase().trim();
if (!fieldListNoAnalyse.contains(sName))
Expand All @@ -155,9 +167,9 @@ void init() {
fieldListStore = new ArrayList<String>();
// add all static default field list
fieldListStore.addAll(DEFAULT_STORE_FIELD_LIST);
if (luceneIndexFieldListStore != null && !luceneIndexFieldListStore.isEmpty()) {
if (indexFieldsStore != null && !indexFieldsStore.isEmpty()) {
// add additional field list from imixs.properties
StringTokenizer st = new StringTokenizer(luceneIndexFieldListStore, ",");
StringTokenizer st = new StringTokenizer(indexFieldsStore, ",");
while (st.hasMoreElements()) {
String sName = st.nextToken().toLowerCase().trim();
if (!fieldListStore.contains(sName))
Expand All @@ -177,45 +189,23 @@ void init() {
}

}





public List<String> getFieldList() {
return fieldList;
}







public List<String> getFieldListAnalyse() {
return fieldListAnalyse;
}





public List<String> getFieldListNoAnalyse() {
return fieldListNoAnalyse;
}





public List<String> getFieldListStore() {
return fieldListStore;
}





/**
* Returns the Lucene schema configuration
*
Expand All @@ -234,4 +224,88 @@ public ItemCollection getConfiguration() {



/**
* Returns the extended search term for a given query. The search term will be
* extended with a users roles to test the read access level of each workitem
* matching the search term.
*
* @param sSearchTerm
* @return extended search term
* @throws QueryException
* in case the searchtem is not understandable.
*/
public String getExtendedSearchTerm(String sSearchTerm) throws QueryException {
// test if searchtem is provided
if (sSearchTerm == null || "".equals(sSearchTerm)) {
logger.warning("No search term provided!");
return "";
}
// extend the Search Term if user is not ACCESSLEVEL_MANAGERACCESS
if (!documentService.isUserInRole(DocumentService.ACCESSLEVEL_MANAGERACCESS)) {
// get user names list
List<String> userNameList = documentService.getUserNameList();
// create search term (always add ANONYMOUS)
String sAccessTerm = "($readaccess:" + ANONYMOUS;
for (String aRole : userNameList) {
if (!"".equals(aRole))
sAccessTerm += " OR $readaccess:\"" + aRole + "\"";
}
sAccessTerm += ") AND ";
sSearchTerm = sAccessTerm + sSearchTerm;
}
logger.finest("......lucene final searchTerm=" + sSearchTerm);

return sSearchTerm;
}


/**
* This helper method escapes wildcard tokens found in a lucene search term. The
* method can be used by clients to prepare a search phrase.
*
* The method rewrites the lucene <code>QueryParser.escape</code> method and did
* not! escape '*' char.
*
* Clients should use the method normalizeSearchTerm() instead of
* escapeSearchTerm() to prepare a user input for a lucene search.
*
*
* @see normalizeSearchTerm
* @param searchTerm
* @param ignoreBracket
* - if true brackes will not be escaped.
* @return escaped search term
*/
public String escapeSearchTerm(String searchTerm, boolean ignoreBracket) {
if (searchTerm == null || searchTerm.isEmpty()) {
return searchTerm;
}

// this is the code from the QueryParser.escape() method without the '*'
// char!
StringBuilder sb = new StringBuilder();
for (int i = 0; i < searchTerm.length(); i++) {
char c = searchTerm.charAt(i);
// These characters are part of the query syntax and must be escaped
// (ignore brackets!)
if (c == '\\' || c == '+' || c == '-' || c == '!' || c == ':' || c == '^' || c == '[' || c == ']'
|| c == '\"' || c == '{' || c == '}' || c == '~' || c == '?' || c == '|' || c == '&' || c == '/') {
sb.append('\\');
}

// escape bracket?
if (!ignoreBracket && (c == '(' || c == ')')) {
sb.append('\\');
}

sb.append(c);
}
return sb.toString();

}

public String escapeSearchTerm(String searchTerm) {
return escapeSearchTerm(searchTerm, false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,10 @@
import org.imixs.workflow.exceptions.QueryException;

/**
* This session ejb provides a service to search the lucene index. The EJB uses
* the IndexSearcher to query the current index. As the index can change across
* multiple searches we can not share a single IndexSearcher instance. For that
* reason the EJB is creating a new IndexSearch per-search.
* This SearchService defines methods to search workitems or collections of
* workitems.
*
* The service provides a set of public methods which can be used to query
* workitems or collections of workitems. A search term can be escaped by
* calling the method <code>escpeSearchTerm</code>. This method prepends a
* <code>\</code> for those characters that QueryParser expects to be escaped.
*
* @see http://stackoverflow.com/questions/34880347/why-did-lucene-indexwriter-
* did-not-update-the-index-when-called-from-a-web-modul
* @version 2.0
* @version 1.0
* @author rsoika
*/
@Local
Expand All @@ -58,7 +49,6 @@ public interface SearchService {
// number of hits
public static final int DEFAULT_PAGE_SIZE = 100; // default docs in one page


/**
* Returns a collection of documents matching the provided search term. The
* provided search team will we extended with a users roles to test the read
Expand All @@ -84,18 +74,19 @@ public interface SearchService {
* @throws QueryException
*/
public List<ItemCollection> search(String sSearchTerm, int pageSize, int pageIndex) throws QueryException;

/**
* Returns a collection of documents matching the provided search term. The term
* will be extended with the current users roles to test the read access level
* of each workitem matching the search term.
* <p>
* The method returns the full loaded documents. If you only want to search for document stubs use instead the method
* The method returns the full loaded documents. If you only want to search for
* document stubs use instead the method
* <p>
* <code>search(String searchTerm, int pageSize, int pageIndex, Sort sortOrder,
Operator defaultOperator, boolean loadStubs)</code>
<p>
* <p>
*
*/
public List<ItemCollection> search(String sSearchTerm, int pageSize, int pageIndex, SortOrder sortOrder,
DefaultOperator defaultOperator) throws QueryException;
Expand Down Expand Up @@ -133,7 +124,6 @@ public List<ItemCollection> search(String sSearchTerm, int pageSize, int pageInd
*/
public List<ItemCollection> search(String searchTerm, int pageSize, int pageIndex, SortOrder sortOrder,
DefaultOperator defaultOperator, boolean loadStubs) throws QueryException;


/**
* Returns the total hits for a given search term from the lucene index. The
Expand All @@ -153,5 +143,6 @@ public List<ItemCollection> search(String searchTerm, int pageSize, int pageInde
* @throws QueryException
* in case the searchterm is not understandable.
*/
public int getTotalHits(final String _searchTerm, final int _maxResult, final DefaultOperator defaultOperator) throws QueryException;
public int getTotalHits(final String _searchTerm, final int _maxResult, final DefaultOperator defaultOperator)
throws QueryException;
}
Loading

0 comments on commit 06df19d

Please sign in to comment.