Пример #1
0
  public void saveFormState(@Nonnull final FormState aFormState) {
    ValueEnforcer.notNull(aFormState, "FormState");

    m_aRWLock.writeLock().lock();
    try {
      m_aMap.put(aFormState.getFlowID(), aFormState);
      m_bAtLeastOnceAFormState = true;
    } finally {
      m_aRWLock.writeLock().unlock();
    }

    if (GlobalDebug.isDebugMode()) s_aLogger.info("Saved form state: " + aFormState.toString());
    else s_aLogger.info("Saved form state for page " + aFormState.getPageID());
  }
Пример #2
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());
      }
    }
  }
Пример #3
0
  @Override
  public void process(final RequestContext context) throws IOException {
    AuthenticationSession session = context.getSession();
    if (session == null) {
      session = new AnonymousSession();
    }

    try {
      final String objectId = context.getParameter("_" + OBJECT);
      final String version = context.getParameter("_" + VERSION);
      final String formId = context.getParameter("_" + FORM_ID);
      String resultName = context.getParameter("_" + RESULT_NAME);
      resultName = resultName == null ? RequestContext.RESULT : resultName;
      final String override = context.getParameter("_" + RESULT_OVERRIDE);
      String message = context.getParameter("_" + MESSAGE);

      final ObjectAdapter adapter = context.getMappedObject(objectId);

      final List<ObjectAssociation> fields =
          adapter
              .getSpecification()
              .getAssociations(
                  Contributed.EXCLUDED,
                  ObjectAssociation.Filters.dynamicallyVisible(session, adapter, where));

      for (final ObjectAssociation objectAssociation : fields) {
        if (objectAssociation.isVisible(session, adapter, where).isVetoed()) {
          throw new NotLoggedInException();
        }
      }

      final FormState entryState = validateObject(context, adapter, fields);
      final Version adapterVersion = adapter.getVersion();
      final Version formVersion = context.getVersion(version);
      if (formVersion != null && adapterVersion.different(formVersion)) {

        IsisContext.getMessageBroker()
            .addMessage(
                "The "
                    + adapter.getSpecification().getSingularName()
                    + " was edited "
                    + "by another user ("
                    + adapterVersion.getUser()
                    + "). Please  make your changes based on their changes.");

        final String view = context.getParameter("_" + ERROR);
        context.setRequestPath(view, Dispatcher.EDIT);

        entryState.setForm(formId);
        context.addVariable(ENTRY_FIELDS, entryState, Scope.REQUEST);
        context.addVariable(resultName, objectId, Scope.REQUEST);
        if (override != null) {
          context.addVariable(resultName, override, Scope.REQUEST);
        }

      } else if (entryState.isValid()) {
        changeObject(context, adapter, entryState, fields);

        if (adapter.isTransient()) {
          IsisContext.getPersistenceSession().makePersistent(adapter);
          context.unmapObject(adapter, Scope.REQUEST);
        }

        String view = context.getParameter("_" + VIEW);

        final String id = context.mapObject(adapter, Scope.REQUEST);
        context.addVariable(resultName, id, Scope.REQUEST);
        if (override != null) {
          context.addVariable(resultName, override, Scope.REQUEST);
        }

        final int questionMark = view == null ? -1 : view.indexOf("?");
        if (questionMark > -1) {
          final String params = view.substring(questionMark + 1);
          final int equals = params.indexOf("=");
          context.addVariable(
              params.substring(0, equals), params.substring(equals + 1), Scope.REQUEST);
          view = view.substring(0, questionMark);
        }
        context.setRequestPath(view);
        if (message == null) {
          message = "Saved changes to " + adapter.getSpecification().getSingularName();
        } else if (message.equals("")) {
          message = null;
        }
        if (message != null) {
          final MessageBroker messageBroker = IsisContext.getMessageBroker();
          messageBroker.addMessage(message);
        }

      } else {
        final String view = context.getParameter("_" + ERROR);
        context.setRequestPath(view, Dispatcher.EDIT);

        entryState.setForm(formId);
        context.addVariable(ENTRY_FIELDS, entryState, Scope.REQUEST);
        context.addVariable(resultName, objectId, Scope.REQUEST);
        if (override != null) {
          context.addVariable(resultName, override, Scope.REQUEST);
        }

        final MessageBroker messageBroker = IsisContext.getMessageBroker();
        messageBroker.addWarning(entryState.getError());
      }

    } catch (final RuntimeException e) {
      IsisContext.getMessageBroker().getMessages();
      IsisContext.getMessageBroker().getWarnings();
      throw e;
    }
  }
Пример #4
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;
  }