|
| 1 | +# HANDS-ON EXERCISE FOR WEEK 2 UNIT 2: CREATING THE DATABASE TABLES |
| 2 | + |
| 3 | + |
| 4 | +## Introduction |
| 5 | +In this hands-on exercise, you will create the database for storing the travel and booking data and fill them with demo data. |
| 6 | + |
| 7 | +You can watch [week 2 unit 2: Creating the Database Tables](https://open.sap.com/courses/cp13/items/2hlNxSdQ9JJZClJcxQOQb6) on the openSAP platform. |
| 8 | + |
| 9 | +> **Hints and Tips** |
| 10 | +> Speed up the typing by making use of the Code Completion feature (shortcut *Ctrl+Space*) and the prepared code snippets provided. |
| 11 | +> You can easily open an object with the shortcut *Ctrl+Shift+A*, format your source code using the Pretty Printer feature *Ctrl+1* and toggle the fullscreen of the editor using the shortcut *Ctrl+M*. |
| 12 | +> |
| 13 | +> A great overview of ADT shortcuts can be found here: [Useful ADT Shortcuts](https://blogs.sap.com/2013/11/21/useful-keyboard-shortcuts-for-abap-in-eclipse/) |
| 14 | +> |
| 15 | +> Please note that the placeholder **`####`** used in object names in the exercise description must be replaced with the suffix of your choice during the exercises. The suffix can contain a maximum of 4 characters (numbers and letters). |
| 16 | +> The screenshots in this document have been taken with the suffix `1234` and system `D20`. Your system id will be `TRL`. |
| 17 | +
|
| 18 | +> Please note that the ADT dialogs and views may change in the future due to software updates. |
| 19 | +
|
| 20 | +Follow the instructions below. |
| 21 | + |
| 22 | +## Step 1. Create the ABAP Package |
| 23 | +First create the new ABAP package **`ZRAP_TRAVEL_####`** (where `####` is your chosen suffix) to group the various development artefacts that you’re going to create during the greenfield implementation of our Travel App. It will be a sub-package of package **`ZRAP_####`** (where `####` is your chosen suffix) created in week 1 unit 6. |
| 24 | + |
| 25 | +1. Go to the Project explorer, right-click on your package **`ZRAP_####`** (where `####` is your chosen suffix) you created in week 1 unit 6 and choose the context menu entry **_New > ABAP Package_**. |
| 26 | + |
| 27 | +  |
| 28 | + |
| 29 | +2. Maintain **`ZRAP_TRAVEL_####`** as name (where `####` is your chosen suffix) and a meaningful description (e.g. _**Greenfield Implementation - Travel List Report App**_) and choose **Next** to continue. |
| 30 | +The Project and the Superpackage fields are automatically assigned. |
| 31 | + |
| 32 | + |
| 33 | +  |
| 34 | + |
| 35 | + |
| 36 | +4. Select an existing transport request or create a new one and then choose **Finish** to create the new package. |
| 37 | + |
| 38 | + |
| 39 | +  |
| 40 | + |
| 41 | + |
| 42 | + The package is now created. |
| 43 | + |
| 44 | + |
| 45 | +  |
| 46 | + |
| 47 | +5. Right-click on the new created package and choose **Add to Favorites Packages** to add it to your favorites. |
| 48 | + |
| 49 | + |
| 50 | +  |
| 51 | + |
| 52 | + |
| 53 | +## Step 2. Create the Travel Database Table |
| 54 | +You will now create the database table **`ZRAP_ATRAV_####`** (where `####` is your chosen suffix), to store the travel data. |
| 55 | +A Travel entity defines general travel data, such as the agency ID or customer ID, the status of the travel booking, and the price of travel. |
| 56 | +1. Right click on your package **`ZRAP_TRAVEL_####`**, choose **_New > Other ABAP Repository Object_** from the context menu. |
| 57 | + |
| 58 | + |
| 59 | +  |
| 60 | + |
| 61 | +2. Enter `database` in the search field, choose **Database table** in the list and then choose **Next**. |
| 62 | + |
| 63 | + |
| 64 | +  |
| 65 | + |
| 66 | +3. Maintain **`ZRAP_ATRAV_####`** as name and a meaningful description (e.g. _**Travel data**_) in the appearing dialog and choose **Next**. |
| 67 | + |
| 68 | + |
| 69 | +  |
| 70 | + |
| 71 | +4. Assign a transport request and choose **Finish**. |
| 72 | + |
| 73 | + |
| 74 | +  |
| 75 | + |
| 76 | + The table is created, and the default code displayed in the new editor. |
| 77 | + The default table-specific technical settings are specified using annotations at the top. |
| 78 | + |
| 79 | + |
| 80 | +  |
| 81 | + |
| 82 | +5. Replace the default source code with the code snippet provided below and replace all occurrences of `####` with your chosen suffix. |
| 83 | + You can make use of the Replace All feature (**Ctrl+F**) in ADT for the purpose. |
| 84 | + |
| 85 | + <pre> |
| 86 | + @EndUserText.label : 'Travel data' |
| 87 | + @AbapCatalog.enhancementCategory : #NOT_EXTENSIBLE |
| 88 | + @AbapCatalog.tableCategory : #TRANSPARENT |
| 89 | + @AbapCatalog.deliveryClass : #C |
| 90 | + @AbapCatalog.dataMaintenance : #RESTRICTED |
| 91 | + define table zrap_atrav_#### { |
| 92 | + key client : mandt not null; |
| 93 | + key travel_uuid : sysuuid_x16 not null; |
| 94 | + travel_id : /dmo/travel_id; |
| 95 | + agency_id : /dmo/agency_id; |
| 96 | + customer_id : /dmo/customer_id; |
| 97 | + begin_date : /dmo/begin_date; |
| 98 | + end_date : /dmo/end_date; |
| 99 | + @Semantics.amount.currencyCode : 'zrap_atrav_####.currency_code' |
| 100 | + booking_fee : /dmo/booking_fee; |
| 101 | + @Semantics.amount.currencyCode : 'zrap_atrav_####.currency_code' |
| 102 | + total_price : /dmo/total_price; |
| 103 | + currency_code : /dmo/currency_code; |
| 104 | + description : /dmo/description; |
| 105 | + overall_status : /dmo/overall_status; |
| 106 | + created_by : syuname; |
| 107 | + created_at : timestampl; |
| 108 | + last_changed_by : syuname; |
| 109 | + last_changed_at : timestampl; |
| 110 | + local_last_changed_at : timestampl; |
| 111 | + } |
| 112 | + </pre> |
| 113 | + |
| 114 | + |
| 115 | +  |
| 116 | + |
| 117 | + |
| 118 | + **Short explanations:** |
| 119 | + - Some data elements from the ABAP Flight Reference Scenario (namespace `/DMO/`) are used. |
| 120 | + - The table key consists of the `CLIENT` field and the `TRAVEL_UUID` field which is a technical key (16 Byte UUID). |
| 121 | + - A human-readable travel identifier: `TRAVEL_ID` |
| 122 | + - The field CURRENCY_CODE is specified as currency key for the amount fields `BOOKING_FEE` and `TOTAL_PRICE` using the semantic annotation `@Semantics.amount.currencyCode` |
| 123 | + - Some standard administrative fields are defined: `CREATED_BY`, `CREATED_AT`, `LAST_CHANGED_BY`, `LAST_CHANGED_AT` and `LOCAL_LAST_CHANGED_AT`. |
| 124 | + |
| 125 | +6. Save  and activate  the changes. |
| 126 | + |
| 127 | +7. Press **F8** to start the data preview. |
| 128 | + Well, the database table is empty for now, so no data is displayed. |
| 129 | + |
| 130 | + |
| 131 | +  |
| 132 | + |
| 133 | +## Step 3. Create the Booking Database Table |
| 134 | +You will now create the database table **`ZRAP_ABOOK_####`** (where `####` is your chosen suffix), to store the booking data. |
| 135 | +A Booking entity comprises general flight and booking data, the customer ID for whom the flight is booked as well as the travel ID to which the booking belongs – and some admin fields. |
| 136 | + |
| 137 | +1. Right click on the **Database Tables** folder, choose **New Database Table** from the context menu. |
| 138 | + |
| 139 | +  |
| 140 | + |
| 141 | +3. Maintain **`ZRAP_ABOOK_####`** as name and a meaningful description (e.g. _**Booking data**_) in the appearing dialog and choose **Next**. |
| 142 | + |
| 143 | +  |
| 144 | + |
| 145 | +4. Assign a transport request and choose **Finish**. |
| 146 | + |
| 147 | +  |
| 148 | + |
| 149 | + The table is created, and the default code displayed in the new editor. |
| 150 | + The default table-specific technical settings are specified using annotations at the top – before the **`DEFINE TABLE`** statement. |
| 151 | + |
| 152 | +  |
| 153 | + |
| 154 | +5. Replace the default source code with the code snippet provided below and replace all occurrences of `####` with your chosen suffix. |
| 155 | + You can make use of the Replace All feature (shortcut **Ctrl+F**) in ADT for the purpose. |
| 156 | + |
| 157 | + <pre> |
| 158 | + @EndUserText.label : 'Booking data' |
| 159 | + @AbapCatalog.enhancementCategory : #NOT_EXTENSIBLE |
| 160 | + @AbapCatalog.tableCategory : #TRANSPARENT |
| 161 | + @AbapCatalog.deliveryClass : #C |
| 162 | + @AbapCatalog.dataMaintenance : #RESTRICTED |
| 163 | + define table zrap_abook_#### { |
| 164 | + key client : mandt not null; |
| 165 | + key booking_uuid : sysuuid_x16 not null; |
| 166 | + travel_uuid : sysuuid_x16 not null; |
| 167 | + booking_id : /dmo/booking_id; |
| 168 | + booking_date : /dmo/booking_date; |
| 169 | + customer_id : /dmo/customer_id; |
| 170 | + carrier_id : /dmo/carrier_id; |
| 171 | + connection_id : /dmo/connection_id; |
| 172 | + flight_date : /dmo/flight_date; |
| 173 | + @Semantics.amount.currencyCode : 'zrap_abook_####.currency_code' |
| 174 | + flight_price : /dmo/flight_price; |
| 175 | + currency_code : /dmo/currency_code; |
| 176 | + created_by : syuname; |
| 177 | + last_changed_by : syuname; |
| 178 | + local_last_changed_at : timestampl; |
| 179 | + } |
| 180 | + </pre> |
| 181 | + |
| 182 | +  |
| 183 | + |
| 184 | + **Short explanations:** |
| 185 | + - Some data elements from the ABAP Flight Reference Scenario (namespace /DMO/) are used. |
| 186 | + - The table key consists of the `CLIENT` field and the `BOOKING_UUID` field which is a technical key (16 Byte UUID). |
| 187 | + - A human-readable travel identifier: `BOOKING_ID` |
| 188 | + - The field CURRENCY_CODE is specified as currency key for the amount field FLIGHT_PRICE using the semantic annotation `@Semantics.amount.currencyCode`. |
| 189 | + - Some standard administrative fields are defined: `CREATED_BY`, `LAST_CHANGED_BY`, and `LOCAL_LAST_CHANGED_AT`. |
| 190 | + |
| 191 | + 6. Save  and activate  the changes. |
| 192 | + |
| 193 | +7. Press **F8** to start the data preview. |
| 194 | + Well, the database table is empty for now, so no data is displayed. |
| 195 | + |
| 196 | + |
| 197 | +  |
| 198 | + |
| 199 | +## Step 4. Fill in the Database Tables with Demo Data |
| 200 | +You will now fill in the created travel and booking database tables with some demo data to ease the test. Demo data provided by the ABAP Flight Reference Scenario (main package: `/DMO/FLIGHT`) will be used for the purpose. |
| 201 | + |
| 202 | +1. Right click on your package **_ZRAP_TRAVEL_####** and choose **_New > ABAP Class_** from the context menu. |
| 203 | + |
| 204 | +  |
| 205 | + |
| 206 | +2. Maintain **`ZCL_GENERATE_DEMO_DATA_####`** as name and a meaningful description (e.g. _**Generate Travel and Booking demo data**_) in the creation wizard for the new ABAP class. |
| 207 | + Add the ABAP interface **`IF_OO_ADT_CLASSRUN`** which needs to be implemented to write outputs to the ABAP Console and continue with **Next**. |
| 208 | + |
| 209 | +  |
| 210 | + |
| 211 | +3. Assign a transport request and choose **Finish**. |
| 212 | + |
| 213 | +  |
| 214 | + |
| 215 | + The ABAP class is created and opened in the source-based class editor area, ready for you to implement. |
| 216 | + |
| 217 | +4. Insert the implementation of method **`if_oo_adt_classrun~main`** with the code snippet provided below (after **CLASS zcl_generate_demo_data_#### IMPLEMENTATION.** and before **ENDCLASS.**) as shown on the screenshot below, and replace all occurrences of `####` with your chosen suffix. |
| 218 | +You can make use of the Replace All feature (**Ctrl+F**) in ADT for the purpose. |
| 219 | + |
| 220 | + <pre> |
| 221 | + METHOD if_oo_adt_classrun~main. |
| 222 | + |
| 223 | + " delete existing entries in the database table |
| 224 | + DELETE FROM zrap_atrav_####. |
| 225 | + DELETE FROM zrap_abook_####. |
| 226 | + |
| 227 | + " insert travel demo data |
| 228 | + INSERT zrap_atrav_#### FROM ( |
| 229 | + SELECT |
| 230 | + FROM /dmo/travel |
| 231 | + FIELDS |
| 232 | + uuid( ) AS travel_uuid , |
| 233 | + travel_id AS travel_id , |
| 234 | + agency_id AS agency_id , |
| 235 | + customer_id AS customer_id , |
| 236 | + begin_date AS begin_date , |
| 237 | + end_date AS end_date , |
| 238 | + booking_fee AS booking_fee , |
| 239 | + total_price AS total_price , |
| 240 | + currency_code AS currency_code , |
| 241 | + description AS description , |
| 242 | + CASE status |
| 243 | + WHEN 'B' THEN 'A' " accepted |
| 244 | + WHEN 'X' THEN 'X' " cancelled |
| 245 | + ELSE 'O' " open |
| 246 | + END AS overall_status , |
| 247 | + createdby AS created_by , |
| 248 | + createdat AS created_at , |
| 249 | + lastchangedby AS last_changed_by , |
| 250 | + lastchangedat AS last_changed_at , |
| 251 | + lastchangedat AS local_last_changed_at |
| 252 | + ORDER BY travel_id UP TO 200 ROWS |
| 253 | + ). |
| 254 | + COMMIT WORK. |
| 255 | + |
| 256 | + " insert booking demo data |
| 257 | + INSERT zrap_abook_#### FROM ( |
| 258 | + SELECT |
| 259 | + FROM /dmo/booking AS booking |
| 260 | + JOIN zrap_atrav_#### AS z |
| 261 | + ON booking~travel_id = z~travel_id |
| 262 | + FIELDS |
| 263 | + uuid( ) AS booking_uuid , |
| 264 | + z~travel_uuid AS travel_uuid , |
| 265 | + booking~booking_id AS booking_id , |
| 266 | + booking~booking_date AS booking_date , |
| 267 | + booking~customer_id AS customer_id , |
| 268 | + booking~carrier_id AS carrier_id , |
| 269 | + booking~connection_id AS connection_id , |
| 270 | + booking~flight_date AS flight_date , |
| 271 | + booking~flight_price AS flight_price , |
| 272 | + booking~currency_code AS currency_code , |
| 273 | + z~created_by AS created_by , |
| 274 | + z~last_changed_by AS last_changed_by , |
| 275 | + z~last_changed_at AS local_last_changed_by |
| 276 | + ). |
| 277 | + COMMIT WORK. |
| 278 | + |
| 279 | + out->write( 'Travel and booking demo data inserted.'). |
| 280 | + ENDMETHOD. |
| 281 | + </pre> |
| 282 | + |
| 283 | +  |
| 284 | + |
| 285 | + **Short explanations:** |
| 286 | + - First, any existing entries in both database tables are deleted. |
| 287 | + - Then the data is selected from the tables `/DMO/TRAVEL` and `/DMO/BOOKING` and inserted into your tables `ZRAP_ATRAV_####` and `ZRAP_ABOOK_####` respectively. |
| 288 | + - The SQL function `UUID( )` is used to set the value of the key fields `TRAVEL_UUID` and `BOOKING_UUID`. |
| 289 | + - The `COMMIT WORK` statement is then executed to persist the data. The data selection has been limited to up to 200 travel records, but you can change this if desired. |
| 290 | + - A success message is written to the Console at the end. |
| 291 | + |
| 292 | +5. Save  and activate  the changes. |
| 293 | + |
| 294 | +6. Press **F9** to run the ABAP class as a console application to generate the demo data and and fill your tables. |
| 295 | + |
| 296 | +  |
| 297 | + |
| 298 | +7. Now you can preview the data from the Travel and Booking database tables. |
| 299 | + Choose the relevant database table (`ZRAP_ATRAV_####` or `ZRAP_ABOOK_####` where #### is your chosen suffix) in the Project Explorer and press **F8**. |
| 300 | + |
| 301 | + The Data Preview will open in the editor area. |
| 302 | + You can play around with the data preview - e.g. choose the **Number of Entries** or **SQL Console**, or filter the entries with **Add filter**. |
| 303 | + |
| 304 | +  |
| 305 | + |
| 306 | +## Summary |
| 307 | +You have completed the exercise! |
| 308 | +In this unit, you have learned how to use the ABAP Development Tools (ADT) to create database tables for your demo Travel app. |
| 309 | + |
| 310 | +## Solution |
| 311 | +Find the source code for the created database tables and class in the **[/week2/sources](/week2/sources)** folder: |
| 312 | +- [W2U2_TABL_ZRAP_ATRAV_####](/week2/sources/W2U2_TABL_ZRAP_ATRAV.txt) |
| 313 | +- [W2U2_TABL_ZRAP_ABOOK_####](/week2/sources/W2U2_TABL_ZRAP_ABOOK.txt) |
| 314 | +- [W2U2_CLAS_ZCL_GENERATE_DEMO_DATA_####](/week2/sources/W2U2_CLAS_ZCL_GENERATE_DEMO_DATA.txt) |
| 315 | + |
| 316 | +Do not forget to replace all the occurrences of `####` with your chosen suffix in the copied source code. |
| 317 | + |
| 318 | +## Next exercise |
| 319 | +[Week 2 Unit 3: Creating the Core Data Services (CDS) Data Model](unit3.md) |
0 commit comments