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