0% found this document useful (0 votes)
74 views40 pages

1.03 Data Modeling - SAP Commerce Cloud Developer Training

Uploaded by

Anis Boulila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views40 pages

1.03 Data Modeling - SAP Commerce Cloud Developer Training

Uploaded by

Anis Boulila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Data Modeling

SAP Commerce Cloud Developer Training


Part I
What we will cover in this topic

Methodology Ecommerce Features

Accelerator Data Product Coupons and Order Search and


Pricing Payment Personalization
Extensions Modeling Modeling Promotions Management Navigation

Development General Services Maintenance

Web Layer Service Layer Background Task


Flexible Building
ImpEx
Search Framework
WCMS Façade Cronjobs

Installation
Validation Security
Process Configuration
AddOn Service
Engine
Event Update and
Cache
System Initialization
Backoffice OCC Workflows

Scripting

2
The Context

SAP Commerce follows a Model-Driven Methodology. We


thus first define the types in XML, and the corresponding Java
types are created automatically during the next build.

3
Run
Preparation
Script

Introduction to the Type System for


Exercise 2

Introduction to the Type System


Collections and Relations
Deployment
Type System Localization
Who is responsible for converting SAP
Commerce type definitions to Java classes?
SAP Commerce and Java
The SAP Commerce build system (ant)

Class
Java is an is a
instance blueprint
of Object of

Type

SAP Commerce is an is a
instance blueprint
of Item of

5
Java Classes vs SAP Commerce Types

Java Type System


Product metatype
is represented as
ProductModel.java code : String
name : localized:String
ComposedType
subclass of subtype of

Car
is represented as fuel : Enumeration
CarModel.java
kwatt : Double metatype

by runtime, is represented
as an instance of

code : HistoricCar
name : Some Old Car
fuel : Gasoline
kwatt : 30

6
Types used in SAP Commerce

AtomicType
▪ Represents Java value objects which are mapped to database types
− Java Primitive keywords: int
− Java Wrapper Classes: java.lang.Integer, java.math.BigInteger
− Some Reference types: java.util.Date, java.lang.String, de.hybris.platform.core.PK

CollectionType
▪ Represents a typed collection

EnumerationType
▪ ComposedType which describes enumerations

7
Types used in SAP Commerce

MapType
▪ Represents a typed Map

RelationType
▪ Used to model binary dependencies between items, representing n:m relations.

ItemType (aka ComposedType)


▪ Record attribute and relation meta data for each type, including unique identifier, db table, and
supporting Java class. The foundation of the Commerce Suite's type system.

8
The sections within extensionName-items.xml (in order)
<items>

<atomictypes> ... </atomictypes>

<collectiontypes> ... </collectiontypes>

<enumtypes> ... </enumtypes>

<maptypes> ... </maptypes>

<relations> ... </relations>

<!-- Composed Types -->


<itemtypes> ... </itemtypes>

</items>

9
Extending the Data Model
▪ Create new types:
− Define a type by extending already existing types, such as:
<itemtype code="Car" extends="Product">

− Define “completely new types”, such as:


<itemtype code="Car"> (implicitly extends from GenericItem)

▪ Extend existing types:


− Add attribute definitions to existing types (attribute injection), such as:
<itemtype code="Product" ...>
...
<attributes>
...
<attribute qualifier="myAttribute">
...
</attributes>
<itemtype>

− Redefine inherited attribute definitions from super type


<attribute qualifier="code" redeclare="true">
• If you change the attribute's java type, the new type must extend the original type

10
Extending the Data Model • Extend Existing Type
Product
Product code vehicle01
+ code: String version Catalog
name F40 + name: String
1 Version
+ brand: String
version Online

brand Ferrari

Car

autocreate
▪ Add attribute definitions to existing types (attribute injection) If set to true, indicates this is the first
mention of this ItemType, which should be
<items> created during initialization. As Product is
<itemtypes> already defined in Commerce, set to false.

<itemtype code="Product" autocreate="false" generate="false">


generate
<attributes>
If set to true, indicates this is the first
<attribute qualifier="Brand" type="java.lang.String” /> mention of this ItemType, for which a
</attributes> model must be created. As Product is
... already defined in Commerce, set to false.

11
Extending the Data Model • New Type
Product
Car code vehicle01 version
+ code: String Catalog
name F40 + name: String
+ brand: String 1 Version
version Online

brand Ferrari
hp 300
Car
kw 212
+ hp: int
+ kw: int

<items>
<itemtypes>
<itemtype code="Car" extends="Product" autocreate="true" generate="true">
<attributes>
<attribute qualifier="hp" type="java.lang.Integer">
<description>Horsepower</description>
<persistence type="property"/>
</attribute> What is the difference
<attribute qualifier="kw" type="java.lang.Integer">
<description>Kilowatt</description>
between property and
<persistence type="dynamic" dynamic?
attributeHandler="kwPowerAttributeHandler"/> In what conditions will
<modifiers write="false" />
</attribute>
dynamic be used?
...
Extending the Data Model • Enumerated Types
Product
Car code vehicle01
+ code: String version Catalog
name F40 + name: String
+ brand: String 1 Version
version Online

brand Ferrari
hp 300
Car
kw 212
+ hp: int
fuel gasoline + kw: int
+ fuel: enumeration
<items>
<enumtypes>
<enumtype code="FuelEnumeration" generate="true" autocreate="true" { gasoline | diesel | ethanol }
dynamic="true">
<value code="diesel"></value>
<value code="gasoline"></value>
<value code=”ethanol"></value>
</enumtype>
</enumtypes>

<itemtypes>
<itemtype code="Car" extends="Product" autocreate="true" generate="true">
<attributes>
...
<attribute qualifier="fuel" type="FuelEnumeration">
<persistence type="property"></persistence>
</attribute>
Extending the Data Model • Composed Type References
Product
Car code vehicle01
+ code: String version Catalog
name F40 + name: String
+ brand: String 1 Version
version Online

brand Ferrari
hp 300
Car mechanic
kw 212
+ hp: int 0..1
fuel gasoline
+ kw: int
User
mechanic Montgomery Scott + fuel: enumeration

<items>
<itemtypes>
<itemtype code="Car" extends="Product" autocreate="true" generate="true">
<attributes>
...
<attribute qualifier="mechanic" type="Employee">
<modifiers read="true" write="true" search="true" />
<persistence type="property" />
</attribute>
...
Extending the Data Model • Relations
Product
Car code vehicle01 User code Alain Prost
+ code: String version Catalog
name F40 team Ferrari + name: String
+ brand: String 1 Version
version Online ... ...

brand Ferrari car vehicle01


hp 300
Car mechanic
kw 212
+ hp: int 0..1
fuel gasoline
code Mario Andretti + kw: int
User
User car drivers
mechanic Montgomery Scott + fuel: enumeration
team Ferrari
1 *
drivers Alain Prost,
... ...
Mario Andretti
car vehicle01

<items>
...
<relations>
<relation code="Car2EmployeeRelation"
localized="false" autocreate="true" generate="true">
<sourceElement qualifier="car" type="Car" cardinality="one" />
<targetElement qualifier="drivers" type="Employee" cardinality="many" /> This example is 1:many, but
</relation> many:many relations are also
</relations> supported
<itemtypes>
...
2.16 (Data Mapping)

Commerce Type System • Automatic Generation

1. SAP Commerce item definitions are


found in each extension's
Resources
extensionName-items.xml DTOs

2. The ant process assembles type


definitions and generates Models, ant build
process
DTOs, and Resources.
service layer
models

3. Invoking initialize or update items


initialize
creates/modifies the
required table.

update

ext-items.xml
DB tables

16
Collections and Relations

Introduction to the Type System


Collections and Relations
Deployment
Type System Localization
Collection types

▪ Collection of target type


▪ Can be used as an attribute type of a ComposedType
▪ Allows you also to define AtomicType collections
▪ Performance considerations: Accessing, Searching
▪ Database integrity considerations

<collectiontype code="StringCollection"
elementtype="java.lang.String" autocreate="true" />
<collectiontype code="LanguageCollection"
elementtype="Language" autocreate="true"/>

...

<itemtype code="..." ...


<attribute qualifier="urlPatterns" type="StringCollection">
<attribute qualifier="writeableLanguages" type="LanguageCollection">
...

18
Relations

▪ One2Many and Many2Many


▪ Both sides are (can be) aware of the other

Category Product
products points to points to supercategories
(List<Product>) (List<Category>)

CategoryProductRelation
products is added supercategories is added
to the Category type
source (Category) to the Product type
as an attribute target (Product) as an attribute

Role name at source:


“products”
Role name at target:
“supercategories”
19
What’s so Important About Relations?

If in doubt: Use relations, not CollectionTypes, because:


− Opposite side is not “aware” of the CollectionType
− CollectionTypes are stored in a database field as a comma-separated list of references (PKs) or atomic
values
− Can cause overflow
− More difficult to search and generally lower performance

20
Deployment

Introduction to the Type System


Collections and Relations
Deployment
Type System Localization
Questions

What is the main incompatibility between object-oriented programming and relational databases?

Representing inheritance relationships

How can we represent inheritance in a relational database

Usually, using one of two strategies:


1. Subtype shares the supertype's table
2. Use different tables for supertype and subtype

What are the advantages and drawbacks of these two strategies?

 Single-table strategy: better query performance but low storage efficiency


 Multiple-table strategy: worse query performance but high storage efficiency

22
Object Relational Mapping • Storing objects in the DB

▪ By default, items for a given type are stored in the same database tables as its supertype
▪ Specify any item type's deployment to store its items in its own db tables.
▪ SAP Commerce recommends that deployment be specified for the first layer of GenericItem subtypes
– Consider carefully the performance implications of specifying deployment for other item types
– Set build.development.mode = true in local.properties to mandate that all direct children of GenericItem
have deployment specified.

GenericItem genericitems

products
MyType Product
mytypes
Car
23
O-R Mapping • Deployment Example

GenericItem genericitems

products
MyType Product
mytypes
cars
Car

<itemtype code="Car" ...


<deployment table="cars" typecode="20100" ...

0 .. 10000 are reserved by SAP Commerce


10000 .. 32767 are free for use, with some exceptions
24
O-R Mapping • Table Structure
GenericItem

PK P1 P2 P3 C1 C2 C3 products

Product
… … … … … …

Products
Car

GenericItem
PK P1 P2 P3
… … … …

products products
Product

PK P1 P2 P3 C1 C2 C3 cars
… … … … … … …
Car
cars
25
O-R Mapping • Attributes of a (Composed) Type

PK code name … mechanic


8796256894977 vehicle01 Ferrari F40 … 8796128083972

products

Atomic type property – value


stored directly in db. For
instance, property of type
String is stored as VARCHAR Composed type property –
reference to another item. The
DB column stores a PrimaryKey
(PK) value.

code vehicle01

name Ferrari F40 PK name …


mechanic Nikola Tesla 8796128083972 Nikola Tesla …

users
26
O-R Mapping • Deployment of Relations • 1

▪ One-to-Many
− Additional column at the many side which holds the PK of the One side
− Users table from the example below would have an additional column car
▫ As with Car, Employee type does not have its own deployment, so its items live in the parent type's table Users

<relation code="Car2DriversRelation" generate="true" autocreate="true"


localized="false" >
<sourceElement qualifier="car" type="Car" cardinality="one" />
<targetElement qualifier="drivers" type="Employee" cardinality="many"/>
</relation>

PK name car …
8796128083972 André-Marie Ampère 5553620010023 …
PK code …
5443669878887 C.-A. de Coulomb 5553620010023
8796256894977 vehicle01 …
USERS table
5553620010023 rocket05 … (holds User and Employee items)

PRODUCTS table
(holds Product and Car items) 27
O-R Mapping • Deployment of Relations • 2
A deployment must be specified
▪ Many-to-Many for many-to-many relationships

− New database table which holds the source and target PKs
<relation code="Product2ReviewerRelation" autocreate="true" generate="true"
localized="false">
<deployment table="Prod2ReviewerRel" typecode="20123"/>
<sourceElement qualifier="reviewers" type="Employee" cardinality="many" >
<modifiers read="true" write="true" search="true" optional="true" />
</sourceElement>
<targetElement qualifier="products" type="Product" cardinality="many" >
<modifiers read="true" write="true" search="true" optional="true" />
</targetElement>
</relation>

Relation "reviewers" source target Relation "products"


PK uid … 3776876789221 8796256894977 PK code …
3776876789221 ajfoyt … 3776876789221 5553620010023 8796256894977 vehicle01 …

6152677365115 mandretti … 6152677365115 5553620010023 5553620010023 rocket05 …

USERS table Prod2ReviewerRel PRODUCTS table


(holds Product and Car items) 28
(holds User and Employee items)
O-R Mapping • Deployment of Collections

▪ Collections
− Stored in one database column
− Comma separated list of PKs or Atomic Values

<collectiontype code="StringCollection"
elementtype="java.lang.String" autocreate="true" />
<collectiontype code="LanguageCollection"
elementtype="Language" autocreate="true" />

...

<itemtype code="..." ...


<attribute qualifier="urlPatterns" type="StringCollection" />
<attribute qualifier="writeableLanguages" type="LanguageCollection" />
...

PK code urlpatterns writeableLanguages …


8796256894977 Example1 http://ex1.com/a,http://ex2.com/b,http://ex3.com/c 93938293,93029304,01920394 …

products
29
Type System Localization

Introduction to the Type System


Collections and Relations
Deployment
Type System Localization
Two Shades of Localization

▪ Service layer provides support for i18n So, who needs what?

▪ Leverage that to localize:


− Types names and their attribute names Backoffice employees in different countries

− Your data values (Itemtype properties) Front-end customers around the globe

31
Type System Localization • Type-Name and Attribute-Name Display Labels

▪ Language-specific labels are used for displaying type-names and attribute-names


– Used in Backoffice, based on session language setting
– Specified in files named extension-locales_XY.properties, where:
− extension is the name of the extension
− XY is the ISO code of the language / locale
− Properties convention (i.e., entries within this .properties file):
type.{typename}.name=value
type.{typename}.description=value
type.{typename}.{attributename}.name=value
type.{typename}.{attributename}.description=value
type.{enumcode}.{valuecode}.name=value
For Backoffice employees
in different countries

32
Localize Types during System Initialization or Update

▪ To read Type-definition localizations from .properties files and store them in the database
metadata tables:

▪ Overrides type localizations in the database with the ones from the locales_XY.properties files

33
Localizing Attribute Values For front-end customers
around the globe
▪ Any itemtype property may be localized 1. Define in *-items.xml
2. Populate in Backoffice or
<itemtype code="Product"> ImpEx
<attribute qualifier="barcode" type="java.lang.String" /> 3. Displayed in storefront
<attribute qualifier="quote" type="localized:java.lang.String" /> based on customer's locale

<attribute qualifier="mugshot" type="localized:Image" />


...

▪ Backoffice apps and ImpEx will allow input in multiple languages

34
Enabling Localized Data Types

▪ The localized: prefix is not a keyword; localized types must be defined in *-items.xml
– A <maptype> entry exists for each OOTB localized type, defined in the core extension's core-items.xml
▪ For example
<itemtypes>
<itemtype code="Car" extends="Product">
<attribute qualifier="ownerManual" type="localized:Booklet" />
... If we wanted to define
</itemtype>
a localized user's
<itemtype code="Booklet" extends="Product">
... manual for our car…
</itemtype>
...
<itemtypes>

<maptypes>
<maptype code="localized:Booklet" argumenttype="Language" We must also define the
returntype="Booklet" autocreate="true" generate="false"> localized:Booklet
...
maptype
<maptypes>
35
Demo
References

Type System Documentation:

▪ https://help.hybris.com/latest/hcd/8c755da8866910149c27ec908fc577ef.html

Type System Definition Items.xml:


▪ https://help.hybris.com/latest/hcd/8bffa9cc86691014bb70ac2d012708bc.html

Specifying a Deployment for SAP Commerce Cloud Platform Types:


▪ https://help.hybris.com/latest/hcd/8c6254f086691014b095a08a61d1efed.html

Data Model Design Resources and Performance Implications


▪ https://www.sap.com/cxworks/article/433893244/Data_Model_Design_with_the_SAP_Commerce_Cloud_T
ype_System

Data Modeling Guidelines

▪ https://help.hybris.com/latest/hcd/8ecae959b9bd46b8b426fa8dbde5cac4.html
37
The SAP Commerce type system is used to model system in an abstract way.
All XML type definitions are converted during the system build into corresponding java classes, which will be
used at runtime.

Item types convert to models – the foundation/entities of the Commerce Suite's type system.
Each model is comprised of attribute and relation metadata, an ID, a DB table and a supporting Java class.
Always update/initialize SAP Commerce to apply any type-related changes to the database.
If possible, try to use relation types rather than collections

The deployment tag deploys the current type to its own table instead of using the table of its super type.
▪ Understand the performance impact
Localization in SAP Commerce is two-fold:
▪ Types and attributes description localization – useful for Backoffice users
▪ Localized attributes – seen by storefront customers based on their locale
38
Exercise 2

39
Thank you.

You might also like