Sap BSP integration with Google Maps v3 and Geocoder

This is an example about how to integrate a Sap BSP Application with Google Maps v3 , displaying a Sap Customer location.

We don’t know Customer latitude/longitude  but we can use google maps GEOCODER to get lat/long using the address in Sap.

HERE Google Documentation about GEOCODING.

This is the Gecoding JSON response

results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   types[]: string
 },
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}

SAP Tcode XD03 Display Customer :

Our BSP Page get Customer Code KNA1-KUNNR as GET Parameter ( ../maps.bsp?kunnr=id_sap_customer ).

In “OnRequest” event , we can check if the customer exists .

We must include in our page the google maps javascript library

<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=true&language=it">
</script>

Geocoder Object is

  google.maps.Geocoder();

Below complete example code of maps.bsp:

<%@page language="abap" %>   
<%
if v_kna1-kunnr is initial.
 %>
<pre>Customer <%= kunnr%> not found!</pre>
 <% else.   
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
   EXPORTING
   input         = kunnr
   IMPORTING
   OUTPUT        = kunnr.
 %>
 <html>
 <head>
 <style> html{
 background-color:#EAF1F6;
 }
 * { font:12px Arial; }
 body * {
     margin: 0;
     padding: 0;
     border: 0;
 }
 .label{
 color:blue;
 }
 h2{
 font-size:20px;
 color:blue;
 }
 #div_left{
 height:300px;
 width:300px;
 float:left;
 }
 #div_map{
 height:350px;
 width:350px;
 border: 2px solid blue;
 }
 </style>
 <script type="text/javascript"
  src="http://maps.google.com/maps/api/js?sensor=true&language=it"></script>
 <script type="text/javascript">
 var create_map = function ()  {
 var address_gmaps = '<%= v_kna1-stras%>'+ ' ' + //address
                       '<%= v_kna1-ort01%>'+   //city
                       ' Italia';  //avoid hardcode

 //alert (address_gmaps);
   geocoder = new google.maps.Geocoder();                   
   var options = {zoom:15,mapTypeId:google.maps.MapTypeId.ROADMAP}
   map = new google.maps.Map(document.getElementById("div_map"),options);   
   geocoder.geocode({'address': address_gmaps}, function(results, status){
                map.setCenter(results[0].geometry.location);               
                 var marker = new google.maps.Marker({ map: map,
                                  position: results[0].geometry.location,
                                  title: '<%= v_kna1-name1%>' });
              });
 }
</script>
</head>
<body onload="create_map()">
<h2>BSP integration with Google Maps v3 / Geocoder</h2>
<br/>
<div id="div_left">
<span>Customer:</span>&nbsp;&nbsp;<%= kunnr %><br/>
<span>Name:</span>&nbsp;&nbsp;<%= v_kna1-name1%><br/>
<span>Address:</span>&nbsp;&nbsp;
<%= v_kna1-stras%>&nbsp;-&nbsp;<%= v_kna1-pstlz%>&nbsp;<%= v_kna1-ort01%><br/>
<span>Telephone:</span>&nbsp;&nbsp;<%= v_kna1-telf1%><br/>
<span>Mobile:</span>&nbsp;&nbsp;<%= v_kna1-telf2%><br/>
<span>Fax:</span>&nbsp;&nbsp;<%= v_kna1-telfx%><br/>
</div>
<div id="div_map"></div>
</body>
</html>
<%
  endif.  
%> 

Abap Example of HTTP REQUEST

This is an example that executes an HTTP Request using GET method . The response is an XML http://www.w3schools.com/xml/note.xml

I used an HTTP DESTINATION created in tcode SM59  (destination type G)

 DATA: http_dest TYPE rfcdest VALUE 'ZTEST_HTTP'.

 DATA: client TYPE REF TO if_http_client.
 DATA: request TYPE REF TO if_http_request,
 rc TYPE sy-subrc,
 http_rc TYPE sy-subrc,
 xml_xstring TYPE xstring.

 START-OF-SELECTION.

 cl_http_client=>create_by_destination( EXPORTING
 destination = http_dest IMPORTING client = client ).

 *request = client->request.

 CALL METHOD client->request->set_method(
 if_http_request=>co_request_method_get ).

*optional Request fields
*CALL METHOD request->SET_FORM_FIELD
*   EXPORTING
*     NAME  = name
*     VALUE = value.

* send and receive
CALL METHOD client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state         = 2
http_processing_failed     = 3
http_invalid_timeout       = 4
OTHERS                     = 5.
IF sy-subrc <> 0.
RAISE connection_error.
ENDIF.

CALL METHOD client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state         = 2
http_processing_failed     = 3
OTHERS                     = 4.

rc = sy-subrc. "error receive

IF rc = 0.
 **http status code
client->response->get_status( IMPORTING code = http_rc ).

IF http_rc <> 200.
 """KO
ELSE. "status 200 ->>OK
CLEAR: xml_xstring.
xml_xstring = client->response->get_data( ).

ENDIF.
ENDIF.

 * VERY IMPORTANT: close your connection
client->close( ).

CALL FUNCTION 'SRTUTIL_HELPER_XML_SHOW'
EXPORTING
xdoc = xml_xstring
html = abap_false.


Categories: Abap Tags: , ,

Construct URL for a Transaction Code Sap WebGui

I haven’t found a standard method to Construct an  Http Url WebGui with TransactionCode (I know similar methods CL_BSP_RUNTIME=>IF_BSP_RUNTIME~CONSTRUCT_BSP_URL for BSP and cl_wd_utilities=>construct_wd_url for Abap Web dynpro)

I’m using this method,if there’s a standard method to do this email me /comment

 DATA: v_host TYPE string,
 v_port TYPE string,
 v_out_protocol TYPE string,
 v_tcode TYPE tcode,
 v_url(500).

 START-OF-SELECTION.
 v_tcode = 'SE38'. "transaction code to call - Example

 cl_http_server=>if_http_server~get_location(
 IMPORTING
 host         = v_host
 port         = v_port
 out_protocol = v_out_protocol
 ).

 CONCATENATE v_out_protocol '://' v_host ':' v_port

 '/sap/bc/gui/sap/its/webgui?~TRANSACTION=' v_tcode

 INTO v_url.

 "WRITE:/ v_url.

 CALL FUNCTION 'CALL_BROWSER'
 EXPORTING
 url = v_url.

UPDATED:a better way is to use

cl_its_runtime=>get_url method

(see the comment , thanks to John)

Categories: Abap Tags: ,

Dinamic Url Generation for an Abap Web Dynpro Application

This is an example to construct the URL for an Abap Web Dynpro application.

Value of abs_url will be :

http://host.domain.com/sap/bc/webdynpro/sap/zwd_application?PARAM_NAME1=value1&PARAM_NAME2=value2

 DATA: t_parameters TYPE tihttpnvp,
 l_parameters TYPE ihttpnvp,
 appl_name TYPE string,
 abs_url TYPE string,
 abs_url_char(500).


 START-OF-SELECTION.
appl_name = 'ZWD_APPLICATION'.

 l_parameters-name = 'PARAM_NAME1'.
 l_parameters-value = 'value1'.
 APPEND l_parameters TO t_parameters.

 l_parameters-name = 'PARAM_NAME2'.
 l_parameters-value = 'value2'.
 APPEND l_parameters TO t_parameters.

 cl_wd_utilities=>construct_wd_url( EXPORTING application_name = appl_name
                                             in_parameters = t_parameters
 IMPORTING out_absolute_url = abs_url ).


abs_url_char = abs_url.

**open application in the browser
  CALL FUNCTION 'CALL_BROWSER'
 EXPORTING
 URL                          = abs_url_char.
Categories: Web Dynpro Abap Tags:

Create a ZIP file using CL_ABAP_ZIP (on presentation server)

How to make a Zip file on Presentation Server : upload a PDF file , create a new zip file and add the pdf data


 DATA: zip_tool TYPE REF TO cl_abap_zip,
 filename TYPE string VALUE 'C:\test\binfile.pdf',
 filename_zip TYPE string VALUE 'C:\test\zipfile.zip'.

 DATA: t_data_tab TYPE TABLE OF x255,
 bin_size TYPE i,
 buffer_x TYPE xstring,
 buffer_zip TYPE xstring.

 START-OF-SELECTION.
 **upload file from presentation server
 CLEAR: t_data_tab[],bin_size.
 CALL FUNCTION 'GUI_UPLOAD'
 EXPORTING
 filename                      = filename
 filetype                      = 'BIN'
 IMPORTING
 filelength                    = bin_size
 *   HEADER                        =
 TABLES
 data_tab                      = t_data_tab.

 **get xstring
 CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
 EXPORTING
 input_length = bin_size
 IMPORTING
 buffer       = buffer_x
 TABLES
 binary_tab   = t_data_tab.
 IF sy-subrc <> 0.
 * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
 *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
 ENDIF.

 **create zip tool
 CREATE OBJECT zip_tool.

 **add binary file
 CALL METHOD zip_tool->add
 EXPORTING
 name    = 'binfile.pdf'
 content = buffer_x.

 **get binary ZIP file
 CALL METHOD zip_tool->save
 RECEIVING
 zip = buffer_zip.

 CLEAR: t_data_tab[],bin_size.
 CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
 EXPORTING
 buffer        = buffer_zip
 IMPORTING
 output_length = bin_size
 TABLES
 binary_tab    = t_data_tab.

**download ZIP file
 CALL FUNCTION 'GUI_DOWNLOAD'
 EXPORTING
 bin_filesize = bin_size
 filename     = filename_zip
 filetype     = 'BIN'
 TABLES
 data_tab     = t_data_tab.

Categories: Abap Tags: , , ,

Download a Smartform to Pdf from Web Dynpro Abap

This is an example to download a Smartform to PDF from a Web Dynpro Abap.
The method ONACTIONSHOW_PDF is binded to “onAction” event Button

method ONACTIONSHOW_PDF.

DATA: smartform_fm  TYPE rs38l_fnam,
bin_pdf TYPE TABLE OF docs,
bin_tline TYPE TABLE OF tline,
bin_filesize TYPE i,
bin_pdfx TYPE xstring,
v_device_type TYPE rspoptype,
v_output_options TYPE ssfcompop,
v_control_parameters TYPE ssfctrlop.

**CONVERT SMARTFORM TO BINARY XSTRING PDF
CALL FUNCTION 'SSF_GET_DEVICE_TYPE'
EXPORTING
    i_language             = 'I'
    i_application          = 'SAPDEFAULT'
IMPORTING
     e_devtype              = v_device_type
EXCEPTIONS
     no_language            = 1
     language_not_installed = 2
     no_devtype_found       = 3
     system_error           = 4
     OTHERS                 = 5.

v_output_options-tdprinter = v_device_type.
v_control_parameters-no_dialog = 'X'.
v_control_parameters-getotf = 'X'.

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
      formname                 = 'ZSMARTFORM'
IMPORTING
      fm_name                  = smartform_fm
EXCEPTIONS
no_form                  = 1
no_function_module       = 2
OTHERS                   = 3.

CALL FUNCTION smartform_fm
EXPORTING
       control_parameters         = v_control_parameters
       output_options             = v_output_options
       id_document                = doc_id
IMPORTING
       job_output_info            = v_output_stampa
EXCEPTIONS
formatting_error           = 1
internal_error             = 2
send_error                 = 3
user_canceled              = 4
OTHERS                     = 5.

***CONVERT OTF TO PDF
CLEAR: bin_pdf,bin_tline,bin_filesize,bin_pdfx.

CALL FUNCTION 'CONVERT_OTF'
EXPORTING
      format                      = 'PDF'
IMPORTING
      bin_filesize                =  bin_filesize
      bin_file                    = bin_pdfx
TABLES
      otf                         = v_output_stampa-otfdata
      lines                       = bin_tline.

**ATTACH PDF TO RESPONSE
CL_WD_RUNTIME_SERVICES=>ATTACH_FILE_TO_RESPONSE(
i_filename = 'PDFSMARTFORM.PDF'
i_content = bin_pdfx
i_mime_type = 'application/pdf' ).

endmethod.

An Abap REST Client (RSICFCLTST01)

My first Blog on SAP SDN ,about Sap Netweaver Rest Client RSICFCLTST01, a great test framework similar to Firefox Extensions like Fiddler/Poster/Rest Client
LINK

Categories: Utilities Tags: , ,

Send an Html Email (using CL_BCS class)

 DATA: send_request       TYPE REF TO cl_bcs.
 DATA: document           TYPE REF TO cl_document_bcs.
 DATA: sender             TYPE REF TO cl_sapuser_bcs.
 DATA: recipient          TYPE REF TO if_recipient_bcs.
 DATA: exception_info     TYPE REF TO if_os_exception_info,
 bcs_exception      TYPE REF TO cx_bcs,
 v_subj(50),
 t_hex TYPE solix_tab,
 html_string TYPE string,
 xhtml_string TYPE xstring,
 v_message(100),
 v_mail TYPE  sza5_d0700-smtp_addr.
 v_subj = 'Subject. This is an Email with HTML'.

 CONCATENATE '<html><strong>Example Html</strong><br/><br/>'
'<table style="background-color: #CDCDCD; border: 1px solid black;">'
'<tr><th style="color:red;">Id Sales Order</th>'
'<th style="color:red;">Type</th><th style="color:red">Message</th></tr>'
             INTO html_string .

 LOOP AT t_htmldata.  "some html data
 CONCATENATE html_string '<tr><td style="border-right: 1px solid red;">'
  t_htmldata-field1 '</td><td style="border-right: 1px solid red;">'
  t_htmldata-field2 '</td><td>' t_htmldata-field3
  '</td></tr>'
  INTO html_string.

 ENDLOOP.

 CONCATENATE html_string '</table></html>' INTO html_string.

 TRY.
 *Create persistent send request
 send_request = cl_bcs=>create_persistent( ).

 CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
 EXPORTING
 text           = html_string
 *   MIMETYPE       = ' '
 *   ENCODING       =
 IMPORTING
 buffer         = xhtml_string
 EXCEPTIONS
 failed         = 1
 OTHERS         = 2.

 CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
 EXPORTING
 buffer                = xhtml_string
 *   APPEND_TO_TABLE       = ' '
 * IMPORTING
 *   OUTPUT_LENGTH         =
 TABLES
 binary_tab            = t_hex.

 document = cl_document_bcs=>create_document(
 i_type    = 'HTM'
 i_hex    = t_hex
 i_subject = v_subj ).

 * Add document to send request
 CALL METHOD send_request->set_document( document ).

 * Get sender object
 sender = cl_sapuser_bcs=>create( sy-uname ).

 * Add sender
 CALL METHOD send_request->set_sender
 EXPORTING
 i_sender = sender.

 v_mail = 'example@example.com'.

 recipient = cl_cam_address_bcs=>create_internet_address( v_mail ).
 * Add recipient with its respective attributes to send request
 CALL METHOD send_request->add_recipient
 EXPORTING
 i_recipient = recipient.

 * Set that you don't need a Return Status E-mail
 DATA: status_mail TYPE bcs_stml.
 status_mail = 'N'.
 CALL METHOD send_request->set_status_attributes
 EXPORTING
 i_requested_status = status_mail
 i_status_mail      = status_mail.

 * set send immediately flag
 send_request->set_send_immediately( 'X' ).

 * Send document
 CALL METHOD send_request->send( ).

 COMMIT WORK.

 CATCH cx_bcs INTO bcs_exception.
 v_message = bcs_exception->get_text( ).
 MESSAGE e000(su) WITH v_message.
 ENDTRY. 
Categories: Abap Tags: , ,

Smartforms to PDF – Part 2 (Java Side)

This is a code snippet from the Java Servlet , calling the function module via RFC described in the part I of this previous blog
https://abapcodexperiments.wordpress.com/2010/12/30/smartforms-to-pdf-part-1-abap-side/

import java.io.IOException;
import com.sap.mw.jco.*;
import it.customer.sap.zcustom.OrderedProperties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletOutputStream;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;

...
...
...
public class PdfDocument extends HttpServlet {

 protected void doGet(
 HttpServletRequest request,
 HttpServletResponse response)
 throws ServletException, IOException {

String Id_document = request.getParameter("id_doc");
String fileNamePdf = new String();

fileNamePdf = Id_document + "_document.pdf";
JCO.Client client = null;
JCO.Repository mRepository = null;   
JCO.Function function = null;

 try {
//    connection with properties file
 OrderedProperties logonProperties =
 OrderedProperties.load("/conf_connection.properties");

 client = JCO.createClient(logonProperties);

//Logon SAP

 mRepository = new JCO.Repository("ZREPOS", client);

 IFunctionTemplate ft = mRepository.getFunctionTemplate("ZSMARTFORMS_TO_PDF");

 function = ft.getFunction();    

 JCO.ParameterList importing_pdf = function.getImportParameterList();
 importing_pdf.setValue(Id_document,"DOC_ID");  //id doc

 client.connect();

//Call Function                
 client.execute(function);        

//  BIN FILE SIZE
 JCO.Field exp_bin_size = function.getExportParameterList().getField("BIN_SIZE");
// PDF DATA
 JCO.Table exp_data_pdf = function.getTableParameterList().getTable("PDF_DATA"); 

 ServletOutputStream pdf_stream = response.getOutputStream();  

//set PDF Response
 response.setHeader( "Pragma", "no-cache" );
 response.addHeader( "Cache-Control", "must-revalidate" );
 response.addHeader( "Cache-Control", "no-cache" );
 response.addHeader( "Cache-Control", "no-store" );
 response.setDateHeader("Expires", 0);
 response.setContentLength((int) exp_bin_size.getInt());
 response.setContentType("application/pdf");
 response.addHeader("Content-Disposition", "attachment; filename=" + fileNamePdf);

 for (int i = 0; i < exp_data_pdf.getNumRows(); i++) {
 exp_data_pdf.setRow(i);
 pdf_stream.write(exp_data_pdf.getByteArray("CONTENT"));   
 }

 pdf_stream.flush();
 pdf_stream.close();
            }
}
Categories: Sap J2ee Tags: , , ,

Smartforms to PDF – Part 1 (ABAP side)

This is a Function Module example to convert a smartform to Binary Pdf.

I used this via RFC to get PDF Data from a J2ee Servlet (Part 2 , JAVA side)

FUNCTION zsmartforms_to_pdf.
*"----------------------------------------------------------------------
*"*"Local Interface:
 *"  IMPORTING
 *"     VALUE(DOC_ID) TYPE  ZDOCUMENT_ID
*"  EXPORTING
*"     VALUE(BIN_SIZE) TYPE  I
*"  TABLES
*"      PDF_DATA STRUCTURE  HRB2A_RAW255 OPTIONAL
*"----------------------------------------------------------------------
DATA: smartform_fm  TYPE rs38l_fnam,
bin_pdf TYPE TABLE OF docs,
bin_tline TYPE TABLE OF tline,
bin_filesize TYPE i,
bin_pdfx TYPE xstring,
v_device_type TYPE rspoptype,
v_output_options TYPE ssfcompop,
v_control_parameters TYPE ssfctrlop.

CALL FUNCTION 'SSF_GET_DEVICE_TYPE'
EXPORTING
    i_language             = 'I'
    i_application          = 'SAPDEFAULT'
IMPORTING
     e_devtype              = v_device_type
EXCEPTIONS
     no_language            = 1
     language_not_installed = 2
     no_devtype_found       = 3
     system_error           = 4
     OTHERS                 = 5.

v_output_options-tdprinter = v_device_type.
v_control_parameters-no_dialog = 'X'.
v_control_parameters-getotf = 'X'.

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
      formname                 = 'ZSMARTFORM'
IMPORTING
      fm_name                  = smartform_fm
EXCEPTIONS
no_form                  = 1
no_function_module       = 2
OTHERS                   = 3.

CALL FUNCTION smartform_fm
EXPORTING
       control_parameters         = v_control_parameters
       output_options             = v_output_options
       id_document                = doc_id
IMPORTING
       job_output_info            = v_output_stampa
EXCEPTIONS
formatting_error           = 1
internal_error             = 2
send_error                 = 3
user_canceled              = 4
OTHERS                     = 5.

***CONVERT OTF TO PDF
CLEAR: bin_pdf,bin_tline,bin_filesize,bin_pdfx.

CALL FUNCTION 'CONVERT_OTF'
EXPORTING
      format                      = 'PDF'
IMPORTING
      bin_filesize                =  bin_filesize
      bin_file                    = bin_pdfx
TABLES
      otf                         = v_output_stampa-otfdata
      lines                       = bin_tline.

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
      buffer                = bin_pdfx
IMPORTING
      output_length         = bin_size
TABLES
      binary_tab            = pdf_data.

*optional code to download a file
*    DATA: file_string TYPE string.
 *   file_string = 'C:\example.pdf'.
 *    CALL FUNCTION 'GUI_DOWNLOAD'
 *      EXPORTING
 *        bin_filesize                    = bin_filesize
 *        filename                        = file_string
 *        filetype                        = 'BIN'
 *      TABLES
 *        data_tab                        = pdf_data
 *     EXCEPTIONS
 *       file_write_error                = 1
 *       no_batch                        = 2
 *       gui_refuse_filetransfer         = 3
 *       invalid_type                    = 4
 *       no_authority                    = 5
 *       unknown_error                   = 6
 *       header_not_allowed              = 7
 *       separator_not_allowed           = 8
 *       filesize_not_allowed            = 9
 *       header_too_long                 = 10
 *       dp_error_create                 = 11
 *       dp_error_send                   = 12
 *       dp_error_write                  = 13
 *       unknown_dp_error                = 14
 *       access_denied                   = 15
 *       dp_out_of_memory                = 16
 *       disk_full                       = 17
 *       dp_timeout                      = 18
 *       file_not_found                  = 19
 *       dataprovider_exception          = 20
 *       control_flush_error             = 21
 *       OTHERS                          = 22.
Categories: Abap Tags: , , ,