org.dspace.discovery.indexobject.CommunityIndexFactoryImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dspace-api Show documentation
Show all versions of dspace-api Show documentation
DSpace core data model and service APIs.
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.discovery.indexobject;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.apache.solr.common.SolrInputDocument;
import org.dspace.content.Community;
import org.dspace.content.Item;
import org.dspace.content.service.CommunityService;
import org.dspace.core.Context;
import org.dspace.discovery.SearchUtils;
import org.dspace.discovery.configuration.DiscoveryConfiguration;
import org.dspace.discovery.configuration.DiscoveryHitHighlightFieldConfiguration;
import org.dspace.discovery.configuration.DiscoveryHitHighlightingConfiguration;
import org.dspace.discovery.indexobject.factory.CommunityIndexFactory;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Factory implementation for indexing/retrieving communities in the search core
* @author Kevin Van de Velde (kevin at atmire dot com)
*/
public class CommunityIndexFactoryImpl extends DSpaceObjectIndexFactoryImpl
implements CommunityIndexFactory {
@Autowired(required = true)
protected CommunityService communityService;
@Override
public Iterator findAll(Context context) throws SQLException {
Iterator communities = communityService.findAll(context).iterator();
return new Iterator() {
@Override
public boolean hasNext() {
return communities.hasNext();
}
@Override
public IndexableCommunity next() {
return new IndexableCommunity(communities.next());
}
};
}
@Override
public String getType() {
return IndexableCommunity.TYPE;
}
@Override
public SolrInputDocument buildDocument(Context context, IndexableCommunity indexableObject)
throws SQLException, IOException {
// Add the ID's, types and call the SolrServiceIndexPlugins
SolrInputDocument doc = super.buildDocument(context, indexableObject);
final Community community = indexableObject.getIndexedObject();
// Retrieve configuration
DiscoveryConfiguration discoveryConfiguration = SearchUtils.getDiscoveryConfiguration(community);
DiscoveryHitHighlightingConfiguration highlightingConfiguration = discoveryConfiguration
.getHitHighlightingConfiguration();
List highlightedMetadataFields = new ArrayList<>();
if (highlightingConfiguration != null) {
for (DiscoveryHitHighlightFieldConfiguration configuration : highlightingConfiguration
.getMetadataFields()) {
highlightedMetadataFields.add(configuration.getField());
}
}
// Add community metadata
String description = communityService.getMetadataFirstValue(community,
CommunityService.MD_INTRODUCTORY_TEXT, Item.ANY);
String description_abstract = communityService.getMetadataFirstValue(community,
CommunityService.MD_SHORT_DESCRIPTION, Item.ANY);
String description_table = communityService.getMetadataFirstValue(community,
CommunityService.MD_SIDEBAR_TEXT, Item.ANY);
String rights = communityService.getMetadataFirstValue(community,
CommunityService.MD_COPYRIGHT_TEXT, Item.ANY);
String title = communityService.getMetadataFirstValue(community,
CommunityService.MD_NAME, Item.ANY);
List toIgnoreMetadataFields = SearchUtils.getIgnoredMetadataFields(community.getType());
addContainerMetadataField(doc, highlightedMetadataFields, toIgnoreMetadataFields, "dc.description",
description);
addContainerMetadataField(doc, highlightedMetadataFields, toIgnoreMetadataFields, "dc.description.abstract",
description_abstract);
addContainerMetadataField(doc, highlightedMetadataFields, toIgnoreMetadataFields,
"dc.description.tableofcontents", description_table);
addContainerMetadataField(doc, highlightedMetadataFields, toIgnoreMetadataFields, "dc.rights", rights);
addContainerMetadataField(doc, highlightedMetadataFields, toIgnoreMetadataFields, "dc.title", title);
doc.addField("dc.title_sort", title);
return doc;
}
@Override
public boolean supports(Object object) {
return object instanceof Community;
}
@Override
public List getIndexableObjects(Context context, Community object) {
return Arrays.asList(new IndexableCommunity(object));
}
@Override
public Optional findIndexableObject(Context context, String id) throws SQLException {
final Community community = communityService.find(context, UUID.fromString(id));
return community == null ? Optional.empty() : Optional.of(new IndexableCommunity(community));
}
@Override
public List getLocations(Context context, IndexableCommunity indexableDSpaceObject) throws SQLException {
final Community target = indexableDSpaceObject.getIndexedObject();
List locations = new ArrayList<>();
// build list of community ids
List communities = target.getParentCommunities();
// now put those into strings
for (Community community : communities) {
locations.add("m" + community.getID());
}
return locations;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy