ContextManagerImpl.java

/*
 * Copyright (c) 2007-2026 MetaSolutions AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.entrystore.impl;

import org.apache.commons.io.FileUtils;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.query.Binding;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.MalformedQueryException;
import org.eclipse.rdf4j.query.QueryEvaluationException;
import org.eclipse.rdf4j.query.QueryLanguage;
import org.eclipse.rdf4j.query.TupleQuery;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.repository.RepositoryResult;
import org.eclipse.rdf4j.rio.RDFHandler;
import org.eclipse.rdf4j.rio.RDFHandlerException;
import org.eclipse.rdf4j.rio.RDFParseException;
import org.eclipse.rdf4j.rio.RDFWriter;
import org.eclipse.rdf4j.rio.helpers.BasicParserSettings;
import org.eclipse.rdf4j.rio.helpers.StatementCollector;
import org.eclipse.rdf4j.rio.trig.TriGParser;
import org.eclipse.rdf4j.rio.trig.TriGWriter;
import org.entrystore.AuthorizationException;
import org.entrystore.Context;
import org.entrystore.ContextManager;
import org.entrystore.Entry;
import lombok.Getter;
import org.entrystore.EntryType;
import org.entrystore.GraphType;
import org.entrystore.PrincipalManager;
import org.entrystore.PrincipalManager.AccessProperty;
import org.entrystore.Resource;
import org.entrystore.ResourceType;
import org.entrystore.SearchIndex;
import org.entrystore.repository.RepositoryEvent;
import org.entrystore.repository.RepositoryEventObject;
import org.entrystore.repository.RepositoryManager;
import org.entrystore.repository.config.Settings;
import org.entrystore.repository.security.ContextImportPendingException;
import org.entrystore.repository.security.ContextRemovalPendingException;
import org.entrystore.repository.util.FileOperations;
import org.entrystore.repository.util.NS;
import org.entrystore.repository.util.URISplit;
import org.entrystore.repository.util.URIType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

/**
 * @author Matthias Palmer
 * @author Hannes Ebner
 */
public class ContextManagerImpl extends EntryNamesContext implements ContextManager {

	Logger log = LoggerFactory.getLogger(ContextManagerImpl.class);

	/** How many skipped objects a single call names in its warning, so a stale index cannot flood the log. */
	private static final int UNUSABLE_TRIPLE_SAMPLE_SIZE = 5;
	/** Bounds one sampled object, which can be a store-fed literal of arbitrary length. */
	private static final int UNUSABLE_TRIPLE_SAMPLE_MAX_CHARS = 200;

	/** C0/C1 controls, format characters such as bidi overrides, and the Unicode line separators. */
	private static final Pattern CONTROL_CHARACTERS = Pattern.compile("[\\p{Cc}\\p{Cf}\\u2028\\u2029]");

	/** Deletes the graphs of removed contexts. Package-private for RepositoryManagerImpl and tests. */
	final ContextRemover contextRemover;

	/** Removes the trees of lists. Package-private for ContextImpl, ListImpl, RepositoryManagerImpl and tests. */
	final ListTreeRemover listTreeRemover;

	/** The ids of the contexts being imported; added and checked under the repository monitor. */
	private final Set<String> importsPending = ConcurrentHashMap.newKeySet();

	/**
	 * Run on the importing thread while an import prepares, once its plan is read, checked and read again without the
	 * monitor, so that a change made here is found only by the read under it; for tests.
	 */
	volatile Runnable whileImportPrepares = () -> {};

	public ContextManagerImpl(RepositoryManagerImpl rman, Repository repo) {
		super(new EntryImpl(rman,repo), URISplit.createURI(rman.getRepositoryURL().toString(),
				RepositoryProperties.SYSTEM_CONTEXTS_ID,
				RepositoryProperties.DATA_PATH,
				RepositoryProperties.SYSTEM_CONTEXTS_ID).toString(),
				rman.getSoftCache());
		contextRemover = new ContextRemover(rman, this.resourceURI);
		listTreeRemover = new ListTreeRemover(rman);
		//First try to load the ContextManager.
		EntryImpl e = (EntryImpl) getByEntryURI(URISplit.createURI(rman.getRepositoryURL().toString(),
				RepositoryProperties.SYSTEM_CONTEXTS_ID,
				RepositoryProperties.ENTRY_PATH,
				RepositoryProperties.SYSTEM_CONTEXTS_ID));
		//If it does not exist, create it.
		if (e == null) {
			e = this.createNewMinimalItem(null, null, EntryType.Local, GraphType.SystemContext,
					null, rman.getSystemContextAliases().getFirst());
		}
		this.entry = e;
		loadIndex();
	}

	@Override
	public boolean isRemovalPending(String contextId) {
		return contextRemover.isPending(contextId);
	}

	/** Whether the context is being imported, which refuses creates in it and a second import of it. */
	boolean isImportPending(String contextId) {
		return contextId != null && importsPending.contains(contextId);
	}

	/**
	 * Marks a removed context deleted and starts deleting its graphs. Runs under the repository monitor, so a
	 * create in the context, or of a context with its id, either completes before or is refused after.
	 */
	@Override
	protected void afterRemoveCommitted(Resource removedResource) {
		if (removedResource instanceof ContextImpl removedContext) {
			removedContext.markDeleted();
			contextRemover.schedule(removedContext.id);
		}
	}

	/** Also refuses a context with the id of one whose graphs are still being deleted. */
	@Override
	protected void checkCreateAllowed(String entryId) {
		super.checkCreateAllowed(entryId);
		if (isRemovalPending(entryId)) {
			throw new ContextRemovalPendingException("Context " + entryId + " is still being removed");
		}
	}

	/** A removed context's entry graph is gone at once, but its id stays taken until its graphs are deleted. */
	@Override
	protected boolean isIdReserved(String id) {
		return isRemovalPending(id);
	}

	public void exportContext(Entry contextEntry, File destFile, Set<URI> users, boolean metadataOnly, Class<? extends RDFWriter> writer) throws RepositoryException {
		String contextResourceURI = contextEntry.getResourceURI().toString();
		if (!contextResourceURI.endsWith("/")) {
			contextResourceURI += "/";
		}
		String contextEntryURI = contextEntry.getEntryURI().toString();
		String contextMetadataURI = contextEntry.getLocalMetadataURI().toString();
		String contextRelationURI = contextEntry.getRelationURI().toString();

		synchronized (this.entry.repository) {
			RepositoryConnection rc = null;
			BufferedOutputStream out = null;

			try {
				try {
					out = new BufferedOutputStream(Files.newOutputStream(destFile.toPath()));
				} catch (IOException e) {
					log.error(e.getMessage());
					throw new RepositoryException(e);
				}

				rc = entry.getRepository().getConnection();

				RepositoryResult<org.eclipse.rdf4j.model.Resource> availableNGs = rc.getContextIDs();
				List<org.eclipse.rdf4j.model.Resource> filteredNGs = new ArrayList<>();
				while (availableNGs.hasNext()) {
					org.eclipse.rdf4j.model.Resource ng = availableNGs.next();
					String ngURI = ng.stringValue();
					if (metadataOnly) {
						if (ngURI.startsWith(contextResourceURI) &&	(ngURI.contains("/metadata/") || ngURI.contains("/cached-external-metadata/"))) {
							filteredNGs.add(ng);
						}
					} else {
						if (ngURI.startsWith(contextResourceURI) ||
								ngURI.equals(contextEntryURI) ||
								ngURI.equals(contextMetadataURI) ||
								ngURI.equals(contextRelationURI)) {
							filteredNGs.add(ng);
						}
					}
				}
				availableNGs.close();

				RDFHandler rdfWriter = null;
				try {
					Constructor<? extends RDFWriter> constructor = writer.getConstructor(OutputStream.class);
					rdfWriter = constructor.newInstance(out);
				} catch (Exception e) {
					log.error(e.getMessage());
				}

				if (rdfWriter == null) {
					log.error("Unable to create an RDF writer, format not supported");
					return;
				}

				rdfWriter.startRDF();
				Map<String, String> namespaces = NS.getMap();
				for (String nsName : namespaces.keySet()) {
					rdfWriter.handleNamespace(nsName, namespaces.get(nsName));
				}

				RepositoryResult<Statement> rr = rc.getStatements(null, null, null, false, filteredNGs.toArray(new org.eclipse.rdf4j.model.Resource[filteredNGs.size()]));
				while (rr.hasNext()) {
					Statement s = rr.next();
					IRI p = s.getPredicate();
					rdfWriter.handleStatement(s);
					if (!metadataOnly) {
						if (p.equals(RepositoryProperties.Creator) ||
								p.equals(RepositoryProperties.Contributor) ||
								p.equals(RepositoryProperties.Read) ||
								p.equals(RepositoryProperties.Write) ||
								p.equals(RepositoryProperties.DeletedBy)) {
							users.add(URI.create(s.getObject().stringValue()));
						}
					}
				}
				rr.close();
				rdfWriter.endRDF();
			} catch (RepositoryException e) {
				log.error("Error when exporting context", e);
				throw e;
			} catch (RDFHandlerException e) {
				log.error(e.getMessage(), e);
				throw new RepositoryException(e);
			} finally {
				rc.close();
				try {
					out.flush();
					out.close();
				} catch (IOException ignored) {}
			}
		}
	}

	public void importContext(Entry contextEntry, File srcFile) throws RepositoryException, IOException {
		File unzippedDir = FileOperations.createTempDirectory("entrystore_import", null);
		try {
			importContextFromUnzippedDir(contextEntry, srcFile, unzippedDir);
		} finally {
			log.info("Removing temporary files");
			FileOperations.deleteDirectory(unzippedDir);
		}
	}

	/**
	 * Replaces the entries of the context, except those an import keeps (see {@link ContextImpl#isKeptByImport}), by
	 * the export in {@code unzippedDir}, atomically: a failure leaves the old content (ENTRYSTORE-1064). Requires
	 * Administer on the context, checked before the upload is read.
	 *
	 * <p>Parses the export and moves it to the context's URIs first. Then, while creates in the context and a second
	 * import of it are refused, reads what to remove without the repository monitor, which scans the store without an
	 * index starting with the context. The monitor is held for one transaction that reads again the entries edited
	 * meanwhile (see {@link EntryPurge}), removes what was read and adds the imported statements, with the index
	 * triples and the counter of the imported entry graphs in place of those of the replaced entries. The imported
	 * entries fire no events, so the context is reindexed in Solr once the transaction has committed.
	 */
	private void importContextFromUnzippedDir(Entry contextEntry, File srcFile, File unzippedDir) throws RepositoryException, IOException {
		Date before = new Date();
		String contextId = contextEntry.getId();
		ContextImpl cont = (ContextImpl) getContext(contextId);
		if (cont == null) {
			throw new ContextRemovalPendingException("Context " + contextId + " has been removed");
		}
		cont.checkAccess(null, AccessProperty.Administer);
		FileOperations.unzipFile(srcFile, unzippedDir);

		File propFile = new File(unzippedDir, "export.properties");
		log.info("Loading property file from {}", propFile);
		Properties props = new Properties();
		props.load(Files.newInputStream(propFile.toPath()));
		String srcBaseURI = props.getProperty("baseURI");
		String srcContextEntryURI = props.getProperty("contextEntryURI");
		String srcContextResourceURI = props.getProperty("contextResourceURI");
		String srcContextMetadataURI = props.getProperty("contextMetadataURI");
		String srcContextRelationURI = props.getProperty("contextRelationURI");
		String srcContainedUsers = props.getProperty("containedUsers");

		if (srcBaseURI == null || srcContextEntryURI == null || srcContextResourceURI == null || srcContainedUsers == null) {
			String msg = "Property file of import ZIP did not contain all necessary properties, aborting import";
			log.error(msg);
			throw new org.entrystore.repository.RepositoryException(msg);
		}

		log.info("baseURI: {}", srcBaseURI);
		log.info("contextEntryURI: {}", srcContextEntryURI);
		log.info("contextResourceURI: {}", srcContextResourceURI);
		log.info("contextMetadataURI: {}", srcContextMetadataURI);
		log.info("contextRelationURI: {}", srcContextRelationURI);
		log.info("containedUsers: {}", srcContainedUsers);

		String[] containedUsers = srcContainedUsers.split(",");
		Map<String, String> id2name = new HashMap<>();
		for (String u : containedUsers) {
			String[] uS = u.split(":");
			if (uS.length == 1) {
				id2name.put(uS[0], null);
			} else if (uS.length == 2) {
				id2name.put(uS[0], uS[1]);
			}
		}

		// parse all statements from the file before removing anything, so that a parse failure
		// aborts the import without first destroying the existing context

		File tripleFile = new File(unzippedDir, "triples.rdf");
		log.info("Loading quadruples from {}", tripleFile);
		StatementCollector collector = new StatementCollector();
		try (InputStream rdfInput = new BufferedInputStream(Files.newInputStream(tripleFile.toPath()))) {
			TriGParser parser = new TriGParser();
			parser.getParserConfig().set(BasicParserSettings.VERIFY_DATATYPE_VALUES, false);
			parser.setRDFHandler(collector);
			parser.parse(rdfInput, srcBaseURI);
		} catch (RDFParseException | RDFHandlerException e) {
			throw new org.entrystore.repository.RepositoryException("Failed to parse RDF data from the import file", e);
		}

		ImportedStatements imported;
		try {
			imported = moveToContext(collector.getStatements(), cont, props, id2name);
		} catch (IllegalArgumentException e) {
			// an IRI of the upload that java.net.URI rejects
			throw new org.entrystore.repository.RepositoryException("Failed to import context data", e);
		} catch (AuthorizationException e) {
			throw e;
		} catch (RuntimeException e) {
			// e.g. a principal lookup in the store failed, which must not read as a bad upload
			throw new IllegalStateException("Failed to move the export to context " + contextId, e);
		}
		collector.clear();
		int importedCount = imported.statements().size();

		synchronized (this.entry.repository) {
			// checked under the monitor: a removal that committed while the import was being read must win
			if (cont.isDeleted()) {
				throw new ContextRemovalPendingException("Context " + contextId + " has been removed");
			}
			if (!importsPending.add(contextId)) {
				throw new ContextImportPendingException("Context " + contextId + " is being imported");
			}
		}
		try {
			ImportPlan plan;
			try {
				plan = planImport(cont);
			} catch (AuthorizationException e) {
				throw e;
			} catch (RuntimeException e) {
				// the store or its data failed, not the upload, which a RepositoryException would be answered as
				throw new IllegalStateException("Failed to plan the import of context " + contextId, e);
			}
			long held = commitImport(cont, plan, imported);

			// committed, so each step below only logs a failure; the removed entries' files go before the
			// imported ones, which may have the same names, are copied
			plan.purge().afterPublish();
			// the import changed their members and fired nothing; invalidated under the monitor, so listeners read
			// the committed ones
			List<EntryImpl> pruned = plan.purge().prunedLists();
			for (EntryImpl keptList : plan.keptLists()) {
				if (!pruned.contains(keptList)) {
					entry.getRepositoryManager().fireRepositoryEvent(
							new RepositoryEventObject(keptList, RepositoryEvent.ResourceUpdated));
				}
			}
			copyImportedFiles(contextId, unzippedDir);
			reindexImportedContext(contextEntry);

			log.info("Import finished in {} ms, holding the repository monitor for {} ms",
					new Date().getTime() - before.getTime(), held);
			log.info("Imported {} triples", importedCount);
			log.info("Skipped {} triples", imported.skipped());
		} finally {
			importsPending.remove(contextId);
		}
	}

	/**
	 * The statements an import adds, moved to the context, with the index triples and the id counter of the imported
	 * entry graphs, and how many exported statements were skipped.
	 */
	private record ImportedStatements(Set<Statement> statements, List<Statement> index, int counter, long skipped) {
	}

	/** The removal of the entries an import replaces, and the lists it keeps, whose members it may change. */
	private record ImportPlan(EntryPurge purge, List<EntryImpl> keptLists) {
	}

	/**
	 * Moves the exported statements to the context: graphs, subjects and objects in the exported context's namespace,
	 * or naming its entry, resource, metadata or relations, are renamed, and the principals that creator, contributor,
	 * ACL and deleted-by statements name are found by their id or name. A statement is skipped if it has no graph, if
	 * its graph is not one an export of the context holds (see {@link #isExportedGraph}), if its principal is unknown,
	 * or if it repeats one moved before. Derives the index triples and the counter from the imported entry graphs as
	 * {@link ContextImpl#reIndex()} does.
	 */
	private ImportedStatements moveToContext(Collection<Statement> exported, ContextImpl cont, Properties props,
			Map<String, String> id2name) {
		Entry contextEntry = cont.getEntry();
		PrincipalManager pm = entry.getRepositoryManager().getPrincipalManager();
		ValueFactory vf = entry.repository.getValueFactory();

		String oldContextNS = withTrailingSlash(props.getProperty("contextResourceURI"));
		String newContextNS = withTrailingSlash(contextEntry.getResourceURI().toString());
		Map<String, String> renamed = new HashMap<>();
		renameIfExported(renamed, props.getProperty("contextEntryURI"), contextEntry.getEntryURI());
		renameIfExported(renamed, props.getProperty("contextResourceURI"), contextEntry.getResourceURI());
		renameIfExported(renamed, props.getProperty("contextMetadataURI"), contextEntry.getLocalMetadataURI());
		renameIfExported(renamed, props.getProperty("contextRelationURI"), contextEntry.getRelationURI());
		Set<String> contextEntryGraphs = Set.of(contextEntry.getEntryURI().toString(),
				contextEntry.getLocalMetadataURI().toString(), contextEntry.getRelationURI().toString());
		log.info("Moving the export from {} to {}", oldContextNS, newContextNS);

		Set<Statement> moved = new LinkedHashSet<>();
		List<Statement> index = new ArrayList<>();
		int counter = 0;
		long skipped = 0;
		long outsideContext = 0;
		for (Statement s : exported) {
			org.eclipse.rdf4j.model.Resource context = s.getContext();
			if (context == null) {
				log.warn("No named graph information provided, ignoring triple");
				skipped++;
				continue;
			}
			if (context instanceof IRI contextIRI) {
				context = moveIRI(contextIRI, oldContextNS, newContextNS, renamed, vf);
			}
			if (!isExportedGraph(context, newContextNS, contextEntryGraphs)) {
				outsideContext++;
				skipped++;
				continue;
			}

			org.eclipse.rdf4j.model.Resource subject = s.getSubject();
			IRI predicate = s.getPredicate();
			Value object = s.getObject();

			if (predicate.equals(RepositoryProperties.Creator) ||
					predicate.equals(RepositoryProperties.Contributor) ||
					predicate.equals(RepositoryProperties.Read) ||
					predicate.equals(RepositoryProperties.Write) ||
					predicate.equals(RepositoryProperties.DeletedBy)) {
				String oldUserID = URISplit.getLastSegment(object.stringValue());
				String oldUserName = id2name.get(oldUserID);
				Entry pE = oldUserName == null ? pm.get(oldUserID) : pm.getPrincipalEntry(oldUserName);
				if (pE == null || pE.getResourceURI() == null) {
					log.info("Unable to detect principal for ID {}, skipping", oldUserID);
					skipped++;
					continue;
				}
				object = vf.createIRI(pE.getResourceURI().toString());
			}

			if (subject instanceof IRI subjectIRI) {
				subject = moveIRI(subjectIRI, oldContextNS, newContextNS, renamed, vf);
			}
			if (object instanceof IRI objectIRI) {
				object = moveIRI(objectIRI, oldContextNS, newContextNS, renamed, vf);
			}

			if (!moved.add(vf.createStatement(subject, predicate, object, context))) {
				skipped++;
				continue;
			}
			boolean resource = predicate.equals(RepositoryProperties.resource);
			if (!resource && !predicate.equals(RepositoryProperties.externalMetadata)
					|| !(object instanceof org.eclipse.rdf4j.model.Resource indexed)) {
				continue;
			}
			Optional<String> entryId = entryIdOf(context, cont.id);
			if (entryId.isEmpty()) {
				continue;
			}
			index.add(vf.createStatement(indexed,
					resource ? RepositoryProperties.resHasEntry : RepositoryProperties.mdHasEntry, context,
					cont.resourceURI));
			if (resource) {
				try {
					counter = Math.max(counter, Integer.parseInt(entryId.get()));
				} catch (NumberFormatException ignored) {
					// an id such as _top, or one beyond the int range the counter is read back in, takes no number
				}
			}
		}
		if (outsideContext > 0) {
			log.warn("Skipped {} statements of the import in graphs outside context {}", outsideContext, cont.id);
		}
		return new ImportedStatements(moved, index, counter, skipped);
	}

	/**
	 * Whether a graph, moved to the context, is one an export of the context holds: one in its namespace, or the
	 * context entry's own entry, metadata or relations graph. The context's index graph is none of them.
	 */
	private static boolean isExportedGraph(org.eclipse.rdf4j.model.Resource graph, String contextNS,
			Set<String> contextEntryGraphs) {
		return graph instanceof IRI iri
				&& (iri.stringValue().startsWith(contextNS) || contextEntryGraphs.contains(iri.stringValue()));
	}

	private static String withTrailingSlash(String uri) {
		return uri.endsWith("/") ? uri : uri + "/";
	}

	private static void renameIfExported(Map<String, String> renamed, String exported, URI imported) {
		if (exported != null) {
			renamed.put(exported, imported.toString());
		}
	}

	/** The IRI moved to the context, or the same instance where it names nothing of the exported context. */
	private static IRI moveIRI(IRI iri, String oldContextNS, String newContextNS, Map<String, String> renamed,
			ValueFactory vf) {
		String uri = iri.stringValue();
		if (uri.startsWith(oldContextNS)) {
			return vf.createIRI(uri.replace(oldContextNS, newContextNS));
		}
		String moved = renamed.get(uri);
		return moved != null ? vf.createIRI(moved) : iri;
	}

	/** The id of the entry whose entry graph {@code graph} is, if it is one of the context's. */
	private Optional<String> entryIdOf(org.eclipse.rdf4j.model.Resource graph, String contextId) {
		if (!(graph instanceof IRI)) {
			return Optional.empty();
		}
		return URISplit.of(URI.create(graph.stringValue()), entry.getRepositoryManager().getRepositoryURL())
				.filter(split -> split.getUriType() == URIType.MetaMetadata && contextId.equals(split.getContextId()))
				.map(URISplit::getId);
	}

	/**
	 * Plans removing the entries the import replaces, as the store's index graph names them, since the in-memory index
	 * skips a statement it cannot parse (ENTRYSTORE-1095). Reads again, still without the monitor, the entries edited
	 * while the plan was read.
	 */
	private ImportPlan planImport(ContextImpl cont) {
		ValueFactory vf = entry.repository.getValueFactory();
		Set<IRI> replaced = new LinkedHashSet<>();
		List<EntryImpl> keptLists = new ArrayList<>();
		for (URI child : cont.getChildEntryURIsFromStore()) {
			if (!cont.isKeptByImport(child)) {
				replaced.add(vf.createIRI(child.toString()));
			} else if (cont.getByEntryURI(child) instanceof EntryImpl kept && kept.getGraphType() == GraphType.List) {
				keptLists.add(kept);
			}
		}
		EntryPurge purge = EntryPurge.plan(cont, replaced);
		try (RepositoryConnection reader = entry.getRepository().getConnection()) {
			purge.refresh(reader);
		}
		whileImportPrepares.run();
		log.info("Removing {} entries ({} statements) of context {}", replaced.size(), purge.statementCount(),
				cont.id);
		return new ImportPlan(purge, keptLists);
	}

	/**
	 * Removes the planned entries and adds the imported statements in one transaction under the monitor, rewrites the
	 * context's index triples and counter, and drops every tree removal marker in the context, which ends those
	 * removals, as a marker may name a list the import replaces. Then, still under the monitor, ends at once those
	 * waiting for a retry, publishes the removal and drops what memory holds of the context's indexes and of the lists
	 * kept, whose members the import may have changed.
	 *
	 * @return how long the monitor was held, in milliseconds
	 */
	private long commitImport(ContextImpl cont, ImportPlan plan, ImportedStatements imported) {
		synchronized (this.entry.repository) {
			long start = System.currentTimeMillis();
			if (cont.isDeleted()) {
				throw new ContextRemovalPendingException("Context " + cont.id + " has been removed");
			}
			RepositoryConnection rc = null;
			try {
				rc = entry.getRepository().getConnection();
				rc.begin();
				int changed = plan.purge().refresh(rc);
				if (changed > 0) {
					log.info("{} entries of context {} changed during the import and were read again", changed,
							cont.id);
				}
				plan.purge().apply(rc);
				rc.remove((org.eclipse.rdf4j.model.Resource) null, RepositoryProperties.treeRemovalPending, null,
						cont.resourceURI);
				rc.remove(cont.resourceURI, RepositoryProperties.counter, null, cont.resourceURI);
				rc.add(cont.resourceURI, RepositoryProperties.counter,
						rc.getValueFactory().createLiteral(imported.counter()), cont.resourceURI);
				rc.add(imported.index());
				rc.add(imported.statements());
				rc.commit();
			} catch (Exception e) {
				if (rc != null) {
					plan.purge().rollBack(rc);
				}
				throw new IllegalStateException("Failed to commit the import of context " + cont.id, e);
			} finally {
				if (rc != null) {
					rc.close();
				}
			}
			listTreeRemover.cancelRetries(cont.id);
			// committed, so a failure is only logged; under the monitor, so no list write computes from members a
			// reader cached during the import
			try {
				plan.purge().publish(true);
			} catch (RuntimeException e) {
				log.error("Context {} was imported, but releasing what memory holds of the replaced entries failed: {}",
						cont.id, e.getMessage(), e);
			}
			for (EntryImpl keptList : plan.keptLists()) {
				// the field, not getResource(), which may load the resource and fail
				if (keptList.resource instanceof ListImpl list) {
					list.invalidateChildren();
				}
			}
			return System.currentTimeMillis() - start;
		}
	}

	/** Starts a Solr reindex of the imported context, if Solr is used; a failure to start it is only logged. */
	private void reindexImportedContext(Entry contextEntry) {
		SearchIndex index = entry.getRepositoryManager().getIndex();
		if (index == null) {
			return;
		}
		try {
			index.reindex(contextEntry.getEntryURI());
		} catch (RuntimeException e) {
			log.error("Context {} was imported, but its Solr reindex could not be started; reindex the context: {}",
					contextEntry.getId(), e.getMessage(), e);
		}
	}

	/** Copies the imported resource files to the context's data folder; a failure is only logged. */
	private void copyImportedFiles(String contextId, File unzippedDir) {
		List<String> failedResourceCopies = new ArrayList<>();
		try {
			File dstDir = new File(entry.getRepositoryManager().getConfiguration().getString(Settings.DATA_FOLDER), contextId);
			if (!dstDir.exists() && !dstDir.mkdirs()) {
				log.error("Could not create data directory {}, skipping copying of imported resource files", dstDir);
			} else {
				File resourceDir = new File(unzippedDir, "resources");
				File[] resourceFiles = resourceDir.isDirectory() ? resourceDir.listFiles() : null;
				if (resourceFiles != null) {
					for (File src : resourceFiles) {
						if (DataImpl.isStagingFile(src.getName())) {
							continue;
						}
						File dst = new File(dstDir, src.getName());
						log.info("Copying {} to {}", src, dst);
						try {
							FileOperations.copyFile(src, dst);
						} catch (Exception e) {
							failedResourceCopies.add(src.getName());
							log.error("Failed to copy imported resource file {} to {}, the corresponding entry will have no data",
									src, dst, e);
						}
					}
				}
			}
		} catch (Exception e) {
			log.error("Failed to copy imported resource files to the data directory", e);
		}
		if (!failedResourceCopies.isEmpty()) {
			log.error("Import of context {} committed, but {} resource file(s) could not be copied "
							+ "and the entries with the following ids have no data: {}",
					contextId, failedResourceCopies.size(), failedResourceCopies);
		}
	}

	/** FIXME: rewrite
	 */
	public boolean deleteBackup(URI contexturi, String fromTime) {
		String folder = getContextBackupFolder(contexturi, fromTime);
		File backupFolder = new File(folder);
		if(backupFolder.exists()) {
			return FileOperations.deleteDirectory(backupFolder);
		} else {
			log.error("The folder does not exist");
		}
		return false;
	}

	/** FIXME: rewrite
	 */
	public String getBackup(URI contexturi, String fromTime) {
		String folder = getContextBackupFolder(contexturi, fromTime);
		try {
			File file = new File(folder, "portfolio-index.rdf");
			File file2 = new File(folder,  "portfolio-entries.rdf");

			String content1 = FileUtils.readFileToString(file);
			String content2 = FileUtils.readFileToString(file2);

			return content1 + content2;

		}catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}

	/** FIXME: rewrite
	 */
	public String createBackup(URI contexturi) throws RepositoryException {
		HashMap<String, String> map = getContextBackupFolder(contexturi, new Date());
		File backupFolder = new File(map.get("dateFolder"));

		backupFolder.mkdirs();

		RepositoryConnection conn = null;
		try {
			conn = entry.getRepository().getConnection();
			Entry portfolio = getEntry(contexturi);

			RDFHandler indexHandler = null;
			try {
				indexHandler = new TriGWriter(Files.newOutputStream(new File(backupFolder, "portfolio-index.rdf").toPath()));
			} catch (IOException e) {
				log.error(e.getMessage(), e);
				throw new RepositoryException(e);
			}

			ValueFactory f = entry.getRepository().getValueFactory();
			try {
				conn.export(indexHandler, f.createIRI(portfolio.getEntryURI().toString()));
				conn.export(indexHandler, f.createIRI(portfolio.getLocalMetadataURI().toString()));
				conn.export(indexHandler, f.createIRI(portfolio.getResourceURI().toString()));
			} catch (RDFHandlerException e) {
				log.error(e.getMessage(), e);
				throw new RepositoryException(e);
			}

			Context pfContext = (Context) portfolio.getResource();

			RDFHandler entryHandler;
			try {
				entryHandler = new TriGWriter(Files.newOutputStream(new File(backupFolder, "portfolio-entries.rdf").toPath()));
			} catch (IOException e) {
				log.error(e.getMessage(), e);
				throw new RepositoryException(e);
			}

			try {
				Set<URI> portfolioResources = pfContext.getEntries();
				for (URI uri : portfolioResources) {
					Entry e = pfContext.getByEntryURI(uri);
					if (e == null) {
						log.warn("Context {} lists entry {}, which does not resolve in it; leaving it out of the "
								+ "backup", contexturi, uri);
						continue;
					}
					conn.export(entryHandler, f.createIRI(e.getEntryURI().toString()));
					if (e.getEntryType() == EntryType.Local || e.getEntryType() == EntryType.Link
							|| e.getEntryType() == EntryType.LinkReference) {
						conn.export(entryHandler, f.createIRI(e.getLocalMetadata().getURI().toString()));
					}
					if (e.getEntryType() == EntryType.Local && e.getGraphType() != GraphType.None) {
						conn.export(entryHandler, f.createIRI(e.getResourceURI().toString()));
					} else if (e.getEntryType() == EntryType.Local && e.getGraphType() == GraphType.None
							&& e.getResourceType() == ResourceType.InformationResource) {
						// File dataFolder = new
						// File(entry.getRepositoryManager().getConfiguration().getString(Settings.DATA_FOLDER));
						// TODO Spara undan filen som ligger i entryt.
						// Det som står här under stämmer inte.
						// String id = new URISplit(uri,
						// e.getRepositoryManager().getRepositoryURL()).getID();
						// File src = new File(dataFolder, id);
						// File dest = new File(backupFolder, id);
						// try {
						// FileOperations.copyFile(src, dest);
						// } catch (IOException ioe) {
						// log.error(ioe.getMessage(), ioe);
						// throw new RepositoryException(ioe);
						// }
					}
				}
			} catch (RDFHandlerException e) {
				log.error(e.getMessage(), e);
				throw new RepositoryException(e);
			}
		} catch (RepositoryException e) {
			log.error("Error in repository", e);
			throw e;
		} finally {
			conn.close();
		}
		return map.get("timestampStr");
	}

	/** FIXME: rewrite
	 */
	public List<Date> listBackups(URI contexturi) {
		File dir = new File(getContextBackupFolder(contexturi));
		String[] children = dir.list();
		List<Date> backups = new ArrayList<>();
		for (int i = 0; i < children.length; i++) {
			Date bkp = parseTimestamp(children[i]);
			if (bkp != null) {
				backups.add(bkp);
			}
		}
		return backups;
	}

	/** FIXME: rewrite
	 */
	public void restoreBackup(URI contexturi, String fromTime) {
		this.remove(this.getByResourceURI(contexturi).iterator().next().getEntryURI());

		TriGParser trigParser = new TriGParser();
		trigParser.getParserConfig().set(BasicParserSettings.VERIFY_DATATYPE_VALUES, false);

		StatementCollector collector = new StatementCollector();
		trigParser.setRDFHandler(collector);
		String folder = getContextBackupFolder(contexturi, fromTime);
		try {
			InputStream fileOut = Files.newInputStream(new File(folder, "portfolio-index.rdf").toPath());
			InputStream fileOut2 = Files.newInputStream(new File(folder,  "portfolio-entries.rdf").toPath());

			String base = entry.getRepositoryManager().getConfiguration().getString(Settings.BASE_URL, "https://entrystore.org");

			trigParser.parse(fileOut, base);
			fileOut.close();
			try (RepositoryConnection conn = entry.getRepository().getConnection()) {
				for (Statement s : collector.getStatements()) {
					conn.add(s);
				}

				trigParser.parse(fileOut2, base);
				fileOut2.close();
				for (Statement s : collector.getStatements()) {
					conn.add(s);
				}
			}
		} catch (RDFHandlerException | RDFParseException | IOException e) {
			log.error(e.getMessage());
		} catch (RepositoryException e) {
			log.error(e.getMessage(), e);
		}

	}

	/** FIXME: rewrite
	 */
	public String getContextBackupFolder(URI contexturi) {
		String backupFolder = entry.getRepositoryManager().getConfiguration().getString(Settings.BACKUP_FOLDER);
		String helper = contexturi.toString();

		// TODO use URIStr instead - but we don't have the baseURL

		String pfId = URISplit.getLastSegment(helper);
		if (!backupFolder.endsWith("/")) {
			backupFolder += "/";
		}
		return backupFolder + pfId;
	}

	public HashMap<String, String> getContextBackupFolder(URI contexturi, Date date) {
		String timestampStr = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
		File backupFolder = new File(getContextBackupFolder(contexturi));
		File datedFolder = new File(backupFolder, timestampStr);

		HashMap<String, String> map = new HashMap<>();
		map.put("dateFolder", datedFolder.toString());
		map.put("timestampStr", timestampStr);
		return map;
	}

	public String getContextBackupFolder(URI contexturi, String date) {
		File backupFolder = new File(getContextBackupFolder(contexturi));
		File datedFolder = new File(backupFolder, date);

		return datedFolder.toString();
	}

	private Date parseTimestamp(String timestamp) {
		Date date = null;
		DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
		try {
			date = formatter.parse(timestamp);
		} catch (ParseException pe) {
			log.error(pe.getMessage());
		}
		return date;
	}

	public String getName(URI contextURI) {
		URISplit us = new URISplit(contextURI, this.entry.getRepositoryManager().getRepositoryURL());
		if (us.getUriType() == URIType.Resource) {
			return super.getName(us.getMetaMetadataURI());
		}
		throw new org.entrystore.repository.RepositoryException("Given URI is not an existing contextURI.");
	}

	public URI getContextURI(String contextAlias) {
		Entry contextEntry = getEntryByName(contextAlias);
		if (contextEntry == null) {
			return null;
		} else if (contextEntry.getGraphType() == GraphType.Context ||
						contextEntry.getGraphType() == GraphType.SystemContext) {
			return contextEntry.getResourceURI();
		}
		throw new org.entrystore.repository.RepositoryException("""
				Found entry for the alias is not a context...
				this is either a programming error or someone have been tampering with the RDF directly.""");
	}

	public Set<String> getNames() {
		return getEntryNames();
	}

	public boolean setName(URI contextURI, String newAlias) {
		URISplit us = new URISplit(contextURI, this.entry.getRepositoryManager().getRepositoryURL());
		Entry contextEntry = getByEntryURI(us.getMetaMetadataURI());
		if (contextEntry == null) {
			throw new org.entrystore.repository.RepositoryException("Cannot find an entry for the specified URI");
		} else if (contextEntry.getGraphType() == GraphType.Context ||
				contextEntry.getGraphType() == GraphType.SystemContext) {
			return setEntryName(us.getMetaMetadataURI(), newAlias);
		}
		throw new org.entrystore.repository.RepositoryException("Given URI does not refer to a Context.");
	}

	/**
	 * @param contextId
	 */
	public Context getContext(String contextId) {
		Entry entry = this.get(contextId);
		if (entry == null) { //By name
			entry = getEntryByName(contextId);
		}
/*		if (entry == null) { //Maybe it is a full URI, lets try.
			try {
				URI uri = new URI(contextId);
				entry = getByEntryURI(uri);
				if (entry == null) {
					Set<Entry> set = getByResourceURI(uri);
					if (!set.isEmpty()) {
						entry = set.iterator().next();
					}
				}
			} catch (URISyntaxException e) {
			}
		}*/
		if(entry != null) {
			if (entry.getGraphType() == GraphType.Context ||
					entry.getGraphType() == GraphType.SystemContext) {
				Resource context = entry.getResource();
				if (context instanceof Context context1) {
					return context1;
				}
			}
		}
		return null;
	}

	public Context getContext(URI contextURI) {
		if (contextURI == null) {
			throw new IllegalArgumentException("Parameter must not be null");
		}
		String contextID = URISplit.getLastSegment(contextURI.toString());
		return getContext(contextID);
	}

	//Retrieve functions
	public Entry getEntry(URI uri) {
		return getEntry(uri, true);
	}

	/**
	 * Like {@link #getEntry(URI)}, but without checking ReadMetadata. For core code that maintains entries the current
	 * user may not read, such as their inverse relations, or that checks access itself before reading from the entry.
	 */
	protected Entry getEntryIgnoreACL(URI uri) {
		return getEntry(uri, false);
	}

	private Entry getEntry(URI uri, boolean withACL) {
		Optional<URISplit> split = URISplit.of(uri, entry.getRepositoryManager().getRepositoryURL());
		URI entryURI = split.flatMap(URISplit::getEntryURI).orElse(null);
		if (entryURI == null) {
			log.warn("Unable to construct entry URI based on likely incorrect URI [{}]", uri);
			return null;
		}
		// Before the cache: entries of a removed context stay cached while its graphs are being deleted.
		if (isRemovalPending(split.get().getContextId())) {
			return null;
		}
		Entry item = softCache.getByEntryURI(entryURI);
		if (item == null) {
			item = loadEntry(split.get(), entryURI, uri);
		}
		if (item != null && withACL) {
			checkReadMetadata(item);
		}
		return item;
	}

	/** Skipped while the principal manager is not yet created. */
	private void checkReadMetadata(Entry item) {
		PrincipalManager pm = entry.getRepositoryManager().getPrincipalManager();
		if (pm != null) {
			pm.checkAuthenticatedUserAuthorized(item, AccessProperty.ReadMetadata);
		}
	}

	private Entry loadEntry(URISplit split, URI entryURI, URI requestedURI) {
		log.debug("SoftCache miss for entry URI {}, falling back to repository lookup", entryURI);
		URI contextEntryURI = split.getContextMetaMetadataURI();
		Entry contextEntry = getByEntryURI(contextEntryURI);
		if (contextEntry == null) {
			log.warn("No context found for entry with URI {} (context URI: {})", requestedURI, contextEntryURI);
			return null;
		}
		Entry resolved = ((Context) contextEntry.getResource()).getByEntryURI(entryURI);
		if (resolved == null) {
			log.warn("Context {} found but entry not resolvable for URI {}", contextEntryURI, requestedURI);
		}
		return resolved;
	}

	public Set<Entry> getLinks(URI resourceURI) {
		return getEntriesFromIndex(resourceURI, RepositoryProperties.resHasEntry, EntryType.Link);
	}

	public Set<Entry> getReferences(URI metadataURI) {
		return getEntriesFromIndex(metadataURI, RepositoryProperties.mdHasEntry, EntryType.Reference);
	}

	/**
	 * Looks up the entries an index property points at from the given URI, keeping only those of the
	 * wanted entry type.
	 * <p>
	 * A single unusable index triple never fails the whole lookup: this enumeration is fed straight
	 * from the store, so it also sees objects that are not IRIs at all, that {@code java.net.URI}
	 * rejects, that do not sit under the current repository base URL (a restored backup, or a changed
	 * base URL), or that resolve to no entry because the entry has since been removed. Each such
	 * triple is skipped and reported through one warning per call.
	 * <p>
	 * Entries the current user may not read metadata for are excluded.
	 *
	 * @param uri the subject of the index triples: a resource URI for resHasEntry, a metadata URI
	 * for mdHasEntry.
	 * @param indexProperty {@code RepositoryProperties.resHasEntry} or {@code mdHasEntry}.
	 * @param wantedEntryType the entry type to keep: Link for resHasEntry, Reference for mdHasEntry.
	 * @return the matching entries, never null.
	 */
	private Set<Entry> getEntriesFromIndex(URI uri, IRI indexProperty, EntryType wantedEntryType) {
		Set<Entry> entries = new HashSet<>();
		UnusableTriples unusable = new UnusableTriples();
		IRI subject = entry.repository.getValueFactory().createIRI(uri.toString());
		try (RepositoryConnection rc = entry.repository.getConnection();
			 RepositoryResult<Statement> indexTriples = rc.getStatements(subject, indexProperty, null, false)) {
			while (indexTriples.hasNext()) {
				Value object = indexTriples.next().getObject();
				if (!(object instanceof IRI entryIRI)) {
					unusable.skipped(object, "not an IRI");
					continue;
				}
				try {
					Entry indexedEntry = getItemInRepositoryByMMdURI(URI.create(entryIRI.stringValue()));
					if (indexedEntry == null) {
						unusable.skipped(object, "resolves to no entry");
					} else if (indexedEntry.getEntryType() == wantedEntryType) {
						entries.add(indexedEntry);
					}
				} catch (AuthorizationException ae) {
					// excluded rather than reported, per the interface contract
				} catch (IllegalArgumentException | IndexOutOfBoundsException | NoSuchElementException e) {
					unusable.skipped(object, e.toString());
				}
			}
		} catch (RepositoryException e) {
			log.error("Repository error", e);
			throw new org.entrystore.repository.RepositoryException("Repository error", e);
		}
		unusable.warn(log, indexProperty, uri);
		return entries;
	}

	/**
	 * Counts the index triples a lookup had to skip and keeps a bounded sample of them for one
	 * warning per call.
	 * <p>
	 * A sampled object comes from the store, where a caller holding WriteMetadata can put a literal
	 * of any size and content under an index predicate, so the object and its reason are each
	 * truncated (URI.create echoes the whole offending IRI in its message) and the rendering is
	 * stripped of the control characters that would otherwise let it forge whole log records.
	 */
	static final class UnusableTriples {

		@Getter
		private int count;

		private final List<String> sample = new ArrayList<>();

		List<String> getSample() {
			return List.copyOf(sample);
		}

		void skipped(Value object, String reason) {
			count++;
			if (sample.size() < UNUSABLE_TRIPLE_SAMPLE_SIZE) {
				sample.add(sanitize(truncate(String.valueOf(object)) + " (" + truncate(reason) + ")"));
			}
		}

		/** Cuts on a code point boundary, so a truncated sample cannot end in a lone surrogate. */
		private static String truncate(String text) {
			if (text.length() <= UNUSABLE_TRIPLE_SAMPLE_MAX_CHARS) {
				return text;
			}
			int end = UNUSABLE_TRIPLE_SAMPLE_MAX_CHARS;
			if (Character.isHighSurrogate(text.charAt(end - 1))) {
				end--;
			}
			return text.substring(0, end) + "...";
		}

		private static String sanitize(String rendered) {
			return CONTROL_CHARACTERS.matcher(rendered).replaceAll(" ");
		}

		void warn(Logger log, IRI indexProperty, URI uri) {
			if (count > sample.size()) {
				log.warn("Skipped {} unusable {} triple(s) for {}, first {}: {}",
					count, indexProperty, uri, sample.size(), sample);
			} else if (count > 0) {
				log.warn("Skipped {} unusable {} triple(s) for {}: {}", count, indexProperty, uri, sample);
			}
		}
	}

	protected Entry getItemInRepositoryByMMdURI(URI mmdURI) {
		Entry found = softCache.getByEntryURI(mmdURI);
		if (found == null) {
			Entry contextItem = getByEntryURI(Util.getContextMMdURIFromURI(entry.getRepositoryManager(), mmdURI));
			if (contextItem == null) {
				return null;
			}
			found = ((Context) contextItem.getResource()).getByEntryURI(mmdURI);
		} else if (found.getContext().isDeleted()) {
			return null;
		}
		if (found != null) {
			checkReadMetadata(found);
		}
		return found;
	}

	public void initResource(EntryImpl newEntry) throws RepositoryException {
		if (newEntry.getEntryType() != EntryType.Local) {
			return;
		}

		switch (newEntry.getGraphType()) {
		case Context:
			Class clsReg = this.entry.repositoryManager.getRegularContextClass();
			try {
				Object[] constrParamReg = {newEntry, newEntry.getResourceURI().toString(), this.softCache};
				Class[] constrClsParamReg = {EntryImpl.class, String.class, SoftCache.class};
				Constructor constrReg = clsReg.getConstructor(constrClsParamReg);
				Context context = (Context) constrReg.newInstance(constrParamReg);
				newEntry.setResource(context);
//				context.initializeSystemEntries();
			} catch (Exception e) {
				throw new RepositoryException("Could not instantiate class "+clsReg.getName()
					+"\nfor regular Context with URI "+newEntry.getEntryURI());
			}
			break;
		case SystemContext:
			Class cls = this.entry.repositoryManager.getSystemContextClassForAlias(newEntry.getId());
			if (cls.isAssignableFrom(this.getClass())) { //Bootstrap case, i.e. when creating the ContextManager itself.
				newEntry.setResource(this);
			} else {
				try {
					Object[] constrParam = {newEntry, newEntry.getResourceURI().toString(), this.softCache};
					Class[] constrClsParam = {EntryImpl.class, String.class, SoftCache.class};
					Constructor constr = cls.getConstructor(constrClsParam);
					newEntry.setResource((Context) constr.newInstance(constrParam));

				} catch (Exception e) {
					throw new RepositoryException("Could not instantiate class "+cls.getName()
							+"\nfor SystemContext with URI "+newEntry.getEntryURI());
				}
			}
			break;
		default:
			super.initResource(newEntry);
		}
	}

	/**
	 * Search in the repository after entries. The metadata query will be evaluated first.
	 *
	 * @param mdQueryString a string which should be a SPARQL quary. The query result must
	 * return a URI to an entry. Can be null if you dont want to search metadata information.
	 * <pre>
	 * Example string:
	 * PREFIX dc:<http://purl.org/dc/terms/>
	 * SELECT ?namedGraphURI
	 * WHERE {
	 * 	GRAPH ?namedGraphURI {
	 * 		?x dc:title ?y
	 * 	}
	 * }
	 * </pre>
	 * @param entryQueryString a SPARQL query which searchs after entries.
	 * <pre>
	 * Example string:
	 * PREFIX dc:<http://purl.org/dc/terms/>
	 * SELECT ?entryURI
	 * WHERE {
	 * 		?entryURI dc:created ?y
	 * }
	 * </pre>
	 * @param contextList a list with URI:s to contexts, if null all contexts will be returned.
	 * Remember to take the context URI. Should be null if you want to search in all contexts.
	 * <pre>
	 * Example:
	 * List<URI> list = new ArrayList<URI>();
	 * list.add(new URI("http://example.se/{context-id}"))
	 * </pre>
	 * @return A list with entries or null.
	 * @throws Exception if something goes wrong
	 */
	public List<Entry> search(String entryQueryString, String mdQueryString, List<URI> contextList) throws Exception {
		List<Entry> mdEntries = null;
		List<Entry> entries = null;
		List<Entry> intersectionEntries = null;

		//First we must evaluate the Metadata query.
		mdEntries = searchMetadataQuery(mdQueryString);

		// Search in the entries.
		entries = searchEntryQuery(entryQueryString);

		// intersect the lists of entries.
		intersectionEntries = intersectEntries(entries, mdEntries);

		// Remove entries which do not contain the contexts which are in the context list
		List<Entry> foundEntries = intersectEntriesFromContexts(intersectionEntries, contextList);

		List<Entry> result = new ArrayList<>();
		for (Entry entry : foundEntries) {
			if (entry != null && isEntryMetadataReadable(entry)) {
				result.add(entry);
			}
		}

		return result;
	}

	public Map<Entry, Integer> searchLiterals(Set<IRI> predicates, String[] terms, String lang, List<URI> context, boolean andOperation) {
		Map<org.eclipse.rdf4j.model.Resource, Integer> matches = new HashMap<>();
		RepositoryConnection rc = null;
		try {
			rc = entry.getRepository().getConnection();
			for (IRI p : predicates) {
				RepositoryResult<Statement> rr = rc.getStatements(null, p, null, false);
				while (rr.hasNext()) {
					Statement s = rr.next();
					Value o = s.getObject();
					if (!(o instanceof Literal)) {
						continue;
					}
					if (lang != null && !lang.equalsIgnoreCase(((Literal) o).getLanguage().orElse(null))) {
						continue;
					}
					org.eclipse.rdf4j.model.Resource c = s.getContext();
					if (context != null && !context.isEmpty()) {
						int contextMatches = 0;
						for (URI cURI : context) {
							if (cURI != null && c.stringValue().startsWith(cURI.toString())) {
								contextMatches++;
							}
						}
						if (contextMatches == 0) {
							continue;
						}
					}
					int matchCount = 0;
					for (String term : terms) {
						if (o.stringValue().toLowerCase().contains(term.toLowerCase())) {
							matchCount++;
						}
					}
					if (andOperation && (matchCount != terms.length)) {
						continue;
					}
					if (matches.containsKey(c)) {
						matches.put(c, matches.get(c) + matchCount);
					} else {
						matches.put(c, matchCount);
					}
				}
				rr.close();
			}
		} catch (RepositoryException re) {
			log.error(re.getMessage(), re);
		} finally {
			if (rc != null) {
				try {
					rc.close();
				} catch (RepositoryException ignore) {}
			}
		}

		Map<Entry, Integer> result = new LinkedHashMap<>();

		for (org.eclipse.rdf4j.model.Resource mdURI : matches.keySet()) {
			URISplit split = new URISplit(URI.create(mdURI.toString()), entry.repositoryManager.getRepositoryURL());
			URI entryURI = split.getMetaMetadataURI();
			if (entryURI != null) {
				Entry e;
				try {
					e = getEntry(entryURI);
				} catch (AuthorizationException ae) {
					continue;
				}
				if (e != null && isEntryMetadataReadable(e)) {
					result.put(e, matches.get(mdURI));
				}
			}
		}

		if (result.size() > 1) {
			Date before = new Date();
			result = sortMapByValue(result, false);
			log.debug("Sorting results took: {} ms", new Date().getTime() - before.getTime());
		}

		return result;
	}

	public static boolean isEntryMetadataReadable(Entry entry) {
		if (entry == null) {
			return false;
		}
		try {
			checkEntryMetadataReadable(entry);
		} catch (AuthorizationException ae) {
			return false;
		}
		return true;
	}

	/**
	 * Checks that the current user may read all metadata of {@code entry}: ReadMetadata on the entry itself and,
	 * for a Reference or LinkReference whose external metadata is the local metadata of an entry in this repository,
	 * also on that entry. A referenced entry that does not exist adds no check. The entry itself is checked first,
	 * so that a caller who may not read it does not load the referenced entry.
	 *
	 * @throws AuthorizationException if the user lacks ReadMetadata on the entry or on the entry it references
	 */
	public static void checkEntryMetadataReadable(Entry entry) {
		RepositoryManager rm = entry.getRepositoryManager();
		PrincipalManager pm = rm.getPrincipalManager();
		pm.checkAuthenticatedUserAuthorized(entry, AccessProperty.ReadMetadata);
		if ((entry.getEntryType() == EntryType.Reference || entry.getEntryType() == EntryType.LinkReference)
				&& entry.getCachedExternalMetadata() instanceof LocalMetadataWrapper) {
			// Null as well for a URI that does not denote an entry
			Entry refEntry = rm.getContextManager().getEntry(entry.getExternalMetadataURI());
			if (refEntry != null) {
				pm.checkAuthenticatedUserAuthorized(refEntry, AccessProperty.ReadMetadata);
			}
		}
	}

	public Map sortMapByValue(Map map, final boolean ascending) {
		List list = new LinkedList(map.entrySet());
		Collections.sort(list, new Comparator() {
			public int compare(Object o1, Object o2) {
				int result = ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
				if (result == 0) {
					result = ((Map.Entry) (o1)).getKey().toString().compareToIgnoreCase(((Map.Entry) (o2)).getKey().toString());
				}
				if (!ascending) {
					result *= -1;
				}
				return result;
			}
		});
		Map result = new LinkedHashMap();
		for (Iterator it = list.iterator(); it.hasNext();) {
			Map.Entry entry = (Map.Entry) it.next();
			result.put(entry.getKey(), entry.getValue());
		}
		return result;
	}

	/**
	 * Intersection
	 * @param entries entries
	 * @param mdEntries entries from metadata query
	 * @return the intersected entries.
	 */
	private List<Entry> intersectEntries(List<Entry> entries, List<Entry> mdEntries) {
		if(mdEntries != null && entries != null) {
			mdEntries.retainAll(entries);
			return mdEntries;
		}
		if (mdEntries != null) {
			return mdEntries;
		}
		return entries;
	}

	/**
	 * Evaluate a metadata query
	 * @param mdQueryString a SPARQL syntax string
	 * @return a list with entries
	 * @throws RepositoryException
	 */
	private List<Entry> searchMetadataQuery(String mdQueryString) throws RepositoryException {
		if(mdQueryString == null) {
			return null;
		}
		List<Entry> entryURIs = new ArrayList<>();
		RepositoryConnection rc = null;
		try {
			rc = entry.getRepository().getConnection();
			TupleQuery mdQuery = rc.prepareTupleQuery(QueryLanguage.SPARQL, mdQueryString);

			List<String> mdURIs = new ArrayList<>();
			TupleQueryResult result = mdQuery.evaluate();

			while (result.hasNext()) {
				BindingSet set = result.next();
				Iterator<Binding> itr = set.iterator();
				while(itr.hasNext())  {
					Binding obj = itr.next();
					if(obj.getValue() instanceof IRI) {
						String mdURI = obj.getValue().toString();
						if(!mdURIs.contains(mdURI)) {
							mdURIs.add(mdURI);
						}
					}
				}
			}

			for (String mdStr : mdURIs) {
				URISplit split = new URISplit(URI.create(mdStr), entry.repositoryManager.getRepositoryURL());
				URI entryURI = split.getMetaMetadataURI();
				if (!entryURIs.contains(entryURI) && entryURI != null) {
					Entry ent;
					try {
						ent = this.getEntry(entryURI);
					} catch (NullPointerException | AuthorizationException ex) {
						continue;
					}
					if (ent != null && !entryURIs.contains(ent)) {
						entryURIs.add(ent);
					}
				}
			}
		} catch (RepositoryException e) {
			log.error(e.getMessage());
			throw e;
		} catch (MalformedQueryException e) {
			log.error(e.getMessage());
		} catch (QueryEvaluationException e) {
			log.error(e.getMessage());
		} finally {
			rc.close();
		}
		return entryURIs;
	}

	/**
	 * intersection for all entries
	 * @param intersectionEntries
	 * @param contextList
	 * @return
	 */
	private List<Entry> intersectEntriesFromContexts(List<Entry> intersectionEntries, List<URI> contextList) {
		if(contextList == null) {
			return intersectionEntries;
		}

		// filter the list so that only entrys wich belong to one of the specified contexts are included
		List<Entry> resultList = new ArrayList<>();
		if (intersectionEntries != null) {
			for (URI u: contextList) {
				for (Entry e: intersectionEntries) {
					if(u.equals(e.getContext().getURI())) {
						resultList.add(e);
					}
				}
			}
		}

		return resultList;
	}

	/**
	 * Evaluates the entry SPARQL query
	 * @param entryQueryString a string with a SPARQL query.
	 * @return list with entries or null.
	 * @throws RepositoryException
	 * @throws Exception if error
	 */
	private List<Entry> searchEntryQuery(String entryQueryString) throws RepositoryException {
		TupleQuery entryQuery;

		if (entryQueryString == null) {
			return null;
		}

		List<Entry> entries = new ArrayList<>();
		RepositoryConnection rc = null;
		try {
			rc = entry.getRepository().getConnection();
			entryQuery = rc.prepareTupleQuery(QueryLanguage.SPARQL, entryQueryString);
			TupleQueryResult result = entryQuery.evaluate();

			while(result.hasNext()) {
				BindingSet set = result.next();
				Iterator<Binding> itr = set.iterator();
				while(itr.hasNext())  {
					Binding obj = itr.next();
					if(obj.getValue() instanceof IRI) {
						Entry entry;
						try {
							entry = this.getEntry(new URI(obj.getValue().toString()));
						} catch (AuthorizationException ae) {
							continue;
						}
						if (!entries.contains(entry))
							entries.add(entry);
					}
				}
			}
		} catch (RepositoryException e) {
			log.error(e.getMessage());
			throw e;
		} catch (MalformedQueryException e) {
			log.error(e.getMessage());
		} catch (URISyntaxException e) {
			log.error(e.getMessage());
		} catch (QueryEvaluationException e) {
			log.error(e.getMessage());
		} finally {
			rc.close();
		}
		return entries;
	}

	// TODO:
	public List<Entry> search(String pattern, List<URI> list) {
		return null;
	}

	public void initializeSystemEntries() {
		super.initializeSystemEntries();

		RepositoryManager repMan = entry.repositoryManager;
		String base = repMan.getRepositoryURL().toString();
		for (String id: repMan.getSystemContextAliases()) {
			Entry scon = this.getByEntryURI(URISplit.createURI(base, RepositoryProperties.SYSTEM_CONTEXTS_ID,
					RepositoryProperties.ENTRY_PATH, id));
			if (scon == null) {
				scon = this.createNewMinimalItem(null, null, EntryType.Local, GraphType.SystemContext, null, id);
				setMetadata(scon, id, null);
			}
//			repMan.setSystemContext(scon.getId(), scon);
			Context sc = (Context) scon.getResource();
			if (this.getName(scon.getResourceURI()) == null) {
				this.setName(scon.getResourceURI(), id);
			}

			if (sc != this) {
				sc.initializeSystemEntries();
			}
			addSystemEntryToSystemEntries(scon.getEntryURI());
		}

		for (String id: repMan.getSystemContextAliases()) {
			Entry scon = this.getByEntryURI(URISplit.createURI(base, RepositoryProperties.SYSTEM_CONTEXTS_ID,
					RepositoryProperties.ENTRY_PATH, id));
			if (scon.getAllowedPrincipalsFor(AccessProperty.ReadMetadata).isEmpty()) {
				scon.addAllowedPrincipalsFor(AccessProperty.ReadMetadata, repMan.getPrincipalManager().getGuestUser().getURI());
			}
		}
	}

}