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)

Monday, January 25, 2010

Send Email Java Class

I believe that the simpler things are the better. I have seen
different API's for sending email with java, but some of them
where really complex and they only work for a jre 1.5 or greater.
I needed a simpler class because SAP work with a smaller JDK.

The other issue, is when you need to install the API's in a server,
if you need to modify the classpath in the JAVA, that meens that in
SAP you have to use the STOPSAP, and STARTSTAP commands. I think
that this isn't really good, you need something simpler, and try
to not bother owr partners in the BASIS team. I don't like to depend
on them as a developer.

I hope that you enjoy the code.

Here is the code:

Thursday, January 14, 2010

ASAP Methodology

Check out this SlideShare Presentation:

Wednesday, August 5, 2009

Create a Web Service in SAP R/3 and Consume it with PHP-SOAP

Intro
If you do much like to work developing web applications, SAP R/3, and mobile smartphones, eventually you find that there's is some task that you want to do with the phone and the data is in SAP. For example, if you are a student of some university and this institution has the Student Life Cycle Management, you may wish to know your personal data form your phone, or you want to see your grades. But the institution doesn't have the SAP Netweaver Mobile 7.1. Well as a developer i recommend to publish a web service because it can work with any plataform and the idea of course is to solve a simple and different task.

Steps
First of all we need to know what we want to publish in the web for our clients, specially in SAP R/3. Second you have to define your architecture for this solution because they are different ways to solve this. I'm going to show a solution, it's not the best way but it is the only resources that I have in my work.

Ok, lets roll. I'm going to publish the personal data of a student, just some values.
First we create the function module Z_WS_PANDA in transaction SE37 or SE80, and of course you should try to create it in a function group for the web services just for SLCM, or in the module that you are working for.

Once you finish creating the Function Module, you need to select the attribute for remote access as shown in picture below.


Second create the variables for export & import as shown below with the following pictures.



Now you develop the application with the following functions. For Example: BAPI_STUDENT_GET_NUMBERS, BAPI_STUDENT_GETDETAIL3,
HRIQ_STUDENT_STUDIES_GET_RFC. If you know ABAP, you will notice that this is really straight foward. Now if your function module it's ready and activated, you have to follow the next step. In the Menu Bar, choose Utilities->More Utilities->Generate Web Service->For Function Module. This will popup a wizard window. Just follow the steps and you are done!!!
(In the figures the name of the service is different)








Final Step with SAP
It is important now to view your WSDL. There is different ways to view the WSDL generated. The easy way is go to TX SE80, In the Browser Repository, select the package in the dropdown list, then search for the package where is the Web Service that you just created. The next step is to open de Enterprise Service folder, then the DefinitionServ and doubleclick the Service that you created. For Example: ZWS_PANDA. (In the figure is selected another service).


Now you need to select the tab WSDL. This one provides with the URL, for Example: http://xxxx.xxx.com:8001/sap/bc/srt/wsdl/sdef_ZWS_PANDA_ADM/wsdl11/ws_policy/document?sap-client=100

PHP
Now I'm just going to try to show you how i call the web service with php-soap. The trick here is to change the part in the URL ws_policy to standard. For example:
http://xxxx.xxx.com:8001/sap/bc/srt/wsdl/sdef_ZWS_PANDA_ADM/wsdl11/ws_policy/document?sap-client=100
change it to
http://xxxx.xxx.com:8001/sap/bc/srt/wsdl/sdef_ZWS_PANDA_ADM/wsdl11/standard/document?sap-client=100

require_once('SOAP/Client.php');

$url = 'http://xxxx.xxx.com:8001/sap/bc/srt/wsdl/sdef_ZWS_PANDA/wsdl11/standard/document?sap-client=100';

$login='user';
$pass='xxxxx';

//Call the client
client = new SoapClient($url,
array('login' => $login,
'password' => $password,
'trace' => true,
'exceptions' => true));

try {
$person = $client->ZWsPanda(array('Cedula' => $_GET['cedula']));
//print_r ( $person );
$datos = $person->PersonaldataT;
print_r( $datos );
}
catch (SoapFault $e) {
echo 'Caught an Error: [' . $e->faultcode . '] - ' . $e->faultstring;

}

Conclusion
It is nice to do some simple task with a very useful webserver like apache and php5. This could easily
shown in the web browser for a mobile solution.

I hope you could enjoy the article!