Package com.im.df.api.util
Class BatchUpdater
- java.lang.Object
-
- com.im.df.api.util.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)
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BatchUpdater.StopReason
-
Constructor Summary
Constructors Constructor Description BatchUpdater(DFEntityDataProvider edp, com.im.commons.progress.DFEnvironmentRW env, int fromIndex, int toIndex, int failLimit)
Creates updater.BatchUpdater(DFEntityDataProvider edp, com.im.commons.progress.DFEnvironmentRW env, DFUndoManager undoManager, java.lang.String undoMsg, SelectionDescription selection, int failLimit)
BatchUpdater(DFEntityDataProvider edp, com.im.commons.progress.DFEnvironmentRW env, SelectionDescription selection, int failLimit)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract DFUpdateDescription
createUpdate(int index)
Subclass must implement this method and provideDFUpdateDescription
instance for given index (typically row).int
getFailuresCount()
Total count of failures: preparation + update callsjava.util.List<java.lang.Integer>
getPreparationFailures()
Get list of indexes which failed to prepareDFUpdateDescription
instance.int
getProcessedRows()
The count of cycles which were processed so far (including failures).java.lang.String
getProgressMessage(int counter, int total)
Override this method if you want to provide customized progress message.BatchUpdater.StopReason
getStopReason()
Return code of the process: normally finished, cancelled or stopped after too many errors?int
getSuccessUpdates()
Number of successful updates so farjava.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.int
getTotalUpdatesCount()
Total expected count of loop cyclesjava.util.Map<DFUpdateDescription,DFUpdateResult>
getUpdateFailures()
Get all failures during update method callboolean
hasFailures()
Were there already any failures?boolean
resultsNotify(java.util.Map<DFUpdateDescription,DFUpdateResult> results)
Notifies about result of batch update method call.void
run()
Starts the process.
-
-
-
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 callrun()
method.- Parameters:
edp
- Entity data provider which will be used for updating dataenv
- The read/write environment with valid lock forDFEntityDataProvider
manipulationfromIndex
- First index of the loop for updating (typically index of row)toIndex
- Last index of the loopfailLimit
- 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 constantDIFUtilities.UPDATE_DESCRIPTORS_RECOMMENDED_LIMIT
then it's possible that there will be more fails than this limit.
-
BatchUpdater
public BatchUpdater(DFEntityDataProvider edp, com.im.commons.progress.DFEnvironmentRW env, SelectionDescription selection, int failLimit)
-
BatchUpdater
public BatchUpdater(DFEntityDataProvider edp, com.im.commons.progress.DFEnvironmentRW env, DFUndoManager undoManager, java.lang.String undoMsg, SelectionDescription selection, int failLimit)
-
-
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 rowstotal
- 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 finishedprogressCounter
- The current number of processed rowstotalCount
- Total number rows which were supposed to be processedfailuresCount
- 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
-
getUpdateFailures
public java.util.Map<DFUpdateDescription,DFUpdateResult> getUpdateFailures()
Get all failures during update method call
-
getPreparationFailures
public java.util.List<java.lang.Integer> getPreparationFailures()
Get list of indexes which failed to prepareDFUpdateDescription
instance.
-
createUpdate
public abstract DFUpdateDescription createUpdate(int index)
Subclass must implement this method and provideDFUpdateDescription
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 ofDFUpdateDescription
. This can be useful if you find our you are not able to createDFUpdateDescription
for some reason during loop.
-
-