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.Transaction;
026import org.fcrepo.kernel.api.exception.AccessDeniedException;
027import org.fcrepo.kernel.api.exception.ItemNotFoundException;
028import org.fcrepo.kernel.api.exception.MalformedRdfException;
029import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
030import org.fcrepo.kernel.api.identifiers.FedoraId;
031import org.fcrepo.kernel.api.services.ReplacePropertiesService;
032import org.fcrepo.kernel.api.services.UpdatePropertiesService;
033import org.fcrepo.persistence.api.PersistentStorageSessionManager;
034import org.fcrepo.persistence.api.exceptions.PersistentItemNotFoundException;
035import org.fcrepo.persistence.api.exceptions.PersistentStorageException;
036import org.springframework.stereotype.Component;
037
038import javax.inject.Inject;
039
040import static org.fcrepo.kernel.api.RdfCollectors.toModel;
041
042/**
043 * This class implements the update properties operation.
044 *
045 * @author dbernstein
046 */
047@Component
048public class UpdatePropertiesServiceImpl extends AbstractService implements UpdatePropertiesService {
049
050    @Inject
051    private ReplacePropertiesService replacePropertiesService;
052
053    @Inject
054    private PersistentStorageSessionManager persistentStorageSessionManager;
055
056    @Override
057    public void updateProperties(final Transaction tx, final String userPrincipal,
058                                 final FedoraId fedoraId, final String sparqlUpdateStatement)
059            throws MalformedRdfException, AccessDeniedException {
060        final var txId = tx.getId();
061        try {
062            final var psession = persistentStorageSessionManager.getSession(txId);
063            final var triples = psession.getTriples(fedoraId, null);
064            final Model model = triples.collect(toModel());
065            final UpdateRequest request = UpdateFactory.create(sparqlUpdateStatement, fedoraId.getFullId());
066            UpdateAction.execute(request, model);
067            replacePropertiesService.perform(tx, userPrincipal, fedoraId, model);
068        } catch (final PersistentItemNotFoundException ex) {
069            throw new ItemNotFoundException(ex.getMessage(), ex);
070        } catch (final PersistentStorageException ex) {
071            throw new RepositoryRuntimeException(ex.getMessage(), ex);
072        }
073
074    }
075}