001
002/*
003 * The contents of this file are subject to the license and copyright
004 * detailed in the LICENSE and NOTICE files at the root of the source
005 * tree.
006 */
007package org.fcrepo.kernel.impl.services;
008
009import static org.fcrepo.kernel.api.RdfCollectors.toModel;
010
011import javax.inject.Inject;
012
013import org.fcrepo.kernel.api.Transaction;
014import org.fcrepo.kernel.api.exception.AccessDeniedException;
015import org.fcrepo.kernel.api.exception.ItemNotFoundException;
016import org.fcrepo.kernel.api.exception.MalformedRdfException;
017import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
018import org.fcrepo.kernel.api.identifiers.FedoraId;
019import org.fcrepo.kernel.api.services.ReplacePropertiesService;
020import org.fcrepo.kernel.api.services.UpdatePropertiesService;
021import org.fcrepo.persistence.api.PersistentStorageSessionManager;
022import org.fcrepo.persistence.api.exceptions.PersistentItemNotFoundException;
023import org.fcrepo.persistence.api.exceptions.PersistentStorageException;
024
025import org.apache.jena.rdf.model.Model;
026import org.apache.jena.update.UpdateAction;
027import org.apache.jena.update.UpdateFactory;
028import org.apache.jena.update.UpdateRequest;
029import org.springframework.stereotype.Component;
030
031/**
032 * This class implements the update properties operation.
033 *
034 * @author dbernstein
035 */
036@Component
037public class UpdatePropertiesServiceImpl extends AbstractService implements UpdatePropertiesService {
038
039    @Inject
040    private ReplacePropertiesService replacePropertiesService;
041
042    @Inject
043    private PersistentStorageSessionManager persistentStorageSessionManager;
044
045    @Override
046    public void updateProperties(final Transaction tx, final String userPrincipal,
047                                 final FedoraId fedoraId, final String sparqlUpdateStatement)
048            throws MalformedRdfException, AccessDeniedException {
049        try {
050            final var psession = persistentStorageSessionManager.getSession(tx);
051            final var triples = psession.getTriples(fedoraId, null);
052            final Model model = triples.collect(toModel());
053            final UpdateRequest request = UpdateFactory.create(sparqlUpdateStatement, fedoraId.getFullDescribedId());
054            UpdateAction.execute(request, model);
055            replacePropertiesService.perform(tx, userPrincipal, fedoraId, model);
056        } catch (final PersistentItemNotFoundException ex) {
057            throw new ItemNotFoundException(ex.getMessage(), ex);
058        } catch (final PersistentStorageException ex) {
059            throw new RepositoryRuntimeException(ex.getMessage(), ex);
060        }
061
062    }
063}