package com.axonivy.ivy.process.element.rule;

import java.io.IOException;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.axonivy.ivy.process.element.rule.resource.RuleResolver;
import com.axonivy.ivy.process.element.rule.ui.RuleConfigEditor;

import ch.ivyteam.awt.swt.SwtRunnable;
import ch.ivyteam.ivy.designer.process.ui.inscriptionMasks.tabs.helper.ProcessExtensionConfigurationEditorEnvironment;
import ch.ivyteam.ivy.process.engine.IRequestId;
import ch.ivyteam.ivy.process.extension.IProcessExtensionConfigurationEditorEnvironment;
import ch.ivyteam.ivy.process.extension.impl.AbstractProcessExtensionConfigurationEditor;
import ch.ivyteam.ivy.process.extension.impl.AbstractUserProcessExtension;
import ch.ivyteam.ivy.scripting.language.IIvyScriptContext;
import ch.ivyteam.ivy.scripting.objects.CompositeObject;

public class RuleActivity extends AbstractUserProcessExtension 
{
  private static final String RULE_NAMESPACE = "RULE_NAMESPACE";
  private static final String INPUT_DATA_MAPPING = "INPUT_DATA_MAPPING";

  @Override
  public CompositeObject perform(IRequestId requestId, CompositeObject in, IIvyScriptContext context)
          throws Exception
  {
    executeIvyScript(context, createIvyScript());
    return in;
  }

  private String createIvyScript() throws IOException
  {
    String namespace = getConfigurationProperty(RULE_NAMESPACE);
    String outData = getConfigurationProperty(INPUT_DATA_MAPPING);
    StringBuilder script = new StringBuilder();
    script.append("IRuleBase ruleBase = ivy.rules.engine.createRuleBase();");
    script.append("\n");
    script.append("ruleBase.loadRulesFromNamespace(\"" + namespace + "\");");
    script.append("\n");
    script.append("ruleBase.createSession().execute(" + outData + ");");
    return script.toString();
  }

  public static class Editor extends AbstractProcessExtensionConfigurationEditor
  {    
    private ProcessExtensionConfigurationEditorEnvironment env;
    
    private String ruleNamespace;
    private String inputData;
    
    private RuleConfigEditor ruleConfigEditor;

    @Override
    public void setEnvironment(IProcessExtensionConfigurationEditorEnvironment environment)
    {
      this.env = (ProcessExtensionConfigurationEditorEnvironment) environment;
    }
    
    @Override
    public Composite getComposite(Composite parent)
    {
      if (ruleConfigEditor == null)
      {
        List<String> availableRuleNamespaces = new RuleResolver(env.getIvyProject()).findAvailableRulenamespaces();
         
        ruleConfigEditor = new RuleConfigEditor(parent, SWT.NONE);
        GridLayout gridLayout = (GridLayout) ruleConfigEditor.getLayout();
        gridLayout.marginHeight = 0;
        gridLayout.marginWidth = 0;
        ruleConfigEditor.setAvailableRuleNamespace(availableRuleNamespaces);
        ruleConfigEditor.setRuleNamespace(ruleNamespace);
        ruleConfigEditor.setInputData(inputData);
        ruleConfigEditor.setDataVariables(env.getDataInputVariables());
      }
      return ruleConfigEditor;
    }

    @Override
    protected void loadUiDataFromConfiguration()
    {
      new SwtRunnable()
        {
          @Override
          public void run()
          {
            ruleNamespace = getBeanConfigurationProperty(RULE_NAMESPACE);
            inputData = getBeanConfigurationProperty(INPUT_DATA_MAPPING);
          }
        }.syncExec();
    }

    @Override
    protected boolean saveUiDataToConfiguration()
    {
      clearBeanConfiguration();
      new SwtRunnable()
        {
          @Override
          public void run()
          {
            setBeanConfigurationProperty(RULE_NAMESPACE, ruleConfigEditor.getRuleNamespace());
            setBeanConfigurationProperty(INPUT_DATA_MAPPING, ruleConfigEditor.getInputData());
          }
        }.syncExec();
      return true;
    }
  }
}

@@ -390,16 +390,95 @@
 nable;%0D%0A
+import ch.ivyteam.ivy.bpm.exec.restricted.acl.scripting.IvyScriptCodeBuilder;%0D%0A
 import c
@@ -1648,22 +1648,35 @@
 );%0D%0A    
-String
+%0D%0A    IvyScriptCode
 Builder 
@@ -1688,22 +1688,29 @@
 t = new 
-String
+IvyScriptCode
 Builder(
@@ -1796,34 +1796,8 @@
 );%0D%0A
-    script.append(%22%5Cn%22);%0D%0A
     
@@ -1875,34 +1875,8 @@
 );%0D%0A
-    script.append(%22%5Cn%22);%0D%0A
     
@@ -1939,24 +1939,28 @@
 ata + %22);%22);
+    
 %0D%0A    return
@@ -2721,19 +2721,8 @@
 );%0D%0A
-         %0D%0A
     

Use IvyScriptCodeBuilder