use of org.opencastproject.util.data.Function0 in project opencast by opencast.
the class SolrRequester method createSearchResult.
/**
* Creates a search result from a given solr response.
*
* @param query
* The solr query.
* @return The search result.
* @throws SolrServerException
* if the solr server is not working as expected
*/
private SearchResult createSearchResult(final SolrQuery query) throws SolrServerException {
// Execute the query and try to get hold of a query response
QueryResponse solrResponse = null;
try {
solrResponse = solrServer.query(query);
} catch (Exception e) {
throw new SolrServerException(e);
}
// Create and configure the query result
final SearchResultImpl result = new SearchResultImpl(query.getQuery());
result.setSearchTime(solrResponse.getQTime());
result.setOffset(solrResponse.getResults().getStart());
result.setLimit(solrResponse.getResults().size());
result.setTotal(solrResponse.getResults().getNumFound());
// Walk through response and create new items with title, creator, etc:
for (final SolrDocument doc : solrResponse.getResults()) {
final SearchResultItemImpl item = SearchResultItemImpl.fill(new SearchResultItem() {
private final String dfltString = null;
@Override
public String getId() {
return Schema.getId(doc);
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.search.api.SearchResultItem#getOrganization()
*/
@Override
public String getOrganization() {
return Schema.getOrganization(doc);
}
@Override
public MediaPackage getMediaPackage() {
MediaPackageBuilder builder = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder();
if (serializer != null)
builder.setSerializer(serializer);
String mediaPackageFieldValue = Schema.getOcMediapackage(doc);
if (mediaPackageFieldValue != null) {
try {
return builder.loadFromXml(mediaPackageFieldValue);
} catch (Exception e) {
logger.warn("Unable to read media package from search result", e);
}
}
return null;
}
@Override
public long getDcExtent() {
if (getType().equals(SearchResultItemType.AudioVisual)) {
Long extent = Schema.getDcExtent(doc);
if (extent != null)
return extent;
}
return -1;
}
@Override
public String getDcTitle() {
final List<DField<String>> titles = Schema.getDcTitle(doc);
// try to return the first title without any language information first...
return head(filter(titles, new Predicate<DField<String>>() {
@Override
public Boolean apply(DField<String> f) {
return f.getSuffix().equals(Schema.LANGUAGE_UNDEFINED);
}
})).map(new Function<DField<String>, String>() {
@Override
public String apply(DField<String> f) {
return f.getValue();
}
}).getOrElse(new Function0<String>() {
@Override
public String apply() {
// ... since none is present return the first arbitrary title
return Schema.getFirst(titles, dfltString);
}
});
}
@Override
public String getDcSubject() {
return Schema.getFirst(Schema.getDcSubject(doc), dfltString);
}
@Override
public String getDcDescription() {
return Schema.getFirst(Schema.getDcDescription(doc), dfltString);
}
@Override
public String getDcCreator() {
return Schema.getFirst(Schema.getDcCreator(doc), dfltString);
}
@Override
public String getDcPublisher() {
return Schema.getFirst(Schema.getDcPublisher(doc), dfltString);
}
@Override
public String getDcContributor() {
return Schema.getFirst(Schema.getDcContributor(doc), dfltString);
}
@Override
public String getDcAbstract() {
return null;
}
@Override
public Date getDcCreated() {
return Schema.getDcCreated(doc);
}
@Override
public Date getDcAvailableFrom() {
return Schema.getDcAvailableFrom(doc);
}
@Override
public Date getDcAvailableTo() {
return Schema.getDcAvailableTo(doc);
}
@Override
public String getDcLanguage() {
return Schema.getDcLanguage(doc);
}
@Override
public String getDcRightsHolder() {
return Schema.getFirst(Schema.getDcRightsHolder(doc), dfltString);
}
@Override
public String getDcSpatial() {
return Schema.getFirst(Schema.getDcSpatial(doc), dfltString);
}
@Override
public String getDcTemporal() {
return null;
}
@Override
public String getDcIsPartOf() {
return Schema.getDcIsPartOf(doc);
}
@Override
public String getDcReplaces() {
return Schema.getDcReplaces(doc);
}
@Override
public String getDcType() {
return Schema.getDcType(doc);
}
@Override
public String getDcAccessRights() {
return Schema.getFirst(Schema.getDcAccessRights(doc), dfltString);
}
@Override
public String getDcLicense() {
return Schema.getFirst(Schema.getDcLicense(doc), dfltString);
}
@Override
public String getOcMediapackage() {
return Schema.getOcMediapackage(doc);
}
@Override
public SearchResultItemType getType() {
String t = Schema.getOcMediatype(doc);
return t != null ? SearchResultItemType.valueOf(t) : null;
}
@Override
public String[] getKeywords() {
if (getType().equals(SearchResultItemType.AudioVisual)) {
String k = Schema.getOcKeywords(doc);
return k != null ? k.split(" ") : new String[0];
} else
return new String[0];
}
@Override
public String getCover() {
return Schema.getOcCover(doc);
}
@Override
public Date getModified() {
return Schema.getOcModified(doc);
}
@Override
public double getScore() {
return Schema.getScore(doc);
}
@Override
public MediaSegment[] getSegments() {
if (SearchResultItemType.AudioVisual.equals(getType()))
return createSearchResultSegments(doc, query).toArray(new MediaSegmentImpl[0]);
else
return new MediaSegmentImpl[0];
}
});
// Add the item to the result set
result.addItem(item);
}
return result;
}
use of org.opencastproject.util.data.Function0 in project opencast by opencast.
the class SeriesServiceImpl method repopulate.
@Override
public void repopulate(final String indexName) {
final String destinationId = SeriesItem.SERIES_QUEUE_PREFIX + indexName.substring(0, 1).toUpperCase() + indexName.substring(1);
try {
final int total = persistence.countSeries();
logger.info("Re-populating '{}' index with series. There are {} series to add to the index.", indexName, total);
final int responseInterval = (total < 100) ? 1 : (total / 100);
List<SeriesEntity> databaseSeries = persistence.getAllSeries();
int current = 1;
for (SeriesEntity series : databaseSeries) {
Organization organization = orgDirectory.getOrganization(series.getOrganization());
SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(systemUserName, organization), new Function0.X<Void>() {
@Override
public Void xapply() throws Exception {
String id = series.getSeriesId();
logger.trace("Adding series '{}' for org '{}'", id, series.getOrganization());
DublinCoreCatalog catalog = DublinCoreXmlFormat.read(series.getDublinCoreXML());
messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, SeriesItem.updateCatalog(catalog));
AccessControlList acl = AccessControlParser.parseAcl(series.getAccessControl());
if (acl != null) {
messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, SeriesItem.updateAcl(id, acl));
}
messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, SeriesItem.updateOptOut(id, series.isOptOut()));
for (Entry<String, String> property : persistence.getSeriesProperties(id).entrySet()) {
messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, SeriesItem.updateProperty(id, property.getKey(), property.getValue()));
}
return null;
}
});
if ((current % responseInterval == 0) || (current == total)) {
logger.info("Initializing {} series index rebuild {}/{}: {} percent", indexName, current, total, current * 100 / total);
}
current++;
}
logger.info("Finished initializing '{}' index rebuild", indexName);
} catch (Exception e) {
logger.warn("Unable to index series instances:", e);
throw new ServiceException(e.getMessage());
}
Organization organization = new DefaultOrganization();
SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(systemUserName, organization), new Effect0() {
@Override
protected void run() {
messageSender.sendObjectMessage(IndexProducer.RESPONSE_QUEUE, MessageSender.DestinationType.Queue, IndexRecreateObject.end(indexName, IndexRecreateObject.Service.Series));
}
});
}
use of org.opencastproject.util.data.Function0 in project opencast by opencast.
the class OaiPmhHarvester method createSecurityConfigurator.
/**
* Return a function that configures the security service with a {@link User} and {@link Organization}.
*/
private static Function0<Void> createSecurityConfigurator(Dictionary properties, ComponentContext cc) throws ConfigurationException {
// get services
final OrganizationDirectoryService organizationDirectoryService = (OrganizationDirectoryService) cc.locateService(REF_ORG_SERVICE);
final SecurityService securityService = (SecurityService) cc.locateService(REF_SECURITY_SERVICE);
final UserDirectoryService userDirectoryService = (UserDirectoryService) cc.locateService(REF_USER_SERVICE);
// get the organization
String organizationName = getCfg(properties, CFG_USER_ORGANIZATION);
final Organization organization;
try {
organization = organizationDirectoryService.getOrganization(organizationName);
} catch (NotFoundException e) {
throw new ConfigurationException(CFG_USER_ORGANIZATION, "Organization '" + organizationName + "' does not exist");
}
// get the user
final User user;
final Organization originalOrg = securityService.getOrganization();
try {
String userName = getCfg(properties, CFG_USER_NAME);
securityService.setOrganization(organization);
user = userDirectoryService.loadUser(userName);
} finally {
securityService.setOrganization(originalOrg);
}
return new Function0<Void>() {
@Override
public Void apply() {
securityService.setOrganization(organization);
securityService.setUser(user);
return null;
}
};
}
Aggregations