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