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
public class Importer extends java.lang.Object implements ImpExLogFilter.LocationProvider
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:- 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 Modifier and Type Field Description static longLOG_INTERVALDuration between two logs.
-
Constructor Summary
Constructors Constructor Description Importer(ImpExImportReader importReader)Instantiates aImporterusing givenimportReaderfor reading input and item processing.Importer(CSVReader source)Instantiates aImporterusing givensourceas input for import.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidabortImport()Aborts the import phase by closing all readers and writers, cleaning dump files and switching to finish state.protected voidactivateLogFilter()protected 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 ImpExImportReadercreateImportReaderForNextPass()Creates anImpExImportReaderinstance for next pass using dump handler for getting input stream and dump stream.protected voiddeactivateLogFilter()protected ItemdoImport()Performs thereadLinemethod on usedImpExImportReaderinstance and returns read item.protected voidfinishImport()Finishes the import phase by closing all readers and writers, cleaning dump files and switching state.HeaderDescriptorgetCurrentHeader()Gets the current header descriptor processed by input reader.intgetCurrentLineNumber()Gets the current line number in input stream.java.lang.StringgetCurrentLocation()Gets the current location within input stream as text representation.intgetCurrentPass()Gets the number of the current pass, the import process performs.intgetDumpedLineCountOverall()Returns the amount of lines written to the dump data file in all passes.intgetDumpedLineCountPerHeader()Returns the amount of lines of current header written to the dump data file.intgetDumpedLineCountPerPass()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.DumpHandlergetDumpHandler()Gets the usedDumpHandlerinstance for handling dump medias.ErrorHandlergetErrorHandler()Gets the usedErrorHandlerinstance for handling exceptions while import.intgetLastImportedItemLineNumber()Gets the line number of last imported line at current input.ImpExLogFiltergetLogFilter()intgetMaxPass()Gets the configured amount of maximal passes the import will perform for unresolving value lines.intgetProcessedItemsCountOverall()Returns the overall processed item count.intgetProcessedItemsCountPerHeader()Returns the overall processed item count of current header.intgetProcessedItemsCountPerPass()Returns the overall processed item count of current pass.ImpExImportReadergetReader()Gets the usedImpExImportReaderinstance.intgetResolvedItemsCountOverall()Returns the overall item count processed and not dumped (means all dependencies are resolved).intgetResolvedItemsCountPerPass()Returns the item count processed and not dumped in current pass (means all dependencies are resolved).intgetValueLineCountPerPass()Returns the overall value line count of current pass.booleanhadError()booleanhasUnresolvedLines()Checks if there are unresolved lines written to dump.voidimportAll()The import process will be performed from input stream given in constructor.ItemimportNext()Tries to import the next item from input stream.protected ItemimportNextInternal()protected voidinit()Initializes the instance and switches to running state.booleanisAborted()Checks if instance has finished reading from input (has finished import process).booleanisFinished()Checks if instance has finished reading from input (has finished import process).booleanisRunning()Checks if instance is in import phase currently.protected voidlogProcess()Logs current state to the log4j system.protected booleanprepareNextPass()After 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 Detail
-
LOG_INTERVAL
public static final long LOG_INTERVAL
Duration between two logs.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Importer
public Importer(CSVReader source)
Instantiates aImporterusing givensourceas input for import.- Parameters:
source- encapsulated CSV-stream
-
Importer
public Importer(ImpExImportReader importReader)
Instantiates aImporterusing givenimportReaderfor reading input and item processing.- Parameters:
importReader- reader used for input reading and item processing
-
-
Method Detail
-
init
protected void init() throws ImpExExceptionInitializes 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
protected Item doImport() throws ImpExException
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
protected boolean prepareNextPass() throws ImpExExceptionAfter 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
protected ImpExImportReader createImportReader(CSVReader csvReader)
Creates anImpExImportReaderinstance for processing given input.- Parameters:
csvReader- reader covering input stream- Returns:
- new instance
-
createImportReaderForNextPass
protected ImpExImportReader 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
public ImpExImportReader getReader()
Gets the usedImpExImportReaderinstance.- Returns:
- used reader instance
-
getDumpHandler
public DumpHandler getDumpHandler()
Gets the usedDumpHandlerinstance for handling dump medias.- Returns:
- used dump handler instance
-
getErrorHandler
public ErrorHandler 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:
CSVReader.getCurrentLineNumber()
-
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
public java.lang.String getCurrentLocation()
Gets the current location within input stream as text representation.- Specified by:
getCurrentLocationin interfaceImpExLogFilter.LocationProvider- Returns:
- current location text
- See Also:
ImpExReader.getCurrentLocation()
-
getCurrentHeader
public HeaderDescriptor getCurrentHeader()
Gets the current header descriptor processed by input reader.- Returns:
- current header
- See Also:
ImpExReader.getCurrentHeader()
-
importAll
public void importAll() throws ImpExExceptionThe 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
public final Item importNext() throws ImpExException
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
protected Item importNextInternal() throws ImpExException
- 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
public void setDumpHandler(DumpHandler handler)
Sets theDumpHandlerto use while import process.- Parameters:
handler- new dump handler instance
-
setErrorHandler
public void setErrorHandler(ErrorHandler handler)
Sets theErrorHandlerto use while import.- Parameters:
handler- new error handler instance
-
setLogFilter
public void setLogFilter(ImpExLogFilter filter)
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
public ImpExLogFilter getLogFilter()
-
hadError
public boolean hadError()
-
-