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