Définition et usage des vues (entity.xml) Tutorial
Tutoriel sur les vues - Création d'une vue simple
Contents |
Contexte
Ce tutoriel va avoir pour but de créer une vue simple. A partir de trois entités, nous allons réunir les données, afin d'avoir un résultat compréhensible. Ce tutoriel est basé sur la documentation que vous pourrez retrouver à cette adresse http://www.opensourcestrategies.com/ofbiz/tutorials.php Ce tutoriel permettra de visualiser le résultat directement dans l'entitymanager, par la suite, nous pourrons créer page Web qui présentera les résultats. Les entités sont : helloPerson, helloHobby, helloPersonHobby helloPerson contient une liste de personnes, helloHobby une liste de hobbies, et helloPersonHobby contient les correspondances, afin de savoir qui apprécie quelle distraction. Ces trois entités nécessitent afin de savoir qui aime quoi, de mettre en relation les entités helloPerson et helloHobby avec l'entité helloPersonHobby.
Préparation des données
Comme nous n'allons qu'effectuer l'affichage d'une vue via l'entitymanager, nous allons simplement créer les fichiers qui vont permettre à OFBiz, lors de l'initialisation de générer les entités et vues. Pour cela, nous allons créer un composant. Dans hot-deploy, création d'un répertoire nommé viewExample.
ofbiz-component.xml
Ensuite dans ce nouveau répertoire, création d'un fichier nommé ofbiz-component.xml. Nous insérons ces lignes à l'intérieur :
<ofbiz-component name="viewExample"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/ofbiz-component.xsd">
<resource-loader name="main" type="component"/>
<entity-resource type="model" reader-name="main" loader="main" location="entitydef/entitymodel.xml"/>
<entity-resource type="group" reader-name="main" loader="main" location="entitydef/entitygroup.xml"/>
<entity-resource type="data" reader-name="demo" loader="main" location="data/HobbiesData.xml"/>
<webapp name="viewTest"
title="A simple example to discover the view-entity"
server="default-server"
location="webapp/viewExample"
mount-point="/viewExample"
app-bar-display="false"/>
</ofbiz-component>
Ce fichier va dire que : un fichier entitygroup.xml est présent, un fichier entitymodel.xml est présent, un fichier de données est présent. Au chargemetn d'OFBIz, il saura ainsi où aller pour récupérer et charger les définitions des entités.
Pour continuer, création d'un répertoire entitydef. Comme vu au-dessus, nous allons y mettre les fichiers entitygroup et entitymodel.
entitygroup.xml
Dans le fichier entitygroup, nous mettons les appels aux entités et vues :
<entitygroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/entitygroup.xsd"> <entity-group group="org.ofbiz" entity="HelloPerson" /> <entity-group group="org.ofbiz" entity="HelloHobby" /> <entity-group group="org.ofbiz" entity="HelloPersonHobby" /> <entity-group group="org.ofbiz" entity="HelloPersonHobbyView" /> </entitygroup>
entitymodel.xml
Ensuite, dans le fichier entitymodel, nous allons écrire les définitions des entités et vues (structures des tables) :
<entitymodel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/entitymodel.xsd"> <title>Entity of an Open For Business Project Component</title> <entity entity-name="HelloPerson" package-name="org.ofbiz.viewExample" title="Entity for storing data about persons"> <field name="helloPersonId" type="id-ne"></field> <field name="firstName" type="id"></field> <field name="middleName" type="id"></field> <field name="lastName" type="id"></field> <field name="comments" type="comment"></field> <prim-key field="helloPersonId"/> </entity> <entity entity-name="HelloHobby" package-name="org.ofbiz.viewExample" title="Hobbies available"> <field name="helloHobbyId" type="id-ne"></field> <field name="description" type="description"></field> <prim-key field="helloHobbyId"/> </entity> <entity entity-name="HelloPersonHobby" package-name="org.ofbiz.viewExample" title="Entity and ttribute Entity"> <field name="helloPersonId" type="id-ne"></field> <field name="helloHobbyId" type="id-ne"></field> <prim-key field="helloPersonId"/> <prim-key field="helloHobbyId"/> <relation type="one" fk-name="HPRSN_PRSN" rel-entity-name="HelloPerson"> <key-map field-name="helloPersonId"/> </relation> <relation type="one" fk-name="HPRSN_HBBY" rel-entity-name="HelloHobby"> <key-map field-name="helloHobbyId"/> </relation> </entity> <view-entity entity-name="HelloPersonHobbyView" package-name="org.ofbiz.viewExample" title="List of the persons with one or more hobbies"> <member-entity entity-alias="P" entity-name="HelloPerson" /> <member-entity entity-alias="H" entity-name="HelloHobby" /> <member-entity entity-alias="PH" entity-name="HelloPersonHobby" /> <alias entity-alias="P" name="firstName"/> <alias entity-alias="P" name="lastName"/> <alias entity-alias="H" name="description"/> <view-link entity-alias="P" rel-entity-alias="PH" rel-optional="true"> <key-map field-name="helloPersonId" /> </view-link> <view-link entity-alias="PH" rel-entity-alias="H" rel-optional="true"> <key-map field-name="helloHobbyId" /> </view-link> </view-entity> </entitymodel>
HobbiesData.xml
Pour terminer, nous allons créer un répertoire data, et ajouter un fichier nommé HobbiesData.xml. Dans celui-ci, nous allons coller les lignes suivantes :
<entity-engine-xml> <HelloHobby helloHobbyId="READING" description="Reading"/> <HelloHobby helloHobbyId="MOVIES" description="Movies"/> <HelloHobby helloHobbyId="THEATER" description="The theater"/> <HelloHobby helloHobbyId="OPERA" description="The opera"/> <HelloHobby helloHobbyId="SKIING" description="Skiing"/> <HelloHobby helloHobbyId="SURFING" description="Surfing"/> <HelloHobby helloHobbyId="WINDSURFING" description="Windsurfing"/> <HelloHobby helloHobbyId="BASKETBALL" description="Basketball"/> <HelloHobby helloHobbyId="FOOTBALL" description="Football (Soccer)"/> <HelloHobby helloHobbyId="COOKING" description="Cooking"/> <HelloHobby helloHobbyId="WINE" description="Wine"/> <HelloPerson helloPersonId="1" firstName="Jean" middleName="" lastName="TALU" comments="" /> <HelloPerson helloPersonId="2" firstName="Alain" middleName="" lastName="TERIEUR" comments="" /> <HelloPerson helloPersonId="3" firstName="Alex" middleName="" lastName="TERIEUR" comments="" /> <HelloPerson helloPersonId="4" firstName="Igor" middleName="" lastName="LAVOITURE" comments="" /> <HelloPersonHobby helloPersonId="1" helloHobbyId="READING"/> <HelloPersonHobby helloPersonId="1" helloHobbyId="WINE"/> <HelloPersonHobby helloPersonId="2" helloHobbyId="SURFING"/> <HelloPersonHobby helloPersonId="3" helloHobbyId="BASKETBALL"/> <HelloPersonHobby helloPersonId="4" helloHobbyId="COOKING"/> <HelloPersonHobby helloPersonId="4" helloHobbyId="MOVIES"/> </entity-engine-xml>
La structure des répertoires sera donc celle-ci :
Exploitation
une fois que tous les fichiers sont en place, il ne reste plus qu'à lancer OFBiz. Lors du chargement, on pourra apercevoir la création des entités.
Auto-Loading component directory : [/home/erwan/workspaceNeogia/OFBiz/ofbiz.4.0/hot-deploy] XML Read 0.0080s: file:/home/erwan/workspaceNeogia/OFBiz/ofbiz.4.0/hot-deploy/viewExample/ofbiz-component.xml Loading component : [viewExample] ... XML Read 0.012s: /home/erwan/workspaceNeogia/OFBiz/ofbiz.4.0/hot-deploy/viewExample/entitydef/entitymodel.xml ... XML Read 0.0050s: /home/erwan/workspaceNeogia/OFBiz/ofbiz.4.0/hot-deploy/viewExample/entitydef/entitygroup.xml
Il reste maintenant une chose à faire avant de voir le résultat de notre vue, c'est de charger les données dans les entités. Pour cela, aller dans Adm. Sys puis dans XML Data Import. Dans Absolute Filename or URL:, mettre les chemin et nom du fichier HobbiesData.xml. Cliquer sur Import file. Si tout s'est bien passé, en bas de la page, une indication donne le nombre de lignes importées. Sinon, vérifier que le chemin du fichier soit le bon.
Affichage du résultat
Maintenant que les données sont présentes dans la table, nous allons pouvoir grâce à notre vue savoir qui aime quoi. Ce premier exemple va nous donner une simple liste avec les goûts de quelques personnes. Par la suite, il va nous être possible d'augmenter les possibilités, en ajoutant des calculs, ou alors en effectuant des regroupements.
Pour afficher notre résultat, aller dans Adm. Sys puis Entity Data Maintenance. Aller directement à la lettre H et afficher, en cliquant sur All, la vue HelloPersonHobbyView. Voici l'écran qui devrait s'afficher :




