Class BasePresenter

java.lang.Object
com.inet.pdfc.presenter.BasePresenter
All Implemented Interfaces:
NamedExtension
Direct Known Subclasses:
ConsolePresenter, DifferencesImagePresenter, ExportPresenter, JsonPresenter, ReportPresenter

public abstract class BasePresenter extends Object implements NamedExtension
A Presenter is responsible to 'present' the result of the comparison in some way.

This class the base class for a result presenter. It automatically registers to a model and should present the result presented by the ResultModel in some way.
There are multiple callback-methods which subclasses can override to react on the event or to present the result.
The presenter must react on errors which occur while reading or comparing the files, see onError(ExceptionData, boolean, ERROR_SOURCE). Exceptions are not thrown by the PDFComparer.

Implementation: To make a presenter generally available, it should only depend on the model and additional dependencies which are passed in the constructor.

Note: The presenter is only meant to present a result. If you want to get progress notifications for instance, please register a DataGeneratorListener to the actual AbstractComparator

Note: The presenter is stateful, not stateless. This means it cannot be used concurrently by multiple comparisons, but it can be used by subsequent comparisons.
Since:
i-net PDFC 3.0
  • Field Details

    • MAX_FILENAME_LENGTH

      public static final int MAX_FILENAME_LENGTH
      The maximum length the filename of an exported file is allowed to have without extension.
      If the names of the compared files plus the additional characters are too long, then the exported name is truncated to this length.
      Since:
      i-net PDFC 3.0
      See Also:
    • LOGGER

      public static final com.inet.logging.Logger LOGGER
      Default Logger for presenter
  • Constructor Details

    • BasePresenter

      public BasePresenter()
  • Method Details

    • configure

      @Deprecated public BasePresenter configure(IProfile configuration)
      Deprecated.
      Configures the presenter according to a configuration. The presenter will read the property PDFCProperty.MAX_ERRORS_PER_FILE from the configuration.
      Parameters:
      configuration - the configuration to read from, must not be null
      Returns:
      this instance for concatenation
      Since:
      i-net PDFC 3.2
    • onClear

      protected void onClear() throws Exception
      Called upon comparison startup. This does not imply which data is available to the model, but most likely the model has been cleared at this point.
      Throws:
      Exception - thrown in case the processing of the clear step fails
      Since:
      i-net PDFC 3.0
    • onInit

      protected void onInit() throws Exception
      Called at the time the document informations have been set.
      Throws:
      Exception - thrown in case the processing of the init step fails
      Since:
      i-net PDFC 3.0
    • onDataUpdate

      protected void onDataUpdate() throws Exception
      Called whenever the referred model receives a data chunk. This does not imply that any differences are available yet as it is called as well for no-difference areas.
      Throws:
      Exception - thrown in case the processing of the update step fails
      Since:
      i-net PDFC 3.0
    • onComparisonDone

      protected abstract void onComparisonDone() throws Exception
      Called to indicate that a comparison has finished. This does not imply that any page data or differences are available.
      Throws:
      Exception - thrown in case the processing of the finish step fails
      Since:
      i-net PDFC 3.0
    • onFinish

      protected void onFinish()
      OPTIONAL: Called in case of batch comparison at the end of all comparisons of the batch run. This method cannot be triggered by an event of any referenced result model. It's sole purpose is to serve as a callback for the PDFComparer.batchCompare(java.io.File, java.io.File) method.
      NOTE: This method only called once per batch compare and only for the root presenter, never for spawned ones.
      Since:
      i-net PDFC 3.0
    • onError

      protected void onError(ExceptionData error, boolean interrupted, BasePresenter.ERROR_SOURCE source)
      Called in case of an error that occurred either in one of the parser threads or in the compare thread.
      NOTE: This method is only called for exceptions which are notified to the ResultModel.
      Parameters:
      error - the exception
      interrupted - indicates whether the comparison was interrupted due to this exception. If true there will be no further calls (e.g. onComparisonDone)
      source - the source module of the exception.
      Since:
      i-net PDFC 3.0
    • onFilterChange

      protected void onFilterChange() throws Exception
      Called whenever the filter conditions of the referenced model have changed. This method is not called automatically during a comparison but rather a reaction to a configuration change that can be applied to the model.
      Throws:
      Exception - thrown in case the processing of the filter update fails
      Since:
      i-net PDFC 3.0
    • onProgressUpdate

      protected void onProgressUpdate(float progress)
      Called whenever the comparison sends a progress state. In some rare cases the same progress value may be sent multiple times.
      Parameters:
      progress - the progress in percent, a value between 0 and 100, bounds included.
      Since:
      i-net PDFC 4.0
    • executeImmediately

      public void executeImmediately(ResultModel model) throws Exception
      Forces an execution of the presenter no matter the current model state.
      Use this method to run a presenter for an already finished model.
      Parameters:
      model - the model to be presented, if null the currently set model will be presented. This model will not be set persistently to the presenter, it will be detached at the end of this method
      Throws:
      IllegalStateException - in case the no model has been set yet
      Exception - thrown in case the processing of fails; this exception is published to the caller to allow a feedback to the user
      Since:
      i-net PDFC 3.0
    • setModel

      public void setModel(ResultModel newModel)
      Registers this presenter to a model. If it already was registered to a model, the old model will be detached first.
      Parameters:
      newModel - the new
      Since:
      i-net PDFC 3.0
    • getModel

      protected ResultModel getModel()
      Returns the model this presenter is registered to
      Returns:
      the model, is null until a comparison was started where this Presenter was added to.
      Since:
      i-net PDFC 3.0
    • detachFromModel

      public void detachFromModel()
      Detaches this presenter from a model. Call this method if the presenter should no longer generate output for the referenced model.
      Since:
      i-net PDFC 3.0
    • getDefaultExportName

      protected String getDefaultExportName(String presenterType)
      Returns the default export file name of the current comparison.
      This method will truncate parts of the second compared file which are equal to the first one, so only the different part of the name remains. Will also limit the finally returned name to MAX_FILENAME_LENGTH characters by truncating both compared file's names again.
      If you want to retain this behavior but want a different Exportname, override createExportFilename(String, String, String) which is called by the implementation of this method in BasePresenter.
      Parameters:
      presenterType - the identifier of the calling presenter, will be part of the export name, should not be null
      Returns:
      the export name WITHOUT SUFFIX, or null if the comparison was not started yet
      Since:
      i-net PDFC 3.0
      See Also:
    • createExportFilename

      public String createExportFilename(String presenterType, String n1, String n2)
      Construct the default Export file name used for exports.
      This is called by getDefaultExportName(String) in BasePresenter with dummy parameters to determine how much characters will be added by this method. That is required to truncate the compared files names in order to return a filename which does not exceed a limit of MAX_FILENAME_LENGTH characters.
      You can safely override this, but make sure that the returned String's length minus n1.length minus n2.length is constant for any n1 or n2 which are not empty strings.
      Parameters:
      presenterType - the Sting passed to getDefaultExportName(String) which describes the presenter
      n1 - the possibly truncated name of the first file, or an empty string for the check for how many characters this methods adds
      n2 - the possibly truncated name of the second file, or an empty string for the check for how many characters this methods adds
      Returns:
      the resulting Export file name, without an extension.
      Since:
      i-net PDFC 3.0
      See Also:
    • getLastPresenterException

      public PresenterExceptionData getLastPresenterException()
      Return the last exception for this presenter. The presenter runs independent and has no effect for the comparison or other presenters.
      Returns:
      the last exception for this presenter or null if no exception available
      Since:
      5.0
    • getPresenterExceptions

      public List<PresenterExceptionData> getPresenterExceptions()
      Return all exceptions for a this presenter. The presenter runs independent and has no effect on the comparison or other presenters.
      Returns:
      all exceptions for a this presenter, empty list for no thrown exception
      Since:
      5.0
    • spawn

      public BasePresenter spawn(boolean spawnWithParent)
      Creates another presenter of the the same type as the current one. The spawned presenter may either have the same settings as the current one or it may be a child of the current one.
      Parameters:
      spawnWithParent - if true, the presenter is allowed to keep a reference to it's parent to create a summary on onFinish()
      Returns:
      a new presenter for the same batch comparison run as the current one
      Throws:
      IllegalStateException - in case the spawn was not successful
      Since:
      i-net PDFC 4.0
    • setParent

      protected void setParent(BasePresenter parent)
      Sets the parent presenter for spawned presenters. Can be used as an central instance to present a summary in batch comparisons
      Parameters:
      parent - the parent presenter.
      Since:
      4.0
    • getParent

      protected BasePresenter getParent()
      Returns the parent presenter. Can be used as an central instance to present a summary in batch comparisons
      Returns:
      the parent or null
      Since:
      4.0
    • canInformUserOnErrors

      protected boolean canInformUserOnErrors()
      Returns whether the presenter is able to handle errors in a way to notify the user. E.g. write the error to the log file/console or pop up an error dialog.
      Returns:
      true of onError(ExceptionData, boolean, ERROR_SOURCE) will directly inform the user.
      Since:
      4.0