NetWeaver Developer’s Guide Release: SAP NetWeaver 2004s Using ABAP SAP AG Dietmar-Hopp-Allee 16 69190 Walldorf Germany T +49/18 05/34 34 24 F +49/18 05/34 34 20 www.sap.com SAP, R/3, mySAP, mySAP.com, xApps, xApp, SAP NetWeaver, © Copyright 2005 SAP AG. All rights reserved. and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of No part of this publication may be reproduced or transmitted in SAP AG in Germany and in several other countries all over the any form or for any purpose without the express permission of world. All other product and service names mentioned are the SAP AG. The information contained herein may be changed trademarks of their respective companies. Data contained in this without prior notice. document serves informational purposes only. National product specifications may vary. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. These materials are subject to change without notice. These Microsoft, Windows, Outlook, and PowerPoint are registered materials are provided by SAP AG and its affiliated companies trademarks of Microsoft Corporation. ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP IBM, DB2, DB2 Universal Database, OS/2, Parallel Sysplex, Group shall not be liable for errors or omissions with respect to MVS/ESA, AIX, S/390, AS/400, OS/390, OS/400, iSeries, the materials. The only warranties for SAP Group products and pSeries, xSeries, zSeries, z/OS, AFP, Intelligent Miner, services are those that are set forth in the express warranty WebSphere, Netfinity, Tivoli, and Informix are trademarks or statements accompanying such products and services, if any. registered trademarks of IBM Corporation in the United States Nothing herein should be construed as constituting an additional and/or other countries. warranty. Oracle is a registered trademark of Oracle Corporation. Disclaimer UNIX, X/Open, OSF/1, and Motif are registered trademarks of Some components of this product are based on Java™. Any code the Open Group. change in these components may cause unpredictable and severe malfunctions and is therefore expressively prohibited, as is any Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, decompilation of these components. VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. Any Java™ Source Code delivered with this product is only to be used by SAP’s Support Services and may not be modified or HTML, XML, XHTML and W3C are trademarks or registered altered in any way. trademarks of W3C®, World Wide Web Consortium, Massachusetts Institute of Technology. Any software coding and/or code lines / strings ("Code") included in this documentation are only examples and are not intended to Java is a registered trademark of Sun Microsystems, Inc. be used in a productive system environment. The Code is only intended better explain and visualize the syntax and phrasing JavaScript is a registered trademark of Sun Microsystems, Inc., rules of certain coding. SAP does not warrant the correctness and used under license for technology invented and implemented by completeness of the Code given herein, and SAP shall not be Netscape. liable for errors or damages caused by the usage of the Code, except if such damages were caused by SAP intentionally or MaxDB is a trademark of MySQL AB, Sweden. grossly negligent. Typographic Conventions Icons Type Style Represents Icon Meaning Example Text Words or characters quoted from Caution the screen. These include field names, screen titles, Example pushbuttons labels, menu names, menu paths, and menu Note options. Cross-references to other Recommendation documentation. Example text Emphasized words or phrases in Syntax body text, graphic titles, and table titles. EXAMPLE TEXT Technical names of system objects. These include report names, program names, transaction codes, table names, and key concepts of a programming language when they are surrounded by body text, for example, SELECT and INCLUDE. Example text Output on the screen. This includes 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 Exact user entry. 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. Contents 1 USING ABAP.....................................................................................................................1 2 GETTING INVOLVED........................................................................................................2 2.1 The Origins of ABAP – an Overview.........................................................................2 2.2 ABAP and Its Infrastructure.......................................................................................3 2.3 ABAP and the SAP NW Application Server..............................................................4 3 GO AND CREATE.............................................................................................................5 3.1 Creating a Database Table and Data Elements.......................................................6 3.2 Creating a Database Table – Part 1.........................................................................6 3.3 Creating Data Elements............................................................................................7 3.4 Creating a Database Table – Part 2.........................................................................8 3.5 Defining the Basic Attributes of a Global Class......................................................10 3.6 Methods, Types, and Attributes of a Global Class..................................................11 4 CORE DEVELOPMENT TASKS.....................................................................................16 4.1 UI Technology.........................................................................................................17 4.2 Advantages of ABAP Objects.................................................................................18 4.2.1 Why You Should Use ABAP Objects...........................................................18 4.2.2 Using ABAP Objects - Best Practices..........................................................20 4.3 Sharing Data...........................................................................................................20 4.3.1 Shared Objects - Basic Features.................................................................21 4.3.2 Shared Objects - Advanced Features..........................................................22 4.3.3 Best Practices...............................................................................................23 4.4 Exception Handling.................................................................................................24 4.4.1 Class-Based Exceptions in ABAP................................................................24 4.4.2 Basic Features..............................................................................................24 4.4.3 Planning Exception Handling and Delegating Exceptions...........................25 4.4.4 The Three Exception Classes in ABAP........................................................27 4.4.5 Defining an Exception Class........................................................................29 4.4.6 New Exceptions and Former Concepts of Error Handling...........................30 4.5 Developing Persistence..........................................................................................30 4.5.1 The Standard Path to Persistence...............................................................31 4.5.2 Other Persistence Options...........................................................................33 4.5.3 All or Nothing – Database and SAP Transactions.......................................34 4.5.4 Exclusive Database Access - Lock Concept of the SAP NW AS.................35 4.5.5 Restricting Access - Authorization Concept of the SAP NW AS..................36 4.6 Using Connectivity and Interoperability...................................................................37 4.6.1 The Integration Strategy of SAP and Web Services....................................38 4.6.2 How Web Services Function........................................................................38 4.6.3 Generating Interfaces and Proxies – The Outside-In Approach..................40 4.6.4 XML-Based Process Integration with the Exchange Infrastructure..............41 4.7 Making Enhancements............................................................................................42 4.7.1 How Do Modification-Free Enhancements Work?.......................................42 4.7.2 Enhancement Framework Details Based on an Example............................44 4.7.3 New BAdIs....................................................................................................47 4.7.4 The Switch Framework.................................................................................49 5 ENSURING QUALITY......................................................................................................53 5.1 ABAP Unit Tests.....................................................................................................53 5.2 Effective Testing with ABAP Unit............................................................................56 5.3 The Philosophy of Unit Tests..................................................................................57 5.4 Unit Tests and Transports.......................................................................................59 5.5 Advantages of ABAP Unit Tests.............................................................................59 October 2005 1 Using ABAP Being able to use ABAP effectively requires more than just knowledge of the keywords. As a programmer, you need to have a basic understanding of the architecture of the SAP NetWeaver Application Server ABAP and how specific fundamental concepts are implemented in ABAP and its infrastructure. The aim of this guide is to enable you to use the possibilities of ABAP and the NW Application Server ABAP as effectively as possible. It is to provide you with guidelines on how best to solve certain standard tasks, which are part of any business programming. Determining the best method to solve a problem in ABAP also means finding the most useful of the various possibilities this language provides. In ABAP, there are often a number of different ways of achieving the same goal. This is the price that has to be paid for evolutionary development and downward compatibility. If old constructs are still required in a new release, because they appear in important application programs, they remain part of the language. Programmers can continue to use them, but should be aware in which cases there are now more effective ways of solving a specific task. Not only does the ABAP Developer Guider provide programmers with an overview of working with ABAP, but also show the best solution to standard problems of business programming. In any cases where there are multiple, equally useful solutions, this guide is to provide information on their advantages and disadvantages. The section Getting Involved [Seite 2] introduces the basic features of the ABAP language and the SAP NetWeaver Application Server ABAP. You will learn how ABAP and its tools support the entire lifecycle of an application and what tailor-made, business programming features the language offers. You will also be familiarized with the three-layer architecture of the server and learn why you do not develop locally with ABAP but always centrally on the server. Programming is a practical experience, and the best way to get an impression of the possibilities of a language and its infrastructure is to write a small program in this language and thus understand its development. The tutorial in the section Go and Create [Seite 5] shows how easily you can use ABAP to create a database table – declaratively and with metadata control – with the relevant fields and attributes. A simple ABAP Objects program manages the data of this table. With the knowledge gained from these two sections, you are ready to understand how specific basic programming concepts are implemented in ABAP. The section Core Development Tasks [Seite 16] provides an overview of the following topics: • UI Technology: [Seite 17] The standard Web user interface is Web Dynpro. For special requirements, Business Server Pages can be configured. • ABAP Objects [Seite 18] offers a number of advantages, even if you want to continue using procedural programming. If you want to use new ABAP features, you have to use object-oriented interfaces anyway. • Sharing Data: [Seite 20] With ABAP shared objects, you can aggregate data once at a central location and the different users and programs can then access this data without the need for copying. • Exception Handling: [Seite 24] With the class-based exception concept of ABAP, you can define a special control flow for a specific error situation and provide the user with information about the error. • Developing Persistency: [Seite 30] For permanent storage of data in ABAP, you use relational database tables by means of database-independent Open SQL, which is Using ABAP 1 October 2005 integrated in ABAP. However, you can also store selected objects transparently or access the integrated database or other databases using proprietary SQL. • Using Connectivity and Interoperability: [Seite 37] The Exchange Infrastructure and Web services are the means by which developers can implement a service-oriented architecture. With Web services, you can provide and consume services independently of implementation or protocol. Furthermore, you can do so within NetWeaver and in the communication with other systems. With the features of the Exchange Infrastructure, you can enable, manage, and adapt integration scenarios between systems. • Making Enhancements: [Seite 42] With the Enhancement Framework, you can enhance programs, function modules, and global classes without modification as well as replace existing code. The Switch Framework enables you activate only specific development objects or enhancements in a system. Testing is an essential part of the software development cycle. The ABAP development environment also provides support for this. The section Ensuring Quality [Seite 53] introduces the basic features of ABAP Unit, a framework with which you can take advantage of unit testing in ABAP. The relevant tools are fully integrated in the standard development environment (SE80, SE24, SE37, SE38) and the code required for testing is implemented in ABAP Objects. 2 Getting Involved When you program with ABAP on the application server, you profit from a range of services, which in other languages would have to be programmed manually with a great amount of effort. Many of these services have resulted from the fact that the SAP NW Application Server is the outcome of a long development period. You are thus benefiting from the experience of many programmers, whose suggestions for improvement are implemented in the server today. The following provides you with an overview of the origins of ABAP, its infrastructure, and the underlying server architecture. You are thus provided with information introducing you to ABAP and its infrastructure. If you are used to languages like C or Java, some aspects of programming with ABAP will at first appear unfamiliar, some will seem unusual, and some may even appear impractical. If you use the possibilities of the language effectively, you will soon see that many aspects of business programming can be done with ABAP very simply. In this language, much has already been anticipated and created. 2.1 The Origins of ABAP – an Overview As a programming language of SAP, ABAP (Advanced Business Application Programming) has contributed in no small way to the success of SAP’s application software. Since the seventies, when the language was a macro assembler, ABAP has been consistently optimized for business programming. With the introduction of the R/3 System in the nineties, the ABAP language – now in its fourth generation – became the basis of the entire system, in which all applications and large parts of Basis (as it was referred to at the time) were implemented. A further, significant step forward was the development of ABAP Objects with Release 4.6. In keeping with SAP’s philosophy of safeguarding the existing billions’ worth of investments Using ABAP 2 October 2005 made by customers in ABAP, ABAP Objects is an object-based enhancement of the procedural ABAP language available up to that point. Thanks to this evolutionary strategy with its high level of downward compatibility, the many millions of lines of productive, procedural ABAP coding still run on the NetWeaver Application Server – the name for the technological basis of SAP since Release 6.10. ABAP is thus not only a mature language that has proven itself in the practical field, but – on the basis of its evolutionary strategy – it continues to be suited to the requirements of business programming. The requests, suggestions, and requirements of the software developers involved have flowed into this language over the years. With the quality of its application programs, it has made SAP a world leader in the development of business software. Up to the year 2000, ABAP was the sole language in which SAP programmed its successful business software. Since then, the SAP NetWeaver AS combines an ABAP and a J2EE server. SAP uses the opportunities provided by both technologies for its products, but still continues to invest in the further development of ABAP. 2.2 ABAP and Its Infrastructure There are more advantages to programming with ABAP than simply using a sophisticated programming language, and there are very good reasons for this. You require more than just the programming language to write business applications successfully. Efficient programming not only involves the writing of source code, especially in the case of very large projects. A single program with its code is only a building block in a component, which in turn is part of a solution. Furthermore, it is not just the creation of the solution that is relevant, but its entire lifecycle, especially considering that, on average, maintenance costs make up some 60% of software costs. When you write an ABAP program, it is, from the outset, integrated in an environment that supports its entire lifecycle. The ABAP Development Workbench, the programming environment of the SAP NW AS ABAP, supports the entire software development cycle with tools for: • Programming in the ABAP language • Efficient debugging • Static and dynamic tests of different scales • Performance analyses • Organizing large projects • Updates and importing patches • Source code management • Connectors to Java and .NET world With regard to the actual programming, ABAP has the following features: • Tailor-made data types for business programming – for example, the internal table, in which you can dynamically create tables with row types structured to any depth and can search their rows for specific fields. • Object-oriented exception handling • Support for dynamic programming; from dynamic data generation to dynamic creation of types and entire programs • Simple management of multilingual texts Using ABAP 3 October 2005 • Integration of the DML part of SQL into the language • Creation of database tables via the definition of metadata • Definition of various types of data structures, which are then available throughout the server • Conversion of XML data into ABAP structures • Simple handling of transactions – that is, program units – which can only be executed fully or not all, in order to ensure data integrity • Possibility to prepare program enhancements so that customers can enhance programs without modification – that is, without the changes being lost after an update or having to be merged manually • Possibility to publish procedures as Web services or access Web services as a user 2.3 ABAP and the SAP NW Application Server Overview If you have experience in programming or languages such as Java and C, you have probably asked yourself why, in the context of ABAP, we speak of the programming environment of the server. In ABAP, you program on the server from the outset. You do not develop the programs locally on your PC, nor do you store the sources in a versioning system or deploy the programs on the server at a later stage. To program with ABAP, you therefore require access to and developer authorization for the SAP NW Application Server ABAP. You write your programs using the ABAP Editor of the ABAP Workbench, which is also part of the server. Once you activate your source code, platform-independent byte code is generated, which the runtime environment interprets for the program execution. However, the SAP NW Application Server ABAP not only contains the programming environment with its tools and those for supporting the software lifecycle. ABAP programs always run on the SAP NW AS ABAP and the architecture of the environment in which they run must be taken into account when designing these programs. For this purpose, you first require some information about this architecture. The Layers of the SAP NW Application Server The SAP NW Application Server ABAP consists of three layers: presentation, application, and database. The database layer of a system is made up of a central database with a database management system and the database itself. It not only contains the user data, but also the entire program code of the SAP NW AS and application programs, all administrative data, and Customizing settings. The application layer lies between the database layer and the presentation layer. It consists of one or more application servers (more than 100 servers possible) and a single message server, which is responsible for communication and load distribution within this layer. The programs that process the business logic of an application run in this layer. Technically, the actual processing takes place in work processes, which represent operating system processes. A fixed database connection is assigned to each work process. There is no fixed Using ABAP 4 October 2005 assignment of users to work processes. A lengthy user session can utilize different work processes sequentially. The kernel and the Basis services make up the part of the application layer that is specific to both the operating system and the database. They are responsible for user and process management, database access, communication with other systems, as well as system monitoring and administration. The software processes or virtual machines, that interpret the platform-independent byte code as operating system-specific machine language commands, run in the kernel. The presentation layer represents the interface with the user and is responsible for the screen display. This layer receives user entries – that is, mouse-clicks and keyboard input – and passes them on to the application layer. Furthermore, it receives data from the application layer and displays it to the user. The purpose of this layering is high performance and scalability. The layer division is purely logical. It does not imply over how many machines the system is distributed. All three layers can actually run on a single computer. However, in the case of large, production applications of customers, this is more of a theoretical possibility, since it would counteract the scalability of the three-level architecture. For demonstration purposes, such as in the case of the mini Basis, it is possible to install all three layers on a single PC. In a production operation, the database usually has a separate machine, as it contains all program, Customizing, and user data. The application layer is spread across several machines, depending on the anticipated load, while the presentation layer runs on the workstations of the individual users. What Does All this Mean for the Programmer? The layers communicate with one another via network connections, meaning that programmers need not concern themselves with these connections. When you write application programs, these run in the application layer in work processes. Therefore, there is always an open database connection. The specifications of the individual database are of just as little concern to you as the operating system. The kernel is responsible for the communication with all supported operating systems and databases. Therefore, you can program platform-independently. 3 Go and Create In the following sections, you will learn how you can easily use the ABAP Dictionary to create a table in the database and a system-wide class that manages this data. You create the database table with its fields and the desired attributes via metadata in the ABAP Dictionary. When you activate the object, the table is created on the database. The field types created in the ABAP Dictionary are then available system-wide. You can access them from your ABAP program if you use ABAP Objects to program corresponding service methods for accessing the table. 3.1 Creating a Database Table and Data Elements Before discussing the theory, let us first look at an example that displays how simple it is to program in ABAP. The aim of the development is to have a small program that stores names on the database, finds them again, and lists them on the screen. The first thing you require is a database table in which the names can be stored. Using ABAP 5
Description: