Article on Custom routine creation in Talend for Magento

2. Create a Talend Job :

Go to Job Designs and select Create job to create a new Talend job and in give the name as MagentoConnection.

Load tWebServiceInput component from the palette and it will open a new window when we double click on it.

2.1 Component configuration:

Configure Magento details in the component like below:

2.1.1 Basic Settings:

WSDL : http://magentohost/index.php/api/soap/?wsdl (replace your Magento host name with magentohost)

Method name: Make this value as empty [ double quotes (“”)]

Time out (second) : 60

2.1.2 Advanced Settings:

Select Advanced settings and make sure to check the Advanced Use checkbox to generate client stub codes in Routines.

By default there is some routine available in the code section, remove it and click on WSDL2Java button so that component will generate wsdl files and shows a success message.

All the files will be stored under Codes/Routines/MagentoConnection directory.

3. Create routine:

After creating wsdl files for the Magento we need to create a routine to access Magento SOAP web services.

Go to code section in Talend and right – click on Routines and select Create routine and give name as Magento_Routine.  

 

4. Magento API Details Configuration:

We need to create an API user in Magento to connect Magento web services. This API user is also related to a so-called API role. By default, there is neither API user nor API role, so we need to configure both.

4.1 Logging into the Magento Admin Panel :

First an API role needs to be added. Login to the Magento Admin Panel and navigate to

System > Web Services > SOAP/XML-RPC – Roles ( or Roles in old versions).

 

4.2 Create a new API role:

This page lists the current API roles. If there none listed yet, click on Add New Role.

Every role needs a name. This can be anything. Just make sure it is descriptive enough for yourself.

The second tab on the left (Role Resources) allows you to select which resources should be available for users with this role.

While you can select exactly which privileges are given, we recommend you select All unless you’re willing to spend hours

with experimenting.

4.3 Create a new API user:

Now that the role is created, you can add a user that makes use of this role.

The strange thing here is that the user fields here seem to indicate a person or individual, while in fact we’re creating a

system account of some kind.

The fields First NameLast Name and Email just need to be filled in with any value you like. Make sure the

User Name and Api Key are secure enough.

The second tab on the left is called User Role, and that’s where you select the role earlier created.

Once the API user created successfully in admin panel then we need to make use those details in the Talend routine.

5. Magento Session Creation:

5.1 Magento soap url:

String url = “http://magentohost/index.php/api/soap/index/”;

5.2 Create porttype object:

Create a porttype object in the routine like below so that we will make use that object in entire routine.

Porttype is the Magento Api Model Server object that we already generated in wsdl files.

routines.Mage_Api_Model_Server_HandlerPortType porttype = new

routines.MagentoServiceLocator().getMage_Api_Model_Server_HandlerPort(new java.net.URL(url));

5.3 Create session id:

Method : login(apiUser,apiKey)

Arguments :

apiUser : API Username

apiKey : API Key

Description : Start the API session, return the sessionID, and authorize the API user.

Example :

String SessionId = (String)porttype.login(“username”, “password”);

6. Magento Product Create:

Method: call(SessionId, resourcePath, arrayArguments)

Arguments :

Sessionid : SessionID

Type :Product type

Set : ID of the product attribute set

Sku : Product SKU

productData : Array of catalogProductCreateEntity

storeView : Store view ID or code

Example:

String productId = (String) porttype.call(SessionId, “catalog_product.create”, arrlist);
Create an Array List object to hold all the required arguments.
java.util.ArrayList arrlist = new java.util.ArrayList();
Add all the attributes to the list object .
arrlist.add(0, "simple");//product type
arrlist.add(1, 10);//ID of the product attribute set
arrlist.add(2, 999);//sku
arrlist.add(prodDataMap);
Here prodDataMap is a Map object and it contains all the product related entities.

7. Magento Product Update:

If we want to update an existing Magento product then we can also update in a following way.

Method: call(SessionId, resourcePath, arrlist)

Arguments :

sessionid : SessionID

produt/productid : Product type

productData : Array of catalogProductCreateEntity

storeView : Store view ID or code(optional)

identifierType : Defines whether the product ID or SKU is passed in the ‘product’ parameter.

Example:

String productId = (String) porttype.call(SessionId, “catalog_product.update ”, arrlist);
Create an array list object to hold all the required arguments to update a Magento product.
java.util.ArrayList updatePrdList = new java.util.ArrayList();
updatePrdList.add(0,20);//Product id
updatePrdList.add(1,arrlist);//productData

8. Calling Magento routine inside Talend job:


Go to Talend job MagentoConnection and edit the tWebServiceInput like below.

String url = “http://magentohost/index.php/api/soap/index/”;
String apiUser =”test”;
String apiPwd =”test123”;
MagentoRoutine magento_routine = new MagentoRoutine(url);
String sessionId = magento_routine.getSessionID(apiUser, apiPwd);
System.out.println(sessionid);

Execute the Magento Connection job then we will get the Magento Sessionid.

Like the same way we can create product, category, customer, sales order, shipment and invoice.

9. References:

http://www.magentocommerce.com/api/soap/introduction.html