001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
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-2.1.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 *     Thomas Roger <troger@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.automation.core.operations.services.directory;
019
020import java.io.IOException;
021import java.io.StringWriter;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.codehaus.jackson.map.ObjectMapper;
026import org.codehaus.jackson.type.TypeReference;
027import org.nuxeo.ecm.automation.OperationContext;
028import org.nuxeo.ecm.automation.core.Constants;
029import org.nuxeo.ecm.automation.core.annotations.Context;
030import org.nuxeo.ecm.automation.core.annotations.Operation;
031import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
032import org.nuxeo.ecm.automation.core.annotations.Param;
033import org.nuxeo.ecm.core.api.Blob;
034import org.nuxeo.ecm.core.api.Blobs;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.schema.SchemaManager;
037import org.nuxeo.ecm.core.schema.types.Schema;
038import org.nuxeo.ecm.directory.Directory;
039import org.nuxeo.ecm.directory.Session;
040import org.nuxeo.ecm.directory.api.DirectoryService;
041
042/**
043 * Delete entries for a given {@link org.nuxeo.ecm.directory.Directory}.
044 * <p>
045 * Entries ids to delete are sent through a JSON array.
046 * <p>
047 * Instead of being deleted, the entries can be marked as obsolete using the {@code markObsolete} parameter.
048 * <p>
049 * Returns deleted, or marked as obsolete, entries id as a JSON array.
050 *
051 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
052 * @since 5.7
053 */
054@Operation(id = DeleteDirectoryEntries.ID, category = Constants.CAT_SERVICES, label = "Deletes directory entries", description = "Deletes directory entries. Entries ids to delete are sent through a JSON array. Returns deleted entries id as a JSON array.", addToStudio = false)
055public class DeleteDirectoryEntries extends AbstractDirectoryOperation {
056
057    public static final String ID = "Directory.DeleteEntries";
058
059    @Context
060    protected OperationContext ctx;
061
062    @Context
063    protected DirectoryService directoryService;
064
065    @Context
066    protected SchemaManager schemaManager;
067
068    @Param(name = "directoryName", required = true)
069    protected String directoryName;
070
071    @Param(name = "entries", required = true)
072    protected String jsonEntries;
073
074    @Param(name = "markObsolete", required = false)
075    protected boolean markObsolete = false;
076
077    @OperationMethod
078    public Blob run() throws IOException {
079        validateCanManageDirectories(ctx);
080
081        ObjectMapper mapper = new ObjectMapper();
082
083        List<String> entries = mapper.readValue(jsonEntries, new TypeReference<List<String>>() {
084        });
085        List<String> ids = new ArrayList<String>();
086        try (Session session = directoryService.open(directoryName)) {
087            for (String entryId : entries) {
088                if (markObsolete) {
089                    markObsoleteOrDelete(session, entryId);
090                } else {
091                    session.deleteEntry(entryId);
092                }
093                ids.add(entryId);
094            }
095        }
096
097        StringWriter writer = new StringWriter();
098        mapper.writeValue(writer, ids);
099        return Blobs.createBlob(writer.toString(), "application/json");
100    }
101
102    protected void markObsoleteOrDelete(Session session, String id) {
103        Directory directory = directoryService.getDirectory(directoryName);
104        String schemaName = directory.getSchema();
105        Schema schema = schemaManager.getSchema(schemaName);
106        if (schema.hasField("obsolete")) {
107            DocumentModel entry = session.getEntry(id);
108            if (entry != null) {
109                entry.setProperty(schemaName, "obsolete", 1);
110                session.updateEntry(entry);
111            }
112        } else {
113            session.deleteEntry(id);
114        }
115    }
116
117}