+ /*
+ * SEARCH
+ */
+
+ @Override
+ public Spliterator<Content> search(ProvidedSession session, BasicSearch search, String relPath) {
+ try {
+ Session jcrSession = getJcrSession(session, jcrWorkspace);
+ BasicSearchToQom jcrBasicSearch = new BasicSearchToQom(jcrSession, search, relPath);
+ Query query = jcrBasicSearch.createQuery();
+ QueryResult queryResult = query.execute();
+ return new QueryResultSpliterator(session, queryResult.getNodes());
+ } catch (RepositoryException e) {
+ throw new JcrException(e);
+ }
+ }
+
+ class QueryResultSpliterator implements Spliterator<Content> {
+ private ProvidedSession providedSession;
+ private NodeIterator nodeIterator;
+
+ public QueryResultSpliterator(ProvidedSession providedSession, NodeIterator nodeIterator) {
+ super();
+ this.providedSession = providedSession;
+ this.nodeIterator = nodeIterator;
+ }
+
+ @Override
+ public boolean tryAdvance(Consumer<? super Content> action) {
+ if (!nodeIterator.hasNext())
+ return false;
+ try {
+ Node node = nodeIterator.nextNode();
+ // TODO optimise by reusing the Node
+ JcrContent jcrContent = new JcrContent(providedSession, JcrContentProvider.this, jcrWorkspace,
+ node.getPath());
+ action.accept(jcrContent);
+ return true;
+ } catch (RepositoryException e) {
+ throw new JcrException(e);
+ }
+ }
+
+ @Override
+ public Spliterator<Content> trySplit() {
+ return null;
+ }
+
+ @Override
+ public long estimateSize() {
+ return nodeIterator.getSize();
+ }
+
+ @Override
+ public int characteristics() {
+ return NONNULL | SIZED;
+ }
+
+ }
+
+ /*
+ * UTILITIES
+ */
+ /**
+ * Just adds a '/' so that it becomes an absolute JCR path within the JCR
+ * workspace of this provider.
+ */
+ private String toJcrPath(String relativePath) {
+ return ContentUtils.SLASH + relativePath;
+ }
+
+ /*
+ * TRANSITIONAL, WHILE MIGRATING FROM JCR TO ACR
+ */
+ @Deprecated
+ public synchronized <T> T doInAdminSession(Function<Session, T> toDo) {
+ try {
+ return toDo.apply(adminSession);
+ } finally {
+ try {
+ if (adminSession.hasPendingChanges())
+ adminSession.save();
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot save admin session", e);
+ }
+ }
+ }
+