protected void addRow(
     Model model, StringToExpressionConverter converter, Instance expression, Object row) {
   Instance listExpression = model.newInstance("expression.ListExpression");
   for (Object v : (List<Object>) row) {
     listExpression.add(
         "expressions", converter.parse(v.toString(), String.class.getName(), false));
   }
   expression.add("expressions", listExpression);
 }
 private Instance getOriginalVariable(final Model model, final String variableToSet) {
   for (Instance variable : model.getAllInstances("process.Data")) {
     String varName = (String) variable.get("name");
     if (varName != null && varName.equals(variableToSet)) {
       return variable;
     }
   }
   return null;
 }
 private Instance getParameterExpressionFor(
     Model model,
     String input,
     StringToExpressionConverter converter,
     Object value,
     String returnType) {
   if (value instanceof String || value instanceof Boolean || value instanceof Number) {
     String type = definitionMapper.getExpectedExpresstionType(input, value);
     if (type == null) {
       return converter.parse(value.toString(), returnType, true);
     } else {
       return converter.parse(value.toString(), returnType, true, type);
     }
   } else if (value instanceof List) {
     List<Object> listValue = (List<Object>) value;
     if (!listValue.isEmpty()) {
       Instance expression = null;
       Object row = listValue.get(0);
       if (row instanceof List) {
         expression = model.newInstance("expression.TableExpression");
         addRow(model, converter, expression, row);
         for (int i = 1; i < listValue.size(); i++) {
           row = listValue.get(i);
           addRow(model, converter, expression, row);
         }
       } else {
         expression = model.newInstance("expression.ListExpression");
         for (int i = 0; i < listValue.size(); i++) {
           Object v = listValue.get(i);
           expression.add(
               "expressions", converter.parse(v.toString(), String.class.getName(), false));
         }
       }
       return expression;
     }
   } else {
     if (BonitaStudioLog.isLoggable(IStatus.OK)) {
       String valueString = "null";
       if (value != null) {
         valueString = value.toString();
       }
       BonitaStudioLog.debug(
           input + " value " + valueString + " cannot be transform to an expression",
           BarImporterPlugin.PLUGIN_ID);
     }
   }
   return null;
 }
 private Instance getConnectorConfiguration(Model model, StringToExpressionConverter converter) {
   final Instance configuration =
       model.newInstance("connectorconfiguration.ConnectorConfiguration");
   configuration.set("definitionId", getDefinitionId());
   configuration.set("version", getDefinitionVersion());
   final Map<String, Object> additionalInputs = definitionMapper.getAdditionalInputs(inputs);
   final Map<String, Object> allInput = new HashMap<String, Object>(inputs);
   allInput.putAll(additionalInputs);
   for (Entry<String, Object> input : allInput.entrySet()) {
     final String parameterKeyFor = getParameterKeyFor(input.getKey());
     if (parameterKeyFor != null) {
       final Instance parameter = model.newInstance("connectorconfiguration.ConnectorParameter");
       parameter.set("key", parameterKeyFor);
       parameter.set(
           "expression",
           getParameterExpressionFor(
               model,
               parameterKeyFor,
               converter,
               definitionMapper.transformParameterValue(parameterKeyFor, input.getValue(), inputs),
               getReturnType(parameterKeyFor)));
       configuration.add("parameters", parameter);
     } else {
       if (BonitaStudioLog.isLoggable(IStatus.OK)) {
         BonitaStudioLog.debug(
             input.getKey() + " not mapped for " + getDefinitionId(), BarImporterPlugin.PLUGIN_ID);
       }
     }
   }
   return configuration;
 }
 private void populateNewConnectorInstance(
     Model model, Instance connectorInstance, StringToExpressionConverter converter) {
   connectorInstance.set(NAME, name);
   connectorInstance.set(DOCUMENTATION, documentation);
   connectorInstance.set(DEFINITION_ID, getDefinitionId());
   connectorInstance.set(DEFINITION_VERSION, getDefinitionVersion());
   connectorInstance.set(ERROR_HANDLE, ignoreErrors);
   connectorInstance.set(EVENT, toConnectorEvent(event));
   connectorInstance.set(CONNECTOR_CONFIGURATION, getConnectorConfiguration(model, converter));
   addOutputs(model, connectorInstance, converter);
 }
  private void addOutputs(
      Model model, Instance connectorInstance, StringToExpressionConverter converter) {
    for (Entry<String, Object> output : outputs.entrySet()) {

      final String outputValue = output.getValue().toString();
      final String outputType =
          isAnOutputConnector(outputValue)
              ? ExpressionConstants.CONNECTOR_OUTPUT_TYPE
              : ExpressionConstants.SCRIPT_TYPE;
      final String returnType = definitionMapper.getOutputReturnType(outputValue);

      final Instance operation =
          converter.parseOperation(
              returnType, false, (String) output.getValue(), output.getKey(), outputType);
      connectorInstance.add(OUTPUTS, operation);
    }
  }
  private void createOperation(
      Model model,
      Instance connectorInstance,
      StringToExpressionConverter converter,
      final String variableToSet,
      final String varValue) {
    Instance operationContainerInstance = connectorInstance.getContainer();
    Instance originalVariable = getOriginalVariable(model, variableToSet);

    // set variable dependency of left operand
    String returnType = Object.class.getName();
    Instance leftOperand = null;
    if (originalVariable != null) {
      returnType = StringToExpressionConverter.getDataReturnType(originalVariable);
      leftOperand =
          StringToExpressionConverter.createExpressionInstanceWithDependency(
              model,
              variableToSet,
              variableToSet,
              returnType,
              ExpressionConstants.VARIABLE_TYPE,
              true,
              originalVariable);
    } else {
      leftOperand =
          StringToExpressionConverter.createExpressionInstance(
              model,
              variableToSet,
              variableToSet,
              returnType,
              ExpressionConstants.VARIABLE_TYPE,
              true);
    }
    Instance rightOperand = converter.parse(varValue, returnType, false);
    Instance operatorInstance = model.newInstance("expression.Operator");
    operatorInstance.set("type", ExpressionConstants.ASSIGNMENT_OPERATOR);
    Instance operationInstance = model.newInstance("expression.Operation");
    operationInstance.set("leftOperand", leftOperand);
    operationInstance.set("rightOperand", rightOperand);
    operationInstance.set("operator", operatorInstance);
    operationContainerInstance.add("operations", operationInstance);
  }
  public Connector5Descriptor(Instance connectorInstance) {
    this.uuid = connectorInstance.getUuid();
    this.name = connectorInstance.get(NAME);
    this.connectorId = connectorInstance.get(CONNECTOR_ID);
    this.documentation = connectorInstance.get(DOCUMENTATION);
    this.event = connectorInstance.get(EVENT);
    this.ignoreErrors = connectorInstance.get(ERROR_HANDLE);
    final List<Instance> parameters = connectorInstance.get(PARAMETERS);
    for (Instance parameter : parameters) {
      String key = parameter.get(PARAMETER_KEY);
      Object value = parameter.get(PARAMETER_VALUE);
      inputs.put(key, value);
    }
    final List<Instance> outputMapping = connectorInstance.get(OUTPUTS);
    for (Instance output : outputMapping) {
      try {
        Instance data = output.get(OUTPUT_DATA);
        String value = output.get(OUTPUT_EXPRESSION);
        if (data != null) {
          outputs.put((String) data.get("name"), value);
        } else {
          Instance container = connectorInstance.getContainer();
          if (container.instanceOf("form.Widget")) {
            String id = "field_" + container.get("name");
            outputs.put(id, value);
          }
        }
      } catch (IllegalArgumentException e) {
        BonitaStudioLog.warning(
            "The connector "
                + connectorId
                + "/"
                + name
                + " doesn't provide the expected feature for outputs.",
            BarImporterPlugin.PLUGIN_ID);
      }
    }
    definitionMapper =
        ConnectorIdToDefinitionMapping.getInstance().getDefinitionMapper(connectorId);

    this.containerType = connectorInstance.getContainer().getType().getEClass();
  }
 public boolean appliesTo(Instance connectorInstance) {
   return uuid != null && uuid.equals(connectorInstance.getUuid());
 }