Creating import/export processes in CRM.COM software gives a developer the ability to import/export data from/into the system using three supported file types: XML, CSV and EXCEL.
What does this section cover?
This section describes the importing and exporting mechanism. Importing and exporting are both considered process runs. For more information on implementing process runs, go to Implement Process Run Definition.
Create Import Processes
To create an import process, you need to:
- Define the import process in the imports.xml metadata file.
- Create the template file for the import process, as defined in the imports.xml metadata file.
- Create a process class extending com.crm.process.CRMImportBean, responsible for parsing the file and processing its records and define this process in modules.xml metadata file.
In the following example, we will implement a multithreaded mechanism to import traceable physical goods as installed items.
In order to do so, we create CRMImportInstalledItemBean.java and implement importInstalledItem method, which parses a file, and by using threads, it saves and posts warehouse transactions, and marks each process as completed or failed accordingly, until all installed items' records have been processed.
public class CRMImportInstalledItemBean extends CRMImportBean {
@EJB private ImportInstalledItemXMLUtilBean importInstalledItemXMLUtilBean;
...
public void importInstalledItem(String processRunLogID)throws Exception{
CRMDOProcessRunLog processRun = (CRMDOProcessRunLog)processRunLogBean.load(processRunLogID);
ImportInstalledItemDefinition importInstalledItemDefinition = new ImportInstalledItemDefinition();
importInstalledItemDefinition.setProcessRunDefinition(processRun.getProcessRunDefinition());
importInstalledItemDefinition = importInstalledItemXMLUtilBean.loadFromXML(importInstalledItemDefinition);
ArrayList<CRMDO> attachents = attachmentBean.load(processRun);
processRun.setStatus(ProcessRunLogStatus.IN_PROGRESS);
processRun.setStartDate(getCurrentDate());
processRunLogBean.save(processRun);
Boolean processComplete = new Boolean(false);
try
{
for (int i=0;i<attachents.size();i++)
{
CRMDOAttachment attachment = (CRMDOAttachment)attachents.get(i);
String[] tokens = StringUtil.split(attachment.getFileName(),".");
String extension = tokens[tokens.length-1];
SupportedImportFileExtensions[] supportedImportFileExtensions = SupportedImportFileExtensions.values();
for (int j=0; j<supportedImportFileExtensions.length; j++)
{
SupportedImportFileExtensions supportedImportFileExtension = supportedImportFileExtensions[j];
if (supportedImportFileExtension.isSupported(extension))
{
if (supportedImportFileExtension.equals(SupportedImportFileExtensions.EXCEL))
{
importInstalledItemEXCEL(importInstalledItemDefinition,attachment,processRun,extension);
}
else if (supportedImportFileExtension.equals(SupportedImportFileExtensions.CSV))
{
importInstalledItemCSV(importInstalledItemDefinition,attachment,processRun);
}
else if (supportedImportFileExtension.equals(SupportedImportFileExtensions.XML))
{
importInstalledItemXML(importInstalledItemDefinition,attachment,processRun);
}
}
}
}
processComplete = new Boolean(true);
}
catch (Exception e)
{
saveEntityLogRecord(processRun,"", "",ProcessRunLogEntityStatus.FAILED,e.getClass().getSimpleName(),ExceptionUtil.getStackTrace(e));
processRun.setStatus(ProcessRunLogStatus.FAILED);
processRunLogBean.save(processRun);
processRun = saveNextProcessRun(processRun);
}
if (processComplete)
{
processRun.setStatus(ProcessRunLogStatus.COMPLETED);
processRun.setEndDate(getCurrentDate());
processRun = saveNextProcessRun(processRun);
processRunLogBean.save(processRun);
}
}
...
}
As you can notice, ImportInstalledItemXMLUtilBean.java must also be implemented, in order to save and load action inputs, criteria and scheduling settings, all of which will be kept in a database field in an XML format.
Create Export Processes
To create an export process, you need to create:
- A process class extending com.crm.process.CRMExportBean, responsible for parsing the file and processing its records.
- A user interface class extending com.crm.process.processrun.CRMUIProcessRunDefinitionBean, responsible for handling the process run definition's page.
- A summary page to display the export process definitions.
- A data entry page to give the user the ability to create, view, edit, delete, save and run an export process definition.
In the following example, we will implement a mechanism to export vouchers. In order to do so, we create CRMExportVoucherBean.java and implement exportVoucher method, which retrieves the vouchers to be exported based on the given lot and writes them to a file of a given type (EXCEL, CSV, XML).
public class CRMExportVoucherBean extends CRMExportBean {
...
public void exportVoucher(String processRunLogID) throws Exception{
CRMDOProcessRunLog processRun = (CRMDOProcessRunLog)processRunLogBean.load(processRunLogID);
ExportVoucherDefinition exportVoucherDefinition = new ExportVoucherDefinition();
exportVoucherDefinition.setProcessRunDefinition(processRun.getProcessRunDefinition());
exportVoucherDefinition = exportVoucherXMLUtilBean.loadFromXML(exportVoucherDefinition);
processRun.setStatus(ProcessRunLogStatus.IN_PROGRESS);
processRun.setStartDate(getCurrentDate());
processRunLogBean.save(processRun);
Boolean processComplete = new Boolean(false);
try
{
CRMDOLot lot = exportVoucherDefinition.getLot();
VoucherFileFormat fileFormat = exportVoucherDefinition.getVoucherFileFormat();
if (lot != null && fileFormat != null)
{
if (fileFormat.equals(VoucherFileFormat.EXCEL))
{
exportVoucherInExcelFormat(exportVoucherDefinition, lot, processRun);
}
else if (fileFormat.equals(VoucherFileFormat.CSV))
{
exportVoucherInCSVFormat(exportVoucherDefinition, lot, processRun);
}
else if (fileFormat.equals(VoucherFileFormat.XML))
{
exportVoucherInXMLFormat(exportVoucherDefinition, lot, processRun);
}
processComplete = new Boolean(true);
}
}
catch (Exception e)
{
processRun.setStatus(ProcessRunLogStatus.FAILED);
processRunLogBean.save(processRun);
}
if (processComplete)
{
processRun.setStatus(ProcessRunLogStatus.COMPLETED);
processRun.setEndDate(getCurrentDate());
processRunLogBean.save(processRun);
}
}
public void exportVoucherInExcelFormat(ExportVoucherDefinition exportVoucherDefinition, CRMDOLot lot, CRMDOProcessRunLog processRun) throws Exception{
ResultSetUtil vouchers = processVoucherBean.getVouchersToBeExported(lot.getId());
if (vouchers!=null && vouchers.getRowCount()>0)
{
ArrayList<String[]> excelRecords = new ArrayList<String[]>();
while(vouchers.next())
{
String voucherID = vouchers.getString("VOUCHERID");
CRMDOVoucher voucher = (CRMDOVoucher) voucherBean.load(voucherID);
try
{
excelRecords.add(constructRecord(voucher));
}
catch (Exception e)
{
Entity voucherEntity = MetadataUtil.getEntityByClass(CRMDOVoucher.class.getName(), getCRMSession().getRealPath(), getOrganisationID());
processLogBean.createEntityLogRecord(
processRun.getId(),
voucher.getId(),
voucherEntity.getId(),
ProcessRunLogEntityStatus.FAILED.toString(),
e.getClass().getSimpleName(),
ExceptionUtil.getStackTrace(e));
}
}
storeEXCELRows(excelRecords, processRun, getFileHeaders());
}
else
{
throw new VoucherNotFoundToBeExportedException(getCRMSession());
}
}
...