Class MultistepFormsService

java.lang.Object
sk.iway.iwcm.components.multistep_form.rest.MultistepFormsService

@Service public class MultistepFormsService extends Object
  • Field Details

    • SESSION_PREFIX

      public static final String SESSION_PREFIX
      Service handling validation, session persistence and saving of multistep forms.

      Responsibilities: - Generate stable session keys per form and user request - Validate step inputs (required, regex, XSS, CAPTCHA, CSRF, file rules) - Orchestrate custom processors/interceptors for steps and final save - Persist intermediate step data into HTTP session and finalize DB save

      See Also:
    • MULTIUPLOAD_PREFIX

      public static final String MULTIUPLOAD_PREFIX
      See Also:
    • FILE_INPUT_FIELD_TYPE

      public static final String FILE_INPUT_FIELD_TYPE
      See Also:
    • VISIBILITY_TAB

      public static final String VISIBILITY_TAB
      See Also:
    • REQUIREMENT_TAB

      public static final String REQUIREMENT_TAB
      See Also:
  • Constructor Details

  • Method Details

    • getSessionKey

      public static final String getSessionKey(String formName, jakarta.servlet.http.HttpServletRequest request)
      Build the session key used to store per-form, per-request step data.
      Parameters:
      formName - logical form name (will be sanitized elsewhere)
      request - HTTP request providing the CSRF token header
      Returns:
      stable session key in format MultistepForm_<formName>_<domainId>_<csrf>
    • getNewSessionKey

      public static final String getNewSessionKey(String formName, String csrf)
      Build a new session key for the given form and CSRF token.
      Parameters:
      formName - logical form name
      csrf - CSRF token value
      Returns:
      session key string
    • getFormName

      public static final String getFormName(Map<String,String> params)
      Extract and sanitize the formName from a params map.
      Parameters:
      params - map potentially containing key formName
      Returns:
      sanitized form name (safe for identifiers)
    • getFormName

      public static final String getFormName(jakarta.servlet.http.HttpServletRequest request)
      Extract and sanitize the formName from the request.
      Parameters:
      request - HTTP request containing parameter formName
      Returns:
      sanitized form name (safe for identifiers)
    • getRegExOptions

      public static final List<LabelValue> getRegExOptions(RegExpRepository regExpRepository, jakarta.servlet.http.HttpServletRequest request)
      Provide available regular expression validations as label/value pairs for UI.
      Parameters:
      regExpRepository - repository of regex definitions
      request - request used to resolve localized titles
      Returns:
      list of label/value options
    • getFieldTypes

      public static final Pair<List<LabelValue>,List<LabelValue>> getFieldTypes(jakarta.servlet.http.HttpServletRequest request)
      Provide supported form field types as label/value pairs.
      Parameters:
      request - request used to resolve localized labels
      Returns:
      list of field type options
    • isFieldtypeIterable

      public static final boolean isFieldtypeIterable(String fieldType, jakarta.servlet.http.HttpServletRequest request)
    • isFileUploadField

      public static final boolean isFileUploadField(String fieldType)
      Determine whether a form field stores an asynchronously uploaded attachment.
      Parameters:
      fieldType - configured form field type
      Returns:
      true for multi-upload variants and the single-file input
    • getFiledTypeVisibility

      public static final List<LabelValue> getFiledTypeVisibility(jakarta.servlet.http.HttpServletRequest request)
      Provide visibility configuration per field type (which technical fields are hidden). Adds a special option allwaysHidden.
      Parameters:
      request - request used to resolve localized labels
      Returns:
      list of visibility label/value pairs
    • getFieldTabVisibility

      public static final List<LabelValue> getFieldTabVisibility(jakarta.servlet.http.HttpServletRequest request)
      Provide tab visibility configuration per field type.

      Depending on the field type, returns list of tabs that should be shown in admin: visibility conditions, requirement conditions, both, or none.

      Parameters:
      request - request used to resolve localized labels and field metadata
      Returns:
      list mapping field type to a comma-separated list of visible tabs
    • getNextStep

      public static final FormStepEntity getNextStep(String formName, FormStepEntity currentStep, FormStepsRepository repo)
      Get the next step following the provided step within a form sequence.
      Parameters:
      formName - logical form name
      currentStep - current step entity
      repo - repository used to fetch ordered steps
      Returns:
      next step entity or null if current step is the last
      Throws:
      IllegalStateException - when the provided step does not belong to the form/domain
    • getRowViewItemTypes

      public static final List<String> getRowViewItemTypes()
      Return technical field types used only for row/column layout in the form editor.
      Returns:
      list of non-input layout item field types
    • getFormDataAsMap

      public static final Map<String,String> getFormDataAsMap(FormsEntity form)
      Convert serialized form data into a key/value map.

      Input format is expected as itemFormId~value pairs separated by |. Multi-upload synthetic suffix -fileNames is normalized away.

      Parameters:
      form - persisted form entity with serialized data
      Returns:
      ordered map of field identifiers and their values
    • getFieldName

      public static final String getFieldName(FormItemEntity stepItem, Prop prop)
      Resolve human-readable field name for validation and UI messages.
      Parameters:
      stepItem - form item definition
      prop - localization provider
      Returns:
      localized field label or plain text extracted from custom HTML label
    • getChartStatInfo

      public static final Pair<String,String> getChartStatInfo(jakarta.servlet.http.HttpServletRequest request)
      Extract chart statistics parameters from request.
      Parameters:
      request - HTTP request containing optional formName and itemFormId
      Returns:
      pair of (formName, itemFormId) when both are present, otherwise null
    • getFormId

      public final int getFormId(String formName)
    • getFormIdStatic

      public static int getFormIdStatic(String formName)
    • validateFormInfo

      public final boolean validateFormInfo(String formName, Long currentStepId, jakarta.servlet.http.HttpServletRequest request)
      Validate whether provided form and step represent an allowed step for current session.
      Parameters:
      formName - logical form name
      currentStepId - step id to validate
      request - request with session context
      Returns:
      true when the step exists and is permitted in current session
    • getAutocompleteOptions

      public final List<LabelValue> getAutocompleteOptions(Long stepId, Long itemId, String term, jakarta.servlet.http.HttpServletRequest request)
      Return matching configured options for an autocomplete form item.
      Parameters:
      stepId - current form step
      itemId - autocomplete form item
      term - text entered by the user; at least two characters are required
      request - current form request
      Returns:
      matching label/value options
    • getAvailableConditionFields

      public final List<LabelValue> getAvailableConditionFields(String formName, Long stepId, Prop prop)
      Build available source fields for condition builder up to the current step.
      Parameters:
      formName - logical form name
      stepId - current step id
      prop - localization provider
      Returns:
      sorted condition field options grouped by step labels
    • prepareConditionFieldOptions

      public final List<LabelValue> prepareConditionFieldOptions(List<FormItemEntity> fields, String formName, Prop prop)
      Build condition field options from arbitrary field collection.
      Parameters:
      fields - fields to transform into options
      formName - logical form name used to resolve step labels/positions
      prop - localization provider
      Returns:
      sorted options in format (Step X) Field Name
    • getFormStepsOptions

      public final List<LabelValue> getFormStepsOptions(String formName, Prop prop)
      Build human-readable options for steps of a form suitable for selection inputs.
      Parameters:
      formName - logical form name
      Returns:
      list of label/value pairs ordered by step sorting
    • getValidItemFormId

      public final String getValidItemFormId(FormItemEntity entity)
      Generate a valid and unique itemFormId for a form item within a form.

      For radio fields, derives id from label/placeholder and required flag, allowing multiple radio buttons to share the same logical id. For other fields, generates a unique suffix (numeric) to avoid collisions within the form and domain.

      Parameters:
      entity - form item to generate id for
      Returns:
      unique itemFormId string safe for storage and queries
    • updateFormPattern

      public final void updateFormPattern(String formName)
      Update or create the pattern entity (form definition) for the form by concatenating all unique itemFormIds in validation order. Multi-upload fields append the -fileNames postfix.
      Parameters:
      formName - logical form name
    • saveFormStep

      public final void saveFormStep(String formName, Long stepId, jakarta.servlet.http.HttpServletRequest request, org.json.JSONObject response) throws SaveFormException, IOException
      Validate and persist a single step of a multistep form into session, and optionally finalize the form submission. Performs CSRF/CAPTCHA/file validations and invokes custom processors. When the last step is completed, triggers final save into DB.
      Parameters:
      formName - logical form name
      stepId - current step identifier
      request - HTTP request containing JSON body of field values
      response - JSON response to be enriched with errors, next step or forward
      Throws:
      SaveFormException - when validation or processing fails in a controlled way
      IOException - when reading request body fails
    • getFormItemsForValidation

      public static List<FormItemEntity> getFormItemsForValidation(String formName)
      Fetch distinct minimal form item definitions required for validation for the form.
      Parameters:
      formName - logical form name
      Returns:
      list of simplified FormItemEntity containing id, label, type, regex, required
    • updateStepsPositions

      public void updateStepsPositions(RowReorderDto rowReorderDto)
      Refresh step positions after row reorder operation payload.
      Parameters:
      rowReorderDto - datatable row reorder payload
    • updateStepsPositions

      public void updateStepsPositions(String formName)
      Recompute current and max positions for all steps in a form.
      Parameters:
      formName - logical form name
    • getFormCounter

      public int getFormCounter(String formName, jakarta.servlet.http.HttpServletRequest request)
      Retrieve the form instance counter for the current request.

      When multiple instances of the same form appear on a single page, each gets a unique counter value (1, 2, 3, ...) stored in session during MultistepFormApp.view(org.springframework.ui.Model, jakarta.servlet.http.HttpServletRequest).

      Parameters:
      formName - logical form name
      request - HTTP request with session containing the counter
      Returns:
      form instance counter (1-based), defaults to 1 when not set or invalid
    • updateFormValues

      public static final StringBuilder updateFormValues(String formName, jakarta.servlet.http.HttpServletRequest request, StringBuilder formHtml)