001/*
002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 */
017
018package org.nuxeo.ecm.directory.impl;
019
020import java.util.regex.Pattern;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.PropertyException;
026import org.nuxeo.ecm.directory.BaseSession;
027import org.nuxeo.ecm.directory.Directory;
028import org.nuxeo.ecm.directory.DirectoryException;
029import org.nuxeo.ecm.directory.EntryAdaptor;
030
031/**
032 * Simple entry adaptor implementation that leaves the entry as editable if the specified field value matches the
033 * provided regexp or set the readonly flag of the entry to true if the value of the field does not match the regexp.
034 * <p>
035 * In any case, if the readonly flag of the adapted entry is already set to true, this value is kept unchanged.
036 */
037public class WritePolicyEntryAdaptor implements EntryAdaptor {
038
039    public static final Log log = LogFactory.getLog(WritePolicyEntryAdaptor.class);
040
041    protected String fieldName;
042
043    protected Pattern pattern;
044
045    public DocumentModel adapt(Directory directory, DocumentModel entry) throws DirectoryException {
046        if (fieldName == null || pattern == null) {
047            log.warn(getClass().getName() + " is missing configuration parameters");
048            return entry;
049        }
050        if (BaseSession.isReadOnlyEntry(entry)) {
051            // keep already existing flag
052            return entry;
053        }
054        try {
055            Object fieldValue = entry.getProperty(directory.getSchema(), fieldName);
056            String value = fieldValue != null ? fieldValue.toString() : "";
057            if (pattern.matcher(value).matches()) {
058                BaseSession.setReadWriteEntry(entry);
059            } else {
060                BaseSession.setReadOnlyEntry(entry);
061            }
062        } catch (PropertyException e) {
063            throw new DirectoryException(
064                    String.format(
065                            "The field '%s' of entry '%s' could not be adapt and map on directory '%s', check that the field exist in the schema",
066                            fieldName, entry.getId(), directory.getName()), e);
067
068        }
069        return entry;
070    }
071
072    public void setParameter(String name, String value) {
073        if ("fieldName".equals(name)) {
074            fieldName = value;
075        } else if ("regexp".equals(name)) {
076            pattern = Pattern.compile(value);
077        } else {
078            log.warn("unexpected parameter " + name + " for class " + getClass().getName());
079        }
080    }
081
082}