Tuesday 28 November 2017

The Type System

Type = type definition in items.xml + its Java implementation
Item = object instance of a Type


The Item type is the supertype of all types. 

Attributes of a Composed Type

  • Can be a reference to
    • a Composed Type
    • a basic Java Type
  • Can have a localized name and description
  • Can have a default value

Creating Types

Adding New Attributes to a Type

Moving a Type (from one extension to another)
Move TypeA from ExtensionA to ExtensionB
extensiona-items.xml
<itemtype
    generate="true"
    code="TypeA"
    jaloclass="de.hybris.extensiona.jalo.TypeA"
    extends="GenericItem"
    autocreate="true" >

   <deployment table="mytype_deployment" typecode="12345"/>
   ...

</itemtype>
  1. Change the package path of jaloclass 
  2. Move the type definition to new items.xml file
  3. Build -> Start -> Update
extensionb-items.xml
<itemtype
    generate="true"
    code="TypeA"
    jaloclass="de.hybris.extensionb.jalo.TypeA"
    extends="GenericItem"
    autocreate="true" >

   <deployment table="mytype_deployment" typecode="12345"/>
   ...

</itemtype>

Limitations
  • Not allowed to change the deployment typecode
  • Cannot move if it affects the classpath
Cannot move TypeA from ExtensionA to ExtensionB.
Compilation fails in this case because the ExtensionC still needs TypeA that is used to extend TypeC.
Possible workaround would be to relate ExtensionC with ExtensionB, but it may not fit your business objectives.

Available Types
core-items.xml (/platform/ext/core/resources)

  1. AtomicTypes
  2. CollectionTypes
  3. EnumerationTypes
  4. MapTypes
  5. RelationTypes
  6. ItemTypes
AtomicTypes
Does not have a code attribute, instead class attribute is used as its reference

To localize atomic types, add the following in locales_xx.properties (resources/localization)
type.localized:<class attribute>.name=<value>
type.localized:<class attribute>.description=<value>



Create a new atomic type
    • Create a class for the atomic type. Eg: PK.java
    • The created class has to implement Serializable interface


    • Define the class in items.xml file

    CollectionTypes
    code attribute - unique identifier
    elementtype attribute - type of elements in the collection
    type attribute – List (ordered items), Set (no duplicates), SortedSet (unique ordered items)



    CollectionTypes vs. RelationTypes (Use RelationTypes whenever possible)
    • Maximum length of a database field is limited and hence the value may get truncated
    • As the database entry only contains the PKs, you cannot get more details on each PK. You would need to query again to get further details.

    EnumerationTypes

    Storage of localized values
    Localized values are stored in a separate database table, 

    whose name = name of the table the type is stored in + the suffix lp (localized property)
    E.g.; If the type is stored in the table sampletype, then its localized values are stored in the table sampletypelp.
     

    MapTypes
    argumenttype attribute - Key
    returntype attribute - Value

    RelationTypes
    Internally, the elements on both sides of the relation are linked together via instances of a helper type called LinkItem.
    LinkItems hold two attributes, SourceItem and TargetItem that hold references to the respective items.


    ItemTypes
    ItemTypes/ComposedTypes hold meta information on types and the types' attributes and relations, including the item type's code (unique identifier), its JNDI deployment location, the database table the item will be stored in and the type's Java class. 

    Every type may have any number of attributes. 


    Every attribute that is inherited downwards has its settings stored separately for each children type. That way, it is possible to override attribute access rights inherited from the supertype for a child type, so that you may set an attribute to be writable for your self-defined types that wasn't set writable on the type the attribute was originally defined with.



    Creating Items at runtime

    Creating Items in the Extension's Manager
    Every extension has a Manager that is responsible for item handling. By calling the Manager's creation methods and passing the necessary parameters for the new item as a Map, you can create items.

    ...
    Map params = new HashMap();
    params.put( HelloWorldWizardCronJob.SCREENTEXT, getScreenText() );
    params.put( HelloWorldWizardCronJob.ACTIVATE, isActivate() );
    params.put( HelloWorldWizardCronJob.INTERVAL, getInterval() );
    params.put( HelloWorldWizardCronJob.CODE, "HelloWorldWizardCronJob" + String.valueOf( jobNum ) );
    params.put( HelloWorldWizardCronJob.JOB, hwwj );
    HelloWorldWizardCronJob hwwcj = HelloWorldWizardManager.getInstance().createHelloWorldWizardCronJob( params );
    ...

    Creating Items Generically
    To create a new instance of a certain type,

    • Select the respective ComposedType and
    ComposedType item = getSession().getTypeManager().getComposedType("mySampleItem");
    • Call its newInstance() method
    item.newInstance(ctx, params);
    Pass the necessary parameters for the new item as a Map

    initial modifier
    To set an attribute to be initial: Set the initial modifier in items.xml to true
    <attributes>
       <attribute qualifier="code" type="java.lang.String">
          <modifiers initial="true"/>
          <persistence type="property"/>
       </attribute>
    </attributes>

    Checking the Mandatory Item Attributes
    Inside the createItem() method, we can use checkMandatoryAttribute() method to check if each mandatory attribute has a value.

    checkMandatoryAttribute(MyType.MYATTRIBUTE1, allAttributes, missing, true);
    checkMandatoryAttribute(MyType.MYATTRIBUTE2, allAttributes, missing, false);
    This method has four parameters:
    • String qualifier - Reference to the attribute to check
    • ItemAttributeMap allAttributes - Map with the initial attribute values
    • Set missingSet - Set which accepts references to all attributes for which no value has been set
    • Boolean nullAllowed - Specifies whether a null value in the ItemAttributeMap is written to the item as a null value (true) or whether the null value is treated as a missing value (false). Defaults to false.

    Relations

    Redeclaring 1:n Relations (redeclare=true)
    Define a relation on an abstract level (between two abstract classes), and then re-declare this relation in concrete classes to point to concrete subclasses.

    Steps:
    • Define a relation between two abstract classes
    • Redeclare the definition of Order item such that it contains only OrderEntries
    • Add OrderEntry CollectionType
    • Redeclare the definition of OrderEntry item

    Custom Ordering
    Custom property: ordering.attribute
    To specify which attribute will be used to order the many-side items when retrieving from the database. 

    Defined the many-side as ordered=false. There is no need for the ORM to add an additional ordering column, therefore the many side is ordered=false. 



    Condition Query
    Custom property: condition.query
    Holds a string that is added to the where part of the select query generated for a one-to-many or many-to-one relation.
    • Shouldn't contain any order by part as it is added at the end of a generated query.
    • Only for relations of a one-to-many or many-to-one type.
    • Can only be defined in one end of the relation. Must be defined in either sourceElement or targetElement that have the many cardinality.

    No comments:

    Post a Comment