Tuesday, May 18, 2010

Django and SAP ERP

For along time ago I was trying to create several apis to integrate
different systems with SAP. I know that SAP has the application
server for this, using SOAP. But sometimes you do not need to wait
for the BASIS team to configure the landscape correctly. Or for example
use the SOAMANAGER to create the endpoint, and another silly task that
we have to do in SAP, as I mention in my other post.

Now in the world of application like twitter we like to see our
information to be in all our devices. For example in blackberry developer
plugin for eclipse you don't have a really cool api for SOAP. If you
try to insert the library into your app, the simulator doesn't load
the specific library. So what you should do?

  • Install SAP Mobile?
  • create your own middleware?


I choose create my own middleware, because is not necessary to buy a bigger server for this specific tasks. Let me explain, I have a Intel(R) Pentium(R) 4 CPU 1.60GHz with 512MB RAM in Linux Ubuntu Server LTS. In this particular server it's impossible to install a SAP Netweaver Application Server (SAP WEBAS). So i think that the right way to go is to use Django, sapnwrfc, and nwrfcsdk.

In a few our you can make it work! I can assure you that you can do almost anything with this three particular fabulous framework, libraries, etc.

Well enough of silly chats and lets get started.

Requirements:
  • SAP NW RFC SDK
  • SAPNWRFC Project by Piers Harding
  • PyYAML
  • DJANGO
  • APACHE2 with mod_wsgi

1. SAP NW RFC SDK Where to get it?


You need to go to the SAP service Portal for Software downloads, and follow the path of: Download -> Support Packages and Patches -> Entry by Application Group -> Additional Components -> SAP NW RFC SDK -> SAP NW RFC SDK 7.10 -> SAP NW RFC SDK 7.10 . (http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/6519)

1.1 Install SAP Netweaver RFC SDK


When you install this you can choose any where on your disk for example:
/usr/sap/nwrfcsdk/

1.2 Set the enviroment Path

In ubuntu you can do the following:
a. $export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/sap/nwrfcsdk/lib
b. add the variable in the /etc/enviroment file adding the line:

$vim /etc/enviroment
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/sap/nwrfcsdk/lib
$source /etc/enviroment

c. Add the variable in the apache2 configuration file:

$vim /etc/apache2/envvars
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/sap/nwrfcsdk/lib
$sudo /etc/init.d/apache2 restart

Why in the apache?


It is really necesary becuase then with the mod_wsgi it wouldn't work. It doesn't recognize the path.

The error in the /var/log/apache2/error.log will be appear in this way

Could not open the ICU common library.
The following files must be in the path described by
the environment variable "LD_LIBRARY_PATH":
libicuuc.so.34, libicudata.so.34, libicui18n.so.34 [nlsui0_mt.c 1529] pid = 25863
LD_LIBRARY_PATH is currently set to [nlsui0_mt.c 1532] pid = 25863



2. Download and install the PyYAML package


Get the latest version of the PyYAML package so that your python script can read yaml files.


3. Download and install sapnwrfc package


Download the latest sapnwrfc package from Piers Harding's website.
3.1.- $ wget http://www.piersharding.com/download/python/sapnwrfc/sapnwrfc-0.10.tar.gz
3.2.- $ tar xvzf sapnwrfc-0.10.tar.gz
3.3.- $ cd sapnwrfc-0.10
3.4.- $ export LD_LIBRARY_PATH=/usr/sap/nwrfcsdk/lib
3.5.- $ python setup.py build_ext -I/usr/sap/nwrfcsdk/include/ -L/usr/sap/nwrfcsdk/lib/
3.6.- $ python setup.py build
3.7.- $ python setup.py install


3.4 Preparing a script


To prepare a script, you'll need a 'yml' file similar to the 'sap.yml' file included with the sapnwrfc download. The file looks like this:


ashost: gecko.local.net
sysnr: "01"
client: "001"
user: developer
passwd: developer
lang: EN
trace: 3
loglevel: warn

Test you connection with the examples that is in the Piers Harding library.

$python examples/conn_test.py


4.DJANGO


In the web page Django Project You can find how to create a web page in less than 20 minutes. Ok lets get started:

a. $django-admin.py startproject sap
b. $cd sap; python manage.py bookflight

Now you can see the following files int the bookflight directory:

__init__.py __init__.pyc models.py tests.py views.py

edit the files views.py

# Create your views here.
from django.shortcuts import render_to_response

import sapnwrfc
  

def results(request):
  if request.is_ajax():
    q = request.GET.get('q')
    if q is not None:
      sapnwrfc.base.config_location = '/path/to/connection/file/sap.yml'
      sapnwrfc.base.load_config()
      
      try:
        conn = sapnwrfc.base.rfc_connect()
        fd = conn.discover("RFC_READ_TABLE")
        f = fd.create_function_call()
        f.QUERY_TABLE("TRDIR")
                                f.ROWCOUNT(50)
                                f.OPTIONS( [{ 'TEXT': "NAME LIKE 'RS%'"}] )
                                f.invoke()

        d = f.DATA.value
                                todo = {'results': d}
        
        conn.close()
        
      except sapnwrfc.RFCCommunicationError:
        message = "bang!"
      
      return render_to_response('html/results.html', todo)

No comments:

Post a Comment