diff --git a/src/main/java/sirius/biz/jobs/batch/ImportBatchProcessFactory.java b/src/main/java/sirius/biz/jobs/batch/ImportBatchProcessFactory.java index a12171653..22be8673f 100644 --- a/src/main/java/sirius/biz/jobs/batch/ImportBatchProcessFactory.java +++ b/src/main/java/sirius/biz/jobs/batch/ImportBatchProcessFactory.java @@ -40,10 +40,19 @@ public String queueName() { /** * Permits selecting the {@link sirius.biz.scripting.ScriptableEventDispatcher} to use for this import. */ - public static final Parameter DISPATCHER_PARAMETER; + public static final Parameter DISPATCHER_PARAMETER = createDispatcherParameter(); - static { - SelectStringParameter dispatcherParameter = new SelectStringParameter("eventDispatcher", "$ImportBatchProcessFactory.eventDispatcher"); + @Part + private ScriptableEvents scriptableEvents; + + /** + * Creates a new instance of the dispatcher parameter. + * + * @return a new instance of the dispatcher parameter + */ + public static Parameter createDispatcherParameter() { + SelectStringParameter dispatcherParameter = + new SelectStringParameter("eventDispatcher", "$ImportBatchProcessFactory.eventDispatcher"); dispatcherParameter.markRequired(); dispatcherParameter.withDescription("$ImportBatchProcessFactory.eventDispatcher.help"); dispatcherParameter.withEntriesProvider(() -> { @@ -51,24 +60,35 @@ public String queueName() { ScriptableEvents scriptableEvents = Injector.context().getPart(ScriptableEvents.class); if (scriptableEvents != null) { scriptableEvents.fetchDispatchersForCurrentTenant() - .forEach(dispatcher -> eventDispatchers.put(dispatcher, dispatcher)); + .forEach(dispatcher -> eventDispatchers.put(dispatcher, dispatcher)); } return eventDispatchers; }); + dispatcherParameter.hideWhen((parameter, context) -> { + return parameter.getValues().size() < 2; + }); - DISPATCHER_PARAMETER = dispatcherParameter.build(); + return dispatcherParameter.build(); } - @Part - private ScriptableEvents scriptableEvents; + /** + * Determines if scriptable events are enabled for this factory. + *

+ * Disabled by default. Override this method to enable scriptable events where needed. + * + * @return true if scriptable events should be enabled, false otherwise + */ + protected boolean enableScriptableEvents() { + return false; + } @Override protected abstract ImportJob createJob(ProcessContext process); @Override protected void collectParameters(Consumer> parameterCollector) { - if (scriptableEvents.fetchDispatchersForCurrentTenant().size() > 1) { - parameterCollector.accept(DISPATCHER_PARAMETER); + if (enableScriptableEvents()) { + parameterCollector.accept(createDispatcherParameter()); } } diff --git a/src/main/java/sirius/biz/scripting/ScriptableEventDispatcherRepository.java b/src/main/java/sirius/biz/scripting/ScriptableEventDispatcherRepository.java index 351a4d904..edd2b0105 100644 --- a/src/main/java/sirius/biz/scripting/ScriptableEventDispatcherRepository.java +++ b/src/main/java/sirius/biz/scripting/ScriptableEventDispatcherRepository.java @@ -11,7 +11,6 @@ import sirius.kernel.di.std.AutoRegister; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.List; import java.util.Optional; @@ -37,8 +36,7 @@ public interface ScriptableEventDispatcherRepository { * @param tenantId the tenant for which to fetch the dispatcher * @param name the name of the dispatcher to fetch * @return the dispatcher with the given name for the given tenant wrapped as optional or an empty optional if - * no such dispatcher exists. NOTE: if an empty name is given, the first dispatcher for the given tenant - * is used. This helps to simplify the usage of custom events. + * no such dispatcher exists. */ - Optional fetchDispatcher(@Nonnull String tenantId, @Nullable String name); + Optional fetchDispatcher(@Nonnull String tenantId, @Nonnull String name); } diff --git a/src/main/java/sirius/biz/scripting/ScriptableEvents.java b/src/main/java/sirius/biz/scripting/ScriptableEvents.java index 9fafacfc9..463df1caf 100644 --- a/src/main/java/sirius/biz/scripting/ScriptableEvents.java +++ b/src/main/java/sirius/biz/scripting/ScriptableEvents.java @@ -8,6 +8,7 @@ package sirius.biz.scripting; +import sirius.kernel.commons.Strings; import sirius.kernel.di.std.Part; import sirius.kernel.di.std.Register; import sirius.web.security.ScopeInfo; @@ -53,11 +54,10 @@ public void handleEvent(ScriptableEvent event) { * * @param name the name of the dispatcher to fetch * @return the dispatcher for the current tenant with the given name or a NOOP dispatcher if no such dispatcher - * exists. Note, if an empty name is given, the first available dispatcher for the current tenant is used. - * This way, if exactly one dispatcher is present, it will be used in all import processes etc. + * exists. */ - public ScriptableEventDispatcher fetchDispatcherForCurrentTenant(@Nullable String name) { - if (dispatcherRepository == null) { + public ScriptableEventDispatcher fetchDispatcherForCurrentTenant(String name) { + if (dispatcherRepository == null || Strings.isEmpty(name)) { return NOOP_DISPATCHER; } diff --git a/src/main/java/sirius/biz/scripting/mongo/MongoCustomEventDispatcherRepository.java b/src/main/java/sirius/biz/scripting/mongo/MongoCustomEventDispatcherRepository.java index a27e702a3..9ba4bf673 100644 --- a/src/main/java/sirius/biz/scripting/mongo/MongoCustomEventDispatcherRepository.java +++ b/src/main/java/sirius/biz/scripting/mongo/MongoCustomEventDispatcherRepository.java @@ -28,7 +28,6 @@ import sirius.pasta.noodle.sandbox.SandboxMode; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.List; import java.util.Optional; @@ -55,6 +54,7 @@ public class MongoCustomEventDispatcherRepository implements ScriptableEventDisp public List fetchAvailableDispatchers(@Nonnull String tenantId) { return mango.select(MongoCustomScript.class) .eq(MongoCustomScript.TENANT, tenantId) + .eq(MongoCustomScript.DISABLED, false) .orderAsc(MongoCustomScript.CODE) .queryList() .stream() @@ -63,22 +63,13 @@ public List fetchAvailableDispatchers(@Nonnull String tenantId) { } @Override - public Optional fetchDispatcher(@Nonnull String tenantId, @Nullable String name) { - if (Strings.isEmpty(name)) { - List mongoCustomScripts = - mango.select(MongoCustomScript.class).eq(MongoCustomScript.TENANT, tenantId).limit(2).queryList(); - if (mongoCustomScripts.size() == 1) { - return compileAndLoad(mongoCustomScripts.getFirst()); - } else { - return Optional.empty(); - } - } else { - return mango.select(MongoCustomScript.class) - .eq(MongoCustomScript.TENANT, tenantId) - .eq(MongoCustomScript.CODE, name) - .first() - .flatMap(this::compileAndLoad); - } + public Optional fetchDispatcher(@Nonnull String tenantId, @Nonnull String name) { + return mango.select(MongoCustomScript.class) + .eq(MongoCustomScript.TENANT, tenantId) + .eq(MongoCustomScript.CODE, name) + .eq(MongoCustomScript.DISABLED, false) + .first() + .flatMap(this::compileAndLoad); } private Optional compileAndLoad(MongoCustomScript script) { diff --git a/src/main/java/sirius/biz/scripting/mongo/MongoCustomScript.java b/src/main/java/sirius/biz/scripting/mongo/MongoCustomScript.java index 1caeac0c9..cd4afa985 100644 --- a/src/main/java/sirius/biz/scripting/mongo/MongoCustomScript.java +++ b/src/main/java/sirius/biz/scripting/mongo/MongoCustomScript.java @@ -25,7 +25,9 @@ * Stores a custom scripting profile for a tenant. */ @Framework(MongoCustomEventDispatcherRepository.FRAMEWORK_SCRIPTING_MONGO) -@Index(name = "lookup", columns = {"tenant", "code"}, columnSettings = {Mango.INDEX_ASCENDING, Mango.INDEX_ASCENDING}) +@Index(name = "script_lookup", + columns = {"tenant", "disabled", "code"}, + columnSettings = {Mango.INDEX_ASCENDING, Mango.INDEX_ASCENDING, Mango.INDEX_ASCENDING}) public class MongoCustomScript extends MongoTenantAware { /** @@ -38,6 +40,11 @@ public class MongoCustomScript extends MongoTenantAware { @AutoImport private String code; + public static final Mapping DISABLED = Mapping.named("disabled"); + @Autoloaded + @AutoImport + private boolean disabled; + /** * Contains the actual scripting code. */ @@ -71,4 +78,12 @@ public String getScript() { public void setScript(String script) { this.script = script; } + + public boolean isDisabled() { + return disabled; + } + + public void setDisabled(boolean inactive) { + this.disabled = inactive; + } } diff --git a/src/main/java/sirius/biz/scripting/mongo/MongoCustomScriptController.java b/src/main/java/sirius/biz/scripting/mongo/MongoCustomScriptController.java index 8e97b7ed5..b1f5848fe 100644 --- a/src/main/java/sirius/biz/scripting/mongo/MongoCustomScriptController.java +++ b/src/main/java/sirius/biz/scripting/mongo/MongoCustomScriptController.java @@ -13,8 +13,6 @@ import sirius.biz.web.BizController; import sirius.biz.web.MongoPageHelper; import sirius.db.mixing.query.QueryField; -import sirius.db.mongo.Mango; -import sirius.kernel.di.std.Part; import sirius.kernel.di.std.Register; import sirius.kernel.tokenizer.Position; import sirius.pasta.noodle.compiler.CompilationContext; @@ -36,9 +34,6 @@ public class MongoCustomScriptController extends BizController { private static final String PARAM_SCRIPT = "script"; - @Part - private Mango mango; - /** * Lists all scripts available for the current tenant. * @@ -60,6 +55,7 @@ public void listScripts(WebContext webContext) { * Modifies / manages the given script. * * @param webContext the request to handle + * @param id the ID of the script to manage */ @Routed("/scripting/scripts/:1") @Permission(ScriptingController.PERMISSION_SCRIPTING) @@ -75,6 +71,7 @@ public void editScript(WebContext webContext, String id) { * Deletes the given script. * * @param webContext the request to handle + * @param id the ID of the script to delete */ @Routed("/scripting/scripts/:1/delete") @Permission(ScriptingController.PERMISSION_SCRIPTING) diff --git a/src/main/resources/biz_de.properties b/src/main/resources/biz_de.properties index bdba5f3b4..2b83a67d2 100644 --- a/src/main/resources/biz_de.properties +++ b/src/main/resources/biz_de.properties @@ -671,6 +671,10 @@ MongoCodeListExportJobFactory.label = Codeliste exportieren MongoCodeListImportJobFactory.description = Importiert eine Codeliste aus einer CSV oder Excel Datei. MongoCodeListImportJobFactory.label = Codeliste importieren MongoCustomScript.code = Code +MongoCustomScript.disabled = Deaktiviert +MongoCustomScript.state = Status +MongoCustomScript.state.active = Aktiv +MongoCustomScript.state.inactive = Inaktiv MongoCustomScript.plural = Kunden-Scripte MongoCustomScript.script = Quelltext MongoCustomScript.unnamedScript = Unbenanntes Script diff --git a/src/main/resources/default/templates/biz/scripting/mongo-script.html.pasta b/src/main/resources/default/templates/biz/scripting/mongo-script.html.pasta index dc1dc29eb..4a22d6459 100644 --- a/src/main/resources/default/templates/biz/scripting/mongo-script.html.pasta +++ b/src/main/resources/default/templates/biz/scripting/mongo-script.html.pasta @@ -21,10 +21,15 @@ - +

+ + +
diff --git a/src/main/resources/default/templates/biz/scripting/mongo-scripts.html.pasta b/src/main/resources/default/templates/biz/scripting/mongo-scripts.html.pasta index da8d1ec1b..70ec298a4 100644 --- a/src/main/resources/default/templates/biz/scripting/mongo-scripts.html.pasta +++ b/src/main/resources/default/templates/biz/scripting/mongo-scripts.html.pasta @@ -14,33 +14,22 @@ -
-
- - - - - - - - - - - - - - - -
@i18n("MongoCustomScript.code") -
- @script.getCode() - - -
- -
-
-
+ + + + + + + @i18n("MongoCustomScript.state.inactive") + + @i18n("MongoCustomScript.state.active") + + + + + + + +