how to use data change notification for cdb update

46
SAP How-to Guide Sybase Unwired Platform Applicable Releases: Sybase Unwired Platform 2.1 Version 1.0 February 2012 How To...Use Data Change Notification for CDB update

Upload: syambabu-allu

Post on 07-Nov-2014

2.415 views

Category:

Education


8 download

DESCRIPTION

 

TRANSCRIPT

Page 1: How to use data change notification for cdb update

SAP How-to Guide

Sybase Unwired Platform

Applicable Releases:

Sybase Unwired Platform 2.1

Version 1.0

February 2012

How To...Use Data Change Notification for CDB update

Page 2: How to use data change notification for cdb update

© Copyright 2012 SAP AG. All rights reserved.

No part of this publication may be reproduced or transmitted in any form

or for any purpose without the express permission of SAP AG. The

information contained herein may be changed without prior notice.

Some software products marketed by SAP AG and its distributors

contain proprietary software components of other software vendors.

Microsoft, Windows, Excel, Outlook, and PowerPoint are registered

trademarks of Microsoft Corporation.

IBM, DB2, DB2 Universal Database, System i, System i5, System p,

System p5, System x, System z, System z10, System z9, z10, z9, iSeries,

pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390,

OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power

Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER,

OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS,

HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex,

MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and

Informix are trademarks or registered trademarks of IBM Corporation.

Linux is the registered trademark of Linus Torvalds in the U.S. and other

countries.

Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either

trademarks or registered trademarks of Adobe Systems Incorporated in

the United States and/or other countries.

Oracle is a registered trademark of Oracle Corporation.

UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open

Group.

Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame,

and MultiWin are trademarks or registered trademarks of Citrix Systems,

Inc.

HTML, XML, XHTML and W3C are trademarks or registered trademarks

of W3C®, World Wide Web Consortium, Massachusetts Institute of

Technology.

Java is a registered trademark of Sun Microsystems, Inc.

JavaScript is a registered trademark of Sun Microsystems, Inc., used

under license for technology invented and implemented by Netscape.

SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP

BusinessObjects Explorer, StreamWork, and other SAP products and

services mentioned herein as well as their respective logos are

trademarks or registered trademarks of SAP AG in Germany and other

countries.

Business Objects and the Business Objects logo, BusinessObjects,

Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other

Business Objects products and services mentioned herein as well as their

respective logos are trademarks or registered trademarks of Business

Objects Software Ltd. Business Objects is an SAP company.

Sybase and Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere,

and other Sybase products and services mentioned herein as well as their

respective logos are trademarks or registered trademarks of Sybase, Inc.

Sybase is an SAP company.

All other product and service names mentioned are the trademarks of

their respective companies. Data contained in this document serves

informational purposes only. National product specifications may vary.

The information in this document is proprietary to SAP. No part of this

document may be reproduced, copied, or transmitted in any form or for

any purpose without the express prior written permission of SAP AG.

This document is a preliminary version and not subject to your license

agreement or any other agreement with SAP. This document contains

only intended strategies, developments, and functionalities of the SAP®

product and is not intended to be binding upon SAP to any particular

course of business, product strategy, and/or development. Please note

that this document is subject to change and may be changed by SAP at

any time without notice.

SAP assumes no responsibility for errors or omissions in this document.

SAP does not warrant the accuracy or completeness of the information,

text, graphics, links, or other items contained within this material. This

document is provided without a warranty of any kind, either express or

implied, including but not limited to the implied warranties of

merchantability, fitness for a particular purpose, or non-infringement.

SAP shall have no liability for damages of any kind including without

limitation direct, special, indirect, or consequential damages that may

result from the use of these materials. This limitation shall not apply in

cases of intent or gross negligence.

The statutory liability for personal injury and defective products is not

affected. SAP has no control over the information that you may access

through the use of hot links contained in these materials and does not

endorse your use of third-party Web pages nor provide any warranty

whatsoever relating to third-party Web pages.

SAP “How-to” Guides are intended to simplify the product implement-

tation. While specific product features and procedures typically are

explained in a practical business context, it is not implied that those

features and procedures are the only approach in solving a specific

business problem using SAP NetWeaver. Should you wish to receive

additional information, clarification or support, please refer to SAP

Consulting.

Any software coding and/or code lines / strings (“Code”) included in this

documentation are only examples and are not intended to be used in a

productive system environment. The Code is only intended better explain

and visualize the syntax and phrasing rules of certain coding. SAP does

not warrant the correctness and completeness of the Code given herein,

and SAP shall not be liable for errors or damages caused by the usage of

the Code, except if such damages were caused by SAP intentionally or

grossly negligent.

Disclaimer

Some components of this product are based on Java™. Any code change

in these components may cause unpredictable and severe malfunctions

and is therefore expressively prohibited, as is any decompilation of these

components.

Any Java™ Source Code delivered with this product is only to be used by

SAP’s Support Services and may not be modified or altered in any way.

Page 3: How to use data change notification for cdb update

Document History

Document Version Description

1.10 << Enter your summary of changes in this version >>

1.00 First official release of this guide

Page 4: How to use data change notification for cdb update

Typographic Conventions

Type Style Description

Example Text Words or characters quoted

from the screen. These

include field names, screen

titles, pushbuttons labels,

menu names, menu paths,

and menu options.

Cross-references to other

documentation

Example text Emphasized words or

phrases in body text, graphic

titles, and table titles

Example text File and directory names and

their paths, messages,

names of variables and

parameters, source text, and

names of installation,

upgrade and database tools.

Example text User entry texts. These are

words or characters that you

enter in the system exactly

as they appear in the

documentation.

<Example

text>

Variable user entry. Angle

brackets indicate that you

replace these words and

characters with appropriate

entries to make entries in the

system.

EXAMPLE TEXT Keys on the keyboard, for

example, F2 or ENTER.

Icons

Icon Description

Caution

Note or Important

Example

Recommendation or Tip

Page 5: How to use data change notification for cdb update

Table of Contents

1. Business Scenario ................................................................................................ 1

2. Introduction to DCN .............................................................................................. 1

3. Prerequisites....................................................................................................... 2

4. Step-by-Step Procedure ....................................................................................... 3

4.1 Configuration Steps DCN with payload ....................................................................3

4.1.1 Create a project ...........................................................................................3

4.1.2 Create MBO ................................................................................................ 4

4.1.3 Create new cache policy and assign the MBO ............................................. 8

4.1.4 Deploy Project. ........................................................................................... 9

4.1.5 Push data from Firefox REST Client ............................................................ 9

4.1.6 Check the data with SQL Anywhere Client ................................................. 12

4.1.7 Multiple record transmission ..................................................................... 15

4.1.8 Delete an entry with a DCN. ....................................................................... 17

4.2 Configuration Steps DCN without payload............................................................. 19

4.2.1 Project and MBO Creation ......................................................................... 19

4.2.2 Create a Personalization Key ..................................................................... 19

4.2.3 Add an operation to the MBO.................................................................... 20

4.2.4 Push data from Firefox REST Client .......................................................... 24

5. Sample ABAP Program to send a DCN Message .................................................... 28

5.1.1 HTTP Destination Creation ....................................................................... 28

5.1.2 Sample program to send DCN data from ABAP system ........................... 30

5.1.3 Sample program to send a DCN request to pull data from ABAP system ..37

6. Appendix .......................................................................................................... 40

Page 6: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 1

1. Business Scenario It is one of the most essential parts of an SUP implementation to think about how to efficiently

update cache data in SUP. Above all, if the data volume is relatively large or users would like to have

fresher data, DCN is the preferred approach for cache updates.

This How-To Guide provides details on how to configure SUP to update the CDB with DCN and test

it.

2. Introduction to DCN Data change notification (DCN) is an update mechanism that allows an enterprise information

system (EIS) to send data changes to an Unwired Server over an HTTP or HTTPS connection using

JavaScript Object Notation (JSON) format.

There are two types of DCN. This document will only refer to Regular DCNs, and not Workflow

DCNs.

Regular DCN (DCN) –DCN to update MBO cache data in Consolidated Database(CDB)

Workflow DCN (WF-DCN) –DCN for Mobile Workflow clients Server Initiated scenario

All DCN commands support both GET and POST methods. The EIS developer creates and sends a DCN to

the Unwired Server through HTTP GET or POST operations. The portion of the DCN command parameters

that come after http://host:8000/dcn/DCNServlet, can all be in POST. Any name=var pair can be

in either the URL (GET) or in the POST. The HTTP POST method is more secure than HTTP GET methods, and therefore Sybase recommends that you include the authenticate password parameter as well as any

sensitive data provided for attributes and parameters in the POST method.

DCN with Payload and DCN with payload DCN requests can be issued with or without payload.

For DCN with Payload, a HTTP request from EIS will contain all of the cache information. When it arrives

at the Unwired Server, it will invoke standard operations (:upsert or :delete ) to insert/update/delete entries

in CDB.

Page 7: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 2

For DCN without Payload, a HTTP request from EIS will contain some parameters only, When it arrives at

the Unwired Server, it will invoke custom MBO operations, execute the operation of EIS and update the cache database (CDB).

While DCN can trigger the data push to device side as well (“server initiated synchronization“), this

document only focus on the CDB cache data update.

3. Prerequisites

In this example, SAP ABAP backend system is required.

Install Firefox and Firefox REST Client.

You can download the software from the following page.

http://www.mozilla.org/en-US/firefox

https://addons.mozilla.org/en-US/firefox/addon/restclient/

Download SQL Anywhere Client Tool from here.

http://www.sybase.com/detail?id=1087327

Page 8: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 3

4. Step-by-Step Procedure << In this section, first provide a high-level overview about the steps that need to be performed.

Then, provide a more detailed description of the procedure. >>

4.1 Configuration Steps DCN with payload ...

4.1.1 Create a project Open Mobile SDK and select File -> New -> Mobile Application Project

Name your project and click on Finish.

Page 9: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 4

4.1.2 Create MBO Right click on the folder Mobile Business Object in your project.

Choose New -> Mobile Business Object

Name your MBO and click on Next.

Choose “SAP” for data source type and select your Connection profile.

(If you haven’t created it, you also can create the connection to your SAP system).

Click on Next.

Page 10: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 5

Click on browse.

It will connect to the SAP Backend System and retrieve BOR (Business Object Repository)

information.

Enter “BAPI_FLIGHT_GETLIST” and click on Search BAPI/RFC.

Select the line of “GetList” and click on “OK”.

Page 11: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 6

Select FLIGHT_LIST as output attributes and click Finish.

You will see the MBO like this.

You also can use another type of MBO (JDBC, Web Service etc.) for DCN instead of the SAP MBO

used in this example.

Page 12: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 7

Click on the MBO.

In the property tab, chose Attributes -> Attributes Mapping.

Select “Primary Key” to AIRLINEID, CONNECTID and FLIGHTDATE.

The warning message shown below can be safely ignored.

Save project.

Page 13: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 8

4.1.3 Create new cache policy and assign the MBO Right Click on Cache Group

Choose New -> Cache Group

Name your cache Group “DCN” and choose “DCN” for the cache policy.

Drag your MBO and drop onto the new cache group “DCN”

It will look like this.

Page 14: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 9

4.1.4 Deploy Project. Right click on the project and choose deploy project..

Deploy the project with default values.

4.1.5 Push data from Firefox REST Client Open Firefox REST Client

Click on Login and enter user name and password.

Page 15: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 10

Choose POST.

Enter the URL.

http://<hostname>:<dcnport>/dcn/HttpAuthDCNServlet?cmd=dcn&domain=default&package=

DCN1:1.0

{"pkg":"dummy","messages": [{"id":"NH01112012-02-29","mbo":"Flight","op":":upsert", "cols":{ "AIRLINEID":"NH", "AIRLINE":"All Nippon Airways", "CONNECTID":"0111", "FLIGHTDATE":"2012-02-29", "AIRPORTFR":"NRT", "CITYFROM":"Tokyo", "AIRPORTTO":"SIN", "CITYTO":"Singapore", "DEPTIME":"10:40:00", "ARRTIME":"17:25:00", "ARRDATE":"2012-02-29", "PRICE":68000, "CURR":"JPY", "CURR_ISO":"JPY" }}]}

Page 16: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 11

Click on Send

You can see HTTP code 200 is back.

Page 17: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 12

4.1.6 Check the data with SQL Anywhere Client Start SQL Anywhere Client.

Right click on SQL Anywhere 12 and choose Connect.

If SQL Anywhere is running on the same host, you can use the following parameter (the user ID and

password are the defaults set during installation).

User ID: dba

Password: sql

Action: Connect with and ODBC Data Source

ODBC Data Source Name: default-cdb

Page 18: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 13

If SQL Anywhere is running on the another host, you can use the following parameter.

User ID: dba

Password: sql

Action: Connect to a running database on another computer

Host name: <host name of CDB>

Port: 5200 (this is the default port)

Page 19: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 14

Double click on Tables.

The tables holding the MBO data will be called something like [domain number e.g. d1]_[project

name] [first 6 chars of MBO name][suffix( in case of naming conflict)].

It will be look like this.

I

Right Click on the table and select “View Data in Interactive SQL”

Page 20: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 15

4.1.7 Multiple record transmission

It is possible to insert several entries with a single HTTP request.

Use

{"pkg":"dummy","messages": [{"id":" AA58502012-03-08","mbo":"Flight","op":":upsert", "cols":{ "AIRLINEID":"AA", "AIRLINE":"American Airline", "CONNECTID":"5815", "FLIGHTDATE":"2012-03-08", "AIRPORTFR":"SFO", "CITYFROM":"San Francisco", "AIRPORTTO":"HND", "CITYTO":"Haneda Tokyo", "DEPTIME":"18:05:00", "ARRTIME":"10:20:00", "ARRDATE":"2012-03-09", "PRICE":98000.1, "CURR":"JPY", "CURR_ISO":"JPY" } }, {"id":" LH07102012-02-29","mbo":"Flight","op":":upsert", "cols":{ "AIRLINEID":"LH", "AIRLINE":"Lufthansa", "CONNECTID":"0710", "FLIGHTDATE":"2012-02-29", "AIRPORTFR":"FRA", "CITYFROM":"Frankfurt", "AIRPORTTO":"NRT", "CITYTO":"Tokyo Narita", "DEPTIME":"13:25:00", "ARRTIME":"08:35:00", "ARRDATE":"2012-03-01", "PRICE":1100, "CURR":"EURO", "CURR_ISO":"EURO" } } ]}

Page 21: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 16

HTTP response returns like this. [{ "recordID": "AA00172011-07-06" ,"success":true,"statusMessage":""},

{ "recordID": " LH07102012-02-29","success":true,"statusMessage":""} ]

You can see multiple entries are added with a HTTP request.

Page 22: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 17

4.1.8 Delete an entry with a DCN.

You also can send a delete request with a DCN.

The content will look like this.

{"pkg":"dummy","messages":

[{"id":"LH0112012-02-29","mbo":"Flight","op":":delete",

"cols":{

"AIRLINEID":"LH",

"AIRLINE":"Lufthansa",

"CONNECTID":"0710",

"FLIGHTDATE":"2012-02-29"

} }]}

You can see LOGICAL_DEL field became “1” at this stage..

Go to Sybase Control Center.

Choose menu <server name> --> Domain --> <domain name> -->Packages --> <package name>

Select Cache tab.

You can see an entry is logically deleted.

Page 23: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 18

Click Purge button.

A confirmation message pops up and click on OK.

When you click on Refresh, the logically deleted entry was physically deleted.

You also can see it in SQL anywhere client.

This physical deletion can be executed through Admin API.

http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc01332.0210/doc/

html/apr1276106349832.html

Page 24: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 19

4.2 Configuration Steps DCN without payload

4.2.1 Project and MBO Creation Repeat same thing as 4.1.1 – 4.1.3 or use existing project created in 4.1.1 – 4.1.3.

4.2.2 Create a Personalization Key This personalization key will be used to pass a variable to an operation as an input parameter.

Right Click on Personalization Keys in your project and choose New -> Personalization Key

Name the Personalization Key "PK_AIRLINE" .

Choose “Transient” for the Storage.

Page 25: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 20

4.2.3 Add an operation to the MBO Double Click on the MBO.

Select property “Operations” tab and Click on “Add”.

Name your operation "CreateDataWithDCN" and select “CREATE” for operation type and click on

Next.

Page 26: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 21

Click on “Browse”.

In the pop up screen, enter “BAPI_FLIGHT_GETLIST” and click on Search BAPI/RFCs.

Select GetList and Click on OK.

Select AIRLINE and FLIGHT_LIST.

Click Finish.

Page 27: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 22

Select the created operation and click on Edit.

In the parameter tab, assign the personalization key PK_AIRLINE.

Page 28: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 23

In Cache Update Policy, check "Apply result to cache" on.

Save the project .

Right click on the project and choose Deploy Project.

Page 29: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 24

4.2.4 Push data from Firefox REST Client Send DCN message with Firefox REST client

In order to check the RFC module is really called, we set session break point in SAP system.

Logon to the SAP system with SAP GUI.

Go to SE37 and click on Display.

Put a cursor on the source code and click on the “Session Break Point” button .

Page 30: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 25

Choose HTTP POST.

Enter user id and password of SUP and set URL.

http://<server name>:<port>/dcn/HttpAuthDCNServlet?cmd=dcn&domain=default&package=DCN1:1.0

When using DCNs without payload, a custom operation like “CreateDataWithDCN” will be used

instead of standard operation :upsert or :delete.

In order to pass the personalization key as the input parameter, we use a parameter “ppm”.

The value of ppm needs to be encoded with BASE64.

First you have to prepare a string like this.

{"PK_AIRLINE":"AA "}

It will be encoded with base 64.

eyJQS19BSVJMSU5FIjoiQUEgIn0="}]

For testing purposes, you can use a freely available online encoder/decoder (do a web search for

this).

Page 31: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 26

The HTTP body should look like this.

{"pkg":"dummy","messages":[{"id":"1","mbo":"Flight","op":"CreateDataWithDCN", "ppm" :

"eyJQS19BSVJMSU5FIjoiQUEgIn0="}] }

Click on the Send button.

When the SUP server receives the HTTP request, it will immediately call an RFC module bound to

the operation.

A debugger pops up and you can see the input parameter is passed to the function module.

Click on Continue or hit F8 key.

You can see the CDB table is filled from the return parameter of the function module.

Page 32: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 27

Page 33: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 28

5. Sample ABAP Program to send a DCN Message

5.1.1 HTTP Destination Creation ...

Log in to SAP System with SAP GUI and go to Transaction Code SM59.

Click on the Create button.

Use G for connection type.

Target Host: <SUP Host name>

Service Number: <Port Number for DCN>

Path prefix: /dcn/HttpAuthDCNServlet

Query string parameters like those below will be added dynamically from the programming.

?cmd=dcn&domain=default&package=DCN1:1.0

Page 34: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 29

In the authentication tab, specify SUP user name and password.

Page 35: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 30

5.1.2 Sample program to send DCN data from ABAP system Utility INCLUDE program *&---------------------------------------------------------------------*

*& Include Z_DCN_UTILS

*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------*

*& Form write_time_to_json

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* -->FIELD_NAME text

* -->FIELD_time text

* -->JSON_CONTENT text

*----------------------------------------------------------------------

FORM write_time_to_json

USING field_name TYPE any

field_time TYPE any

CHANGING json_content.

DATA time_str TYPE string.

IF json_content <> ''.

CONCATENATE json_content ',"' field_name '"' INTO json_content.

ELSE.

CONCATENATE json_content '"' field_name '"' INTO json_content.

ENDIF.

CONCATENATE field_time(2) field_time+2(2) field_time+4(2) INTO time_str S

EPARATED BY ':'.

CONCATENATE json_content ':"' time_str '"' INTO json_content.

ENDFORM. "write_time_to_json

*&---------------------------------------------------------------------*

*& Form write_dec_to_json

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* -->FIELD_NAME text

* -->FIELD_dec text

* -->JSON_CONTENT text

*----------------------------------------------------------------------

FORM write_dec_to_json

USING field_name TYPE any

field_dec TYPE any

CHANGING json_content.

DATA dec_str TYPE string.

IF json_content <> ''.

CONCATENATE json_content ',"' field_name '"' INTO json_content.

ELSE.

CONCATENATE json_content '"' field_name '"' INTO json_content.

ENDIF.

dec_str = field_dec.

condense dec_str.

CONCATENATE json_content ':' dec_str INTO json_content.

ENDFORM. "write_dec_to_json

Page 36: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 31

*&---------------------------------------------------------------------*

*& Form write_date_to_json

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* -->FIELD_NAME text

* -->FIELD_VALUE text

* -->JSON_CONTENT text

*----------------------------------------------------------------------

FORM write_date_to_json

USING field_name TYPE any

field_date TYPE any

CHANGING json_content.

DATA date_str TYPE string.

IF json_content <> ''.

CONCATENATE json_content ',"' field_name '"' INTO json_content.

ELSE.

CONCATENATE json_content '"' field_name '"' INTO json_content.

ENDIF.

CONCATENATE field_date(4) field_date+4(2) field_date+6(2) INTO date_str S

EPARATED

BY '-'.

CONCATENATE json_content ':"' date_str '"' INTO json_content.

ENDFORM. "write_date_to_json

*&---------------------------------------------------------------------*

*& Form write_datetime_to_json

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* -->FIELD_NAME text

* -->FIELD_DATE text

* -->FIELD_TIME text

* -->JSON_CONTENT text

*----------------------------------------------------------------------*

FORM write_datetime_to_json

USING field_name TYPE any

field_date TYPE any

field_time TYPE any

CHANGING json_content.

DATA date_str TYPE string.

DATA time_str TYPE string.

IF json_content <> ''.

CONCATENATE json_content ',"' field_name '"' INTO json_content.

ELSE.

CONCATENATE json_content '"' field_name '"' INTO json_content.

ENDIF.

CONCATENATE field_date(4) field_date+4(2) field_date+6(2) INTO date_str S

EPARATED

BY '-'.

CONCATENATE field_time(2) field_time+2(2) field_time+4(2) INTO time_str S

EPARATED

BY ':'.

CONCATENATE json_content ':"' date_str 'T' time_str '"' INTO json_content.

ENDFORM. "write_datetime_to_json

*&---------------------------------------------------------------------*

*& Form write_boolean_to_json

Page 37: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 32

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* -->FIELD_NAME text

* -->FIELD_VALUE text

* -->JSON_CONTENT text

*----------------------------------------------------------------------*

FORM write_boolean_to_json

USING field_name TYPE any

field_value TYPE any

CHANGING json_content.

IF json_content <> ''.

CONCATENATE json_content ',"' field_name '"' INTO json_content.

ELSE.

CONCATENATE json_content '"' field_name '"' INTO json_content.

ENDIF.

CONCATENATE json_content ':' field_value INTO json_content.

ENDFORM. "write_boolean_to_json

*&---------------------------------------------------------------------*

*& Form write_string_to_json

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* -->FIELD_NAME text

* -->FIELD_VALUE text

* -->JSON_CONTENT text

*----------------------------------------------------------------------*

FORM write_string_to_json

USING field_name TYPE any

field_value TYPE any

CHANGING json_content.

DATA str TYPE string.

DATA strout TYPE string.

DATA strtemp TYPE string.

DATA c1 TYPE c LENGTH 1.

DATA c2 TYPE c LENGTH 4.

DATA int4 TYPE i.

str = field_value.

strout = str.

IF json_content <> ''.

CONCATENATE json_content ',"' field_name '"' INTO json_content.

ELSE.

CONCATENATE json_content '"' field_name '"' INTO json_content.

ENDIF.

REPLACE ALL OCCURRENCES OF '\' IN str WITH '\\'.

REPLACE ALL OCCURRENCES OF '"' IN str WITH '\"'.

int4 = strlen( str ).

DO int4 TIMES.

c1 = str(1).

c2 = cl_abap_conv_out_ce=>uccp( c1 ).

IF NOT c2(2) = '00'.

CONCATENATE '\u' c2 INTO strtemp.

REPLACE ALL OCCURRENCES OF c1 IN strout WITH strtemp.

ENDIF.

SHIFT str.

ENDDO.

CONCATENATE json_content ':"' strout '"' INTO json_content.

ENDFORM. "write_string_to_json

Page 38: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 33

Sample program to send a DCN message. *&---------------------------------------------------------------------*

*& Report Z_FLIGHT_JSON_SEND

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT z_flight_json_send.

INCLUDE z_dcn_utils .

DATA : lv_body TYPE string ,

lv_id TYPE string ,

json_content TYPE string ,

json_data TYPE string ,

json_col TYPE string ,

dcn_id TYPE string.

DATA : http_rc TYPE i ,

http_reason TYPE string.

DATA : lt_flight TYPE TABLE OF bapisfldat ,

lt_return TYPE TABLE OF bapiret2 .

FIELD-SYMBOLS <flight> LIKE LINE OF lt_flight.

DATA : dest TYPE rfcdest ,

client TYPE REF TO if_http_client ,

errortext TYPE string,

subrc TYPE sy-subrc.

DATA: lv_resp TYPE string,

lt_match_open TYPE match_result_tab ,

lt_match_close TYPE match_result_tab ,

lw_match_open TYPE match_result ,

lw_match_close TYPE match_result,

lt_string TYPE TABLE OF string ,

lw_string TYPE string ,

lf_length TYPE i.

START-OF-SELECTION.

DATA maxrow TYPE bapisflaux-bapimaxrow .

maxrow = 15 .

CALL FUNCTION 'BAPI_FLIGHT_GETLIST'

* EXPORTING

* max_rows = maxrow

TABLES

flight_list = lt_flight

return = lt_return.

LOOP AT lt_flight ASSIGNING <flight> .

CLEAR : json_col , json_content.

CONCATENATE <flight>-airlineid <flight>-connectid <flight>-

flightdate INTO lv_id .

PERFORM write_string_to_json USING 'id' lv_id CHANGING json_conten

t .

Page 39: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 34

PERFORM write_string_to_json USING 'mbo' 'Flight' CHANGING json_conten

t .

PERFORM write_string_to_json USING 'op' ':upsert' CHANGING json_conten

t .

PERFORM write_string_to_json USING 'AIRLINEID' <flight>-

airlineid CHANGING json_col.

PERFORM write_string_to_json USING 'AIRLINE' <flight>-

airline CHANGING json_col.

PERFORM write_string_to_json USING 'CONNECTID' <flight>-

connectid CHANGING json_col.

PERFORM write_date_to_json USING 'FLIGHTDATE' <flight>-

flightdate CHANGING json_col.

PERFORM write_string_to_json USING 'AIRPORTFR' <flight>-

airportfr CHANGING json_col.

PERFORM write_string_to_json USING 'CITYFROM' <flight>-

cityfrom CHANGING json_col.

PERFORM write_string_to_json USING 'AIRPORTTO' <flight>-

airportto CHANGING json_col.

PERFORM write_string_to_json USING 'CITYTO' <flight>-

cityto CHANGING json_col.

PERFORM write_time_to_json USING 'DEPTIME' <flight>-

deptime CHANGING json_col.

PERFORM write_date_to_json USING 'ARRDATE' <flight>-

arrdate CHANGING json_col.

PERFORM write_time_to_json USING 'ARRTIME' <flight>-

arrtime CHANGING json_col.

PERFORM write_dec_to_json USING 'PRICE' <flight>-

price CHANGING json_col.

PERFORM write_string_to_json USING 'CURR' <flight>-

curr CHANGING json_col.

PERFORM write_string_to_json USING 'CURR_ISO' <flight>-

curr_iso CHANGING json_col.

IF sy-tabix = 1 .

CONCATENATE '{' json_content ',"cols":{' json_col '}}' INTO json

_data .

ELSE.

CONCATENATE json_data ',{' json_content ',"cols":{' json_col '}}

' INTO json_data .

ENDIF.

ENDLOOP.

CONCATENATE '{"pkg":"dummy","messages":[' json_data ']}' INTO lv_body.

dest = 'DCN_DESTINATION' .

cl_http_client=>create_by_destination(

EXPORTING destination = dest

IMPORTING client = client

EXCEPTIONS

destination_not_found = 1

internal_error = 2

argument_not_found = 3

destination_no_authority = 4

plugin_not_active = 5

OTHERS = 6 ) .

client->request->set_form_field(

EXPORTING

name = 'cmd'

value = 'dcn') .

Page 40: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 35

client->request->set_form_field(

EXPORTING

name = 'domain'

value = 'default') .

client->request->set_form_field(

EXPORTING

name = 'package'

value = 'DCN1:1.0') .

client->request->set_cdata( lv_body ).

client->send(

EXCEPTIONS http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

OTHERS = 4 ) .

IF sy-subrc <> 0.

client->get_last_error(

IMPORTING code = subrc

message = errortext ) .

WRITE: / 'communication_error( send )',

/ 'code: ', subrc, 'message:' , errortext.

EXIT.

ENDIF.

client->receive(

EXCEPTIONS http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

OTHERS = 4 ) .

IF sy-subrc <> 0.

client->get_last_error(

IMPORTING code = subrc

message = errortext ) .

WRITE: / 'communication_error( receive )',

/ 'code: ', subrc, 'message: ', errortext.

EXIT.

ELSE. " not communicaiton error

client->response-

>get_status( IMPORTING code = http_rc reason = http_reason ).

IF http_rc NE 200 .

WRITE : 'Error. HTTP response code:' , http_rc , / , http_reason .

ELSE.

*HTTP return code 200 Check Application error.

*check if there is any record which contain false with Regex.

lv_resp = client->response->get_cdata( ).

FIND ALL OCCURRENCES OF REGEX '\{' IN lv_resp RESULTS lt_match_open.

FIND ALL OCCURRENCES OF REGEX '\}' IN lv_resp RESULTS lt_match_close.

LOOP AT lt_match_open INTO lw_match_open .

READ TABLE lt_match_close INTO lw_match_close INDEX sy-tabix.

lf_length = lw_match_close-offset - lw_match_open-offset + 1 .

lw_string = lv_resp+lw_match_open-offset(lf_length) .

APPEND lw_string TO lt_string .

ENDLOOP.

Page 41: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 36

DATA num TYPE i .

LOOP AT lt_string INTO lw_string.

FIND ALL OCCURRENCES OF REGEX 'false' IN lw_string MATCH COUNT num

.

IF num > 0 .

WRITE lw_string .

ENDIF.

ENDLOOP.

ENDIF.

ENDIF.

Page 42: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 37

5.1.3 Sample program to send a DCN request to pull data from ABAP system

...

*&---------------------------------------------------------------------*

*& Report Z_FLIGHT_JSON_WITHOUT_PAYLOAD

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT z_flight_json_without_payload.

PARAMETERS lp_fid TYPE bapisflkey-airlineid.

DATA : lv_str TYPE string ,

lv_x64str TYPE string ,

lv_body TYPE string .

DATA : http_rc TYPE i ,

http_reason TYPE string.

DATA : dest TYPE rfcdest ,

client TYPE REF TO if_http_client ,

errortext TYPE string,

subrc TYPE sy-subrc.

DATA: lv_resp TYPE string,

lt_match_open TYPE match_result_tab ,

lt_match_close TYPE match_result_tab ,

lw_match_open TYPE match_result ,

lw_match_close TYPE match_result,

lt_string TYPE TABLE OF string ,

lw_string TYPE string ,

lf_length TYPE i.

lv_str = lp_fid .

CONCATENATE '{"PK_AIRLINE":"' lv_str '"}' INTO lv_str .

lv_x64str = cl_http_utility=>if_http_utility~encode_base64( lv_str ) .

CONCATENATE

'{"pkg":"dummy","messages":[{"id":"'

lp_fid

'","mbo":"Flight","op":"CreateDataWithDCN", "ppm" : "'

lv_x64str

'"}] }' INTO lv_body.

dest = 'DCN_DESTINATION' .

cl_http_client=>create_by_destination(

EXPORTING destination = dest

IMPORTING client = client

EXCEPTIONS

destination_not_found = 1

Page 43: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 38

internal_error = 2

argument_not_found = 3

destination_no_authority = 4

plugin_not_active = 5

OTHERS = 6 ) .

client->request->set_form_field(

EXPORTING

name = 'cmd'

value = 'dcn') .

client->request->set_form_field(

EXPORTING

name = 'domain'

value = 'default') .

client->request->set_form_field(

EXPORTING

name = 'package'

value = 'DCN1:1.0') .

client->request->set_cdata( lv_body ).

client->send(

EXCEPTIONS http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

OTHERS = 4 ) .

IF sy-subrc <> 0.

client->get_last_error(

IMPORTING code = subrc

message = errortext ) .

WRITE: / 'communication_error( send )',

/ 'code: ', subrc, 'message:' , errortext.

EXIT.

ENDIF.

client->receive(

EXCEPTIONS http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

OTHERS = 4 ) .

IF sy-subrc <> 0.

client->get_last_error(

IMPORTING code = subrc

message = errortext ) .

WRITE: / 'communication_error( receive )',

/ 'code: ', subrc, 'message: ', errortext.

EXIT.

ELSE. " not communicaiton error

client->response-

Page 44: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 39

>get_status( IMPORTING code = http_rc reason = http_reason ).

IF http_rc NE 200 .

WRITE : 'Error. HTTP response code:' , http_rc , / , http_reason .

ELSE.

*HTTP return code 200 Check Application error.

*check if there is any record which contain false with Regex.

lv_resp = client->response->get_cdata( ).

FIND ALL OCCURRENCES OF REGEX '\{' IN lv_resp RESULTS lt_match_open.

FIND ALL OCCURRENCES OF REGEX '\}' IN lv_resp RESULTS lt_match_close.

LOOP AT lt_match_open INTO lw_match_open .

READ TABLE lt_match_close INTO lw_match_close INDEX sy-tabix.

lf_length = lw_match_close-offset - lw_match_open-offset + 1 .

lw_string = lv_resp+lw_match_open-offset(lf_length) .

APPEND lw_string TO lt_string .

ENDLOOP.

DATA num TYPE i .

LOOP AT lt_string INTO lw_string.

FIND ALL OCCURRENCES OF REGEX 'false' IN lw_string MATCH COUNT num .

IF num > 0 .

WRITE lw_string .

ENDIF.

ENDLOOP.

ENDIF.

ENDIF.

Page 45: How to use data change notification for cdb update

How To... Use Data Change Notificatoin for CDB update

May 2012 40

6. Appendix

Consideration points / limitations

If you use the DCN mechanism, take care to specify primary key(s) for MBOs, otherwise duplicate records will be inserted.

It’s not possible to have multiple CDB partitions when using DCNs (like with the on-demand cache policy).

It is technically possible to send a DCN message to an MBO belonging to other cache policy than DCN. However it is highly recommended to use the DCN cache policy for DCN updates.

You must use exactly the same attribute name in the return parameter of the operation when using DCNs without payload

If you’d like you send messages asynchronously from an ABAP backend,, internal queuing technology like qRFC or bgRFC can help.

DCN without payload cannot update multiple MBOs at the same time even if multiple MBOs were generated from a RFC Module/Web service.

Page 46: How to use data change notification for cdb update

www.sap.com/contactsap

www.sdn.sap.com/irj/sdn/howtoguides