mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
ExportMetadata method now in SearchMetadataExportReader. Cleaned up code. Added some comments.
This commit is contained in:
@@ -17,13 +17,10 @@ import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.excalibur.source.SourceValidity;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.dspace.app.util.MetadataExposure;
|
||||
import org.dspace.app.bulkedit.DSpaceCSV;
|
||||
import org.dspace.app.bulkedit.MetadataExport;
|
||||
import org.dspace.app.xmlui.cocoon.AbstractDSpaceTransformer;
|
||||
import org.dspace.app.xmlui.utils.DSpaceValidity;
|
||||
import org.dspace.app.xmlui.utils.HandleUtil;
|
||||
import org.dspace.app.xmlui.utils.UIException;
|
||||
import org.dspace.app.xmlui.utils.ContextUtil;
|
||||
import org.dspace.app.xmlui.wing.Message;
|
||||
import org.dspace.app.xmlui.wing.WingException;
|
||||
import org.dspace.app.xmlui.wing.element.*;
|
||||
@@ -31,8 +28,6 @@ import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.content.*;
|
||||
import org.dspace.content.Collection;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.ItemIterator;
|
||||
import org.dspace.core.Context;
|
||||
import org.dspace.core.Constants;
|
||||
import org.dspace.core.LogManager;
|
||||
import org.dspace.discovery.*;
|
||||
@@ -63,36 +58,39 @@ import java.util.List;
|
||||
public abstract class AbstractSearch extends AbstractDSpaceTransformer implements CacheableProcessingComponent{
|
||||
|
||||
private static final Logger log = Logger.getLogger(AbstractSearch.class);
|
||||
|
||||
|
||||
/**
|
||||
* Language strings
|
||||
*/
|
||||
private static final Message T_head1_community =
|
||||
message("xmlui.Discovery.AbstractSearch.head1_community");
|
||||
|
||||
private static final Message T_head1_collection =
|
||||
message("xmlui.Discovery.AbstractSearch.head1_collection");
|
||||
|
||||
private static final Message T_head1_none =
|
||||
message("xmlui.Discovery.AbstractSearch.head1_none");
|
||||
|
||||
private static final Message T_no_results =
|
||||
message("xmlui.ArtifactBrowser.AbstractSearch.no_results");
|
||||
|
||||
private static final Message T_all_of_dspace =
|
||||
message("xmlui.ArtifactBrowser.AbstractSearch.all_of_dspace");
|
||||
|
||||
private static final Message T_sort_by_relevance =
|
||||
message("xmlui.Discovery.AbstractSearch.sort_by.relevance");
|
||||
private static final Message T_sort_by =
|
||||
message("xmlui.Discovery.AbstractSearch.sort_by.head");
|
||||
private static final Message T_rpp =
|
||||
message("xmlui.Discovery.AbstractSearch.rpp");
|
||||
private static final Message T_result_head_3 =
|
||||
message("xmlui.Discovery.AbstractSearch.head3");
|
||||
private static final Message T_result_head_2 =
|
||||
message("xmlui.Discovery.AbstractSearch.head2");
|
||||
|
||||
private static final Message T_sort_by = message("xmlui.Discovery.AbstractSearch.sort_by.head");
|
||||
|
||||
private static final Message T_rpp = message("xmlui.Discovery.AbstractSearch.rpp");
|
||||
private static final Message T_result_head_3 = message("xmlui.Discovery.AbstractSearch.head3");
|
||||
private static final Message T_result_head_2 = message("xmlui.Discovery.AbstractSearch.head2");
|
||||
|
||||
/**
|
||||
* Cached query results
|
||||
*/
|
||||
protected DiscoverResult queryResults;
|
||||
|
||||
|
||||
/**
|
||||
* Cached query arguments
|
||||
*/
|
||||
@@ -290,6 +288,7 @@ public abstract class AbstractSearch extends AbstractDSpaceTransformer implement
|
||||
Division results = search.addDivision("search-results", "primary");
|
||||
buildSearchControls(results);
|
||||
|
||||
|
||||
DSpaceObject searchScope = getScope();
|
||||
|
||||
int displayedResults;
|
||||
@@ -714,29 +713,34 @@ public abstract class AbstractSearch extends AbstractDSpaceTransformer implement
|
||||
* Query DSpace for a list of all items / collections / or communities that
|
||||
* match the given search query.
|
||||
*
|
||||
*
|
||||
* @param scope the dspace object parent
|
||||
*/
|
||||
public void performSearch(DSpaceObject scope) throws UIException, SearchServiceException
|
||||
{
|
||||
if (queryResults != null) {
|
||||
public void performSearch(DSpaceObject scope) throws UIException, SearchServiceException {
|
||||
|
||||
if (queryResults != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
String query = getQuery();
|
||||
|
||||
|
||||
//DSpaceObject scope = getScope();
|
||||
|
||||
int page = getParameterPage();
|
||||
|
||||
List<String> filterQueries = new ArrayList<String>();
|
||||
|
||||
String[] fqs = getFilterQueries();
|
||||
|
||||
if (fqs != null) {
|
||||
if (fqs != null)
|
||||
{
|
||||
filterQueries.addAll(Arrays.asList(fqs));
|
||||
}
|
||||
|
||||
|
||||
|
||||
this.queryArgs = new DiscoverQuery();
|
||||
|
||||
queryArgs.setMaxResults(getParameterRpp());
|
||||
|
||||
//Add the configured default filter queries
|
||||
DiscoveryConfiguration discoveryConfiguration = SearchUtils.getDiscoveryConfiguration(scope);
|
||||
@@ -747,34 +751,40 @@ public abstract class AbstractSearch extends AbstractDSpaceTransformer implement
|
||||
queryArgs.addFilterQueries(filterQueries.toArray(new String[filterQueries.size()]));
|
||||
}
|
||||
|
||||
|
||||
queryArgs.setMaxResults(getParameterRpp());
|
||||
|
||||
String sortBy = ObjectModelHelper.getRequest(objectModel).getParameter("sort_by");
|
||||
DiscoverySortConfiguration searchSortConfiguration = discoveryConfiguration.getSearchSortConfiguration();
|
||||
if(sortBy == null) {
|
||||
if(sortBy == null){
|
||||
//Attempt to find the default one, if none found we use SCORE
|
||||
sortBy = "score";
|
||||
if(searchSortConfiguration != null) {
|
||||
if(searchSortConfiguration != null){
|
||||
for (DiscoverySortFieldConfiguration sortFieldConfiguration : searchSortConfiguration.getSortFields()) {
|
||||
if(sortFieldConfiguration.equals(searchSortConfiguration.getDefaultSort())) {
|
||||
if(sortFieldConfiguration.equals(searchSortConfiguration.getDefaultSort())){
|
||||
sortBy = SearchUtils.getSearchService().toSortFieldIndex(sortFieldConfiguration.getMetadataField(), sortFieldConfiguration.getType());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String sortOrder = ObjectModelHelper.getRequest(objectModel).getParameter("order");
|
||||
if(sortOrder == null && searchSortConfiguration != null) {
|
||||
if(sortOrder == null && searchSortConfiguration != null){
|
||||
sortOrder = searchSortConfiguration.getDefaultSortOrder().toString();
|
||||
}
|
||||
|
||||
if (sortOrder == null || sortOrder.equalsIgnoreCase("DESC")) {
|
||||
if (sortOrder == null || sortOrder.equalsIgnoreCase("DESC"))
|
||||
{
|
||||
queryArgs.setSortField(sortBy, DiscoverQuery.SORT_ORDER.desc);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
queryArgs.setSortField(sortBy, DiscoverQuery.SORT_ORDER.asc);
|
||||
}
|
||||
|
||||
|
||||
String groupBy = ObjectModelHelper.getRequest(objectModel).getParameter("group_by");
|
||||
|
||||
|
||||
// Enable groupBy collapsing if designated
|
||||
if (groupBy != null && !groupBy.equalsIgnoreCase("none")) {
|
||||
/** Construct a Collapse Field Query */
|
||||
@@ -789,174 +799,37 @@ public abstract class AbstractSearch extends AbstractDSpaceTransformer implement
|
||||
// TODO: I think that can be more transparently done in the solr solrconfig.xml with DISMAX and boosting
|
||||
/** sort in groups to get publications to top */
|
||||
queryArgs.setSortField("dc.type", DiscoverQuery.SORT_ORDER.asc);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
queryArgs.setQuery(query != null && !query.trim().equals("") ? query : null);
|
||||
|
||||
if (page > 1) {
|
||||
if (page > 1)
|
||||
{
|
||||
queryArgs.setStart((page - 1) * queryArgs.getMaxResults());
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
queryArgs.setStart(0);
|
||||
}
|
||||
|
||||
if(discoveryConfiguration.getHitHighlightingConfiguration() != null) {
|
||||
if(discoveryConfiguration.getHitHighlightingConfiguration() != null)
|
||||
{
|
||||
List<DiscoveryHitHighlightFieldConfiguration> metadataFields = discoveryConfiguration.getHitHighlightingConfiguration().getMetadataFields();
|
||||
for (DiscoveryHitHighlightFieldConfiguration fieldConfiguration : metadataFields) {
|
||||
for (DiscoveryHitHighlightFieldConfiguration fieldConfiguration : metadataFields)
|
||||
{
|
||||
queryArgs.addHitHighlightingField(new DiscoverHitHighlightingField(fieldConfiguration.getField(), fieldConfiguration.getMaxSize(), fieldConfiguration.getSnippets()));
|
||||
}
|
||||
}
|
||||
|
||||
queryArgs.setSpellCheck(discoveryConfiguration.isSpellCheckEnabled());
|
||||
|
||||
|
||||
this.queryResults = SearchUtils.getSearchService().search(context, scope, queryArgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the search results as a csv file
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public DSpaceCSV exportMetadata(Context context, Request request, Map objectModel, String query, String scopeString, String filters) throws IOException, UIException, SearchServiceException, SQLException
|
||||
{
|
||||
DiscoverResult qResults = new DiscoverResult();
|
||||
DiscoverQuery qArgs = new DiscoverQuery();
|
||||
|
||||
try {
|
||||
scopeString = scopeString.replace("~", "/");
|
||||
}
|
||||
catch(NullPointerException e) { }
|
||||
|
||||
// Are we in a community or collection?
|
||||
DSpaceObject scope;
|
||||
if (scopeString == null || "".equals(scopeString)) {
|
||||
// get the search scope from the url handle
|
||||
scope = HandleUtil.obtainHandle(objectModel);
|
||||
}
|
||||
else {
|
||||
// Get the search scope from the location parameter
|
||||
scope = HandleManager.resolveToObject(context, scopeString);
|
||||
}
|
||||
|
||||
List<String> filterQueries = new ArrayList<String>();
|
||||
|
||||
String[] fqs = filters.split(",");
|
||||
|
||||
if (fqs != null) {
|
||||
filterQueries.addAll(Arrays.asList(fqs));
|
||||
}
|
||||
|
||||
qArgs.setMaxResults(getParameterRpp());
|
||||
|
||||
//Add the configured default filter queries
|
||||
DiscoveryConfiguration discoveryConfiguration = SearchUtils.getDiscoveryConfiguration(scope);
|
||||
List<String> defaultFilterQueries = discoveryConfiguration.getDefaultFilterQueries();
|
||||
qArgs.addFilterQueries(defaultFilterQueries.toArray(new String[defaultFilterQueries.size()]));
|
||||
|
||||
if (filterQueries.size() > 0) {
|
||||
qArgs.addFilterQueries(filterQueries.toArray(new String[filterQueries.size()]));
|
||||
}
|
||||
|
||||
String sortBy = ObjectModelHelper.getRequest(objectModel).getParameter("sort_by");
|
||||
DiscoverySortConfiguration searchSortConfiguration = discoveryConfiguration.getSearchSortConfiguration();
|
||||
if(sortBy == null) {
|
||||
//Attempt to find the default one, if none found we use SCORE
|
||||
sortBy = "score";
|
||||
if(searchSortConfiguration != null) {
|
||||
for (DiscoverySortFieldConfiguration sortFieldConfiguration : searchSortConfiguration.getSortFields()) {
|
||||
if(sortFieldConfiguration.equals(searchSortConfiguration.getDefaultSort())) {
|
||||
sortBy = SearchUtils.getSearchService().toSortFieldIndex(sortFieldConfiguration.getMetadataField(), sortFieldConfiguration.getType());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String sortOrder = ObjectModelHelper.getRequest(objectModel).getParameter("order");
|
||||
if(sortOrder == null && searchSortConfiguration != null) {
|
||||
sortOrder = searchSortConfiguration.getDefaultSortOrder().toString();
|
||||
}
|
||||
|
||||
if (sortOrder == null || sortOrder.equalsIgnoreCase("DESC")) {
|
||||
qArgs.setSortField(sortBy, DiscoverQuery.SORT_ORDER.desc);
|
||||
}
|
||||
else {
|
||||
qArgs.setSortField(sortBy, DiscoverQuery.SORT_ORDER.asc);
|
||||
}
|
||||
|
||||
String groupBy = ObjectModelHelper.getRequest(objectModel).getParameter("group_by");
|
||||
|
||||
// Enable groupBy collapsing if designated
|
||||
if (groupBy != null && !groupBy.equalsIgnoreCase("none")) {
|
||||
/** Construct a Collapse Field Query */
|
||||
qArgs.addProperty("collapse.field", groupBy);
|
||||
qArgs.addProperty("collapse.threshold", "1");
|
||||
qArgs.addProperty("collapse.includeCollapsedDocs.fl", "handle");
|
||||
qArgs.addProperty("collapse.facet", "before");
|
||||
|
||||
//queryArgs.a type:Article^2
|
||||
|
||||
// TODO: This is a hack to get Publications (Articles) to always be at the top of Groups.
|
||||
// TODO: I think that can be more transparently done in the solr solrconfig.xml with DISMAX and boosting
|
||||
/** sort in groups to get publications to top */
|
||||
qArgs.setSortField("dc.type", DiscoverQuery.SORT_ORDER.asc);
|
||||
}
|
||||
|
||||
qArgs.setQuery(query != null && !query.trim().equals("") ? query : null);
|
||||
|
||||
qArgs.setStart(0);
|
||||
|
||||
if(discoveryConfiguration.getHitHighlightingConfiguration() != null) {
|
||||
List<DiscoveryHitHighlightFieldConfiguration> metadataFields = discoveryConfiguration.getHitHighlightingConfiguration().getMetadataFields();
|
||||
for (DiscoveryHitHighlightFieldConfiguration fieldConfiguration : metadataFields) {
|
||||
qArgs.addHitHighlightingField(new DiscoverHitHighlightingField(fieldConfiguration.getField(), fieldConfiguration.getMaxSize(), fieldConfiguration.getSnippets()));
|
||||
}
|
||||
}
|
||||
|
||||
qArgs.setSpellCheck(discoveryConfiguration.isSpellCheckEnabled());
|
||||
|
||||
qResults = SearchUtils.getSearchService().search(context, scope, qArgs);
|
||||
|
||||
qArgs.setMaxResults(safeLongToInt(qResults.getTotalSearchResults()));
|
||||
|
||||
qResults = SearchUtils.getSearchService().search(context, scope, qArgs);
|
||||
|
||||
Item[] resultsItems;
|
||||
|
||||
// Get a list of found items
|
||||
ArrayList<Item> items = new ArrayList<Item>();
|
||||
for (DSpaceObject resultDSO : qResults.getDspaceObjects()) {
|
||||
if (resultDSO instanceof Item) {
|
||||
items.add((Item) resultDSO);
|
||||
}
|
||||
}
|
||||
resultsItems = new Item[items.size()];
|
||||
resultsItems = items.toArray(resultsItems);
|
||||
|
||||
// Log the attempt
|
||||
log.info(LogManager.getHeader(context, "metadataexport", "exporting_search"));
|
||||
|
||||
// Export a search view
|
||||
ArrayList iids = new ArrayList();
|
||||
for (Item item : items) {
|
||||
iids.add(item.getID());
|
||||
}
|
||||
ItemIterator ii = new ItemIterator(context, iids);
|
||||
MetadataExport exporter = new MetadataExport(context, ii, false);
|
||||
|
||||
// Perform the export
|
||||
DSpaceCSV csv = exporter.export();
|
||||
log.info(LogManager.getHeader(context, "metadataexport", "exported_file:search-results.csv"));
|
||||
|
||||
return csv;
|
||||
}
|
||||
|
||||
public static int safeLongToInt(long l) {
|
||||
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
|
||||
throw new IllegalArgumentException(l + " cannot be cast to int.");
|
||||
}
|
||||
return (int) l;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of the filter queries for use in rendering pages, creating page more urls, ....
|
||||
* @return an array containing the filter queries
|
||||
@@ -1069,7 +942,8 @@ public abstract class AbstractSearch extends AbstractDSpaceTransformer implement
|
||||
* @param parameters
|
||||
* @return The post URL
|
||||
*/
|
||||
protected abstract String generateURL(Map<String, String> parameters) throws UIException;
|
||||
protected abstract String generateURL(Map<String, String> parameters)
|
||||
throws UIException;
|
||||
|
||||
|
||||
/**
|
||||
@@ -1082,13 +956,17 @@ public abstract class AbstractSearch extends AbstractDSpaceTransformer implement
|
||||
super.recycle();
|
||||
}
|
||||
|
||||
protected void buildSearchControls(Division div) throws WingException, SQLException
|
||||
{
|
||||
|
||||
protected void buildSearchControls(Division div)
|
||||
throws WingException, SQLException {
|
||||
|
||||
|
||||
DSpaceObject dso = HandleUtil.obtainHandle(objectModel);
|
||||
DiscoveryConfiguration discoveryConfiguration = SearchUtils.getDiscoveryConfiguration(dso);
|
||||
|
||||
Division searchControlsGear = div.addDivision("masked-page-control").addDivision("search-controls-gear", "controls-gear-wrapper");
|
||||
|
||||
|
||||
/**
|
||||
* Add sort by options, the gear will be rendered by a combination of javascript & css
|
||||
*/
|
||||
@@ -1133,7 +1011,8 @@ public abstract class AbstractSearch extends AbstractDSpaceTransformer implement
|
||||
*/
|
||||
protected DSpaceObject getScope() throws SQLException {
|
||||
Request request = ObjectModelHelper.getRequest(objectModel);
|
||||
String scopeString = request.getParameter("scope");
|
||||
String scopeString = request.getParameter("scope");
|
||||
|
||||
// Are we in a community or collection?
|
||||
DSpaceObject dso;
|
||||
if (scopeString == null || "".equals(scopeString))
|
||||
@@ -1146,6 +1025,7 @@ public abstract class AbstractSearch extends AbstractDSpaceTransformer implement
|
||||
// Get the search scope from the location parameter
|
||||
dso = HandleManager.resolveToObject(context, scopeString);
|
||||
}
|
||||
|
||||
return dso;
|
||||
}
|
||||
|
||||
@@ -1192,4 +1072,5 @@ public abstract class AbstractSearch extends AbstractDSpaceTransformer implement
|
||||
+ (queryArgs == null ? "" : queryArgs.getQuery()) + "\",results=(" + countCommunities + ","
|
||||
+ countCollections + "," + countItems + ")"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -79,7 +79,7 @@ public class Navigation extends AbstractDSpaceTransformer implements CacheablePr
|
||||
*
|
||||
* language FIXME: add languages
|
||||
*
|
||||
* context no context options are added.
|
||||
* context - export metadata if in discover
|
||||
*
|
||||
* action no action options are added.
|
||||
*/
|
||||
@@ -89,6 +89,8 @@ public class Navigation extends AbstractDSpaceTransformer implements CacheablePr
|
||||
Context context = ContextUtil.obtainContext(objectModel);
|
||||
Request request = ObjectModelHelper.getRequest(objectModel);
|
||||
|
||||
// code remnants left behind, probably can be deleted
|
||||
|
||||
//List test = options.addList("browse");
|
||||
//List discovery = options.addList("discovery-search");
|
||||
//discovery.setHead("Discovery");
|
||||
@@ -124,18 +126,25 @@ public class Navigation extends AbstractDSpaceTransformer implements CacheablePr
|
||||
options.addList("account");
|
||||
options.addList("administrative");
|
||||
|
||||
String uri = request.getSitemapURI();
|
||||
|
||||
// get uri to see if using discovery and if under a specific handle
|
||||
String uri = request.getSitemapURI();
|
||||
|
||||
// check value in dspace.cfg
|
||||
String search_export_config = ConfigurationManager.getProperty("xmlui.search.metadata_export");
|
||||
|
||||
// get query
|
||||
String query = decodeFromURL(request.getParameter("query"));
|
||||
|
||||
// get scope, if not under handle returns null
|
||||
String scope= request.getParameter("scope");
|
||||
|
||||
// used to serialize all query filters together
|
||||
String filters = "";
|
||||
|
||||
// get all query filters
|
||||
String[] fqs = DiscoveryUIUtils.getFilterQueries(ObjectModelHelper.getRequest(objectModel), context);
|
||||
|
||||
if (fqs != null)
|
||||
|
||||
if (fqs != null)
|
||||
{
|
||||
for(int i = 0; i < fqs.length; i++) {
|
||||
if(i < fqs.length - 1)
|
||||
@@ -144,30 +153,31 @@ public class Navigation extends AbstractDSpaceTransformer implements CacheablePr
|
||||
filters += fqs[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(uri.contains("discover")) {
|
||||
// check scope
|
||||
if(scope == null || "".equals(scope))
|
||||
scope = "/";
|
||||
// check query
|
||||
if(query == null || "".equals(query))
|
||||
query = "*";
|
||||
// check if under a handle, already in discovery
|
||||
if(uri.contains("handle")) {
|
||||
scope = uri.replace("handle/", "").replace("/discover", "");
|
||||
}
|
||||
// replace forward slash to pass through sitemap
|
||||
try {
|
||||
scope = scope.replace("/", "~");
|
||||
}
|
||||
catch(NullPointerException e) { }
|
||||
if(search_export_config != null) {
|
||||
|
||||
if(false) {
|
||||
log.warn("**************************************");
|
||||
log.warn("uri: " + uri);
|
||||
log.warn("query: " + query);
|
||||
log.warn("scope: " + scope);
|
||||
log.warn("filters: " + filters);
|
||||
log.warn("**************************************");
|
||||
}
|
||||
|
||||
// some logging
|
||||
if(true) {
|
||||
log.info("uri: " + uri);
|
||||
log.info("query: " + query);
|
||||
log.info("scope: " + scope);
|
||||
log.info("filters: " + filters);
|
||||
}
|
||||
if(search_export_config.equals("admin")) {
|
||||
if(AuthorizeManager.isAdmin(context)) {
|
||||
List results = options.addList("context");
|
||||
|
@@ -17,6 +17,8 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import org.apache.avalon.excalibur.pool.Recyclable;
|
||||
import org.apache.avalon.framework.parameters.Parameters;
|
||||
import org.apache.cocoon.ProcessingException;
|
||||
@@ -29,17 +31,13 @@ import org.apache.cocoon.reading.AbstractReader;
|
||||
|
||||
import org.dspace.app.bulkedit.DSpaceCSV;
|
||||
import org.dspace.app.bulkedit.MetadataExport;
|
||||
|
||||
import org.dspace.app.xmlui.wing.WingException;
|
||||
import org.dspace.app.xmlui.wing.element.Body;
|
||||
import org.dspace.app.xmlui.utils.UIException;
|
||||
import org.dspace.app.xmlui.utils.AuthenticationUtil;
|
||||
import org.dspace.app.xmlui.utils.ContextUtil;
|
||||
|
||||
import org.dspace.app.xmlui.utils.HandleUtil;
|
||||
import org.dspace.app.xmlui.aspect.discovery.AbstractSearch;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.authorize.AuthorizeManager;
|
||||
import org.dspace.handle.HandleManager;
|
||||
@@ -47,15 +45,21 @@ import org.dspace.core.Context;
|
||||
import org.dspace.core.Constants;
|
||||
import org.dspace.core.LogManager;
|
||||
import org.dspace.core.ConfigurationManager;
|
||||
import org.dspace.content.*;
|
||||
import org.dspace.content.Collection;
|
||||
import org.dspace.content.Community;
|
||||
import org.dspace.content.DSpaceObject;
|
||||
import org.dspace.content.ItemIterator;
|
||||
import org.dspace.discovery.*;
|
||||
import org.dspace.discovery.configuration.DiscoveryConfiguration;
|
||||
import org.dspace.discovery.configuration.DiscoveryHitHighlightFieldConfiguration;
|
||||
import org.dspace.discovery.configuration.DiscoverySortConfiguration;
|
||||
import org.dspace.discovery.configuration.DiscoverySortFieldConfiguration;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* AbstractReader that generates a CSV of search
|
||||
* SearchMetadataExportReader that generates a CSV of search
|
||||
* result metadata using MetadataExport
|
||||
*
|
||||
*/
|
||||
@@ -113,8 +117,6 @@ public class SearchMetadataExportReader extends AbstractReader implements Recycl
|
||||
String query = par.getParameter("query");
|
||||
String scope = par.getParameter("scope");
|
||||
String filters = par.getParameter("filters");
|
||||
|
||||
ExportSearch exportSearch = new ExportSearch();
|
||||
|
||||
Context context = ContextUtil.obtainContext(objectModel);
|
||||
|
||||
@@ -122,7 +124,7 @@ public class SearchMetadataExportReader extends AbstractReader implements Recycl
|
||||
|
||||
if(search_export_config.equals("admin")) {
|
||||
if(AuthorizeManager.isAdmin(context)) {
|
||||
csv = exportSearch.exportMetadata(context, this.request, objectModel, query, scope, filters);
|
||||
csv = exportMetadata(context, objectModel, query, scope, filters);
|
||||
filename = "search-results.csv";
|
||||
}
|
||||
else {
|
||||
@@ -148,7 +150,7 @@ public class SearchMetadataExportReader extends AbstractReader implements Recycl
|
||||
}
|
||||
else if(search_export_config.equals("user")) {
|
||||
if(AuthenticationUtil.isLoggedIn(request)) {
|
||||
csv = exportSearch.exportMetadata(context, this.request, objectModel, query, scope, filters);
|
||||
csv = exportMetadata(context, objectModel, query, scope, filters);
|
||||
filename = "search-results.csv";
|
||||
}
|
||||
else {
|
||||
@@ -160,7 +162,7 @@ public class SearchMetadataExportReader extends AbstractReader implements Recycl
|
||||
}
|
||||
}
|
||||
else if(search_export_config.equals("anonymous")) {
|
||||
csv = exportSearch.exportMetadata(context, this.request, objectModel, query, scope, filters);
|
||||
csv = exportMetadata(context, objectModel, query, scope, filters);
|
||||
filename = "search-results.csv";
|
||||
}
|
||||
}
|
||||
@@ -193,14 +195,173 @@ public class SearchMetadataExportReader extends AbstractReader implements Recycl
|
||||
public void recycle() {
|
||||
this.response = null;
|
||||
this.request = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save and return the search results as a csv file
|
||||
*
|
||||
* @params context, objectModel, query, scopeString, filters
|
||||
*
|
||||
* @throws IOException, UIException, SearchServiceException, SQLException
|
||||
*/
|
||||
public DSpaceCSV exportMetadata(Context context, Map objectModel, String query, String scopeString, String filters) throws IOException, UIException, SearchServiceException, SQLException
|
||||
{
|
||||
DiscoverResult qResults = new DiscoverResult();
|
||||
DiscoverQuery qArgs = new DiscoverQuery();
|
||||
|
||||
try {
|
||||
scopeString = scopeString.replace("~", "/");
|
||||
}
|
||||
catch(NullPointerException e) { }
|
||||
|
||||
// Are we in a community or collection?
|
||||
DSpaceObject scope;
|
||||
if (scopeString == null || "".equals(scopeString)) {
|
||||
// get the search scope from the url handle
|
||||
scope = HandleUtil.obtainHandle(objectModel);
|
||||
}
|
||||
else {
|
||||
// Get the search scope from the location parameter
|
||||
scope = HandleManager.resolveToObject(context, scopeString);
|
||||
}
|
||||
|
||||
List<String> filterQueries = new ArrayList<String>();
|
||||
|
||||
class ExportSearch extends AbstractSearch
|
||||
{
|
||||
public ExportSearch() {}
|
||||
protected String getQuery() throws UIException { return null; }
|
||||
protected String getBasicUrl() throws SQLException { return null; }
|
||||
protected String generateURL(Map<String, String> parameters) throws UIException { return null; }
|
||||
public void addBody(Body body) throws SAXException, WingException, UIException, SQLException, IOException, AuthorizeException {}
|
||||
}
|
||||
String[] fqs = filters.split(",");
|
||||
|
||||
if (fqs != null) {
|
||||
filterQueries.addAll(Arrays.asList(fqs));
|
||||
}
|
||||
|
||||
// some arbitrary value for first search
|
||||
qArgs.setMaxResults(10);
|
||||
|
||||
//Add the configured default filter queries
|
||||
DiscoveryConfiguration discoveryConfiguration = SearchUtils.getDiscoveryConfiguration(scope);
|
||||
List<String> defaultFilterQueries = discoveryConfiguration.getDefaultFilterQueries();
|
||||
qArgs.addFilterQueries(defaultFilterQueries.toArray(new String[defaultFilterQueries.size()]));
|
||||
|
||||
if (filterQueries.size() > 0) {
|
||||
qArgs.addFilterQueries(filterQueries.toArray(new String[filterQueries.size()]));
|
||||
}
|
||||
|
||||
String sortBy = ObjectModelHelper.getRequest(objectModel).getParameter("sort_by");
|
||||
DiscoverySortConfiguration searchSortConfiguration = discoveryConfiguration.getSearchSortConfiguration();
|
||||
if(sortBy == null) {
|
||||
//Attempt to find the default one, if none found we use SCORE
|
||||
sortBy = "score";
|
||||
if(searchSortConfiguration != null) {
|
||||
for (DiscoverySortFieldConfiguration sortFieldConfiguration : searchSortConfiguration.getSortFields()) {
|
||||
if(sortFieldConfiguration.equals(searchSortConfiguration.getDefaultSort())) {
|
||||
sortBy = SearchUtils.getSearchService().toSortFieldIndex(sortFieldConfiguration.getMetadataField(), sortFieldConfiguration.getType());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String sortOrder = ObjectModelHelper.getRequest(objectModel).getParameter("order");
|
||||
if(sortOrder == null && searchSortConfiguration != null) {
|
||||
sortOrder = searchSortConfiguration.getDefaultSortOrder().toString();
|
||||
}
|
||||
|
||||
if (sortOrder == null || sortOrder.equalsIgnoreCase("DESC")) {
|
||||
qArgs.setSortField(sortBy, DiscoverQuery.SORT_ORDER.desc);
|
||||
}
|
||||
else {
|
||||
qArgs.setSortField(sortBy, DiscoverQuery.SORT_ORDER.asc);
|
||||
}
|
||||
|
||||
String groupBy = ObjectModelHelper.getRequest(objectModel).getParameter("group_by");
|
||||
|
||||
// Enable groupBy collapsing if designated
|
||||
if (groupBy != null && !groupBy.equalsIgnoreCase("none")) {
|
||||
/** Construct a Collapse Field Query */
|
||||
qArgs.addProperty("collapse.field", groupBy);
|
||||
qArgs.addProperty("collapse.threshold", "1");
|
||||
qArgs.addProperty("collapse.includeCollapsedDocs.fl", "handle");
|
||||
qArgs.addProperty("collapse.facet", "before");
|
||||
|
||||
//queryArgs.a type:Article^2
|
||||
|
||||
// TODO: This is a hack to get Publications (Articles) to always be at the top of Groups.
|
||||
// TODO: I think that can be more transparently done in the solr solrconfig.xml with DISMAX and boosting
|
||||
/** sort in groups to get publications to top */
|
||||
qArgs.setSortField("dc.type", DiscoverQuery.SORT_ORDER.asc);
|
||||
}
|
||||
|
||||
qArgs.setQuery(query != null && !query.trim().equals("") ? query : null);
|
||||
|
||||
// no paging required
|
||||
qArgs.setStart(0);
|
||||
|
||||
if(discoveryConfiguration.getHitHighlightingConfiguration() != null) {
|
||||
List<DiscoveryHitHighlightFieldConfiguration> metadataFields = discoveryConfiguration.getHitHighlightingConfiguration().getMetadataFields();
|
||||
for (DiscoveryHitHighlightFieldConfiguration fieldConfiguration : metadataFields) {
|
||||
qArgs.addHitHighlightingField(new DiscoverHitHighlightingField(fieldConfiguration.getField(), fieldConfiguration.getMaxSize(), fieldConfiguration.getSnippets()));
|
||||
}
|
||||
}
|
||||
|
||||
qArgs.setSpellCheck(discoveryConfiguration.isSpellCheckEnabled());
|
||||
|
||||
// search once to get total search results
|
||||
qResults = SearchUtils.getSearchService().search(context, scope, qArgs);
|
||||
|
||||
// set max results to total search results
|
||||
qArgs.setMaxResults(safeLongToInt(qResults.getTotalSearchResults()));
|
||||
|
||||
// search again to return all search results
|
||||
qResults = SearchUtils.getSearchService().search(context, scope, qArgs);
|
||||
|
||||
Item[] resultsItems;
|
||||
|
||||
// Get a list of found items
|
||||
ArrayList<Item> items = new ArrayList<Item>();
|
||||
for (DSpaceObject resultDSO : qResults.getDspaceObjects()) {
|
||||
if (resultDSO instanceof Item) {
|
||||
items.add((Item) resultDSO);
|
||||
}
|
||||
}
|
||||
resultsItems = new Item[items.size()];
|
||||
resultsItems = items.toArray(resultsItems);
|
||||
|
||||
// Log the attempt
|
||||
log.info(LogManager.getHeader(context, "metadataexport", "exporting_search"));
|
||||
|
||||
// Export a search view
|
||||
ArrayList iids = new ArrayList();
|
||||
for (Item item : items) {
|
||||
iids.add(item.getID());
|
||||
}
|
||||
ItemIterator ii = new ItemIterator(context, iids);
|
||||
MetadataExport exporter = new MetadataExport(context, ii, false);
|
||||
|
||||
// Perform the export
|
||||
DSpaceCSV csv = exporter.export();
|
||||
log.info(LogManager.getHeader(context, "metadataexport", "exported_file:search-results.csv"));
|
||||
|
||||
return csv;
|
||||
}
|
||||
|
||||
public static int safeLongToInt(long l) {
|
||||
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
|
||||
throw new IllegalArgumentException(l + " cannot be cast to int.");
|
||||
}
|
||||
return (int) l;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of the filter queries for use in rendering pages, creating page more urls, ....
|
||||
* @return an array containing the filter queries
|
||||
*/
|
||||
protected Map<String, String[]> getParameterFilterQueries()
|
||||
{
|
||||
try {
|
||||
Map<String, String[]> result = new HashMap<String, String[]>();
|
||||
result.put("fq", ObjectModelHelper.getRequest(objectModel).getParameterValues("fq"));
|
||||
return result;
|
||||
}
|
||||
catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -2064,7 +2064,7 @@ mirage2.item-view.bitstream.href.label.2 = title
|
||||
# records the original IP address.
|
||||
#xmlui.controlpanel.activity.ipheader = X-Forward-For
|
||||
|
||||
xmlui.search.metadata_export = anonymous
|
||||
xmlui.search.metadata_export = admin
|
||||
|
||||
#---------------------------------------------------------------#
|
||||
#----------------REQUEST ITEM CONFIGURATION---------------------#
|
||||
|
Reference in New Issue
Block a user