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