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 public DocumentModel adapt(Directory directory, DocumentModel entry) throws DirectoryException { 048 if (fieldName == null || pattern == null) { 049 log.warn(getClass().getName() + " is missing configuration parameters"); 050 return entry; 051 } 052 if (BaseSession.isReadOnlyEntry(entry)) { 053 // keep already existing flag 054 return entry; 055 } 056 try { 057 Object fieldValue = entry.getProperty(directory.getSchema(), fieldName); 058 String value = fieldValue != null ? fieldValue.toString() : ""; 059 if (pattern.matcher(value).matches()) { 060 BaseSession.setReadWriteEntry(entry); 061 } else { 062 BaseSession.setReadOnlyEntry(entry); 063 } 064 } catch (PropertyException e) { 065 throw new DirectoryException( 066 String.format( 067 "The field '%s' of entry '%s' could not be adapt and map on directory '%s', check that the field exist in the schema", 068 fieldName, entry.getId(), directory.getName()), e); 069 070 } 071 return entry; 072 } 073 074 public void setParameter(String name, String value) { 075 if ("fieldName".equals(name)) { 076 fieldName = value; 077 } else if ("regexp".equals(name)) { 078 pattern = Pattern.compile(value); 079 } else { 080 log.warn("unexpected parameter " + name + " for class " + getClass().getName()); 081 } 082 } 083 084}