001/*
002 * (C) Copyright 2016-2018 Nuxeo (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 *     Thibaud Arguillere
018 *     Miguel Nixo
019 */
020package org.nuxeo.ecm.automation.core.operations.services.directory;
021
022import static org.apache.commons.lang3.StringUtils.defaultIfEmpty;
023
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.commons.lang3.StringUtils;
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.schema.SchemaManager;
034import org.nuxeo.ecm.core.schema.types.Schema;
035import org.nuxeo.ecm.directory.Session;
036import org.nuxeo.ecm.directory.api.DirectoryService;
037
038/**
039 * @since 8.3 Adds a new entry to a vocabulary.
040 *        <p>
041 *        Notice: This is for a nuxeo Vocabulary, which is a specific kind of Directory. This code expects the
042 *        following:
043 *        <ul>
044 *        <li>The vocabulary schema <i>must</i> have <code>id</code>, <code>label</code>, <code>obsolete</code> and
045 *        <code>ordering</code> fields</li>
046 *        <li>If it is hierarchical, it must also have the <code>parent</code> field</li>
047 *        </ul>
048 */
049@Operation(id = CreateVocabularyEntry.ID, category = Constants.CAT_SERVICES, label = "Vocabulary: Add Entry", description = "Add a new entry in the <i>vocabularyName</i> vocabulary only if <i>id</i> is not found (an existing entry is"
050        + "not updated). If <i>label</i> is empty, it is set to the id. WARNING: Current user must have enough rights "
051        + "to write in a vocabulary.")
052public class CreateVocabularyEntry {
053
054    public static final String ID = "Directory.CreateVocabularyEntry";
055
056    @Context
057    protected DirectoryService directoryService;
058
059    @Context
060    protected SchemaManager schemaManager;
061
062    @Param(name = "vocabularyName")
063    protected String name;
064
065    @Param(name = "id")
066    protected String id;
067
068    @Param(name = "label", required = false)
069    protected String label;
070
071    @Param(name = "parent", required = false)
072    protected String parent = "";
073
074    @Param(name = "obsolete", required = false)
075    protected long obsolete;
076
077    @Param(name = "ordering", required = false)
078    protected long ordering;
079
080    @OperationMethod
081    public void run() {
082
083        if (StringUtils.isBlank(id)) {
084            return;
085        }
086
087        try (Session directorySession = directoryService.open(name)) {
088            if (directorySession.hasEntry(id)) {
089                return;
090            }
091            Map<String, Object> entry = new HashMap<>();
092            entry.put("id", id);
093            entry.put("label", defaultIfEmpty(label, id));
094            String dirSchema = directoryService.getDirectorySchema(name);
095            Schema schema = schemaManager.getSchema(dirSchema);
096            if (schema.hasField("parent")) {
097                entry.put("parent", parent);
098            }
099            entry.put("obsolete", obsolete);
100            entry.put("ordering", ordering);
101            directorySession.createEntry(entry);
102        }
103
104    }
105
106}