EntryImpl.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 lombok.Getter;
import lombok.Setter;
import org.eclipse.rdf4j.common.iteration.Iterations;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.LinkedHashModel;
import org.eclipse.rdf4j.model.util.Models;
import org.eclipse.rdf4j.model.vocabulary.RDF;
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.entrystore.Context;
import org.entrystore.Entry;
import org.entrystore.EntryType;
import org.entrystore.GraphType;
import org.entrystore.Group;
import org.entrystore.Metadata;
import org.entrystore.PrincipalManager;
import org.entrystore.PrincipalManager.AccessProperty;
import org.entrystore.Provenance;
import org.entrystore.Resource;
import org.entrystore.ResourceType;
import org.entrystore.User;
import org.entrystore.exception.InvalidExternalMetadataURIException;
import org.entrystore.exception.SelfReferencingExternalMetadataException;
import org.entrystore.repository.CorruptEntryException;
import org.entrystore.repository.RepositoryEvent;
import org.entrystore.repository.RepositoryEventObject;
import org.entrystore.repository.RepositoryManager;
import org.entrystore.repository.util.ModelUtil;
import org.entrystore.repository.util.URISplit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.net.URI;
import java.net.URL;
import java.util.Arrays;
import java.util.Date;
import java.util.EnumMap;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static org.eclipse.rdf4j.model.util.Values.iri;

public class EntryImpl implements Entry {
	@Getter
	protected volatile String id;
	@Setter
	protected volatile Resource resource;
	protected volatile MetadataImpl localMetadata;
	@Getter
	protected volatile Metadata cachedExternalMetadata;
	protected volatile IRI localMdURI;
	protected volatile IRI relationURI;

	protected volatile IRI externalMdURI;
	protected volatile IRI cachedExternalMdURI;
	protected ContextImpl context;
	@Getter
	protected final Repository repository;
	protected RepositoryManagerImpl repositoryManager;

	protected volatile IRI entryURI;
	private volatile URI entryUriCache;
	protected volatile IRI resURI;
	protected volatile EntryType locType = EntryType.Local;
	protected volatile ResourceType repType = ResourceType.InformationResource;
	@Getter
	protected volatile GraphType graphType = GraphType.None;
	protected volatile XMLGregorianCalendar created;
	protected volatile XMLGregorianCalendar modified;
	protected volatile XMLGregorianCalendar cachedAt;
	protected volatile IRI creator;
	//protected Set<URI> referredIn = new HashSet<URI>();
	protected volatile Set<IRI> contributors = new HashSet<>();
	protected volatile URI status;

	Logger log = LoggerFactory.getLogger(EntryImpl.class);
	private volatile Set<URI> administerPrincipals;
	private volatile Set<URI> readMetadataPrincipals;
	private volatile Set<URI> writeMetadataPrincipals;
	private volatile Set<URI> writeResourcePrincipals;
	private volatile Set<URI> readResourcePrincipals;
    protected boolean invRelations = false;
	private volatile String format;
	private volatile long fileSize = -1;
	private volatile String filename;
	private volatile Boolean hasExplicitAcl;
	private String originalList;
	private ProvenanceImpl provenance;
	@Getter
	private volatile boolean deleted = false;

	//An ugly hack to be able to initialize the ContextManager itself.
	EntryImpl(RepositoryManagerImpl repositoryManager, Repository repository) {
		this.repositoryManager = repositoryManager;
		this.repository = repository;
		if (repositoryManager.getProvenanceRepository() != null) {
			this.provenance = new ProvenanceImpl(this);
		}
	}

	public EntryImpl(String id, ContextImpl context, RepositoryManagerImpl repositoryManager, Repository repository) {
		this.id = id;
		String base = repositoryManager.getRepositoryURL().toString();
		ValueFactory vf = repository.getValueFactory();
		this.entryURI = vf.createIRI(URISplit.createURI(base, context.id, RepositoryProperties.ENTRY_PATH, this.id).toString());
		this.relationURI = vf.createIRI(URISplit.createURI(base, context.id, RepositoryProperties.RELATION, this.id).toString());
		this.localMdURI = vf.createIRI(URISplit.createURI(base, context.id, RepositoryProperties.MD_PATH, this.id).toString());
		this.context = context;
		this.repositoryManager = repositoryManager;
		this.repository = repository;
		if (repositoryManager.getProvenanceRepository() != null) {
			this.provenance = new ProvenanceImpl(this);
		}
	}

	/**
	 * Loads an MetaMetadataImpl by listing all statements for the named graph given by uri.
	 */
	protected boolean load() {
		try {
			try (RepositoryConnection rc = repository.getConnection()) {
				if (loadFromStatements(Iterations.asList(rc.getStatements(null, null, null, false, this.entryURI)))) {
					initMetadataObjects();
					return true;
				}
				return false;
			} catch (Exception e) {
				log.error(e.getMessage());
				throw new org.entrystore.repository.RepositoryException("Error in repository connection.", e);
			}
		} catch (RepositoryException e) {
			log.error(e.getMessage());
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}
	}

	/** Loads the entry from its entry graph, read through {@code rc}. */
	protected boolean load(RepositoryConnection rc) throws RepositoryException {
		return load(Iterations.asList(rc.getStatements(null, null, null, false, this.entryURI)));
	}

	/** Loads the entry from the statements of its entry graph, which the caller has read. */
	boolean load(List<Statement> entryGraph) {
		if (loadFromStatements(entryGraph)) {
			initMetadataObjects();
			return true;
		}
		return false;
	}

	protected void initMetadataObjects() {

		if (locType == EntryType.LinkReference || locType == EntryType.Reference) {
			if (externalMdURI != null && externalMdURI.stringValue().startsWith(this.repositoryManager.getRepositoryURL().toString())) {
				this.cachedExternalMetadata = new LocalMetadataWrapper(this);
			} else {
				this.cachedExternalMetadata = new MetadataImpl(this, cachedExternalMdURI, resURI, true);
			}
		}

		if (locType == EntryType.Local || locType == EntryType.Link || locType == EntryType.LinkReference) {
			this.localMetadata = new MetadataImpl(this, localMdURI, resURI, false);
		}
	}

	/**
	 * Use when a new entry information object is to be created within an existing transaction.
	 *
	 * @throws DatatypeConfigurationException
	 * @throws RepositoryException
	 */
	protected void create(IRI resURI, IRI externalMetadataURI, GraphType bType, EntryType lType, ResourceType rType, RepositoryConnection rc) throws RepositoryException, DatatypeConfigurationException {
		String base = repositoryManager.getRepositoryURL().toString();
		ValueFactory vf = repository.getValueFactory();
		this.resURI = resURI;

		if (lType == EntryType.LinkReference || lType == EntryType.Reference) {
			checkExternalMetadataURI(URI.create(externalMetadataURI.stringValue()));
			this.cachedExternalMdURI = vf.createIRI(URISplit.createURI(base, context.id, RepositoryProperties.EXTERNAL_MD_PATH, this.id).toString());
			this.externalMdURI = externalMetadataURI;
		}

		initialize(bType, lType, rType, rc);
		initMetadataObjects();
	}

	private void initialize(GraphType bt, EntryType locT, ResourceType repT, RepositoryConnection rc) throws RepositoryException, DatatypeConfigurationException {
		ValueFactory vf = rc.getRepository().getValueFactory();
		if (bt != null) {
			setGraphType(bt, rc);
		}
		if (repT != null && graphType == GraphType.None) {
			setResourceType(repT, rc);
		}
		setLocationType(locT, rc);
		rc.add(entryURI, RepositoryProperties.resource, this.resURI, entryURI);
		created = DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
		rc.add(entryURI, RepositoryProperties.Created, vf.createLiteral(created), entryURI);

		registerEntryModified(rc, vf);

		/*
		 * Adds a statement with the entry URI as subject,
		 * relation as predicate, and the relation URI as
		 * an object to the scam repository, the named context is the entry.
		 */
		rc.add(entryURI, RepositoryProperties.relation, this.relationURI, entryURI);

		if (locT != EntryType.Reference) {
			rc.add(entryURI, RepositoryProperties.metadata, this.localMdURI, entryURI);
		}

		if (locT == EntryType.Reference || locT == EntryType.LinkReference) {
			rc.add(entryURI, RepositoryProperties.externalMetadata, this.externalMdURI, entryURI);
			rc.add(entryURI, RepositoryProperties.cachedExternalMetadata, this.cachedExternalMdURI, entryURI);
		}

		PrincipalManager pm = this.repositoryManager.getPrincipalManager();
		if (pm != null) {
			URI userURI = pm.getAuthenticatedUserURI();
			if (userURI != null) {
				this.creator = vf.createIRI(userURI.toString());
				rc.add(entryURI, RepositoryProperties.Creator, this.creator, entryURI);
			}
		}

		//referredIn = new HashSet<URI>();
		//TODO Check so that not a Context is a NamedResource and other restrictions.
	}

	protected void refreshFromRepository(RepositoryConnection rc) throws RepositoryException {
		loadFromStatements(Iterations.asList(rc.getStatements(null, null, null, false, entryURI)));
	}

	/**
	 * Loads the entry from the statements of its entry graph.
	 *
	 * @throws CorruptEntryException if the entry graph is corrupt, e.g. it lacks the resource statement or
	 *                               contains a literal where a URI is expected or a malformed date
	 */
	private boolean loadFromStatements(List<Statement> existingStatements) throws RepositoryException {
		try {
			return parseStatements(existingStatements);
		} catch (ClassCastException | IllegalArgumentException e) {
			throw new CorruptEntryException(getEntryURI(), "Entry graph <" + existingStatements.getFirst().getContext()
					+ "> is corrupt: it contains a malformed value", e);
		}
	}

	private boolean parseStatements(List<Statement> existingStatements) throws RepositoryException {
		if (existingStatements.isEmpty()) {
			return false;
		}

		Set<URI> administerPrincipals = null;
		Set<URI> readMetadataPrincipals = null;
		Set<URI> writeMetadataPrincipals = null;
		Set<URI> readResourcePrincipals = null;
		Set<URI> writeResourcePrincipals = null;
		Set<IRI> contributors = new HashSet<>();
		IRI entryURI = null;
		IRI resURI = null;
		IRI localMdURI = null;
		IRI cachedExternalMdURI = null;
		IRI relationURI = null;
		XMLGregorianCalendar created = null;
		IRI creator = null;
		XMLGregorianCalendar modified = null;
		IRI externalMdURI = null;
		XMLGregorianCalendar cachedAt = null;
		EntryType locType = EntryType.Local;
		ResourceType repType = ResourceType.InformationResource;
		GraphType graphType = GraphType.None;

		// The following are cached on request. (Move more values here if possible.)
		String format = null;
		long fileSize = -1;
		String filename = null;
		boolean invRelations = false;

		String base = repositoryManager.getRepositoryURL().toString();
		//referredIn = new HashSet<>();
		for (Statement statement : existingStatements) {
			IRI predicate = statement.getPredicate();
			if (predicate.equals(RepositoryProperties.resource)) {
				entryURI = (IRI) statement.getSubject();
				resURI = (IRI) statement.getObject();
			} else if (predicate.equals(RepositoryProperties.metadata)) {
				localMdURI = ((IRI) statement.getObject());
			} else if (predicate.equals(RepositoryProperties.externalMetadata)) {
				externalMdURI = ((IRI) statement.getObject());
			} else if (predicate.equals(RepositoryProperties.cachedExternalMetadata)) {
				cachedExternalMdURI = ((IRI) statement.getObject());
			} else if (predicate.equals(RepositoryProperties.relation)) {
				relationURI = ((IRI) statement.getObject());
			} else if (predicate.equals(RepositoryProperties.cached)) {
				// TODO: also wrong
				cachedAt = ((Literal) statement.getObject()).calendarValue();
//			} else if (predicate.equals(RepositoryProperties.referredIn)) {
//				referredIn.add(URI.create(statement.getObject().stringValue()));
			} else if (predicate.equals(RepositoryProperties.Created)) {
				created = ((Literal) statement.getObject()).calendarValue();
			} else if (predicate.equals(RepositoryProperties.Creator)) {
				creator = ((IRI) statement.getObject());
			} else if (predicate.equals(RepositoryProperties.Contributor)) {
				contributors.add((IRI) statement.getObject());
			} else if (predicate.equals(RepositoryProperties.Modified)) {
				try {
					//log.info(statement.getObject().stringValue());
					modified = ((Literal) statement.getObject()).calendarValue();
				} catch (NullPointerException e) {
					log.error(e.getMessage());
				}
			} else {
				//Check if statement refer other entries that affect their inv-rel cache.
				if (!predicate.equals(RepositoryProperties.Read)
						&& !predicate.equals(RepositoryProperties.Write)
						&& !predicate.equals(RepositoryProperties.Pipeline)
						&& !predicate.equals(RepositoryProperties.originallyCreatedIn)) {
					Value obj = statement.getObject();
					org.eclipse.rdf4j.model.Resource subj = statement.getSubject();
					//Check for relations between this resource and another entry (resourceURI (has to be a repository resource), metadataURI, or entryURI)
					if (obj instanceof IRI
							&& obj.stringValue().startsWith(base)
							&& subj.stringValue().startsWith(base)) {
						invRelations = true;
					}
				}
			}
		}

		if (resURI == null) {
			// Not the local entryURI above: it is only set from the resource statement, which is missing here
			throw new CorruptEntryException(getEntryURI(), describeGraphWithoutResource(existingStatements));
		}

		//Detect types.
		for (Statement statement : existingStatements) {
			org.eclipse.rdf4j.model.Resource subject = statement.getSubject();
			if (statement.getPredicate().equals(RDF.TYPE)) {
				if (resURI.equals(subject)) {
					GraphType gt = getGraphType(statement.getObject());
					if (gt != null) {
						graphType = gt;
					} else {
						ResourceType rt = getResourceType(statement.getObject());
						if (rt != null) {
							repType = rt;
						}
					}
				} else if (entryURI.equals(subject)) {
					EntryType lt = getEntryType(statement.getObject());
					if (lt != null) {
						locType = lt;
					}
				}
			}
		}

		// We set all values at once to avoid any delays and possible
		// inconsistencies that could occur when setting in the loop above
		this.administerPrincipals = administerPrincipals;
		this.readMetadataPrincipals = readMetadataPrincipals;
		this.writeMetadataPrincipals = writeMetadataPrincipals;
		this.readResourcePrincipals = readResourcePrincipals;
		this.writeResourcePrincipals = writeResourcePrincipals;
		this.contributors = contributors;
		this.entryURI = entryURI;
		this.entryUriCache = null;
		this.resURI = resURI;
		this.localMdURI = localMdURI;
		this.cachedExternalMdURI = cachedExternalMdURI;
		this.relationURI = relationURI;
		this.created = created;
		this.creator = creator;
		this.modified = modified;
		this.externalMdURI = externalMdURI;
		this.cachedAt = cachedAt;
		this.locType = locType;
		this.repType = repType;
		this.graphType = graphType;
		this.format = format;
		this.fileSize = fileSize;
		this.filename = filename;
		this.invRelations = invRelations;

		return true;
	}

	/**
	 * Describes an entry graph that has statements but no {@code es:resource} statement, which leaves the
	 * entry without a resource URI. Such a graph is typically the leftover of an incompletely removed entry;
	 * the message names the graph and its predicates so the operator can locate and repair it.
	 */
	private static String describeGraphWithoutResource(List<Statement> statements) {
		String predicates = statements.stream()
				.map(s -> s.getPredicate().stringValue())
				.distinct()
				.sorted()
				.collect(Collectors.joining(", "));
		return "Entry graph <" + statements.getFirst().getContext() + "> is corrupt: it contains "
				+ statements.size() + " statement(s) but no <" + RepositoryProperties.resource
				+ "> statement; predicates present: " + predicates;
	}

	private ResourceType getResourceType(Value rt) {
		if (rt.equals(RepositoryProperties.NamedResource)) {
			return ResourceType.NamedResource;
		} else if (rt.equals(RepositoryProperties.ResolvableInformationResource)) {
			return ResourceType.ResolvableInformationResource;
		} else if (rt.equals(RepositoryProperties.Unknown)) {
			return ResourceType.Unknown;
		} else if (rt.equals(RepositoryProperties.InformationResource)) {  //Default, unneccessary expression.
			return ResourceType.InformationResource;
		}
		return null;
	}

	private GraphType getGraphType(Value bt) {
		if (bt.equals(RepositoryProperties.Context)) {
			return GraphType.Context;
		} else if (bt.equals(RepositoryProperties.SystemContext)) {
			return GraphType.SystemContext;
		} else if (bt.equals(RepositoryProperties.List)) {
			return GraphType.List;
		} else if (bt.equals(RepositoryProperties.ResultList)) {
			return GraphType.ResultList;
		} else if (bt.equals(RepositoryProperties.User)) {
			return GraphType.User;
		} else if (bt.equals(RepositoryProperties.Group)) {
			return GraphType.Group;
		} else if (bt.equals(RepositoryProperties.Pipeline)) {
			return GraphType.Pipeline;
		} else if (bt.equals(RepositoryProperties.PipelineResult)) {
			return GraphType.PipelineResult;
		} else if (bt.equals(RepositoryProperties.String)) {
			return GraphType.String;
		} else if (bt.equals(RepositoryProperties.Graph)) {
			return GraphType.Graph;
		} else if (bt.equals(RepositoryProperties.None)) {
			return GraphType.None;
		}
		return null;
	}

	private EntryType getEntryType(Value rt) {
		if (rt.equals(RepositoryProperties.Reference)) {
			return EntryType.Reference;
		} else if (rt.equals(RepositoryProperties.Link)) {
			return EntryType.Link;
		} else if (rt.equals(RepositoryProperties.LinkReference)) {
			return EntryType.LinkReference;
		} else if (rt.equals(RepositoryProperties.Local)) {
			return EntryType.Local;
		}
		return null;
	}

	public URI getEntryURI() {
		URI cached = entryUriCache;
		if (cached == null) {
			cached = URI.create(entryURI.toString());
			entryUriCache = cached;
		}
		return cached;
	}

	public IRI getSesameEntryURI() {
		return entryURI;
	}

	public IRI getSesameResourceURI() {
		return resURI;
	}

	public URI getResourceURI() {
		return URI.create(resURI.stringValue());
	}

	public URI getLocalMetadataURI() {
		if (localMdURI != null) {
			return URI.create(getSesameLocalMetadataURI().stringValue());
		}
		return null;
	}

	public IRI getSesameLocalMetadataURI() {
		return localMdURI;
	}

	public URI getExternalMetadataURI() {
		if (externalMdURI != null) {
			return URI.create(externalMdURI.stringValue());
		}
		return null;
	}

	public IRI getSesameExternalMetadataURI() {
		return externalMdURI;
	}

	public URI getCachedExternalMetadataURI() {
		if (cachedExternalMdURI != null) {
			return URI.create(cachedExternalMdURI.stringValue());
		}
		return null;
	}

	public IRI getSesameCachedExternalMetadataURI() {
		return cachedExternalMdURI;
	}

	public Date getExternalMetadataCacheDate() {
		if (cachedExternalMdURI == null) {
			return null;
		}
		return cachedAt != null ? cachedAt.toGregorianCalendar().getTime() : null;
	}

	public URI getCreator() {
		if (this.creator != null) {
			return URI.create(this.creator.stringValue());
		}
		return null;
	}

	public void setCreator(URI userURI) {
		if (userURI == null) {
			throw new IllegalArgumentException("User URI must not be null");
		}
		checkAdministerRights();
		try {
			synchronized (this.repository) {
				RepositoryConnection rc = this.repository.getConnection();
				rc.begin();
				try {
					IRI creatorURI = rc.getValueFactory().createIRI(userURI.toString());
					rc.remove(rc.getStatements(entryURI, RepositoryProperties.Creator, null, false, entryURI), entryURI);
					rc.add(entryURI, RepositoryProperties.Creator, creatorURI, entryURI);
					registerEntryModified(rc, this.repository.getValueFactory());
					rc.commit();
					this.creator = creatorURI;
				} catch (Exception e) {
					rc.rollback();
					log.error(e.getMessage());
					throw new org.entrystore.repository.RepositoryException("Error in repository connection", e);
				} finally {
					rc.close();
				}
			}
		} catch (RepositoryException e) {
			log.error(e.getMessage());
			throw new org.entrystore.repository.RepositoryException("Failed to connect to repository", e);
		}
	}

	public Set<URI> getContributors() {
		Set<URI> result = new HashSet<>();
		for (IRI contribURI : this.contributors) {
			result.add(URI.create(contribURI.stringValue()));
		}
		return result;
	}

	public Date getCreationDate() {
		return created != null ? created.toGregorianCalendar().getTime() : null;
	}

	public Date getModifiedDate() {
		return modified != null ? modified.toGregorianCalendar().getTime() : null;
	}

	public ResourceType getResourceType() {
		return repType;
	}

	public EntryType getEntryType() {
		return locType;
	}

	public Model getGraph() {
		//ACL check not necessary as the prerequisite for accessing the MetaMetadata object at all is
		//AccessProperty.readMetadata rights. It is supposed that the object is not delegated
		//to principals with less rights.
		try (RepositoryConnection rc = this.repository.getConnection()) {
			Model graph = Iterations.addAll(rc.getStatements(null, null, null, false, entryURI), new LinkedHashModel());
			//TODO following is a fix for backwards compatability where homeContext is set on user object rather than in the entryinfo.
			if (this.resource instanceof User user && user.getHomeContext() != null) {
				Context context = user.getHomeContext();
				graph.add(this.getSesameResourceURI(), RepositoryProperties.homeContext, ((ContextImpl) context).getSesameURI());
			}
			//End of fix.
			return graph;
		} catch (RepositoryException e) {
			log.error(e.getMessage(), e);
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}
	}

    public Set<URI> getReferringListsInSameContext() {
		Set<URI> set = new HashSet<>();
		Model relations = getRelations();
		if (relations != null) {
			for (Statement statement : relations) {
				if (statement.getPredicate().equals(RepositoryProperties.hasListMember) ||
						statement.getPredicate().equals(RepositoryProperties.hasGroupMember)) {
					set.add(URI.create(statement.getSubject().toString()));
				}
			}
		}
		return set;
	}

	protected void addReferringList(ResourceImpl resource, RepositoryConnection rc) throws RepositoryException {
		synchronized (this.repository) {
			// rc.add(entryURI, RepositoryProperties.referredIn, listResource, entryURI);
			// referredIn.add(URI.create(listResource.stringValue()));
			ValueFactory vf = this.repository.getValueFactory();
			this.addRelationSynchronized(vf.createStatement(resource.resourceURI,
					resource instanceof Group ? RepositoryProperties.hasGroupMember : RepositoryProperties.hasListMember, this.getSesameEntryURI()), rc);
		}
	}

	protected void removeReferringList(ResourceImpl resource, RepositoryConnection rc) throws RepositoryException {
		synchronized (this.repository) {
			// rc.remove(entryURI, RepositoryProperties.referredIn, listResource, entryURI);
			// referredIn.remove(URI.create(listResource.stringValue()));
			ValueFactory vf = this.repository.getValueFactory();
			this.removeRelationSynchronized(vf.createStatement(resource.resourceURI,
					resource instanceof Group ? RepositoryProperties.hasGroupMember : RepositoryProperties.hasListMember, this.getSesameEntryURI()), rc);
		}
	}

	public void setEntryType(EntryType entryType) {
		checkAdministerRights();
		EntryType oldLT = locType;
		try {
			synchronized (this.repository) {
				RepositoryConnection rc = this.repository.getConnection();
				rc.begin();
				try {
					// we add a metadata triple, or we convert from Reference to LinkReference
					if (EntryType.Reference.equals(locType) && EntryType.LinkReference.equals(entryType)) {
						rc.add(entryURI, RepositoryProperties.metadata, this.localMdURI, entryURI);
					}
					setLocationType(entryType, rc); // this also sets locType, therefore the assignment in the catch clause
					registerEntryModified(rc, this.repository.getValueFactory());
					rc.commit();
				} catch (Exception e) {
					rc.rollback();
					locType = oldLT;
					log.error(e.getMessage());
					throw new org.entrystore.repository.RepositoryException("Error in repository connection.", e);
				} finally {
					rc.close();
				}
			}
		} catch (RepositoryException e) {
			log.error(e.getMessage());
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}
	}

	public void setResourceURI(URI resourceURI) {
		ValueFactory vf = getRepositoryManager().getValueFactory();
		IRI newResourceURI = vf.createIRI(resourceURI.toString());
		IRI oldResourceURI;

		try {
			// the whole rename holds the monitor, so a concurrent one starts from the URI this one committed
			synchronized (this.repository) {
				oldResourceURI = this.resURI;
				if (newResourceURI.equals(oldResourceURI)) {
					return;
				}
				checkAdministerRights();
				checkResourceURIChange(newResourceURI);

				// before the transaction, not outside the monitor: MetadataImpl.setGraph maintains other entries'
				// relation caches, and resURI is still the old one, so a failure below leaves the rename retryable
				renameInMetadata(getLocalMetadata(), oldResourceURI, newResourceURI);
				renameInMetadata(getCachedExternalMetadata(), oldResourceURI, newResourceURI);

				try (RepositoryConnection rc = this.repository.getConnection()) {
					rc.begin();
					// read here, since the metadata writes above also touched this graph; the graph type,
					// resource type and resource-level ACL hang off the resource URI as subject
					List<Statement> originalStatements = Iterations.asList(
							rc.getStatements(null, null, null, false, entryURI));
					try {
						Model newEntryGraph = renameResourceInEntryGraph(new LinkedHashModel(originalStatements),
								oldResourceURI, newResourceURI);
						removeInverseRelations(rc);
						rc.clear(entryURI);
						rc.add(newEntryGraph, entryURI);
						registerEntryModified(rc, vf);
						addInverseRelations(rc, newEntryGraph);

						IRI contextURI = vf.createIRI(this.getContext().getEntry().getResourceURI().toString());
						rc.remove(vf.createStatement(oldResourceURI, RepositoryProperties.resHasEntry, entryURI, contextURI));
						rc.add(vf.createStatement(newResourceURI, RepositoryProperties.resHasEntry, entryURI, contextURI));
						rc.commit();
					} catch (Exception e) {
						rollbackQuietly(rc, e);
						// removeInverseRelations and registerEntryModified changed cached fields before the commit
						loadFromStatements(originalStatements);
						throw new org.entrystore.repository.RepositoryException("Failed to move resource " + oldResourceURI + " to " + newResourceURI, e);
					}
					loadFromStatements(Iterations.asList(rc.getStatements(null, null, null, false, entryURI)));
					initMetadataObjects();
					// rebuilt, not nulled: it holds the URI it was built with, and remove() deletes the file through it
					if (resource instanceof DataImpl) {
						this.context.initResource(this);
					}
				}

				// inside the monitor like every index writer, so concurrent renames reach the index in commit order;
				// before the event, since a synchronous listener resolves related entries by resource URI
				this.context.updateResource2EntryIndex(
						URI.create(oldResourceURI.stringValue()),
						URI.create(newResourceURI.stringValue()),
						URI.create(this.entryURI.stringValue())
				);
			}
		} catch (RepositoryException e) {
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}

		getRepositoryManager().fireRepositoryEvent(new RepositoryEventObject(this, RepositoryEvent.EntryUpdated));
	}

	/**
	 * @throws IllegalArgumentException if this entry's resource URI cannot be changed, or not to {@code candidate}
	 */
	private void checkResourceURIChange(IRI candidate) {
		// a context's URI keys its index graph; a principal's URI is the object of every ACL naming it
		GraphType graphType = getGraphType();
		if (graphType == GraphType.Context || graphType == GraphType.SystemContext
			|| graphType == GraphType.User || graphType == GraphType.Group) {
			throw new IllegalArgumentException("The resource URI of a " + graphType + " cannot be changed");
		}
		// a local builtin resource keys its own graph, and a list its children's membership, by its URI;
		// a local file is stored by entry id
		if (locType == EntryType.Local && graphType != GraphType.None) {
			throw new IllegalArgumentException(
					"The resource URI of a local entry of graph type " + graphType + " cannot be changed");
		}
		// these name the entry's own graphs, whose statements a later rename would move as the resource's
		if (candidate.equals(entryURI) || candidate.equals(localMdURI) || candidate.equals(relationURI)
				|| candidate.equals(cachedExternalMdURI)) {
			throw new IllegalArgumentException(
					"The resource URI must differ from the entry's own entry, metadata and relation URIs");
		}
	}

	/**
	 * Moves the resource URI as subject, which carries the type and resource-level ACL triples, and as the object
	 * of es:resource. Every other object is left alone, as in 5.x: an external metadata URI may equal the resource
	 * URI, and only {@link #setExternalMetadataURI} changes it, together with its index.
	 */
	private static Model renameResourceInEntryGraph(Model entryGraph, IRI from, IRI to) {
		Model renamed = new LinkedHashModel();
		for (Statement statement : entryGraph) {
			org.eclipse.rdf4j.model.Resource subject =
					from.equals(statement.getSubject()) ? to : statement.getSubject();
			Value object = RepositoryProperties.resource.equals(statement.getPredicate())
					&& from.equals(statement.getObject()) ? to : statement.getObject();
			renamed.add(subject, statement.getPredicate(), object, statement.getContext());
		}
		return renamed;
	}

	/**
	 * Rewrites {@code from} as subject only, matching 5.x. Writes only when {@code from} is a subject, so an
	 * unrelated rename bumps no modified date and records no revision. A LocalMetadataWrapper is another entry's
	 * metadata and is left alone.
	 */
	private static void renameInMetadata(Metadata metadata, IRI from, IRI to) {
		if (!(metadata instanceof MetadataImpl metadataImpl)) {
			return;
		}
		Model graph = metadataImpl.getGraph();
		if (graph != null && graph.contains(from, null, null)) {
			metadataImpl.setGraphOfRenamedResource(ModelUtil.replaceSubject(graph, from, to), to);
		}
	}

	/** Rolls back without losing the failure that made it necessary. */
	static void rollbackQuietly(RepositoryConnection rc, Exception cause) {
		try {
			rc.rollback();
		} catch (RuntimeException e) {
			cause.addSuppressed(e);
		}
	}

	/** An IRI the store accepts is not always a java.net.URI, and the JDK's message must not reach the client. */
	private static URI toURI(IRI iri, String what) {
		try {
			return URI.create(iri.stringValue());
		} catch (IllegalArgumentException e) {
			throw new IllegalArgumentException("Invalid " + what + " in entry graph", e);
		}
	}

	public void setExternalMetadataURI(URI externalMetadataURI) {
		if (this.externalMdURI != null && externalMetadataURI.toString().equals(this.externalMdURI.toString())) {
			return;
		}

		checkAdministerRights();
		checkExternalMetadataURIChange(externalMetadataURI);

		ValueFactory vf = getRepositoryManager().getValueFactory();
		IRI oldExternalMetadataURI = vf.createIRI(getExternalMetadataURI().toString());
		IRI newExternalMetadataURI = vf.createIRI(externalMetadataURI.toString());

		// update entry graph
		Model entryGraph = getGraph();
		Model newEntryGraph = new LinkedHashModel();
		for (Statement statement : entryGraph) {
			if (statement.getSubject().equals(oldExternalMetadataURI)) {
				// we don't want to take the cached date into the new graph,
				// so we don't to anything here
			} else if (statement.getObject().equals(oldExternalMetadataURI)) {
				// replace object URI
				newEntryGraph.add(statement.getSubject(), statement.getPredicate(), newExternalMetadataURI);
			} else {
				// leave everything else untouched
				newEntryGraph.add(statement);
			}
		}

		// set entry graph
		setGraphRaw(newEntryGraph);

		// update index of context with new triple
		try {
			synchronized (this.repository) {
				try (RepositoryConnection rc = this.repository.getConnection()) {
					rc.begin();
					try {
						IRI contextURI = vf.createIRI(this.getContext().getEntry().getResourceURI().toString());
						rc.remove(vf.createStatement(oldExternalMetadataURI, RepositoryProperties.mdHasEntry, entryURI, contextURI));
						rc.add(vf.createStatement(newExternalMetadataURI, RepositoryProperties.mdHasEntry, entryURI, contextURI));
						rc.commit();
					} catch (Exception e) {
						rollbackQuietly(rc, e);
						throw new org.entrystore.repository.RepositoryException("Failed to move external metadata " + oldExternalMetadataURI + " to " + newExternalMetadataURI, e);
					}
				}
			}
		} catch (RepositoryException e) {
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}

		// update local variable with new URI
		this.externalMdURI = newExternalMetadataURI;

		// update index
		this.context.updateExternalMetadata2EntryIndex(
				URI.create(oldExternalMetadataURI.stringValue()),
				URI.create(this.externalMdURI.stringValue()),
				URI.create(this.entryURI.stringValue())
		);
	}

	/**
	 * Sets a location to the entry. If the entryType is Local no location is set.
	 *
	 * @param entryType
	 * @param rc
	 * @throws RepositoryException
	 */
	protected void setLocationType(EntryType entryType, RepositoryConnection rc) throws RepositoryException {
		rc.remove(rc.getStatements(entryURI, RDF.TYPE, null, false, entryURI), entryURI);
		switch (entryType) {
		case Reference:
			rc.add(entryURI, RDF.TYPE, RepositoryProperties.Reference, entryURI);
			break;
		case LinkReference:
			rc.add(entryURI, RDF.TYPE, RepositoryProperties.LinkReference, entryURI);
			break;
		case Link:
			rc.add(entryURI, RDF.TYPE, RepositoryProperties.Link, entryURI);
			break;
		}
		locType = entryType;
	}

	private Set<URI> getCachedAllowedPrincipalsFor(AccessProperty prop) {
		return switch (prop) {
			case Administer -> administerPrincipals;
			case ReadMetadata -> readMetadataPrincipals;
			case WriteMetadata -> writeMetadataPrincipals;
			case ReadResource -> readResourcePrincipals;
			case WriteResource -> writeResourcePrincipals;
		};
	}

	private void setCachedAllowedPrincipalsFor(AccessProperty prop, Set<URI> set) {
		switch (prop) {
			case Administer -> administerPrincipals = set;
			case ReadMetadata -> readMetadataPrincipals = set;
			case WriteMetadata -> writeMetadataPrincipals = set;
			case ReadResource -> readResourcePrincipals = set;
			case WriteResource -> writeResourcePrincipals = set;
		}
	}

	public Set<URI> getAllowedPrincipalsFor(AccessProperty prop) {
		// No access check since everyone, even guest should have access to read entry information.
		Set<URI> set = getCachedAllowedPrincipalsFor(prop);
		if (set != null) {
			return set;
		}

		try (RepositoryConnection rc = this.repository.getConnection()) {
			IRI subject = getAccessSubject(prop);
			IRI predicate = getAccessPredicate(prop);
			List<Statement> statements = Iterations.asList(rc.getStatements(subject, predicate, null, false, entryURI));
			set = new HashSet<>();
			for (Statement statement : statements) {
				if (statement.getObject() instanceof IRI) {
					set.add(URI.create(statement.getObject().stringValue()));
				}
			}
			setCachedAllowedPrincipalsFor(prop, set);
			return set;
		} catch (RepositoryException e) {
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}
	}

	public void setAllowedPrincipalsFor(AccessProperty prop, Set<URI> principals) {
		setAllowedPrincipalsFor(Map.of(prop, principals));
	}

	/**
	 * Replaces the ACL of each given property in one write, so {@link ListImpl#applyACLtoChildren} reposts each
	 * child once rather than once per property.
	 */
	void setAllowedPrincipalsFor(Map<AccessProperty, Set<URI>> acl) {
		checkAdministerRights();
		Map<AccessProperty, AclUpdate> updates = new EnumMap<>(AccessProperty.class);
		acl.forEach((prop, principals) -> updates.put(prop, replacing(toIRIs(principals))));
		updateAcl(updates, acl);
	}

	public void addAllowedPrincipalsFor(AccessProperty prop, URI principal) {
		checkAdministerRights();
		appendAllowedPrincipals(Map.of(prop, Set.of(principal)));
	}

	/**
	 * Adds principals to the ACL of each given property in one write, so {@link ContextImpl#copyACL} reposts the
	 * entry once, and not at all if every principal was already allowed. Has no Administer check of its own:
	 * {@link #addAllowedPrincipalsFor} checks before delegating here; copyACL cannot, since during creation the
	 * entry has no ACL yet and the check would fall back to the context ACL, denying the list-only writer that
	 * copyACL exists to admit.
	 */
	void appendAllowedPrincipals(Map<AccessProperty, Set<URI>> acl) {
		Map<AccessProperty, AclUpdate> updates = new EnumMap<>(AccessProperty.class);
		acl.forEach((prop, principals) -> {
			if (!principals.isEmpty()) {
				updates.put(prop, adding(toIRIs(principals)));
			}
		});
		updateAcl(updates, Map.of());
	}

	public boolean removeAllowedPrincipalsFor(AccessProperty prop, URI principal) {
		checkAdministerRights();
		IRI principalIRI = this.repository.getValueFactory().createIRI(principal.toString());
		AclUpdate removing = (rc, subject, predicate) -> {
			boolean listed = rc.hasStatement(subject, predicate, principalIRI, false, entryURI);
			rc.remove(subject, predicate, principalIRI, entryURI);
			return listed;
		};
		return updateAcl(Map.of(prop, removing), Map.of());
	}

	/** Converted before the transaction, so a malformed principal fails as bad input rather than as a store error. */
	private Set<IRI> toIRIs(Set<URI> principals) {
		ValueFactory vf = this.repository.getValueFactory();
		return principals.stream().map(principal -> vf.createIRI(principal.toString())).collect(Collectors.toSet());
	}

	/** Makes the property's ACL exactly the given principals. */
	private AclUpdate replacing(Set<IRI> principals) {
		return (rc, subject, predicate) -> {
			Set<Value> current = Iterations.asList(rc.getStatements(subject, predicate, null, false, entryURI))
					.stream().map(Statement::getObject).collect(Collectors.toSet());
			if (current.equals(principals)) {
				return false;
			}
			rc.remove(subject, predicate, null, entryURI);
			for (IRI principal : principals) {
				rc.add(subject, predicate, principal, entryURI);
			}
			return true;
		};
	}

	/** Adds the given principals that the property's ACL lacks. */
	private AclUpdate adding(Set<IRI> principals) {
		return (rc, subject, predicate) -> {
			boolean added = false;
			for (IRI principal : principals) {
				if (!rc.hasStatement(subject, predicate, principal, false, entryURI)) {
					rc.add(subject, predicate, principal, entryURI);
					added = true;
				}
			}
			return added;
		};
	}

	/**
	 * One property's ACL change against a connection already in a transaction that {@link #updateAcl} commits or
	 * rolls back; answers whether it changed the store.
	 */
	@FunctionalInterface
	private interface AclUpdate {
		boolean apply(RepositoryConnection rc, IRI subject, IRI predicate) throws RepositoryException;
	}

	/**
	 * Runs the ACL changes in one transaction under the repository monitor. If they changed the store, publishes
	 * them as {@link #setGraph} does: {@link RepositoryEvent#EntryUpdated}, plus
	 * {@link RepositoryEvent#EntryAclGuestUpdated} when a guest grant of a context entry changed. The events fire
	 * after the ACL cache is updated, since the Solr listener builds the document from it synchronously.
	 *
	 * @param cacheAfter what {@link #getAllowedPrincipalsFor} may answer per property once the changes are
	 * committed; a property it lacks reloads from the store.
	 * @return whether the changes altered the store
	 */
	private boolean updateAcl(Map<AccessProperty, AclUpdate> updates, Map<AccessProperty, Set<URI>> cacheAfter) {
		if (updates.isEmpty()) {
			return false;
		}
		Map<AccessProperty, Set<URI>> cached = new EnumMap<>(AccessProperty.class);
		cacheAfter.forEach((prop, principals) -> cached.put(prop, Set.copyOf(principals)));
		IRI guest = GraphType.Context.equals(getGraphType())
				? iri(getRepositoryManager().getPrincipalManager().getGuestUser().getURI().toString())
				: null;
		boolean changed = false;
		boolean guestChanged = false;
		try {
			synchronized (this.repository) {
				try (RepositoryConnection rc = this.repository.getConnection()) {
					rc.begin();
					try {
						for (Map.Entry<AccessProperty, AclUpdate> update : updates.entrySet()) {
							IRI subject = getAccessSubject(update.getKey());
							IRI predicate = getAccessPredicate(update.getKey());
							boolean guestBefore = guest != null
									&& rc.hasStatement(subject, predicate, guest, false, entryURI);
							changed |= update.getValue().apply(rc, subject, predicate);
							guestChanged |= guest != null
									&& guestBefore != rc.hasStatement(subject, predicate, guest, false, entryURI);
						}
						rc.commit();
					} catch (Exception e) {
						rollbackQuietly(rc, e);
						throw new org.entrystore.repository.RepositoryException("Error in repository connection.", e);
					}
					// after the commit, so a reader loading between invalidation and commit cannot pin the old
					// answer; one that loaded before the commit and stores after this line still can, and the
					// stale answer then survives until the next ACL write on this entry
					this.hasExplicitAcl = null;
					updates.keySet().forEach(prop -> setCachedAllowedPrincipalsFor(prop, cached.get(prop)));
				}
			}
		} catch (RepositoryException e) {
			log.error(e.getMessage(), e);
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}
		if (changed) {
			RepositoryManager rm = getRepositoryManager();
			rm.fireRepositoryEvent(new RepositoryEventObject(this, RepositoryEvent.EntryUpdated));
			if (guestChanged) {
				rm.fireRepositoryEvent(new RepositoryEventObject(this, RepositoryEvent.EntryAclGuestUpdated));
			}
		}
		return changed;
	}

	/** A copy of the entry's whole ACL, one principal set per property. */
	static Map<AccessProperty, Set<URI>> aclOf(Entry entry) {
		Map<AccessProperty, Set<URI>> acl = new EnumMap<>(AccessProperty.class);
		for (AccessProperty prop : AccessProperty.values()) {
			acl.put(prop, Set.copyOf(entry.getAllowedPrincipalsFor(prop)));
		}
		return acl;
	}

	public boolean hasAllowedPrincipals() {
		Boolean explicitAcl = this.hasExplicitAcl;
		if (explicitAcl == null) {
			try (RepositoryConnection rc = this.repository.getConnection()) {
				explicitAcl = rc.hasStatement(null, RepositoryProperties.Write, null, false, entryURI)
					|| rc.hasStatement(null, RepositoryProperties.Read, null, false, entryURI);
				this.hasExplicitAcl = explicitAcl;
			} catch (RepositoryException e) {
				throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
			}
		}
		return explicitAcl;
	}

	private IRI getAccessSubject(AccessProperty prop) {
		return switch (prop) {
			case Administer -> entryURI;
			case ReadMetadata, WriteMetadata -> getMetadataAccessSubject();
			case ReadResource, WriteResource -> resURI;
		};
	}

	/**
	 * The subject of the ReadMetadata/WriteMetadata ACL, which governs all metadata of the entry, local and cached
	 * external: {@code {context}/metadata/{id}} for every entry type, which is also where clients read and write it.
	 * A Reference loaded from the store has no {@link #localMdURI}, since it has no es:metadata; the URI is derived
	 * for it without setting that field, whose null-ness tells other code that the entry has no local metadata.
	 */
	private IRI getMetadataAccessSubject() {
		IRI mdURI = localMdURI;
		if (mdURI != null) {
			return mdURI;
		}
		return repository.getValueFactory().createIRI(URISplit.createURI(repositoryManager.getRepositoryURL().toString(),
				context.id, RepositoryProperties.MD_PATH, id).toString());
	}

	private IRI getAccessPredicate(AccessProperty prop) {
		return switch (prop) {
			case Administer, WriteMetadata, WriteResource -> RepositoryProperties.Write;
			case ReadResource, ReadMetadata -> RepositoryProperties.Read;
		};
	}

	public void setGraphType(GraphType gt) {
		checkAdministerRights();
		if (this.graphType != gt && this.locType == EntryType.Local) {
			throw new org.entrystore.repository.RepositoryException("Cannot change the graph type of a local resource");
		}
		try {
			synchronized (this.repository) {
				RepositoryConnection rc = this.repository.getConnection();
				rc.begin();
				try {
					setGraphType(gt, rc);
					registerEntryModified(rc, this.repository.getValueFactory());
					rc.commit();
				} catch (Exception e) {
					rc.rollback();
					throw new org.entrystore.repository.RepositoryException("Error in repository connection.", e);
				} finally {
					rc.close();
				}
			}
		} catch (RepositoryException e) {
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}

	}

	/**
	 * If this method is called successfully (no Exception thrown), and for some reason
	 * the transaction is rolled back, than the cached built-in type will be wrong,
	 * call {@link #refreshFromRepository(RepositoryConnection)} to correct this.
	 *
	 * @param gt the new {@link org.entrystore.GraphType}
	 * @param rc a RepositoryConnection
	 * @throws RepositoryException
	 */
	protected void setGraphType(GraphType gt, RepositoryConnection rc) throws RepositoryException {
		List<Statement> statements = Iterations.asList(rc.getStatements(resURI, RDF.TYPE, null, false, entryURI));
		for (Statement statement : statements) {
			if (getGraphType(statement.getObject()) != null) {
				rc.remove(statement, entryURI);
			}
		}
		switch (gt) {
			case Context -> rc.add(resURI, RDF.TYPE, RepositoryProperties.Context, entryURI);
			case SystemContext -> rc.add(resURI, RDF.TYPE, RepositoryProperties.SystemContext, entryURI);
			case List -> rc.add(resURI, RDF.TYPE, RepositoryProperties.List, entryURI);
			case ResultList -> rc.add(resURI, RDF.TYPE, RepositoryProperties.ResultList, entryURI);
			case User -> rc.add(resURI, RDF.TYPE, RepositoryProperties.User, entryURI);
			case Group -> rc.add(resURI, RDF.TYPE, RepositoryProperties.Group, entryURI);
			case Pipeline -> rc.add(resURI, RDF.TYPE, RepositoryProperties.Pipeline, entryURI);
			case PipelineResult -> rc.add(resURI, RDF.TYPE, RepositoryProperties.PipelineResult, entryURI);
			case String -> rc.add(resURI, RDF.TYPE, RepositoryProperties.String, entryURI);
			case Graph -> rc.add(resURI, RDF.TYPE, RepositoryProperties.Graph, entryURI);
		}

		graphType = gt;
	}

	public void setResourceType(ResourceType resType) {
		checkAdministerRights();
		if (this.repType != resType && this.locType == EntryType.Local && (this.graphType != GraphType.None && this.graphType != GraphType.Pipeline && this.graphType != GraphType.PipelineResult)) {
			throw new org.entrystore.repository.RepositoryException("Cannot change the resource type of a local and/or built-in resource");
		}
		try {
			synchronized (this.repository) {
				RepositoryConnection rc = this.repository.getConnection();
				rc.begin();
				try {
					setResourceType(resType, rc);
					registerEntryModified(rc, this.repository.getValueFactory());
					rc.commit();
				} catch (Exception e) {
					rc.rollback();
					throw new org.entrystore.repository.RepositoryException("Error in repository connection.", e);
				} finally {
					rc.close();
				}
			}
		} catch (RepositoryException e) {
			log.error(e.getMessage(), e);
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository", e);
		}
	}

	protected void setResourceType(ResourceType resType, RepositoryConnection rc) throws RepositoryException {
		List<Statement> statements = Iterations.asList(rc.getStatements(resURI, RDF.TYPE, null, false, entryURI));
		for (Statement statement : statements) {
			if (getResourceType(statement.getObject()) != null) {
				rc.remove(statement, entryURI);
			}
		}

		switch (resType) {
			case ResolvableInformationResource ->
					rc.add(resURI, RDF.TYPE, RepositoryProperties.ResolvableInformationResource, entryURI);
			case Unknown -> rc.add(resURI, RDF.TYPE, RepositoryProperties.Unknown, entryURI);
			case NamedResource -> rc.add(resURI, RDF.TYPE, RepositoryProperties.NamedResource, entryURI);
		}
		repType = resType;
	}

	protected void updateModifiedDateSynchronized(RepositoryConnection rc, ValueFactory vf) throws RepositoryException {
		synchronized (this.repository) {
			registerEntryModified(rc, vf);
		}
	}

	protected void registerEntryModified(RepositoryConnection rc, ValueFactory vf) throws RepositoryException {
		try {
			modified = DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
		} catch (DatatypeConfigurationException e) {
			log.error(e.getMessage());
		}
		rc.remove(rc.getStatements(entryURI, RepositoryProperties.Modified, null, false, entryURI), entryURI);
		rc.add(entryURI, RepositoryProperties.Modified, vf.createLiteral(modified), entryURI);

		//Also adding the one who updates using dcterms:contributor
		if (this.repositoryManager != null &&
				this.repositoryManager.getPrincipalManager() != null &&
				this.repositoryManager.getPrincipalManager().getAuthenticatedUserURI() != null) {
			URI contrib = this.repositoryManager.getPrincipalManager().getAuthenticatedUserURI();
			String contributor = contrib.toString();
			IRI contributorURI = vf.createIRI(contributor);

		    //Do not add if the contributor is the same as the creator
		    if (!contrib.equals(this.getCreator()) && !contributors.contains(contributorURI)) {
				rc.add(this.entryURI, RepositoryProperties.Contributor, contributorURI, this.entryURI);
		    	contributors.add(contributorURI);
		    }
		}
	}

	public void updateModificationDate() {
		try (RepositoryConnection rc = repository.getConnection()) {
			this.updateModifiedDateSynchronized(rc, getRepositoryManager().getValueFactory());
		}
	}

	public void updateCachedExternalMetadataDateSynchronized(RepositoryConnection rc, ValueFactory vf) throws RepositoryException, DatatypeConfigurationException {
		synchronized (this.repository) {
			if (this.getEntryType() == EntryType.Reference || this.getEntryType() == EntryType.LinkReference) {
				cachedAt = DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
				rc.remove(rc.getStatements(cachedExternalMdURI, RepositoryProperties.cached, null, false, entryURI), entryURI);
				// TODO: maybe entryUri as context last parameter..
				rc.add(cachedExternalMdURI, RepositoryProperties.cached, vf.createLiteral(cachedAt), entryURI);
			}
			registerEntryModified(rc, vf);
		}
	}

	/**
	 * WARNING, do not use unless you are very certain that you know what you are doing.
	 * Replaces the entryinformation without questions asked.
     * The new graph should not change anything that affects inverse-relation-cache on other entries, if so, use setGraph instead.
     *
	 * @param metametadata
	 */
	protected void setGraphRaw(Model metametadata) {
		checkAdministerRights();
		try {
			synchronized (this.repository) {
				RepositoryConnection rc = this.repository.getConnection();
				ValueFactory vf = this.repository.getValueFactory();
				rc.begin();
				try {
					rc.clear(entryURI);
					rc.add(metametadata, entryURI);
					registerEntryModified(rc, vf);
					rc.commit();

					// we reload the internal cache
					loadFromStatements(Iterations.asList(rc.getStatements(null, null, null, false, entryURI)));
					initMetadataObjects();

					getRepositoryManager().fireRepositoryEvent(new RepositoryEventObject(this, RepositoryEvent.EntryUpdated));
				} catch (Exception e) {
					rc.rollback();
					// Reset to previous saved values, just in case we saved the types above halfway through.
					loadFromStatements(Iterations.asList(rc.getStatements(null, null, null, false, entryURI)));
					rc.close();
					throw new org.entrystore.repository.RepositoryException("Error in repository connection.", e);
				} finally {
					rc.close();
				}
			}
		} catch (RepositoryException e) {
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}
	}

	public void setGraph(Model submitted) {
		checkAdministerRights();
		synchronized (this.repository) {
			setGraphSynchronized(submitted);
		}
	}

	/**
	 * Holds the repository monitor from reading the current resource URI to the final write, so a concurrent
	 * rename cannot change the URI the submitted graph was rewritten to.
	 */
	private void setGraphSynchronized(Model submitted) {
		Model oldGraph = getGraph();

		// a client echoes the old resource URI as subject of the type and ACL triples it PUTs back
		Model metametadata = submitted;
		URI newResourceURI = null;
		IRI oldResourceURI = this.resURI;
		Iterator<Statement> resourceURIStmnts = submitted.filter(this.entryURI, RepositoryProperties.resource, null).iterator();
		if (resourceURIStmnts.hasNext() && resourceURIStmnts.next().getObject() instanceof IRI newResourceIRI
				&& !newResourceIRI.equals(oldResourceURI)) {
			newResourceURI = toURI(newResourceIRI, "resource URI");
			metametadata = renameResourceInEntryGraph(submitted, oldResourceURI, newResourceIRI);
		}

		URI newExternalMetadataURI = null;
		Iterator<Statement> externalMdURIStmnts =
				metametadata.filter(this.entryURI, RepositoryProperties.externalMetadata, null).iterator();
		if (externalMdURIStmnts.hasNext() && externalMdURIStmnts.next().getObject() instanceof IRI externalMdIRI
				&& (externalMdURI == null || !externalMdIRI.stringValue().equals(externalMdURI.stringValue()))) {
			newExternalMetadataURI = toURI(externalMdIRI, "external metadata URI");
			// Validated before anything is changed, so that a rejected URI leaves the entry unchanged. An unchanged
			// URI is not validated, so that entries created before the validation can still be modified.
			checkExternalMetadataURIChange(newExternalMetadataURI);
		}

		if (newResourceURI != null) {
			setResourceURI(newResourceURI);
		}
		if (newExternalMetadataURI != null) {
			setExternalMetadataURI(newExternalMetadataURI);
		}
		String originalList = this.getOriginalList();

		try {
			synchronized (this.repository) {
				ValueFactory vf = this.repository.getValueFactory();
				RepositoryConnection rc = this.repository.getConnection();
				rc.begin();
				Model minimalProvenanceGraph = null;
				if (this.provenance != null) {
					minimalProvenanceGraph = this.provenance.getMinimalGraph(rc);
				}

				try {
                    removeInverseRelations(rc);
					rc.clear(entryURI);

					for (Statement statement : metametadata) {
						IRI predicate = statement.getPredicate();
						if (predicate.stringValue().startsWith(RepositoryProperties.NSbase)) {
							//Ignore basic structure subgraph, will be added below.
							if (!(predicate.equals(RepositoryProperties.resource)
									|| predicate.equals(RepositoryProperties.metadata)
									|| predicate.equals(RepositoryProperties.externalMetadata)
									|| predicate.equals(RepositoryProperties.cachedExternalMetadata)
									|| predicate.equals(RepositoryProperties.cached)
									|| predicate.equals(RepositoryProperties.relation)
									//|| predicate.equals(RepositoryProperties.referredIn)
									)) {
								rc.add(statement, entryURI);
							}
						} else if (this.entryURI.equals(statement.getSubject())
								&& (predicate.equals(RepositoryProperties.Created)
										|| predicate.equals(RepositoryProperties.Modified)
										|| predicate.equals(RepositoryProperties.Creator)
										|| predicate.equals(RepositoryProperties.Contributor))) {
							//In the basic structure below.
						} else if (predicate.equals(RDF.TYPE)) {
							if (this.entryURI.equals(statement.getSubject())) {
								EntryType lt = getEntryType(statement.getObject());
								if (lt == EntryType.LinkReference && Arrays.asList(EntryType.Reference, EntryType.Link).contains(locType)) {
									if (locType == EntryType.Reference) {
										localMdURI = vf.createIRI(URISplit.createURI(repositoryManager.getRepositoryURL().toString(), context.id, RepositoryProperties.MD_PATH, this.id).toString());
									} else if (locType == EntryType.Link) {
										cachedExternalMdURI = vf.createIRI(URISplit.createURI(repositoryManager.getRepositoryURL().toString(), context.id, RepositoryProperties.EXTERNAL_MD_PATH, this.id).toString());
									}
									locType = lt;
								}
							} else {
								GraphType gt = getGraphType(statement.getObject());
								if (gt != null) {
									if (locType != EntryType.Local) { //Only allowed to change builtintype for non-local resources.
										this.graphType = gt;
									}
								} else {
									ResourceType rt = getResourceType(statement.getObject());
									if (rt != null) {
										if (locType != EntryType.Local) { //Only allowed to change representationtype for non-local resources.
											repType = rt;
										}
									} else { //Some other rdf:type, just add it.
										rc.add(statement, entryURI);
									}
								}
							}
						} else if (this.provenance != null
								&& this.provenance.hasProvenanceCharacter(statement)) {
							//Filter out provenance as it will be added back in a controlled manner below
						} else {
							rc.add(statement, entryURI);
						}
					}

					//----------Start basic structure:
					//Since we cleared the previous graph, we set the types again, they might have been updated in the loop above.
					setLocationType(this.locType, rc);
					setGraphType(this.graphType, rc);
					setResourceType(this.repType, rc);

					rc.add(entryURI, RepositoryProperties.resource, getSesameResourceURI(), entryURI);
					rc.add(entryURI, RepositoryProperties.relation, this.relationURI, entryURI);
					if (localMdURI != null) {
						rc.add(entryURI, RepositoryProperties.metadata, localMdURI, entryURI);
					}

					if (externalMdURI != null) {
						rc.add(entryURI, RepositoryProperties.externalMetadata, externalMdURI, entryURI);
					}
					if (cachedExternalMdURI != null) {
						rc.add(entryURI, RepositoryProperties.cachedExternalMetadata, cachedExternalMdURI, entryURI);
						if (cachedAt != null) {
							rc.add(cachedExternalMdURI, RepositoryProperties.cached, vf.createLiteral(cachedAt), entryURI);
						}
					}
/*					for (URI refIn : this.referredIn) { //Adds the referredIn relations to the graph.
						rc.add(entryURI, RepositoryProperties.referredIn, vf.createIRI(refIn.toString()), entryURI);
					}*/

					if (created != null) {
						rc.add(entryURI, RepositoryProperties.Created, vf.createLiteral(created), entryURI);
					}

					if (creator != null) {
						rc.add(entryURI, RepositoryProperties.Creator, creator, entryURI);
					}

					if (contributors != null && !contributors.isEmpty()) {
						for (IRI contrib : contributors) {
							rc.add(entryURI, RepositoryProperties.Contributor, contrib, entryURI);
						}
					}

					if (originalList != null) {
						rc.add(entryURI, RepositoryProperties.originallyCreatedIn, vf.createIRI(originalList), entryURI);
					}

					modified = DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
					rc.add(entryURI, RepositoryProperties.Modified, vf.createLiteral(modified), entryURI);
					if (minimalProvenanceGraph != null) {
						rc.add(minimalProvenanceGraph, entryURI);
					}
					//------------End basic structure

                    addInverseRelations(rc, metametadata);
					rc.commit();
					administerPrincipals = null;
					readMetadataPrincipals = null;
					writeMetadataPrincipals = null;
					readResourcePrincipals = null;
					writeResourcePrincipals = null;
					hasExplicitAcl = null;
					format = null;
					status = null;

					// we reload the internal cache
					loadFromStatements(Iterations.asList(rc.getStatements(null, null, null, false, entryURI)));
					initMetadataObjects();
					getRepositoryManager().fireRepositoryEvent(new RepositoryEventObject(this, RepositoryEvent.EntryUpdated));
					if (GraphType.Context.equals(this.getGraphType())) {
						if (hasAclChangedForGuest(oldGraph, metametadata)) {
							getRepositoryManager().fireRepositoryEvent(new RepositoryEventObject(this, RepositoryEvent.EntryAclGuestUpdated, metametadata));
						}
						if (hasProjectTypeChanged(oldGraph, metametadata)) {
							getRepositoryManager().fireRepositoryEvent(new RepositoryEventObject(this, RepositoryEvent.EntryProjectTypeUpdated, metametadata));
						}
					}
				} catch (Exception e) {
					rc.rollback();
					// Reset to previous saved values, just in case we saved the types above halfway through.
					loadFromStatements(Iterations.asList(rc.getStatements(null, null, null, false, entryURI)));
					throw new org.entrystore.repository.RepositoryException("Error in repository connection.", e);
				} finally {
					rc.close();
				}
			}
		} catch (RepositoryException e) {
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}
	}

	private boolean hasAclChangedForGuest(Model oldGraph, Model newGraph) {
		if (oldGraph == null || newGraph == null) {
			throw new IllegalArgumentException("Parameters must not be null");
		}

		Model oldModel = new LinkedHashModel(oldGraph);
		Model newModel = new LinkedHashModel(newGraph);

		IRI guestURI = getRepositoryManager().getValueFactory().createIRI(getRepositoryManager().getPrincipalManager().getGuestUser().getURI().toString());

		Model oldAcl = new LinkedHashModel();
		oldAcl.addAll(oldModel.filter(null, RepositoryProperties.Read, guestURI));
		oldAcl.addAll(oldModel.filter(null, RepositoryProperties.Write, guestURI));

		Model newAcl = new LinkedHashModel();
		newAcl.addAll(newModel.filter(null, RepositoryProperties.Read, guestURI));
		newAcl.addAll(newModel.filter(null, RepositoryProperties.Write, guestURI));

		return !Models.isomorphic(oldAcl, newAcl);
	}

	private boolean hasProjectTypeChanged(Model oldGraph, Model newGraph) {
		if (oldGraph == null || newGraph == null) {
			throw new IllegalArgumentException("Parameters must not be null");
		}
		IRI projectTypeProperty = iri("http://entryscape.com/terms/projectType");
		return !Models.isomorphic(oldGraph.filter(null, projectTypeProperty, null), newGraph.filter(null, projectTypeProperty, null));
	}

	/** Package-private for {@code ContextRemover}, which removes these copies without loading the entries. */
	static boolean isStatementInvRelationCandidate(Statement statement, String base) {
		IRI predicate = statement.getPredicate();
        if (!predicate.equals(RepositoryProperties.resource)
                && !predicate.equals(RepositoryProperties.metadata)
                && !predicate.equals(RepositoryProperties.externalMetadata)
                && !predicate.equals(RepositoryProperties.cachedExternalMetadata)
                && !predicate.equals(RepositoryProperties.relation)
                && !predicate.equals(RepositoryProperties.cached)
                && !predicate.equals(RepositoryProperties.Creator)
                && !predicate.equals(RepositoryProperties.Contributor)
                && !predicate.equals(RepositoryProperties.Read)
                && !predicate.equals(RepositoryProperties.Write)
//                && !predicate.equals(RepositoryProperties.Pipeline)
                && !predicate.equals(RepositoryProperties.originallyCreatedIn)) {
            Value obj = statement.getObject();
            org.eclipse.rdf4j.model.Resource subj = statement.getSubject();
            //Check for relations between this resource and another entry (resourceURI (has to be a repository resource), metadataURI, or entryURI)
			return obj instanceof IRI
                    && obj.stringValue().startsWith(base)
				&& subj.stringValue().startsWith(base);
        }
        return false;
    }

	private void removeInverseRelations(RepositoryConnection rc) throws RepositoryException {
		if (invRelations) {
			try (RepositoryResult<Statement> rr = rc.getStatements(null, null, null, false, entryURI)) {
				String base = repositoryManager.getRepositoryURL().toString();
				for (Statement statement : rr) {
					if (isStatementInvRelationCandidate(statement, base)) {
						EntryImpl sourceEntry = relationTarget(repositoryManager, statement);
						if (sourceEntry != null && sourceEntry != this) {
							sourceEntry.removeRelationSynchronized(statement, rc);
						}
					}
				}
			}
			invRelations = false;
		}
	}

    private void addInverseRelations(RepositoryConnection rc, Model graph) {
        String base = repositoryManager.getRepositoryURL().toString();
        for (Statement statement : graph) {
            if (isStatementInvRelationCandidate(statement, base)) {
				EntryImpl sourceEntry = relationTarget(repositoryManager, statement);
                if (sourceEntry != null && sourceEntry != this) {
                    sourceEntry.addRelationSynchronized(statement, rc);
                }
            }
        }
    }

	/**
	 * Validates an external metadata URI for this entry, see {@link #checkExternalMetadataURI(URI, URI, URL)}.
	 */
	private void checkExternalMetadataURI(URI externalMetadataURI) {
		checkExternalMetadataURI(externalMetadataURI, URI.create(entryURI.stringValue()),
				repositoryManager.getRepositoryURL());
	}

	/**
	 * Validates a change of the external metadata URI to the given one. Only an existing external metadata URI
	 * can be replaced, so an entry that has none is rejected, e.g. a Local or Link entry, or a Link that was
	 * converted to a LinkReference.
	 *
	 * @throws InvalidExternalMetadataURIException if the entry has no external metadata URI, or the new one is
	 *                                             not acceptable, see {@link #checkExternalMetadataURI(URI, URI, URL)}
	 */
	private void checkExternalMetadataURIChange(URI newExternalMetadataURI) {
		if (externalMdURI == null) {
			throw new InvalidExternalMetadataURIException("The entry " + entryURI.stringValue()
					+ " has no external metadata URI that could be changed");
		}
		checkExternalMetadataURI(newExternalMetadataURI);
	}

	/**
	 * Validates the external metadata URI of a Reference or LinkReference entry. A URI in the repository, i.e.
	 * one that starts with its base URL, gets its metadata from the local metadata of the entry it denotes (see
	 * {@link LocalMetadataWrapper}), so it must be a URI that the repository can split, and must not denote the
	 * entry itself: the entry would be the source of its own cached external metadata. The base URL followed only
	 * by query parameters, e.g. a search URL, does not denote an entry and is accepted as before; its metadata is
	 * an empty graph. A URI of another system is not restricted; its metadata is cached in the entry.
	 * <p>
	 * The check for the entry itself compares the entry URI that {@link URISplit} derives from the URI, so it
	 * detects every URI that splits into the entry's context and ID: its entry, resource, metadata and cached
	 * external metadata URIs, also with a trailing slash. It does not detect URIs that denote the entry over HTTP
	 * but split differently, e.g. with a query string appended to the ID ({@code .../metadata/2?}), or with
	 * another host name of the same server. Such URIs are only kept from recursing by {@link LocalMetadataWrapper}
	 * resolving the referenced entry lazily.
	 *
	 * @param entryURI the URI of the entry that the external metadata URI is set for
	 * @throws SelfReferencingExternalMetadataException if the URI belongs to the entry itself
	 * @throws InvalidExternalMetadataURIException if the URI is in the repository but cannot be split, e.g. the
	 *                                             base URL itself or an entry path without an entry ID
	 */
	public static void checkExternalMetadataURI(URI externalMetadataURI, URI entryURI, URL repositoryURL) {
		if (!externalMetadataURI.toString().startsWith(repositoryURL.toString())) {
			return;
		}
		URISplit split;
		try {
			split = new URISplit(externalMetadataURI, repositoryURL);
		} catch (IllegalArgumentException e) {
			throw new InvalidExternalMetadataURIException("The external metadata URI " + externalMetadataURI
					+ " is in this repository but does not denote an entry");
		}
		if (split.getEntryURI().equals(Optional.of(entryURI))) {
			throw new SelfReferencingExternalMetadataException(externalMetadataURI, entryURI);
		}
	}

    private void checkAdministerRights() {
		PrincipalManager pm = this.getRepositoryManager().getPrincipalManager();
		pm.checkAuthenticatedUserAuthorized(this, AccessProperty.Administer);
	}

	public Context getContext() {
		return context;
	}

	public RepositoryManager getRepositoryManager() {
		return repositoryManager;
	}

	@Override
	public Provenance getProvenance() {
		return this.provenance;
	}

	public Resource getResource() {
		if (resource == null) {
			ContextImpl contextImpl = ((ContextImpl) this.getContext());
			try {
				contextImpl.initResource(this);
			} catch (RepositoryException e) {
				log.error(e.getMessage());
			}
		}
		return resource;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		return entryURI != null
				&& obj instanceof Entry entry
				&& !(entry instanceof EntryImpl other && other.entryURI == null)
				&& getEntryURI().equals(entry.getEntryURI());
	}

	@Override
	public int hashCode() {
		return entryURI == null ? 0 : getEntryURI().hashCode();
	}

	public boolean isExternalMetadataCached() {
		return getExternalMetadataCacheDate() != null;
	}

	public Metadata getLocalMetadata() {
		if (localMetadata == null) {
			initMetadataObjects();
		}

		return localMetadata;
	}

	public Model getMetadataGraph() {
		if (getEntryType().equals(EntryType.Local) || getEntryType().equals(EntryType.Link)) {
			if (getLocalMetadata() != null && getLocalMetadata().getGraph() != null) {
				return getLocalMetadata().getGraph();
			}
		} else if (getEntryType().equals(EntryType.Reference)) {
			if (getCachedExternalMetadata() != null && getCachedExternalMetadata().getGraph() != null) {
				return getCachedExternalMetadata().getGraph();
			}
		} else if (getEntryType().equals(EntryType.LinkReference)) {
			Model mergedMd = new LinkedHashModel();
			if (getLocalMetadata() != null && getLocalMetadata().getGraph() != null) {
				mergedMd.addAll(getLocalMetadata().getGraph());
			}
			if (getCachedExternalMetadata() != null && getCachedExternalMetadata().getGraph() != null) {
				mergedMd.addAll(getCachedExternalMetadata().getGraph());
			}
			return mergedMd;
		}
		return new LinkedHashModel();
	}

	/** Flags an entry removed without {@link #remove(RepositoryConnection)}: with its context, or by a purge. */
	void markDeleted() {
		deleted = true;
	}

	/**
	 * Removes the entry within the supplied transaction, except a data file, which is deleted at once and not restored
	 * by a rollback; see {@link EntryPurge} for removing many at once.
	 */
	public void remove(RepositoryConnection rc) throws Exception {
		// TODO the handling of removal is non-atomic and should be rewritten to take
		//  failures (i.e. rollbacks of the ongoing transaction) into consideration
		deleted = true;

		log.debug("Removing entry {}", entryURI);
        removeInverseRelations(rc);
		rc.clear(entryURI);
		if ((locType == EntryType.Local) || (locType == EntryType.Link) || (locType == EntryType.LinkReference)) {
			localMetadata.removeGraphSynchronized(rc);
		}

		if ((locType == EntryType.LinkReference) || (locType == EntryType.Reference)) {
            if (cachedExternalMetadata instanceof MetadataImpl impl) {
                impl.removeGraphSynchronized(rc);
            } else {
                rc.clear(cachedExternalMdURI);
            }
		}

		if (relationURI != null) {
			rc.clear(relationURI);
		}

		localMetadata = null;
		cachedExternalMetadata = null;

		if (resource != null && resource.isRemovable()) {
			resource.remove(rc);
			resource = null;
		}
	}

	public String getFilename() {
		if (this.filename == null) {
			Statement st = getStatement(resURI, RepositoryProperties.filename, null);
			if (st != null) {
				this.filename = st.getObject().stringValue();
			}
		}
		return this.filename;
	}

	public URI getRelationURI() {
		return URI.create(relationURI.toString());
	}

	public void setFilename(String name) {
		PrincipalManager pm = this.getRepositoryManager().getPrincipalManager();
		pm.checkAuthenticatedUserAuthorized(this, AccessProperty.WriteResource);

		ValueFactory vf = this.repository.getValueFactory();
		if (replaceStatement(resURI, RepositoryProperties.filename, vf.createLiteral(name))) {
			this.filename = name;
		}
	}

	public long getFileSize() {
		if (this.fileSize < 0) {
			Statement st = getStatement(resURI, RepositoryProperties.fileSize, null);
			if (st != null) {
				this.fileSize = ((Literal) st.getObject()).longValue();
			}
		}
		return this.fileSize;
	}

	public void setFileSize(long size) {
		PrincipalManager pm = this.getRepositoryManager().getPrincipalManager();
		pm.checkAuthenticatedUserAuthorized(this, AccessProperty.WriteResource);

		ValueFactory vf = this.repository.getValueFactory();
		if (replaceStatement(resURI, RepositoryProperties.fileSize, vf.createLiteral(size))) {
			this.fileSize = size;
		}
	}

	public URI getStatus() {
		if (this.status == null) {
			// the mime type in the local MD overwrites the mime type in the entry graph
			Statement st = getStatement(entryURI, RepositoryProperties.status, null);
			if (st != null) {
				this.status = URI.create(st.getObject().stringValue());
			}
		}
		return this.status;
	}

	public void setStatus(URI newStatus) {
		checkAdministerRights();
		ValueFactory vf = this.repository.getValueFactory();
		if (replaceStatement(entryURI, RepositoryProperties.status, vf.createIRI(newStatus.toString()))) {
			this.status = newStatus;
		}
	}

	public String getMimetype() {
		if (this.format == null) {
			// the mime type in the local MD overwrites the mime type in the entry graph
			String mtMd = getMimetypeFromMetadata();
			if (mtMd != null) {
				this.format = mtMd;
				return this.format;
			}
			Statement st = getStatement(resURI, RepositoryProperties.format, null);
			if (st != null) {
				this.format = st.getObject().stringValue();
			}
		}
		return this.format;
	}

	public void setMimetype(String mt) {
		PrincipalManager pm = this.getRepositoryManager().getPrincipalManager();
		pm.checkAuthenticatedUserAuthorized(this, AccessProperty.WriteResource);

		ValueFactory vf = this.repository.getValueFactory();
		if (replaceStatement(resURI, RepositoryProperties.format, vf.createLiteral(mt))) {
			this.format = mt;
		}

		// if the mime-type is set (overwritten) in the metadata, we take that one instead
		String mtMd = getMimetypeFromMetadata();
		if (mtMd != null) {
			this.format = mtMd;
		}
	}

	private String getMimetypeFromMetadata() {
		Statement st = getStatementFromLocalMetadata(resURI, RepositoryProperties.format, null);
		if (st != null) {
			Value obj = st.getObject();
			if (obj.isLiteral()) {
				return obj.stringValue();
			}
		}
		return null;
	}

	public boolean replaceStatementSynchronized(IRI subject, IRI predicate, Value object, RepositoryConnection rc, ValueFactory vf) throws RepositoryException {
		rc.remove(subject, predicate, null, entryURI);
		rc.add(subject, predicate, object, entryURI);
		registerEntryModified(rc, vf);
		return true;
	}

	private boolean replaceStatementSynchronized(IRI subject, IRI predicate, Value object) {
		try {
			RepositoryConnection rc = this.repository.getConnection();
			rc.begin();
			try {
				boolean result = this.replaceStatementSynchronized(subject, predicate, object, rc, this.repository.getValueFactory());
				rc.commit();
				getRepositoryManager().fireRepositoryEvent(new RepositoryEventObject(this, RepositoryEvent.EntryUpdated));
				return result;
			} catch (Exception e) {
				rc.rollback();
				throw new org.entrystore.repository.RepositoryException("Error in repository connection.", e);
			} finally {
				rc.close();
			}
		} catch (RepositoryException e) {
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}
	}

	private boolean replaceStatement(IRI subject, IRI predicate, Value object) {
		synchronized (this.repository) {
			return this.replaceStatementSynchronized(subject, predicate, object);
		}
	}

	public Statement getStatement(IRI subject, IRI predicate, Value object) {
		try {
			RepositoryConnection rc = this.repository.getConnection();
			try {
				RepositoryResult<Statement> matches = rc.getStatements(subject, predicate, object, false, entryURI);
				if (matches.hasNext()) {
					Statement result = matches.next();
					matches.close();
					return result;
				}
				if (!matches.isClosed()) {
					matches.close();
				}
				rc.close();
				return null;
			} catch (org.eclipse.rdf4j.repository.RepositoryException e) {
				rc.close();
				log.error(e.getMessage());
				throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
			} finally {
				rc.close();
			}
		} catch (RepositoryException e) {
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}
	}

	public Statement getStatementFromLocalMetadata(IRI subject, IRI predicate, Value object) {
		if (localMdURI == null) {
			return null;
		}

		try {
			RepositoryConnection rc = this.repository.getConnection();
			try {
				RepositoryResult<Statement> matches = rc.getStatements(subject, predicate, object, false, localMdURI);
				if (matches.hasNext()) {
					Statement result = matches.next();
					matches.close();
					return result;
				}
				if (!matches.isClosed()) {
					matches.close();
				}
				rc.close();
				return null;
			} catch (org.eclipse.rdf4j.repository.RepositoryException e) {
				rc.close();
				log.error(e.getMessage());
				throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
			} finally {
				rc.close();
			}
		} catch (RepositoryException e) {
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}
	}

	/**
	 * @return the original list where the entry was created, null if the creator was the owner of
	 * the current context or has since added it to another list or removed it from all lists.
	 */
	public String getOriginalList() {
		if (this.originalList == null) {
			Statement st = this.getStatement(this.entryURI, RepositoryProperties.originallyCreatedIn, null);
			if (st != null && st.getObject() instanceof IRI) {
				this.originalList = st.getObject().stringValue();
			} else {
				this.originalList = "";
			}
		}
		if (this.originalList.isEmpty()) {
			return null;
		}
		return this.originalList;
	}

	public void setOriginalListSynchronized(String list, RepositoryConnection rc, ValueFactory vf) throws RepositoryException {
		rc.remove(this.entryURI, RepositoryProperties.originallyCreatedIn, null, this.entryURI);
		if (list != null) {
			rc.add(this.entryURI, RepositoryProperties.originallyCreatedIn, vf.createIRI(list), this.entryURI);
			this.originalList = list;
		} else {
			this.originalList = "";
		}
	}

	public void setOriginalListSynchronized(String list) {
		try {
			RepositoryConnection rc = this.repository.getConnection();
			rc.begin();
			try {
				rc.remove(this.entryURI, RepositoryProperties.originallyCreatedIn, null, this.entryURI);
				if (list != null) {
					rc.add(this.entryURI, RepositoryProperties.originallyCreatedIn, this.repository.getValueFactory().createIRI(list), this.entryURI);
					this.originalList = list;
				} else {
					this.originalList = "";
				}
				rc.commit();
			} catch (Exception e) {
				rc.rollback();
				throw new org.entrystore.repository.RepositoryException("Error in repository connection.", e);
			} finally {
				rc.close();
			}
		} catch (RepositoryException e) {
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}
	}

	public Model getRelations() {
		try (RepositoryConnection rc = this.repository.getConnection()) {
			return Iterations.addAll(rc.getStatements(null, null, null, false, this.relationURI), new LinkedHashModel());
		} catch (RepositoryException e) {
			log.error(e.getMessage());
			throw new org.entrystore.repository.RepositoryException("Failed to connect to Repository.", e);
		}
	}

	/**
	 * The entry the object of {@code relation} denotes, looked up without checking ReadMetadata: inverse relations are
	 * bookkeeping, kept also on entries the current user may not read.
	 */
	static EntryImpl relationTarget(RepositoryManager rm, Statement relation) {
		return (EntryImpl) ((ContextManagerImpl) rm.getContextManager())
				.getEntryIgnoreACL(URI.create(relation.getObject().stringValue()));
	}

	protected void addRelationSynchronized(Statement statement, RepositoryConnection rc) {
		synchronized (this) {
			addRelation(statement, rc);
		}
	}

	private void addRelation(Statement statement, RepositoryConnection rc) {
		try {
			rc.add(statement, relationURI);
		} catch (RepositoryException e) {
			log.error(e.getMessage());
			throw new org.entrystore.repository.RepositoryException("Failed to connect to repository", e);
		}
	}

	protected void removeRelationSynchronized(Statement statement, RepositoryConnection rc) {
		synchronized (this) {
			removeRelation(statement, rc);
		}
	}

	private void removeRelation(Statement statement, RepositoryConnection rc) {
		try {
			rc.remove(statement, relationURI);
		} catch (RepositoryException e) {
			log.error(e.getMessage());
			throw new org.entrystore.repository.RepositoryException("Failed to connect to repository", e);
		}
	}

	@Override
	public String toString() {
		return entryURI.toString() + "," + super.toString();
	}

}