Ejemplo n.º 1
0
  @Override
  public Map<String, Object> getChangedValues() {

    final Map<String, Object> changedValues = new HashMap<String, Object>();
    final ModelNode src = editedEntity == null ? new ModelNode() : editedEntity;
    final ModelNode dest = getUpdatedEntity();

    ModelNodeInspector inspector = new ModelNodeInspector(this.getUpdatedEntity());
    inspector.accept(
        new ModelNodeVisitor() {
          @Override
          public boolean visitValueProperty(
              String propertyName, ModelNode value, PropertyContext ctx) {

            // protected mode (see edit() ) requires us to prevent mutation
            ModelNode modelNode = src.hasDefined(propertyName) ? src.get(propertyName) : UNDEFINED;

            if (!modelNode.equals(dest.get(propertyName))) {
              Object castedValue =
                  downCast(dest.get(propertyName), getAttributeMetaData(propertyName));
              changedValues.put(propertyName, castedValue);
            }

            return true;
          }
        });

    Map<String, Object> finalDiff = new HashMap<String, Object>();

    // map changes, but skip unmodified fields
    for (Map<String, FormItem> groupItems : formItems.values()) {
      for (FormItem item : groupItems.values()) {
        Object val = changedValues.get(item.getName());

        // expression have precedence over real values
        if (item.isExpressionValue()) {
          finalDiff.put(item.getName(), item.asExpressionValue());
        }

        // regular values
        else if (val != null && item.isModified()) {
          if (item.isUndefined()) finalDiff.put(item.getName(), FormItem.VALUE_SEMANTICS.UNDEFINED);
          else finalDiff.put(item.getName(), val);
        }
      }
    }

    return finalDiff;
  }
  private ToolStrip mainTableTools() {
    ToolStrip tools = new ToolStrip();
    ToolButton addButton =
        new ToolButton(
            Console.CONSTANTS.common_label_add(),
            event -> {
              ModelNodeFormBuilder.FormAssets addFormAssets =
                  new ModelNodeFormBuilder()
                      .setResourceDescription(resourceDescription)
                      .setCreateMode(true)
                      .unsorted()
                      .setCreateNameAttribute(false)
                      .setSecurityContext(securityContext)
                      .build();
              addFormAssets.getForm().setEnabled(true);
              addFormAssets
                  .getForm()
                  .addFormValidator(
                      (formItems, formValidation) -> {

                        // at least one field is necessary to fill
                        boolean allEmpty = true;
                        for (FormItem formItem : formItems) {
                          if (!formItem.isUndefined()) {
                            allEmpty = false;
                            break;
                          }
                        }
                        if (allEmpty) {
                          formValidation.addError("match-abstract-type");
                          FormItem mechanismNameFormItem =
                              formItem(formItems, "match-abstract-type");
                          mechanismNameFormItem.setErrMessage(
                              "At least one field must contain valid values.");
                          mechanismNameFormItem.setErroneous(true);
                        }
                      });

              DefaultWindow dialog = new DefaultWindow(Console.MESSAGES.newTitle("Match Rule"));
              AddResourceDialog.Callback callback =
                  new AddResourceDialog.Callback() {
                    @Override
                    public void onAdd(ModelNode payload) {
                      for (Property node : payload.asPropertyList()) {
                        // remove undefined attributes
                        if (!node.getValue().isDefined()) {
                          payload.remove(node.getName());
                        }
                      }
                      circuit.dispatch(
                          new AddListAttribute(
                              ElytronStore.AUTHENTICATION_CONTEXT_ADDRESS,
                              MATCH_RULES,
                              authContextName,
                              payload));
                      dialog.hide();
                    }

                    @Override
                    public void onCancel() {
                      dialog.hide();
                    }
                  };
              AddResourceDialog addDialog =
                  new AddResourceDialog(addFormAssets, resourceDescription, callback);
              dialog.setWidth(480);
              dialog.setHeight(510);
              dialog.setWidget(addDialog);
              dialog.setGlassEnabled(true);
              dialog.center();
            });
    ToolButton removeButton =
        new ToolButton(
            Console.CONSTANTS.common_label_delete(),
            event -> {
              final ModelNode selection = selectionModel.getSelectedObject();
              if (selection != null) {
                Feedback.confirm(
                    "Match Rule",
                    Console.MESSAGES.deleteConfirm("Match Rule " + selection.asString()),
                    isConfirmed -> {
                      if (isConfirmed) {
                        circuit.dispatch(
                            new RemoveListAttribute(
                                ElytronStore.AUTHENTICATION_CONTEXT_ADDRESS,
                                authContextName,
                                MATCH_RULES,
                                selection));
                      }
                    });
              }
            });
    tools.addToolButtonRight(addButton);
    tools.addToolButtonRight(removeButton);
    return tools;
  }