Esempio n. 1
0
 public Element getControlElement(FormData formData) {
   if (controlElement == null) {
     if (getPropertyString("controlField") != null
         && !getPropertyString("controlField").isEmpty()) {
       Form form = FormUtil.findRootForm(this);
       controlElement = FormUtil.findElement(getPropertyString("controlField"), form, formData);
     }
   }
   return controlElement;
 }
  @RequestMapping("/form/embed")
  public String embedForm(
      ModelMap model,
      HttpServletRequest request,
      HttpServletResponse response,
      @RequestParam("_submitButtonLabel") String buttonLabel,
      @RequestParam("_json") String json,
      @RequestParam("_callback") String callback,
      @RequestParam("_setting") String callbackSetting,
      @RequestParam(required = false) String id,
      @RequestParam(value = "_a", required = false) String action)
      throws JSONException, UnsupportedEncodingException {
    FormData formData = new FormData();
    if (id != null && !id.isEmpty()) {
      formData.setPrimaryKeyValue(id);
    }
    Form form = formService.loadFormFromJson(json, formData);

    AppDefinition appDef = AppUtil.getCurrentAppDefinition();
    String appId = "";
    String appVersion = "";
    if (appDef != null) {
      appId = appDef.getAppId();
      appVersion = appDef.getVersion().toString();
    }
    String nonce = request.getParameter("_nonce");
    if (form == null
        || !SecurityUtil.verifyNonce(
            nonce,
            new String[] {"EmbedForm", appId, appVersion, form.getPropertyString("id"), nonce})) {
      response.setStatus(HttpServletResponse.SC_FORBIDDEN);
      return null;
    }

    if (callbackSetting == null || (callbackSetting != null && callbackSetting.isEmpty())) {
      callbackSetting = "{}";
    }

    form.setProperty(
        "url",
        "?_nonce="
            + URLEncoder.encode(nonce, "UTF-8")
            + "&_a=submit&_callback="
            + callback
            + "&_setting="
            + StringEscapeUtils.escapeHtml(callbackSetting)
            + "&_submitButtonLabel="
            + StringEscapeUtils.escapeHtml(buttonLabel));

    if (form != null) {
      // if id field not exist, automatically add an id hidden field
      Element idElement = FormUtil.findElement(FormUtil.PROPERTY_ID, form, formData);
      if (idElement == null) {
        Collection<Element> formElements = form.getChildren();
        idElement = new HiddenField();
        idElement.setProperty(FormUtil.PROPERTY_ID, FormUtil.PROPERTY_ID);
        idElement.setParent(form);
        formElements.add(idElement);
      }

      // create new section for buttons
      Section section = new Section();
      section.setProperty(FormUtil.PROPERTY_ID, "section-actions");
      Collection<Element> sectionChildren = new ArrayList<Element>();
      section.setChildren(sectionChildren);
      Collection<Element> formChildren = form.getChildren(formData);
      if (formChildren == null) {
        formChildren = new ArrayList<Element>();
      }
      formChildren.add(section);

      // add new horizontal column to section
      Column column = new Column();
      column.setProperty("horizontal", "true");
      Collection<Element> columnChildren = new ArrayList<Element>();
      column.setChildren(columnChildren);
      sectionChildren.add(column);

      Element hiddenField = (Element) pluginManager.getPlugin(HiddenField.class.getName());
      hiddenField.setProperty(FormUtil.PROPERTY_ID, "_json");
      hiddenField.setProperty(FormUtil.PROPERTY_VALUE, json);
      columnChildren.add((Element) hiddenField);

      Element submitButton = (Element) pluginManager.getPlugin(SubmitButton.class.getName());
      submitButton.setProperty(FormUtil.PROPERTY_ID, "submit");
      submitButton.setProperty("label", buttonLabel);
      columnChildren.add((Element) submitButton);
    }

    // generate form HTML
    String formHtml = null;

    if ("submit".equals(action)) {
      formData = formService.retrieveFormDataFromRequest(formData, request);
      formData = formService.executeFormActions(form, formData);

      // check for validation errors
      Map<String, String> errors = formData.getFormErrors();
      int errorCount = 0;
      if (!formData.getStay() && (errors == null || errors.isEmpty())) {
        // render normal template
        formHtml = formService.generateElementHtml(form, formData);

        // convert submitted
        JSONObject jsonResult = new JSONObject();

        // get binder of main form
        FormStoreBinder mainBinder = form.getStoreBinder();
        FormRowSet rows = formData.getStoreBinderData(mainBinder);

        for (FormRow row : rows) {
          for (Object o : row.keySet()) {
            jsonResult.accumulate(o.toString(), row.get(o));
          }
          Map<String, String> tempFilePathMap = row.getTempFilePathMap();
          if (tempFilePathMap != null && !tempFilePathMap.isEmpty()) {
            jsonResult.put(FormUtil.PROPERTY_TEMP_FILE_PATH, tempFilePathMap);
          }
        }

        model.addAttribute("jsonResult", StringEscapeUtils.escapeJavaScript(jsonResult.toString()));
      } else {
        // render error template
        formHtml = formService.generateElementErrorHtml(form, formData);
        errorCount = errors.size();
      }

      model.addAttribute("setting", callbackSetting);
      model.addAttribute("callback", callback);
      model.addAttribute("submitted", Boolean.TRUE);
      model.addAttribute("errorCount", errorCount);
      model.addAttribute("stay", formData.getStay());
    } else {
      formHtml = formService.retrieveFormHtml(form, formData);
    }

    model.addAttribute("formHtml", formHtml);

    if (request.getParameter("_mapp") != null) {
      response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
      response.setHeader("Access-Control-Allow-Credentials", "true");
      response.setHeader("Content-type", "application/xml");

      return "mapp/embedForm";
    } else {
      return "fbuilder/embedForm";
    }
  }