001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.ecm.directory.impl;
021
022import java.util.regex.Pattern;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.PropertyException;
028import org.nuxeo.ecm.directory.BaseSession;
029import org.nuxeo.ecm.directory.Directory;
030import org.nuxeo.ecm.directory.DirectoryException;
031import org.nuxeo.ecm.directory.EntryAdaptor;
032
033/**
034 * Simple entry adaptor implementation that leaves the entry as editable if the specified field value matches the
035 * provided regexp or set the readonly flag of the entry to true if the value of the field does not match the regexp.
036 * <p>
037 * In any case, if the readonly flag of the adapted entry is already set to true, this value is kept unchanged.
038 */
039public class WritePolicyEntryAdaptor implements EntryAdaptor {
040
041    public static final Log log = LogFactory.getLog(WritePolicyEntryAdaptor.class);
042
043    protected String fieldName;
044
045    protected Pattern pattern;
046
047    @Override
048    public DocumentModel adapt(Directory directory, DocumentModel entry) throws DirectoryException {
049        if (fieldName == null || pattern == null) {
050            log.warn(getClass().getName() + " is missing configuration parameters");
051            return entry;
052        }
053        if (BaseSession.isReadOnlyEntry(entry)) {
054            // keep already existing flag
055            return entry;
056        }
057        try {
058            Object fieldValue = entry.getProperty(directory.getSchema(), fieldName);
059            String value = fieldValue != null ? fieldValue.toString() : "";
060            if (pattern.matcher(value).matches()) {
061                BaseSession.setReadWriteEntry(entry);
062            } else {
063                BaseSession.setReadOnlyEntry(entry);
064            }
065        } catch (PropertyException e) {
066            throw new DirectoryException(
067                    String.format(
068                            "The field '%s' of entry '%s' could not be adapt and map on directory '%s', check that the field exist in the schema",
069                            fieldName, entry.getId(), directory.getName()), e);
070
071        }
072        return entry;
073    }
074
075    @Override
076    public void setParameter(String name, String value) {
077        if ("fieldName".equals(name)) {
078            fieldName = value;
079        } else if ("regexp".equals(name)) {
080            pattern = Pattern.compile(value);
081        } else {
082            log.warn("unexpected parameter " + name + " for class " + getClass().getName());
083        }
084    }
085
086}