001/* 002 * (C) Copyright 2009 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 * Anahide Tchertchian 018 */ 019package org.nuxeo.ecm.directory; 020 021import java.io.Serializable; 022import java.util.HashMap; 023import java.util.Map; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.ecm.core.api.DocumentModelList; 028import org.nuxeo.ecm.directory.DirectoryException; 029import org.nuxeo.ecm.directory.Session; 030import org.nuxeo.ecm.directory.api.DirectoryService; 031import org.nuxeo.ecm.directory.impl.AbstractDirectoryDeleteConstraint; 032 033/** 034 * Delete constraint preventing from removing an entry from a parent directory if it is referenced in a child directory. 035 * <p> 036 * Needs to know the child directory name and the field where parent entry id is declared on it. 037 * 038 * @author Anahide Tchertchian 039 */ 040public class HierarchicalDirectoryDeleteConstraint extends AbstractDirectoryDeleteConstraint { 041 042 private static final long serialVersionUID = 1L; 043 044 private static final Log log = LogFactory.getLog(HierarchicalDirectoryDeleteConstraint.class); 045 046 protected String targetDirectory; 047 048 protected String targetDirectoryField; 049 050 @Override 051 public void setProperties(Map<String, String> properties) { 052 String targetDirKey = "targetDirectory"; 053 String targetDirFieldKey = "targetDirectoryField"; 054 if (properties == null) { 055 throw new DirectoryException(String.format("This delete constraint requires properties '%s' and '%s'", 056 targetDirKey, targetDirFieldKey)); 057 } 058 if (!properties.containsKey(targetDirKey)) { 059 throw new DirectoryException(String.format("This delete constraint requires property '%s'", targetDirKey)); 060 } 061 if (!properties.containsKey(targetDirFieldKey)) { 062 throw new DirectoryException(String.format("This delete constraint requires property '%s'", 063 targetDirFieldKey)); 064 } 065 targetDirectory = properties.get(targetDirKey); 066 targetDirectoryField = properties.get(targetDirFieldKey); 067 } 068 069 public boolean canDelete(DirectoryService dirService, String entryId) { 070 try (Session dirSession = dirService.open(targetDirectory)) { 071 // search for given entry id usage in this directory 072 Map<String, Serializable> filter = new HashMap<String, Serializable>(); 073 filter.put(targetDirectoryField, entryId); 074 DocumentModelList res = dirSession.query(filter); 075 if (res.isEmpty()) { 076 return true; 077 } 078 if (log.isDebugEnabled()) { 079 log.debug("Can not delete " + targetDirectory + " " + entryId + ", constraint on " 080 + targetDirectoryField + ":" + res.get(0).getId()); 081 } 082 return false; 083 } 084 } 085 086}