|
| 1 | +/* |
| 2 | + * Copyright (C) 2020-2026 Linagora |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General |
| 5 | + * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) |
| 6 | + * any later version, provided you comply with the Additional Terms applicable for LinID Identity Manager software by |
| 7 | + * LINAGORA pursuant to Section 7 of the GNU Affero General Public License, subsections (b), (c), and (e), pursuant to |
| 8 | + * which these Appropriate Legal Notices must notably (i) retain the display of the "LinID™" trademark/logo at the top |
| 9 | + * of the interface window, the display of the “You are using the Open Source and free version of LinID™, powered by |
| 10 | + * Linagora © 2009–2013. Contribute to LinID R&D by subscribing to an Enterprise offer!” infobox and in the e-mails |
| 11 | + * sent with the Program, notice appended to any type of outbound messages (e.g. e-mail and meeting requests) as well |
| 12 | + * as in the LinID Identity Manager user interface, (ii) retain all hypertext links between LinID Identity Manager |
| 13 | + * and https://linid.org/, as well as between LINAGORA and LINAGORA.com, and (iii) refrain from infringing LINAGORA |
| 14 | + * intellectual property rights over its trademarks and commercial brands. Other Additional Terms apply, see |
| 15 | + * <http://www.linagora.com/licenses/> for more details. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 18 | + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
| 19 | + * details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Affero General Public License and its applicable Additional Terms for |
| 22 | + * LinID Identity Manager along with this program. If not, see <http://www.gnu.org/licenses/> for the GNU Affero |
| 23 | + * General Public License version 3 and <http://www.linagora.com/licenses/> for the Additional Terms applicable to the |
| 24 | + * LinID Identity Manager software. |
| 25 | + */ |
| 26 | + |
| 27 | +package io.github.linagora.linid.im.emtp; |
| 28 | + |
| 29 | +import io.github.linagora.linid.im.corelib.exception.ApiException; |
| 30 | +import io.github.linagora.linid.im.corelib.i18n.I18nMessage; |
| 31 | +import io.github.linagora.linid.im.corelib.plugin.config.JinjaService; |
| 32 | +import io.github.linagora.linid.im.corelib.plugin.config.dto.TaskConfiguration; |
| 33 | +import io.github.linagora.linid.im.corelib.plugin.entity.DynamicEntity; |
| 34 | +import io.github.linagora.linid.im.corelib.plugin.task.TaskExecutionContext; |
| 35 | +import io.github.linagora.linid.im.corelib.plugin.task.TaskPlugin; |
| 36 | +import java.util.Map; |
| 37 | +import lombok.NonNull; |
| 38 | +import org.springframework.beans.factory.annotation.Autowired; |
| 39 | +import org.springframework.stereotype.Component; |
| 40 | +import tools.jackson.core.type.TypeReference; |
| 41 | + |
| 42 | +/** |
| 43 | + * Task plugin that maps values from the execution context into a dynamic entity. |
| 44 | + * |
| 45 | + * <p>This plugin uses templates defined in the task configuration to populate entity attributes dynamically. |
| 46 | + * It supports tasks of type "entity-mapping". |
| 47 | + */ |
| 48 | +@Component |
| 49 | +public class EntityMappingTaskPlugin implements TaskPlugin { |
| 50 | + |
| 51 | + /** |
| 52 | + * Service used to render Jinja templates with the task execution context and entity attributes. |
| 53 | + */ |
| 54 | + private final JinjaService jinjaService; |
| 55 | + |
| 56 | + /** |
| 57 | + * Constructs an EntityMappingTaskPlugin with the specified JinjaService. |
| 58 | + * |
| 59 | + * @param jinjaService the service responsible for rendering Jinja templates |
| 60 | + */ |
| 61 | + @Autowired |
| 62 | + public EntityMappingTaskPlugin(final JinjaService jinjaService) { |
| 63 | + this.jinjaService = jinjaService; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public boolean supports(final @NonNull String type) { |
| 68 | + return "entity-mapper".equals(type); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void execute(final TaskConfiguration taskConfiguration, |
| 73 | + final DynamicEntity dynamicEntity, |
| 74 | + final TaskExecutionContext taskExecutionContext) { |
| 75 | + Map<String, String> mapping = taskConfiguration.getOption( |
| 76 | + "mapping", |
| 77 | + new TypeReference<Map<String, String>>() {}) |
| 78 | + .orElseThrow(() -> new ApiException(500, I18nMessage.of( |
| 79 | + "error.plugin.default.missing.option", |
| 80 | + Map.of("option", "mapping") |
| 81 | + ))); |
| 82 | + |
| 83 | + mapping.forEach((String key, String template) -> { |
| 84 | + String value = jinjaService.render(taskExecutionContext, dynamicEntity, template); |
| 85 | + dynamicEntity.getAttributes().put(key, value); |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
0 commit comments