001
002/*
003 * Licensed to DuraSpace under one or more contributor license agreements.
004 * See the NOTICE file distributed with this work for additional information
005 * regarding copyright ownership.
006 *
007 * DuraSpace licenses this file to you under the Apache License,
008 * Version 2.0 (the "License"); you may not use this file except in
009 * compliance with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.fcrepo.kernel.impl.services;
020
021import org.apache.jena.rdf.model.Model;
022import org.apache.jena.update.UpdateAction;
023import org.apache.jena.update.UpdateFactory;
024import org.apache.jena.update.UpdateRequest;
025import org.fcrepo.kernel.api.exception.AccessDeniedException;
026import org.fcrepo.kernel.api.exception.ItemNotFoundException;
027import org.fcrepo.kernel.api.exception.MalformedRdfException;
028import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
029import org.fcrepo.kernel.api.identifiers.FedoraId;
030import org.fcrepo.kernel.api.services.ReplacePropertiesService;
031import org.fcrepo.kernel.api.services.UpdatePropertiesService;
032import org.fcrepo.persistence.api.PersistentStorageSessionManager;
033import org.fcrepo.persistence.api.exceptions.PersistentItemNotFoundException;
034import org.fcrepo.persistence.api.exceptions.PersistentStorageException;
035import org.springframework.stereotype.Component;
036
037import javax.inject.Inject;
038
039import static org.fcrepo.kernel.api.RdfCollectors.toModel;
040
041/**
042 * This class implements the update properties operation.
043 *
044 * @author dbernstein
045 */
046@Component
047public class UpdatePropertiesServiceImpl extends AbstractService implements UpdatePropertiesService {
048
049    @Inject
050    private ReplacePropertiesService replacePropertiesService;
051
052    @Inject
053    private PersistentStorageSessionManager persistentStorageSessionManager;
054
055    @Override
056    public void updateProperties(final String txId, final String userPrincipal,
057                                 final FedoraId fedoraId, final String sparqlUpdateStatement)
058            throws MalformedRdfException, AccessDeniedException {
059        try {
060            final var psession = persistentStorageSessionManager.getSession(txId);
061            final var triples = psession.getTriples(fedoraId, null);
062            final Model model = triples.collect(toModel());
063            final UpdateRequest request = UpdateFactory.create(sparqlUpdateStatement, fedoraId.getFullId());
064            UpdateAction.execute(request, model);
065            replacePropertiesService.perform(txId, userPrincipal, fedoraId, model);
066        } catch (final PersistentItemNotFoundException ex) {
067            throw new ItemNotFoundException(ex.getMessage(), ex);
068        } catch (final PersistentStorageException ex) {
069            throw new RepositoryRuntimeException(ex.getMessage(), ex);
070        }
071
072    }
073}