Saturday, 30 September 2017

ABAP: Data Elements

In my previous post, I explained about the domains in ABAP Dictionary. I showed you how to create domains (ZRATING, ZDATE_OF_JOIN, ZID, ZEMAIL, ZMYNAME). This is continuation to my previous post, I recommend you to go through my previous post before reading this.

Domain VS Data Elements
Domain holds the technical details like data type, number of characters to store, case sensitivity, output length, value range etc., Data Elements holds the semantics of the filed like meaningful field names, description about the field etc.,

Domain
Corresponding data elements using this domain
ZRATING
ZSTUDENT_RATING, ZFACULTY_RATING
ZDATE_OF_JOIN
ZSTUDENT_DATE_OF_JOIN, ZFACULTY_DATE_OF_JOIN
ZID
ZSTUDENT_ID, ZFACULTY_ID, ZPARENT_ID
ZEMAIL
ZSTUDENT_PRIMARY_EMAIL, ZSTUDENT_SECONDARY_EMAIL, ZFACULTY_PRIMARY_EMAIL, ZFACULTY_WORK_EMAIL, ZPARENT_PRIMARY_EMAIL, ZPARENT_SECONDARY_EMAIL
ZMYNAME
ZSTUDENT_FIRST_NAME, ZSTUDENT_LAST_NAME, ZFACULTY_FIRST_NAME, ZFACULTY_LAST_NAME, ZPARENT_FIRST_NAME, ZPARENT_LAST_NAME

First let us create a data element 'ZPHONE_NUMBER' without using any domain, later I will show you how to create the data elemented mentioned in above table using corresponding domains.

Create ZPHONE_NUMBER data element with simple built in data type CHAR
Open ABAP dictionary by using the transaction SE11.


Select the check box ‘Data type’ and give the name as ZPHONE_NUMBER and click the button Create.
It opens below window. Choose the radio button 'Data Element' and press Ok.
It opens below screen.


As you see above screen, it is similar to the screen that we saw while creating domains.

Update ‘Short Description’ as ‘Phone number of a person’.
Select the check box ‘Built-in type’ and select the Char type and give length as 20 characters.
Go to ‘Field Label’ section, given some meaningful labels to this data element.
Save the changes and activate the data element. That’s it, you are done with the creation of first data element.

Creating data elements using domain
In my previous post, I created below domains.
a.   ZRATING
b.   ZDATE_OF_JOIN
c.   ZID
d.   ZEMAIL
e.   ZMYNAME
        
Now let’s create data elements using above domains.

Create ‘ZSTUDENT_PRIMARY_EMAIL´ data element using ZEMAIL domain
Open ABAP dictionary by using the transaction SE11.

Give the Data type as ‘ZSTUDENT_PRIMARY_EMAIL’ and click Create button.
It opens below window. Select the check box ‘Data element’ and press ok.
It opens below window. Add some ‘Short Description’ and add the Domain as ZEMAIL.
Update field labels.
Press Save and activate the data element. That’s it you are done.

Add documentation to data element
Open the Data element using ABAP dictionary. Click on the button ‘Documentation’.
It open editor like below.

Add some documentation and save.

Same way, you can create following data elements from the domains
Domain
Corresponding data elements using this domain
ZRATING
ZSTUDENT_RATING, ZFACULTY_RATING
ZDATE_OF_JOIN
ZSTUDENT_DATE_OF_JOIN, ZFACULTY_DATE_OF_JOIN
ZID
ZSTUDENT_ID, ZFACULTY_ID, ZPARENT_ID
ZEMAIL
ZSTUDENT_PRIMARY_EMAIL, ZSTUDENT_SECONDARY_EMAIL, ZFACULTY_PRIMARY_EMAIL, ZFACULTY_WORK_EMAIL, ZPARENT_PRIMARY_EMAIL, ZPARENT_SECONDARY_EMAIL
ZMYNAME
ZSTUDENT_FIRST_NAME, ZSTUDENT_LAST_NAME, ZFACULTY_FIRST_NAME, ZFACULTY_LAST_NAME, ZPARENT_FIRST_NAME, ZPARENT_LAST_NAME


Previous                                                 Next                                                 Home

ABAP Dictionary: Domains

Domain is a data type with specific value range. A domain is assigned to a data element. If a table/structure use this data element, then the value must be in the range defined by the domain.

For example,
the 'status' of an order can be in [open, in_process, closed] states.
Email id length must be < 50 characters. etc.,

Let me explain with an example.

To understand the domain concept, let me try to explain with an example.

Suppose we are trying to build a school management application.
a.   Every student must be identified by unique id
b.   Student parent details can be retrieved by his id
c.   School admin can able to send mails to student, parents and school staff
d.   School admin can able to call parents and school staff
e.   By using faculty id, we can able to get all the students that he teaches at given period.
f.    Every faculty has a rating [average, good, excellent] given by student
g.   Every student has a rating [average, good, excellent] given by faculty.

From the above requirements, I designed below tables.

Student
student_id
first_name
last_name
primary_email
secondary_email
phone_number
date_of_join








Parent
parent_id
first_name
last_name
primary_email
secondary_email
phone_number







Teaching_Faculty
faculty_id
first_name
last_name
primary_email
work_email
phone_number
date_of_join
Salary









Non_Teaching_faculty
non_teaching_faculty_id
first_name
last_name
primary_email
work_email
phone_number
date_of_join
Salary









faculty_student
faculty_id
student_id
Year




        
student_parent
student_id
parent_id


        
faculty_rating
faculty_id
student_id
faculty_rating




student_rating
student_id
faculty_id
student_rating




        
Now, let’s see the problem with above model.

Suppose, at the time of initial design we decided to give 8 characters to the ids like student_id, faculty_id, parent_id and non_teaching_faculty_id.

Later point of time, we decided to give 15 characters to the id filed (student_id, faculty_id, parent_id and non_teaching_faculty_id). 

With our current design, we need to update all the tables and at 12 places.

Field
Number of Occurrences
Tables get affected
student_id
5
Student, faculty_student, student_parent, faculty_rating, student_rating
faculty_id
4
Teaching_Faculty, faculty_student, faculty_rating, student_rating
parent_id
2
Parent, student_parent
non_teaching_faculty_id
1
Non_Teaching_faculty


If we follow ABAP, domain model, the updation is only at only one place. First we create a domain for the ids like z_id.

As you see above image, I created a domain ‘z_id’ of length 8 characters to represent ids.

In later point of time, if you want to change the length of the id to 15 characters, you need to change the length at only domain level.
Same is applicable to emails ids, phone numbers, rating in this example.

How to create a domain?
Let’s try to create domain for the rating field.
In the command field type the transaction ‘SE11’. It opens below window.
 Select the radio button ‘Domain’ and give the name as ZRATING.
It opens below window.
Update the Short Description, since it is mandatory while creating domain. Update the Short Description as ‘Rating given to person’.
Now concentrate on Definition tab, we need to select the data type for this domain.

Click on the Data Type text box, click the right side button, it opens list of built in data types available. Select the Char data type and press Ok.
Give ‘No. Characters’ as 1. Since we are going to use A, G, E (A – Average,
G – Good, E – Excellent) to describe about the rating.
If you select the check box ‘Case-sensitive’, the letters upper case and lower case will be distinguished, else both upper case and lower case will be treated as same. I am not going to select the ‘Case-sensitive’ check box.

Now select the tab ‘Value Range’. In the ‘Single Vals’ section, update possible values (A, G, E) and their short descriptions (Average, Good, Excellent) for the domain ZRATING.
Click on Save button. Once you click on save button, it opens below window.

If you want to store this domain in some package, give the package name and click save button, else click on the button ‘Local Object’.

Once the domain is saved, you need to activate this domain. That’s it you are done.


Creating domain for the field ‘date_of_join’.
Give the domain name as ‘ZDATE_OF_JOIN’ and click on ‘Create’.
Select the data type ‘DATS’.
You may ask me, why the 'No. Characters' fields has length of 8 and 'Output length' is 10. It is because, the date is stored like 20170621 (8 characters are enough to store) in the database, but while displaying, it is displayed like 2017-06-21 (or) 2017/06/21 (or) 2017.06.21 (Require 10 characters to display) etc.,

Press Save and activate the domain.


Create domain for Id
Create domain ZID, with 8 characters length. I am not mentioning complete steps, since it is same like above.

Create domain for email
Create domain ZEMAIL, with 40 characters length. I am not mentioning complete steps, since it is same like above.

Create domain to represent first_name, last_name
Create domain ZMYNAME, with 20 characters length. I am not mentioning complete steps, since it is same like above.


Finally, we created below domains.

a.   ZRATING
b.    ZDATE_OF_JOIN
c.   ZID
d.   ZEMAIL
e.    ZMYNAME

In the next post, I am going to explain the creation of data elements using above domains.


Previous                                                 Next                                                 Home

Elements of ABAP Dictionary

Following are the basic building blocks of ABAP Dictionary.

a.   Built-in Types
b.   Domains
c.   Data Elements
d.   Structures
e.   Table Types
f.    Database Tables
g.   Views



Previous                                                 Next                                                 Home

ABAP Dictionary

ABAP Dictionary is a tool used to manage your database in ABAP. The transaction 'SE11' is used to open ABAP Dictionary.

SQL is mainly categorized into two parts.
         a. DDL: Data Definition Language.
         b. DML: Data Manipulation Language
        
DML is used to query the database tables, insert, update and delete records from table (SELECT, UPDATE, DELETE, INSERT etc.,).

DDL is used work on schema level like creating table, altering table, deleting entire table, creating indexes, creating views (CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX etc.,).

By using ABAP dictionary, we can manage DDL part of the SQL. ABAP dictionary provides nice graphical user interface to manage our databases.

How to open ABAP Dictionary?
By using the transaction ‘SE11’ or by navigating the folder tree ‘Tools -> ABAP Workbench -> Development -> ABAP Dictionary’.

Once you click on ‘ABAP Dictionary’, it opens below window.



Previous                                                 Next                                                 Home

How to get the interfaces implemented by a class?

'java.lang.Class' provides 'getGenericInterfaces' and 'getInterfaces' methods to determine the interfaces implemented by given class.

getGenericInterfaces vs getInterfaces
getGenericInterfaces return the Types representing the interfaces directly implemented by the class or interface represented by this object, whereas getInterfaces return the interfaces implemented by the class or interface represented by this object.

Test.java
package com.sample.test;
import java.lang.reflect.Type;
import java.util.HashSet;

public class Test {
 public static void main(String args[]) {
  Class clazz = HashSet.class;

  System.out.println("Generic interfaces implemented by HashSet are : ");
  Type[] types = clazz.getGenericInterfaces();
  for (Type type : types) {
   System.out.println(type.getTypeName());
  }

  System.out.println("\n\nInterfaces implemented by HashSet are : ");
  types = clazz.getInterfaces();

  for (Type type : types) {
   System.out.println(type.getTypeName());
  }

 }
}


Output
Generic interfaces implemented by HashSet are : 
java.util.Set<E>
java.lang.Cloneable
java.io.Serializable


Interfaces implemented by HashSet are : 
java.util.Set
java.lang.Cloneable
java.io.Serializable


The order of the returned interfaces from methods getInterfaces, getGenericInterfaces is the order of the interface names in the implements clause of class definition.


TempSet.java
package com.sample.test;

public interface TempSet<T> {

}

IntegerSet.java
package com.sample.test;

import java.io.Serializable;

public class IntegerSet implements TempSet<Integer>, Cloneable, Serializable {

 private static final long serialVersionUID = 1L;

}


Test.java
package com.sample.test;

import java.lang.reflect.Type;

public class Test {
 public static void main(String args[]) {
  Class clazz = IntegerSet.class;

  System.out.println("Generic interfaces implemented by HashSet are : ");
  Type[] types = clazz.getGenericInterfaces();
  for (Type type : types) {
   System.out.println(type.getTypeName());
  }

  System.out.println("\n\nInterfaces implemented by HashSet are : ");
  types = clazz.getInterfaces();

  for (Type type : types) {
   System.out.println(type.getTypeName());
  }

 }
}


Output
Generic interfaces implemented by HashSet are : 
com.sample.test.TempSet<java.lang.Integer>
java.lang.Cloneable
java.io.Serializable


Interfaces implemented by HashSet are : 
com.sample.test.TempSet
java.lang.Cloneable
java.io.Serializable

As you notify the output, the interfaces returned are TempSet, Cloneable and Serializable, these are in the order they specified in IntegerSet implements clause.

Array types implements Cloneable and Serializable interfaces. You can check the same by calling getInterfaces and getGenericInterfaces methods.


Test.java
package com.sample.test;

import java.lang.reflect.Type;

public class Test {
 public static void main(String args[]) {
  int a[] = new int[10];
  Class clazz = a.getClass();

  System.out.println("Generic interfaces implemented by array are : ");
  Type[] types = clazz.getGenericInterfaces();
  for (Type type : types) {
   System.out.println(type.getTypeName());
  }

  System.out.println("\nInterfaces implemented by array are : ");
  types = clazz.getInterfaces();

  for (Type type : types) {
   System.out.println(type.getTypeName());
  }

 }
}

Output
Generic interfaces implemented by array are : 
java.lang.Cloneable
java.io.Serializable

Interfaces implemented by array are : 
java.lang.Cloneable
java.io.Serializable