@RequestMapping("/fbuilder/app/(*:appId)/(~:appVersion)/form/(*:formId)/element/preview")
  public String previewElement(
      ModelMap model,
      @RequestParam("appId") String appId,
      @RequestParam(value = "appVersion", required = false) String appVersion,
      @RequestParam("formId") String formId,
      @RequestParam("json") String json) {
    try {
      FormUtil.setProcessedFormJson(json);

      model.addAttribute("appId", appId);
      model.addAttribute("appVersion", appVersion);
      AppDefinition appDef = appService.getAppDefinition(appId, appVersion);

      String tempJson = json;
      if (tempJson.contains(SecurityUtil.ENVELOPE)
          || tempJson.contains(PropertyUtil.PASSWORD_PROTECTED_VALUE)) {
        FormDefinition formDef = formDefinitionDao.loadById(formId, appDef);

        if (formDef != null) {
          tempJson = PropertyUtil.propertiesJsonStoreProcessing(formDef.getJson(), tempJson);
        }
      }

      String elementHtml = formService.previewElement(tempJson);
      model.addAttribute("elementTemplate", elementHtml);
      model.addAttribute("elementJson", json);
    } finally {
      FormUtil.clearProcessedFormJson();
    }

    return "fbuilder/previewElement";
  }
예제 #2
0
  @Override
  public FormRowSet formatData(FormData formData) {
    FormRowSet rowSet = null;

    // get value
    String id = getPropertyString(FormUtil.PROPERTY_ID);
    if (id != null) {
      String[] values = FormUtil.getElementPropertyValues(this, formData);
      if (values != null && values.length > 0) {
        // check for empty submission via parameter
        String[] paramValues = FormUtil.getRequestParameterValues(this, formData);
        if ((paramValues == null || paramValues.length == 0)
            && FormUtil.isFormSubmitted(this, formData)) {
          values = new String[] {""};
        }

        // formulate values
        String delimitedValue = FormUtil.generateElementPropertyValues(values);

        // set value into Properties and FormRowSet object
        FormRow result = new FormRow();
        result.setProperty(id, delimitedValue);
        rowSet = new FormRowSet();
        rowSet.add(result);
      }
    }

    return rowSet;
  }
예제 #3
0
  protected void dynamicOptions(FormData formData) {
    if (getControlElement(formData) != null) {
      setProperty(
          "controlFieldParamName", FormUtil.getElementParameterName(getControlElement(formData)));

      FormUtil.setAjaxOptionsElementProperties(this, formData);
    }
  }
예제 #4
0
 @Override
 public FormData formatDataForValidation(FormData formData) {
   String[] paramValues = FormUtil.getRequestParameterValues(this, formData);
   if ((paramValues == null || paramValues.length == 0)
       && FormUtil.isFormSubmitted(this, formData)) {
     String paramName = FormUtil.getElementParameterName(this);
     formData.addRequestParameterValues(paramName, new String[] {""});
   }
   return formData;
 }
예제 #5
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("/json/app/(*:appId)/(~:appVersion)/form/options")
  public void formAjaxOptions(
      Writer writer,
      @RequestParam("appId") String appId,
      @RequestParam(value = "appVersion", required = false) String appVersion,
      @RequestParam("_dv") String dependencyValue,
      @RequestParam("_n") String nonce,
      @RequestParam("_bd") String binderData)
      throws JSONException {
    AppDefinition appDef = appService.getAppDefinition(appId, appVersion);
    FormRowSet rowSet =
        FormUtil.getAjaxOptionsBinderData(dependencyValue, appDef, nonce, binderData);

    JSONArray jsonArray = new JSONArray();
    if (rowSet != null) {
      for (Map row : rowSet) {
        Map<String, String> data = new HashMap<String, String>();
        data.put(FormUtil.PROPERTY_LABEL, (String) row.get(FormUtil.PROPERTY_LABEL));
        data.put(FormUtil.PROPERTY_VALUE, (String) row.get(FormUtil.PROPERTY_VALUE));
        jsonArray.put(data);
      }
    }

    jsonArray.write(writer);
  }
예제 #7
0
 @Override
 public FormData actionPerformed(Form form, FormData formData) {
   Logger.getLogger(LinkButton.class.getName())
       .log(
           Level.INFO, " -- LinkButton actionPerformed " + FormUtil.getElementParameterName(this));
   return formData;
 }
예제 #8
0
  @Override
  public String renderTemplate(FormData formData, Map dataModel) {
    String template = "selectBox.ftl";

    // set value
    String[] valueArray = FormUtil.getElementPropertyValues(this, formData);
    List<String> values = Arrays.asList(valueArray);
    dataModel.put("values", values);

    // set options
    Collection<Map> optionMap = getOptionMap(formData);
    dataModel.put("options", optionMap);

    String html = FormUtil.generateElementHtml(this, formData, template, dataModel);
    return html;
  }
예제 #9
0
  @Override
  public String renderTemplate(FormData formData, Map dataModel) {
    if (getPropertyString("target") == null || getPropertyString("target").isEmpty()) {
      setProperty("target", "top");
    }

    String template = "linkButton.ftl";
    String html = FormUtil.generateElementHtml(this, formData, template, dataModel);
    return html;
  }
  @RequestMapping("/console/app/(*:appId)/(~:version)/form/builder/(*:formId)")
  public String formBuilder(
      ModelMap model,
      @RequestParam("appId") String appId,
      @RequestParam(value = "version", required = false) String version,
      @RequestParam("formId") String formId,
      @RequestParam(required = false) String json) {
    // verify app version
    ConsoleWebPlugin consoleWebPlugin =
        (ConsoleWebPlugin) pluginManager.getPlugin(ConsoleWebPlugin.class.getName());
    String page = consoleWebPlugin.verifyAppVersion(appId, version);
    if (page != null) {
      return page;
    }

    // set flag in request
    FormUtil.setFormBuilderActive(true);

    // load form definition
    model.addAttribute("appId", appId);
    AppDefinition appDef = appService.getAppDefinition(appId, version);
    FormDefinition formDef = null;
    if (appDef == null) {
      // TODO: handle invalid app
    } else {
      model.addAttribute("appDefinition", appDef);
      formDef = formDefinitionDao.loadById(formId, appDef);
    }

    if (formDef != null) {
      String formJson = null;
      if (json != null && !json.trim().isEmpty()) {
        // read custom JSON from request
        formJson = json;
      } else {
        // get JSON from form definition
        formJson = formDef.getJson();
      }
      if (formJson != null && formJson.trim().length() > 0) {
        String processedformJson = PropertyUtil.propertiesJsonLoadProcessing(formJson);

        try {
          FormUtil.setProcessedFormJson(processedformJson);
          String elementHtml = formService.previewElement(formJson);
          model.addAttribute("elementHtml", elementHtml);
          model.addAttribute("elementJson", processedformJson);
        } finally {
          FormUtil.clearProcessedFormJson();
        }
      } else {
        // default empty form
        String tableName = formDef.getTableName();
        if (tableName == null || tableName.isEmpty()) {
          tableName = formDef.getId();
        }
        String escapedFormName = StringEscapeUtils.escapeJavaScript(formDef.getName());
        String defaultJson =
            "{className: 'org.joget.apps.form.model.Form',  \"properties\":{ \"id\":\""
                + formId
                + "\", \"name\":\""
                + escapedFormName
                + "\", \"tableName\":\""
                + tableName
                + "\", \"loadBinder\":{ \"className\":\"org.joget.apps.form.lib.WorkflowFormBinder\" }, \"storeBinder\":{ \"className\":\"org.joget.apps.form.lib.WorkflowFormBinder\" } }}";
        String formHtml = formService.previewElement(defaultJson);
        model.addAttribute("elementHtml", formHtml);
      }
    } else {
      // default empty form
      String formJson =
          "{className: 'org.joget.apps.form.model.Form',  \"properties\":{ \"id\":\""
              + formId
              + "\", \"name\":\"\" \"loadBinder\":{ \"className\":\"org.joget.apps.form.lib.WorkflowFormBinder\" }, \"storeBinder\":{ \"className\":\"org.joget.apps.form.lib.WorkflowFormBinder\" } }}";
      String formHtml = formService.previewElement(formJson);
      model.addAttribute("elementHtml", formHtml);
    }

    // add palette
    model.addAttribute("palette", formBuilderPalette);

    // add form def id
    model.addAttribute("formId", formId);
    model.addAttribute("formDef", formDef);

    return "fbuilder/formBuilder";
  }
  @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";
    }
  }
예제 #12
0
 public String getTableName() {
   Form form = FormUtil.findRootForm(getElement());
   return form.getPropertyString(FormUtil.PROPERTY_TABLE_NAME);
 }
예제 #13
0
 public String getFormId() {
   Form form = FormUtil.findRootForm(getElement());
   return form.getPropertyString(FormUtil.PROPERTY_ID);
 }
예제 #14
0
 @Override
 public String renderTemplate(FormData formData, Map dataModel) {
   String template = "column.ftl";
   String html = FormUtil.generateElementHtml(this, formData, template, dataModel);
   return html;
 }
예제 #15
0
 /**
  * Returns the option key=value pairs for this select box.
  *
  * @param formData
  * @return
  */
 public Collection<Map> getOptionMap(FormData formData) {
   Collection<Map> optionMap = FormUtil.getElementPropertyOptionsMap(this, formData);
   return optionMap;
 }