Class DefaultImportManager

java.lang.Object
com.broadleafcommerce.dataimport.service.DefaultImportManager
All Implemented Interfaces:
ImportManager

@DataRouteByExample(Import.class) public class DefaultImportManager extends Object implements ImportManager
Author:
Phillip Verheyden (phillipuniverse)
  • Constructor Details

    • DefaultImportManager

      public DefaultImportManager(com.broadleafcommerce.common.extension.TypeFactory typeFactory, StorageService storage, ImportService<Import> importService, List<ImportProcessor> processors, List<ImportValidator> validators, List<FileReader<? extends FileReaderContext>> readers, @Nullable com.broadleafcommerce.common.extension.data.DataRouteReference reference, List<com.broadleafcommerce.data.tracking.core.web.ContextRequestHydrator> contextRequestHydrators, @Nullable com.broadleafcommerce.data.tracking.core.policy.trackable.TrackablePolicyUtils trackablePolicyUtils)
  • Method Details

    • initiateImport

      public Import initiateImport(ImportRequest request, org.springframework.core.io.InputStreamSource inputStreamSource, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: ImportManager

      Validates, prepares, and requests an import to be executed from the given request, but does not actually execute the import. The output of this is that the import is scheduled and returned back to the caller.

      Emits a ProcessImportRequest.

      Delegates to ImportValidator for validation.

      Specified by:
      initiateImport in interface ImportManager
      Parameters:
      request - metadata around where the import will go.

      For each of the "importing context" fields describing where the operation will be performed, the default implementation will first check if the field is supplied in ImportRequest, and if not, use the value from the ContextInfo.

      inputStreamSource - an input stream source for the data that should be used to import
      contextInfo - context information surrounding sandboxing and multitenant state. This is the context from which the operation itself is being initiated.
      Returns:
      the newly created Import if valid
    • generateImportFromRequest

      protected Import generateImportFromRequest(ImportRequest request)
      Parameters:
      request - the import request that was received
      Returns:
      an Import object initialized from the ImportRequest
    • initializeContextFields

      protected void initializeContextFields(Import target, ImportRequest request, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Initializes the context-related fields on the given Import, including the fields describing the context where the operation itself was initiated (such as Import.getApplicationId()), and "importing context" fields describing the context where the operation will be performed (such as Import.getImportingApplicationId()).

      For each of the "importing context" fields describing where the operation will be performed, the default implementation will first check if the field is supplied in ImportRequest, and if not, use the value from the ContextInfo.

      Importing catalog ID initialization requires access to the specification, and is done in initializeImportingCatalogId(Import, ImportRequest, ImportSpecification, ContextInfo).

      Importing sandbox ID initialization requires access to the specification, and is done in initializeImportingSandboxFields(Import, ImportRequest, ImportSpecification, ContextInfo).

      Parameters:
      target - the Import whose context-related fields should be initialized
      request - the ImportRequest which should be checked as a potential source for context fields
      contextInfo - the context info used in the request to start the import. Not necessarily the same as the context the import's data will go into.
    • initializeImportingCatalogId

      protected void initializeImportingCatalogId(Import target, ImportRequest request, ImportSpecification spec, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo requestingContextInfo)
      Initializes Import.getImportingCatalogId().

      Separated from initializeContextFields(Import, ImportRequest, ContextInfo) since this requires having the specification available. Fetching the specification itself requires the importing context (with just app/tenant information) to be available. Thus, the other fields are initialized first, and importing catalog ID is initialized here, in a later step.

      Parameters:
      target - the import on which to initialize Import.getImportingCatalogId()
      request - the original import request
      spec - the specification for the import
      requestingContextInfo - the requesting context info from which the import was initiated (not the ultimate importing context info)
    • initializeImportingSandboxFields

      protected void initializeImportingSandboxFields(Import target, ImportRequest request, ImportSpecification spec, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo requestingContextInfo)
      Initializes sandbox-related fields on the importing context.

      Separated from initializeContextFields(Import, ImportRequest, ContextInfo) since this requires having the specification available. Fetching the specification itself requires the importing context (with just app/tenant information) to be available. Thus, the other fields are initialized first, and importing sandbox information is initialized here, in a later step.

      Parameters:
      target - the import on which to initialize importing sandbox related fields
      request - the original import request
      spec - the specification for the import
      requestingContextInfo - the requesting context info from which the import was initiated (not the ultimate importing context info)
    • validateCanPerformImport

      protected void validateCanPerformImport(Import metadata, ImportSpecification spec, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo requestingContextInfo)
    • validateImportingContextAccessibility

      protected void validateImportingContextAccessibility(Import metadata, ImportSpecification spec, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo requestingContextInfo)
      Builds a ContextInfo matching the importing context fields from metadata, and then evaluates whether the currently authenticated user is allowed to perform the operation in that context.

      In the event that policy validation is disabled, the TrackablePolicyUtils bean will be unavailable and thus this step will be skipped.

      Parameters:
      metadata - the import whose importing context fields should be checked for accessibility
      spec - the import specification to use for this import
      requestingContextInfo - the context info used in the request to start the import. Not necessarily the same as the context the import's data will go into.
      Throws:
      com.broadleafcommerce.data.tracking.core.exception.NotVisibleException - if the importing context is not visible by the current user
      com.broadleafcommerce.data.tracking.core.exception.NotMutableException - if the importing context is not mutable by the current user
    • validateImportingContextReadableAndMutable

      protected void validateImportingContextReadableAndMutable(@Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo importingContextInfo)
    • validateImportingVendorAccessibility

      protected void validateImportingVendorAccessibility(Import metadata, ImportSpecification spec, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo requestingContextInfo, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo importingContextInfo)
    • buildImportingContextInfo

      public com.broadleafcommerce.data.tracking.core.context.ContextInfo buildImportingContextInfo(Import metadata, com.broadleafcommerce.data.tracking.core.type.OperationType operationType)
      Description copied from interface: ImportManager
      Convenience method to construct the 'importing' ContextInfo based on the Import metadata.

      This represents the context that the import will actually be performed in, rather than the context the import itself was requested from.

      Note that this method should not perform any security or access validation on the resulting context info and is merely used to construct the instance itself.

      Specified by:
      buildImportingContextInfo in interface ImportManager
      Parameters:
      metadata - the import instance itself from which context fields will be sourced
      operationType - the operation type to set on the context info
      Returns:
      the importing context info for the given import
    • initializeParentEntityId

      protected void initializeParentEntityId(ImportRequest request, Import newImport)
      Initializes Import.getParentEntityId() from ImportRequest.getParentEntityId().

      The value itself should already be validated via ImportValidator before invoking this method.

      Parameters:
      request - the request to initialize the field from
      newImport - the destination instance to set the field on
    • resolveParentEntityId

      @Deprecated(since="1.8.2", forRemoval=true) protected Optional<String> resolveParentEntityId(ImportRequest request, Import newImport)
      Deprecated, for removal: This API element is subject to removal in a future version.
      If the imported entity has parent entity, we need to resolve it's id. In case of grid level import - parent entity id will be included in the request ImportRequest.parentEntityId. In case of global import - parent entity id should be extracted from the import file.
      Parameters:
      request - the import request
      newImport - initiated import
      Returns:
      parent entity id or null if not exists for the entity
    • validateRequest

      protected ImportValidationResponse validateRequest(ImportRequest request, org.springframework.core.io.InputStreamSource inputStreamSource, ImportSpecification spec)
    • userHasRequiredAuthorities

      protected ImportValidationResponse userHasRequiredAuthorities(ImportSpecification spec)
    • findValidator

      @Nullable protected ImportValidator findValidator(ImportRequest request)
    • determineImportingSandboxId

      @Nullable protected String determineImportingSandboxId(ImportRequest request)
      If ImportRequest.productionImport is true or if ImportRequest.importingSandboxId is blank, this will return null.

      Otherwise, this will return ImportRequest.importingSandboxId.

      Note that in the case that ImportRequest.productionImport is false and ImportRequest.importingSandboxId is blank, this means a new sandbox needs to be created for this import. The generation of this new sandbox ID is not done at this stage. Instead, it's done in processImport(String). At this point, we will just guarantee it is null.

      Parameters:
      request - the import request
      Returns:
      the requested sandbox ID if it is not blank and this is a non-production import, null otherwise
    • determineName

      protected String determineName(ImportRequest request)
    • generateName

      protected String generateName(ImportRequest request)
    • initializeSandboxNameToCreate

      protected void initializeSandboxNameToCreate(Import newImport, ImportRequest request)
      Sets the Import.importingSandboxNameToCreate to the requested value if provided, else sets it to the name of the import.
      Parameters:
      newImport - the import whose field needs to be initialized. This should be the import after its Import.name has already been finalized/generated and set.
      request - the import request
    • doesRequireSandboxCreation

      protected boolean doesRequireSandboxCreation(Import metadata)
    • generateStoragePath

      protected String generateStoragePath(ImportRequest request)
    • processImport

      public void processImport(String importId)
      Description copied from interface: ImportManager
      Begins processing the import by reading the uploaded file and parsing out the items within it.

      If a new sandbox needs to be created for the import, emits a CreateSandboxRequest.

      Specified by:
      processImport in interface ImportManager
      Parameters:
      importId - an import managed by the persistent store. The import passed here should have its status as ImportStatus.REQUESTED
    • markProcessing

      @Nullable protected Import markProcessing(String importId)
    • markErrored

      @Nullable protected Import markErrored(String importId, String errorMessage)
    • notifyOfDeleteSandboxRequest

      protected void notifyOfDeleteSandboxRequest(String importId)
    • createSandbox

      @Nullable protected Import createSandbox(String importId, String importingSandboxId)
    • setTotalItems

      @Nullable protected Import setTotalItems(String importId, long totalItems)
    • findProcessor

      @Nullable protected ImportProcessor findProcessor(Import metadata)
    • findReader

      @Nullable protected FileReader<FileReaderContext> findReader(String fileType)
    • createProcessingContext

      protected ImportProcessingContext<FileReaderContext> createProcessingContext(Import metadata, ImportSpecification importSpecification, FileReader<FileReaderContext> reader, long dataLinesToSkip) throws IOException
      Throws:
      IOException
    • createValidationContext

      protected ImportValidationContext<FileReaderContext> createValidationContext(ImportRequest request, ImportSpecification importSpecification, FileReader<FileReaderContext> fileReader, org.springframework.core.io.InputStreamSource inputStreamSource) throws IOException
      Throws:
      IOException
    • getTypeFactory

      @NonNull protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory()
    • getStorage

      @NonNull protected StorageService getStorage()
    • getImportService

      @NonNull protected ImportService<Import> getImportService()
    • getProcessors

      @NonNull protected List<ImportProcessor> getProcessors()
    • getValidators

      @NonNull protected List<ImportValidator> getValidators()
    • getReaders

      @NonNull protected List<FileReader<? extends FileReaderContext>> getReaders()
    • setImportSpecificationService

      @Autowired public void setImportSpecificationService(@NonNull ImportSpecificationService importSpecificationService)
    • getImportSpecificationService

      @NonNull protected ImportSpecificationService getImportSpecificationService()
    • getReference

      @Nullable protected com.broadleafcommerce.common.extension.data.DataRouteReference getReference()
    • getContextRequestHydrators

      protected List<com.broadleafcommerce.data.tracking.core.web.ContextRequestHydrator> getContextRequestHydrators()
    • getTrackablePolicyUtils

      @Nullable protected com.broadleafcommerce.data.tracking.core.policy.trackable.TrackablePolicyUtils getTrackablePolicyUtils()
      Used to validate that the user initiating an operation is able to access and mutate the context in which the operation will apply.

      If policy validation is disabled, this bean will be unavailable and policy validation will be skipped.

    • getSECURE_RANDOM

      @NonNull protected static Random getSECURE_RANDOM()
    • getUTC_TIMESTAMP_FORMATTER

      @NonNull protected static DateTimeFormatter getUTC_TIMESTAMP_FORMATTER()