Unmanaged scenario-CREATE- basic example in RAP

                                     Refer - Introduction to unmanaged scenario in RAP

Steps to implement unmanaged scenario

Step 1: Create a table for Header details. 

@EndUserText.label : 'Student header table'

@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE

@AbapCatalog.tableCategory : #TRANSPARENT

@AbapCatalog.deliveryClass : #A

@AbapCatalog.dataMaintenance : #RESTRICTED

define table ztl_ab_stuh_12 {


key client : abap.clnt not null;

key id : sysuuid_x16 not null;

stu_id : abap.dec(10,0);

stu_name : abap.char(25);

stu_age : abap.numc(2);

stu_course : abap.char(30);

stu_cour_dur : abap.numc(2);

stu_status : abap_boolean;

stu_gender : abap.char(1);

stu_gen_desc : abap.char(10);

stu_dob : abap.dats;

last_ch_time : timestampl;

last_ch_date : timestampl;


}

        * Check and activate.

Step 2: Create Interface view for Header table.

@AbapCatalog.viewEnhancementCategory: [#NONE]

@AccessControl.authorizationCheck: #NOT_REQUIRED

@EndUserText.label: 'Interface entity view for student header'

@Metadata.ignorePropagatedAnnotations: true

@ObjectModel.usageType:{

serviceQuality: #X,

sizeCategory: #S,

dataClass: #MIXED

}

define root view entity ZIV_AB_STUH_12 as select from ztl_ab_stuh_12

{

key id as Id,

stu_id as StuId,

stu_name as StuName,

stu_age as StuAge,

stu_course as StuCourse,

stu_cour_dur as StuCourDur,

stu_status as StuStatus,

stu_gender as StuGender,

stu_gen_desc as StuGenDesc,

stu_dob as StuDob,

last_ch_time as LastChTime,

last_ch_date as LastChDate

}

     * Check and activate.

Step 3: Create Projection/consumption view for Header table.

@AccessControl.authorizationCheck: #NOT_REQUIRED

@EndUserText.label: 'Consumption view for Header'

@Metadata.ignorePropagatedAnnotations: true

@Metadata.allowExtensions: true

define root view entity ZCV_AB_STUH_12

provider contract transactional_query

as projection on ZIV_AB_STUH_12

{

key Id,

@EndUserText.label: 'Student ID'

StuId,

@EndUserText.label: 'Student Name'

StuName,

@EndUserText.label: 'Age'

StuAge,

@EndUserText.label: 'Course'

StuCourse,

@EndUserText.label: 'Course Duration'

StuCourDur,

@EndUserText.label: 'Status'

StuStatus,

@EndUserText.label: 'Gender'

StuGender,

@EndUserText.label: 'Gender Desc'

StuGenDesc,

@EndUserText.label: 'Date of Birth'

StuDob,

LastChTime,

LastChDate

}

Step 4: Create Meta data extension for above consumption view.

@Metadata.layer: #CORE

@UI: {

headerInfo:

{

typeName: 'Student',

typeNamePlural: 'Students',

title: {

type: #STANDARD,

label: 'Student',

value: 'StuId'

}

}

}

annotate entity ZCV_AB_STUH_12 with

{


@UI.facet:

[{

id : 'Student',

purpose: #STANDARD,

type: #IDENTIFICATION_REFERENCE,

label: 'Student',

position: 10

}]

@UI:

{

lineItem: [{ position: 10 , label : 'ID'}],

identification: [{ position: 10 , label : 'ID' }],

selectionField: [{ position: 10 }]

}

Id;

@UI:

{

lineItem: [{ position: 20 , label : 'Student ID'}],

identification: [{ position: 20 , label : 'Student ID' }],

selectionField: [{ position: 20 }]

}

StuId;

@UI:

{

lineItem: [{ position: 30 , label : 'Student Name'}],

identification: [{ position: 30 , label : 'Student Name' }],

selectionField: [{ position: 30 }]

}

StuName;

@UI:

{

lineItem: [{ position: 40 , label : 'Student Age'}],

identification: [{ position: 40 , label : 'Student Age' }]

}

StuAge;

@UI:

{

lineItem: [{ position: 50 , label : 'Student Course'}],

identification: [{ position: 50 , label : 'Student Course' }]

}

StuCourse;

@UI:

{

lineItem: [{ position: 60 , label : 'Course Duration'}],

identification: [{ position: 60 , label : 'Course Duration' }]

}

StuCourDur;

@UI:

{

lineItem: [{ position: 70 , label : 'Status'}],

identification: [{ position: 70 , label : 'Status' }]

}

StuStatus;

@UI:

{

lineItem: [{ position: 80 , label : 'Gender'}],

identification: [{ position: 80 , label : 'Gender' }]

}

StuGender;

@UI:

{

lineItem: [{ position: 90 , label : 'Gender Desc'}],

identification: [{ position: 90 , label : 'Gender Desc' }]

}

StuGenDesc;

@UI:

{

lineItem: [{ position: 100 , label : 'Date of Birth'}],

identification: [{ position: 100 , label : 'Date of Birth' }]

}

StuDob;

}

Step 5: Create behavior definition for Header Interface view.

unmanaged implementation in class zbp_iv_ab_stuh_12 unique;

strict ( 2 );

with draft;

define behavior for ZIV_AB_STUH_12 alias Student

draft table ziv_ab_drafh_12

early numbering

lock master

total etag LastChDate

authorization master ( instance )

etag master LastChDate

{

create ( authorization :global );

update;

delete;

field ( readonly ) Id,StuId,StuGenDesc;


draft action Edit;

draft action Activate;

draft action Discard;

draft action Resume;

draft determine action Prepare;

mapping for ztl_ab_stuh_12

{

Id = id;

StuId = stu_id;

StuName = stu_name;

StuAge = stu_age;

StuCourse = stu_course;

StuCourDur = stu_cour_dur;

StuGender = stu_gender;

StuGenDesc = stu_gen_desc;

StuDob = stu_dob;

StuStatus = stu_status;


}

}

Step 6: Implement the above behavior class and activate.

Step 7: Double (ctrl+double click) click on any method like create or update and add logic.

CLASS lhc_Student DEFINITION INHERITING FROM cl_abap_behavior_handler.

PRIVATE SECTION.


METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION

IMPORTING keys REQUEST requested_authorizations FOR Student RESULT result.


METHODS get_global_authorizations FOR GLOBAL AUTHORIZATION

IMPORTING REQUEST requested_authorizations FOR Student RESULT result.


METHODS create FOR MODIFY

IMPORTING entities FOR CREATE Student.


METHODS update FOR MODIFY

IMPORTING entities FOR UPDATE Student.


METHODS delete FOR MODIFY

IMPORTING keys FOR DELETE Student.


METHODS read FOR READ

IMPORTING keys FOR READ Student RESULT result.


METHODS lock FOR LOCK

IMPORTING keys FOR LOCK Student.

METHODS earlynumbering_create FOR NUMBERING

IMPORTING entities FOR CREATE Student.


ENDCLASS.


CLASS lhc_Student IMPLEMENTATION.


METHOD get_instance_authorizations.

ENDMETHOD.


METHOD get_global_authorizations.

ENDMETHOD.


METHOD create.

zcl_ab_stuh_12=>get_instance( )->create_student(

EXPORTING

entities = entities

CHANGING

failed = failed

mapped = mapped

reported = reported ).

ENDMETHOD.


METHOD update.

ENDMETHOD.


METHOD delete.

ENDMETHOD.


METHOD read.

ENDMETHOD.


METHOD lock.

ENDMETHOD.


METHOD earlynumbering_create.

TRY.

zcl_ab_stuh_12=>get_instance( )->earlynumbering_create(

EXPORTING

entities = entities

CHANGING

failed = failed

mapped = mapped

reported = reported ).

CATCH cx_uuid_error.

"handle exception

ENDTRY.

ENDMETHOD.


ENDCLASS.


CLASS lsc_ZIV_AB_STUH_12 DEFINITION INHERITING FROM cl_abap_behavior_saver.

PROTECTED SECTION.


METHODS finalize REDEFINITION.


METHODS check_before_save REDEFINITION.


METHODS save REDEFINITION.


METHODS cleanup REDEFINITION.


METHODS cleanup_finalize REDEFINITION.


ENDCLASS.


CLASS lsc_ZIV_AB_STUH_12 IMPLEMENTATION.


METHOD finalize.

ENDMETHOD.


METHOD check_before_save.

ENDMETHOD.


METHOD save.

zcl_ab_stuh_12=>get_instance( )->save_data(

CHANGING

reported = reported ).


ENDMETHOD.


METHOD cleanup.

ENDMETHOD.


METHOD cleanup_finalize.

ENDMETHOD.


ENDCLASS.

Step 8: Create global class and add logic- This class is called in above class.

                    * While adding class definition 

                        - go to main class select method definition and click on F2 to see parameters.

CLASS zcl_ab_stuh_12 DEFINITION

PUBLIC

FINAL

CREATE PUBLIC .


PUBLIC SECTION.


TYPES:

tt_create_student TYPE TABLE FOR CREATE ziv_ab_stuh_12,

tt_map_early TYPE RESPONSE FOR MAPPED EARLY ziv_ab_stuh_12,

tt_failed_early TYPE RESPONSE FOR FAILED EARLY ziv_ab_stuh_12,

tt_report_early TYPE RESPONSE FOR REPORTED EARLY ziv_ab_stuh_12,

tt_response_late TYPE RESPONSE FOR REPORTED LATE ziv_ab_stuh_12.

CLASS-METHODS: get_instance

RETURNING VALUE(r_instance)

TYPE REF TO zcl_ab_stuh_12.

CLASS-METHODS: create_student

IMPORTING

entities TYPE tt_create_student

CHANGING

mapped TYPE tt_map_early

failed TYPE tt_failed_early

reported TYPE tt_report_early.

CLASS-METHODS: get_next_id

RETURNING VALUE(rv_id) TYPE ziv_ab_stuh_12-StuId.

METHODS: earlynumbering_create

IMPORTING entities TYPE tt_create_student

CHANGING mapped TYPE tt_map_early

failed TYPE tt_failed_early

reported TYPE tt_report_early

RAISING

cx_uuid_error.

CLASS-METHODS: save_data

CHANGING

reported TYPE tt_response_late.

PROTECTED SECTION.

PRIVATE SECTION.

CLASS-DATA: mo_instance TYPE REF TO zcl_ab_stuh_12,

gt_student TYPE STANDARD TABLE OF ztl_ab_stuh_12 WITH EMPTY KEY,

gs_student TYPE ztl_ab_stuh_12,

gs_mmaped TYPE tt_map_early.

ENDCLASS.



CLASS zcl_ab_stuh_12 IMPLEMENTATION.


METHOD get_instance.

mo_instance = r_instance = COND #( WHEN mo_instance IS BOUND

THEN mo_instance

ELSE NEW #( ) ).

ENDMETHOD.


METHOD get_next_id.

SELECT MAX( stu_id ) FROM ztl_ab_stuh_12 INTO @DATA(lv_id1).

rv_id = lv_id1 + 1.

ENDMETHOD.


METHOD earlynumbering_create.

DATA(ls_mapped) = gs_mmaped.

DATA(lv_id) = cl_uuid_factory=>create_system_uuid( )->create_uuid_x16( ).

DATA(lv_stuid) = get_next_id( ).

READ TABLE gt_student ASSIGNING FIELD-SYMBOL(<gs_student>) INDEX 1.

IF <gs_student> IS ASSIGNED.

<gs_student>-id = lv_stuid.

UNASSIGN <gs_student>.

ENDIF.


mapped-student = VALUE #(

FOR ls_entities IN entities WHERE ( id IS INITIAL )

(

%cid = ls_entities-%cid

%is_draft = ls_entities-%is_draft

Id = lv_id ) ) .

ENDMETHOD.


METHOD create_student.

gt_student = CORRESPONDING #( entities MAPPING FROM ENTITY ).

IF gt_student IS NOT INITIAL.

gt_student[ 1 ]-stu_id = get_next_id( ).


ENDIF.

LOOP AT entities ASSIGNING FIELD-SYMBOL(<ls_entities>).

mapped-student = VALUE #( (

%cid = <ls_entities>-%cid

%key = <ls_entities>-%key

%is_draft = <ls_entities>-%is_draft

) ) .

ENDLOOP.

ENDMETHOD.


METHOD save_data.

IF NOT gt_student[] IS INITIAL.

MODIFY ztl_ab_stuh_12 FROM TABLE @gt_student.

ENDIF.

ENDMETHOD.

ENDCLASS.

Step 9: Create service definition for consumption view.

@EndUserText.label: 'Service definition for Student'

define service ZSD_AB_STUH_12 {

expose ZCV_AB_STUH_12;

}


Step 9: Create service binding for above service definition, check, activate and publish the service.

Testing
        * Preview the application.
        * Click on create button.
        * Give values and click on create.

        * Record is created successfully.
        * Click on Go to see all records.



******************************Thank you*******************************


Comments

Popular posts from this blog

New syntax for append- VALUE (new syntax 7.4+) in ABAP

Read statement new syntax in ABAP. (7.4+).

Concatenation new syntax( 7.4+ ).