Showing posts with label Hybris. Show all posts
Showing posts with label Hybris. Show all posts

Saturday, 28 May 2016

Apply/Add restrictions on a component

In real time scenarios sometimes we have requirements to restrict some sort of information to few customers only. So for such kind of requirements we have concept of restrictions. We have multiple type of restrictions in Hybris. To see type of restrictions login to hmc tool http://localhost:9001/hmc/hybris

Go to WCMS -> Restrictions type. See screen shot below


Now, how we can add restriction to a component. To explain restrictions lets take user restriction as of now. Follow below steps.

1.) Go to WCMS-> Components and search for sitelogocomponent
2.) Double click that component and go to restrictions section of that. Right click on restriction and create new restriction as shown below.


















3.) Below screen will be displayed. Enter the details like name, id ,catalog version, name of the component this restriction applied to and the user id of the user as this restriction type is user restriction. 
Screen shot below for reference.





















4.) Save and close
5.) Open electronics site locally and we could see the restriction is applied on the SiteLogo component.As the site logo is restricted to the user, we can't see the site logo in general. See screen shot below.
















6.) Now lets login with customer 'kuldeep.gju@gmail.com' which we added as user in restriction and observed that the logo is displayed. See screen shot below.














7.) Now go back to component sitelogocomponent and remove that restriction and save. Now you can again see the logo even though user is not logged in.

This is how we can add restrictions on components and we can also define which page we can choose for those components

Thursday, 12 May 2016

Understanding Hybris Data Models

Data Models are defined in the <extension>-items.xml file of each extension. Essentially data entities (called "items" in hybris) are defined with itemtype elements, whereas relations between items are defined with relation elements.

Below is the sample <extension>-items.xml.

            xsi:noNamespaceSchemaLocation="items.xsd">
    
    <itemtypes>
        <itemtype code="Stadium" generate="true" autocreate="true">
            <deployment table="CuppyTrailStadium" typecode="10123" />
            <attributes>
                <attribute qualifier="code" type="java.lang.String" >
                    <persistence type="property"/>
                    <modifiers optional="false" unique="true"/>
                </attribute>
                <attribute qualifier="capacity" type="java.lang.Integer">
                    <description>Capacity</description>
                    <persistence type="property" />
                </attribute>
            </attributes>
        </itemtype>
    </itemtypes>
</items>


itemtype code=Stadium generate=true autocreate=true
  • defines a new type Stadium which implicitly extends from GenericItem
  • autocreate=true indicates that Stadium is a new type
  • generate=true creates required sourcecode (not the model class) for the type

deployment table=CuppyTrailStadium typecode=10123 
  • specifies how the new type is mapped to the database
  • table defines the database table
  • typecode is used internally e.g. to create item pks. Typecodes between 0 and 10000 are reserved by hybris
attribute qualifier=code type=java.lang.String
  • creates a new attribute code by using the atomic type java.lang.String
persistence type=property
  • Defines how item are stored. property reflects the normal persistent behaviour

modifiers ...
  • advanced settings like read and write access

Relations

The below sample code shows the relation we define in xml


<relations>
    <relation code="StadiumMatchRelation" localized="false" generate="true" autocreate="true">
       <sourceElement type="Stadium" qualifier="stadium" cardinality="one" />
       <targetElement type="Match" qualifier="matches" cardinality="many"/>
    </relation>
</relations> 

  • Define a new relationship StadiumMatchRelation between Stadium and Match using the relation tag
  • Add the above element immediately  before the itemtypes element
  •  The relation is a one-to-many relation. which means that the mapping will be done by an additional column on the many side, i.e. the table Match

  Enumtypes

  The following element before the relations element in any <extension>-items.xml

<enumtypes>
        <enumtype code="StadiumType" autocreate="true" generate="true" dynamic="false">
            <value code="openair"/>
            <value code="enclosed"/>
        </enumtype>
        <enumtype code="StadiumAccess" autocreate="true" generate="true" dynamic="true">
            <value code="road"/>
            <value code="rail"/>
            <value code="plane"/>
        </enumtype>
</enumtypes>


We can also define the default values for an attribute


<attribute type="StadiumType" qualifier="StadiumType">

    <persistence type="property"/>
    <defaultvalue>em().getEnumerationValue("StadiumType","openair")</defaultvalue>
</attribute>

In Simple words if we related this with JAVA, then we can say that Type in Hybris is same as class in Java and Item in Hybris is same as Object in Java


Understanding Accelerator extension structure

Modulegen copies codes from Accelerator extension template to your custom extension folder. It is important to understand the significance of the accelerator structure as it will help in finding out the place where you need to make change for your personal customization,dependency between different extension and platform .It will further help in writing cleaner and modularized code which will assist in extending hybris platform services and easier hybris version update process in future.

Once you have completed the steps mentioned in Create/Publish store you will find out that modulegen has added 7 new extensions folder to C:\hybristest\hybris\bin\custom\training folder.

We will explore the structure and importance of new custom extensions :


1.)trainingcockpits  
This extension is where you will programmatically extend hybris cockpit business tools to customize existing features of a cockpit or add additional features. It extends the acceleratorcoreextension.All the  XML file based configuration that is added to any of the extension will be added to  acceleratorcore extension
2.)trainingcore
It is the business service layer extensions where the data model is extended, services from other hybris extensions are combined, added to and enriched and further services are added for project use. This extension come with a core and hybris Management Console (hMC) module. All core data vital for the provisioning of an accelerator storefront is loaded in the trainingcore extension as a part of essential and project data. You can alter these scripts and add further data scripts here.
3.)trainingfacades
This extensions organize and aggregate multiple business services to provide a storefront API that exposes actions and a data model that is optimized for a B2C commerce front-end use. It extends the functionality offered by the commercefacades extension and would be where a you would add all additional facades and customize the commercefacades extension functionality. It has a core module but no hybris Management Console (hMC) and web module.
4.)trainingfulfilmentprocess
Here you can extend and customize the fulfillment process that comes OOB with hybris. It contains order process and consignment process XMLs that define the order fulfillment and management process in Hybris
5.)traininginitialdata
It is used to add data for running and setting up  your storefront and application. It provides initial impex templates. From here acceleratorsampledata extension is coupled to this particular implementation. The acceleratorsampledata extension adds all the products and content necessary for each of the three sample storefronts as well as test promotions, users, advanced personalization rules and etc.. A project would not go live with acceleratorsampledata extension, it is provided purely as a sample. The data is separated into this extension to make it very easy to not include the sample data as a part of a project.
6.)trainingstorefront
This extensions contain the code for the front-end tiers that expose storefront functionality using web site. And do not have a core or the hybris Management Console (hMC) module.
7.trainingtest
Here you can put your JUnit testcases to test the functionality provided by your implementation. It Provides  tools, configuration and data for testing hybris Multichannel Accelerator.


Create/Publish new store(website) using hybris accelerator extension template

In Hybris yaccelerator is a default out-of-box store but we want to create our own  by using Hybris multi-channel suite.Follow the below steps for the same.

Step 1: Go to cmd and hybris bin plateform directory and run >ant modulegen











Step 2: Choose extension template as "accelerator" , name as "training" and package name as "com.training"














After performing all above step , Below seven extension gets

(1)  trainingcockpits
(2)  trainingcore
(3)  trainingfacades
(4)  trainingfulfilmentprocess
(5)  traininginitialdata
(6)  trainingstorefront
(7)  trainingtest








































Step 3: Now add all the 7 newly created extensions in localextensions.xml

Step 4: Run ant clean all

Step 5: Start hybris server using hybrisserver.bat

Step 6: Initialize extension from Hybris admin console


In case any CMSSite related issues is faced. Please initialise apparel/electronics(which ever you going to use) first from admin console.

Wednesday, 11 May 2016

Initialization and Update of the hybris Commerce Suite

There are two representations of the Type System of the hybris Commerce Suite.
  •    There is a file-based representation that is spread across in the various items.xml files of the hybris Commerce Suite extensions. This representation is not actively used by the hybris Commerce Suite at runtime. You can modify this representation at any time by modifying the items.xml file of an extension. However, modifications of the items.xml files only take effect after an initialization or an update of the hybris Commerce Suite.
  •    The database of the hybris Commerce Suite also contains a representation of the type system. This representation is actually in use by the hybris Commerce Suite at runtime. The representation in use reflects the state the type system was in when the hybris Commerce Suite was last updated or initialized






Locking the hybris Suite

Hybris Administration Console also allows you to lock the system against initialization or update. While this lock is in effect, the hybris Commerce Suite can neither be updated nor initialized. This lock therefore prevents loss of data. Lock can be activated and deactivated both from theInitialization and the Update page of the hybris Administration Console

Initialization

Initialization drops existing type definitions from the database prior to rebuilding, so the entire type system is created from scratch. So during an initialization, type system definitions are created to match the type system definition in the items.xml files

Initializing the hybris Commerce Suite

Access the Initialization Page

 1.)Open the hybris Administration Console.
2.) Go to the Platform tab and select Initialization option. The Initialization page now displays.



Click the Initialize button. This:

1.) Aborts all running cron jobs
2.) Removes all tables from the database schema. The DROP TABLE statement is used. This removes not only hybris Commerce Suite data, but all data stored in the database schema. Since hybris Commerce Suite version 5.1, during the initialization process the system no longer removes all tables. Now it only removes those tables that are declared in its current items.xml files. Old tables - orphaned data - stay intact

The init process prior to hybris Commerce Suite version 5.1 creates the type system by iterating through all extension managers and calling their methods in this order:

initializeRemoveObjects
initializeCreateTypes
initializeModifyTypes
initializeCreateObjects

3.) The init process in hybris Commerce Suite 5.1 or later prepares the ddl and dml scripts and executes them (unless dryrun is enabled, in which case the scripts are not executed). This way - the schema is prepared and persisted into db. Ddl scripts also remove tables.

4.) Clears cache and Creates a number of media folders

5.) Sets licenses

At this point the type system has been initialized. The Initialization continues with creating essential data and project data (This is optional, but enabled for all extensions by defautl). Furthermore, during the init process the hmc configuration is cleared and types are localized.

You can also initialize the system from the command line by running the command ant initialize.

Update

During an update, type system definitions are modified to match the new type system definition in the items.xml files.  
First and foremost, the update mechanism makes sure that all data that existed in the system before the update is still accessible after the update.
Therefore:
1.)Update preserves the table name, to which a type was mapped, even if it was changed in items.xml
2.)Update preserves the column name, to which an attribute was mapped, even if it was changed in items.xml
3.)Update preserves the column type for an attribute, even if it was changed in items.xml
4.)Update does not drop any tables and columns
5.)Update does not delete any item data, including type instances and type system data (composed types etc.)
6.)Update drops and recreate indices, if they are added or changed in items.xml
7.)Update does NOT change the attribute from optional to mandatory, even if it was changed in items.xml

This is what the process looks like:
  • The type system definitions from all extensions items.xml files are read in.
  • The hybris Commerce Suite type system in the database is modified according to the type definitions of all extensions items.xml files.


  1. Adding newly defined types to the type system definition in the database.Type definitions and attribute definitions that are not part of the type system definition in the database are added.
  2. Modifying existing types to match the type system definition in the database.Type definitions and attribute definitions that are changed compared to the type system definition in the database are modified.
  3. Again, like in the init process, the old update logic (prior to hybris Commerce Suite 5.1) iterates through extensions and uses extension manager methods to create and modify types. Whereas, in the new update (since hybris Commerce Suite 5.1), ddl and dml scripts are also generated here.
  4. Update then continues with creating essential and project data, optionally, if selected.