View source | View content page | Page history | Printable version   

Projects:External data loading process/How To Import Data From External Platform

Contents

Overview

Bulbgraph.png   This how to is based on version 1.0.0 of module

The main idea of this how to is to explain how to create a module to import data from external platform like an SFTP server, Web service, FTP server, etc and integrate in Openbravo ERP

There are 2 important parts:

-implement a class to connect to external platform an load the data in entity ImportEntry

-implement some clases to get the information from entity ImportEntry and integrate in Openbravo ERP environment

How to implement a class to connect to external platform

First of all and out of this documentation, it is necessary to create a module


STEP 1: Define the name of the source type in reference "Source type"

-Login as "System administrator"

-Go to window reference and find reference "Source types"

-Go to tab "List Reference" and insert the new value to be exported in the created module (it is necessary to insert a search key with module dbprefix) The value defined it is available in window "Client -> Source and process"


Reference definition

ReferenceDefinition.jpg


Client window

ClientSource.jpg


STEP 2: Define a process in window "Report and process" to enable the possibility to schedule importing process

As "System Administrato" go to window "Report and process" an define one:

-Search key: it is the name of the business unit that we are going to import. For example HighRisk in case that we are going to import that business unit.

-Name: insert the correct name to identify the business unit to be imported

-UI Pattern: manual

-Background: this checkbox must be checked

-Java Class Name: insert value "org.openbravo.externaldata.integration.DataLoadingProcess"


ProcessDefinition.jpg


STEP 3: Create a Java class extending class BaseComunnication

In this part it is going to be necessary to implement some necessary methods and the class must have some annotations

Class annotations

It is necessary to add annotation @Dependent because in that way, every execution of the defined background process, it will create a new instance of the class. Also it is necessary to add @Qualifier("SFTP") annotation where the value will be the same value of the list "Source types" previously defined in step 1.


JAVA class example:

 @Dependent
 @Qualifier("SFTP")
 public class SftpCommunication implements BaseCommunication

Methods to implement

configure: get the configuration inserted by the user in window Client and initialize necessary variables.

checkCommunication: this is going to check if the inserted configuration by the user is a valid one.

connect: connect to the external platform.

disConnect: disconnect or end the communication with external plarform.

downloadData: download the data from external platform to Openbravo ERP server. This method can download and save the data in the server file system or also it can load the information in the server memory.

readData: get the information that it has been downloaded in method "downloadData" and using methods "createDataJS" and "insertDataToImportEntry" generate the data in entity "ImportEntry".

getSourceType: return the value that previously it has been inserted as class Qualifier.

createDataJS: this method only encapsulate the data in a JSON object. In case of the example the generated JSON is

   {
   "data": "......",
   "Client": "39363B0921BB4293B48383844325E84C",
   "Organization": "0",
   "ProcessRun": "219A9FB3D6064A4792550630EECA3067"
   }

insertDataToImportEntry: insert the value in entity "ImportEntry". At this point it is really recommendable to inject using Weld any instance of class "ImportEntryManager" and use that to implement the method.

Example:

In the class header include

 @Inject
 private ImportEntryManager importEntryManager;

due to that the method implementation will simplify to

 @Override
 public void insertDataToImportEntry(String uuid, String qualifier, String data) {
   importEntryManager.createImportEntry(uuid, qualifier, data);
 }

How to integrate the data in Openbravo ERP environment

When the data is already imported in entity ImportEntry it is going to be necessary to implement 2 classes:

-one is going to extend class "ExternalDataSyncProcess"

-the another one extends class "ImportEntryProcessor"

Extending class ExternalDataSyncProcess

In the class header it is going to be necessary to define the entity that we are going to import. For example:

 @ExternalDataSync(entity = EntityName)
 public class EntityNameClass extends ExternalDataSyncProcess {}

Apart from that, it is necessary to implement 2 methods:

saveRecord: it is going to receive previously inserted record in entity "ImportEntry" with all the downloaded information from external platform. This method will insert the data in Openbravo ERP database

getImportQualifier: return the value of "EntityName". The same of the class annotation

  protected String getImportQualifier() {
   return ImportDataConstants.HIGH_RISK;
 }

Extending class ImportEntryProcessor

In this class it is necessary to add 2 annotations in the class

 @ImportEntryQualifier(entity = EntityName)
 @ApplicationScoped
 public class EntityNameEntryProcessor extends ImportEntryProcessor {

Above annotations are necessary to enable the platform to create instances of this class.

Apart from annotations, it is necessary to implement the following methods and classes.

-Methods-

getMaxNumberOfThreads: The maximum number of threads to be created during importing process.

 protected int getMaxNumberOfThreads() {
   return Runtime.getRuntime().availableProcessors() / 2;
 }

ImportEntryProcessRunnable: Create an instance of class "EntityNameRunnable" that it is necessary to define in the same class.

 protected ImportEntryProcessRunnable createImportEntryProcessRunnable() {
   return WeldUtils.getInstanceFromStaticBeanManager(AltoRiesgoRunnable.class);
 }

canHandleImportEntry: this method will return true or false if it is able to manage the same type of data of the ImportEntry that it is going to receive as paramater

 protected boolean canHandleImportEntry(ImportEntry importEntry) {
   return EntityName.equals(importEntry.getTypeofdata());
 }

getProcessSelectionKey: this method will define the key/rule to create new threads to import the data. For example if you are importing Orders from different stores, you can set as key the store name. In that way you will have one thread working and importing data for one store name

 protected String getProcessSelectionKey(ImportEntry importEntry) {
   return (String) importEntry.getTerminalName();
 }

-Clasess-

getDataSynchronizationClass: protected class that it will return the the value of the class that it is going to import the information. In this case "EntityName.class"

 protected Class<? extends ExternalDataSyncProcess> getDataSynchronizationClass() {
   return EntityName.class;
 }


EntityNameRunnable: private static class that extends class "ImportEntryProcessRunnable" and it overwrites method "processEntry"

 private static class EntityNameRunnable extends ImportEntryProcessRunnable {
   protected Class<? extends ExternalDataSyncProcess> getDataSynchronizationClass() {
     return EntityName.class;
   }
   @Override
   protected void processEntry(ImportEntry importEntry) throws Exception {
     final ExternalDataSyncProcess dataSynchronization = WeldUtils
         .getInstanceFromStaticBeanManager(getDataSynchronizationClass());
     dataSynchronization.setImportEntryId(importEntry.getId());
     dataSynchronization.saveRecord(importEntry);
   }
 }

Retrieved from "http://wiki.openbravo.com/wiki/Projects:External_data_loading_process/How_To_Import_Data_From_External_Platform"

This page has been accessed 2,721 times. This page was last modified on 20 July 2016, at 08:59. Content is available under Creative Commons Attribution-ShareAlike 2.5 Spain License.