Class BatchUpdater


  • public abstract class BatchUpdater
    extends java.lang.Object
    Utility class for multi-row update which can simplify loop for updating data in entities.

    This class provides these functions for you:

    • Loop control
    • Checking DFFeedback.isCancelled() and stopping the loop
    • Updating progress provided by DFEnvironmentRO.getFeedback()
    • Undo/redo support according to configuration given in constructor
    • Customizable messages for progress, cancel
    • Customizable stop criteria (e.g. based on failures of update method calls)
    Example of usage:
           DFField field = ....
           DFResultSet.VertexState vs = ...
           DFEntity entity = vs.getVertex().getEntity();
           DFEntityDataProvider edp = DFEntityDataProviders.find(entity);
           DFEnvironmentRW env = ...
           DFUndoConfig undoConfig = DFUndoConfig.create("Deleting data in column "+field.getName(), true);
           SelectionDescription sel = vs.getSelection();
           Map<String,Object> data = new HashMap<String,Object>();
           data.put(f.getId(), null);
    
           BatchUpdater updater = new BatchUpdater(edp, env, undoConfig, sel.getMinSelectionIndex(), sel.getMaxSelectionIndex(), 1) { // NO-CHECKSTYLE
               public DFUpdateDescription createUpdate(int index) {
                   return DFUpdateDescription.create(entity, vs.getIdAt(index), data);
               }
    
               public String getProgressMessage(int counter, int total) {
                   return "Deleted " + counter + " out of " + total + " rows";
               }
    
               public String getCancelledMessage(int progressCounter, int failuresCount, int totalCount) {
                   return "Delete stopped after " + progressCounter + " out of " + totalCount + "(failures: " + failuresCount + ")"; // NO-CHECKSTYLE
               }
           };
           updater.run();
           if (updater.hasFailures()) {
               Collection<DFUpdateResult> failures = updater.getUpdateFailures().values();
               ....
           }
     
    Author:
    Petr Hamernik
    • Constructor Detail

      • BatchUpdater

        public BatchUpdater​(DFEntityDataProvider edp,
                            com.im.commons.progress.DFEnvironmentRW env,
                            int fromIndex,
                            int toIndex,
                            int failLimit)
        Creates updater. Once the instance is created you should call run() method.
        Parameters:
        edp - Entity data provider which will be used for updating data
        env - The read/write environment with valid lock for DFEntityDataProvider manipulation
        fromIndex - First index of the loop for updating (typically index of row)
        toIndex - Last index of the loop
        failLimit - Loop will stop if update method calls fails for more rows than this limit. As the update method is called for more rows in a single call (this implementation uses constant DIFUtilities.UPDATE_DESCRIPTORS_RECOMMENDED_LIMIT then it's possible that there will be more fails than this limit.
    • Method Detail

      • run

        public void run()
        Starts the process.
      • getStopReason

        public BatchUpdater.StopReason getStopReason()
        Return code of the process: normally finished, cancelled or stopped after too many errors?
      • getProgressMessage

        public java.lang.String getProgressMessage​(int counter,
                                                   int total)
        Override this method if you want to provide customized progress message.
        Parameters:
        counter - The current number of processed rows
        total - Total number rows to be processed
        Returns:
        The message which is used in progress bar in IJC
      • getSummaryMessage

        public java.lang.String getSummaryMessage​(BatchUpdater.StopReason reason,
                                                  int progressCounter,
                                                  int totalCount,
                                                  int failuresCount)
        Override this method if you want to provide customized operation summary message when operations is finished.
        Parameters:
        reason - Why operation is finished
        progressCounter - The current number of processed rows
        totalCount - Total number rows which were supposed to be processed
        failuresCount - The number of update method calls which failed
        Returns:
        The message which is used in progress bar in IJC
      • resultsNotify

        public boolean resultsNotify​(java.util.Map<DFUpdateDescription,​DFUpdateResult> results)
        Notifies about result of batch update method call. You can override this method and change the decision if process should continue after failures (for example).
        Parameters:
        results - The partial results
        Returns:
        True if process should continue otherwise false
      • getSuccessUpdates

        public int getSuccessUpdates()
        Number of successful updates so far
      • getTotalUpdatesCount

        public int getTotalUpdatesCount()
        Total expected count of loop cycles
      • getFailuresCount

        public int getFailuresCount()
        Total count of failures: preparation + update calls
      • getProcessedRows

        public int getProcessedRows()
        The count of cycles which were processed so far (including failures).
      • hasFailures

        public boolean hasFailures()
        Were there already any failures?
        Returns:
        True if there is at least one failure of update method call
      • getPreparationFailures

        public java.util.List<java.lang.Integer> getPreparationFailures()
        Get list of indexes which failed to prepare DFUpdateDescription instance.
      • createUpdate

        public abstract DFUpdateDescription createUpdate​(int index)
        Subclass must implement this method and provide DFUpdateDescription instance for given index (typically row).

        It's allowed that this method returns null. In this case the current index is added to list of failures during preparation of DFUpdateDescription. This can be useful if you find our you are not able to create DFUpdateDescription for some reason during loop.