001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.kernel.impl.observer;
017
018import com.google.common.base.Function;
019import org.fcrepo.kernel.observer.FedoraEvent;
020import org.slf4j.Logger;
021
022import javax.jcr.NamespaceRegistry;
023import javax.jcr.RepositoryException;
024import javax.jcr.Session;
025
026import static org.fcrepo.kernel.utils.NamespaceTools.getNamespaceRegistry;
027import static org.slf4j.LoggerFactory.getLogger;
028
029/**
030 * @author Andrew Woods
031 * @since 11/22/14
032 */
033public class GetNamespacedProperties implements Function<FedoraEvent, FedoraEvent> {
034
035    private static final Logger LOGGER = getLogger(SimpleObserver.class);
036
037    private Session session;
038
039    /**
040     * Constructor
041     *
042     * @param session used to get NamespaceRegistry
043     */
044    public GetNamespacedProperties(final Session session) {
045        this.session = session;
046    }
047
048    @Override
049    public FedoraEvent apply(final FedoraEvent evt) {
050        final NamespaceRegistry namespaceRegistry = getNamespaceRegistry(session);
051
052        final FedoraEvent event = new FedoraEvent(evt);
053        for (String property : evt.getProperties()) {
054            final String[] parts = property.split(":", 2);
055            if (parts.length == 2) {
056                final String prefix = parts[0];
057                try {
058                    event.addProperty(namespaceRegistry.getURI(prefix) + parts[1]);
059                } catch (RepositoryException ex) {
060                    LOGGER.trace("Prefix could not be dereferenced using the namespace registry: {}", property);
061                    event.addProperty(property);
062                }
063            } else {
064                event.addProperty(property);
065            }
066        }
067
068        for (Integer type : evt.getTypes()) {
069            event.addType(type);
070        }
071        return event;
072    }
073
074}