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