mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
Webscripts (or plain javascript) in the repo tier can now optionally provide an additional 'store' reference as the first argument to the various search APIs:
- search.luceneSearch("TEXT:alfresco") <- original API - search.luceneSearch("workspace://SiteStore", "TEXT:website") <- new API This allows script developers to search other stores such as the new SiteStore. NOTE: some script writers were trying to use the public Sping bean setter method on the Search object called setStoreUrl() as a hack to do this previously - that technique is NOT thread safe and should not be used - that mechanism has now been blocked by the bean and will throw an exception. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@9508 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -39,6 +39,7 @@ import org.alfresco.service.cmr.search.ResultSet;
|
|||||||
import org.alfresco.service.cmr.search.ResultSetRow;
|
import org.alfresco.service.cmr.search.ResultSetRow;
|
||||||
import org.alfresco.service.cmr.search.SearchParameters;
|
import org.alfresco.service.cmr.search.SearchParameters;
|
||||||
import org.alfresco.service.cmr.search.SearchService;
|
import org.alfresco.service.cmr.search.SearchService;
|
||||||
|
import org.alfresco.util.ParameterCheck;
|
||||||
import org.dom4j.Document;
|
import org.dom4j.Document;
|
||||||
import org.dom4j.Element;
|
import org.dom4j.Element;
|
||||||
import org.dom4j.io.SAXReader;
|
import org.dom4j.io.SAXReader;
|
||||||
@@ -69,6 +70,7 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
/** Repository helper */
|
/** Repository helper */
|
||||||
private Repository repository;
|
private Repository repository;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the default store reference
|
* Set the default store reference
|
||||||
*
|
*
|
||||||
@@ -76,6 +78,11 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
*/
|
*/
|
||||||
public void setStoreUrl(String storeRef)
|
public void setStoreUrl(String storeRef)
|
||||||
{
|
{
|
||||||
|
// ensure this is not set again by a script instance!
|
||||||
|
if (this.storeRef != null)
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("Default store URL can only be set once.");
|
||||||
|
}
|
||||||
this.storeRef = new StoreRef(storeRef);
|
this.storeRef = new StoreRef(storeRef);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,6 +106,9 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// JavaScript API
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a single Node by the Node reference
|
* Find a single Node by the Node reference
|
||||||
*
|
*
|
||||||
@@ -108,7 +118,17 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
*/
|
*/
|
||||||
public ScriptNode findNode(NodeRef ref)
|
public ScriptNode findNode(NodeRef ref)
|
||||||
{
|
{
|
||||||
return findNode(ref.toString());
|
ParameterCheck.mandatory("ref", ref);
|
||||||
|
String query = "ID:" + LuceneQueryParser.escape(ref.toString());
|
||||||
|
Object[] result = query(ref.getStoreRef().toString(), query, SearchService.LANGUAGE_LUCENE);
|
||||||
|
if (result.length != 0)
|
||||||
|
{
|
||||||
|
return (ScriptNode)result[0];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -120,16 +140,8 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
*/
|
*/
|
||||||
public ScriptNode findNode(String ref)
|
public ScriptNode findNode(String ref)
|
||||||
{
|
{
|
||||||
String query = "ID:" + LuceneQueryParser.escape(ref);
|
ParameterCheck.mandatoryString("ref", ref);
|
||||||
Object[] result = query(query, SearchService.LANGUAGE_LUCENE);
|
return findNode(new NodeRef(ref));
|
||||||
if (result.length != 0)
|
|
||||||
{
|
|
||||||
return (ScriptNode)result[0];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -157,6 +169,8 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
*/
|
*/
|
||||||
public ScriptNode findNode(String referenceType, String[] reference)
|
public ScriptNode findNode(String referenceType, String[] reference)
|
||||||
{
|
{
|
||||||
|
ParameterCheck.mandatoryString("referenceType", referenceType);
|
||||||
|
ParameterCheck.mandatory("reference", reference);
|
||||||
ScriptNode result = null;
|
ScriptNode result = null;
|
||||||
NodeRef nodeRef = this.repository.findNodeRef(referenceType, reference);
|
NodeRef nodeRef = this.repository.findNodeRef(referenceType, reference);
|
||||||
if (nodeRef != null)
|
if (nodeRef != null)
|
||||||
@@ -174,10 +188,23 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
* @return JavaScript array of Node results from the search - can be empty but not null
|
* @return JavaScript array of Node results from the search - can be empty but not null
|
||||||
*/
|
*/
|
||||||
public Scriptable xpathSearch(String search)
|
public Scriptable xpathSearch(String search)
|
||||||
|
{
|
||||||
|
return xpathSearch(null, search);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a XPath search
|
||||||
|
*
|
||||||
|
* @param store Store reference to search against i.e. workspace://SpacesStore
|
||||||
|
* @param search XPath search string to execute
|
||||||
|
*
|
||||||
|
* @return JavaScript array of Node results from the search - can be empty but not null
|
||||||
|
*/
|
||||||
|
public Scriptable xpathSearch(String store, String search)
|
||||||
{
|
{
|
||||||
if (search != null && search.length() != 0)
|
if (search != null && search.length() != 0)
|
||||||
{
|
{
|
||||||
Object[] results = query(search, SearchService.LANGUAGE_XPATH);
|
Object[] results = query(store, search, SearchService.LANGUAGE_XPATH);
|
||||||
return Context.getCurrentContext().newArray(getScope(), results);
|
return Context.getCurrentContext().newArray(getScope(), results);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -194,10 +221,23 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
* @return JavaScript array of Node results from the search - can be empty but not null
|
* @return JavaScript array of Node results from the search - can be empty but not null
|
||||||
*/
|
*/
|
||||||
public Scriptable luceneSearch(String search)
|
public Scriptable luceneSearch(String search)
|
||||||
|
{
|
||||||
|
return luceneSearch(null, search);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a Lucene search
|
||||||
|
*
|
||||||
|
* @param store Store reference to search against i.e. workspace://SpacesStore
|
||||||
|
* @param search Lucene search string to execute
|
||||||
|
*
|
||||||
|
* @return JavaScript array of Node results from the search - can be empty but not null
|
||||||
|
*/
|
||||||
|
public Scriptable luceneSearch(String store, String search)
|
||||||
{
|
{
|
||||||
if (search != null && search.length() != 0)
|
if (search != null && search.length() != 0)
|
||||||
{
|
{
|
||||||
Object[] results = query(search, SearchService.LANGUAGE_LUCENE);
|
Object[] results = query(store, search, SearchService.LANGUAGE_LUCENE);
|
||||||
return Context.getCurrentContext().newArray(getScope(), results);
|
return Context.getCurrentContext().newArray(getScope(), results);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -216,6 +256,21 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
* @return JavaScript array of Node results from the search - can be empty but not null
|
* @return JavaScript array of Node results from the search - can be empty but not null
|
||||||
*/
|
*/
|
||||||
public Scriptable luceneSearch(String search, String sortColumn, boolean asc)
|
public Scriptable luceneSearch(String search, String sortColumn, boolean asc)
|
||||||
|
{
|
||||||
|
return luceneSearch(null, search, sortColumn, asc);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a Lucene search (sorted)
|
||||||
|
*
|
||||||
|
* @param store Store reference to search against i.e. workspace://SpacesStore
|
||||||
|
* @param search Lucene search string to execute
|
||||||
|
* @param sortKey property name to sort on
|
||||||
|
* @param asc true => ascending sort
|
||||||
|
*
|
||||||
|
* @return JavaScript array of Node results from the search - can be empty but not null
|
||||||
|
*/
|
||||||
|
public Scriptable luceneSearch(String store, String search, String sortColumn, boolean asc)
|
||||||
{
|
{
|
||||||
if (search == null || search.length() == 0)
|
if (search == null || search.length() == 0)
|
||||||
{
|
{
|
||||||
@@ -223,12 +278,12 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
}
|
}
|
||||||
if (sortColumn == null || sortColumn.length() == 0)
|
if (sortColumn == null || sortColumn.length() == 0)
|
||||||
{
|
{
|
||||||
return luceneSearch(search);
|
return luceneSearch(store, search);
|
||||||
}
|
}
|
||||||
|
|
||||||
SortColumn[] sort = new SortColumn[1];
|
SortColumn[] sort = new SortColumn[1];
|
||||||
sort[0] = new SortColumn(sortColumn, asc);
|
sort[0] = new SortColumn(sortColumn, asc);
|
||||||
Object[] results = query(search, sort, SearchService.LANGUAGE_LUCENE);
|
Object[] results = query(store, search, sort, SearchService.LANGUAGE_LUCENE);
|
||||||
return Context.getCurrentContext().newArray(getScope(), results);
|
return Context.getCurrentContext().newArray(getScope(), results);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -272,7 +327,7 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
|
|
||||||
if (search != null)
|
if (search != null)
|
||||||
{
|
{
|
||||||
Object[] results = query(search, SearchService.LANGUAGE_LUCENE);
|
Object[] results = query(null, search, SearchService.LANGUAGE_LUCENE);
|
||||||
return Context.getCurrentContext().newArray(getScope(), results);
|
return Context.getCurrentContext().newArray(getScope(), results);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -303,28 +358,31 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
/**
|
/**
|
||||||
* Execute the query
|
* Execute the query
|
||||||
*
|
*
|
||||||
* Removes any duplicates that may be present (ID search can cause duplicates - it is better to remove them here)
|
* Removes any duplicates that may be present (ID search can cause duplicates -
|
||||||
|
* it is better to remove them here)
|
||||||
*
|
*
|
||||||
|
* @param store StoreRef to search against - null for default configured store
|
||||||
* @param search Lucene search to execute
|
* @param search Lucene search to execute
|
||||||
* @param language Search language to use e.g. SearchService.LANGUAGE_LUCENE
|
* @param language Search language to use e.g. SearchService.LANGUAGE_LUCENE
|
||||||
*
|
*
|
||||||
* @return Array of Node objects
|
* @return Array of Node objects
|
||||||
*/
|
*/
|
||||||
private Object[] query(String search, String language)
|
private Object[] query(String store, String search, String language)
|
||||||
{
|
{
|
||||||
LinkedHashSet<ScriptNode> set = new LinkedHashSet<ScriptNode>();
|
LinkedHashSet<ScriptNode> set = null;
|
||||||
|
|
||||||
// perform the search against the repo
|
// perform the search against the repo
|
||||||
ResultSet results = null;
|
ResultSet results = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
results = this.services.getSearchService().query(
|
results = this.services.getSearchService().query(
|
||||||
this.storeRef,
|
store != null ? new StoreRef(store) : this.storeRef,
|
||||||
language,
|
language,
|
||||||
search);
|
search);
|
||||||
|
|
||||||
if (results.length() != 0)
|
if (results.length() != 0)
|
||||||
{
|
{
|
||||||
|
set = new LinkedHashSet<ScriptNode>(results.length(), 1.0f);
|
||||||
for (ResultSetRow row: results)
|
for (ResultSetRow row: results)
|
||||||
{
|
{
|
||||||
NodeRef nodeRef = row.getNodeRef();
|
NodeRef nodeRef = row.getNodeRef();
|
||||||
@@ -344,30 +402,32 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return set.toArray(new Object[(set.size())]);
|
return set != null ? set.toArray(new Object[(set.size())]) : new Object[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the query
|
* Execute the query
|
||||||
*
|
*
|
||||||
* Removes any duplicates that may be present (ID search can cause duplicates - it is better to remove them here)
|
* Removes any duplicates that may be present (ID search can cause duplicates -
|
||||||
|
* it is better to remove them here)
|
||||||
*
|
*
|
||||||
|
* @param store StoreRef to search against - null for default configured store
|
||||||
* @param search Lucene search to execute
|
* @param search Lucene search to execute
|
||||||
* @param sort Columns to sort by
|
* @param sort Columns to sort by
|
||||||
* @param language Search language to use e.g. SearchService.LANGUAGE_LUCENE
|
* @param language Search language to use e.g. SearchService.LANGUAGE_LUCENE
|
||||||
*
|
*
|
||||||
* @return Array of Node objects
|
* @return Array of Node objects
|
||||||
*/
|
*/
|
||||||
private Object[] query(String search, SortColumn[] sort, String language)
|
private Object[] query(String store, String search, SortColumn[] sort, String language)
|
||||||
{
|
{
|
||||||
LinkedHashSet<ScriptNode> set = new LinkedHashSet<ScriptNode>();
|
LinkedHashSet<ScriptNode> set = null;
|
||||||
|
|
||||||
// perform the search against the repo
|
// perform the search against the repo
|
||||||
ResultSet results = null;
|
ResultSet results = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
SearchParameters sp = new SearchParameters();
|
SearchParameters sp = new SearchParameters();
|
||||||
sp.addStore(this.storeRef);
|
sp.addStore(store != null ? new StoreRef(store) : this.storeRef);
|
||||||
sp.setLanguage(language);
|
sp.setLanguage(language);
|
||||||
sp.setQuery(search);
|
sp.setQuery(search);
|
||||||
if (sort != null)
|
if (sort != null)
|
||||||
@@ -382,6 +442,7 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
|
|
||||||
if (results.length() != 0)
|
if (results.length() != 0)
|
||||||
{
|
{
|
||||||
|
set = new LinkedHashSet<ScriptNode>(results.length(), 1.0f);
|
||||||
for (ResultSetRow row: results)
|
for (ResultSetRow row: results)
|
||||||
{
|
{
|
||||||
NodeRef nodeRef = row.getNodeRef();
|
NodeRef nodeRef = row.getNodeRef();
|
||||||
@@ -401,9 +462,10 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return set.toArray(new Object[(set.size())]);
|
return set != null ? set.toArray(new Object[(set.size())]) : new Object[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search sort column
|
* Search sort column
|
||||||
*/
|
*/
|
||||||
@@ -424,5 +486,4 @@ public final class Search extends BaseScopableProcessorExtension
|
|||||||
public String column;
|
public String column;
|
||||||
public boolean asc;
|
public boolean asc;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user