Skip to content

Commit

Permalink
Merge pull request #80 from sedici/DS-1244
Browse files Browse the repository at this point in the history
[DS-1244] XMLUI's OpenSearch implementation refactored to work with Discovery

Squashed commit of the following:

commit 1268c9af08018afb46d213910d8a96c79d20715c
Author: Andrea Bollini <bollini@cilea.it>
Date:   Fri Sep 14 16:11:02 2012 +0200

    Fix JSPUI side to work with new classes

    also included some bug fix about I18N access in Open Search for JSPUI

commit 885012fcf32943dc1ce2ac7f35bb3810f70363aa
Merge: 12b4d6b d8b1b4c
Author: Andrea Bollini <bollini@cilea.it>
Date:   Fri Sep 14 15:28:09 2012 +0200

    Merge remote-tracking branch 'sedici/open-search-discovery' into DS-1244

commit d8b1b4c
Merge: 53ad4e3 58fe789
Author: Nestor Oviedo <nestor@pc-nestor.(none)>
Date:   Thu Sep 13 13:50:03 2012 -0300

    Merge remote-tracking branch 'origin/open-search-discovery' into open-search-discovery

    Conflicts:
    	dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/OpenSearchServlet.java

    Rebase with 7bc92a8
    Conflict on dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/OpenSearchServlet.java resolved: new version preserved

commit 53ad4e3
Author: nestor <nestor@pc-nestor>
Date:   Thu Sep 13 10:27:32 2012 -0300

    Added license header to DiscoveryOpenSearchGenerator

commit bdb22e0
Author: nestor <nestor@pc-nestor>
Date:   Thu Sep 6 13:36:01 2012 -0300

    DS-1244 - XMLUI's OpenSearch implementation refactored.
    It's in order to use either PostgreSQL's indexes or Discovery services
    based on the requested URL

commit 58fe789
Author: nestor <nestor@pc-nestor>
Date:   Thu Sep 13 10:27:32 2012 -0300

    Added license header to DiscoveryOpenSearchGenerator

commit efbf7d2
Author: nestor <nestor@pc-nestor>
Date:   Thu Sep 6 13:36:01 2012 -0300

    DS-1244 - XMLUI's OpenSearch implementation refactored.
    It's in order to use either PostgreSQL's indexes or Discovery services
    based on the requested URL
  • Loading branch information
nesovi authored and Andrea Bollini committed Sep 14, 2012
1 parent 12b4d6b commit 97580ee
Show file tree
Hide file tree
Showing 10 changed files with 779 additions and 477 deletions.
81 changes: 50 additions & 31 deletions dspace-api/src/main/java/org/dspace/app/util/OpenSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
*/
package org.dspace.app.util;

import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.handle.HandleManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;

import org.w3c.dom.Document;

Expand All @@ -26,7 +31,6 @@

import org.dspace.content.DSpaceObject;
import org.dspace.core.ConfigurationManager;
import org.dspace.search.QueryResults;

import com.sun.syndication.feed.module.opensearch.OpenSearchModule;
import com.sun.syndication.feed.module.opensearch.entity.OSQuery;
Expand Down Expand Up @@ -139,57 +143,60 @@ public static String getDescription(String scope)
*
* @param format results format - html, rss or atom
* @param query - the search query
* @param qResults - the query results to be formatted
* @param totalResults - the hit count
* @param start - start result index
* @param pageSize - page size
* @param scope - search scope, null or community/collection handle
* @param results the retreived DSpace objects satisfying search
* @param labels labels to apply - format specific
* @return formatted search results
* @throws IOException
*/
public static String getResultsString(String format, String query, QueryResults qResults,
public static String getResultsString(String format, String query, int totalResults, int start, int pageSize,
DSpaceObject scope, DSpaceObject[] results,
Map<String, String> labels) throws IOException
{
try
{
return getResults(format, query, qResults, scope, results, labels).outputString();
}
try
{
return getResults(format, query, totalResults, start, pageSize, scope, results, labels).outputString();
}
catch (FeedException e)
{
{
log.error(e.toString(), e);
throw new IOException("Unable to generate feed", e);
}
}
}

/**
* Returns a formatted set of search results as a document
*
* @param format results format - html, rss or atom
* @param query - the search query
* @param qResults - the query results to be formatted
* @param totalResults - the hit count
* @param start - start result index
* @param pageSize - page size
* @param scope - search scope, null or community/collection handle
* @param results the retreived DSpace objects satisfying search
* @param labels labels to apply - format specific
* @return formatted search results
* @throws IOException
*/
public static Document getResultsDoc(String format, String query, QueryResults qResults,
public static Document getResultsDoc(String format, String query, int totalResults, int start, int pageSize,
DSpaceObject scope, DSpaceObject[] results, Map<String, String> labels)
throws IOException
{
try
{
return getResults(format, query, qResults, scope, results, labels).outputW3CDom();
}
try
{
return getResults(format, query, totalResults, start, pageSize, scope, results, labels).outputW3CDom();
}
catch (FeedException e)
{
{
log.error(e.toString(), e);
throw new IOException("Unable to generate feed", e);
}

}

private static SyndicationFeed getResults(String format, String query, QueryResults qResults,
}

private static SyndicationFeed getResults(String format, String query, int totalResults, int start, int pageSize,
DSpaceObject scope, DSpaceObject[] results, Map<String, String> labels)
{
// Encode results in requested format
Expand All @@ -205,7 +212,7 @@ else if ("atom".equals(format))
SyndicationFeed feed = new SyndicationFeed(labels.get(SyndicationFeed.MSG_UITYPE));
feed.populate(null, scope, results, labels);
feed.setType(format);
feed.addModule(openSearchMarkup(query, qResults));
feed.addModule(openSearchMarkup(query, totalResults, start, pageSize));
return feed;
}

Expand All @@ -217,26 +224,26 @@ else if ("atom".equals(format))
* @param qRes the search results
* @return module
*/
private static OpenSearchModule openSearchMarkup(String query, QueryResults qRes)
{
private static OpenSearchModule openSearchMarkup(String query, int totalResults, int start, int pageSize)
{
OpenSearchModule osMod = new OpenSearchModuleImpl();
osMod.setTotalResults(qRes.getHitCount());
osMod.setStartIndex(qRes.getStart());
osMod.setItemsPerPage(qRes.getPageSize());
osMod.setTotalResults(totalResults);
osMod.setStartIndex(start);
osMod.setItemsPerPage(pageSize);
OSQuery osq = new OSQuery();
osq.setRole("request");
try
{
osq.setSearchTerms(URLEncoder.encode(query, "UTF-8"));
}
}
catch(UnsupportedEncodingException e)
{
{
log.error(e);
}
osq.setStartPage(1 + (qRes.getStart() / qRes.getPageSize()));
}
osq.setStartPage(1 + (start / pageSize));
osMod.addQuery(osq);
return osMod;
}
}

/**
* Returns as a document the OpenSearch service document
Expand Down Expand Up @@ -327,4 +334,16 @@ private static Document jDomToW3(org.jdom.Document jdomDoc) throws IOException
throw new IOException("JDOM output exception", jde);
}
}

public static DSpaceObject resolveScope(Context context, String scope) throws SQLException
{
if(scope == null || "".equals(scope))
return null;

DSpaceObject dso = HandleManager.resolveToObject(context, scope);
if(dso == null || dso.getType() == Constants.ITEM)
throw new IllegalArgumentException("Scope handle "+scope+" should point to a valid Community or Collection");
return dso;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -172,86 +172,8 @@ else if ("".equals(autoQuery))
return queryArgs;
}

// /**
// * Build the query from the advanced search form
// *
// * @param request
// * @return
// */
// public static String buildQuery(HttpServletRequest request)
// {
// int num_field = UIUtil.getIntParameter(request, "num_search_field");
// if (num_field <= 0)
// {
// num_field = 3;
// }
// StringBuffer query = new StringBuffer();
// buildQueryPart(query, request.getParameter("field"),
// request.getParameter("query"), null);
// for (int i = 1; i < num_field; i++)
// {
// buildQueryPart(query, request.getParameter("field" + i),
// request.getParameter("query" + i),
// request.getParameter("conjuction" + i));
// }
// return query.toString();
// }
//
// private static void buildQueryPart(StringBuffer currQuery, String field,
// String queryPart, String conjuction)
// {
// if (StringUtils.isBlank(queryPart))
// {
// return;
// }
// else
// {
// StringBuffer tmp = new StringBuffer(queryPart);
// if (StringUtils.isNotBlank(field))
// {
// tmp.insert(0, field + ":(").append(")");
// }
//
// if (StringUtils.isNotBlank(conjuction) && currQuery.length() > 0)
// {
// currQuery.append(conjuction);
// }
// currQuery.append(tmp);
// }
// }

/**
* Return a QueryResults object as needed by the OpenSearch classes
*
* @param qResults
* @return
*/
public static QueryResults toQueryResults(DiscoverResult qResults)
{
QueryResults qR = new QueryResults();
qR.setHitCount((int) qResults.getTotalSearchResults());
qR.setPageSize(qResults.getMaxResults());
qR.setStart(qResults.getStart());

List<String> handles = new ArrayList<String>();
List<Integer> types = new ArrayList<Integer>();
List<Integer> ids = new ArrayList<Integer>();

for (DSpaceObject dso : qResults.getDspaceObjects())
{
handles.add(dso.getHandle());
ids.add(dso.getID());
types.add(dso.getType());
}

qR.setHitIds(ids);
qR.setHitTypes(types);
qR.setHitHandles(handles);
return qR;
}

/**
* Setup the basic query argumnets: the main query and all the filters
* Setup the basic query arguments: the main query and all the filters
* (default + user). Return the list of user filter
*
* @param context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dspace.app.bulkedit.DSpaceCSV;
import org.dspace.app.bulkedit.MetadataExport;
Expand All @@ -37,8 +36,8 @@
import org.dspace.content.ItemIterator;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context;
import org.dspace.core.I18nUtil;
import org.dspace.core.LogManager;
import org.dspace.discovery.DiscoverFilterQuery;
import org.dspace.discovery.DiscoverQuery;
import org.dspace.discovery.DiscoverResult;
import org.dspace.discovery.SearchServiceException;
Expand All @@ -47,8 +46,6 @@
import org.dspace.discovery.configuration.DiscoverySearchFilter;
import org.dspace.discovery.configuration.DiscoverySearchFilterFacet;
import org.dspace.discovery.configuration.DiscoverySortFieldConfiguration;
import org.dspace.discovery.configuration.SidebarFacetConfiguration;
import org.dspace.search.QueryResults;
import org.w3c.dom.Document;

public class DiscoverySearchRequestProcessor implements SearchRequestProcessor
Expand All @@ -61,15 +58,20 @@ public class DiscoverySearchRequestProcessor implements SearchRequestProcessor
// locale-sensitive metadata labels
private Map<String, Map<String, String>> localeLabels = null;

public void init()
public synchronized void init()
{
localeLabels = new HashMap<String, Map<String, String>>();
if (localeLabels == null)
{
localeLabels = new HashMap<String, Map<String, String>>();
}
}

public void doOpenSearch(Context context, HttpServletRequest request,
HttpServletResponse response) throws SearchProcessorException,
IOException, ServletException
{
init();

// dispense with simple service document requests
String scope = request.getParameter("scope");
if (scope != null && "".equals(scope))
Expand Down Expand Up @@ -143,9 +145,9 @@ public void doOpenSearch(Context context, HttpServletRequest request,
DSpaceObject[] dsoResults = new DSpaceObject[qResults
.getDspaceObjects().size()];
qResults.getDspaceObjects().toArray(dsoResults);
QueryResults qR = DiscoverUtility.toQueryResults(qResults);
Document resultsDoc = OpenSearch.getResultsDoc(format, query, qR,
container, dsoResults, labelMap);
Document resultsDoc = OpenSearch.getResultsDoc(format, query,
(int)qResults.getTotalSearchResults(), qResults.getStart(),
qResults.getMaxResults(), container, dsoResults, labelMap);
try
{
Transformer xf = TransformerFactory.newInstance().newTransformer();
Expand All @@ -163,7 +165,7 @@ public void doOpenSearch(Context context, HttpServletRequest request,
private Map<String, String> getLabels(HttpServletRequest request)
{
// Get access to the localized resource bundle
Locale locale = request.getLocale();
Locale locale = UIUtil.getSessionLocale(request);
Map<String, String> labelMap = localeLabels.get(locale.toString());
if (labelMap == null)
{
Expand All @@ -176,19 +178,13 @@ private Map<String, String> getLabels(HttpServletRequest request)
private Map<String, String> getLocaleLabels(Locale locale)
{
Map<String, String> labelMap = new HashMap<String, String>();
ResourceBundle labels = ResourceBundle.getBundle("Messages", locale);

labelMap.put(SyndicationFeed.MSG_UNTITLED,
labels.getString(msgKey + ".notitle"));
labelMap.put(SyndicationFeed.MSG_LOGO_TITLE,
labels.getString(msgKey + ".logo.title"));
labelMap.put(SyndicationFeed.MSG_FEED_DESCRIPTION,
labels.getString(msgKey + ".general-feed.description"));
labelMap.put(SyndicationFeed.MSG_UNTITLED, I18nUtil.getMessage(msgKey + ".notitle", locale));
labelMap.put(SyndicationFeed.MSG_LOGO_TITLE, I18nUtil.getMessage(msgKey + ".logo.title", locale));
labelMap.put(SyndicationFeed.MSG_FEED_DESCRIPTION, I18nUtil.getMessage(msgKey + ".general-feed.description", locale));
labelMap.put(SyndicationFeed.MSG_UITYPE, SyndicationFeed.UITYPE_JSPUI);
for (String selector : SyndicationFeed.getDescriptionSelectors())
{
labelMap.put("metadata." + selector,
labels.getString(SyndicationFeed.MSG_METADATA + selector));
labelMap.put("metadata." + selector, I18nUtil.getMessage(SyndicationFeed.MSG_METADATA + selector, locale));
}
return labelMap;
}
Expand Down

0 comments on commit 97580ee

Please sign in to comment.