Class ClientUtil

java.lang.Object
com.groiss.smartclient.ClientUtil

public final class ClientUtil extends Object
A potpourri of utility methods for smartclients.
  • Method Details

    • getGuiId

      public static String getGuiId()
      Get the id of the current gui.
      Returns:
      the id of the current gui associated with the session of this threads HttpThread or the ThreadContext Gui attribute or null if there is neither a HttpSession nor a Gui attribute in the ThreadContext available.
    • getGuiId

      public static String getGuiId(HttpServletRequest req)
      Get the id of the current gui from the HttpServletRequest
      Parameters:
      req - the request
      Returns:
      the id of the current gui associated with the session of the request or the ThreadContext Gui attribute or null if there is neither a HttpSession nor a Gui attribute in the ThreadContext available.
    • getChangesAsJSON

      public static JSONObject getChangesAsJSON()
      Get a JSONObject suitable for sending the threads own changes back to the Gui. The guiid used is the one returned by getGuiId().
      Returns:
      a JSONObject
      See Also:
    • getChangesAsJSON

      public static JSONObject getChangesAsJSON(boolean avoidAutoNotification)
      Get a JSONObject suitable for sending the threads own changes back to the Gui. The guiid used is the one returned by getGuiId().
      Parameters:
      avoidAutoNotification - if true, the changes will not be send back to the originating client session via notification.
      Returns:
      a JSONObject
      See Also:
    • getChangesAsJSON

      public static JSONObject getChangesAsJSON(String guiId)
      Get a JSONObject suitable for sending the threads own changes back to the Gui.
      Parameters:
      guiId - the id of the XMLGui to use
      Returns:
      a JSONObject
      See Also:
    • getChangesAsJSON

      public static JSONObject getChangesAsJSON(String guiId, boolean avoidAutoNotification)
      Get a JSONObject suitable for sending the threads own changes back to the Gui.
      Parameters:
      guiId - the id of the XMLGui to use
      avoidAutoNotification - if true, the changes will not be send back to the originating client session via notification.
      Returns:
      a JSONObject
      See Also:
    • toChangeSet

      public static Set<Pair<Long,Long>> toChangeSet(Collection<ActivityInstance> ais)
      Transform a collection of ActivityInstances to a suitable structure for getChangesAsJSON(String, Set).
      Parameters:
      ais - the collection of ActivityInstances
      Returns:
      the change set.
    • getChangesAsJSON

      public static JSONObject getChangesAsJSON(String guiId, Set<Pair<Long,Long>> additionalChangeSet)
      Get a JSONObject suitable for sending the threads own changes back to the Gui.

      Beware: this call commits the actions.

      Parameters:
      guiId - the id of the XMLGui to use; can also be nodeId, then the guiId prefix will be extracted.
      additionalChangeSet - additional information about changes to activityinstances that could not be automatically detected by the system. Can be null.
      Returns:
      a JSONObject with entries for pending, deleted and changed worklist items.
      See Also:
    • getChangesAsJSON

      public static JSONObject getChangesAsJSON(String guiId, Set<Pair<Long,Long>> additionalChangeSet, boolean avoidAutoNotification)
      Get a JSONObject suitable for sending the threads own changes back to the Gui.

      Beware: this call commits the actions.

      According the additionalChangeSet parameter: sometimes the activityinstances do not change, but nevertheless they may end up in different worklists (e.g. movement), since there are no updates associated with this activityinstances, there are no detectable storage events for this activityinstances. The additionalChangeSet parameter can be used to inform this method about such changes. It is a set of pairs of oids and transactionids of such activityinstances that did not change in terms of the database but in terms of gui representation (see: toChangeSet(Collection)).

      N.B.Changes specified via additionalChangeSet will only be reflected in the result of this function and not in notifications for other sessions. If other sessions should be notified about such undetectable changes too, use WfEngine.propagateChange(ActivityInstance) before calling this function.

      Parameters:
      guiId - the id of the XMLGui to use; can also be nodeId, then the guiId prefix will be extracted.
      additionalChangeSet - additional information about changes to activityinstances that could not be automatically detected by the system. Can be null.
      avoidAutoNotification - if true, the changes will not be send back to the originating client session via notification. In this case the it is the responsibility of the caller to send the changes back itself.
      Returns:
      a JSONObject with entries for pending, deleted and changed worklist items. The JSONObject is also written to the log if the log level for com.groiss.smartclient.Util is set at least to DEBUG
    • getAsJSON

      public static Pair<String,JSONObject> getAsJSON(ActivityInstance ai)
      Get a JSONObject from an ActivityInstance in the context of a worklist node. The worklist node is determined from the gui of the current session. The worklists are consulted one after the other just with the ActivityInstance. If a worklist returns something, this worklist is used, no other worklists are consulted. If no worklist returns anything, null is returned.
      Parameters:
      ai - the ActivityInstance
      Returns:
      a Pair the first element is the id of the worklist node, the second element is a JSONObject representation of the ActivityInstance according to the worklist node The JSONObject is also written to the log if the log level for com.groiss.smartclient.Util is set at least to DEBUG
    • getAsJSON

      public static JSONObject getAsJSON(String wlNodeId, ActivityInstance ai)
      Get a JSONObject from an ActivityInstance in the context of a worklist node.
      Parameters:
      wlNodeId - the id of the worklist node in the the XMLGui to use. Must be prefixed by a gui id (e.g. standard.wl).
      ai - the ActivityInstance
      Returns:
      a JSONObject representation of the ActivityInstance according to the worklist node The JSONObject is also written to the log if the log level for com.groiss.smartclient.Util is set at least to DEBUG
    • getAsJSON

      public static JSONObject getAsJSON(String nodeId, HttpServletRequest req, String objectId) throws Exception
      Builds a JSONObject representation for an object as it's expected by the client side store.
      Parameters:
      nodeId - the XML node id. Must be prefixed by the gui id (e.g. standard.mynode).
      req - the current servlet request. Required for initializing the data provider of the node.
      objectId - the id of the object (in the form of classname:oid)
      Returns:
      the JSONObject representing the object.
      Throws:
      Exception
    • getAsJSON

      public static JSONObject getAsJSON(HttpServletRequest req, Persistent object) throws Exception
      Builds a JSONObject representation for an object as it's expected by the client side store. The content depends on the store definition. The request parameter "nodeid" should contain the GUI-id of a table (alternative, deprecated parameter name: node).
      Parameters:
      req - a servlet request
      object - a persistent object
      Returns:
      the JSONObject representing the object.
      Throws:
      Exception
    • getAsJSON

      public static JSONObject getAsJSON(Map<String,Object> row) throws Exception
      Builds a JSONObject from a Map of data for a table row. Performs all the serialization (handles CellValue, Persistent, Date ...)
      Parameters:
      row - the data
      Returns:
      the JSONObject representing the data
      Throws:
      Exception
    • userInput

      public static String userInput(String parameterName, Consumer<ClientUtil.DialogConfiguration> builder)
      Can be used to request input from the user during interactive transactions. If the requested parameter is not yet present on the ThreadContext.getThreadRequest() the current transaction is aborted by throwing an exception, and a dialog is presented to the user. After providing a value, the same request is performed a second time. A common use case would be to request data or confirm proceeding in a workflow-postcondition/preprocessing or when saving a form.

      Examples:
      String name = ClientUtil.userInput("promptTest", dlg -> dlg
      .setMessage("whats your name")
      .setType(Type.PROMPT));


      or:
      ClientUtil.userInput("confirmTest", dlg -> dlg
      .setMessage("Do you really want to continue?"));


      or:
      String gender = ClientUtil.userInput("genderTest", dlg -> dlg
      .setMessage("Choose your gender:") .setOptions(Arrays.asList(new Pair<>("male","male"), new Pair<>("female","female"), new Pair<>("other","other"))));


      To abort the current transaction, a special exception is thrown by this method. Therefore you must not catch any exception thrown by this method to ensure common BeanManager-transaction handling kicks in and performs a clean BeanManager.rollback().

      userInput(String, Consumer) doesn't perform any explicit transaction-handling.
      Parameters:
      parameterName - the parametername to be used on ServletRequest.getParameter(String)
      builder - usually a lambda expression, that configures the passed ClientUtil.DialogConfiguration
      Returns:
      the value
    • userInput

      @Deprecated(forRemoval=true, since="11.0") public static String userInput(String parameterName, Supplier<ClientUtil.DialogConfiguration> builder)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Can be used to request input from the user during interactive transactions. If the requested parameter is not yet present on the ThreadContext.getThreadRequest() the current transaction is aborted by throwing an exception, and a dialog is presented to the user. After providing a value, the same request is performed a second time. A common use case would be to request data or confirm proceeding in a workflow-postcondition/preprocessing or when saving a form.

      Examples:
      String name = ClientUtil.userInput("promptTest", () -> new DialogConfiguration()
      .setMessage("whats your name")
      .setType(Type.PROMPT));


      or:
      ClientUtil.userInput("confirmTest", () -> new DialogConfiguration()
      .setMessage("Do you really want to continue?"));


      or:
      String gender = ClientUtil.userInput("genderTest", () -> new DialogConfiguration()
      .setMessage("Choose your gender:") .setOptions(Arrays.asList(new Pair<>("male","male"), new Pair<>("female","female"), new Pair<>("other","other"))));


      To abort the current transaction, a special exception is thrown by this method. Therefore you must not catch any exception thrown by this method to ensure common BeanManager-transaction handling kicks in and performs a clean BeanManager.rollback().

      userInput(String, Supplier) doesn't perform any explicit transaction-handling.
      Parameters:
      parameterName - the parametername to be used on ServletRequest.getParameter(String)
      builder - usually a lambda expression, that provides a ClientUtil.DialogConfiguration to be used
      Returns:
      the value