예제 #1
0
 @Override
 public String parseAndValidate(
     final ScalarModel scalarModel, final String proposedPojoAsStr) {
   final OneToOneAssociation property = scalarModel.getPropertyMemento().getProperty();
   ParseableFacet parseableFacet = property.getFacet(ParseableFacet.class);
   if (parseableFacet == null) {
     parseableFacet = property.getSpecification().getFacet(ParseableFacet.class);
   }
   try {
     final ObjectAdapter parentAdapter =
         scalarModel.parentObjectAdapterMemento.getObjectAdapter(ConcurrencyChecking.CHECK);
     final ObjectAdapter currentValue = property.get(parentAdapter);
     Localization localization = IsisContext.getLocalization();
     final ObjectAdapter proposedAdapter =
         parseableFacet.parseTextEntry(currentValue, proposedPojoAsStr, localization);
     final Consent valid = property.isAssociationValid(parentAdapter, proposedAdapter);
     return valid.isAllowed() ? null : valid.getReason();
   } catch (final ConcurrencyException ex) {
     // disregard concurrency exceptions because will pick up at the IFormValidator level
     // rather
     // than each individual property.
     return null;
   } catch (final Exception ex) {
     return ex.getLocalizedMessage();
   }
 }
예제 #2
0
  public void setObjectAsString(final String enteredText) {
    // parse text to get adapter
    final ParseableFacet parseableFacet = getTypeOfSpecification().getFacet(ParseableFacet.class);
    if (parseableFacet == null) {
      throw new RuntimeException(
          "unable to parse string for " + getTypeOfSpecification().getFullIdentifier());
    }
    Localization localization = IsisContext.getLocalization();
    final ObjectAdapter adapter =
        parseableFacet.parseTextEntry(getObject(), enteredText, localization);

    setObject(adapter);
  }
예제 #3
0
 @Override
 public String parseAndValidate(
     final ScalarModel scalarModel, final String proposedPojoAsStr) {
   final ObjectActionParameter parameter =
       scalarModel.getParameterMemento().getActionParameter();
   ParseableFacet parseableFacet = parameter.getFacet(ParseableFacet.class);
   if (parseableFacet == null) {
     parseableFacet = parameter.getSpecification().getFacet(ParseableFacet.class);
   }
   try {
     final ObjectAdapter parentAdapter =
         scalarModel.parentObjectAdapterMemento.getObjectAdapter(ConcurrencyChecking.CHECK);
     Localization localization = IsisContext.getLocalization();
     final String invalidReasonIfAny =
         parameter.isValid(parentAdapter, proposedPojoAsStr, localization);
     return invalidReasonIfAny;
   } catch (final Exception ex) {
     return ex.getLocalizedMessage();
   }
 }
예제 #4
0
  private void changeObject(
      final RequestContext context,
      final ObjectAdapter object,
      final FormState editState,
      final List<ObjectAssociation> fields) {
    for (int i = 0; i < fields.size(); i++) {
      final FieldEditState field = editState.getField(fields.get(i).getId());
      if (field == null) {
        continue;
      }
      final String newEntry = field.getEntry();
      final ObjectAdapter originalValue = fields.get(i).get(object);
      final boolean isVisible =
          fields
              .get(i)
              .isVisible(IsisContext.getAuthenticationSession(), object, where)
              .isAllowed();
      final boolean isUsable =
          fields.get(i).isUsable(IsisContext.getAuthenticationSession(), object, where).isAllowed();
      final boolean bothEmpty = originalValue == null && newEntry.equals("");
      final boolean bothSame =
          newEntry.equals(originalValue == null ? "" : originalValue.titleString());
      if ((!isVisible || !isUsable) || bothEmpty || bothSame) {
        if (fields.get(i).getSpecification().getFacet(ParseableFacet.class) == null) {
          // REVIEW restores object to loader
          context.getMappedObject(newEntry);
        }
        continue;
      }

      if (fields.get(i).getSpecification().containsFacet(ParseableFacet.class)) {
        final ParseableFacet facet =
            fields.get(i).getSpecification().getFacet(ParseableFacet.class);
        Localization localization = IsisContext.getLocalization();
        final ObjectAdapter newValue = facet.parseTextEntry(originalValue, newEntry, localization);
        ((OneToOneAssociation) fields.get(i)).set(object, newValue);
      } else {
        ((OneToOneAssociation) fields.get(i)).set(object, field.getValue());
      }
    }
  }
예제 #5
0
 protected Localization getLocalization() {
   return IsisContext.getLocalization();
 }
예제 #6
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;
  }