Esempio n. 1
0
  private FormState validateObject(
      final RequestContext context,
      final ObjectAdapter object,
      final List<ObjectAssociation> fields) {
    final FormState formState = new FormState();
    for (int i = 0; i < fields.size(); i++) {
      final ObjectAssociation field = fields.get(i);
      final String fieldId = field.getId();
      String newEntry = context.getParameter(fieldId);
      if (fields.get(i).isOneToManyAssociation()) {
        continue;
      }
      if (fields
          .get(i)
          .isVisible(IsisContext.getAuthenticationSession(), object, where)
          .isVetoed()) {
        continue;
      }
      if (field.isUsable(IsisContext.getAuthenticationSession(), object, where).isVetoed()) {
        continue;
      }

      if (newEntry != null && newEntry.equals("-OTHER-")) {
        newEntry = context.getParameter(fieldId + "-other");
      }

      if (newEntry == null) {
        // TODO duplicated in EditObject; line 97
        final ObjectSpecification spec = field.getSpecification();
        if (spec.isOfType(IsisContext.getSpecificationLoader().loadSpecification(boolean.class))
            || spec.isOfType(
                IsisContext.getSpecificationLoader().loadSpecification(Boolean.class))) {
          newEntry = FALSE;
        } else {
          continue;
        }
      }
      final FieldEditState fieldState = formState.createField(fieldId, newEntry);

      Consent consent = null;
      if (field.isMandatory() && (newEntry.equals("") || newEntry.equals("NULL"))) {
        consent = new Veto(field.getName() + " required");
        formState.setError("Not all fields have been set");
      } else if (field.getSpecification().containsFacet(ParseableFacet.class)) {
        try {
          final ParseableFacet facet = field.getSpecification().getFacet(ParseableFacet.class);
          final ObjectAdapter originalValue = field.get(object);
          Localization localization = IsisContext.getLocalization();
          final ObjectAdapter newValue =
              facet.parseTextEntry(originalValue, newEntry, localization);
          consent = ((OneToOneAssociation) field).isAssociationValid(object, newValue);
          fieldState.setValue(newValue);
        } catch (final TextEntryParseException e) {
          consent = new Veto(e.getMessage());
          // formState.setError("Not all fields have been entered correctly");
        }

      } else {
        final ObjectAdapter associate =
            newEntry.equals("null") ? null : context.getMappedObject(newEntry);
        if (associate != null) {
          IsisContext.getPersistenceSession().resolveImmediately(associate);
        }
        consent = ((OneToOneAssociation) field).isAssociationValid(object, associate);
        fieldState.setValue(associate);
      }
      if (consent.isVetoed()) {
        fieldState.setError(consent.getReason());
        formState.setError("Not all fields have been entered correctly");
      }
    }

    // TODO check the state of the complete object.
    return formState;
  }
Esempio n. 2
0
  private static void specificationFields(
      final ObjectSpecification specification, final DebugBuilder debugBuilder) {
    final List<ObjectAssociation> fields = specification.getAssociations(Contributed.EXCLUDED);
    debugBuilder.appendln("All");
    debugBuilder.indent();
    for (int i = 0; i < fields.size(); i++) {
      debugBuilder.appendln((i + 1) + "." + fields.get(i).getId());
    }
    debugBuilder.unindent();

    final List<ObjectAssociation> fields2 =
        specification.getAssociations(
            Contributed.EXCLUDED, ObjectAssociation.Filters.VISIBLE_AT_LEAST_SOMETIMES);
    debugBuilder.appendln("Static");
    debugBuilder.indent();
    for (int i = 0; i < fields2.size(); i++) {
      debugBuilder.appendln((i + 1) + "." + fields2.get(i).getId());
    }
    debugBuilder.unindent();
    debugBuilder.appendln();

    try {
      if (fields.size() == 0) {
        debugBuilder.appendln("none");
      } else {
        for (int i = 0; i < fields.size(); i++) {

          final ObjectAssociation field = fields.get(i);
          debugBuilder.appendln(
              (i + 1) + "." + field.getId() + "  (" + field.getClass().getName() + ")");

          debugBuilder.indent();
          final String description = field.getDescription();
          if (description != null && !description.equals("")) {
            debugBuilder.appendln("Description", description);
          }
          final String help = field.getHelp();
          if (help != null && !help.equals("")) {
            debugBuilder.appendln(
                "Help",
                help.substring(0, Math.min(30, help.length())) + (help.length() > 30 ? "..." : ""));
          }

          debugBuilder.appendln("ID", field.getIdentifier());
          debugBuilder.appendln("Short ID", field.getId());
          debugBuilder.appendln("Name", field.getName());
          final String type =
              field.isOneToManyAssociation()
                  ? "Collection"
                  : field.isOneToOneAssociation() ? "Object" : "Unknown";
          debugBuilder.appendln("Type", type);
          final ObjectSpecification fieldSpec = field.getSpecification();
          final boolean hasIdentity =
              !(fieldSpec.isParentedOrFreeCollection()
                  || fieldSpec.isParented()
                  || fieldSpec.isValue());
          debugBuilder.appendln("Has identity", hasIdentity);
          debugBuilder.appendln("Spec", fieldSpec.getFullIdentifier());

          debugBuilder.appendln(
              "Flags",
              (field.isAlwaysHidden() ? "" : "Visible ")
                  + (field.isNotPersisted() ? "Not Persisted " : " ")
                  + (field.isMandatory() ? "Mandatory " : ""));

          final Class<? extends Facet>[] facets = field.getFacetTypes();
          if (facets.length > 0) {
            debugBuilder.appendln("Facets");
            debugBuilder.indent();
            boolean none = true;
            for (final Class<? extends Facet> facet : facets) {
              debugBuilder.appendln(field.getFacet(facet).toString());
              none = false;
            }
            if (none) {
              debugBuilder.appendln("none");
            }
            debugBuilder.unindent();
          }

          debugBuilder.appendln(field.debugData());

          debugBuilder.unindent();
        }
      }
    } catch (final RuntimeException e) {
      debugBuilder.appendException(e);
    }
  }