Package de.hybris.platform.impex.jalo
Class Importer
java.lang.Object
de.hybris.platform.impex.jalo.Importer
- All Implemented Interfaces:
ImpExLogFilter.LocationProvider
- Direct Known Subclasses:
MultiThreadedImporter
Central class for processing an import. An import process is always based on a CSV-stream with special ImpEx syntax
(see ImpEx Documentation for how to define an ImpEx CSV-file).
The process is separated in 3 steps:
The process is separated in 3 steps:
- Instantiation:
While instantiating theImporterclass, this CSV-stream will be given using anCSVReaderor anImpExImportReader. If you only want to specify the input stream use anCSVReader, theImporterwill instantiate an correspondingImpExImportReader. The usage of anImpExImportReaderis only preferred, if special settings while instantiation are needed (settings after instantiation can be done using thegetReader()method of theImporterinstance).
Example:
CSVReader reader = new CSVReader( "input.csv", "utf-8" );or
Importer importer = new Importer( reader );
Importer importer = new Importer( new ImpExImportReader( reader, new MyImportProcessor() ) );
- Configuration:
You have several possibilities for configuring the import process.
First you can configure the usedImpExImportReaderusing thegetReadermethod. Here you can configure several things about the reading of the input (skipValueLines, enableCodeExecution, ..) or item processing ( setRelaxedMode, .. ).
Second, you can set anDumpHandlerfor specifying the dump file handling.
Third, you can set anErrorHandlerto specify the process in case of an error.
Fourth, you can set the maximal amount of passes for resolving dumped value lines. - Import:
The import can be performed using theimportNextwhich processes the input stream until an item was processed (insert, update or remove). It returns the processed item.
An other possibility is the usage of theimportAllmethod which calls theimportNextmethod until finishing of the input stream. While and after the import process you have several possibilities to get information about the state (current pass, processed items .. ).
Example:
Item item = null;or
do
{
item = importer.importNext();
System.out.println( "Processed items: " + getProcessedItemsCountOverall() );
}
while( item != null );
importer.importAll();
System.out.println( "Processed items: " + getProcessedItemsCountOverall() );
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionImporter(ImpExImportReader importReader) Instantiates aImporterusing givenimportReaderfor reading input and item processing.Instantiates aImporterusing givensourceas input for import. -
Method Summary
Modifier and TypeMethodDescriptionvoidAborts the import phase by closing all readers and writers, cleaning dump files and switching to finish state.protected voidprotected voidassureStates(de.hybris.platform.impex.jalo.Importer.STATE... states) Assures that the instance is in one of given states.voidclose()Closes the used reader instance.protected ImpExImportReadercreateImportReader(CSVReader csvReader) Creates anImpExImportReaderinstance for processing given input.protected ImpExImportReaderCreates anImpExImportReaderinstance for next pass using dump handler for getting input stream and dump stream.protected voidprotected ItemdoImport()Performs thereadLinemethod on usedImpExImportReaderinstance and returns read item.protected voidFinishes the import phase by closing all readers and writers, cleaning dump files and switching state.Gets the current header descriptor processed by input reader.intGets the current line number in input stream.Gets the current location within input stream as text representation.intGets the number of the current pass, the import process performs.intReturns the amount of lines written to the dump data file in all passes.intReturns the amount of lines of current header written to the dump data file.intReturns the amount of lines written to the dump data file in current pass intended to be processed within a second import run or more.Gets the usedDumpHandlerinstance for handling dump medias.Gets the usedErrorHandlerinstance for handling exceptions while import.intGets the line number of last imported line at current input.intGets the configured amount of maximal passes the import will perform for unresolving value lines.intReturns the overall processed item count.intReturns the overall processed item count of current header.intReturns the overall processed item count of current pass.Gets the usedImpExImportReaderinstance.intReturns the overall item count processed and not dumped (means all dependencies are resolved).intReturns the item count processed and not dumped in current pass (means all dependencies are resolved).intReturns the overall value line count of current pass.booleanhadError()booleanChecks if there are unresolved lines written to dump.voidThe import process will be performed from input stream given in constructor.final ItemTries to import the next item from input stream.protected Itemprotected voidinit()Initializes the instance and switches to running state.booleanChecks if instance has finished reading from input (has finished import process).booleanChecks if instance has finished reading from input (has finished import process).booleanChecks if instance is in import phase currently.protected voidLogs current state to the log4j system.protected booleanAfter checking the need for a next pass, it prepares the next import cycle by callingprepareNextPassof usedDumpHandlerand setting old dump file as input.voidsetDumpHandler(DumpHandler handler) Sets theDumpHandlerto use while import process.voidsetErrorHandler(ErrorHandler handler) Sets theErrorHandlerto use while import.voidsetLogFilter(ImpExLogFilter filter) Sets the logging filter to use while import.voidsetMaxPass(int maxPass) Sets the maximal amount of passes the import process will perform.protected voidswitchToState(de.hybris.platform.impex.jalo.Importer.STATE newState) Sets thecurSteateflag to given state.
-
Field Details
-
LOG_INTERVAL
public static final long LOG_INTERVALDuration between two logs.- See Also:
-
-
Constructor Details
-
Importer
Instantiates aImporterusing givensourceas input for import.- Parameters:
source- encapsulated CSV-stream
-
Importer
Instantiates aImporterusing givenimportReaderfor reading input and item processing.- Parameters:
importReader- reader used for input reading and item processing
-
-
Method Details
-
init
Initializes the instance and switches to running state.- Throws:
ImpExException- error while dump file creation
-
switchToState
protected void switchToState(de.hybris.platform.impex.jalo.Importer.STATE newState) Sets thecurSteateflag to given state.- Parameters:
newState- new state to which the current state will be set
-
logProcess
protected void logProcess()Logs current state to the log4j system. Will log only if log level and log interval fits. -
doImport
Performs thereadLinemethod on usedImpExImportReaderinstance and returns read item. If setErrorHandlerevaluated an error to ignore, the next item will be read.- Returns:
- read item or null
- Throws:
ImpExException- error while reading from input or item processing
-
prepareNextPass
After checking the need for a next pass, it prepares the next import cycle by callingprepareNextPassof usedDumpHandlerand setting old dump file as input.- Returns:
- true if import process is ready to continue with next pass (input is configured and ready for reading), false if a next pass is not needed
- Throws:
ImpExException- error while preparing dump files
-
finishImport
protected void finishImport()Finishes the import phase by closing all readers and writers, cleaning dump files and switching state. -
abortImport
public void abortImport()Aborts the import phase by closing all readers and writers, cleaning dump files and switching to finish state. -
assureStates
protected void assureStates(de.hybris.platform.impex.jalo.Importer.STATE... states) Assures that the instance is in one of given states. ThrowsIllegalStateExceptionif instance is not in one of given states currently.- Parameters:
states- states to check for
-
createImportReader
Creates anImpExImportReaderinstance for processing given input.- Parameters:
csvReader- reader covering input stream- Returns:
- new instance
-
createImportReaderForNextPass
Creates anImpExImportReaderinstance for next pass using dump handler for getting input stream and dump stream.- Returns:
- new reader instance for processing next pass
-
isRunning
public boolean isRunning()Checks if instance is in import phase currently.- Returns:
- true if instance is in import phase, else false
-
isFinished
public boolean isFinished()Checks if instance has finished reading from input (has finished import process).- Returns:
- true if instance has finished reading from input, else false
-
isAborted
public boolean isAborted()Checks if instance has finished reading from input (has finished import process).- Returns:
- true if instance has finished reading from input, else false
-
getMaxPass
public int getMaxPass()Gets the configured amount of maximal passes the import will perform for unresolving value lines.- Returns:
- amount of maximal passes
-
getCurrentPass
public int getCurrentPass()Gets the number of the current pass, the import process performs.- Returns:
- current pass number
-
getReader
Gets the usedImpExImportReaderinstance.- Returns:
- used reader instance
-
getDumpHandler
Gets the usedDumpHandlerinstance for handling dump medias.- Returns:
- used dump handler instance
-
getErrorHandler
Gets the usedErrorHandlerinstance for handling exceptions while import.- Returns:
- used error handler
-
hasUnresolvedLines
public boolean hasUnresolvedLines()Checks if there are unresolved lines written to dump.- Returns:
- true if there are unresolved lines, otherwise false
-
getValueLineCountPerPass
public int getValueLineCountPerPass()Returns the overall value line count of current pass. This count includes all value lines which are read, including skipped, unresolved or otherwise unprocessable lines!- Returns:
- the overall value line count of current pass
-
getProcessedItemsCountPerHeader
public int getProcessedItemsCountPerHeader()Returns the overall processed item count of current header.- Returns:
- overall processed item count of current header
-
getProcessedItemsCountPerPass
public int getProcessedItemsCountPerPass()Returns the overall processed item count of current pass. EqualsgetValueCountPerPasswithout skipped and discarded value lines.- Returns:
- the overall processed item count of current pass
-
getProcessedItemsCountOverall
public int getProcessedItemsCountOverall()Returns the overall processed item count.- Returns:
- the overall processed item count
-
getDumpedLineCountPerHeader
public int getDumpedLineCountPerHeader()Returns the amount of lines of current header written to the dump data file.- Returns:
- amount of lines of current header written to the dump data file
-
getResolvedItemsCountPerPass
public int getResolvedItemsCountPerPass()Returns the item count processed and not dumped in current pass (means all dependencies are resolved).- Returns:
- overall item count processed and not dumped in current pass
-
getResolvedItemsCountOverall
public int getResolvedItemsCountOverall()Returns the overall item count processed and not dumped (means all dependencies are resolved).- Returns:
- overall item count processed and not dumped in current pass
-
getDumpedLineCountPerPass
public int getDumpedLineCountPerPass()Returns the amount of lines written to the dump data file in current pass intended to be processed within a second import run or more.
If this method returns 0 all value lines of current pass should have been processed properly and no second pass is necessary.- Returns:
- amount of lines written to the dump data file in current pass
-
getDumpedLineCountOverall
public int getDumpedLineCountOverall()Returns the amount of lines written to the dump data file in all passes.
ATTENTION: A value greater 0 means that there was a pass with dumped lines, it does not say anything about the current pass exclusively.- Returns:
- amount of lines written to the dump data file in current pass
-
getCurrentLineNumber
public int getCurrentLineNumber()Gets the current line number in input stream.- Returns:
- current line number of input
- See Also:
-
getLastImportedItemLineNumber
public int getLastImportedItemLineNumber()Gets the line number of last imported line at current input.- Returns:
- current line number of last imported line of input
-
getCurrentLocation
Gets the current location within input stream as text representation.- Specified by:
getCurrentLocationin interfaceImpExLogFilter.LocationProvider- Returns:
- current location text
- See Also:
-
getCurrentHeader
Gets the current header descriptor processed by input reader.- Returns:
- current header
- See Also:
-
importAll
The import process will be performed from input stream given in constructor. Before calling this method you can configure theDumpHandler, theErrorHandlerand the maximal amount of passes. The call will automatically perform the whole import process including the dumped line processing (if maximal passes are a positive number). You can call theimportNextmethod too for importing item based. This method is only a convenience kind which calls theimportNextmethod until it returns null.- Throws:
ImpExException- an error is occurred while import processing (reasoned by input stream errors, dump file handling or item processing)
-
importNext
Tries to import the next item from input stream. If called for one time, you can not callset..methods anymore. If the input stream ends and there are unresolved lines, the call will automatically start the next pass by using the dump file as new input stream.- Returns:
- the last imported item
- Throws:
ImpExException- ImpExException an error is occurred while import processing (reasoned by input stream errors, dump file handling or item processing)
-
importNextInternal
- Throws:
ImpExException
-
activateLogFilter
protected void activateLogFilter() -
deactivateLogFilter
protected void deactivateLogFilter() -
close
public void close()Closes the used reader instance. Is not needed to call if the import process has finished (isFinished()returnstrue). -
setMaxPass
public void setMaxPass(int maxPass) Sets the maximal amount of passes the import process will perform. Use -1 for infinit loops (will break if no changes occur anymore).- Parameters:
maxPass- new amount of maximal passes
-
setDumpHandler
Sets theDumpHandlerto use while import process.- Parameters:
handler- new dump handler instance
-
setErrorHandler
Sets theErrorHandlerto use while import.- Parameters:
handler- new error handler instance
-
setLogFilter
Sets the logging filter to use while import. The default one extends each message with the current reader location except for info logs.- Parameters:
filter- new error handler instance
-
getLogFilter
-
hadError
public boolean hadError()
-