-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
088fed7
commit 778947d
Showing
8 changed files
with
1,302 additions
and
1,135 deletions.
There are no files selected for viewing
491 changes: 0 additions & 491 deletions
491
shared/common/src/main/java/cz/incad/kramerius/fedora/impl/AbstractRepositoryAccess.java
This file was deleted.
Oops, something went wrong.
1,317 changes: 673 additions & 644 deletions
1,317
shared/common/src/main/java/cz/incad/kramerius/fedora/impl/RepositoryAccessImpl.java
Large diffs are not rendered by default.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
shared/common/src/main/java/cz/incad/kramerius/fedora/impl/tmp/ResultMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package cz.incad.kramerius.fedora.impl.tmp; | ||
|
||
import org.apache.solr.common.SolrDocument; | ||
|
||
import java.util.List; | ||
|
||
@FunctionalInterface | ||
public interface ResultMapper<T> { | ||
|
||
T map(List<SolrDocument> documents, long totalRecords); | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
shared/common/src/main/java/cz/incad/kramerius/fedora/impl/tmp/SolrQueryParameters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cz.incad.kramerius.fedora.impl.tmp; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class SolrQueryParameters { | ||
private final Map<String, String> filters; | ||
private final String sortField; | ||
private final boolean ascending; | ||
private final int rows; | ||
private final int pageIndex; | ||
private final List<String> fieldsToFetch; | ||
|
||
// Constructor, getters, and builder pattern for flexibility | ||
} |
11 changes: 11 additions & 0 deletions
11
shared/common/src/main/java/cz/incad/kramerius/fedora/impl/tmp/SolrQueryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cz.incad.kramerius.fedora.impl.tmp; | ||
|
||
import cz.incad.kramerius.fedora.om.repository.RepositoryException; | ||
|
||
import java.io.IOException; | ||
|
||
public interface SolrQueryService { | ||
|
||
<T> T query(SolrQueryParameters params, ResultMapper<T> mapper) throws RepositoryException, IOException, SolrServerException; | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
shared/common/src/main/java/cz/incad/kramerius/fedora/impl/tmp/SolrQueryServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cz.incad.kramerius.fedora.impl.tmp; | ||
|
||
import cz.incad.kramerius.fedora.om.repository.RepositoryException; | ||
import cz.incad.kramerius.utils.java.Pair; | ||
import org.apache.solr.client.solrj.SolrServerException; | ||
import org.apache.solr.common.SolrDocument; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class SolrQueryServiceImpl implements SolrQueryService { | ||
|
||
@Override | ||
public <T> T query(SolrQueryParameters params, ResultMapper<T> mapper) | ||
throws RepositoryException, IOException, SolrServerException { | ||
|
||
// Build query string from parameters | ||
StringBuilder queryBuilder = new StringBuilder(); | ||
params.getFilters().forEach((field, value) -> | ||
queryBuilder.append(field).append(":").append(value).append(" AND ") | ||
); | ||
if (queryBuilder.length() > 0) { | ||
queryBuilder.setLength(queryBuilder.length() - 5); // Remove trailing " AND " | ||
} | ||
|
||
// Fetch results from SOLR | ||
org.apache.commons.lang3.tuple.Pair<Long, List<SolrDocument>> cp = | ||
akubraRepositoryImpl.getProcessingIndexFeeder().getPageSortedByTitle( | ||
queryBuilder.toString(), | ||
params.getRows(), | ||
params.getPageIndex(), | ||
params.getFieldsToFetch() | ||
); | ||
|
||
// Use the provided mapper to convert results | ||
return mapper.map(cp.getRight(), cp.getLeft()); | ||
} | ||
|
||
public static void main(String[] args) { | ||
// Fetching a List of Strings: | ||
SolrQueryParameters params = new SolrQueryParameters.Builder() | ||
.filter("type", "description") | ||
.filter("model", "desiredModel") | ||
.sort("title", true) | ||
.rows(10) | ||
.pageIndex(0) | ||
.fieldsToFetch(List.of("source")) | ||
.build(); | ||
|
||
List<String> pids = solrQueryService.query(params, (docs, total) -> | ||
docs.stream() | ||
.map(doc -> doc.getFieldValue("source").toString()) | ||
.collect(Collectors.toList()) | ||
); | ||
|
||
// Fetching a Pair (Total Records, List of Strings): | ||
Pair<Long, List<String>> result = solrQueryService.query(params, (docs, total) -> | ||
new Pair<>(total, docs.stream() | ||
.map(doc -> doc.getFieldValue("source").toString()) | ||
.collect(Collectors.toList())) | ||
); | ||
|
||
// Fetching Custom Triplets: | ||
List<Triplet<String, String, String>> triplets = solrQueryService.query(params, (docs, total) -> | ||
docs.stream() | ||
.map(doc -> new Triplet<>( | ||
doc.getFieldValue("source").toString(), | ||
doc.getFieldValue("title").toString(), | ||
doc.getFieldValue("model").toString())) | ||
.collect(Collectors.toList()) | ||
); | ||
|
||
} | ||
} |
277 changes: 277 additions & 0 deletions
277
.../common/src/main/java/cz/incad/kramerius/fedora/impl/tmp/TmpAbstractRepositoryAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
package cz.incad.kramerius.fedora.impl.tmp; | ||
|
||
import java.io.IOException; | ||
import java.util.*; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.xml.bind.JAXBContext; | ||
import javax.xml.bind.JAXBException; | ||
import javax.xml.bind.Unmarshaller; | ||
import javax.xml.xpath.XPathExpressionException; | ||
import javax.xml.xpath.XPathFactory; | ||
|
||
import com.google.inject.name.Named; | ||
import com.qbizm.kramerius.imp.jaxb.DigitalObject; | ||
import cz.incad.kramerius.fedora.RepositoryAccess; | ||
import cz.incad.kramerius.fedora.om.repository.RepositoryException; | ||
import cz.incad.kramerius.fedora.om.repository.impl.AkubraDOManager; | ||
import cz.incad.kramerius.fedora.om.repository.impl.AkubraRepositoryImpl; | ||
import cz.incad.kramerius.fedora.om.resourceindex.ProcessingIndexFeeder; | ||
import cz.incad.kramerius.statistics.accesslogs.AggregatedAccessLogs; | ||
import org.dom4j.Namespace; | ||
import org.ehcache.CacheManager; | ||
import org.w3c.dom.DOMException; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
|
||
import com.google.inject.Inject; | ||
|
||
import cz.incad.kramerius.FedoraNamespaces; | ||
import cz.incad.kramerius.ProcessSubtreeException; | ||
import cz.incad.kramerius.TreeNodeProcessor; | ||
import cz.incad.kramerius.statistics.StatisticsAccessLog; | ||
import cz.incad.kramerius.fedora.utils.FedoraUtils; | ||
import cz.incad.kramerius.utils.XMLUtils; | ||
import cz.incad.kramerius.utils.conf.KConfiguration; | ||
import cz.incad.kramerius.fedora.utils.pid.LexerException; | ||
|
||
public abstract class TmpAbstractRepositoryAccess implements RepositoryAccess { | ||
//---------------------- | ||
public static final Logger LOGGER = Logger.getLogger(RepositoryApi.class.getName()); | ||
|
||
private static final Namespace NS_FOXML = new Namespace("foxml", "info:fedora/fedora-system:def/foxml#"); | ||
private final AkubraRepositoryImpl akubraRepositoryImpl; | ||
private final Unmarshaller digitalObjectUnmarshaller; | ||
//------------------------------------------------------------------------- | ||
|
||
public static final Logger LOGGER = Logger.getLogger(TmpAbstractRepositoryAccess.class.getName()); | ||
protected XPathFactory xPathFactory; | ||
protected KConfiguration configuration = KConfiguration.getInstance(); | ||
|
||
@Inject | ||
public TmpAbstractRepositoryAccess(@Nullable StatisticsAccessLog accessLog) | ||
throws IOException { | ||
super(); | ||
this.xPathFactory = XPathFactory.newInstance(); | ||
} | ||
|
||
|
||
|
||
@Override | ||
public String findFirstViewablePid(String pid) throws IOException { | ||
final List<String> foundPids = new ArrayList<String>(); | ||
try { | ||
processSubtree(makeSureObjectPid(pid), new TreeNodeProcessor() { | ||
boolean breakProcess = false; | ||
int previousLevel = 0; | ||
|
||
@Override | ||
public boolean breakProcessing(String pid, int level) { | ||
return breakProcess; | ||
} | ||
|
||
@Override | ||
public boolean skipBranch(String pid, int level) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void process(String pid, int level) throws ProcessSubtreeException { | ||
try { | ||
if (previousLevel < level || level == 0) { | ||
if (TmpAbstractRepositoryAccess.this.isImageFULLAvailable(pid)) { | ||
foundPids.add(pid); | ||
breakProcess = true; | ||
} | ||
} else if (previousLevel > level) { | ||
breakProcess = true; | ||
} else if ((previousLevel == level) && (level != 0)) { | ||
breakProcess = true; | ||
} | ||
previousLevel = level; | ||
} catch (Exception e) { | ||
throw new ProcessSubtreeException(e); | ||
} | ||
} | ||
}); | ||
} catch (ProcessSubtreeException e) { | ||
throw new IOException(e); | ||
} catch (LexerException e) { | ||
throw new IOException(e); | ||
} | ||
|
||
return foundPids.isEmpty() ? null : foundPids.get(0); | ||
} | ||
|
||
@Override | ||
public void processSubtree(String pid, TreeNodeProcessor processor) throws ProcessSubtreeException, IOException { | ||
try { | ||
pid = makeSureObjectPid(pid); | ||
Document relsExt = null; | ||
try { | ||
// should be from | ||
if (isStreamAvailable(pid, FedoraUtils.RELS_EXT_STREAM)) { | ||
relsExt = getRelsExt(pid); | ||
} else { | ||
LOGGER.warning("could not read root RELS-EXT, skipping object (" + pid + ")"); | ||
} | ||
} catch (Exception ex) { | ||
LOGGER.warning("could not read root RELS-EXT, skipping object (" + pid + "):" + ex); | ||
} | ||
if (!processor.skipBranch(pid, 0)) { | ||
processSubtreeInternal(pid, relsExt, processor, 0, new Stack<String>()); | ||
} | ||
} catch (LexerException e) { | ||
LOGGER.warning("Error in pid: " + pid); | ||
throw new ProcessSubtreeException(e); | ||
} catch (XPathExpressionException e) { | ||
throw new ProcessSubtreeException(e); | ||
} | ||
} | ||
|
||
|
||
// TODO something more generic like getFieldFromStream | ||
@Override | ||
public String getDonator(String pid) throws IOException { | ||
return getDonator(getRelsExt(pid)); | ||
} | ||
|
||
// TODO something more generic like getFieldFromStream | ||
@Override | ||
public String getKrameriusModelName(String pid) throws IOException { | ||
return getKrameriusModelName(getRelsExt(pid)); | ||
} | ||
|
||
// TODO something more generic like getFieldFromStream | ||
@Override | ||
public List<Element> getPages(String pid, boolean deep) throws IOException { | ||
Document relsExt = getRelsExt(pid); | ||
return getPages(pid, relsExt.getDocumentElement()); | ||
} | ||
|
||
|
||
// TODO something more generic like getFieldFromStream | ||
@Override | ||
public String getFirstItemPid(String pid) throws IOException { | ||
Document relsExt = getRelsExt(pid); | ||
return getFirstItemPid(relsExt); | ||
} | ||
|
||
// TODO something more generic like getFieldFromStream | ||
@Override | ||
public String getFirstVolumePid(String pid) throws IOException { | ||
Document relsExt = getRelsExt(pid); | ||
return getFirstVolumePid(relsExt); | ||
} | ||
|
||
/* | ||
@Override | ||
public boolean isImageFULLAvailable(String pid) throws IOException { | ||
try { | ||
return isStreamAvailable(makeSureObjectPid(pid), FedoraUtils.IMG_FULL_STREAM); | ||
} catch (LexerException e) { | ||
LOGGER.log(Level.SEVERE, e.getMessage(), e); | ||
throw new IOException(e); | ||
} | ||
}*/ | ||
|
||
@Override | ||
public boolean getFirstViewablePath(List<String> pids, List<String> models) throws IOException { | ||
try { | ||
String pid = pids.get(pids.size() - 1); | ||
pid = makeSureObjectPid(pid); | ||
if (isImageFULLAvailable(pid)) { | ||
return true; | ||
} | ||
Document relsExt = getRelsExt(pid); | ||
Element descEl = XMLUtils.findElement(relsExt.getDocumentElement(), "Description", | ||
FedoraNamespaces.RDF_NAMESPACE_URI); | ||
List<Element> els = XMLUtils.getElements(descEl); | ||
for (Element el : els) { | ||
if (getTreePredicates().contains(el.getLocalName())) { | ||
if (el.hasAttribute("rdf:resource")) { | ||
pid = el.getAttributes().getNamedItem("rdf:resource").getNodeValue(); | ||
pids.add(pid); | ||
models.add(getKrameriusModelName(pid)); | ||
if (getFirstViewablePath(pids, models)) { | ||
return true; | ||
} else { | ||
pids.remove(pids.size() - 1); | ||
models.remove(pids.size() - 1); | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} catch (DOMException e) { | ||
LOGGER.log(Level.SEVERE, e.getMessage(), e); | ||
throw new IOException(e); | ||
} catch (LexerException e) { | ||
LOGGER.log(Level.SEVERE, e.getMessage(), e); | ||
throw new IOException(e); | ||
} | ||
} | ||
|
||
/* | ||
@Override | ||
public List<String> getModelsOfRel(Document relsExt) { | ||
try { | ||
throw new UnsupportedOperationException("still unsupported"); | ||
// Element foundElement = | ||
// XMLUtils.findElement(relsExt.getDocumentElement(), "hasModel", | ||
// FedoraNamespaces.FEDORA_MODELS_URI); | ||
// if (foundElement != null) { | ||
// String sform = | ||
// foundElement.getAttributeNS(FedoraNamespaces.RDF_NAMESPACE_URI, | ||
// "resource"); | ||
// PIDParser pidParser = new PIDParser(sform); | ||
// pidParser.disseminationURI(); | ||
// ArrayList<String> model = | ||
// RelsExtModelsMap.getModelsOfRelation(pidParser.getObjectId()); | ||
// return model; | ||
// } else { | ||
// throw new IllegalArgumentException("cannot find model of "); | ||
// } | ||
} catch (DOMException e) { | ||
LOGGER.log(Level.SEVERE, e.getMessage(), e); | ||
throw new IllegalArgumentException(e); | ||
} | ||
}*/ | ||
|
||
/* | ||
@Override | ||
public List<String> getModelsOfRel(String pid) throws IOException { | ||
return getModelsOfRel(getRelsExt(pid)); | ||
}*/ | ||
// RepoApiImpl------------------------------------------------------------------------------------------- | ||
@Inject | ||
public RepositoryApiImpl(ProcessingIndexFeeder processingIndexFeeder, @Named("akubraCacheManager") CacheManager cacheManager) throws RepositoryException { | ||
try { | ||
AkubraDOManager akubraDOManager = new AkubraDOManager(cacheManager); | ||
this.akubraRepositoryImpl = (AkubraRepositoryImpl) AkubraRepositoryImpl.build(processingIndexFeeder, akubraDOManager); | ||
this.digitalObjectUnmarshaller = JAXBContext.newInstance(DigitalObject.class).createUnmarshaller(); | ||
} catch (IOException e) { | ||
throw new RepositoryException(e); | ||
} catch (JAXBException e) { | ||
throw new RepositoryException("Error initializing JAXB unmarshaller for " + DigitalObject.class.getName()); | ||
} | ||
} | ||
|
||
|
||
//--------- KraRepositoryAPIIMPl | ||
@javax.inject.Inject | ||
private RepositoryApiImpl repositoryApi; | ||
|
||
@javax.inject.Inject | ||
private AggregatedAccessLogs accessLog; | ||
|
||
|
||
@Override | ||
public RepositoryApi getLowLevelApi() { | ||
return repositoryApi; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.